2005-11-10


  • Select a lighting model
  • OpenGL's lighting model has 3 components:

    void glLightModel{if}( GLenum pname, TYPE param );
    
    void glLightModel{if}v( GLenum pname, TYPE *param );
        

    Parameter Name

    Default Value

    Meaning

    GL_LIGHT_MODEL_AMBIENT

    (0.2, 0.2, 0.2, 1.0)

    ambient RGBA intensity of the entire scene

    GL_LIGHT_MODEL_LOCAL_VIEWER

    0.0 or GL_FALSE

    how specular reflection angles are computed

    GL_LIGHT_MODEL_TWO_SIDE

    0.0 or GL_FALSE

    choose between one-sided or two-sided lighting

    GL_LIGHT_MODEL_COLOR_CONTROL GL_SINGLE_COLOR whether specular color is calculated separately from ambient and diffuse

  • Enable lighting
  • glEnable(GL_LIGHTING);
    
    glEnable(GL_LIGHT0);
        

  • Defining Material Properties

    glMaterial*()
    void glMaterial{if}  (GLenum face, GLenum pname, TYPE param);
    void glMaterial{if}v (GLenum face, GLenum pname, TYPE *param);
    
    face -> { GL_FRONT, GL_BACK, GL_FRONT_AND_BACK }
    Non-vector version only works for GL_SHINNESS.
        
    Like colors, material properties are 'state' oriented.

    Table 6-3. Default Values for pname Parameter of glMaterial*()

    Parameter Name

    Default Value

    Meaning

    GL_AMBIENT

    (0.2, 0.2, 0.2, 1.0)

    ambient color of material

    GL_DIFFUSE

    (0.8, 0.8, 0.8, 1.0)

    diffuse color of material

    GL_AMBIENT_AND_DIFFUSE

    ambient and diffuse color of material

    GL_SPECULAR

    (0.0, 0.0, 0.0, 1.0)

    specular color of material

    GL_SHININESS

    0.0

    specular exponent

    GL_EMISSION

    (0.0, 0.0, 0.0, 1.0)

    emissive color of material

    GL_COLOR_INDEXES

    (0,1,1)

    ambient, diffuse, and specular color indices

    Example:Different Material Properties

       //material.cpp
       GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
       GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
       GLfloat mat_ambient_color[] = { 0.8, 0.8, 0.2, 1.0 };
       GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
       GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
       GLfloat no_shininess[] = { 0.0 };
       GLfloat low_shininess[] = { 5.0 };
       GLfloat high_shininess[] = { 100.0 };
       GLfloat mat_emission[] = {0.3, 0.2, 0.2, 0.0};
    
       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    /*  draw sphere in first row, first column
     *  diffuse reflection only; no ambient or specular  
     */
       glPushMatrix();
       glTranslatef (-3.75, 3.0, 0.0);
       glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
       glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
       glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
       glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
       glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
       glutSolidSphere(1.0, 16, 16);
       glPopMatrix();
    
    /*  draw sphere in first row, second column
     *  diffuse and specular reflection; low shininess; no ambient
     */
       glPushMatrix();
       glTranslatef (-1.25, 3.0, 0.0);
       glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
       glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
       glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
       glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
       glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
       glutSolidSphere(1.0, 16, 16);
       glPopMatrix();
    
    /*  draw sphere in first row, third column
     *  diffuse and specular reflection; high shininess; no ambient
     */
       glPushMatrix();
       glTranslatef (1.25, 3.0, 0.0);
       glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
       glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
       glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
       glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
       glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
       glutSolidSphere(1.0, 16, 16);
       glPopMatrix();
    
    /*  draw sphere in first row, fourth column
     *  diffuse reflection; emission; no ambient or specular refl.
     */
       glPushMatrix();
       glTranslatef (3.75, 3.0, 0.0);
       glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
       glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
       glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
       glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
       glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
       glutSolidSphere(1.0, 16, 16);
       glPopMatrix();
    

     
     
     
    No ambient reflection
     

     

    Grey ambient reflection
     

     

    Blue ambient reflection
     

    Figure column:

    1. blue diffuse material color with no specular properties
    2. adds white specular reflection with a low shininess exponent
    3. uses a high shininess exponent and thus a more concentrated light
    4. uses blue diffuse color and, instead of specular reflection, adds an emissive component

    More teapots:

    In summary, the default OpenGL lighting math model ('redbook' page. 220 - page. 225)

    vertex color = emissionmaterial +

    ambientlight model * ambientmaterial +

    [ambientlight *ambientmaterial +

    (max { L (dot) n , 0} ) * diffuselight * diffusematerial +

    (max { s (dot) n , 0} )shininess * specularlight * specularmaterial ] i


    Multiple views with GLUT

    GLUT Windows management sub-api

    "GLUT supports two types of windows: top-level windows and subwindows. Both types support OpenGL rendering and GLUT callbacks. There is a single identifier space for both types of windows."

    Ref: GLUT - Windows Managements

    int glutCreateWindow(char *name) Creates a new top-level window
    int glutCreateSubWindow(int win,
       int x, int y, int width, int height)
    Creates a sub-window
    void glutSetWindow(int winId) Set the window with ID winId as current window
    int glutGetWindow(void) Requests the identifier for the current window
    void glutDestroyWindow(int winId) Destroys the window specified by winId
    void glutPostRedisplay(void) Tells the GLUT event processor that the current window needs to be redisplayed
    void glutSwapBuffers(void) Swaps the buffers for the current window
    void glutPositionWindow(int x, int y) Requests a change in the position of the window
    void glutReshapeWindow(int width, int height) Requests a change in the size of the window
    void glutFullScreen() Requests that the current window be made full screen
    void glutPopWindow(void)
    void glutPushWindow(void)
    Push or Pop the current window relative to others in the stack
    void glutShowWindow(void)
    void glutHideWindow(void)
    void glutIconifyWindow(void)
    Show, hide or iconify the current window
    void glutSetWindowTitle(char *name)
    void glutSetIconTitle(char *name)
    Set title bar in window or iconified window
    Let's look at the simple code.


    GLUI - GLUI User Interface Library

    What is GLUI?

    "GLUI is a GLUT-based C++ user interface library which provides controls such as buttons, checkboxes, radio buttons, and spinners to OpenGL applications. It is window-system independent, relying on GLUT to handle all system-dependent issues, such as window and mouse management." - GLUI website

    http://glui.sourceforge.net/


    Texture mapping... a quick overview

    Steps of doing texture mapping: Example: checker.c

    1. Create a textue object and specify a texture for that object.
    2. Load or create an image as texture(an 1 or multi-dimension array basically).

    3. Indicate how the texture is to be applied to each pixel
    4. Enable texture mapping
    5. Draw the scene, supplying both texture and geometric coordinates
    Some hints


    Julian Yu-Chung Chen
    Last modified: Wed Sep 07 17:40:25 CDT 2005