Diffuse and ambient lighting implemented

simple_math_single_header
Martin Felis 2018-03-11 11:58:50 +01:00
parent 499eba60b7
commit cf024f4f72
6 changed files with 118 additions and 45 deletions

View File

@ -0,0 +1,22 @@
#version 150 core
uniform vec4 uColor;
uniform vec3 uLightDirection;
smooth in vec4 ioFragColor;
in vec3 ioNormal;
out vec4 outColor;
void main() {
float ambient_strength = 0.1;
vec4 ambient = ambient_strength * ioFragColor;
vec3 norm = normalize(ioNormal);
vec3 light_dir = normalize(uLightDirection);
float diff = max(dot(norm, light_dir), 0.0);
vec4 diffuse = diff * ioFragColor;
outColor = ambient + diffuse;
}

View File

@ -0,0 +1,19 @@
#version 150 core
#extension GL_ARB_explicit_attrib_location : require
in vec4 inCoord;
in vec3 inNormal;
in vec2 inUV;
in vec4 inColor;
uniform mat4 uModelViewProj;
uniform vec3 uLightDirection;
smooth out vec4 ioFragColor;
out vec3 ioNormal;
void main() {
gl_Position = uModelViewProj * inCoord;
ioFragColor = inColor;
ioNormal = inNormal;
}

View File

