aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/extras/Mesa_6.4.2/progs/demos/spectex.c
blob: 6ab1191579c3be6d41e43329c40e84c8e2a057b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272

/*
 * GLUT demonstration of texturing with specular highlights.
 *
 * When drawing a lit, textured surface one usually wants the specular
 * highlight to override the texture colors.  However, OpenGL applies
 * texturing after lighting so the specular highlight is modulated by
 * the texture.
 *
 * The solution here shown here is a two-pass algorithm:
 *  1. Draw the textured surface without specular lighting.
 *  2. Enable blending to add the next pass:
 *  3. Redraw the surface with a matte white material and only the
 *     specular components of light sources enabled.
 *
 * Brian Paul  February 1997
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>


static GLUquadricObj *Quadric;
static GLuint Sphere;
static GLfloat LightPos[4] = {10.0, 10.0, 10.0, 1.0};
static GLfloat Delta = 20.0;
static GLint Mode = 4;

/*static GLfloat Blue[4] = {0.0, 0.0, 1.0, 1.0};*/
/*static GLfloat Gray[4] = {0.5, 0.5, 0.5, 1.0};*/
static GLfloat Black[4] = {0.0, 0.0, 0.0, 1.0};
static GLfloat White[4] = {1.0, 1.0, 1.0, 1.0};

static GLboolean smooth = 1;

static void
Idle(void)
{
   static double t0 = -1.;
   double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;;
   if (t0 < 0.0)
      t0 = t;
   dt = t - t0;
   t0 = t;
   LightPos[0] += Delta * dt;
   if (LightPos[0]>15.0 || LightPos[0]<-15.0)
      Delta = -Delta;

   glutPostRedisplay();
}


static void Display( void )
{
   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

   glLightfv(GL_LIGHT0, GL_POSITION, LightPos);

   glPushMatrix();
   glRotatef(90.0, 1.0, 0.0, 0.0);

   if (Mode==0) {
      /* Typical method: diffuse + specular + texture */
      glEnable(GL_TEXTURE_2D);
      glLightfv(GL_LIGHT0, GL_DIFFUSE, White);  /* enable diffuse */
      glLightfv(GL_LIGHT0, GL_SPECULAR, White);  /* enable specular */
#ifdef GL_VERSION_1_2
      glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SINGLE_COLOR);
#endif
      glCallList(Sphere);
   }
   else if (Mode==1) {
      /* just specular highlight */
      glDisable(GL_TEXTURE_2D);
      glLightfv(GL_LIGHT0, GL_DIFFUSE, Black);  /* disable diffuse */
      glLightfv(GL_LIGHT0, GL_SPECULAR, White);  /* enable specular */
#ifdef GL_VERSION_1_2
      glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SINGLE_COLOR);
#endif
      glCallList(Sphere);
   }
   else if (Mode==2) {
      /* diffuse textured */
      glEnable(GL_TEXTURE_2D);
      glLightfv(GL_LIGHT0, GL_DIFFUSE, White);  /* enable diffuse */
      glLightfv(GL_LIGHT0, GL_SPECULAR, Black);  /* disable specular */
#ifdef GL_VERSION_1_2
      glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SINGLE_COLOR);
#endif
      glCallList(Sphere);
   }
   else if (Mode==3) {
      /* 2-pass: diffuse textured then add specular highlight*/
      glEnable(GL_TEXTURE_2D);
      glLightfv(GL_LIGHT0, GL_DIFFUSE, White);  /* enable diffuse */
      glLightfv(GL_LIGHT0, GL_SPECULAR, Black);  /* disable specular */
#ifdef GL_VERSION_1_2
      glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SINGLE_COLOR);
#endif
      glCallList(Sphere);
      /* specular highlight */
      glDepthFunc(GL_EQUAL);  /* redraw same pixels */
      glDisable(GL_TEXTURE_2D);
      glEnable(GL_BLEND);  /* add */
      glLightfv(GL_LIGHT0, GL_DIFFUSE, Black);  /* disable diffuse */
      glLightfv(GL_LIGHT0, GL_SPECULAR, White);  /* enable specular */
      glCallList(Sphere);
      glDepthFunc(GL_LESS);
      glDisable(GL_BLEND);
   }
   else if (Mode==4) {
      /* OpenGL 1.2's separate diffuse and specular color */
      glEnable(GL_TEXTURE_2D);
      glLightfv(GL_LIGHT0, GL_DIFFUSE, White);  /* enable diffuse */
      glLightfv(GL_LIGHT0, GL_SPECULAR, White);  /* enable specular */
#ifdef GL_VERSION_1_2
      glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
#endif
      glCallList(Sphere);
   }

   glPopMatrix();

   glutSwapBuffers();
}


