Browse Source

further work on quaternions, removal of camera and ray, shadow tests

Kajetan Johannes Hammerle 3 years ago
parent
commit
ab55794d6b

+ 26 - 22
client/Game.cpp

@@ -5,16 +5,15 @@
 #include "common/utils/Random.h"
 #include "math/Quaternion.h"
 
-Game::Game(const Control& control, const Camera& camera, Ray& ray, const Clock& fps, const Clock& tps,
-        RenderSettings& renderSettings) :
-control(control), camera(camera), ray(ray), fps(fps), tps(tps), renderSettings(renderSettings), lengthAngle(240.0f),
-widthAngle(20.0f), world(blockRegistry), worldRenderer(world), pointIndex(0), moveSpeed(0.25f), movedLength(0.0f),
-mode(Mode::AUTO) {
+Game::Game(const Control& control, const Clock& fps, const Clock& tps, RenderSettings& renderSettings) :
+control(control), fps(fps), tps(tps), renderSettings(renderSettings), world(blockRegistry), worldRenderer(world),
+pointIndex(0), moveSpeed(0.25f), movedLength(0.0f), mode(Mode::AUTO) {
     Random r(0);
     float h = World::WORLD_SIZE * 0.75f;
     float mid = World::WORLD_SIZE * 0.5f;
     float randLength = World::WORLD_SIZE * 0.125f;
     pos.set(0, h, 0);
+    lastPos.set(pos);
 
     for(uint i = 0; i < cameraPoints.getCapacity(); i++) {
         Vector offset(mid, h, mid);
@@ -29,28 +28,33 @@ mode(Mode::AUTO) {
 }
 
 void Game::tick() {
+    lastPos = pos;
     if(mode == Mode::PLAYER) {
-        const float speed = 0.25f;
+        Vector right(1.0f, 0.0f, 0.0f);
+        Vector up(0.0f, 1.0f, 0.0f);
+        Vector back(0.0f, 0.0f, -1.0f);
+
+        const float speed = 1.0f;
         if(control.keys.down.isDown()) {
-            pos.addMul(camera.getFlatBack(), speed);
+            pos.addMul(back, speed);
         }
         if(control.keys.up.isDown()) {
-            pos.addMul(camera.getFlatBack(), -speed);
+            pos.addMul(back, -speed);
         }
         if(control.keys.left.isDown()) {
-            pos.addMul(camera.getFlatRight(), -speed);
+            pos.addMul(right, -speed);
         }
         if(control.keys.right.isDown()) {
-            pos.addMul(camera.getFlatRight(), speed);
+            pos.addMul(right, speed);
         }
         if(control.keys.jump.isDown()) {
-            pos.addMul(camera.getFlatUp(), speed);
+            pos.addMul(up, speed);
         }
         if(control.keys.sneak.isDown()) {
-            pos.addMul(camera.getFlatUp(), -speed);
+            pos.addMul(up, -speed);
         }
 
-        const float rotation = 5.0f;
+        /*const float rotation = 5.0f;
         if(control.keys.camLeft.isDown()) {
             lengthAngle += rotation;
         }
@@ -62,10 +66,7 @@ void Game::tick() {
         }
         if(control.keys.camDown.isDown() && widthAngle + rotation < 90.0f) {
             widthAngle += rotation;
-        }
-
-        ray.store();
-        ray.set(pos, lengthAngle, widthAngle);
+        }*/
     } else if(mode == Mode::AUTO) {
         movedLength += moveSpeed;
     }
@@ -89,7 +90,7 @@ void Game::renderWorld(float lag, Renderer& renderer) const {
             leftLength -= cameraPoints[index].distance;
             index = (index + 1) % cameraPoints.getLength();
         }
-        
+
         uint prev = index;
         float t = leftLength / cameraPoints[index].distance;
         if(prev == 0) {
@@ -106,16 +107,19 @@ void Game::renderWorld(float lag, Renderer& renderer) const {
 
         Vector interpolatedPos = interpolate(cameraPoints[currentA].pos, cameraPoints[currentB].pos, tangentA, tangentB, t);
 
-        pos.set(interpolatedPos);
-        ray.set(interpolatedPos, lengthAngle, widthAngle);
-        ray.store();
+        renderer.update(interpolatedPos, Quaternion(Vector(1.0f, 0.0f, 0.0f), -30.0f));
+        pos = interpolatedPos;
+    } else if(mode == Mode::PLAYER) {
+        Vector v(lastPos);
+        v.addMul(pos, lag).addMul(lastPos, -lag);
+        renderer.update(v, Quaternion(Vector(1.0f, 0.0f, 0.0f), -80.0f));
     }
     worldRenderer.render(lag, renderer);
 }
 
 void Game::renderTextOverlay(float lag, Renderer& renderer, FontRenderer& fr) const {
     (void) lag;
-    renderer.scale(1.0f).update();
+    renderer.scale(2.0f).update();
 
     String s;
     fr.drawString(10, 10, s.append("FPS: ").append(fps.getUpdatesPerSecond()));

+ 2 - 7
client/Game.h

@@ -2,7 +2,6 @@
 #define GAME_H
 
 #include "client/input/Control.h"
-#include "client/math/Camera.h"
 #include "client/utils/Clock.h"
 #include "client/rendering/RenderSettings.h"
 #include "client/rendering/Renderer.h"
@@ -15,8 +14,7 @@
 
 class Game final {
 public:
-    Game(const Control& control, const Camera& camera, Ray& ray, const Clock& fps, const Clock& tps,
-            RenderSettings& renderSettings);
+    Game(const Control& control, const Clock& fps, const Clock& tps, RenderSettings& renderSettings);
 
     void tick();
     void renderWorld(float lag, Renderer& renderer) const;
@@ -30,14 +28,11 @@ public:
 
 private:
     const Control& control;
-    const Camera& camera;
-    Ray& ray;
     const Clock& fps;
     const Clock& tps;
     RenderSettings& renderSettings;
 
-    float lengthAngle;
-    float widthAngle;
+    Vector lastPos;
     mutable Vector pos;
 
     BlockRegistry blockRegistry;

+ 2 - 4
client/Main.cpp

@@ -70,15 +70,13 @@ int main() {
         return 0;
     }
     
-    Ray ray;
-    Camera camera(ray);
     RenderSettings renderSettings;
-    Engine engine(shaders, framebuffers, camera, size, renderSettings);
+    Engine engine(shaders, framebuffers, size, renderSettings);
     
     Control control;
     Clock fps;
     Clock tps;
-    static Game game(control, camera, ray, fps, tps, renderSettings);
+    static Game game(control, fps, tps, renderSettings);
     
     initCallbacks(window, size, framebuffers, control);
     window.show();

+ 0 - 44
client/math/Camera.cpp

@@ -1,44 +0,0 @@
-#include "client/math/Camera.h"
-
-Camera::Camera(const Ray& ray) : ray(ray) {
-}
-
-const Vector& Camera::getBack() const {
-    return back;
-}
-
-const Vector& Camera::getRight() const {
-    return right;
-}
-
-const Vector& Camera::getUp() const {
-    return up;
-}
-
-const Vector& Camera::getFlatBack() const {
-    return flatBack;
-}
-
-const Vector& Camera::getFlatRight() const {
-    return flatRight;
-}
-
-const Vector& Camera::getFlatUp() const {
-    return flatUp;
-}
-
-const Vector& Camera::getPosition() const {
-    return position;
-}
-
-void Camera::update(float lag) {
-    back = ray.getDirection(lag);
-    right.setInverse(back).cross(0.0f, 1.0f, 0.0f).normalize();
-    up.set(back).cross(right).normalize();
-    
-    flatBack.set(back).setY(0.0f).normalize();
-    flatRight.setInverse(back).cross(0.0f, 1.0f, 0.0f).normalize();
-    flatUp.set(0.0f, 1.0f, 0.0f);
-    
-    position = ray.getPosition(lag);
-}

+ 0 - 36
client/math/Camera.h

@@ -1,36 +0,0 @@
-#ifndef CAMERA3D_H
-#define CAMERA3D_H
-
-#include "client/math/Ray.h"
-
-class Camera final {
-public:
-    Camera(const Ray& ray);
-
-    const Vector& getBack() const;
-    const Vector& getRight() const;
-    const Vector& getUp() const;
-
-    const Vector& getFlatBack() const;
-    const Vector& getFlatRight() const;
-    const Vector& getFlatUp() const;
-
-    const Vector& getPosition() const;
-
-    void update(float lag);
-
-private:
-    const Ray& ray;
-
-    Vector back;
-    Vector right;
-    Vector up;
-
-    Vector flatBack;
-    Vector flatRight;
-    Vector flatUp;
-
-    Vector position;
-};
-
-#endif

+ 4 - 0
client/math/Frustum.cpp

@@ -27,3 +27,7 @@ void Frustum::setProjection(Matrix& m, int width, int height) {
     m.set(14, (2.0f * nearClip * farClip) / (nearClip - farClip));
     m.set(15, 0.0f);
 }
+
+float Frustum::getClipDifference() const {
+    return farClip - nearClip;
+}

+ 2 - 0
client/math/Frustum.h

@@ -7,6 +7,8 @@ class Frustum final {
 public:
     Frustum(float fovY, float nearClip, float farClip);
     void setProjection(Matrix& m, int width, int height);
+    
+    float getClipDifference() const;
 
 private:
     float fovY;

+ 0 - 29
client/math/Ray.cpp

@@ -1,29 +0,0 @@
-#include "client/math/Ray.h"
-#include "client/utils/Utils.h"
-
-Ray::Ray() : lastLengthAngle(0), lengthAngle(0), lastWidthAngle(0), widthAngle(0) {
-}
-
-void Ray::store() {
-    lastPosition = position;
-    lastLengthAngle = lengthAngle;
-    lastWidthAngle = widthAngle;
-}
-
-void Ray::set(const Vector& position, float lengthAngle, float widthAngle) {
-    Ray::position = position;
-    Ray::lengthAngle = lengthAngle;
-    Ray::widthAngle = widthAngle;
-}
-
-Vector Ray::getPosition(float lag) const {
-    Vector pos(lastPosition);
-    pos.addMul(position, lag).addMul(lastPosition, -lag);
-    return pos;
-}
-
-Vector Ray::getDirection(float lag) const {
-    Vector direction;
-    direction.setAngles(interpolate(lag, lastLengthAngle, lengthAngle), interpolate(lag, lastWidthAngle, widthAngle));
-    return direction;
-}

+ 0 - 27
client/math/Ray.h

@@ -1,27 +0,0 @@
-#ifndef RAY_H
-#define RAY_H
-
-#include "client/math/Vector.h"
-
-class Ray final {
-public:
-    Ray();
-    
-    void store();
-    void set(const Vector& position, float lengthAngle, float widthAngle);
-
-    Vector getPosition(float lag) const;
-    Vector getDirection(float lag) const;
-
-private:
-    Vector lastPosition;
-    Vector position;
-
-    float lastLengthAngle;
-    float lengthAngle;
-
-    float lastWidthAngle;
-    float widthAngle;
-};
-
-#endif

+ 16 - 151
client/rendering/Engine.cpp

@@ -7,9 +7,8 @@
 #include "client/rendering/Renderer.h"
 #include "client/math/Plane.h"
 
-Engine::Engine(Shaders& shaders, Framebuffers& fb, Camera& camera, const WindowSize& size,
-        RenderSettings& renderSettings) :
-shaders(shaders), fb(fb), camera(camera), size(size), renderSettings(renderSettings), frustum(60.0f, 0.1f, 80.0f) {
+Engine::Engine(Shaders& shaders, Framebuffers& fb, const WindowSize& size, RenderSettings& renderSettings) :
+shaders(shaders), fb(fb), size(size), renderSettings(renderSettings), frustum(60.0f, 0.1f, 80.0f) {
     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});
@@ -20,7 +19,6 @@ shaders(shaders), fb(fb), camera(camera), size(size), renderSettings(renderSetti
 }
 
 void Engine::renderTick(float lag, const Game& game) {
-    camera.update(lag);
     updateWorldProjection();
     updateWorldView();
 
@@ -41,7 +39,7 @@ void Engine::renderShadow(float lag, const Game& game) {
     shaders.shadow.use();
     worldShadowProjView.set(worldShadowProj).mul(worldShadowView);
     shaders.shadow.setMatrix("projView", worldShadowProjView.getValues());
-    Renderer renderer(shaders.shadow, model);
+    Renderer renderer(shaders.shadow, model, worldView);
     game.renderWorld(lag, renderer);
 }
 
@@ -55,14 +53,14 @@ void Engine::renderWorld(float lag, const Game& game) {
 
     shaders.world.setMatrix("projViewShadow", rWorldShadowProjView.getValues());
     shaders.world.setMatrix("proj", worldProj.getValues());
-    shaders.world.setMatrix("view", worldView.getValues());
+    shaders.world.setMatrix("view", worldView.setToIdentity().getValues());
     model.clear();
     shaders.world.setMatrix("model", model.get().getValues());
     fb.shadow.bindDepthTexture(1);
     shaders.world.setInt("shadows", renderSettings.shadows);
     shaders.world.setFloat("radius", renderSettings.testRadius);
     shaders.world.setFloat("bias", renderSettings.testBias);
-    Renderer renderer(shaders.world, model);
+    Renderer renderer(shaders.world, model, worldView);
     game.renderWorld(lag, renderer);
 }
 
@@ -110,11 +108,10 @@ void Engine::renderTextOverlay(float lag, const Game& game) {
     m.translate(-1.0f, 1.0f, 0.0f).scale(2.0f / size.width, -2.0f / size.height, 1.0f);
     shaders.text.setMatrix("view", m.getValues());
     model.clear();
-    model.get().scale(2.0f, 2.0f, 2.0f);
     shaders.text.setMatrix("model", model.get().getValues());
 
     GLWrapper::enableBlending();
-    Renderer renderer(shaders.text, model);
+    Renderer renderer(shaders.text, model, m);
     game.renderTextOverlay(lag, renderer, fontRenderer);
     GLWrapper::disableBlending();
 }
@@ -125,153 +122,21 @@ void Engine::updateWorldProjection() {
     if(!renderSettings.shadows) {
         return;
     }
-
-    // http://cgvr.informatik.uni-bremen.de/teaching/cg_literatur/lighthouse3d_view_frustum_culling/index.html
-    const float fovY = 60.0f;
-    const float nearClip = 0.1f;
-    const float farClip = 80.0f;
-
-    float tan = tanf((0.5f * fovY) * M_PI / 180.0f);
-    float aspect = (float) size.width / size.height;
-
-    float closeFarClip = 16;
-    float nearHigh = tan * nearClip;
-    float nearWidth = nearHigh * aspect;
-    float farHigh = tan * closeFarClip;
-    float farWidth = farHigh * aspect;
-
-    Vector farCenter = camera.getPosition();
-    farCenter.addMul(camera.getBack(), -closeFarClip);
-
-    Vector farTopLeft = farCenter;
-    farTopLeft.addMul(camera.getRight(), -farWidth).addMul(camera.getUp(), farHigh);
-
-    Vector farBottomLeft = farCenter;
-    farBottomLeft.addMul(camera.getRight(), -farWidth).addMul(camera.getUp(), -farHigh);
-
-    Vector farTopRight = farCenter;
-    farTopRight.addMul(camera.getRight(), farWidth).addMul(camera.getUp(), farHigh);
-
-    Vector farBottomRight = farCenter;
-    farBottomRight.addMul(camera.getRight(), farWidth).addMul(camera.getUp(), -farHigh);
-
-    Vector nearCenter = camera.getPosition();
-    nearCenter.addMul(camera.getBack(), -nearClip);
-
-    Vector nearTopLeft = nearCenter;
-    nearTopLeft.addMul(camera.getRight(), -nearWidth).addMul(camera.getUp(), nearHigh);
-
-    Vector nearBottomLeft = nearCenter;
-    nearBottomLeft.addMul(camera.getRight(), -nearWidth).addMul(camera.getUp(), -nearHigh);
-
-    Vector nearTopRight = nearCenter;
-    nearTopRight.addMul(camera.getRight(), nearWidth).addMul(camera.getUp(), nearHigh);
-
-    Vector nearBottomRight = nearCenter;
-    nearBottomRight.addMul(camera.getRight(), nearWidth).addMul(camera.getUp(), -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);
-
-    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;
-
-    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;
-
-    // not the real center, but good guess
-    renderSettings.testOrthoCenter = nearCenter;
-    renderSettings.testOrthoCenter.addMul(camera.getBack(), -closeFarClip * 0.5f);
-
-    if(renderSettings.ortho) {
-        worldProj.setToIdentity();
-        worldProj.set(0, 2.0f / lightWidth);
-        worldProj.set(5, 2.0f / lightHeight);
-        worldProj.set(10, -2.0f / (farClip - nearClip));
-    }
-
     worldShadowProj.setToIdentity();
-    worldShadowProj.set(0, 2.0f / lightWidth);
-    worldShadowProj.set(5, 2.0f / lightHeight);
-    worldShadowProj.set(10, -2.0f / (farClip - nearClip));
+    worldShadowProj.set(0, 2.0f / 40.0f);
+    worldShadowProj.set(5, 2.0f / 30.0f);
+    worldShadowProj.set(10, -2.0f / frustum.getClipDifference());
 }
 
 void Engine::updateWorldView() {
-    Vector right = camera.getRight();
-    Vector up = camera.getUp();
-    Vector back = camera.getBack();
-    Vector pos = camera.getPosition();
-
-    if(renderSettings.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 = renderSettings.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(!renderSettings.shadows) {
         return;
     }
 
-    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 = renderSettings.testOrthoCenter;
+    Vector right(0.939693f, 0.0f, -0.34202f);
+    Vector back(0.280166f, 0.573576f, 0.769751f);
+    Vector up(-0.196175f, 0.819152f, -0.538986f);
+    Vector center(16.0f, 24.0f, 24.0f);
 
     worldShadowView.set(0, right.getX());
     worldShadowView.set(1, up.getX());
@@ -282,7 +147,7 @@ void Engine::updateWorldView() {
     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));
+    worldShadowView.set(12, right.dotInverse(center));
+    worldShadowView.set(13, up.dotInverse(center));
+    worldShadowView.set(14, back.dotInverse(center));
 }

+ 1 - 3
client/rendering/Engine.h

@@ -3,7 +3,6 @@
 
 #include "client/rendering/Shaders.h"
 #include "client/rendering/Framebuffers.h"
-#include "client/math/Camera.h"
 #include "client/rendering/RenderSettings.h"
 #include "client/Game.h"
 #include "client/math/Frustum.h"
@@ -13,7 +12,7 @@
 
 class Engine final {
 public:
-    Engine(Shaders& shaders, Framebuffers& fb, Camera& camera, const WindowSize& size, RenderSettings& renderSettings);
+    Engine(Shaders& shaders, Framebuffers& fb, const WindowSize& size, RenderSettings& renderSettings);
     void renderTick(float lag, const Game& game);
 
 private:
@@ -27,7 +26,6 @@ private:
 
     Shaders& shaders;
     Framebuffers& fb;
-    Camera& camera;
     const WindowSize& size;
     RenderSettings& renderSettings;
     

+ 2 - 2
client/rendering/Framebuffers.cpp

@@ -1,10 +1,10 @@
 #include "client/rendering/Framebuffers.h"
 
 Framebuffers::Framebuffers(const WindowSize& size) :
-world(size, Framebuffer::POSITION | Framebuffer::NORMAL | Framebuffer::COLOR | Framebuffer::RED | Framebuffer::DEPTH24_STENCIL8),
+world(size, Framebuffer::POSITION | Framebuffer::NORMAL | Framebuffer::COLOR | Framebuffer::RED | Framebuffer::DEPTH),
 ssao(size, Framebuffer::RED),
 ssaoBlur(size, Framebuffer::RED),
-shadow(size, Framebuffer::DEPTH24_STENCIL8) {
+shadow(size, Framebuffer::DEPTH) {
 }
 
 void Framebuffers::resize(uint width, uint height) {

+ 1 - 1
client/rendering/RenderSettings.cpp

@@ -1,4 +1,4 @@
 #include "client/rendering/RenderSettings.h"
 
-RenderSettings::RenderSettings() : ssao(true), shadows(false), testRadius(0.0005f), testBias(0.0005), ortho(false) {
+RenderSettings::RenderSettings() : ssao(false), shadows(true), testRadius(0.5f), testBias(0.0005) {
 }

+ 0 - 2
client/rendering/RenderSettings.h

@@ -10,8 +10,6 @@ struct RenderSettings final {
     bool shadows;
     float testRadius;
     float testBias;
-    bool ortho;
-    Vector testOrthoCenter;
 };
 
 #endif

+ 28 - 1
client/rendering/Renderer.cpp

@@ -1,6 +1,6 @@
 #include "client/rendering/Renderer.h"
 
-Renderer::Renderer(Shader& shader, MatrixStack& stack) : shader(shader), stack(stack) {
+Renderer::Renderer(Shader& shader, MatrixStack& stack, Matrix& view) : shader(shader), stack(stack), view(view) {
 }
 
 void Renderer::pop() {
@@ -16,6 +16,33 @@ Renderer& Renderer::update() {
     return *this;
 }
 
+Renderer& Renderer::update(const Vector& pos, const Quaternion& rotation) {
+    Matrix m = rotation.toMatrix();
+    Vector right(1.0f, 0.0f, 0.0f);
+    Vector up(0.0f, 1.0f, 0.0f);
+    Vector back(0.0f, 0.0f, -1.0f);
+    
+    right.mul(m);
+    up.mul(m);
+    back.mul(m);
+
+    view.set(0, right.getX());
+    view.set(1, up.getX());
+    view.set(2, back.getX());
+    view.set(4, right.getY());
+    view.set(5, up.getY());
+    view.set(6, back.getY());
+    view.set(8, right.getZ());
+    view.set(9, up.getZ());
+    view.set(10, back.getZ());
+    view.set(12, right.dotInverse(pos));
+    view.set(13, up.dotInverse(pos));
+    view.set(14, back.dotInverse(pos));
+    
+    shader.setMatrix("view", view.getValues());
+    return *this;
+}
+
 Renderer& Renderer::scale(float sx, float sy, float sz) {
     stack.get().scale(sx, sy, sz);
     return *this;

+ 5 - 1
client/rendering/Renderer.h

@@ -3,15 +3,18 @@
 
 #include "client/rendering/wrapper/Shader.h"
 #include "client/math/MatrixStack.h"
+#include "client/math/Vector.h"
+#include "client/math/Quaternion.h"
 
 class Renderer final {
 public:
-    Renderer(Shader& shader, MatrixStack& stack);
+    Renderer(Shader& shader, MatrixStack& stack, Matrix& view);
     
     void pop();
     void push();
     
     Renderer& update();
+    Renderer& update(const Vector& pos, const Quaternion& rotation);
     
     Renderer& scale(float sx, float sy, float sz);
     Renderer& scale(float s);
@@ -29,6 +32,7 @@ public:
 private:
     Shader& shader;
     MatrixStack& stack;
+    Matrix& view;
 };
 
 #endif

+ 3 - 3
client/rendering/wrapper/Framebuffer.cpp

@@ -11,7 +11,7 @@ buffer(0), error(false) {
     data[1] = {NORMAL, GL_RGB16F, GL_RGB, GL_FLOAT};
     data[2] = {COLOR, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE};
     data[3] = {RED, GL_R32F, GL_RGB, GL_FLOAT};
-    data[4] = {DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8};
+    data[4] = {DEPTH, GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT, GL_FLOAT};
 
     GLuint attachments[4];
     uint counter = 0;
@@ -20,13 +20,13 @@ buffer(0), error(false) {
             setupTexture(i, size.width, size.height, attachments, counter);
         }
     }
-    if(mode & DEPTH24_STENCIL8) {
+    if(mode & DEPTH) {
         genTexture(4, size.width, size.height);
         if(texCompare) {
             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
         }
-        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, textures[4], 0);
+        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, textures[4], 0);
     }
     glDrawBuffers(counter, attachments);
 

+ 1 - 1
client/rendering/wrapper/Framebuffer.h

@@ -13,7 +13,7 @@ public:
     static const uint NORMAL = 2;
     static const uint COLOR = 4;
     static const uint RED = 8;
-    static const uint DEPTH24_STENCIL8 = 16;
+    static const uint DEPTH = 16;
 
     Framebuffer(const WindowSize& size, uint mode, bool texCompare = false);
     ~Framebuffer();

+ 1 - 1
meson.build

@@ -6,7 +6,7 @@ sourcesCommon = ['common/block/BlockBuilder.cpp', 'common/block/Block.cpp', 'com
 
 sourcesServer = ['server/Main.cpp', 'server/network/Server.cpp', 'server/GameServer.cpp', 'server/commands/ServerCommands.cpp', 'server/commands/CommandManager.cpp', 'server/network/Socket.cpp', 'server/commands/CommandEditor.cpp', 'server/Clock.cpp']
 
-sourcesClient = ['client/Main.cpp', 'client/rendering/WindowSize.cpp', 'client/math/Frustum.cpp', 'client/math/Ray.cpp', 'client/rendering/Framebuffers.cpp', 'client/rendering/wrapper/GLFWWrapper.cpp', 'client/rendering/wrapper/Window.cpp', 'client/rendering/Engine.cpp', 'client/input/Keys.cpp', 'client/rendering/wrapper/Shader.cpp', 'client/rendering/Shaders.cpp', 'client/utils/Utils.cpp', 'client/rendering/Mesh.cpp', 'client/math/Matrix.cpp', 'client/math/MatrixStack.cpp', 'client/math/Vector.cpp', 'client/math/Quaternion.cpp', 'client/math/Camera.cpp', 'client/math/Plane.cpp', 'client/Game.cpp', 'client/input/MouseButtons.cpp', 'client/rendering/FileTexture.cpp', 'client/rendering/FontRenderer.cpp', 'client/rendering/wrapper/Framebuffer.cpp', 'client/rendering/NoiseTexture.cpp', 'client/utils/Clock.cpp', 'client/input/Control.cpp', 'client/rendering/RenderSettings.cpp', 'client/rendering/wrapper/VertexBuffer.cpp', 'client/rendering/wrapper/StreamBuffer.cpp', 'client/rendering/wrapper/Texture.cpp', 'client/utils/PNGReader.cpp', 'client/rendering/wrapper/GLWrapper.cpp', 'client/rendering/Renderer.cpp', 'client/rendering/renderer/WorldRenderer.cpp']
+sourcesClient = ['client/Main.cpp', 'client/rendering/WindowSize.cpp', 'client/math/Frustum.cpp', 'client/rendering/Framebuffers.cpp', 'client/rendering/wrapper/GLFWWrapper.cpp', 'client/rendering/wrapper/Window.cpp', 'client/rendering/Engine.cpp', 'client/input/Keys.cpp', 'client/rendering/wrapper/Shader.cpp', 'client/rendering/Shaders.cpp', 'client/utils/Utils.cpp', 'client/rendering/Mesh.cpp', 'client/math/Matrix.cpp', 'client/math/MatrixStack.cpp', 'client/math/Vector.cpp', 'client/math/Quaternion.cpp', 'client/math/Plane.cpp', 'client/Game.cpp', 'client/input/MouseButtons.cpp', 'client/rendering/FileTexture.cpp', 'client/rendering/FontRenderer.cpp', 'client/rendering/wrapper/Framebuffer.cpp', 'client/rendering/NoiseTexture.cpp', 'client/utils/Clock.cpp', 'client/input/Control.cpp', 'client/rendering/RenderSettings.cpp', 'client/rendering/wrapper/VertexBuffer.cpp', 'client/rendering/wrapper/StreamBuffer.cpp', 'client/rendering/wrapper/Texture.cpp', 'client/utils/PNGReader.cpp', 'client/rendering/wrapper/GLWrapper.cpp', 'client/rendering/Renderer.cpp', 'client/rendering/renderer/WorldRenderer.cpp']
 
 sourcesTest = ['tests/Main.cpp', 'common/utils/String.cpp', 'common/utils/SplitString.cpp', 'client/math/Matrix.cpp', 'client/math/Vector.cpp', 'common/utils/HashedString.cpp', 'common/utils/Random.cpp']
 

+ 13 - 8
resources/shader/worldFragment.fs

@@ -49,18 +49,23 @@ void main(void) {
     worldPosition = varPosition;
     worldNormal = normalize(varNormal);
 
-    if(shadows) {
+    /*if(shadows) {
         vec3 pos = varShadow.xyz / varShadow.w;
         float fbias = bias * (1.0 - max(dot(worldNormal, -light), 0.0)); 
-        float shadow = 0;
-        for(int i = 0; i < sampleAmount; i++) {
-            shadow += float(texture(shadowSamp, pos.xy + vec2(samples[i].x, samples[i].y) * radius).r + fbias > pos.z);
-        }
-        shadow /= sampleAmount;
-        worldShadow = shadow;
+        float shadow = 0.0; 
+        shadow = float(texture(shadowSamp, pos.xy).r == 1);//float(texture(shadowSamp, pos.xy).r == 1);
+        //for(int i = 0; i < sampleAmount; i++) {
+        //    shadow += float(texture(shadowSamp, pos.xy + samples[i] * radius).r + fbias > pos.z);
+        //}
+        //shadow /= sampleAmount;
+        worldShadow = 1;//shadow;
     } else {
         worldShadow = 1.0;
-    }
+    }*/
+
+    vec3 pos = varShadow.xyz / varShadow.w;
+    float test = texture(shadowSamp, pos.xy).r;
+    worldShadow = float(test >= 0.99 && test <= 1.01);
 
     worldColor = texture(samp, varTex);
 }

+ 4 - 3
resources/shader/worldPostFragment.fs

@@ -21,8 +21,9 @@ void main() {
         color = vec4(texture(colorSamp, varTex).xyz, 1.0);
     }
     if(shadows) {
-        float diffuseLight = float(dot(texture(worldNormalSamp, varTex).xyz, -light) > 0);
-        float f = diffuseLight * texture(shadowSamp, varTex).r;
-        color *= f * 0.5 + 0.5;
+        //float diffuseLight = 1.0;//float(dot(texture(worldNormalSamp, varTex).xyz, -light) > 0);
+        //float f = diffuseLight * texture(shadowSamp, varTex).r;
+        //color *= f * 0.5 + 0.5;
+        color *= texture(shadowSamp, varTex).r;
     }
 }  

+ 1 - 1
resources/shader/worldShadowFragment.fs

@@ -3,5 +3,5 @@
 out vec4 color;
 
 void main(void) {
-    color = vec4(1.0, 0.0, 1.0, 1.0);
+    color = vec4(1.0);
 }

+ 0 - 2
resources/shader/worldVertex.vs

@@ -15,8 +15,6 @@ out vec2 varTex;
 out vec3 varNormal;
 out vec4 varShadow;
 
-const vec3 light = vec3(-0.280166, -0.573576, -0.769751);
-
 void main(void) { 
     // transforming normals must not use the fourth dimension
     // should be the inverse transposed matrix