From 2bdc1655725b022e042eec6d640c7aa5679e3c8b Mon Sep 17 00:00:00 2001 From: elipzer Date: Mon, 10 Sep 2018 20:42:50 -0400 Subject: [PATCH] Quick fixed Poseable vtable issue with glVertexAttribPointer Poseable is not a struct because it has a vtable. This means that it cannot be used easily with glVertexAttribPointer. This quick fix should be overwritten by a full fix that stores the matrix data in a seperate vector and modifies the matrices with poseable interfaces. (Will go into more detail in that commit.) --- OpenGLEngine/MyBatch.cpp | 17 +++++++-- OpenGLEngine/MyBatchTestFragmentShader.glsl | 2 +- OpenGLEngine/MyBatchTestVertexShader.glsl | 2 +- OpenGLEngine/MyObjectOrientedScene.cpp | 42 ++++++++++++++------- OpenGLEngine/Poseable.h | 2 +- 5 files changed, 45 insertions(+), 20 deletions(-) diff --git a/OpenGLEngine/MyBatch.cpp b/OpenGLEngine/MyBatch.cpp index 93edb4c..f1e509c 100644 --- a/OpenGLEngine/MyBatch.cpp +++ b/OpenGLEngine/MyBatch.cpp @@ -19,14 +19,23 @@ void MyBatch::setup_vao() glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, NULL); glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[POSEABLE_VBO_INDEX]); glEnableVertexAttribArray(2); - 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); + glEnableVertexAttribArray(3); + glEnableVertexAttribArray(4); + glEnableVertexAttribArray(5); + + int orientation_location = offsetof(Poseable, m_orientation_matrix); + + glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(orientation_location + 0 * sizeof(vec4))); + glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(orientation_location + 1 * sizeof(vec4))); + glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(orientation_location + 2 * sizeof(vec4))); + glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(orientation_location + 3 * sizeof(vec4))); 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 glVertexAttribDivisor(2, 1); // Send the offset data for each instance drawn + glVertexAttribDivisor(3, 1); // Send the offset data for each instance drawn + glVertexAttribDivisor(4, 1); // Send the offset data for each instance drawn + glVertexAttribDivisor(5, 1); // Send the offset data for each instance drawn } void MyBatch::update_element_buffers() diff --git a/OpenGLEngine/MyBatchTestFragmentShader.glsl b/OpenGLEngine/MyBatchTestFragmentShader.glsl index 51980c7..2fd2ef0 100644 --- a/OpenGLEngine/MyBatchTestFragmentShader.glsl +++ b/OpenGLEngine/MyBatchTestFragmentShader.glsl @@ -1,4 +1,4 @@ -#version 400 +#version 430 in vec4 fragment_color; out vec4 frag_color; diff --git a/OpenGLEngine/MyBatchTestVertexShader.glsl b/OpenGLEngine/MyBatchTestVertexShader.glsl index 94e4ea1..f49b865 100644 --- a/OpenGLEngine/MyBatchTestVertexShader.glsl +++ b/OpenGLEngine/MyBatchTestVertexShader.glsl @@ -10,5 +10,5 @@ out vec4 fragment_color; void main() { fragment_color = vertex_color; - gl_Position = pv * vec4(vertex_position, 1.0); + gl_Position = pv * m * vec4(vertex_position, 1.0); } \ No newline at end of file diff --git a/OpenGLEngine/MyObjectOrientedScene.cpp b/OpenGLEngine/MyObjectOrientedScene.cpp index ce213de..bb99211 100644 --- a/OpenGLEngine/MyObjectOrientedScene.cpp +++ b/OpenGLEngine/MyObjectOrientedScene.cpp @@ -11,18 +11,21 @@ #include "MeshFactory.h" #define FIRST_VERT -50.0f, 50.0f, 0.0f +#define SECOND_VERT 50.0f, 150.0f, 0.0f +#define THIRD_VERT -100.0f, -50.0f, 0.0f +#define FOURTH_VERT 100.0f, -50.0f, 0.0f MyObjectOrientedScene::MyObjectOrientedScene(Application& application) : Scene(application), m_shape(MeshFactory::gen( DrawMode::DRAW_TRIANGLES, 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) + MyBatchTestShaderProgram::Vertex(SECOND_VERT), + MyBatchTestShaderProgram::Vertex(THIRD_VERT), + MyBatchTestShaderProgram::Vertex(FOURTH_VERT) ), DrawMode::DRAW_TRIANGLES), m_batch(&m_shape, 2), - m_camera(vec3(m_screen_size, 4.0f))// TODO: change this back to just m_screen_size + m_camera(vec3(m_screen_size, 2.0f))// TODO: change this back to just m_screen_size { } @@ -70,7 +73,7 @@ void MyObjectOrientedScene::update(float delta_time, clock_t clock) color.a = 1.0f; Poseable& pose = m_batch.get_pose(0); - pose.update_position(vec3(3 * (float)cos(radians), 0.0f, 0.0f)); + pose.update_position(vec3(cos(radians) * 50, 2.0f, 0.0f)); } { @@ -81,7 +84,7 @@ void MyObjectOrientedScene::update(float delta_time, clock_t clock) color.a = 1.0f; Poseable& pose = m_batch.get_pose(1); - pose.update_position(vec3(0.0f, 3 * (float)sin(radians), 0.0f)); + pose.update_position(vec3(-3.0f, -2.0f, 0.0f)); } vec2 camera_translation(0.0f, 0.0f); @@ -93,19 +96,29 @@ void MyObjectOrientedScene::update(float delta_time, clock_t clock) m_camera.translate(camera_translation * delta_time * 100.0f); + const mat4& projection = m_camera.get_projection_matrix(); + const mat4& camera_orientation = m_camera.get_orientation_matrix(); const mat4& world_to_view = m_camera.get_world_to_view_matrix(); const mat4& orientation = m_batch.get_pose(0).get_orientation_matrix(); + + const mat4 mvp = world_to_view * orientation; + vec4 first_vert(FIRST_VERT, 1.0f); + vec4 second_vert(SECOND_VERT, 1.0f); + vec4 third_vert(THIRD_VERT, 1.0f); + vec4 fourth_vert(FOURTH_VERT, 1.0f); Util::set_console_position(0, 0); + std::cout << "Projection" << std::endl; + Util::print_matrix(projection); std::cout << "World to View" << std::endl; - Util::print_matrix(m_camera.get_world_to_view_matrix()); + Util::print_matrix(world_to_view); 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); + Util::print_matrix(orientation); + std::cout << "Full Projection Matrix" << std::endl; + Util::print_matrix(world_to_view * orientation); + std::cout << " First Second Third Fourth " << std::endl; + Util::print_matrix(mat4(mvp * first_vert, mvp * second_vert, mvp * third_vert, mvp * fourth_vert)); // TODO: Make a prerender function or move this to render m_batch.prerender(); @@ -119,4 +132,7 @@ void MyObjectOrientedScene::render() m_batch.render(); } -#undef FIRST_VERT \ No newline at end of file +#undef FIRST_VERT +#undef SECOND_VERT +#undef THIRD_VERT +#undef FOURTH_VERT \ No newline at end of file diff --git a/OpenGLEngine/Poseable.h b/OpenGLEngine/Poseable.h index ac05ced..e2ebd00 100644 --- a/OpenGLEngine/Poseable.h +++ b/OpenGLEngine/Poseable.h @@ -24,6 +24,6 @@ public: const mat4& get_orientation_matrix() const { return m_orientation_matrix; } -protected: + // Please dont use this. If you see it, tell michael that he is bad mat4 m_orientation_matrix; }; \ No newline at end of file