1 #include <stdlib.h>
2 #include <GL/glew.h>
3 #include "glut_wrap.h"
4 
5 
6 static const float black[4]    = { 0, 0, 0, 1 };
7 static const float white[4]    = { 1, 1, 1, 1 };
8 static const float diffuse[4]  = { 1, 0, 0, 1 };
9 static const float diffuseb[4] = { 0, 1, 0, 1 };
10 static const float specular[4] = { 0.5, 0.5, 0.5, 1 };
11 static const float ambient[4]  = { 0.2, 0.2, 0.2, 1 };
12 static const float lightpos[4] = { 0, 0, 10, 0 };
13 static const float shininess = 50;
14 
15 static double angle = 0.0;
16 
17 static int autorotate = 1;
18 static double angle_delta = 1.0;
19 static int timeout = 10;
20 
21 
22 static void
display(void)23 display(void)
24 {
25    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
26    glPushMatrix();
27    glRotated(angle, 0.0, 1.0, 0.0);
28 
29    glBegin(GL_QUADS);
30    glColor3f(1, 1, 1);
31    glNormal3d(0, 0, 1);
32    glVertex3d(-1, -1, 0);
33    glVertex3d(+1, -1, 0);
34    glVertex3d(+1, +1, 0);
35    glVertex3d(-1, +1, 0);
36    glEnd();
37 
38    glPopMatrix();
39 
40    glutSwapBuffers();
41 }
42 
43 
44 static void
timer(int value)45 timer(int value)
46 {
47    angle += angle_delta;
48    glutTimerFunc(timeout, timer, 0);
49    glutPostRedisplay();
50 }
51 
52 
53 static void
key(unsigned char key,int x,int y)54 key(unsigned char key, int x, int y)
55 {
56    if (key == 27) {
57       exit(0);
58    }
59 }
60 
61 
62 int
main(int argc,char ** argv)63 main(int argc, char **argv)
64 {
65    /* init glut */
66    glutInit(&argc, argv);
67    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
68    glutCreateWindow("Backface specular test");
69    glutDisplayFunc(display);
70    glutKeyboardFunc(key);
71    if (autorotate)
72       glutTimerFunc(timeout, timer, 0);
73 
74    /* setup lighting */
75    glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
76    glMaterialfv(GL_BACK,  GL_DIFFUSE, diffuseb);
77 
78    glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION,  black);
79    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,  specular);
80    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT,   ambient);
81    glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, shininess);
82 
83    glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
84    glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
85    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
86 
87    glLightfv(GL_LIGHT0, GL_AMBIENT,  black);
88    glLightfv(GL_LIGHT0, GL_DIFFUSE,  white);
89    glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
90    glLightfv(GL_LIGHT0, GL_SPECULAR, white);
91 
92    glDisable(GL_CULL_FACE);
93    glEnable(GL_LIGHTING);
94    glEnable(GL_LIGHT0);
95 
96    /* setup camera */
97    glMatrixMode(GL_PROJECTION);
98    gluPerspective(30.0, 1.0, 1.0, 10.0);
99    glMatrixMode(GL_MODELVIEW);
100    gluLookAt(0.0, 0.0, 5.0,
101              0.0, 0.0, 0.0,
102              0.0, 1.0, 0.0);
103 
104    /* setup misc */
105    glEnable(GL_DEPTH_TEST);
106    glClearColor(0, 0, 1, 1);
107 
108    /* run */
109    glutMainLoop();
110 
111    return 0;
112 }
113