Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SER332 Lecture 19

SER332 Lecture 19

Introduction to Graphics and Game Development
Lighting
(201804)

Javier Gonzalez-Sanchez
PRO

April 03, 2018
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs
    SER332
    Introduction to Graphics and Game
    Development
    Lecture 19: Lighting
    Javier Gonzalez-Sanchez
    [email protected]
    PERALTA 230U
    Office Hours: By appointment

    View Slide

  2. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 2
    jgs
    Lighting
    § By default, OpenGL's fixed-function pipeline implements the Blinn-Phong
    Shading Model.
    § Three steps to activate OpenGL fixed-function pipeline lighting:
    1. Enable light source(s) and define their attributes
    (position, ambient, diffuse, specular).
    1. Define normal vectors
    2. Define materials

    View Slide

  3. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 3
    jgs
    Enable Light Source | 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);

    View Slide

  4. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 4
    jgs
    Moving Light Sources | display method
    glPushMatrix();
    gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    glPushMatrix();
    glRotated(spin, 1.0, 0.0, 0.0);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glPopMatrix()
    // draw something else here
    glPopMatrix();

    View Slide

  5. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 5
    jgs
    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.

    View Slide

  6. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 6
    jgs
    Light Source’s Attributes
    Peter Wonka

    View Slide

  7. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 7
    jgs
    Example (original code without lighting)
    § https://github.com/javiergs/SER332/blob/master/Lecture19/original.cpp

    View Slide

  8. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 8
    jgs
    Example (update one)
    void init () {

    // RGBA
    GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
    GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
    GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
    GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
    // what kind of light source?
    glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    // enable
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
    }

    View Slide

  9. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 9
    jgs
    Example (update one)
    § https://github.com/javiergs/SER332/blob/master/Lecture19/light.cpp

    View Slide

  10. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 10
    jgs
    Add Normals
    § Per Face Normals
    § Per Vertex Normals
    § Per Vertex Weight Normals

    View Slide

  11. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 11
    jgs
    Example (update two)
    struct Mesh {
    // vertex
    vector dot_vertex;
    vector dot_normalPerFace;
    vector dot_normalPerVertex;
    vector dot_texture;
    // faces
    vector face_index_vertex;
    vector face_index_normalPerFace;
    vector face_index_normalPerVertex;
    vector face_index_texture;
    };

    View Slide

  12. jgs
    Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 12
    // normal per face
    void calculateNormalPerFace(Mesh* m) {
    Vec3 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);
    }
    }

    View Slide

  13. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 13
    jgs
    Example (update two)
    § https://github.com/javiergs/SER332/blob/master/Lecture19/light.cpp

    View Slide

  14. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 14
    jgs
    Example (update three)
    // calculate normal per vertex
    void calculateNormalPerVertex(Mesh* m) {
    m->dot_normalPerVertex.clear();
    m->face_index_normalPerVertex.clear();
    Vec3 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]);
    }
    }

    View Slide

  15. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 15
    jgs
    Per Vertex Normal
    § https://github.com/javiergs/SER332/blob/master/Lecture19/light.cpp

    View Slide

  16. jgs
    SER332 Introduction to Graphics
    Javier Gonzalez-Sanchez
    [email protected]
    Spring 2018
    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.

    View Slide