Working on Model-to-World Matrices

Currently, this feature breaks a bunch of stuff :(. Have to get
poseable to work with the objects.
This commit is contained in:
elipzer 2018-09-10 11:35:02 -04:00
parent a6b8382b54
commit f57c972be0
17 changed files with 111 additions and 86 deletions

View File

@ -2,6 +2,8 @@
#include "Exception.h"
#include "Util.h"
Application::Application(int width, int height)
: m_screen_size(width, height)
{
@ -52,7 +54,6 @@ int Application::run()
try
{
init();
GLenum gl_err;
while (!glfwWindowShouldClose(m_p_window))
{
// Handle all messages
@ -62,11 +63,7 @@ int Application::run()
clock_t clock = m_fps.get_clock();
update(delta_time, clock);
render();
gl_err = glGetError();
if (gl_err != GL_NO_ERROR)
{
throw EXCEPTION("Caught OpenGL Error: " + std::to_string(gl_err));
}
CHECK_GL_ERR();
glfwSwapBuffers(m_p_window);
}

View File

@ -1,14 +0,0 @@
#include "Exception.h"
Exception::Exception(const std::string& message, const std::string& class_name)
: m_message(message), m_class_name(class_name) {}
const std::string& Exception::get_message()
{
return m_message;
}
const std::string& Exception::get_class_name()
{
return m_class_name;
}

View File

@ -3,17 +3,21 @@
#include <string>
// TODO: This MUST be changed to something less generic
#define EXCEPTION(message) Exception(message, typeid(*this).name())
#define EXCEPTION(message) Exception(message, __FILE__, __LINE__)
class Exception
{
public:
Exception(const std::string& message, const std::string& class_name);
Exception(const std::string& message, const std::string& file_name, int line)
: m_message(message), m_file_name(file_name), m_line(line)
{}
const std::string& get_message();
const std::string& get_class_name();
const std::string& get_message() { return m_message; }
const std::string& get_file_name() { return m_file_name; }
int get_line() { return m_line; }
private:
std::string m_message;
std::string m_class_name;
std::string m_file_name;
int m_line;
};

View File

@ -29,6 +29,6 @@ void GLFWInputManager::key_callback(GLFWwindow* p_window, int key, int scancode,
input_manager.key_up(key);
else if (action == GLFW_REPEAT) { /* Ignored */}
else
throw Exception("Invalid GLFW Key Action: " + std::to_string(action) + " (" + std::to_string(scancode) + ")", "class GLFWInputManager");
throw EXCEPTION("Invalid GLFW Key Action: " + std::to_string(action) + " (" + std::to_string(scancode) + ")");
}

View File

@ -50,7 +50,7 @@ public:
case DrawMode::DRAW_TRIANGLES_ADJACENCY:
case DrawMode::DRAW_PATCHES:
default:
throw Exception("Unable to gen for current draw mode: " + std::to_string(draw_mode), "class MeshFactory");
throw EXCEPTION("Unable to gen for current draw mode: " + std::to_string(draw_mode));
}
}
@ -78,7 +78,7 @@ public:
case DrawMode::DRAW_TRIANGLES_ADJACENCY:
case DrawMode::DRAW_PATCHES:
default:
throw Exception("Unable to gen for current draw mode: " + std::to_string(draw_mode), "class MeshFactory");
throw EXCEPTION("Unable to gen for current draw mode: " + std::to_string(draw_mode));
}
}
@ -108,7 +108,7 @@ public:
case DrawMode::DRAW_TRIANGLES_ADJACENCY:
case DrawMode::DRAW_PATCHES:
default:
throw Exception("Unable to gen for current draw mode: " + std::to_string(draw_mode), "class MeshFactory");
throw EXCEPTION("Unable to gen for current draw mode: " + std::to_string(draw_mode));
}
}
@ -152,7 +152,7 @@ public:
case DrawMode::DRAW_TRIANGLES_ADJACENCY:
case DrawMode::DRAW_PATCHES:
default:
throw Exception("Unable to gen for current draw mode: " + std::to_string(draw_mode), "class MeshFactory");
throw EXCEPTION("Unable to gen for current draw mode: " + std::to_string(draw_mode));
}
}
@ -182,7 +182,7 @@ public:
case DrawMode::DRAW_TRIANGLES_ADJACENCY:
case DrawMode::DRAW_PATCHES:
default:
throw Exception("Unable to gen for current draw mode: " + std::to_string(draw_mode), "class MeshFactory");
throw EXCEPTION("Unable to gen for current draw mode: " + std::to_string(draw_mode));
}
}

View File

