/*
* OpenGL implementation of Warns illumination model(spotlight)
* for better understanding read Computer Graphics by Hill
* page 426
*/
#include <GL/glut.h>
void init(void)
{
GLfloat mat_specular[] = { 1.0, 1.0, 0.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
//set spotlight’s position
GLfloat dir_light[]={2.0, 1.0, -4.0};
GLfloat light_position[] = { 2.0, 1.0, 5.0, 1.0 };
glClearColor (1.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
/*
* a cutoff angle of 45 degrees
* default value is 180 degrees
*/
glLightf(GL_LIGHT0,GL_SPOT_CUTOFF, 45.0);
/*
* The desired falloff of light with angle(E) is 1
* As E increases the spotlight disappears
* default value is 0
*/
glLightf(GL_LIGHT0,GL_SPOT_EXPONENT, 1.0);
glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,dir_light);
//enables light
glEnable(GL_LIGHTING);
//enables this particular source.
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere (1.0, 20, 16);
glutSwapBuffers();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w, 1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho (-1.5*(GLfloat)w/(GLfloat)h, 1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (”Single-Point Light Source”);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
All comments are moderated. Your comments will not appear here unless approved by the blog owner. Thank you.