/*
* This is Phong illumination model, an illumination model for
* non-perfect reflectors.This is the most popular illumination model
* although does not actually explain the real light source.
*/
#include <GL/glut.h>
void init(void)
{
GLfloat mat_shininess[] = { 10.0 };
//set light position
GLfloat light_position[] = { 1.0, 1.0, 2.0, 1.0 };
//define colors for ambient, diffuse, and specular light
GLfloat light_Ka[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat light_Kd[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_Ks[] = { 1.0, 1.0, 1.0, 1.0 };
glClearColor (1.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
/*GL_LIGHT0 means there is only one light source
* we can actually have maximum of 8 light sources
* denoted as GL_LIGHT0, GL_LIGHT1,…,GL_LIGHT7
*/
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
//attach defined lights to light source
glLightfv(GL_LIGHT0, GL_AMBIENT, light_Ka);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_Kd);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_Ks);
glMaterialfv(GL_FRONT, GL_SPECULAR, light_Ks);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
//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.