@ -11,11 +11,13 @@
#include "MeshFactory.h"
#include "Util.h"
#define FIRST_VERT -1.0f, 1.0f, 0.0f
My3DScene::My3DScene(Application& application)
: Scene(application),
m_shape(MeshFactory<MyBatchTestShaderProgram::Vertex, MyBatchTestShaderProgram::Index>::gen(
DrawMode::DRAW_TRIANGLES,
MyBatchTestShaderProgram::Vertex(-1.0f, 1.0f, 0.0f),
MyBatchTestShaderProgram::Vertex(FIRST_VERT),
MyBatchTestShaderProgram::Vertex(1.0f, 1.0f, 0.0f),
MyBatchTestShaderProgram::Vertex(-2.0f, -1.0f, 0.0f),
MyBatchTestShaderProgram::Vertex(2.0f, -1.0f, 0.0f)
@ -61,28 +63,24 @@ void My3DScene::update(float delta_time, clock_t clock)
{
MyBatchTestShaderProgram::Color& color = m_batch.get_color(0);
color.r = 0.5f;
color.g = 0.5f;
color.b = 0.5f;
color.r = brightness;
color.g = brightness;
color.b = brightness;
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;
Poseable& pose = m_batch.get_pose(0);
pose.update_position(vec3(3 * (float)cos(radians), 0.0f, -1.0f));
}
{
MyBatchTestShaderProgram::Color& color = m_batch.get_color(1);
color.r = 1.0f;
color.g = 1.0f;
color.b = 1.0f;
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 = 0.0f;// 200 * (float)sin(radians);
offset.z = 0.0f;
Poseable& pose = m_batch.get_pose(1);
pose.update_position(vec3(0.0f, 3 * (float)sin(radians), 0.0f));
}
vec3 camera_translation(0.0f, 0.0f, 0.0f);
@ -96,6 +94,20 @@ void My3DScene::update(float delta_time, clock_t clock)
m_camera.translate(camera_translation * delta_time);
const mat4& world_to_view = m_camera.get_world_to_view_matrix();
const mat4& orientation = m_batch.get_pose(0).get_orientation_matrix();
const vec4 first_vert(FIRST_VERT, 1.0f);
Util::set_console_position(0, 0);
std::cout << "World to View" << std::endl;
Util::print_matrix(m_camera.get_world_to_view_matrix());
std::cout << "Pose 0 Orientation" << std::endl;
Util::print_matrix(m_batch.get_pose(0).get_orientation_matrix());
std::cout << "First Vert" << std::endl;
Util::print_vec(first_vert);
std::cout << "Projected Position" << std::endl;
Util::print_vec(world_to_view * orientation * first_vert);
// TODO: Make a prerender function or move this to render
m_batch.prerender();
}
@ -104,9 +116,8 @@ void My3DScene::render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_shader_program.use();
glUniformMatrix4fv(3, 1, GL_FALSE, &m_camera.get_world_to_view_matrix()[0][0]);
glUniformMatrix4fv(6, 1, GL_FALSE, &m_camera.get_world_to_view_matrix()[0][0]);
m_batch.render();
}
#undef NEAR_PLANE
#undef FAR_PLANE
#undef FIRST_VERT

View File

@ -5,8 +5,8 @@ void MyBatch::setup_element_buffers()
glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[COLOR_VBO_INDEX]);
glBufferData(GL_ARRAY_BUFFER, m_color_elements.size() * sizeof(MyBatchTestShaderProgram::Color), NULL, GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[OFFSET_VBO_INDEX]);
glBufferData(GL_ARRAY_BUFFER, m_offset_elements.size() * sizeof(MyBatchTestShaderProgram::Offset), NULL, GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[POSEABLE_VBO_INDEX]);
glBufferData(GL_ARRAY_BUFFER, m_poseable_elements.size() * sizeof(Poseable), NULL, GL_STREAM_DRAW);
}
void MyBatch::setup_vao()
@ -17,9 +17,12 @@ void MyBatch::setup_vao()
glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[COLOR_VBO_INDEX]);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[OFFSET_VBO_INDEX]);
glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[POSEABLE_VBO_INDEX]);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, NULL);
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, 0, NULL);
glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, 0, NULL);
glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, 0, NULL);
glVertexAttribDivisor(0, 0); // Only need to send the mesh data once (This should probably be done every time)
glVertexAttribDivisor(1, 1); // Send the color data for each instance drawn
@ -33,8 +36,8 @@ void MyBatch::update_element_buffers()
glBufferData(GL_ARRAY_BUFFER, m_color_elements.size() * sizeof(MyBatchTestShaderProgram::Color), NULL, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, m_color_elements.size() * sizeof(MyBatchTestShaderProgram::Color), m_color_elements.data());
glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[OFFSET_VBO_INDEX]);
glBufferData(GL_ARRAY_BUFFER, m_offset_elements.size() * sizeof(MyBatchTestShaderProgram::Offset), NULL, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, m_offset_elements.size() * sizeof(MyBatchTestShaderProgram::Offset), m_offset_elements.data());
glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[POSEABLE_VBO_INDEX]);
glBufferData(GL_ARRAY_BUFFER, m_poseable_elements.size() * sizeof(Poseable), NULL, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, m_poseable_elements.size() * sizeof(Poseable), m_poseable_elements.data());
}

