Added specular highlights

simple_math_single_header
Martin Felis 2018-03-11 14:16:23 +01:00
parent cf024f4f72
commit 8de75c7c47
3 changed files with 27 additions and 9 deletions

View File

@ -2,21 +2,32 @@
uniform vec4 uColor; uniform vec4 uColor;
uniform vec3 uLightDirection; uniform vec3 uLightDirection;
uniform vec3 uViewPosition;
smooth in vec4 ioFragColor; smooth in vec4 ioFragColor;
in vec3 ioNormal; in vec3 ioNormal;
in vec3 ioFragPosition;
out vec4 outColor; out vec4 outColor;
void main() { void main() {
// ambient lighting
float ambient_strength = 0.1; float ambient_strength = 0.1;
vec4 ambient = ambient_strength * ioFragColor; vec4 ambient = ambient_strength * ioFragColor;
// diffuse lighting
vec3 norm = normalize(ioNormal); vec3 norm = 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(norm, light_dir), 0.0);
vec4 diffuse = diff * ioFragColor; vec4 diffuse = diff * ioFragColor;
outColor = ambient + diffuse; // specular lighting
vec3 view_dir = normalize(uViewPosition - ioFragPosition);
vec3 reflect_dir = reflect(-light_dir, norm);
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);
outColor = ambient + diffuse + specular;
} }

View File

@ -6,14 +6,19 @@ in vec3 inNormal;
in vec2 inUV; in vec2 inUV;
in vec4 inColor; in vec4 inColor;
uniform mat4 uModelViewProj; uniform mat4 uModelMatrix;
uniform mat4 uViewMatrix;
uniform mat4 uProjectionMatrix;
uniform vec3 uLightDirection; uniform vec3 uLightDirection;
uniform vec3 uViewPosition;
smooth out vec4 ioFragColor; smooth out vec4 ioFragColor;
out vec3 ioNormal; out vec3 ioNormal;
out vec3 ioFragPosition;
void main() { void main() {
gl_Position = uModelViewProj * inCoord; gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * inCoord;
ioFragColor = inColor; ioFragColor = inColor;
ioNormal = inNormal; ioNormal = inNormal;
ioFragPosition = (uModelMatrix * inCoord).xyz;
} }

View File

@ -465,13 +465,15 @@ void Renderer::RenderGl() {
// Unit cube // Unit cube
glUseProgram(mDefaultProgram.mProgramId); glUseProgram(mDefaultProgram.mProgramId);
model_view_projection = model_matrix = TranslateMat44(3.0f, 0.0f, 1.0f);
TranslateMat44(3.0f, 0.0f, 1.0f)
* mCamera.mViewMatrix mDefaultProgram.SetMat44("uModelMatrix", model_matrix);
* mCamera.mProjectionMatrix; mDefaultProgram.SetMat44("uViewMatrix", mCamera.mViewMatrix);
mDefaultProgram.SetMat44("uModelViewProj", model_view_projection); mDefaultProgram.SetMat44("uProjectionMatrix", mCamera.mProjectionMatrix);
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);
mDefaultProgram.SetVec3("uViewPosition", mCamera.mEye);
gVertexArray.Bind(); gVertexArray.Bind();
gUnitCubeMesh.Draw(GL_TRIANGLES); gUnitCubeMesh.Draw(GL_TRIANGLES);