Hello,
I’m trying to implement flat shading in my geometry shader (because it is per-primitive).
I use tessellation to transform a colour cube into a colour sphere.
Then I want to combine the Phong Lighting Model and Flat Shading Model to shade the sphere.
This is my geometry shader:
#version 420
layout (triangles) in;
in vec3 Colour[];
layout(triangle_strip, max_vertices = 3) out;
out vec3 v_colour;
void main() {
vec3 lightPos = vec3(0.0, 2.0, 0.0); // position of light source.
vec3 eye = vec3(0.0); // vector the camera is looking along.
vec3 v0 = gl_in[0].gl_Position.xyz;
vec3 v1 = gl_in[1].gl_Position.xyz;
vec3 v2 = gl_in[2].gl_Position.xyz;
vec3 N = normalize(cross(v1 - v0, v2 - v0)); // Primitive normal.
for (int i = 0; i < 3; i++) {
vec3 L = normalize(lightPos - gl_in[i].gl_Position.xyz); // direction from fragment to light source.
vec3 E = normalize(eye - gl_in[i].gl_Position.xyz); // view vector.
vec3 R = normalize(reflect(-L, N)); // reflection vector of light source.
vec3 ambient = vec3(0.1);
vec3 diffuse = vec3(0.2) * dot(L, N);
vec3 specular = vec3(0.3) * pow(max(dot(R, E), 0.0), 0.3 * 60.0);
vec3 intensity = ambient + diffuse + specular;
gl_Position = gl_in[i].gl_Position;
v_colour = intensity * Colour[i];
EmitVertex();
}
EndPrimitive();
}
With this code, there is a small spot of light on top of the sphere, but no specular highlights or anything.
Thanks.