From 32056800621c4753a093b60a34674d52680bb621 Mon Sep 17 00:00:00 2001 From: Elipzer Date: Wed, 12 Sep 2018 11:22:56 -0400 Subject: [PATCH] Fixed Camera Also added a required prerender function to scene and application. This function is intended to be used as a way to prepare the scene to be rendered in its current state. For that reason, the delta time and the current clock tick are not passed to it. --- OpenGLEngine/Application.cpp | 1 + OpenGLEngine/Application.h | 2 ++ OpenGLEngine/Camera.h | 10 +++++----- OpenGLEngine/Camera3D.cpp | 8 +++----- OpenGLEngine/MyApplication.cpp | 5 +++++ OpenGLEngine/MyApplication.h | 2 ++ OpenGLEngine/MyBasicScene.cpp | 4 ++++ OpenGLEngine/MyBasicScene.h | 2 ++ OpenGLEngine/MySimple2DScene.cpp | 4 +++- OpenGLEngine/MySimple2DScene.h | 2 ++ OpenGLEngine/MySimple3DScene.cpp | 4 +++- OpenGLEngine/MySimple3DScene.h | 2 ++ OpenGLEngine/MySimpleCubeScene.cpp | 13 ++++++++++--- OpenGLEngine/MySimpleCubeScene.h | 2 ++ OpenGLEngine/Scene.h | 4 ++++ 15 files changed, 50 insertions(+), 15 deletions(-) diff --git a/OpenGLEngine/Application.cpp b/OpenGLEngine/Application.cpp index a2672f9..c3b4452 100644 --- a/OpenGLEngine/Application.cpp +++ b/OpenGLEngine/Application.cpp @@ -59,6 +59,7 @@ int Application::run() float delta_time = m_fps.mark(); clock_t clock = m_fps.get_clock(); update(delta_time, clock); + prerender(); render(); CHECK_GL_ERR(); glfwSwapBuffers(m_p_window); diff --git a/OpenGLEngine/Application.h b/OpenGLEngine/Application.h index 6de5f5f..89e022e 100644 --- a/OpenGLEngine/Application.h +++ b/OpenGLEngine/Application.h @@ -31,6 +31,8 @@ protected: virtual void update(float delta_time, clock_t clock) = 0; + virtual void prerender() = 0; + virtual void render() = 0; // Called on closing of the application (called before base_close) diff --git a/OpenGLEngine/Camera.h b/OpenGLEngine/Camera.h index 006b6ae..5f1fb7f 100644 --- a/OpenGLEngine/Camera.h +++ b/OpenGLEngine/Camera.h @@ -11,13 +11,13 @@ class Camera public: Camera() : m_view_matrix(1.0f), m_projection_matrix(1.0f) {} - mat4x4 get_world_to_view_matrix() const { return m_projection_matrix * m_view_matrix; } + mat4 get_world_to_view_matrix() const { return m_projection_matrix * m_view_matrix; } - const mat4x4& get_view_matrix() const { return m_view_matrix; } + const mat4& get_view_matrix() const { return m_view_matrix; } - const mat4x4& get_projection_matrix() const { return m_projection_matrix; } + const mat4& get_projection_matrix() const { return m_projection_matrix; } protected: - mat4x4 m_view_matrix; - mat4x4 m_projection_matrix; + mat4 m_view_matrix; + mat4 m_projection_matrix; }; \ No newline at end of file diff --git a/OpenGLEngine/Camera3D.cpp b/OpenGLEngine/Camera3D.cpp index 98a982e..573e4b3 100644 --- a/OpenGLEngine/Camera3D.cpp +++ b/OpenGLEngine/Camera3D.cpp @@ -69,14 +69,12 @@ void Camera3D::rotate(const vec3& axis, float angle) void Camera3D::prerender() { - // This works but it is backwards. - m_view_matrix = glm::lookAt(m_position, m_position + vec3(m_forward), vec3(m_up)); - /* + // This works but it mirrors the scene left and right? m_view_matrix = mat4( vec4(m_right.x, m_up.x, -m_forward.x, 0.0f), vec4(m_right.y, m_up.y, -m_forward.y, 0.0f), vec4(m_right.z, m_up.z, -m_forward.z, 0.0f), - vec4(glm::dot(vec3(m_right), -m_position), glm::dot(vec3(m_up), -m_position), glm::dot(vec3(m_forward), -m_position), 1.0f) + vec4(-glm::dot(vec3(m_right), m_position), -glm::dot(vec3(m_up), m_position), glm::dot(vec3(m_forward), m_position), 1.0f) ); - */ + } \ No newline at end of file diff --git a/OpenGLEngine/MyApplication.cpp b/OpenGLEngine/MyApplication.cpp index e31e541..e879ff4 100644 --- a/OpenGLEngine/MyApplication.cpp +++ b/OpenGLEngine/MyApplication.cpp @@ -36,6 +36,11 @@ void MyApplication::update(float delta_time, clock_t clock) m_p_current_scene->update(delta_time, clock); } +void MyApplication::prerender() +{ + m_p_current_scene->prerender(); +} + void MyApplication::render() { m_p_current_scene->render(); diff --git a/OpenGLEngine/MyApplication.h b/OpenGLEngine/MyApplication.h index cbcb605..9a99236 100644 --- a/OpenGLEngine/MyApplication.h +++ b/OpenGLEngine/MyApplication.h @@ -17,6 +17,8 @@ protected: void update(float delta_time, clock_t clock) override; + void prerender() override; + void render() override; void close() override; diff --git a/OpenGLEngine/MyBasicScene.cpp b/OpenGLEngine/MyBasicScene.cpp index b2ba12c..d6a0433 100644 --- a/OpenGLEngine/MyBasicScene.cpp +++ b/OpenGLEngine/MyBasicScene.cpp @@ -43,6 +43,10 @@ void MyBasicScene::update(float delta_time, clock_t clock) { } +void MyBasicScene::prerender() +{ +} + void MyBasicScene::render() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); diff --git a/OpenGLEngine/MyBasicScene.h b/OpenGLEngine/MyBasicScene.h index 4a88abb..36be4eb 100644 --- a/OpenGLEngine/MyBasicScene.h +++ b/OpenGLEngine/MyBasicScene.h @@ -18,6 +18,8 @@ public: void update(float delta_time, clock_t clock) override; + void prerender() override; + void render() override; private: diff --git a/OpenGLEngine/MySimple2DScene.cpp b/OpenGLEngine/MySimple2DScene.cpp index 0afb21a..336224c 100644 --- a/OpenGLEngine/MySimple2DScene.cpp +++ b/OpenGLEngine/MySimple2DScene.cpp @@ -88,8 +88,10 @@ void MySimple2DScene::update(float delta_time, clock_t clock) if (m_input_manager.is_key_down(GLFW_KEY_D)) camera_translation.x += 1; m_camera.translate(camera_translation * delta_time * 100.0f); +} - // TODO: Make a prerender function or move this to render +void MySimple2DScene::prerender() +{ m_camera.prerender(); m_batch.prerender(); } diff --git a/OpenGLEngine/MySimple2DScene.h b/OpenGLEngine/MySimple2DScene.h index 099910e..ad1654c 100644 --- a/OpenGLEngine/MySimple2DScene.h +++ b/OpenGLEngine/MySimple2DScene.h @@ -21,6 +21,8 @@ public: void update(float delta_time, clock_t clock) override; + void prerender() override; + void render() override; private: diff --git a/OpenGLEngine/MySimple3DScene.cpp b/OpenGLEngine/MySimple3DScene.cpp index 27343cd..89aac4d 100644 --- a/OpenGLEngine/MySimple3DScene.cpp +++ b/OpenGLEngine/MySimple3DScene.cpp @@ -90,8 +90,10 @@ void MySimple3DScene::update(float delta_time, clock_t clock) if (m_input_manager.is_key_down(GLFW_KEY_E)) camera_translation.z += 1; m_camera.translate(camera_translation * delta_time); +} - // TODO: Make a prerender function or move this to render +void MySimple3DScene::prerender() +{ m_camera.prerender(); m_batch.prerender(); } diff --git a/OpenGLEngine/MySimple3DScene.h b/OpenGLEngine/MySimple3DScene.h index 9e7a19e..b01ee8a 100644 --- a/OpenGLEngine/MySimple3DScene.h +++ b/OpenGLEngine/MySimple3DScene.h @@ -21,6 +21,8 @@ public: void update(float delta_time, clock_t clock) override; + void prerender() override; + void render() override; private: diff --git a/OpenGLEngine/MySimpleCubeScene.cpp b/OpenGLEngine/MySimpleCubeScene.cpp index 554f7ba..bb69fb8 100644 --- a/OpenGLEngine/MySimpleCubeScene.cpp +++ b/OpenGLEngine/MySimpleCubeScene.cpp @@ -19,7 +19,7 @@ MySimpleCubeScene::MySimpleCubeScene(Application& application) MySimpleShaderProgram::Vertex( 1.0f, -1.0f, 1.0f) ), DrawMode::DRAW_TRIANGLES), m_batch(&m_shape, 1), - m_camera((float)egm::TAU_1_8, (float)m_screen_size.x / m_screen_size.y, 1.0f, 10.0f, vec3(0.0f, 0.0f, -5.0f)) + 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)) {} MySimpleCubeScene::~MySimpleCubeScene() @@ -78,9 +78,16 @@ void MySimpleCubeScene::update(float delta_time, clock_t clock) if (m_input_manager.is_key_down(GLFW_KEY_Q)) camera_translation.z -= 1; if (m_input_manager.is_key_down(GLFW_KEY_E)) camera_translation.z += 1; - m_camera.translate(camera_translation * delta_time); + float camera_rotation = 0.0f; + if (m_input_manager.is_key_down(GLFW_KEY_Z)) camera_rotation += 1; + if (m_input_manager.is_key_down(GLFW_KEY_C)) camera_rotation -= 1; - // TODO: Make a prerender function or move this to render + m_camera.translate(camera_translation * delta_time); + m_camera.rotate(vec3(0.0f, 1.0f, 0.0f), camera_rotation * (float)egm::TAU_1_8 * delta_time); +} + +void MySimpleCubeScene::prerender() +{ m_camera.prerender(); m_batch.prerender(); } diff --git a/OpenGLEngine/MySimpleCubeScene.h b/OpenGLEngine/MySimpleCubeScene.h index 6e00d2b..2dd5456 100644 --- a/OpenGLEngine/MySimpleCubeScene.h +++ b/OpenGLEngine/MySimpleCubeScene.h @@ -21,6 +21,8 @@ public: void update(float delta_time, clock_t clock) override; + void prerender() override; + void render() override; private: diff --git a/OpenGLEngine/Scene.h b/OpenGLEngine/Scene.h index 8e605ad..7678030 100644 --- a/OpenGLEngine/Scene.h +++ b/OpenGLEngine/Scene.h @@ -29,6 +29,10 @@ public: // Called when the frame is being updated virtual void update(float delta_time, clock_t clock) = 0; + // Called before the scene is rendered. + // Intended to prepare objects for rendering (generally by calling their prerender function) + virtual void prerender() = 0; + // Called when the frame is being rendered virtual void render() = 0; protected: