Total members 11889 |It is currently Fri Mar 29, 2024 1:35 pm 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 attenuation affect on object by controlling the light attenuation factor , there different type of effectors : GL_CONSTANT_ATTENUATION,GL_LINEAR_ATTENUATION and GL_QUADRATIC_ATTENUATION.

Code:
#include <GL\glut.h>
#include <math.h>      // For math routines (such as sqrt & trig).
 
GLdouble radius
=1;
GLfloat qaBlack[] = {0.0, 0.0, 0.0, 1.0}; //Black Color
GLfloat qaGreen[] = {0.0, 1.0, 0.0, 1.0}; //Green Color
GLfloat qaWhite[] = {1.0, 1.0, 1.0, 1.0}; //White Color
GLfloat qaRed[] = {1.0, 0.0, 0.0, 1.0}; //Red Color
GLfloat qaBlue[] = {0.0, 0.0, 1.0, 1.0}; //Blue 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, -14.5, 1};// Positional Light
GLfloat qaLightDirection[]    = {1, 1, 1, 0};// Directional Light

void display(void);
void reshape(int x, int y);
 
void initLighting
()
{

    // 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,-10.0);
 
      glPushMatrix
();
 
      glTranslatef
(2.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();
 


      glPushMatrix
();
 
      glTranslatef
(-2.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:
Attenuation.jpg
Attenuation.jpg [ 15.37 KiB | Viewed 8066 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 attenuation to light
 Simple Light     -  
 Add light to texture     -  
 Add spot light to object     -  
 rotate sphere in a circle with light     -  
 J2ME Image Capturing with Flash Light     -  
 modeling traffic light control system using neural network     -  



Topic Tags

C++ Graphics






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