Boost Fertility New Invention Ups Success Rates.pdf
201707 SER332 Lecture18
1. SER332
Introduction to Graphics and
Game Development
Lecture 18:
Lighting
Javier Gonzalez-Sanchez
javiergs@asu.edu
PERALTA 230U
Office Hours: By appointment
3. Javier Gonzalez-Sanchez | SER332 | Spring2017 | 3
Lighting
To activate OpenGL fixed function pipeline lighting,
you have to define the followings:
§ light source(s) and their attributes, such as
position, ambient, diffuse, specular
§ normals
§ materials
4. Javier Gonzalez-Sanchez | SER332 | Spring2017 | 4
Enable Light Sources | init method
// light position
// Point light: w component is 1.0
// Directional light: w component is 0.0
Glfloat light_position[] = {-1.0, 0.0, 1.0, 0.0};
// light configuration
glLightfv( GL_LIGHT0, GL_POSITION, light_position);
// Enable lighting + each individual light!!!
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
6. Javier Gonzalez-Sanchez | SER332 | Spring2017 | 6
Ambient + Diffuse + Specular
ambient
ambient + diffuse ambient + diffuse + specular
• Diffuse color of light interacts with diffuse component of material
• Specular color of light interacts with specular component of material
13. Javier Gonzalez-Sanchez | SER431 | Fall 2016 | 13
// 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);
}
}
15. Javier Gonzalez-Sanchez | SER332 | Spring2017 | 15
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]);
}
}
17. Javier Gonzalez-Sanchez | SER332 | Spring2017 | 17
Test Yourself
• Light [0,100,0]
• Per Face Normal
• Per Vertex Normal
• Light moving
• Change something in the world (for instance, more
diamonds or other mesh)
18. SER332 Introduction to Graphics
Javier Gonzalez-Sanchez
javiergs@asu.edu
Spring 2017
Disclaimer. These slides can only be used as study material for the class SER332 at ASU. They cannot be distributed or used for another purpose.