Переглянути джерело

Add some graphics helper functions

Sam Jaffe 6 роки тому
батько
коміт
1c2b3f32b0

+ 6 - 0
graphics/graphics.xcodeproj/project.pbxproj

@@ -7,6 +7,7 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
+		CD1C82BD22988EC700825C4E /* matrix.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD1C82BC22988EC700825C4E /* matrix.cxx */; };
 		CD3AC6F21D2C03B7002B4BB0 /* material.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD3AC6F01D2C03B7002B4BB0 /* material.cpp */; };
 		CD3AC6F81D2C0518002B4BB0 /* texture.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD3AC6F61D2C0518002B4BB0 /* texture.cpp */; };
 		CD3AC6FD1D2C06B5002B4BB0 /* shader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD3AC6FB1D2C06B5002B4BB0 /* shader.cpp */; };
@@ -54,6 +55,8 @@
 /* End PBXContainerItemProxy section */
 
 /* Begin PBXFileReference section */
+		CD1C82B722988E4E00825C4E /* matrix.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = matrix.hpp; sourceTree = "<group>"; };
+		CD1C82BC22988EC700825C4E /* matrix.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = matrix.cxx; sourceTree = "<group>"; };
 		CD3AC6E21D2C0364002B4BB0 /* libgraphics.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = libgraphics.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
 		CD3AC6F01D2C03B7002B4BB0 /* material.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = material.cpp; sourceTree = "<group>"; };
 		CD3AC6F61D2C0518002B4BB0 /* texture.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = texture.cpp; sourceTree = "<group>"; };
@@ -144,6 +147,8 @@
 			children = (
 				CD62FD1C2292412900376440 /* renderer.cxx */,
 				CD62FD202292C76B00376440 /* renderer_impl.hpp */,
+				CD1C82B722988E4E00825C4E /* matrix.hpp */,
+				CD1C82BC22988EC700825C4E /* matrix.cxx */,
 				CD62FD212292C76B00376440 /* opengl_renderer.cxx */,
 			);
 			name = renderer;
