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.
This commit is contained in:
Elipzer 2018-09-12 11:22:56 -04:00
parent ee7f2e4006
commit 3205680062
15 changed files with 50 additions and 15 deletions

View File

@ -59,6 +59,7 @@ int Application::run()
float delta_time = m_fps.mark(); float delta_time = m_fps.mark();
clock_t clock = m_fps.get_clock(); clock_t clock = m_fps.get_clock();
update(delta_time, clock); update(delta_time, clock);
prerender();
render(); render();
CHECK_GL_ERR(); CHECK_GL_ERR();
glfwSwapBuffers(m_p_window); glfwSwapBuffers(m_p_window);

View File

@ -31,6 +31,8 @@ protected:
virtual void update(float delta_time, clock_t clock) = 0; virtual void update(float delta_time, clock_t clock) = 0;
virtual void prerender() = 0;
virtual void render() = 0; virtual void render() = 0;
// Called on closing of the application (called before base_close) // Called on closing of the application (called before base_close)

View File

@ -11,13 +11,13 @@ class Camera
public: public:
Camera() : m_view_matrix(1.0f), m_projection_matrix(1.0f) {} 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: protected:
mat4x4 m_view_matrix; mat4 m_view_matrix;
mat4x4 m_projection_matrix; mat4 m_projection_matrix;
}; };

View File

@ -69,14 +69,12 @@ void Camera3D::rotate(const vec3& axis, float angle)
void Camera3D::prerender() void Camera3D::prerender()
{ {
// This works but it is backwards. // This works but it mirrors the scene left and right?
m_view_matrix = glm::lookAt(m_position, m_position + vec3(m_forward), vec3(m_up));
/*
m_view_matrix = mat4( m_view_matrix = mat4(
vec4(m_right.x, m_up.x, -m_forward.x, 0.0f), 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.y, m_up.y, -m_forward.y, 0.0f),
vec4(m_right.z, m_up.z, -m_forward.z, 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)
); );
*/
} }

View File

@ -36,6 +36,11 @@ void MyApplication::update(float delta_time, clock_t clock)
m_p_current_scene->update(delta_time, clock); m_p_current_scene->update(delta_time, clock);
} }
void MyApplication::prerender()
{
m_p_current_scene->prerender();
}
void MyApplication::render() void MyApplication::render()
{ {
m_p_current_scene->render(); m_p_current_scene->render();

View File

@ -17,6 +17,8 @@ protected:
void update(float delta_time, clock_t clock) override; void update(float delta_time, clock_t clock) override;
void prerender() override;
void render() override; void render() override;
void close() override; void close() override;

View File

@ -43,6 +43,10 @@ void MyBasicScene::update(float delta_time, clock_t clock)
{ {
} }
void MyBasicScene::prerender()
{
}
void MyBasicScene::render() void MyBasicScene::render()
{ {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

View File

@ -18,6 +18,8 @@ public:
void update(float delta_time, clock_t clock) override; void update(float delta_time, clock_t clock) override;
void prerender() override;
void render() override; void render() override;
private: private:

View File

@ -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; if (m_input_manager.is_key_down(GLFW_KEY_D)) camera_translation.x += 1;
m_camera.translate(camera_translation * delta_time * 100.0f); 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_camera.prerender();
m_batch.prerender(); m_batch.prerender();
} }

View File

@ -21,6 +21,8 @@ public:
void update(float delta_time, clock_t clock) override; void update(float delta_time, clock_t clock) override;
void prerender() override;
void render() override; void render() override;
private: private:

View File

@ -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; if (m_input_manager.is_key_down(GLFW_KEY_E)) camera_translation.z += 1;
m_camera.translate(camera_translation * delta_time); m_camera.translate(camera_translation * delta_time);
}
// TODO: Make a prerender function or move this to render void MySimple3DScene::prerender()
{
m_camera.prerender(); m_camera.prerender();
m_batch.prerender(); m_batch.prerender();
} }

View File

@ -21,6 +21,8 @@ public:
void update(float delta_time, clock_t clock) override; void update(float delta_time, clock_t clock) override;
void prerender() override;
void render() override; void render() override;
private: private:

View File

@ -19,7 +19,7 @@ MySimpleCubeScene::MySimpleCubeScene(Application& application)
MySimpleShaderProgram::Vertex( 1.0f, -1.0f, 1.0f) MySimpleShaderProgram::Vertex( 1.0f, -1.0f, 1.0f)
), DrawMode::DRAW_TRIANGLES), ), DrawMode::DRAW_TRIANGLES),
m_batch(&m_shape, 1), 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() 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_Q)) camera_translation.z -= 1;
if (m_input_manager.is_key_down(GLFW_KEY_E)) 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_camera.prerender();
m_batch.prerender(); m_batch.prerender();
} }

View File

@ -21,6 +21,8 @@ public:
void update(float delta_time, clock_t clock) override; void update(float delta_time, clock_t clock) override;
void prerender() override;
void render() override; void render() override;
private: private:

View File

@ -29,6 +29,10 @@ public:
// Called when the frame is being updated // Called when the frame is being updated
virtual void update(float delta_time, clock_t clock) = 0; 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 // Called when the frame is being rendered
virtual void render() = 0; virtual void render() = 0;
protected: protected: