Cleanup, using blinn-phong

simple_math_single_header
Martin Felis 2018-03-11 17:30:56 +01:00
parent 8de75c7c47
commit fb3e1e7432
6 changed files with 18 additions and 83 deletions

View File

@ -16,18 +16,17 @@ void main() {
vec4 ambient = ambient_strength * ioFragColor; vec4 ambient = ambient_strength * ioFragColor;
// diffuse lighting // diffuse lighting
vec3 norm = normalize(ioNormal); vec3 normal_dir = normalize(ioNormal);
vec3 light_dir = normalize(uLightDirection); 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; vec4 diffuse = diff * ioFragColor;
// specular lighting // specular lighting
vec3 view_dir = normalize(uViewPosition - ioFragPosition); 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(normal_dir, halfway_dir), 0.0), 32);
float spec = pow(max(dot(view_dir, reflect_dir), 0.0), 32); vec4 specular = spec * vec4(0.5);
vec4 specular = specular_strength * spec * vec4(1.0, 1.0, 1.0, 1.0);
outColor = ambient + diffuse + specular; outColor = ambient + diffuse + specular;
} }

View File

@ -9,6 +9,7 @@ in vec4 inColor;
uniform mat4 uModelMatrix; uniform mat4 uModelMatrix;
uniform mat4 uViewMatrix; uniform mat4 uViewMatrix;
uniform mat4 uProjectionMatrix; uniform mat4 uProjectionMatrix;
uniform mat3 uNormalMatrix;
uniform vec3 uLightDirection; uniform vec3 uLightDirection;
uniform vec3 uViewPosition; uniform vec3 uViewPosition;
@ -19,6 +20,6 @@ out vec3 ioFragPosition;
void main() { void main() {
gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * inCoord; gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * inCoord;
ioFragColor = inColor; ioFragColor = inColor;
ioNormal = inNormal; ioNormal = uNormalMatrix * inNormal;
ioFragPosition = (uModelMatrix * inCoord).xyz; ioFragPosition = (uModelMatrix * inCoord).xyz;
} }

View File

@ -51,7 +51,7 @@ static const GLfloat g_coordinate_system_vertex_buffer_data[] = {
}; };
VertexArray gVertexArray; VertexArray gVertexArray;
VertexArrayMesh gVertexArrayMesh; VertexArrayMesh gCoordinateFrameMesh;
VertexArrayMesh gXZPlaneMesh; VertexArrayMesh gXZPlaneMesh;
VertexArrayMesh gUnitCubeMesh; 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)); mDefaultTexture.MakeGrid(128, Vector3f (0.8, 0.8f, 0.8f), Vector3f (0.2f, 0.2f, 0.2f));
gVertexArray.Initialize(1000, GL_STATIC_DRAW); gVertexArray.Initialize(1000, GL_STATIC_DRAW);
gVertexArrayMesh.Initialize(gVertexArray, 6); gCoordinateFrameMesh.Initialize(gVertexArray, 6);
VertexArray::VertexData vertex_data[] = { 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 }, {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} {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 // Plane
const int plane_grid_size = 20; const int plane_grid_size = 20;
@ -289,27 +289,6 @@ void Renderer::Initialize(int width, int height) {
}; };
gUnitCubeMesh.SetIndexData(unit_cube_index_data, 36); 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 // Simple Shader
mSimpleProgram = RenderProgram("data/shaders/vs_simple.glsl", "data/shaders/fs_simple.glsl"); mSimpleProgram = RenderProgram("data/shaders/vs_simple.glsl", "data/shaders/fs_simple.glsl");
bool load_result = mSimpleProgram.Load(); bool load_result = mSimpleProgram.Load();
@ -357,12 +336,6 @@ void Renderer::Initialize(int width, int height) {
} }
void Renderer::Shutdown() { 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.SetMat44("uModelViewProj", model_view_projection);
mSimpleProgram.SetVec4("uColor", Vector4f (1.0f, 0.0f, 0.0f, 1.0f)); 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 // Coordinate System: VertexArrayMesh
model_view_projection = model_view_projection =
TranslateMat44(0.0f, 0.0f, 0.0f) TranslateMat44(0.0f, 0.0f, 0.0f)
@ -450,7 +387,7 @@ void Renderer::RenderGl() {
mSimpleProgram.SetMat44("uModelViewProj", model_view_projection); mSimpleProgram.SetMat44("uModelViewProj", model_view_projection);
mSimpleProgram.SetVec4("uColor", Vector4f (0.0f, 0.0f, 0.0f, 1.0f)); mSimpleProgram.SetVec4("uColor", Vector4f (0.0f, 0.0f, 0.0f, 1.0f));
gVertexArray.Bind(); gVertexArray.Bind();
gVertexArrayMesh.Draw(GL_LINES); gCoordinateFrameMesh.Draw(GL_LINES);
// Plane // Plane
model_view_projection = model_view_projection =
@ -464,12 +401,16 @@ void Renderer::RenderGl() {
// Unit cube // Unit cube
glUseProgram(mDefaultProgram.mProgramId); glUseProgram(mDefaultProgram.mProgramId);
gVertexArray.Bind();
model_matrix = TranslateMat44(3.0f, 0.0f, 1.0f); 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("uModelMatrix", model_matrix);
mDefaultProgram.SetMat44("uViewMatrix", mCamera.mViewMatrix); mDefaultProgram.SetMat44("uViewMatrix", mCamera.mViewMatrix);
mDefaultProgram.SetMat44("uProjectionMatrix", mCamera.mProjectionMatrix); mDefaultProgram.SetMat44("uProjectionMatrix", mCamera.mProjectionMatrix);
mDefaultProgram.SetMat33("uNormalMatrix", normal_matrix);
mDefaultProgram.SetVec4("uColor", Vector4f (1.0f, 0.0f, 0.0f, 1.0f)); mDefaultProgram.SetVec4("uColor", Vector4f (1.0f, 0.0f, 0.0f, 1.0f));
mDefaultProgram.SetVec3("uLightDirection", mLight.mDirection); mDefaultProgram.SetVec3("uLightDirection", mLight.mDirection);

View File

@ -117,9 +117,6 @@ struct Renderer {
Light mLight; Light mLight;
Camera mCamera; Camera mCamera;
Mesh mMesh;
Mesh mPlane;
Mesh mCoordinateSystem;
Texture mDefaultTexture; Texture mDefaultTexture;

View File

@ -469,7 +469,7 @@ void VertexArray::Bind() {
(void*)0 (void*)0
); );
// Attribute 1: normals // Attribute 1: normals
glEnableVertexAttribArray(2); glEnableVertexAttribArray(1);
glVertexAttribPointer( glVertexAttribPointer(
1, 1,
3, 3,
@ -479,7 +479,7 @@ void VertexArray::Bind() {
(void*)(sizeof(float) * 4) (void*)(sizeof(float) * 4)
); );
// Attribute 2: texture coordinates // Attribute 2: texture coordinates
glEnableVertexAttribArray(3); glEnableVertexAttribArray(2);
glVertexAttribPointer( glVertexAttribPointer(
2, 2,
2, 2,

View File

@ -218,10 +218,7 @@ struct Texture {
bool Load(const char* path, int num_components = 3); bool Load(const char* path, int num_components = 3);
}; };
struct Mesh { struct VertexArrayMesh;
GLuint mVertexArrayId = -1;
GLuint mVertexBuffer = -1;
};
/** /**
* Multiple VertexArrayMeshes can be stored in a single VertexArray. * Multiple VertexArrayMeshes can be stored in a single VertexArray.