View File

@ -2,6 +2,8 @@
#include "Batch.h"
#include "Poseable.h"
#include "MyBatchTestShaderProgram.h"
class MyBatch : public Batch<MyBatchTestShaderProgram::Vertex, MyBatchTestShaderProgram::Index, 2>
@ -16,11 +18,11 @@ public:
const MyBatchTestShaderProgram::Renderable* renderable,
const SizeType& element_count,
const SizeType& element_render_count
) : Batch(renderable, element_render_count), m_color_elements(element_count), m_offset_elements(element_count) {}
) : Batch(renderable, element_render_count), m_color_elements(element_count), m_poseable_elements(element_count) {}
MyBatchTestShaderProgram::Color& get_color(const SizeType& index) { return m_color_elements[index]; }
MyBatchTestShaderProgram::Offset& get_offset(const SizeType& index) { return m_offset_elements[index]; }
Poseable& get_pose(const SizeType& index) { return m_poseable_elements[index]; }
protected:
void setup_element_buffers() override;
@ -31,8 +33,8 @@ protected:
private:
const int COLOR_VBO_INDEX = 0;
const int OFFSET_VBO_INDEX = 1;
const int POSEABLE_VBO_INDEX = 1;
std::vector<MyBatchTestShaderProgram::Color> m_color_elements;
std::vector<MyBatchTestShaderProgram::Offset> m_offset_elements;
std::vector<Poseable> m_poseable_elements;
};

View File

@ -1,14 +1,14 @@
#version 430
layout(location = 0) in vec3 vertex_position;
layout(location = 1) in vec4 vertex_color;
layout(location = 2) in vec3 vertex_offset;
layout(location = 2) in mat4 m;
// TODO: Try this with location 0
layout(location = 3) uniform mat4 vp;
layout(location = 6) uniform mat4 pv;
out vec4 fragment_color;
void main()
{
fragment_color = vertex_color;
gl_Position = vp * vec4(vertex_position + vertex_offset, 1.0);
gl_Position = pv * vec4(vertex_position, 1.0);
}

View File

@ -10,17 +10,19 @@
#include "DrawMode.h"
#include "MeshFactory.h"
#define FIRST_VERT -50.0f, 50.0f, 0.0f
MyObjectOrientedScene::MyObjectOrientedScene(Application& application)
: Scene(application),
m_shape(MeshFactory<MyBatchTestShaderProgram::Vertex, MyBatchTestShaderProgram::Index>::gen(
DrawMode::DRAW_TRIANGLES,
MyBatchTestShaderProgram::Vertex(-50.0f, 50.0f, 0.0f),
MyBatchTestShaderProgram::Vertex(FIRST_VERT),
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)
m_camera(vec3(m_screen_size, 4.0f))// TODO: change this back to just m_screen_size
{
}
@ -67,10 +69,8 @@ void MyObjectOrientedScene::update(float delta_time, clock_t clock)
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;
Poseable& pose = m_batch.get_pose(0);
pose.update_position(vec3(3 * (float)cos(radians), 0.0f, 0.0f));
}
{
@ -80,10 +80,8 @@ void MyObjectOrientedScene::update(float delta_time, clock_t clock)
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;
Poseable& pose = m_batch.get_pose(1);
pose.update_position(vec3(0.0f, 3 * (float)sin(radians), 0.0f));
}
vec2 camera_translation(0.0f, 0.0f);
@ -95,8 +93,19 @@ void MyObjectOrientedScene::update(float delta_time, clock_t clock)
m_camera.translate(camera_translation * delta_time * 100.0f);
const mat4& world_to_view = m_camera.get_world_to_view_matrix();
const mat4& orientation = m_batch.get_pose(0).get_orientation_matrix();
vec4 first_vert(FIRST_VERT, 1.0f);
Util::set_console_position(0, 0);
std::cout << "World to View" << std::endl;
Util::print_matrix(m_camera.get_world_to_view_matrix());
std::cout << "Pose 0 Orientation" << std::endl;
Util::print_matrix(m_batch.get_pose(0).get_orientation_matrix());
std::cout << "First Vert" << std::endl;
Util::print_vec(first_vert);
std::cout << "Projected Position" << std::endl;
Util::print_vec(world_to_view * orientation * first_vert);
// TODO: Make a prerender function or move this to render
m_batch.prerender();
@ -106,6 +115,8 @@ void MyObjectOrientedScene::render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_shader_program.use();
glUniformMatrix4fv(3, 1, GL_FALSE, &m_camera.get_world_to_view_matrix()[0][0]);
glUniformMatrix4fv(6, 1, GL_FALSE, &m_camera.get_world_to_view_matrix()[0][0]);
m_batch.render();
}
#undef FIRST_VERT

