123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582 |
- #include <iostream>
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include <unordered_map>
- #include <cmath>
- #include <cstring>
- #include <array>
- #include <cfloat>
- #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/Plane.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 = 80.0f;
- static InternGameClient client;
- static Keys keys;
- static MouseButtons mButtons;
- static bool useSSAO = false;
- static float testRadius = 0.0005;
- static float testBias = 0.000025;
- static bool ortho = false;
- static Vector testOrthoCenter;
- 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") {
- worldProj.set(11, -1.0f);
- worldProj.set(15, 0.0f);
- }
- Shader world;
- Shader ssao;
- Shader ssaoBlur;
- Shader shadow;
- Shader postWorld;
- Shader text;
- bool once = true;
- Matrix worldProj;
- Matrix worldView;
- Matrix worldShadowProj;
- Matrix worldShadowView;
- Matrix worldShadowProjView;
- bool isValid() const {
- return world.isValid() && ssao.isValid() && ssaoBlur.isValid() &&
- shadow.isValid() && postWorld.isValid() && text.isValid();
- }
- void updateWorldProjection(const Camera& cam) {
- float tan = tanf((0.5f * fovY) * M_PI / 180.0f);
- float q = 1.0f / tan;
- float aspect = (float) width / height;
- worldProj.setToIdentity();
- worldProj.set(11, -1.0f);
- worldProj.set(15, 0.0f);
- worldProj.set(0, q / aspect);
- worldProj.set(5, q);
- worldProj.set(10, (nearClip + farClip) / (nearClip - farClip));
- worldProj.set(14, (2.0f * nearClip * farClip) / (nearClip - farClip));
- // http://cgvr.informatik.uni-bremen.de/teaching/cg_literatur/lighthouse3d_view_frustum_culling/index.html
- float closeFarClip = 4;
- float nearHigh = tan * nearClip;
- float nearWidth = nearHigh * aspect;
- float farHigh = tan * closeFarClip;
- float farWidth = farHigh * aspect;
- Vector farCenter = cam.getPosition();
- farCenter.addMul(cam.getFront(), closeFarClip);
- Vector farTopLeft = farCenter;
- farTopLeft.addMul(cam.getLeft(), farWidth);
- farTopLeft.addMul(cam.getUp(), farHigh);
- Vector farBottomLeft = farCenter;
- farBottomLeft.addMul(cam.getLeft(), farWidth);
- farBottomLeft.addMul(cam.getDown(), farHigh);
- Vector farTopRight = farCenter;
- farTopRight.addMul(cam.getRight(), farWidth);
- farTopRight.addMul(cam.getUp(), farHigh);
- Vector farBottomRight = farCenter;
- farBottomRight.addMul(cam.getRight(), farWidth);
- farBottomRight.addMul(cam.getDown(), farHigh);
- Vector nearCenter = cam.getPosition();
- nearCenter.addMul(cam.getFront(), nearClip);
- Vector nearTopLeft = nearCenter;
- nearTopLeft.addMul(cam.getLeft(), nearWidth);
- nearTopLeft.addMul(cam.getUp(), nearHigh);
- Vector nearBottomLeft = nearCenter;
- nearBottomLeft.addMul(cam.getLeft(), nearWidth);
- nearBottomLeft.addMul(cam.getDown(), nearHigh);
- Vector nearTopRight = nearCenter;
- nearTopRight.addMul(cam.getRight(), nearWidth);
- nearTopRight.addMul(cam.getUp(), nearHigh);
- Vector nearBottomRight = nearCenter;
- nearBottomRight.addMul(cam.getRight(), nearWidth);
- nearBottomRight.addMul(cam.getDown(), nearHigh);
- Vector light(-0.280166, -0.573576, -0.769751);
- Vector lightLeft = light;
- lightLeft.cross(0.0f, 1.0f, 0.0f);
- Vector lightUp = lightLeft;
- lightUp.cross(light);
-
- //std::cout << "-------------------------\n";
- Plane plane;
- plane.set(Vector(), light, lightUp);
- float f[8];
- f[0] = plane.getSignedDistance(farTopLeft);
- f[1] = plane.getSignedDistance(farBottomLeft);
- f[2] = plane.getSignedDistance(farTopRight);
- f[3] = plane.getSignedDistance(farBottomRight);
- f[4] = plane.getSignedDistance(nearTopLeft);
- f[5] = plane.getSignedDistance(nearBottomLeft);
- f[6] = plane.getSignedDistance(nearTopRight);
- f[7] = plane.getSignedDistance(nearBottomRight);
- float min = FLT_MAX;
- float max = -FLT_MAX;
- for(uint i = 0; i < 8; i++) {
- if(f[i] < min) {
- min = f[i];
- }
- if(f[i] > max) {
- max = f[i];
- }
- }
- float lightWidth = max - min;
- //std::cout << lightWidth << "\n";
-
- plane.set(Vector(), light, lightLeft);
- f[0] = plane.getSignedDistance(farTopLeft);
- f[1] = plane.getSignedDistance(farBottomLeft);
- f[2] = plane.getSignedDistance(farTopRight);
- f[3] = plane.getSignedDistance(farBottomRight);
- f[4] = plane.getSignedDistance(nearTopLeft);
- f[5] = plane.getSignedDistance(nearBottomLeft);
- f[6] = plane.getSignedDistance(nearTopRight);
- f[7] = plane.getSignedDistance(nearBottomRight);
-
- min = FLT_MAX;
- max = -FLT_MAX;
- for(uint i = 0; i < 8; i++) {
- if(f[i] < min) {
- min = f[i];
- }
- if(f[i] > max) {
- max = f[i];
- }
- }
- float lightHeight = max - min;
- //std::cout << "\n" << max << " - " << min << " " << lightHeight << "\n";
-
- // not the real center, but good guess
- testOrthoCenter = nearCenter;
- testOrthoCenter.addMul(cam.getFront(), closeFarClip * 0.5f);
- if(ortho) {
- worldProj.setToIdentity();
- worldProj.set(0, 2.0f / lightWidth);
- worldProj.set(5, 2.0f / lightHeight);
- worldProj.set(10, -2.0f / (farClip - nearClip));
- }
- if(once) {
- worldShadowProj.setToIdentity();
- worldShadowProj.set(0, 2.0f / lightWidth);
- worldShadowProj.set(5, 2.0f / lightHeight);
- worldShadowProj.set(10, -2.0f / (farClip - nearClip));
- }
- }
- void updateWorldView(Camera& cam) {
- Vector right = cam.getRight();
- Vector up = cam.getUp();
- Vector back = cam.getBack();
- Vector pos = cam.getPosition();
- if(ortho) {
- right.set(0.939693f, 0.0f, -0.34202f);
- back.set(0.280166f, 0.573576f, 0.769751f);
- up.set(-0.196175f, 0.819152f, -0.538986f);
- pos = testOrthoCenter;
- }
- worldView.set(0, right.getX());
- worldView.set(1, up.getX());
- worldView.set(2, back.getX());
- worldView.set(4, right.getY());
- worldView.set(5, up.getY());
- worldView.set(6, back.getY());
- worldView.set(8, right.getZ());
- worldView.set(9, up.getZ());
- worldView.set(10, back.getZ());
- worldView.set(12, right.dotInverse(pos));
- worldView.set(13, up.dotInverse(pos));
- worldView.set(14, back.dotInverse(pos));
- //if(once) {
- // lengthAngle = 20; widthAngle = 35;
- right.set(0.939693f, 0.0f, -0.34202f);
- back.set(0.280166f, 0.573576f, 0.769751f);
- up.set(-0.196175f, 0.819152f, -0.538986f);
- pos = testOrthoCenter;
- //once = false;
- worldShadowView.set(0, right.getX());
- worldShadowView.set(1, up.getX());
- worldShadowView.set(2, back.getX());
- worldShadowView.set(4, right.getY());
- worldShadowView.set(5, up.getY());
- worldShadowView.set(6, back.getY());
- worldShadowView.set(8, right.getZ());
- worldShadowView.set(9, up.getZ());
- worldShadowView.set(10, back.getZ());
- worldShadowView.set(12, right.dotInverse(pos));
- worldShadowView.set(13, up.dotInverse(pos));
- worldShadowView.set(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, 0),
- ssao(w, h, Framebuffer::RED, 0),
- ssaoBlur(w, h, Framebuffer::RED, 0),
- shadow(w, h, Framebuffer::DEPTH24_STENCIL8, 0 /*GL_LEQUAL*/) {
- }
- 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.test5.getDownTime() == 1) {
- ortho = !ortho;
- }
- if(keys.test.isDown()) {
- //ortho = !ortho;
- testRadius /= 0.95f;
- }
- if(keys.test2.isDown()) {
- testRadius *= 0.95f;
- }
- if(keys.test3.isDown()) {
- testBias /= 0.95f;
- }
- if(keys.test4.isDown()) {
- testBias *= 0.95f;
- }
- mButtons.postTick();
- }
- static void renderShadow(float lag, Shaders& shaders, InternGame& game, Framebuffers& fb) {
- fb.shadow.bind();
- glEnable(GL_DEPTH_TEST);
- shaders.shadow.use();
- shaders.worldShadowProjView = shaders.worldShadowProj;
- shaders.worldShadowProjView.mul(shaders.worldShadowView);
- shaders.shadow.setMatrix("projView", shaders.worldShadowProjView.getValues());
- //glEnable(GL_CULL_FACE);
- //glCullFace(GL_FRONT);
- game.game.renderWorld(lag, game.model, shaders.shadow);
- //glCullFace(GL_BACK);
- }
- static void renderWorld(float lag, Shaders& shaders, InternGame& game, Framebuffers& fb) {
- fb.world.bind();
- glEnable(GL_DEPTH_TEST);
- shaders.world.use();
- Matrix rWorldShadowProjView;
- rWorldShadowProjView.translate(0.5f, 0.5f, 0.5f);
- rWorldShadowProjView.scale(0.5f, 0.5f, 0.5f);
- rWorldShadowProjView.mul(shaders.worldShadowProjView);
- shaders.world.setMatrix("projViewShadow", rWorldShadowProjView.getValues());
- shaders.world.setMatrix("proj", shaders.worldProj.getValues());
- shaders.world.setMatrix("view", shaders.worldView.getValues());
- game.model.clear();
- shaders.world.setMatrix("model", game.model.get().getValues());
- fb.shadow.bindDepthTexture(1);
- shaders.world.setFloat("radius", testRadius);
- shaders.world.setFloat("bias", testBias);
- game.game.renderWorld(lag, game.model, shaders.world);
- }
- static void renderSSAO(Shaders& shaders, InternGame& game, Framebuffers& fb) {
- // ssao
- shaders.ssao.use();
- Matrix rProj;
- rProj.translate(0.5f, 0.5f, 0.5f);
- rProj.scale(0.5f, 0.5f, 0.5f);
- rProj.mul(shaders.worldProj);
- shaders.ssao.setMatrix("proj", rProj.getValues());
- 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);
- fb.world.bindNormalTexture(3);
- shaders.postWorld.setInt("useSSAO", useSSAO);
- game.rectangle.draw();
- }
- static u64 last = 0;
- static u64 sum = 0;
- static u64 tindex = 0;
- static std::array<u64, 128> values;
- 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);
- char buffer[50];
- snprintf(buffer, 50, "FPS: %f %f %f", 128000000000.0f / sum, testRadius, testBias);
- game.fontRenderer.drawString(20, 20, buffer);
- game.game.renderTextOverlay(lag, game.model, shaders.text, game.fontRenderer);
- glDisable(GL_BLEND);
- }
- static void renderTick(float lag, Shaders& shaders, InternGame& game, Framebuffers& fb) {
- u64 current = getTimeNanos();
- sum -= values[tindex];
- values[tindex] = current - last;
- sum += values[tindex];
- last = current;
- tindex = (tindex + 1) & (127);
- if(resize) {
- fb.resize(width, height);
- resize = false;
- }
- game.cam.update(lag);
- shaders.updateWorldProjection(game.cam);
- shaders.updateWorldView(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;
- last = getTimeNanos();
- 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();
- }
|