#include "MyObjectOrientedScene.h" #include #include "constants.h" #include "DrawMode.h" #include "MeshFactory.h" MyObjectOrientedScene::MyObjectOrientedScene(Application& application) : Scene(application), m_shape(MeshFactory::gen( DrawMode::DRAW_TRIANGLES, 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) ), DrawMode::DRAW_TRIANGLES), m_batch(&m_shape, 2), m_camera(m_screen_size) { } MyObjectOrientedScene::~MyObjectOrientedScene() { } void MyObjectOrientedScene::init() { m_batch.init(); } void MyObjectOrientedScene::use() { glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); } void MyObjectOrientedScene::unuse() { } void MyObjectOrientedScene::update(float delta_time, clock_t clock) { float brightness; float radians; clock_t c; const clock_t intervals = 512 * CLOCKS_PER_SEC / 100; const clock_t half_interval = 256 * CLOCKS_PER_SEC / 100; c = clock % intervals; if (c < half_interval) brightness = (float)c / half_interval; else brightness = (float)(intervals - c) / half_interval; radians = (float)egm::TAU * c / intervals; { MyBatchTestShaderProgram::Color& color = m_batch.get_color(0); color.r = brightness; color.g = brightness; color.b = brightness; color.a = 1.0f; MyBatchTestShaderProgram::Offset& offset = m_batch.get_offset(0); offset.x = 200 * (float)cos(radians); offset.y = 0.0f; offset.z = 0.0f; } { MyBatchTestShaderProgram::Color& color = m_batch.get_color(1); color.r = 1.0f - brightness; color.g = 1.0f - brightness; color.b = 1.0f - brightness; color.a = 1.0f; MyBatchTestShaderProgram::Offset& offset = m_batch.get_offset(1); offset.x = 0.0f; offset.y = 200 * (float)sin(radians); offset.z = 0.0f; } vec2 camera_translation; if (m_input_manager.is_key_down(GLFW_KEY_W)) camera_translation.y += 1; if (m_input_manager.is_key_down(GLFW_KEY_S)) camera_translation.y -= 1; 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); m_camera.update_view_projection_matrix(); // TODO: Make a prerender function or move this to render m_batch.prerender(); } void MyObjectOrientedScene::render() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); m_shader_program.use(); glUniformMatrix4fv(3, 1, GL_TRUE, &m_camera.get_view_projection_matrix().a.x); m_batch.render(); }