/***************************** Example Program to illustrate 1 - creating simple shadows 2 - querying the system state A simple triangle and its shadow are drawn. A light is at location 5,20,0 ******************************/ #include #include /*******************************************************************/ void print_matrix() { GLfloat mv[16]; //matrix vector int r,c; glGetFloatv(GL_MODELVIEW_MATRIX,mv); for (c=0;c<4;c++) { for (r=0;r<4;r++) printf("%5.2f ",mv[c+r*4]); printf("\n"); } printf("\n\n"); } /*******************************************************************/ void display(void) { GLfloat shadow_matrix[16]; int i; /**** build the shadow matrix *****/ for (i=0;i<16;i++) shadow_matrix[i] = 0.0; shadow_matrix[0] = shadow_matrix[5] = shadow_matrix[10] = 1.0; shadow_matrix[7] = -1.0/20; /***** clear the screen ******/ glClearColor (0.0, 0.0, 0.0, 0.0); // clear to black glClear(GL_COLOR_BUFFER_BIT); /**** draw a triangle in white ****/ glColor3f(1.0,1.0,1.0); glBegin(GL_POLYGON); glVertex3i(10,14,-10); glVertex3i(-10,14,-10); glVertex3i(0,9,-5); glEnd(); /***** reset the Modelview Matrix to create shadow ****/ glTranslatef(5,20,0); // step 3: move light back to original position print_matrix(); /*** when debuging it was useful to tilt the shadow glRotatef(45,1,0,0); ****/ glMultMatrixf(shadow_matrix); // step 2: add the shadow effect matrix print_matrix(); glTranslatef(-5,-20,0); // step 1: move the light to the origin print_matrix(); /***** redraw the triangle in a shadow color *****/ glColor3f(0.3,0.3,0.3); glBegin(GL_POLYGON); glVertex3i(10,14,-10); glVertex3i(-10,14,-10); glVertex3i(0,9,-5); glEnd(); glFlush(); } /********************************************************/ void reshape(int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-15.0, 15.0, -1.0, 15.0, 5.0, 80.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /********************************************************/ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_RGB); glutInitWindowSize (450, 450); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0; }