2018-09-11 05:18:17 +00:00
|
|
|
#include "MySimple2DScene.h"
|
|
|
|
|
2018-10-11 05:26:24 +00:00
|
|
|
#include <charcoal/deps.h>
|
|
|
|
#include <charcoal/constants.h>
|
|
|
|
#include <charcoal/DrawMode.h>
|
|
|
|
#include <charcoal/MeshFactory.h>
|
2018-09-11 05:18:17 +00:00
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
MySimple2DScene::MySimple2DScene(Application& application)
|
|
|
|
: Scene(application),
|
|
|
|
m_shape(MeshFactory<MySimpleShaderProgram::Vertex, MySimpleShaderProgram::Index>::gen(
|
|
|
|
DrawMode::DRAW_TRIANGLES,
|
|
|
|
MySimpleShaderProgram::Vertex(-50.0f, 50.0f, 0.0f),
|
|
|
|
MySimpleShaderProgram::Vertex(50.0f, 150.0f, 0.0f),
|
|
|
|
MySimpleShaderProgram::Vertex(-100.0f, -50.0f, 0.0f),
|
|
|
|
MySimpleShaderProgram::Vertex(100.0f, -50.0f, 0.0f)
|
|
|
|
), DrawMode::DRAW_TRIANGLES),
|
|
|
|
m_batch(&m_shape, 2),
|
|
|
|
m_camera(m_screen_size)
|
|
|
|
{}
|
|
|
|
|
|
|
|
MySimple2DScene::~MySimple2DScene()
|
|
|
|
{}
|
|
|
|
|
|
|
|
void MySimple2DScene::init()
|
|
|
|
{
|
|
|
|
m_batch.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MySimple2DScene::use()
|
|
|
|
{
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glDepthFunc(GL_LESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MySimple2DScene::unuse()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void MySimple2DScene::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;
|
|
|
|
|
2018-09-12 21:03:46 +00:00
|
|
|
radians = (float)TAU * c / intervals;
|
2018-09-11 05:18:17 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
MySimpleShaderProgram::Color& color = m_batch.get_color(0);
|
|
|
|
color.r = brightness;
|
|
|
|
color.g = brightness;
|
|
|
|
color.b = brightness;
|
|
|
|
color.a = 1.0f;
|
|
|
|
|
|
|
|
Poseable& pose = m_batch.get_pose(0);
|
|
|
|
pose.update_position(vec3(cos(radians) * 50, 0.0f, 0.0f));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
MySimpleShaderProgram::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;
|
|
|
|
|
|
|
|
Poseable& pose = m_batch.get_pose(1);
|
|
|
|
pose.update_position(vec3(0.0f, sin(radians) * 50, 0.0f));
|
|
|
|
}
|
|
|
|
|
|
|
|
vec2 camera_translation(0.0f, 0.0f);
|
|
|
|
|
|
|
|
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 * 100.0f);
|
2018-09-12 15:22:56 +00:00
|
|
|
}
|
2018-09-11 05:18:17 +00:00
|
|
|
|
2018-09-12 15:22:56 +00:00
|
|
|
void MySimple2DScene::prerender()
|
|
|
|
{
|
2018-09-12 13:46:55 +00:00
|
|
|
m_camera.prerender();
|
2018-09-11 05:18:17 +00:00
|
|
|
m_batch.prerender();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MySimple2DScene::render()
|
|
|
|
{
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
m_shader_program.use();
|
|
|
|
glUniformMatrix4fv(0, 1, GL_FALSE, &m_camera.get_world_to_view_matrix()[0][0]);
|
|
|
|
m_batch.render();
|
|
|
|
}
|