View File

@ -152,7 +152,6 @@
<ClCompile Include="Application.cpp" />
<ClCompile Include="Camera2D.cpp" />
<ClCompile Include="Camera3D.cpp" />
<ClCompile Include="Exception.cpp" />
<ClCompile Include="FPS.cpp" />
<ClCompile Include="GLFWInputManager.cpp" />
<ClCompile Include="InputManager.cpp" />

View File

@ -57,9 +57,6 @@
<ClCompile Include="main.cpp">
<Filter>Source Files\Example</Filter>
</ClCompile>
<ClCompile Include="Exception.cpp">
<Filter>Source Files\Engine</Filter>
</ClCompile>
<ClCompile Include="stdafx.cpp">
<Filter>Source Files\Engine</Filter>
</ClCompile>

View File

@ -9,10 +9,10 @@ class Poseable
public:
// Assumes that forward, up, and right are orthogonal and normalized
Poseable(
const vec3& position,
const vec3& forward = vec3(0.0f, 0.0f, 1.0f),
const vec3& up = vec3(0.0f, 1.0f, 0.0f),
const vec3& right = vec3(1.0f, 0.0f, 0.0f)
const vec3& position = vec3(0.0f, 0.0f, 0.0f),
const vec3& forward = vec3(0.0f, 0.0f, 1.0f),
const vec3& up = vec3(0.0f, 1.0f, 0.0f),
const vec3& right = vec3(1.0f, 0.0f, 0.0f)
);
virtual void update_position(const vec3& position);

View File

@ -18,7 +18,7 @@ std::string Util::load_file(const std::string& path)
}
catch (std::ios::failure& e)
{
throw Exception(std::string("Error Opening File: ") + e.what(), "class Util");
throw EXCEPTION(std::string("Error Opening File: ") + e.what());
}
if (input_stream.is_open())
{
@ -31,7 +31,7 @@ std::string Util::load_file(const std::string& path)
}
else
{
throw Exception("Unable to access file: " + path, "class Util");
throw EXCEPTION("Unable to access file: " + path);
}
}
@ -73,4 +73,11 @@ void Util::print_vec(const vec4& v)
<< "[" << v.y << "]" << std::endl
<< "[" << v.z << "]" << std::endl
<< "[" << v.w << "]" << std::endl;
}
void Util::_check_gl_err(const char* file_name, int line)
{
GLenum gl_err = glGetError();
if (gl_err != GL_NO_ERROR)
throw Exception(("Caught OpenGL Error: " + std::string((const char*)gluErrorString(gl_err)) + " (" + std::to_string(gl_err) + ")").c_str(), file_name, line);
}

View File

@ -9,6 +9,8 @@ using namespace glm;
#define DISPLAY_DIGITS 5
#define CHECK_GL_ERR() Util::_check_gl_err(__FILE__, __LINE__)
class Util
{
public:
@ -18,4 +20,7 @@ public:
static void print_vec(const vec2& v);
static void print_vec(const vec3& v);
static void print_vec(const vec4& v);
// Use the CHECK_GL_ERR macro unless you know what you are doing with this function
static void _check_gl_err(const char* file_name, int line);
};

View File

@ -16,7 +16,9 @@ int main(int argc, char** argv)
catch (Exception& e)
{
OutputDebugString("Caught Exception: [");
OutputDebugString(e.get_class_name().c_str());
OutputDebugString(e.get_file_name().c_str());
OutputDebugString(":");
OutputDebugString(std::to_string(e.get_line()).c_str());
OutputDebugString("]: ");
OutputDebugString(e.get_message().c_str());
OutputDebugString("\n");

View File

@ -6,4 +6,5 @@
#pragma comment(lib, "glew32.lib")
#pragma comment(lib, "glfw3.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")