Program5

Compute vertex normals from triangle faces. Then, shade the model with Phong reflectance and Gouraud and Phong shading. Your repository will contain template code. Be sure to fill in all the TODO blocks. You will need to modify Model.h and the shader programs.

Constants

Hints

Fixes to make things work

Code

Here is the code we wrote in class. It goes in Model.h init(). You will need to comment out the code that sets the color to white!
colors.resize( positions.size() );

//TODO compute the vertex normals by averaging the face normals
for(size_t i=0; i<elements.size(); i+=3) {

	size_t vertexId[3];
	for(size_t v=0; v<3; v++)
		vertexId[v] = elements[i+v];
	
	glm::vec3 vertexPos[3];
	for(size_t v=0; v<3; v++)
		for(size_t c=0; c<3; c++)
			vertexPos[v][c] = positions[ vertexId[v]*3 + c ];
	
	glm::vec3 a = vertexPos[1] - vertexPos[0];
	glm::vec3 b = vertexPos[2] - vertexPos[1];
	glm::vec3 faceNormal = glm::normalize(glm::cross(a, b));

	//scale to RGB range (undo this when using as a normal vector!)
	faceNormal = (faceNormal + 1.0f) * 0.5f;
	
	for(size_t v=0; v<3; v++)
		for(size_t c=0; c<3; c++)
			colors[ vertexId[v]*3 + c ] = faceNormal[c];
}

Rubric


Vertex normals computed, uploaded, and used as colors-1: No normals0: Face normals only1: Correct vertex normals
Ambient reflectance0: No ambient1: Ambient reflectance
Diffuse reflectance0: No diffuse1: Phong diffuse term correctly uses normal and light vector
Specular reflectance0: No specular1: Phong specular term correctly using view and light vector
Light rotation0: Incorrect rotation1: Correct reflectance for rotating light
Object position transformation0: Incorrect reflectance after transform1: Correct reflectance after transform
Object normal transformation0: Incorrect reflectance after transform1: Correct reflectance after transform
Gouraud shading0: No shading1: One reflectance per vertex, then color interpolated
Phong shading0: No shading2: Normals interpolated, then a reflectance per interpolated normal

Reference solution