Switch to full style
C++ openGL code examples
Post a reply

Change the shading of object

Sat Mar 17, 2012 11:01 pm

In this example we change the shading model of object affected by light source from its default which is GL_SMOOTH to GL_FLAT.
Code:


#include <GL\glut.h>
#include <math.h>      // For math routines (such as sqrt & trig).
 
GLdouble radius
=1.5;
GLfloat qaWhite[] = {1.0, 1.0, 1.0, 1.0}; //White Color
GLfloat qaRed[] = {1.0, 0.0, 0.0, 1.0}; //Red Color

    // Set lighting intensity and color
GLfloat qaAmbientLight[]    = {0.1, 0.1, 0.1, 1.0};
GLfloat qaDiffuseLight[]    = {1, 1, 1, 1.0};
    // Light source position
GLfloat qaLightPosition[]    = {-2, 0, -9.5, 1};// Positional Light
GLfloat qaLightDirection[]    = {1, 1, 1, 0};// Directional Light

void display(void);
void reshape(int x, int y);
 
void initLighting
()
{
    // Add smooth
    glShadeModel(GL_FLAT);
    // Enable lighting
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

     // Set lighting intensity and color
       glLightfv(GL_LIGHT0, GL_AMBIENT, qaAmbientLight);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, qaDiffuseLight);
     glLightfv(GL_LIGHT0, GL_POSITION, qaLightPosition);
    ////////////////////////////////////////////////
    //glLightf( GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.2f );
     glLightf( GL_LIGHT0, GL_LINEAR_ATTENUATION , 0.5f );
    //glLightf( GL_LIGHT0, GL_QUADRATIC_ATTENUATION , 0.009f );

        

}
void keyboard(unsigned char key, int x, int y)
{
    
     
     if 
(key == 'l' || key == 'L')  
    
{ 
          glEnable
(GL_LIGHTING);
    }
    else if (key == 'd' || key == 'D')  
    
{ 
        glDisable
(GL_LIGHTING);
    }

}

 
int main 
(int argc, char **argv)
{
    glutInit(&argc, argv); 
     glutInitDisplayMode
(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
    glutInitWindowSize(750,750);
    glutCreateWindow("Teapot -light attenuation");
    initLighting(); 

     
 
    glutDisplayFunc
(display);
    glutKeyboardFunc(keyboard); // Register keyboard callback
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}
 
void display
(void)
{


    glMatrixMode(GL_MODELVIEW);
    // clear the drawing buffer.
    glClear(GL_COLOR_BUFFER_BIT);
    // clear the identity matrix.
    glLoadIdentity();
   
    glTranslatef
(0.0,0.0,-5.0);
 
    


      glPushMatrix
();
 
      glTranslatef
(0.0,0.0,-2.0);
    // Set material properties
       glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, qaRed);

    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, qaRed);

 
     glutSolidTeapot
(radius);
     glPopMatrix();


     

    glFlush
();
    glutSwapBuffers();      
   
}
 
void reshape
(int x, int y)
{
    if (== 0 || x == 0) return;   
    glMatrixMode
(GL_PROJECTION);  
    glLoadIdentity
(); 
    
    gluPerspective
(39.0,(GLdouble)x/(GLdouble)y,0.6,40.0);
    glMatrixMode(GL_MODELVIEW);
    glViewport(0,0,x,y);  //Use the whole window for rendering
}
 










 



Attachments
smooth_shade.png
smooth_shade.png (63.01 KiB) Viewed 8596 times
flat_shade.png
flat_shade.png (20.27 KiB) Viewed 8596 times

Post a reply
  Related Posts  to : Change the shading of object
 How to use connection object in jsp     -  
 Object without new Operator     -  
 object detection     -  
 Object Orientation in c++, how to a do object Orientation     -  
 use out object in a method at jsp     -  
 3D-object loader     -  
 Connection object in jsp     -  
 Java Object Reading     -  
 Create Object and get its type     -  
 object's finalize() in java     -  

Topic Tags

C++ Graphics