@ -85,6 +85,7 @@ static void module_serialize (
SerializeVec3(*serializer, "protot.RenderModule.Camera.mUp", gRenderer->mCamera.mUp); SerializeVec3(*serializer, "protot.RenderModule.Camera.mUp", gRenderer->mCamera.mUp);
SerializeFloat(*serializer, "protot.RenderModule.Camera.mNear", gRenderer->mCamera.mNear); SerializeFloat(*serializer, "protot.RenderModule.Camera.mNear", gRenderer->mCamera.mNear);
SerializeFloat(*serializer, "protot.RenderModule.Camera.mFar", gRenderer->mCamera.mFar); SerializeFloat(*serializer, "protot.RenderModule.Camera.mFar", gRenderer->mCamera.mFar);
SerializeVec3(*serializer, "protot.RenderModule.mLight.mDirection", gRenderer->mLight.mDirection);
// SerializeBool (*serializer, "protot.RenderModule.draw_floor", gRenderer->drawFloor); // SerializeBool (*serializer, "protot.RenderModule.draw_floor", gRenderer->drawFloor);
// SerializeBool (*serializer, "protot.RenderModule.draw_skybox", gRenderer->drawSkybox); // SerializeBool (*serializer, "protot.RenderModule.draw_skybox", gRenderer->drawSkybox);
@ -310,12 +311,15 @@ void Renderer::Initialize(int width, int height) {
glBufferData(GL_ARRAY_BUFFER, sizeof(g_coordinate_system_vertex_buffer_data), g_coordinate_system_vertex_buffer_data, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(g_coordinate_system_vertex_buffer_data), g_coordinate_system_vertex_buffer_data, GL_STATIC_DRAW);
// Simple Shader // Simple Shader
mDefaultProgram = 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 = mDefaultProgram.Load(); bool load_result = mSimpleProgram.Load();
mSimpleProgram.RegisterFileModification();
assert(load_result);
mDefaultProgram = RenderProgram("data/shaders/vs_default.glsl", "data/shaders/fs_default.glsl");
load_result = mDefaultProgram.Load();
mDefaultProgram.RegisterFileModification(); mDefaultProgram.RegisterFileModification();
assert(load_result); assert(load_result);
muDefaultModelViewProjection = mDefaultProgram.GetUniformLocation("uModelViewProj");
muDefaultColor = mDefaultProgram.GetUniformLocation("uColor");
// Render Target // Render Target
mRenderTarget = RenderTarget (width, height, mRenderTarget = RenderTarget (width, height,
@ -398,9 +402,9 @@ void Renderer::RenderGl() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glUseProgram(mDefaultProgram.mProgramId); glUseProgram(mSimpleProgram.mProgramId);
glUniformMatrix4fv(muDefaultModelViewProjection, 1, GL_FALSE, model_view_projection.data()); mSimpleProgram.SetMat44("uModelViewProj", model_view_projection);
glUniform4fv(muDefaultColor, 1, Vector4f(1.0f, 0.0f, 0.0f, 1.0f).data()); mSimpleProgram.SetVec4("uColor", Vector4f (1.0f, 0.0f, 0.0f, 1.0f));
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, mMesh.mVertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, mMesh.mVertexBuffer);
@ -413,11 +417,10 @@ void Renderer::RenderGl() {
(void*)0 // offset (void*)0 // offset
); );
// glDrawArrays(GL_TRIANGLES, 0, 3); // starting from vertex 0; 3 vertices total // Coordinate system
// // Coordinate system
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glBindVertexArray(mCoordinateSystem.mVertexArrayId); glBindVertexArray(mCoordinateSystem.mVertexArrayId);
glUniform4fv(muDefaultColor, 1, Vector4f(0.0f, 0.0f, 0.0f, 1.0f).data()); mSimpleProgram.SetVec4("uColor", Vector4f (0.0f, 0.0f, 0.0f, 1.0f));
glBindBuffer(GL_ARRAY_BUFFER, mCoordinateSystem.mVertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, mCoordinateSystem.mVertexBuffer);
glVertexAttribPointer( glVertexAttribPointer(
0, 0,
@ -444,8 +447,8 @@ void Renderer::RenderGl() {
TranslateMat44(0.0f, 0.0f, 0.0f) TranslateMat44(0.0f, 0.0f, 0.0f)
* mCamera.mViewMatrix * mCamera.mViewMatrix
* mCamera.mProjectionMatrix; * mCamera.mProjectionMatrix;
glUniformMatrix4fv(muDefaultModelViewProjection, 1, GL_FALSE, model_view_projection.data()); mSimpleProgram.SetMat44("uModelViewProj", model_view_projection);
glUniform4fv(muDefaultColor, 1, Vector4f(1.0f, 0.0f, 0.0f, 1.0f).data()); mSimpleProgram.SetVec4("uColor", Vector4f (0.0f, 0.0f, 0.0f, 1.0f));
gVertexArray.Bind(); gVertexArray.Bind();
gVertexArrayMesh.Draw(GL_LINES); gVertexArrayMesh.Draw(GL_LINES);
@ -454,25 +457,21 @@ void Renderer::RenderGl() {
TranslateMat44(0.0f, 0.0f, 0.0f) TranslateMat44(0.0f, 0.0f, 0.0f)
* mCamera.mViewMatrix * mCamera.mViewMatrix
* mCamera.mProjectionMatrix; * mCamera.mProjectionMatrix;
glUniformMatrix4fv(muDefaultModelViewProjection, 1, GL_FALSE, model_view_projection.data()); mSimpleProgram.SetMat44("uModelViewProj", model_view_projection);
glUniform4fv(muDefaultColor, 1, Vector4f(1.0f, 0.0f, 0.0f, 1.0f).data()); mSimpleProgram.SetVec4("uColor", Vector4f (1.0f, 0.0f, 0.0f, 1.0f));
glBindAttribLocation(mDefaultProgram.mProgramId, 0, "inCoord");
glBindAttribLocation(mDefaultProgram.mProgramId, 1, "inNormal");
glBindAttribLocation(mDefaultProgram.mProgramId, 2, "inUV");
glBindAttribLocation(mDefaultProgram.mProgramId, 3, "inColor");
gVertexArray.Bind(); gVertexArray.Bind();
gXZPlaneMesh.Draw(GL_LINES); gXZPlaneMesh.Draw(GL_LINES);
// Unit cube
glUseProgram(mDefaultProgram.mProgramId);
model_view_projection = model_view_projection =
TranslateMat44(3.0f, 0.0f, 1.0f) TranslateMat44(3.0f, 0.0f, 1.0f)
* mCamera.mViewMatrix * mCamera.mViewMatrix
* mCamera.mProjectionMatrix; * mCamera.mProjectionMatrix;
glUniformMatrix4fv(muDefaultModelViewProjection, 1, GL_FALSE, model_view_projection.data()); mDefaultProgram.SetMat44("uModelViewProj", model_view_projection);
glUniform4fv(muDefaultColor, 1, Vector4f(1.0f, 0.0f, 0.0f, 1.0f).data()); mDefaultProgram.SetVec4("uColor", Vector4f (1.0f, 0.0f, 0.0f, 1.0f));
glBindAttribLocation(mDefaultProgram.mProgramId, 0, "inCoord"); mDefaultProgram.SetVec3("uLightDirection", mLight.mDirection);
glBindAttribLocation(mDefaultProgram.mProgramId, 1, "inNormal");
glBindAttribLocation(mDefaultProgram.mProgramId, 2, "inUV");
glBindAttribLocation(mDefaultProgram.mProgramId, 3, "inColor");
gVertexArray.Bind(); gVertexArray.Bind();
gUnitCubeMesh.Draw(GL_TRIANGLES); gUnitCubeMesh.Draw(GL_TRIANGLES);
@ -542,8 +541,8 @@ void Renderer::RenderGui() {
ImGui::EndDock(); ImGui::EndDock();
if (ImGui::BeginDock("Render Settings")) { if (ImGui::BeginDock("Render Settings")) {
ImGui::Text("Render Target"); ImGui::Text("Light");
ImGui::Text("Width %d, Height %d", mRenderTarget.mWidth, mRenderTarget.mHeight); ImGui::SliderFloat3("Direction", mLight.mDirection.data(), -10.0f, 10.0f);
ImGui::Text("Camera"); ImGui::Text("Camera");
mCamera.DrawGui(); mCamera.DrawGui();

View File

@ -57,8 +57,8 @@ struct Camera {
}; };
struct Light { struct Light {
Vector3f pos; Vector3f mPosition;
Vector3f dir; Vector3f mDirection;
float mViewMatrix[16]; float mViewMatrix[16];
float mProjectionMatrix[16]; float mProjectionMatrix[16];
@ -74,8 +74,8 @@ struct Light {
float area; float area;
Light() : Light() :
pos (Vector3f(0.f, 10.f, 10.f)), mPosition (Vector3f(0.f, 10.f, 10.f)),
dir (Vector3f(-1.f, -1.f, -1.f)), mDirection (Vector3f(-1.f, -1.f, -1.f)),
mViewMatrix { mViewMatrix {
1.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f,
0.f, 1.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f,
@ -115,6 +115,7 @@ struct Renderer {
uint32_t mSceneAreaWidth = 1; uint32_t mSceneAreaWidth = 1;
uint32_t mSceneAreaHeight = 1; uint32_t mSceneAreaHeight = 1;
Light mLight;
Camera mCamera; Camera mCamera;
Mesh mMesh; Mesh mMesh;
Mesh mPlane; Mesh mPlane;
@ -122,6 +123,8 @@ struct Renderer {
Texture mDefaultTexture; Texture mDefaultTexture;
RenderProgram mSimpleProgram;
RenderProgram mDefaultProgram; RenderProgram mDefaultProgram;
GLuint muDefaultModelViewProjection; GLuint muDefaultModelViewProjection;
GLuint muDefaultColor; GLuint muDefaultColor;

View File

@ -108,6 +108,13 @@ GLuint RenderProgram::LinkProgram(GLuint vertex_shader, GLuint fragment_shader)
GLuint ProgramID = glCreateProgram(); GLuint ProgramID = glCreateProgram();
glAttachShader(ProgramID, vertex_shader); glAttachShader(ProgramID, vertex_shader);
glAttachShader(ProgramID, fragment_shader); glAttachShader(ProgramID, fragment_shader);
// Bind attribute locations
glBindAttribLocation(ProgramID, 0, "inCoord");
glBindAttribLocation(ProgramID, 1, "inNormal");
glBindAttribLocation(ProgramID, 2, "inUV");
glBindAttribLocation(ProgramID, 3, "inColor");
glLinkProgram(ProgramID); glLinkProgram(ProgramID);
GLint Result = GL_FALSE; GLint Result = GL_FALSE;
@ -461,36 +468,36 @@ void VertexArray::Bind() {
(sizeof(VertexData)), (sizeof(VertexData)),
(void*)0 (void*)0
); );
// Attribute 1: color // Attribute 1: normals
glEnableVertexAttribArray(1);
glVertexAttribPointer(
1,
4,
GL_UNSIGNED_BYTE,
GL_FALSE,
(sizeof(VertexData)),
(void*)(sizeof(float) * 9)
);
// Attribute 2: normals
glEnableVertexAttribArray(2); glEnableVertexAttribArray(2);
glVertexAttribPointer( glVertexAttribPointer(
2, 1,
3, 3,
GL_FLOAT, GL_FLOAT,
GL_FALSE, GL_FALSE,
(sizeof(VertexData)), (sizeof(VertexData)),
(void*)(sizeof(float) * 4) (void*)(sizeof(float) * 4)
); );
// Attribute 3: texture coordinates // Attribute 2: texture coordinates
glEnableVertexAttribArray(3); glEnableVertexAttribArray(3);
glVertexAttribPointer( glVertexAttribPointer(
3, 2,
2, 2,
GL_FLOAT, GL_FLOAT,
GL_FALSE, GL_FALSE,
(sizeof(VertexData)), (sizeof(VertexData)),
(void*)(sizeof(float) * 7) (void*)(sizeof(float) * 7)
); );
// Attribute 1: color
glEnableVertexAttribArray(3);
glVertexAttribPointer(
3,
4,
GL_UNSIGNED_BYTE,
GL_TRUE,
(sizeof(VertexData)),
(void*)(sizeof(float) * 9)
);
} }
bool VertexArray::IsBound() { bool VertexArray::IsBound() {
@ -561,7 +568,9 @@ void VertexArrayMesh::SetData(
} }
if (have_colors) { if (have_colors) {
memcpy (vertex_data[i].mColor, colors[i].data(), sizeof(float) * 4); for (int j = 0; j < 4; ++j) {
vertex_data[i].mColor[j] = GLubyte(colors[i][j] * 255.0f);
}
} else { } else {
memset (vertex_data[i].mColor, 0, sizeof(float) * 4); memset (vertex_data[i].mColor, 0, sizeof(float) * 4);
} }

View File

@ -153,6 +153,27 @@ struct RenderProgram : AFileModificationListener {
GLuint CompileFragmentShader(); GLuint CompileFragmentShader();
GLuint LinkProgram(GLuint vertex_shader, GLuint fragment_shader); GLuint LinkProgram(GLuint vertex_shader, GLuint fragment_shader);
void SetVec3(const char* name, const Vector3f& vec) {
GLint location = glGetUniformLocation(mProgramId, name);
glUniform3fv(location, 1, vec.data());
}
void SetVec4(const char* name, const Vector3f& vec, float w = 1.0f) {
GLint location = glGetUniformLocation(mProgramId, name);
glUniform4f(location, vec[0], vec[1], vec[2], w);
}
void SetVec4(const char* name, const Vector4f& vec) {
GLint location = glGetUniformLocation(mProgramId, name);
glUniform4fv(location, 1, vec.data());
}
void SetMat44 (const char* name, const Matrix44f& mat) {
GLint location = glGetUniformLocation(mProgramId, name);
glUniformMatrix4fv(location, 1, GL_FALSE, mat.data());
}
void SetMat33 (const char* name, const Matrix33f& mat) {
GLint location = glGetUniformLocation(mProgramId, name);
glUniformMatrix3fv(location, 1, GL_FALSE, mat.data());
}
void RegisterFileModification(); void RegisterFileModification();
virtual bool OnFileChanged(const std::string& filename); virtual bool OnFileChanged(const std::string& filename);
}; };