From fb3e1e74326ea37e4b72277025d198014a9b20bb Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Sun, 11 Mar 2018 17:30:56 +0100 Subject: [PATCH] Cleanup, using blinn-phong --- data/shaders/fs_default.glsl | 11 +++--- data/shaders/vs_default.glsl | 3 +- src/modules/RenderModule.cc | 75 ++++-------------------------------- src/modules/RenderModule.h | 3 -- src/modules/RenderUtils.cc | 4 +- src/modules/RenderUtils.h | 5 +-- 6 files changed, 18 insertions(+), 83 deletions(-) diff --git a/data/shaders/fs_default.glsl b/data/shaders/fs_default.glsl index e7c4656..43747df 100644 --- a/data/shaders/fs_default.glsl +++ b/data/shaders/fs_default.glsl @@ -16,18 +16,17 @@ void main() { vec4 ambient = ambient_strength * ioFragColor; // diffuse lighting - vec3 norm = normalize(ioNormal); + vec3 normal_dir = normalize(ioNormal); vec3 light_dir = normalize(uLightDirection); - float diff = max(dot(norm, light_dir), 0.0); + float diff = max(dot(normal_dir, light_dir), 0.0); vec4 diffuse = diff * ioFragColor; // specular lighting vec3 view_dir = normalize(uViewPosition - ioFragPosition); - vec3 reflect_dir = reflect(-light_dir, norm); + vec3 halfway_dir = normalize(light_dir + view_dir); - float specular_strength = 0.5; - float spec = pow(max(dot(view_dir, reflect_dir), 0.0), 32); - vec4 specular = specular_strength * spec * vec4(1.0, 1.0, 1.0, 1.0); + float spec = pow(max(dot(normal_dir, halfway_dir), 0.0), 32); + vec4 specular = spec * vec4(0.5); outColor = ambient + diffuse + specular; } diff --git a/data/shaders/vs_default.glsl b/data/shaders/vs_default.glsl index 29d3b7f..549947c 100644 --- a/data/shaders/vs_default.glsl +++ b/data/shaders/vs_default.glsl @@ -9,6 +9,7 @@ in vec4 inColor; uniform mat4 uModelMatrix; uniform mat4 uViewMatrix; uniform mat4 uProjectionMatrix; +uniform mat3 uNormalMatrix; uniform vec3 uLightDirection; uniform vec3 uViewPosition; @@ -19,6 +20,6 @@ out vec3 ioFragPosition; void main() { gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * inCoord; ioFragColor = inColor; - ioNormal = inNormal; + ioNormal = uNormalMatrix * inNormal; ioFragPosition = (uModelMatrix * inCoord).xyz; } diff --git a/src/modules/RenderModule.cc b/src/modules/RenderModule.cc index 1ed1724..d106bde 100644 --- a/src/modules/RenderModule.cc +++ b/src/modules/RenderModule.cc @@ -51,7 +51,7 @@ static const GLfloat g_coordinate_system_vertex_buffer_data[] = { }; VertexArray gVertexArray; -VertexArrayMesh gVertexArrayMesh; +VertexArrayMesh gCoordinateFrameMesh; VertexArrayMesh gXZPlaneMesh; VertexArrayMesh gUnitCubeMesh; @@ -192,7 +192,7 @@ void Renderer::Initialize(int width, int height) { mDefaultTexture.MakeGrid(128, Vector3f (0.8, 0.8f, 0.8f), Vector3f (0.2f, 0.2f, 0.2f)); gVertexArray.Initialize(1000, GL_STATIC_DRAW); - gVertexArrayMesh.Initialize(gVertexArray, 6); + gCoordinateFrameMesh.Initialize(gVertexArray, 6); VertexArray::VertexData vertex_data[] = { {0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 255, 0, 0, 255 }, @@ -205,7 +205,7 @@ void Renderer::Initialize(int width, int height) { {0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, 0, 255, 255} }; - gVertexArrayMesh.SetData(vertex_data, 6); + gCoordinateFrameMesh.SetData(vertex_data, 6); // Plane const int plane_grid_size = 20; @@ -289,27 +289,6 @@ void Renderer::Initialize(int width, int height) { }; gUnitCubeMesh.SetIndexData(unit_cube_index_data, 36); - // Mesh - glGenVertexArrays(1, &mMesh.mVertexArrayId); - glBindVertexArray(mMesh.mVertexArrayId); - glGenBuffers(1, &mMesh.mVertexBuffer); - glBindBuffer(GL_ARRAY_BUFFER, mMesh.mVertexBuffer); - glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); - - // Plane - glGenVertexArrays(1, &mPlane.mVertexArrayId); - glBindVertexArray(mPlane.mVertexArrayId); - glGenBuffers(1, &mPlane.mVertexBuffer); - glBindBuffer(GL_ARRAY_BUFFER, mPlane.mVertexBuffer); - glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); - - // Coordinate System - glGenVertexArrays(1, &mCoordinateSystem.mVertexArrayId); - glBindVertexArray(mCoordinateSystem.mVertexArrayId); - glGenBuffers(1, &mCoordinateSystem.mVertexBuffer); - glBindBuffer(GL_ARRAY_BUFFER, mCoordinateSystem.mVertexBuffer); - glBufferData(GL_ARRAY_BUFFER, sizeof(g_coordinate_system_vertex_buffer_data), g_coordinate_system_vertex_buffer_data, GL_STATIC_DRAW); - // Simple Shader mSimpleProgram = RenderProgram("data/shaders/vs_simple.glsl", "data/shaders/fs_simple.glsl"); bool load_result = mSimpleProgram.Load(); @@ -357,12 +336,6 @@ void Renderer::Initialize(int width, int height) { } void Renderer::Shutdown() { - glDeleteVertexArrays(1, &mMesh.mVertexArrayId); - glDeleteBuffers(1, &mMesh.mVertexBuffer); - glDeleteVertexArrays(1, &mPlane.mVertexArrayId); - glDeleteBuffers(1, &mPlane.mVertexBuffer); - glDeleteVertexArrays(1, &mCoordinateSystem.mVertexArrayId); - glDeleteBuffers(1, &mCoordinateSystem.mVertexBuffer); } @@ -406,42 +379,6 @@ void Renderer::RenderGl() { mSimpleProgram.SetMat44("uModelViewProj", model_view_projection); mSimpleProgram.SetVec4("uColor", Vector4f (1.0f, 0.0f, 0.0f, 1.0f)); - glEnableVertexAttribArray(0); - glBindBuffer(GL_ARRAY_BUFFER, mMesh.mVertexBuffer); - glVertexAttribPointer( - 0, // attribute 0 - 3, // size - GL_FLOAT, // type - GL_FALSE, // normalized? - 0, // stride - (void*)0 // offset - ); - - // Coordinate system - glEnableVertexAttribArray(0); - glBindVertexArray(mCoordinateSystem.mVertexArrayId); - mSimpleProgram.SetVec4("uColor", Vector4f (0.0f, 0.0f, 0.0f, 1.0f)); - glBindBuffer(GL_ARRAY_BUFFER, mCoordinateSystem.mVertexBuffer); - glVertexAttribPointer( - 0, - 3, - GL_FLOAT, - GL_FALSE, - (sizeof(float)*6), - (void*)0 - ); - - glEnableVertexAttribArray(1); - glVertexAttribPointer( - 1, - 3, - GL_FLOAT, - GL_FALSE, - (sizeof(float)*6), - (void*)(sizeof(float) * 3) - ); - glDrawArrays(GL_LINES, 0, 6); - // Coordinate System: VertexArrayMesh model_view_projection = TranslateMat44(0.0f, 0.0f, 0.0f) @@ -450,7 +387,7 @@ void Renderer::RenderGl() { mSimpleProgram.SetMat44("uModelViewProj", model_view_projection); mSimpleProgram.SetVec4("uColor", Vector4f (0.0f, 0.0f, 0.0f, 1.0f)); gVertexArray.Bind(); - gVertexArrayMesh.Draw(GL_LINES); + gCoordinateFrameMesh.Draw(GL_LINES); // Plane model_view_projection = @@ -464,12 +401,16 @@ void Renderer::RenderGl() { // Unit cube glUseProgram(mDefaultProgram.mProgramId); + gVertexArray.Bind(); model_matrix = TranslateMat44(3.0f, 0.0f, 1.0f); + Matrix33f normal_matrix = model_matrix.block<3,3>(0,0).transpose(); + normal_matrix = normal_matrix.inverse(); mDefaultProgram.SetMat44("uModelMatrix", model_matrix); mDefaultProgram.SetMat44("uViewMatrix", mCamera.mViewMatrix); mDefaultProgram.SetMat44("uProjectionMatrix", mCamera.mProjectionMatrix); + mDefaultProgram.SetMat33("uNormalMatrix", normal_matrix); mDefaultProgram.SetVec4("uColor", Vector4f (1.0f, 0.0f, 0.0f, 1.0f)); mDefaultProgram.SetVec3("uLightDirection", mLight.mDirection); diff --git a/src/modules/RenderModule.h b/src/modules/RenderModule.h index dcae0e3..04dc367 100644 --- a/src/modules/RenderModule.h +++ b/src/modules/RenderModule.h @@ -117,9 +117,6 @@ struct Renderer { Light mLight; Camera mCamera; - Mesh mMesh; - Mesh mPlane; - Mesh mCoordinateSystem; Texture mDefaultTexture; diff --git a/src/modules/RenderUtils.cc b/src/modules/RenderUtils.cc index 47b7a57..1b5024e 100644 --- a/src/modules/RenderUtils.cc +++ b/src/modules/RenderUtils.cc @@ -469,7 +469,7 @@ void VertexArray::Bind() { (void*)0 ); // Attribute 1: normals - glEnableVertexAttribArray(2); + glEnableVertexAttribArray(1); glVertexAttribPointer( 1, 3, @@ -479,7 +479,7 @@ void VertexArray::Bind() { (void*)(sizeof(float) * 4) ); // Attribute 2: texture coordinates - glEnableVertexAttribArray(3); + glEnableVertexAttribArray(2); glVertexAttribPointer( 2, 2, diff --git a/src/modules/RenderUtils.h b/src/modules/RenderUtils.h index 255db66..428f8a0 100644 --- a/src/modules/RenderUtils.h +++ b/src/modules/RenderUtils.h @@ -218,10 +218,7 @@ struct Texture { bool Load(const char* path, int num_components = 3); }; -struct Mesh { - GLuint mVertexArrayId = -1; - GLuint mVertexBuffer = -1; -}; +struct VertexArrayMesh; /** * Multiple VertexArrayMeshes can be stored in a single VertexArray.