charcoal/OpenGLEngine/My3DScene.cpp

141 lines
4.2 KiB
C++
Raw Normal View History

#include "My3DScene.h"
#include "stdafx.h"
#include <cmath>
#include <iostream>
#include "constants.h"
#include "DrawMode.h"
#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<MyBatchTestShaderProgram::Vertex, MyBatchTestShaderProgram::Index>::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)
), 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))
{}
My3DScene::~My3DScene()
{}
void My3DScene::init()
{
m_batch.init();
}
void My3DScene::use()
{
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
}
void My3DScene::unuse()
{
}
void My3DScene::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 = 0.5f;
color.g = 0.5f;
color.b = 0.5f;
color.a = 1.0f;
MyBatchTestShaderProgram::Offset& offset = m_batch.get_offset(0);
offset.x = 1.0f;// 200 * (float)cos(radians);
offset.y = -1.0f;
offset.z = 0.0f;
}
{
MyBatchTestShaderProgram::Color& color = m_batch.get_color(1);
color.r = 1.0f;
color.g = 1.0f;
color.b = 1.0f;
color.a = 1.0f;
MyBatchTestShaderProgram::Offset& offset = m_batch.get_offset(1);
offset.x = 0.0f;
offset.y = 0.0f;// 200 * (float)sin(radians);
offset.z = 0.0f;
}
vec3 camera_translation(0.0f, 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;
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);
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();
}
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]);
m_batch.render();
}
#undef NEAR_PLANE
#undef FAR_PLANE