257 lines
8.6 KiB
C++
Executable File
257 lines
8.6 KiB
C++
Executable File
#include "Game.h"
|
||
#include "AnimatedModel.h"
|
||
#include "BoneAnimatedModel.h"
|
||
#include "Utils.h"
|
||
#include "OpenGlExtensions.h"
|
||
#include <iostream>
|
||
#include "TextureManager.h"
|
||
#include "TextModel.h"
|
||
|
||
namespace ZL
|
||
{
|
||
const char* CONST_ZIP_FILE = "";
|
||
|
||
Game::Game()
|
||
: window(nullptr)
|
||
, glContext(nullptr)
|
||
, newTickCount(0)
|
||
, lastTickCount(0)
|
||
{
|
||
}
|
||
|
||
Game::~Game() {
|
||
if (glContext) {
|
||
SDL_GL_DeleteContext(glContext);
|
||
}
|
||
if (window) {
|
||
SDL_DestroyWindow(window);
|
||
}
|
||
SDL_Quit();
|
||
}
|
||
|
||
void Game::setup() {
|
||
glContext = SDL_GL_CreateContext(ZL::Environment::window);
|
||
|
||
ZL::BindOpenGlFunctions();
|
||
ZL::CheckGlError();
|
||
|
||
// Initialize renderer
|
||
|
||
#ifdef EMSCRIPTEN
|
||
renderer.shaderManager.AddShaderFromFiles("default", "./shaders/default.vertex", "./shaders/default_web.fragment", CONST_ZIP_FILE);
|
||
renderer.shaderManager.AddShaderFromFiles("defaultColor", "./shaders/defaultColor.vertex", "./shaders/defaultColor_web.fragment", CONST_ZIP_FILE);
|
||
#else
|
||
renderer.shaderManager.AddShaderFromFiles("default", "./shaders/default.vertex", "./shaders/default_desktop.fragment", CONST_ZIP_FILE);
|
||
renderer.shaderManager.AddShaderFromFiles("defaultColor", "./shaders/defaultColor.vertex", "./shaders/defaultColor_desktop.fragment", CONST_ZIP_FILE);
|
||
#endif
|
||
renderer.shaderManager.AddShaderFromFiles("env", "./shaders/env.vertex", "./shaders/env.fragment", CONST_ZIP_FILE);
|
||
|
||
|
||
// std::array<std::string, 6> cubemapTextureNightStr = {
|
||
//"../resources/sky/space_rt.bmp", "../resources/sky/space_lf.bmp", "../resources/sky/space_up.bmp", "../resources/sky/space_dn.bmp", "../resources/sky/space_bk.bmp", "../resources/sky/space_ft.bmp"
|
||
// };
|
||
cubemapTexture = std::make_shared<Texture>(
|
||
std::array<TextureDataStruct, 6>{
|
||
CreateTextureDataFromBmp24("./resources/sky/space_rt.bmp"),
|
||
CreateTextureDataFromBmp24("./resources/sky/space_lf.bmp"),
|
||
CreateTextureDataFromBmp24("./resources/sky/space_up.bmp"),
|
||
CreateTextureDataFromBmp24("./resources/sky/space_dn.bmp"),
|
||
CreateTextureDataFromBmp24("./resources/sky/space_bk.bmp"),
|
||
CreateTextureDataFromBmp24("./resources/sky/space_ft.bmp")
|
||
});
|
||
|
||
|
||
cubemap.data = ZL::CreateCubemap(500);
|
||
cubemap.RefreshVBO();
|
||
|
||
//Load texture
|
||
spaceshipTexture = std::make_unique<Texture>(CreateTextureDataFromPng("./resources/DefaultMaterial_BaseColor.png"));
|
||
spaceshipBase = LoadFromTextFile02("./resources/spaceship005.txt");
|
||
spaceshipBase.RotateByMatrix(QuatToMatrix(QuatFromRotateAroundY(M_PI / 2.0)));
|
||
spaceshipBase.Move(Vector3f{ -0.52998, -13, 0 });
|
||
|
||
spaceship.AssignFrom(spaceshipBase);
|
||
spaceship.RefreshVBO();
|
||
|
||
|
||
renderer.InitOpenGL();
|
||
|
||
}
|
||
|
||
void Game::drawScene() {
|
||
static const std::string defaultShaderName = "default";
|
||
static const std::string envShaderName = "env";
|
||
|
||
static const std::string vPositionName = "vPosition";
|
||
static const std::string vTexCoordName = "vTexCoord";
|
||
static const std::string textureUniformName = "Texture";
|
||
|
||
glClearColor(0.0f, 0.5f, 1.0f, 1.0f);
|
||
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
||
|
||
glViewport(0, 0, Environment::width, Environment::height);
|
||
|
||
CheckGlError();
|
||
|
||
renderer.shaderManager.PushShader(envShaderName);
|
||
renderer.RenderUniform1i(textureUniformName, 0);
|
||
renderer.EnableVertexAttribArray(vPositionName);
|
||
renderer.PushPerspectiveProjectionMatrix(1.0 / 1.5,
|
||
static_cast<float>(Environment::width) / static_cast<float>(Environment::height),
|
||
1, 1000);
|
||
renderer.PushMatrix();
|
||
renderer.LoadIdentity();
|
||
renderer.RotateMatrix(Environment::shipMatrix);
|
||
|
||
CheckGlError();
|
||
|
||
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture->getTexID());
|
||
renderer.DrawVertexRenderStruct(cubemap);
|
||
|
||
CheckGlError();
|
||
|
||
|
||
renderer.PopMatrix();
|
||
renderer.PopProjectionMatrix();
|
||
renderer.DisableVertexAttribArray(vPositionName);
|
||
|
||
renderer.shaderManager.PopShader();
|
||
CheckGlError();
|
||
|
||
|
||
renderer.shaderManager.PushShader(defaultShaderName);
|
||
renderer.RenderUniform1i(textureUniformName, 0);
|
||
renderer.EnableVertexAttribArray(vPositionName);
|
||
renderer.EnableVertexAttribArray(vTexCoordName);
|
||
|
||
renderer.PushPerspectiveProjectionMatrix(1.0 / 1.5,
|
||
static_cast<float>(Environment::width) / static_cast<float>(Environment::height),
|
||
1, 1000);
|
||
renderer.PushMatrix();
|
||
|
||
renderer.LoadIdentity();
|
||
//renderer.RotateMatrix(Environment::shipMatrix);
|
||
renderer.TranslateMatrix({ 0,0, -1.0f * Environment::zoom });
|
||
|
||
|
||
glBindTexture(GL_TEXTURE_2D, spaceshipTexture->getTexID());
|
||
renderer.DrawVertexRenderStruct(spaceship);
|
||
|
||
renderer.PopMatrix();
|
||
renderer.PopProjectionMatrix();
|
||
renderer.DisableVertexAttribArray(vPositionName);
|
||
renderer.DisableVertexAttribArray(vTexCoordName);
|
||
|
||
renderer.shaderManager.PopShader();
|
||
|
||
CheckGlError();
|
||
}
|
||
|
||
void Game::processTickCount() {
|
||
|
||
if (lastTickCount == 0) {
|
||
lastTickCount = SDL_GetTicks64();
|
||
return;
|
||
}
|
||
|
||
newTickCount = SDL_GetTicks64();
|
||
if (newTickCount - lastTickCount > CONST_TIMER_INTERVAL) {
|
||
size_t delta = (newTickCount - lastTickCount > CONST_MAX_TIME_INTERVAL) ?
|
||
CONST_MAX_TIME_INTERVAL : newTickCount - lastTickCount;
|
||
|
||
//gameObjects.updateScene(delta);
|
||
|
||
if (Environment::tapDownHold) {
|
||
|
||
float diffx = Environment::tapDownCurrentPos.v[0] - Environment::tapDownStartPos.v[0];
|
||
float diffy = Environment::tapDownCurrentPos.v[1] - Environment::tapDownStartPos.v[1];
|
||
|
||
|
||
if (abs(diffy) > 5.0 || abs(diffx) > 5.0) //threshold
|
||
{
|
||
|
||
float rotationPower = sqrtf(diffx * diffx + diffy * diffy);
|
||
|
||
std::cout << rotationPower << std::endl;
|
||
|
||
float deltaAlpha = rotationPower * delta * M_PI / 500000.f;
|
||
|
||
Vector3f rotationDirection = { -diffy, -diffx, 0 };
|
||
|
||
rotationDirection = rotationDirection.normalized();
|
||
|
||
Vector4f rotateQuat = {
|
||
rotationDirection.v[0] * sin(deltaAlpha * 0.5f),
|
||
rotationDirection.v[1] * sin(deltaAlpha * 0.5f),
|
||
rotationDirection.v[2] * sin(deltaAlpha * 0.5f),
|
||
cos(deltaAlpha * 0.5f) };
|
||
|
||
Matrix3f rotateMat = QuatToMatrix(rotateQuat);
|
||
|
||
Environment::shipMatrix = MultMatrixMatrix(rotateMat, Environment::shipMatrix);
|
||
|
||
}
|
||
}
|
||
|
||
lastTickCount = newTickCount;
|
||
}
|
||
}
|
||
|
||
void Game::render() {
|
||
SDL_GL_MakeCurrent(ZL::Environment::window, glContext);
|
||
ZL::CheckGlError();
|
||
|
||
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
|
||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||
|
||
drawScene();
|
||
processTickCount();
|
||
|
||
SDL_GL_SwapWindow(ZL::Environment::window);
|
||
}
|
||
void Game::update() {
|
||
SDL_Event event;
|
||
while (SDL_PollEvent(&event)) {
|
||
if (event.type == SDL_QUIT) {
|
||
Environment::exitGameLoop = true;
|
||
|
||
}
|
||
else if (event.type == SDL_MOUSEBUTTONDOWN) {
|
||
// 1. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
||
Environment::tapDownHold = true;
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
Environment::tapDownStartPos.v[0] = event.button.x;
|
||
Environment::tapDownStartPos.v[1] = event.button.y;
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
Environment::tapDownCurrentPos.v[0] = event.button.x;
|
||
Environment::tapDownCurrentPos.v[1] = event.button.y;
|
||
}
|
||
else if (event.type == SDL_MOUSEBUTTONUP) {
|
||
// 2. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
||
Environment::tapDownHold = false;
|
||
}
|
||
else if (event.type == SDL_MOUSEMOTION) {
|
||
// 3. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
||
if (Environment::tapDownHold) {
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
Environment::tapDownCurrentPos.v[0] = event.motion.x;
|
||
Environment::tapDownCurrentPos.v[1] = event.motion.y;
|
||
}
|
||
}
|
||
else if (event.type == SDL_MOUSEWHEEL) {
|
||
static const float zoomstep = 2.0f;
|
||
if (event.wheel.y > 0) {
|
||
Environment::zoom -= zoomstep;
|
||
}
|
||
else if (event.wheel.y < 0) {
|
||
Environment::zoom += zoomstep;
|
||
}
|
||
if (Environment::zoom < zoomstep) {
|
||
Environment::zoom = zoomstep;
|
||
}
|
||
}
|
||
}
|
||
render();
|
||
}
|
||
|
||
} // namespace ZL
|