From a6b8382b54379e7d2c37b6768293d34460524fc2 Mon Sep 17 00:00:00 2001 From: elipzer Date: Mon, 10 Sep 2018 00:42:09 -0400 Subject: [PATCH] Camera3D Works! Multiplication for the world to view matrix was just backwards. --- OpenGLEngine/Camera.h | 2 +- OpenGLEngine/My3DScene.cpp | 41 ++++---------------------- OpenGLEngine/MyObjectOrientedScene.cpp | 31 +++---------------- 3 files changed, 11 insertions(+), 63 deletions(-) diff --git a/OpenGLEngine/Camera.h b/OpenGLEngine/Camera.h index 8aab9f2..369bfc0 100644 --- a/OpenGLEngine/Camera.h +++ b/OpenGLEngine/Camera.h @@ -17,7 +17,7 @@ public: ) : Poseable(position, forward, up, right), m_projection_matrix(1.0f) {} - mat4x4 get_view_projection_matrix() const { return m_orientation_matrix * m_projection_matrix; } + mat4x4 get_world_to_view_matrix() const { return m_projection_matrix * m_orientation_matrix; } const mat4x4& get_projection_matrix() const { return m_projection_matrix; } diff --git a/OpenGLEngine/My3DScene.cpp b/OpenGLEngine/My3DScene.cpp index 241c7b1..f3d4239 100644 --- a/OpenGLEngine/My3DScene.cpp +++ b/OpenGLEngine/My3DScene.cpp @@ -11,22 +11,17 @@ #include "MeshFactory.h" #include "Util.h" -#define NEAR_PLANE 0.1f -#define FAR_PLANE 1000.0f - -#define FIRST_VERT MyBatchTestShaderProgram::Vertex(-1.0f, 1.0f, -3.0f) - My3DScene::My3DScene(Application& application) : Scene(application), m_shape(MeshFactory::gen( DrawMode::DRAW_TRIANGLES, - FIRST_VERT, - MyBatchTestShaderProgram::Vertex(1.0f, 1.0f, -3.0f), - MyBatchTestShaderProgram::Vertex(-2.0f, -1.0f, -3.0f), - MyBatchTestShaderProgram::Vertex(2.0f, -1.0f, -3.0f) + MyBatchTestShaderProgram::Vertex(-1.0f, 1.0f, 0.0f), + MyBatchTestShaderProgram::Vertex(1.0f, 1.0f, 0.0f), + MyBatchTestShaderProgram::Vertex(-2.0f, -1.0f, 0.0f), + MyBatchTestShaderProgram::Vertex(2.0f, -1.0f, 0.0f) ), DrawMode::DRAW_TRIANGLES), m_batch(&m_shape, 2), - m_camera((float)egm::TAU_1_4, (float)m_screen_size.x / m_screen_size.y, NEAR_PLANE, FAR_PLANE, vec3(0.0f, 0.0f, -0.5f)) + m_camera((float)egm::TAU_1_4, (float)m_screen_size.x / m_screen_size.y, 1.0f, 10.0f, vec3(0.0f, 0.0f, -5.0f)) {} My3DScene::~My3DScene() @@ -101,30 +96,6 @@ void My3DScene::update(float delta_time, clock_t clock) m_camera.translate(camera_translation * delta_time); - const MyBatchTestShaderProgram::Vertex first_vert = FIRST_VERT; - vec4 vertex_shader_position_0; - vec4 vertex_shader_position_1; - vec3 base_vertex = vec3(first_vert.x, first_vert.y, first_vert.z); - const MyBatchTestShaderProgram::Offset& offset_0 = m_batch.get_offset(0); - const MyBatchTestShaderProgram::Offset& offset_1 = m_batch.get_offset(1); - vec3 offset_vec_0 = vec3(offset_0.x, offset_0.y, offset_0.z); - vec3 offset_vec_1 = vec3(offset_1.x, offset_1.y, offset_1.z); - vec4 shader_vec_0 = vec4(base_vertex + offset_vec_0, 1); - vec4 shader_vec_1 = vec4(base_vertex + offset_vec_1, 1); - vertex_shader_position_0 = m_camera.get_view_projection_matrix() * shader_vec_0; - vertex_shader_position_1 = m_camera.get_view_projection_matrix() * shader_vec_1; - - Util::set_console_position(0, 0); - std::cout << "Orientation Matrix:" << std::endl; - Util::print_matrix(m_camera.get_orientation_matrix()); - std::cout << "Projection Matrix:" << std::endl; - Util::print_matrix(m_camera.get_projection_matrix()); - std::cout << "View Projection:" << std::endl; // This was off on row 3 column 3 - Util::print_matrix(m_camera.get_view_projection_matrix()); - std::cout << "0 | Norm(0) | 1 | Norm(1):" << std::endl; - Util::print_matrix(mat4x4(vertex_shader_position_0, vertex_shader_position_0 / vertex_shader_position_0.w, vertex_shader_position_1, vertex_shader_position_1 / vertex_shader_position_1.w)); - - // TODO: Make a prerender function or move this to render m_batch.prerender(); } @@ -133,7 +104,7 @@ void My3DScene::render() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); m_shader_program.use(); - glUniformMatrix4fv(3, 1, GL_FALSE, &m_camera.get_view_projection_matrix()[0][0]); + glUniformMatrix4fv(3, 1, GL_FALSE, &m_camera.get_world_to_view_matrix()[0][0]); m_batch.render(); } diff --git a/OpenGLEngine/MyObjectOrientedScene.cpp b/OpenGLEngine/MyObjectOrientedScene.cpp index 1b40026..6054038 100644 --- a/OpenGLEngine/MyObjectOrientedScene.cpp +++ b/OpenGLEngine/MyObjectOrientedScene.cpp @@ -10,13 +10,11 @@ #include "DrawMode.h" #include "MeshFactory.h" -#define FIRST_VERT MyBatchTestShaderProgram::Vertex(-50.0f, 50.0f, 0.0f) - MyObjectOrientedScene::MyObjectOrientedScene(Application& application) : Scene(application), m_shape(MeshFactory::gen( DrawMode::DRAW_TRIANGLES, - FIRST_VERT, + MyBatchTestShaderProgram::Vertex(-50.0f, 50.0f, 0.0f), MyBatchTestShaderProgram::Vertex(50.0f, 150.0f, 0.0f), MyBatchTestShaderProgram::Vertex(-100.0f, -50.0f, 0.0f), MyBatchTestShaderProgram::Vertex(100.0f, -50.0f, 0.0f) @@ -95,31 +93,10 @@ void MyObjectOrientedScene::update(float delta_time, clock_t clock) if (m_input_manager.is_key_down(GLFW_KEY_A)) camera_translation.x -= 1; if (m_input_manager.is_key_down(GLFW_KEY_D)) camera_translation.x += 1; - m_camera.translate(camera_translation * delta_time); - - const MyBatchTestShaderProgram::Vertex first_vert = FIRST_VERT; - - vec4 vertex_shader_position_0; - vec4 vertex_shader_position_1; - vec3 base_vertex = vec3(first_vert.x, first_vert.y, first_vert.z); - const MyBatchTestShaderProgram::Offset& offset_0 = m_batch.get_offset(0); - const MyBatchTestShaderProgram::Offset& offset_1 = m_batch.get_offset(1); - vec3 offset_vec_0 = vec3(offset_0.x, offset_0.y, offset_0.z); - vec3 offset_vec_1 = vec3(offset_1.x, offset_1.y, offset_1.z); - vec4 shader_vec_0 = vec4(base_vertex + offset_vec_0, 1); - vec4 shader_vec_1 = vec4(base_vertex + offset_vec_1, 1); - vertex_shader_position_0 = m_camera.get_view_projection_matrix() * shader_vec_0; - vertex_shader_position_1 = m_camera.get_view_projection_matrix() * shader_vec_1; + m_camera.translate(camera_translation * delta_time * 100.0f); Util::set_console_position(0, 0); - std::cout << "Orientation Matrix:" << std::endl; - Util::print_matrix(m_camera.get_orientation_matrix()); - std::cout << "Projection Matrix:" << std::endl; - Util::print_matrix(m_camera.get_projection_matrix()); - std::cout << "View Projection:" << std::endl; - Util::print_matrix(m_camera.get_view_projection_matrix()); - std::cout << "0 | Norm(0) | 1 | Norm(1):" << std::endl; - Util::print_matrix(mat4x4(vertex_shader_position_0, vertex_shader_position_0 / vertex_shader_position_0.w, vertex_shader_position_1, vertex_shader_position_1 / vertex_shader_position_1.w)); + Util::print_matrix(m_camera.get_world_to_view_matrix()); // TODO: Make a prerender function or move this to render m_batch.prerender(); @@ -129,6 +106,6 @@ void MyObjectOrientedScene::render() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); m_shader_program.use(); - glUniformMatrix4fv(3, 1, GL_FALSE, &m_camera.get_view_projection_matrix()[0][0]); + glUniformMatrix4fv(3, 1, GL_FALSE, &m_camera.get_world_to_view_matrix()[0][0]); m_batch.render(); }