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

SER431 Lecture 08

SER431 Lecture 08

Advanced Graphics
Environment (Planar reflections)
(201809)

Javier Gonzalez-Sanchez
PRO

September 10, 2018
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs
    SER 431
    Advanced Graphics
    Lecture 08: Environment (Planar reflections)
    Javier Gonzalez-Sanchez
    [email protected]
    PERALTA 230U
    Office Hours: By appointment

    View Slide

  2. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 1
    jgs
    Our Goal

    View Slide

  3. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 2
    jgs
    We are here

    View Slide

  4. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 3
    jgs
    Next step

    View Slide

  5. jgs
    Stencil for 3D scenarios

    View Slide

  6. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 5
    jgs
    Step 1. main()
    // main
    void main(int argc, char* argv[]) {
    glutInit(&argc, argv);
    glutInitDisplayMode(
    GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA | GLUT_STENCIL
    );
    // main as usual here...
    }

    View Slide

  7. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 6
    jgs
    Step 2. init()
    // init
    void init() {
    // init as usual here...
    //define the value to use as clean.
    glClearStencil(0);
    }

    View Slide

  8. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 7
    jgs
    Step 3. display()
    void display(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    glEnable(GL_STENCIL_TEST);
    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
    glStencilFunc(GL_ALWAYS, 1, 1);
    glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
    //Render stencil box

    View Slide

  9. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 8
    jgs
    glDisable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-50.0, 50, -50.0, 50.0, 1.0, -1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glPushMatrix();
    glTranslatef(0, 0, 0);
    glBegin(GL_TRIANGLES);
    glVertex2f(-20.0, -20.0);
    glVertex2f( 20.0, -20.0);
    glVertex2f( 0.0, 20.0);
    glEnd();
    glPopMatrix();
    glEnable(GL_DEPTH_TEST);

    View Slide

  10. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 9
    jgs
    // re-enable color and disable stencil
    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    glStencilFunc(GL_EQUAL, 1, 1);
    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
    // draw your colors here
    glDisable(GL_STENCIL_TEST);
    glutSwapBuffers();
    }

    View Slide

  11. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 10
    jgs
    New Step 3. display()
    void display(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    //Configure projection and modelview (camera) as usual
    glEnable(GL_STENCIL_TEST);
    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
    glStencilFunc(GL_ALWAYS, 1, 1);
    glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
    // glDisable(GL_DEPTH_TEST);
    //Render stencil box
    // glDisable(GL_DEPTH_TEST);
    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    glStencilFunc(GL_NOTEQUAL, 1, 1);
    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
    // draw your colors here
    glDisable(GL_STENCIL_TEST);
    glutSwapBuffers();
    }

    View Slide

  12. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 11
    jgs
    Next step

    View Slide

  13. jgs
    Reflections

    View Slide

  14. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 13
    jgs
    Planar Reflections
    § Easy hack, draw the object twice,
    § One time has glScalef(1,-1,1) to reflect through the floor

    View Slide

  15. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 14
    jgs
    Planar Reflections
    § But, the plain obstruct the boxes below.

    View Slide

  16. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 15
    jgs
    Planar Reflections
    § Make the plain to be blended with the boxes (Disable the light)
    //plane
    glEnable(GL_BLEND);
    glDisable(GL_LIGHTING);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glColor4f(0.7, 0.0, 0.0, 0.3);
    glColor4f(1.0, 1.0, 1.0, 0.3);
    glPushMatrix();
    glTranslatef(-900, 0, -900);
    glCallList(display1);
    glPopMatrix();
    glEnable(GL_LIGHTING);
    glDisable(GL_BLEND);

    View Slide

  17. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 16
    jgs
    Planar Reflections
    § But, the image’s reflection falls off the floor.

    View Slide

  18. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 17
    jgs
    Planar Reflections
    § Stencil can help to maintain the floor
    § Clear stencil to zero.
    § Draw floor polygon with stencil set to one.
    § Then, only draw reflection where stencil is one.
    § Finally, deactivate stencil and draw the rest of the scene.

    View Slide

  19. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 18
    jgs
    Planar Reflections
    glEnable(GL_STENCIL_TEST);
    glDisable(GL_DEPTH_TEST);
    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
    glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
    glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
    // Draw the PLAIN for the stencil
    glPushMatrix();
    glTranslatef(-900, 0, -900);
    glCallList(display1);
    glPopMatrix();
    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    glEnable(GL_DEPTH_TEST);
    glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

    View Slide

  20. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 19
    jgs
    // boxes reflections
    glPushMatrix();
    glScalef(1.0, -1.0, 1.0);
    glCallList(display2);
    glPopMatrix();
    glPushMatrix();
    glTranslatef(200, 0, 0);
    glScalef(1.0, -1.0, 1.0);
    glCallList(display3);
    glPopMatrix();
    glPushMatrix();
    glTranslatef(-200, 0, 0);
    glScalef(1.0, -1.0, 1.0);
    glCallList(display4);
    glPopMatrix();
    glDisable(GL_STENCIL_TEST);

    View Slide

  21. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 20
    jgs
    Planar Reflections

    View Slide

  22. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 21
    jgs
    One Last Thing
    void display(void) {
    glEnable(GL_CULL_FACE);
    // checks all the faces that are front facing towards
    // the viewer and renders those while discarding all
    // the faces that are back facing
    // Improve performance also!
    glDisable(GL_CULL_FACE);
    }

    View Slide

  23. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 22
    jgs
    Test Yourselves
    § Test 1. Add reflections to your game. For instance, add a mirror or a
    table with a reflective surface
    § Test 2. Add a menu that allows enable/disable reflections

    View Slide

  24. jgs
    SER431 Advanced Graphics
    Javier Gonzalez-Sanchez
    [email protected]
    Fall 2018
    Disclaimer. These slides can only be used as study material for the class SER431 at ASU. They cannot be
    distributed or used for another purpose.

    View Slide