Total members 11893 |It is currently Tue Nov 05, 2024 8:50 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





In this example we add light to teapot object and rotating the light around the Y axis and around the teapot object.

Code:
#include <GL\glut.h>
#include <math.h>      // For math routines (such as sqrt & trig).

GLfloat xRotatedyRotatedzRotated;
GLdouble radius=2;
GLfloat qaBlack[] = {0.00.00.01.0}; //Black Color
GLfloat qaGreen[] = {1.00.00.01.0}; //Green Color
GLfloat qaWhite[] = {1.01.01.01.0}; //White Color
GLfloat qaRed[] = {1.00.00.01.0}; //Red Color

    // Set lighting intensity and color
GLfloat qaAmbientLight[]    = {0.10.10.11.0};
GLfloat qaDiffuseLight[]    = {1111.0};
GLfloat qaSpecularLight[]    = {1.01.01.01.0};
GLfloat emitLight[] = {0.90.90.90.01};
GLfloat Noemit[] = {0.00.00.01.0};
    
// Light source position
GLfloat qaLightPosition[]    = {0001};// Positional Light
GLfloat qaLightDirection[]    = {1110};// Directional Light

void display(void);
void reshape(int xint y);
 
void idleFunc(void)
{
        if ( 
zRotated 360.0 ) {
         
zRotated -= 360.0*floor(zRotated/360.0);   // Don't allow overflow
      
}

          if ( 
yRotated 360.0 ) {
         
yRotated -= 360.0*floor(yRotated/360.0);   // Don't allow overflow
      
}
     
zRotated += 0.1;
     
yRotated +=0.1;
     
    
display();
}
void initLighting()
{

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

     
// Set lighting intensity and color
       
glLightfv(GL_LIGHT0GL_AMBIENTqaAmbientLight);
    
glLightfv(GL_LIGHT0GL_DIFFUSEqaDiffuseLight);
     
glLightfv(GL_LIGHT0GL_POSITIONqaLightPosition);
     
glLightfv(GL_LIGHT0GL_SPECULARqaSpecularLight);
    
////////////////////////////////////////////////


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

}

 
int main (int argcchar **argv)
{
    
glutInit(&argcargv); 
     
glutInitDisplayMode(GLUT_DOUBLE GLUT_RGB GLUT_DEPTH );
    
glutInitWindowSize(350,350);
    
glutCreateWindow("Teapot -");
    
initLighting(); 

    
xRotated yRotated zRotated 0.0;
    
    
glutIdleFunc(idleFunc);
    
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,-20.0);
 
   
glPushMatrix();
 
     
    
glTranslatef(0.0,0.0,0);
    
// Set material properties
       
glMaterialfv(GL_FRONT_AND_BACKGL_AMBIENTqaRed);

    
glMaterialfv(GL_FRONT_AND_BACKGL_DIFFUSEqaRed);

     
glMaterialfv(GL_FRONT_AND_BACKGL_SPECULARqaWhite);

     
glMaterialf(GL_FRONT_AND_BACKGL_SHININESS1);
 
         
glutSolidTeapot(radius);
     
glPopMatrix();



    
glPushMatrix();  
    
glRotatef(yRotated,0.0,1.0,0.0);
    
glTranslatef(5.0,0.0,0.0);
     
glMaterialfv(GL_FRONT_AND_BACKGL_EMISSIONemitLight);   // Make sphere glow (emissive) 
     
glutSolidSphere(radius/6,25,25);
     
glMaterialfv(GL_FRONT_AND_BACKGL_EMISSIONNoemit);  
    
glPopMatrix(); 

    
glPushMatrix();  
    
glRotatef(-yRotated,0.0,1.0,0.0);
    
glTranslatef(5.0,0.0,0.0);
      
glLightfv(GL_LIGHT0GL_POSITIONqaLightPosition);
    
glPopMatrix(); 


    
glFlush();
    
glutSwapBuffers();      
   
}
 
void reshape(int xint y)
{
    if (
== || == 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:
TeaPot_light.png
TeaPot_light.png [ 7.82 KiB | Viewed 11087 times ]

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada
Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : add lighting to teapot 3d
 Drawing teapot using OpenGL.     -  
 Add texture to teapot object using     -  
 Turn on-off lighting effects     -  
 Adding lighting to a solid sphere     -  
 demonstrate how to add lighting and material properties     -  



Topic Tags

C++ Graphics
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com