Ambient + Diffuse + Specular § Ambient It is a constant term. Applies equally to all points on the object § Ambient + Diffuse Diffuse color of light interacts with diffuse component of material. Its intensity depends on the angle between the light source and the surface normal § Ambient + Diffuse + Specular Specular color of light interacts with specular component of material. Its intensity depends on the angle between the viewer and the direction of a ray reflected from the light source.
// normal per face void calculateNormalPerFace(Mesh* m) { Vec3<float> v1, v2, v3, v4, v5; for (int i = 0; i < m->face_index_vertex.size(); i += 3) { v1 = m->dot_vertex[m->face_index_vertex[i]]; v2 = m->dot_vertex[m->face_index_vertex[i + 1]]; v3 = m->dot_vertex[m->face_index_vertex[i + 2]]; v4 = (v2 - v1); v5 = (v3 - v1); v4 = v4.cross(v5); v4.normalize(); m->dot_normalPerFace.push_back(v4); int pos = m->dot_normalPerFace.size() - 1; // same normal for all vertex in this face m->face_index_normalPerFace.push_back(pos); m->face_index_normalPerFace.push_back(pos); m->face_index_normalPerFace.push_back(pos); } }
Example (update three) // calculate normal per vertex void calculateNormalPerVertex(Mesh* m) { m->dot_normalPerVertex.clear(); m->face_index_normalPerVertex.clear(); Vec3<float> suma; suma.x = 0; suma.y = 0; suma.z = 0; //initialize for (unsigned int val = 0; val < m->dot_vertex.size(); val++) { m->dot_normalPerVertex.push_back(suma); } // calculate sum for vertex for (long pos = 0; pos < m->face_index_vertex.size(); pos++) { m->dot_normalPerVertex[m->face_index_vertex[pos]] += m->dot_normalPerFace[m->face_index_normalPerFace[pos]]; } // normalize for vertex for (unsigned int val = 0; val < m->dot_normalPerVertex.size(); val++) { m->dot_normalPerVertex[val] = m->dot_normalPerVertex[val].normalize(); } //normalVertexIndex is the same that vertexIndex for (unsigned int pos = 0; pos < m->face_index_vertex.size(); pos++) { m->face_index_normalPerVertex.push_back(m->face_index_vertex[pos]); } }