#include #include #include #include #include #include #include "common/utils/Types.h" #include "client/GameClient.h" #include "client/input/Keys.h" #include "client/input/MouseButtons.h" #include "client/rendering/Shader.h" #include "client/rendering/FontRenderer.h" #include "client/rendering/Mesh.h" #include "client/rendering/Framebuffer.h" #include "client/Utils.h" #include "client/math/Camera.h" #include "client/math/Matrix.h" #include "client/math/MatrixStack.h" #include "client/Game.h" #include "rendering/NoiseTexture.h" struct InternGameClient { ~InternGameClient() { if(window != nullptr) { glfwDestroyWindow(window); } if(glfwInitDone) { glfwTerminate(); } } bool glfwInitDone = false; GLFWwindow* window = nullptr; }; static const u64 NANOS_PER_TICK = 50000000; static const float lagFactor = 1.0f / NANOS_PER_TICK; static u64 timeFactor = 1; static int width = 0; static int height = 0; static bool resize = false; static float fovY = 60.0f; static float nearClip = 0.1f; static float farClip = 1000.0f; static InternGameClient client; static Keys keys; static MouseButtons mButtons; static bool useSSAO = false; struct Shaders { Shaders() : world("resources/shader/worldVertex.vs", "resources/shader/worldFragment.fs"), ssao("resources/shader/ssaoVertex.vs", "resources/shader/ssaoFragment.fs"), ssaoBlur("resources/shader/ssaoBlurVertex.vs", "resources/shader/ssaoBlurFragment.fs"), shadow("resources/shader/worldShadowVertex.vs", "resources/shader/worldShadowFragment.fs"), postWorld("resources/shader/worldPostVertex.vs", "resources/shader/worldPostFragment.fs"), text("resources/shader/textVertex.vs", "resources/shader/textFragment.fs") { } Shader world; Shader ssao; Shader ssaoBlur; Shader shadow; Shader postWorld; Shader text; bool once = true; float worldProj[16] = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0, 0.0f }; float worldView[16]= { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; float worldShadowProj[16] = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0, 0.0f }; float worldShadowView[16]= { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; bool isValid() const { return world.isValid() && ssao.isValid() && ssaoBlur.isValid() && shadow.isValid() && postWorld.isValid() && text.isValid(); } void updateWorldProjection() { float tan = tanf((0.5f * fovY) * M_PI / 180.0f); float q = 1.0f / tan; float aspect = (float) width / height; worldProj[0] = q / aspect; worldProj[5] = q; worldProj[10] = (nearClip + farClip) / (nearClip - farClip); worldProj[14] = (2.0f * nearClip * farClip) / (nearClip - farClip); if(once) { worldShadowProj[0] = q / aspect; worldShadowProj[5] = q; worldShadowProj[10] = (nearClip + farClip) / (nearClip - farClip); worldShadowProj[14] = (2.0f * nearClip * farClip) / (nearClip - farClip); } } void updateWorldView(float lag, Camera& cam) { cam.update(lag); const Vector right = cam.getRight(); const Vector up = cam.getUp(); const Vector back = cam.getBack(); const Vector pos = cam.getPosition(); worldView[0] = right.getX(); worldView[1] = up.getX(); worldView[2] = back.getX(); worldView[4] = right.getY(); worldView[5] = up.getY(); worldView[6] = back.getY(); worldView[8] = right.getZ(); worldView[9] = up.getZ(); worldView[10] = back.getZ(); worldView[12] = right.dotInverse(pos); worldView[13] = up.dotInverse(pos); worldView[14] = back.dotInverse(pos); if(once) { once = false; worldShadowView[0] = right.getX(); worldShadowView[1] = up.getX(); worldShadowView[2] = back.getX(); worldShadowView[4] = right.getY(); worldShadowView[5] = up.getY(); worldShadowView[6] = back.getY(); worldShadowView[8] = right.getZ(); worldShadowView[9] = up.getZ(); worldShadowView[10] = back.getZ(); worldShadowView[12] = right.dotInverse(pos); worldShadowView[13] = up.dotInverse(pos); worldShadowView[14] = back.dotInverse(pos); } } }; struct Framebuffers { Framebuffers(u32 w, u32 h) : world(w, h, Framebuffer::POSITION | Framebuffer::NORMAL | Framebuffer::COLOR | Framebuffer::RED | Framebuffer::DEPTH24_STENCIL8), ssao(w, h, Framebuffer::RED), ssaoBlur(w, h, Framebuffer::RED), shadow(w, h, Framebuffer::DEPTH24_STENCIL8) { } void resize(u32 w, u32 h) const { world.resize(w, h); ssao.resize(w, h); ssaoBlur.resize(w, h); shadow.resize(w, h); } bool isValid() const { return world.isValid() && ssao.isValid() && ssaoBlur.isValid(); } Framebuffer world; Framebuffer ssao; Framebuffer ssaoBlur; Framebuffer shadow; }; struct InternGame { InternGame() : ssaoNoise(4, 4) { rectangle.add({-1, -1, 0, 0, 0, 0, 0, 0}); rectangle.add({ 1, 1, 0, 1, 1, 0, 0, 0}); rectangle.add({-1, 1, 0, 0, 1, 0, 0, 0}); rectangle.add({-1, -1, 0, 0, 0, 0, 0, 0}); rectangle.add({ 1, -1, 0, 1, 0, 0, 0, 0}); rectangle.add({ 1, 1, 0, 1, 1, 0, 0, 0}); rectangle.build(); } Game game; Camera cam; MatrixStack model; FontRenderer fontRenderer; NoiseTexture ssaoNoise; Mesh rectangle; }; static u64 getTimeNanos() { return glfwGetTimerValue() * timeFactor; } static bool initGLFW() { client.glfwInitDone = glfwInit(); if(!client.glfwInitDone) { std::cout << "could not initialize GLFW\n"; return true; } timeFactor = 1000000000 / glfwGetTimerFrequency(); return false; } static bool initWindow(int w, int h, const char* windowName) { width = w; height = h; glfwDefaultWindowHints(); glfwWindowHint(GLFW_VISIBLE, 0); glfwWindowHint(GLFW_RESIZABLE, 1); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); client.window = glfwCreateWindow(width, height, windowName, nullptr, nullptr); if(client.window == nullptr) { std::cout << "could not create window\n"; return true; } glfwMakeContextCurrent(client.window); glfwSwapInterval(1); return false; } static bool initGLEW() { GLenum err = glewInit(); if(err != GLEW_OK) { std::cout << "could not initialize GLEW: " << glewGetErrorString(err) << "\n"; return true; } std::cout << "using GLEW " << glewGetString(GLEW_VERSION) << "\n"; return false; } static void initCallbacks() { // GLFWwindow* w, int key, int scancode, int action, int mod glfwSetKeyCallback(client.window, [](GLFWwindow*, int key, int, int action, int) { if(action == GLFW_PRESS) { keys.press(key); } else if(action == GLFW_RELEASE) { keys.release(key); } }); // GLFWwindow* w, int button, int action, int mods glfwSetMouseButtonCallback(client.window, [](GLFWwindow*, int button, int action, int) { if(action == GLFW_PRESS) { mButtons.press(button); } else if(action == GLFW_RELEASE) { mButtons.release(button); } }); // GLFWwindow* w, double xpos, double ypos glfwSetCursorPosCallback(client.window, [](GLFWwindow*, double x, double y) { mButtons.move(x, y); }); // GLFWwindow* w, int width, int height glfwSetFramebufferSizeCallback(client.window, [](GLFWwindow*, int w, int h) { glViewport(0, 0, w, h); width = w; height = h; resize = true; }); } static void tick(InternGame& game) { keys.tick(); mButtons.tick(); game.game.tick(keys, mButtons, game.cam); if(keys.test.getDownTime() == 1) { useSSAO = !useSSAO; } mButtons.postTick(); } static void renderShadow(float lag, Shaders& shaders, InternGame& game, Framebuffers& fb) { fb.shadow.bind(); glEnable(GL_DEPTH_TEST); shaders.shadow.use(); shaders.shadow.setMatrix("proj", shaders.worldShadowProj); shaders.shadow.setMatrix("view", shaders.worldShadowView); glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset(2.0f, 4.0f); game.game.renderWorld(lag, game.model, shaders.shadow); glDisable(GL_POLYGON_OFFSET_FILL); } static void renderWorld(float lag, Shaders& shaders, InternGame& game, Framebuffers& fb) { fb.world.bind(); glEnable(GL_DEPTH_TEST); shaders.world.use(); shaders.world.setMatrix("projShadow", shaders.worldShadowProj); shaders.world.setMatrix("viewShadow", shaders.worldShadowView); shaders.world.setMatrix("proj", shaders.worldProj); shaders.world.setMatrix("view", shaders.worldView); game.model.clear(); shaders.world.setMatrix("model", game.model.get().getValues()); fb.shadow.bindDepthTexture(1); game.game.renderWorld(lag, game.model, shaders.world); } static void renderSSAO(Shaders& shaders, InternGame& game, Framebuffers& fb) { // ssao shaders.ssao.use(); shaders.ssao.setMatrix("view", shaders.worldView); shaders.ssao.setMatrix("proj", shaders.worldProj); shaders.ssao.setInt("width", width); shaders.ssao.setInt("height", height); fb.world.bindPositionTexture(0); fb.world.bindNormalTexture(1); fb.world.bindColorTexture(2); fb.world.bindDepthTexture(3); game.ssaoNoise.bind(4); fb.ssao.bind(); game.rectangle.draw(); // ssao blur shaders.ssaoBlur.use(); fb.ssao.bindRedTexture(0); fb.ssaoBlur.bind(); game.rectangle.draw(); } static void renderPostWorld(Shaders& shaders, InternGame& game, Framebuffers& fb) { glBindFramebuffer(GL_FRAMEBUFFER, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); shaders.postWorld.use(); fb.world.bindColorTexture(0); fb.ssaoBlur.bindRedTexture(1); fb.world.bindRedTexture(2); shaders.postWorld.setInt("useSSAO", useSSAO); game.rectangle.draw(); } static void renderTextOverlay(float lag, Shaders& shaders, InternGame& game) { glDisable(GL_DEPTH_TEST); shaders.text.use(); Matrix m; shaders.text.setMatrix("proj", m.getValues()); m.translate(-1.0f, 1.0f, 0.0f); m.scale(2.0f / width, -2.0f / height, 1.0f); shaders.text.setMatrix("view", m.getValues()); game.model.clear(); game.model.get().scale(2.0f, 2.0f, 2.0f); shaders.text.setMatrix("model", game.model.get().getValues()); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendEquation(GL_FUNC_ADD); game.game.renderTextOverlay(lag, game.model, shaders.text, game.fontRenderer); glDisable(GL_BLEND); } static void renderTick(float lag, Shaders& shaders, InternGame& game, Framebuffers& fb) { if(resize) { fb.resize(width, height); resize = false; } shaders.updateWorldProjection(); shaders.updateWorldView(lag, game.cam); renderShadow(lag, shaders, game, fb); renderWorld(lag, shaders, game, fb); if(useSSAO) { renderSSAO(shaders, game, fb); } renderPostWorld(shaders, game, fb); renderTextOverlay(lag, shaders, game); } static void loop() { Shaders shaders; if(!shaders.isValid()) { return; } Framebuffers fb(width, height); if(!fb.isValid()) { return; } InternGame game; u64 lastTime = getTimeNanos(); u64 lag = 0; while(!glfwWindowShouldClose(client.window)) { renderTick(lag * lagFactor, shaders, game, fb); glfwSwapBuffers(client.window); u64 newTime = getTimeNanos(); lag += newTime - lastTime; lastTime = newTime; while(lag >= NANOS_PER_TICK) { lag -= NANOS_PER_TICK; tick(game); } glfwPollEvents(); } } void GameClient::start(int w, int h, const char* windowName) { if(initGLFW() || initWindow(w, h, windowName) || initGLEW()) { return; } initCallbacks(); glfwShowWindow(client.window); loop(); }