static void Reshape( int width, int height )
{
   glViewport( 0, 0, width, height );
   glMatrixMode( GL_PROJECTION );
   glLoadIdentity();
   glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
   glMatrixMode( GL_MODELVIEW );
   glLoadIdentity();
   glTranslatef( 0.0, 0.0, -12.0 );
}


static void Key( unsigned char key, int x, int y )
{
   (void) x;
   (void) y;
   switch (key) {
   case 27:
      exit(0);
      break;
   case 's':
      smooth = !smooth;
      if (smooth)
         glShadeModel(GL_SMOOTH);
      else
         glShadeModel(GL_FLAT);
      break;
   }
   glutPostRedisplay();
}


static void SpecialKey( int key, int x, int y )
{
   (void) x;
   (void) y;
   switch (key) {
      case GLUT_KEY_UP:
         break;
      case GLUT_KEY_DOWN:
         break;
   }
   glutPostRedisplay();
}


static void Init( void )
{
   int i, j;
   GLubyte texImage[64][64][3];

   glEnable(GL_LIGHTING);
   glEnable(GL_LIGHT0);
   glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 0);
   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, Black);

   glShadeModel(GL_SMOOTH);

   glMaterialfv(GL_FRONT, GL_DIFFUSE, White);
   glMaterialfv(GL_FRONT, GL_SPECULAR, White);
   glMaterialf(GL_FRONT, GL_SHININESS, 20.0);

   /* Actually, these are set again later */
   glLightfv(GL_LIGHT0, GL_DIFFUSE, White);
   glLightfv(GL_LIGHT0, GL_SPECULAR, White);

   Quadric = gluNewQuadric();
   gluQuadricTexture( Quadric, GL_TRUE );

   Sphere= glGenLists(1);
   glNewList( Sphere, GL_COMPILE );
   gluSphere( Quadric, 1.0, 24, 24 );
   glEndList();

   glEnable(GL_DEPTH_TEST);
   glEnable(GL_CULL_FACE);

   for (i=0;i<64;i++) {
      for (j=0;j<64;j++) {
         int k = ((i>>3)&1) ^ ((j>>3)&1);
         texImage[i][j][0] = 255*k;
         texImage[i][j][1] = 255*(1-k);
         texImage[i][j][2] = 0;
      }
   }

   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   glTexImage2D( GL_TEXTURE_2D,
                 0,
                 3,
                 64, 64,
                 0,
                 GL_RGB, GL_UNSIGNED_BYTE,
                 texImage );
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
   glEnable(GL_TEXTURE_2D);

   glBlendFunc(GL_ONE, GL_ONE);
}


static void ModeMenu(int entry)
{
   if (entry==99)
      exit(0);
   Mode = entry;
}


int main( int argc, char *argv[] )
{

   glutInit( &argc, argv );
   glutInitWindowPosition( 0, 0 );
   glutInitWindowSize( 300, 300 );

   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );

   glutCreateWindow( "spectex" );

   Init();

   glutReshapeFunc( Reshape );
   glutKeyboardFunc( Key );
   glutSpecialFunc( SpecialKey );
   glutDisplayFunc( Display );
   glutIdleFunc( Idle );

   glutCreateMenu( ModeMenu );
   glutAddMenuEntry("1-pass lighting + texturing", 0);
   glutAddMenuEntry("specular lighting", 1);
   glutAddMenuEntry("diffuse lighting + texturing", 2);
   glutAddMenuEntry("2-pass lighting + texturing", 3);
#ifdef GL_VERSION_1_2
   glutAddMenuEntry("OpenGL 1.2 separate specular", 4);
#endif
   glutAddMenuEntry("Quit", 99);
   glutAttachMenu(GLUT_RIGHT_BUTTON);

   glutMainLoop();
   return 0;
}