@@ -287,6 +292,7 @@
 			files = (
 				CD3AC6FD1D2C06B5002B4BB0 /* shader.cpp in Sources */,
 				CD3AC7191D2C0950002B4BB0 /* shader_program.cpp in Sources */,
+				CD1C82BD22988EC700825C4E /* matrix.cxx in Sources */,
 				CD3AC6F21D2C03B7002B4BB0 /* material.cpp in Sources */,
 				CD62FD1E2292412900376440 /* renderer.cxx in Sources */,
 				CD3AC6F81D2C0518002B4BB0 /* texture.cpp in Sources */,

+ 50 - 0
graphics/src/matrix.cxx

@@ -0,0 +1,50 @@
+//
+//  matrix.cxx
+//  graphics
+//
+//  Created by Sam Jaffe on 5/24/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#include "matrix.hpp"
+
+#include "game/math/angle.hpp"
+#include "matrix/matrix.hpp"
+
+namespace graphics {
+  math::matr4 orthogonal_view(float left, float right, float bottom, float top,
+                              float near, float far) {
+    math::matr4 matrix;
+    matrix(0, 0) = 2.f / (right - left);
+    matrix(1, 1) = 2.f / (top - bottom);
+    matrix(2, 2) = -2.f / (far - near);
+    matrix(0, 3) = -(right + left) / (right - left);
+    matrix(1, 3) = -(top + bottom) / (top - bottom);
+    matrix(2, 3) = -(far + near) / (far - near);
+    matrix(3, 3) = 1.f;
+    return matrix;
+  }
+
+  math::matr4 frustum_view(float left, float right, float bottom, float top,
+                           float near, float far) {
+    math::matr4 matrix;
+    matrix(0, 0) = 2.f * near / (right - left);
+    matrix(1, 1) = 2.f * near / (top - bottom);
+    matrix(2, 0) = (right + left) / (right - left);
+    matrix(2, 1) = (top + bottom) / (top - bottom);
+    matrix(2, 2) = -(far + near) / (far - near);
+    matrix(2, 3) = -1.f;
+    matrix(3, 2) = -(2.f * far * near) / (far - near);
+    matrix(3, 3) = 0.f;
+    return matrix;
+  }
+
+  math::matr4 perspective(math::degree fovY, float aspect, float front,
+                          float back) {
+    float tangent = tan(fovY);
+    float height = front * tangent;
+    float width = height * aspect;
+
+    return frustum_view(-width, width, -height, height, front, back);
+  }
+}

+ 20 - 0
graphics/src/matrix.hpp

@@ -0,0 +1,20 @@
+//
+//  matrix.hpp
+//  graphics
+//
+//  Created by Sam Jaffe on 5/24/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#pragma once
+
+#include "game/math/math_fwd.hpp"
+
+namespace graphics {
+  math::matr4 orthogonal_view(float left, float right, float bottom, float top,
+                              float near, float far);
+  math::matr4 frustum_view(float left, float right, float bottom, float top,
+                           float near, float far);
+  math::matr4 perspective(math::degree fovY, float aspect, float front,
+                          float back);
+}

+ 18 - 8
graphics/src/opengl_renderer.cxx

@@ -14,6 +14,9 @@
 
 #include "game/graphics/material.hpp"
 #include "game/graphics/vertex.h"
+#include "game/util/env.hpp"
+#include "game/util/time.hpp"
+#include "matrix.hpp"
 #include "matrix/matrix.hpp"
 #include "matrix/matrix_helpers.hpp"
 
@@ -51,19 +54,25 @@ opengl_renderer::~opengl_renderer() {
 void opengl_renderer::clear() {
   world_to_clip = identity;
 
-  // vec2i resolution = env::resolution();
-  // orthogonal_view(0.0, resolution[0], 0.0, resolution[1], -1.0, 1.0);
+  // math::vec2i resolution = env::resolution();
+  math::vec2i resolution = env::screen_size();
+  world_to_clip = graphics::orthogonal_view(0.f, resolution[0], 0.f,
+                                            resolution[1], -1.f, 1.f);
 
   glClearDepth(1.f);
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glClearColor(0.f, 0.f, 0.0f, 1.f);
   glEnable(GL_DEPTH_TEST);
 
-  // TODO: Use a unified time calculation
-  current_time = time(NULL);
+  // TODO (sjaffe): Refactor this out...
+  current_time =
+      std::chrono::duration<double>(engine::clock::now().time_since_epoch())
+          .count();
 
-  // TODO: ???
-  // scale(0.5, 0.5, 1); // Don't know why I need to do this
+  // TODO (sjaffe): I labeled this as needed before, but I don't know why
+  //      I think it has to do with the resolution being 2x the screen size
+  //  static auto const rescale = make_vector(0.5f, 0.5f, 1.f);
+  //  world_to_clip = world_to_clip * math::matrix::scalar(rescale);
 }
 
 void opengl_renderer::flush() { glFlush(); }
@@ -78,10 +87,11 @@ void opengl_renderer::draw(flyweight<material> material,
   mat.activate();
 
   int objectLocation = glGetUniformLocation(id, "u_objectToWorldMatrix");
-  glUniformMatrix4fv(objectLocation, 1, false, &object_to_world(0, 0));
+  // Argument 2: GL_TRUE -> row-major ; GL_FALSE -> column-major
+  glUniformMatrix4fv(objectLocation, 1, GL_TRUE, &object_to_world(0, 0));
 
   int clipLocation = glGetUniformLocation(id, "u_worldToClipMatrix");
-  glUniformMatrix4fv(clipLocation, 1, false, &world_to_clip(0, 0));
+  glUniformMatrix4fv(clipLocation, 1, GL_TRUE, &world_to_clip(0, 0));
 
   int timeLocation = glGetUniformLocation(id, "u_time");
   glUniform1d(timeLocation, current_time);

+ 1 - 0
math/include/game/math/angle.hpp

@@ -22,4 +22,5 @@ namespace math {
 
   double sin(radian r);
   double cos(radian r);
+  double tan(radian r);
 }

+ 1 - 0
math/src/angle.cpp

@@ -18,4 +18,5 @@ namespace math {
 
   double sin(radian r) { return std::sin(r.value); }
   double cos(radian r) { return std::cos(r.value); }
+  double tan(radian r) { return std::tan(r.value); }
 }