Browse Source

refactoring

Kajetan Johannes Hammerle 3 years ago
parent
commit
2b6c8191f8
8 changed files with 64 additions and 63 deletions
  1. 14 35
      client/Engine.cpp
  2. 0 1
      client/Main.cpp
  3. 33 12
      client/math/Matrix.cpp
  4. 14 12
      client/math/Matrix.h
  5. 3 3
      client/math/Ray.cpp
  6. BIN
      resources/font16x16.png
  7. BIN
      resources/font24x24.png
  8. BIN
      resources/font8x8.png

+ 14 - 35
client/Engine.cpp

@@ -18,7 +18,7 @@ void Engine::renderTick(float lag, const Game& game) {
     camera.update(lag);
     updateWorldProjection();
     updateWorldView();
-
+    
     renderShadow(lag, game);
     renderWorld(lag, game);
     if(renderSettings.useSSAO) {
@@ -32,14 +32,9 @@ void Engine::renderShadow(float lag, const Game& game) {
     fb.shadow.bind();
     glEnable(GL_DEPTH_TEST);
     shaders.shadow.use();
-    worldShadowProjView = worldShadowProj;
-    worldShadowProjView.mul(worldShadowView);
+    worldShadowProjView.set(worldShadowProj).mul(worldShadowView);
     shaders.shadow.setMatrix("projView", worldShadowProjView.getValues());
-
-    //glEnable(GL_CULL_FACE);
-    //glCullFace(GL_FRONT);
     game.renderWorld(lag, model, shaders.shadow);
-    //glCullFace(GL_BACK);
 }
 
 void Engine::renderWorld(float lag, const Game& game) {
@@ -48,9 +43,7 @@ void Engine::renderWorld(float lag, const Game& game) {
     shaders.world.use();
 
     Matrix rWorldShadowProjView;
-    rWorldShadowProjView.translate(0.5f, 0.5f, 0.5f);
-    rWorldShadowProjView.scale(0.5f, 0.5f, 0.5f);
-    rWorldShadowProjView.mul(worldShadowProjView);
+    rWorldShadowProjView.translate(0.5f, 0.5f, 0.5f).scale(0.5f).mul(worldShadowProjView);
 
     shaders.world.setMatrix("projViewShadow", rWorldShadowProjView.getValues());
     shaders.world.setMatrix("proj", worldProj.getValues());
@@ -67,9 +60,7 @@ void Engine::renderSSAO() {
     shaders.ssao.use();
 
     Matrix rProj;
-    rProj.translate(0.5f, 0.5f, 0.5f);
-    rProj.scale(0.5f, 0.5f, 0.5f);
-    rProj.mul(worldProj);
+    rProj.translate(0.5f, 0.5f, 0.5f).scale(0.5f).mul(worldProj);
 
     shaders.ssao.setMatrix("proj", rProj.getValues());
     shaders.ssao.setInt("width", size.width);
@@ -81,7 +72,7 @@ void Engine::renderSSAO() {
     ssaoNoise.bind(4);
     fb.ssao.bind();
     rectangle.draw();
-    // ssao blur
+
     shaders.ssaoBlur.use();
     fb.ssao.bindRedTexture(0);
     fb.ssaoBlur.bind();
@@ -106,8 +97,7 @@ void Engine::renderTextOverlay(float lag, const Game& game) {
 
     Matrix m;
     shaders.text.setMatrix("proj", m.getValues());
-    m.translate(-1.0f, 1.0f, 0.0f);
-    m.scale(2.0f / size.width, -2.0f / size.height, 1.0f);
+    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);
@@ -141,39 +131,31 @@ void Engine::updateWorldProjection() {
     farCenter.addMul(camera.getBack(), -closeFarClip);
 
     Vector farTopLeft = farCenter;
-    farTopLeft.addMul(camera.getRight(), -farWidth);
-    farTopLeft.addMul(camera.getUp(), farHigh);
+    farTopLeft.addMul(camera.getRight(), -farWidth).addMul(camera.getUp(), farHigh);
 
     Vector farBottomLeft = farCenter;
-    farBottomLeft.addMul(camera.getRight(), -farWidth);
-    farBottomLeft.addMul(camera.getUp(), -farHigh);
+    farBottomLeft.addMul(camera.getRight(), -farWidth).addMul(camera.getUp(), -farHigh);
 
     Vector farTopRight = farCenter;
-    farTopRight.addMul(camera.getRight(), farWidth);
-    farTopRight.addMul(camera.getUp(), farHigh);
+    farTopRight.addMul(camera.getRight(), farWidth).addMul(camera.getUp(), farHigh);
 
     Vector farBottomRight = farCenter;
-    farBottomRight.addMul(camera.getRight(), farWidth);
-    farBottomRight.addMul(camera.getUp(), -farHigh);
+    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);
-    nearTopLeft.addMul(camera.getUp(), nearHigh);
+    nearTopLeft.addMul(camera.getRight(), -nearWidth).addMul(camera.getUp(), nearHigh);
 
     Vector nearBottomLeft = nearCenter;
-    nearBottomLeft.addMul(camera.getRight(), -nearWidth);
-    nearBottomLeft.addMul(camera.getUp(), -nearHigh);
+    nearBottomLeft.addMul(camera.getRight(), -nearWidth).addMul(camera.getUp(), -nearHigh);
 
     Vector nearTopRight = nearCenter;
-    nearTopRight.addMul(camera.getRight(), nearWidth);
-    nearTopRight.addMul(camera.getUp(), nearHigh);
+    nearTopRight.addMul(camera.getRight(), nearWidth).addMul(camera.getUp(), nearHigh);
 
     Vector nearBottomRight = nearCenter;
-    nearBottomRight.addMul(camera.getRight(), nearWidth);
-    nearBottomRight.addMul(camera.getUp(), -nearHigh);
+    nearBottomRight.addMul(camera.getRight(), nearWidth).addMul(camera.getUp(), -nearHigh);
 
     Vector light(-0.280166, -0.573576, -0.769751);
     Vector lightLeft = light;
@@ -181,7 +163,6 @@ void Engine::updateWorldProjection() {
     Vector lightUp = lightLeft;
     lightUp.cross(light);
 
-    //std::cout << "-------------------------\n";
     Plane plane;
     plane.set(Vector(), light, lightUp);
     float f[8];
@@ -204,7 +185,6 @@ void Engine::updateWorldProjection() {
         }
     }
     float lightWidth = max - min;
-    //std::cout << lightWidth << "\n";
 
     plane.set(Vector(), light, lightLeft);
     f[0] = plane.getSignedDistance(farTopLeft);
@@ -227,7 +207,6 @@ void Engine::updateWorldProjection() {
         }
     }
     float lightHeight = max - min;
-    //std::cout << "\n" << max << " - " << min << " " << lightHeight << "\n";
 
     // not the real center, but good guess
     renderSettings.testOrthoCenter = nearCenter;

+ 0 - 1
client/Main.cpp

@@ -97,6 +97,5 @@ int main() {
         }
         glfwPollEvents();
     }
-
     return 0;
 }

+ 33 - 12
client/math/Matrix.cpp

@@ -8,7 +8,12 @@ Matrix::Matrix() {
     setToIdentity();
 }
 
-void Matrix::setToIdentity() {
+Matrix& Matrix::set(const Matrix& other) {
+    *this = other;
+    return *this;
+}
+
+Matrix& Matrix::setToIdentity() {
     data[0] = 1.0f;
     data[1] = 0.0f;
     data[2] = 0.0f;
@@ -25,17 +30,19 @@ void Matrix::setToIdentity() {
     data[13] = 0.0f;
     data[14] = 0.0f;
     data[15] = 1.0f;
+    return *this;
 }
 
-void Matrix::set(unsigned int index, float f) {
+Matrix& Matrix::set(unsigned int index, float f) {
     data[index] = f;
+    return *this;
 }
 
 const float* Matrix::getValues() const {
     return data;
 }
 
-void Matrix::mul(const Matrix& m) {
+Matrix& Matrix::mul(const Matrix& m) {
     float mNew[16];
     mNew[0] = data[0] * m.data[0] + data[4] * m.data[1] + data[8] * m.data[2] + data[12] * m.data[3];
     mNew[1] = data[1] * m.data[0] + data[5] * m.data[1] + data[9] * m.data[2] + data[13] * m.data[3];
@@ -54,9 +61,10 @@ void Matrix::mul(const Matrix& m) {
     mNew[14] = data[2] * m.data[12] + data[6] * m.data[13] + data[10] * m.data[14] + data[14] * m.data[15];
     mNew[15] = data[3] * m.data[12] + data[7] * m.data[13] + data[11] * m.data[14] + data[15] * m.data[15];
     std::memcpy(data, mNew, sizeof (float) * 16);
+    return *this;
 }
 
-void Matrix::scale(float sx, float sy, float sz) {
+Matrix& Matrix::scale(float sx, float sy, float sz) {
     data[0] *= sx;
     data[1] *= sx;
     data[2] *= sx;
@@ -69,37 +77,46 @@ void Matrix::scale(float sx, float sy, float sz) {
     data[9] *= sz;
     data[10] *= sz;
     data[11] *= sz;
+    return *this;
+}
+
+Matrix& Matrix::scale(float s) {
+    return scale(s, s, s);
 }
 
-void Matrix::translate(float tx, float ty, float tz) {
+Matrix& Matrix::translate(float tx, float ty, float tz) {
     data[12] += data[0] * tx + data[4] * ty + data[8] * tz;
     data[13] += data[1] * tx + data[5] * ty + data[9] * tz;
     data[14] += data[2] * tx + data[6] * ty + data[10] * tz;
     data[15] += data[3] * tx + data[7] * ty + data[11] * tz;
+    return *this;
 }
 
-void Matrix::translateX(float tx) {
+Matrix& Matrix::translateX(float tx) {
     data[12] += data[0] * tx;
     data[13] += data[1] * tx;
     data[14] += data[2] * tx;
     data[15] += data[3] * tx;
+    return *this;
 }
 
-void Matrix::translateY(float ty) {
+Matrix& Matrix::translateY(float ty) {
     data[12] += data[4] * ty;
     data[13] += data[5] * ty;
     data[14] += data[6] * ty;
     data[15] += data[7] * ty;
+    return *this;
 }
 
-void Matrix::translateZ(float tz) {
+Matrix& Matrix::translateZ(float tz) {
     data[12] += data[8] * tz;
     data[13] += data[9] * tz;
     data[14] += data[10] * tz;
     data[15] += data[11] * tz;
+    return *this;
 }
 
-void Matrix::translateTo(float tx, float ty, float tz) {
+Matrix& Matrix::translateTo(float tx, float ty, float tz) {
     data[0] = 1.0f;
     data[1] = 0.0f;
     data[2] = 0.0f;
@@ -116,9 +133,10 @@ void Matrix::translateTo(float tx, float ty, float tz) {
     data[13] = ty;
     data[14] = tz;
     data[15] = 1.0f;
+    return *this;
 }
 
-void Matrix::rotateX(float degrees) {
+Matrix& Matrix::rotateX(float degrees) {
     degrees *= M_PI / 180.0f;
     float sin = sinf(degrees);
     float cos = cosf(degrees);
@@ -142,9 +160,10 @@ void Matrix::rotateX(float degrees) {
     b = data[11];
     data[7] = a * cos + b * sin;
     data[11] = a * -sin + b * cos;
+    return *this;
 }
 
-void Matrix::rotateY(float degrees) {
+Matrix& Matrix::rotateY(float degrees) {
     degrees *= M_PI / 180.0f;
     float sin = sinf(degrees);
     float cos = cosf(degrees);
@@ -168,9 +187,10 @@ void Matrix::rotateY(float degrees) {
     b = data[11];
     data[3] = a * cos + b * -sin;
     data[11] = a * sin + b * cos;
+    return *this;
 }
 
-void Matrix::rotateZ(float degrees) {
+Matrix& Matrix::rotateZ(float degrees) {
     degrees *= M_PI / 180.0f;
     float sin = sinf(degrees);
     float cos = cosf(degrees);
@@ -194,6 +214,7 @@ void Matrix::rotateZ(float degrees) {
     b = data[7];
     data[3] = a * cos + b * sin;
     data[7] = a * -sin + b * cos;
+    return *this;
 }
 
 std::ostream& operator<<(std::ostream& os, const Matrix& m) {

+ 14 - 12
client/math/Matrix.h

@@ -7,24 +7,26 @@ class Matrix final {
 public:
     Matrix();
 
-    void setToIdentity();
-    void set(unsigned int index, float f);
+    Matrix& set(const Matrix& other);
+    Matrix& setToIdentity();
+    Matrix& set(unsigned int index, float f);
 
     const float* getValues() const;
 
-    void mul(const Matrix& m);
+    Matrix& mul(const Matrix& m);
 
-    void scale(float sx, float sy, float sz);
+    Matrix& scale(float sx, float sy, float sz);
+    Matrix& scale(float s);
 
-    void translate(float tx, float ty, float tz);
-    void translateX(float tx);
-    void translateY(float ty);
-    void translateZ(float tz);
-    void translateTo(float tx, float ty, float tz);
+    Matrix& translate(float tx, float ty, float tz);
+    Matrix& translateX(float tx);
+    Matrix& translateY(float ty);
+    Matrix& translateZ(float tz);
+    Matrix& translateTo(float tx, float ty, float tz);
 
-    void rotateX(float degrees);
-    void rotateY(float degrees);
-    void rotateZ(float degrees);
+    Matrix& rotateX(float degrees);
+    Matrix& rotateY(float degrees);
+    Matrix& rotateZ(float degrees);
 
 private:
     float data[16];

+ 3 - 3
client/math/Ray.cpp

@@ -11,9 +11,9 @@ void Ray::store() {
 }
 
 void Ray::set(const Vector& position, float lengthAngle, float widthAngle) {
-    this->position = position;
-    this->lengthAngle = lengthAngle;
-    this->widthAngle = widthAngle;
+    Ray::position = position;
+    Ray::lengthAngle = lengthAngle;
+    Ray::widthAngle = widthAngle;
 }
 
 Vector Ray::getPosition(float lag) const {

BIN
resources/font16x16.png


BIN
resources/font24x24.png


BIN
resources/font8x8.png