Implementing Flat Shading


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];
	vec3 v1 = gl_in[1];
	vec3 v2 = gl_in[2];
	vec3 N = normalize(cross(v1 - v0, v2 - v0)); // Primitive normal.

	for (int i = 0; i < 3; i++) {
		vec3 L = normalize(lightPos - gl_in[i]; // direction from fragment to light source.
		vec3 E = normalize(eye - gl_in[i]; // 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];

With this code, there is a small spot of light on top of the sphere, but no specular highlights or anything.


I can’t tell what your vertex shader does, but I guess the gl_in[i].gl_Position is already after applying perspective (or whatever) projection and the light positon is thus not in camera space.

My vertex shader just passes through the position.
This is my tessellation evaluation shader:

#version 420

layout (triangles, equal_spacing) in;

out vec3 Colour;
out vec4 LightPos;

uniform mat4 projection;
uniform mat4 world;
uniform mat4 view;

void main() {
	vec3 p0 = gl_in[0] * gl_TessCoord.x;
	vec3 p1 = gl_in[1] * gl_TessCoord.y;
	vec3 p2 = gl_in[0] * gl_TessCoord.z;
	vec3 position = normalize(p0 + p1 + p2);
	gl_Position = projection * view * world * vec4(position, 1.0);
	LightPos = projection * view * world * vec4(0.0, 2.0, 0.0, 1.0);
	Colour = vec3(position.x, position.y, position.z);

I removed lightPos from the geometry shader and calculated it in the TES.
I’m not sure whether I should be multiplying LightPos with the matrices.