summaryrefslogtreecommitdiff
path: root/shared_cpp/Renderer3d.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'shared_cpp/Renderer3d.cpp')
-rw-r--r--shared_cpp/Renderer3d.cpp24
1 files changed, 19 insertions, 5 deletions
diff --git a/shared_cpp/Renderer3d.cpp b/shared_cpp/Renderer3d.cpp
index 4ec5504..d6be3cb 100644
--- a/shared_cpp/Renderer3d.cpp
+++ b/shared_cpp/Renderer3d.cpp
@@ -15,14 +15,29 @@ const char* vertexSrc =
" vec4 fragmentPosition = projection * view * model * vec4(position, 1);"
" gl_Position = fragmentPosition;\n"
" VertexColor = color;\n"
- " VertexNormal = normal;\n"
+ " mat3 normalMatrix = mat3(model);\n"
+ " VertexNormal = normalize(normalMatrix * normal);\n"
"}\n";
const char* fragmentSrc =
+ "precision mediump float;\n"
"varying lowp vec4 VertexColor; \n"
"varying lowp vec3 VertexNormal; \n"
+ "vec4 ambientColor = vec4(0.2, 0.2, 0.2, 1.0);\n"
+ "vec3 lightDirection = vec3(0.0, 1.0, 0.0);\n"
+ "vec4 lightColor = vec4(0.7, 0.7, 0.7, 1.0);\n"
"void main() { \n"
- " gl_FragColor = VertexColor; \n"
+ " vec4 finalColor = VertexColor * ambientColor;\n"
+ " float normalDotDir = max(0.0, dot(VertexNormal, lightDirection));\n"
+ " if (normalDotDir <= 0.0001) {"
+ " finalColor += vec4(0, 0, 0, 0);"
+ " }\n"
+ " else {\n"
+ " float cosTheta = clamp(normalDotDir, 0.0, 1.0);\n"
+ " vec4 diffuseFactor = VertexColor * cosTheta;\n"
+ " finalColor += lightColor * diffuseFactor; \n"
+ " }\n"
+ " gl_FragColor = finalColor;\n"
"}";
@@ -80,9 +95,8 @@ void Mesh3d::load(Vertex3d* inVertices, uint32 inNumVertices, uint32* inIndices,
glEnableVertexAttribArray(renderer->attributes.position);
glVertexAttribPointer(renderer->attributes.position, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3d), (GLvoid *)0);
- // @TODO: Show normal once it's actually implemented
- //glEnableVertexAttribArray(renderer->attributes.normal);
- //glVertexAttribPointer(renderer->attributes.normal, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3d), (GLvoid *)offsetof(Vertex3d, normal));
+ glEnableVertexAttribArray(renderer->attributes.normal);
+ glVertexAttribPointer(renderer->attributes.normal, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3d), (GLvoid *)offsetof(Vertex3d, normal));
glEnableVertexAttribArray(renderer->attributes.color);
glVertexAttribPointer(renderer->attributes.color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex3d), (GLvoid *)offsetof(Vertex3d, color));