nv: define NV30/40/50_MAX_TEXTURE_LEVELS
[mesa.git] / progs / demos / arbfplight.c
1 /*
2 * Use GL_ARB_fragment_program and GL_ARB_vertex_program to implement
3 * simple per-pixel lighting.
4 *
5 * Brian Paul
6 * 17 April 2003
7 */
8
9 #include <assert.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <math.h>
14 #include <GL/glut.h>
15
16
17 static GLfloat Diffuse[4] = { 0.5, 0.5, 1.0, 1.0 };
18 static GLfloat Specular[4] = { 0.8, 0.8, 0.8, 1.0 };
19 static GLfloat LightPos[4] = { 0.0, 10.0, 20.0, 1.0 };
20 static GLfloat Delta = 1.0;
21
22 static GLuint FragProg;
23 static GLuint VertProg;
24 static GLboolean Anim = GL_TRUE;
25 static GLboolean Wire = GL_FALSE;
26 static GLboolean PixelLight = GL_TRUE;
27 static GLint Win;
28
29 static GLint T0 = 0;
30 static GLint Frames = 0;
31
32 static GLfloat Xrot = 0, Yrot = 0;
33
34 static PFNGLPROGRAMLOCALPARAMETER4FVARBPROC glProgramLocalParameter4fvARB_func;
35 static PFNGLPROGRAMLOCALPARAMETER4DARBPROC glProgramLocalParameter4dARB_func;
36 static PFNGLGETPROGRAMLOCALPARAMETERDVARBPROC glGetProgramLocalParameterdvARB_func;
37 static PFNGLGENPROGRAMSARBPROC glGenProgramsARB_func;
38 static PFNGLPROGRAMSTRINGARBPROC glProgramStringARB_func;
39 static PFNGLBINDPROGRAMARBPROC glBindProgramARB_func;
40 static PFNGLISPROGRAMARBPROC glIsProgramARB_func;
41 static PFNGLDELETEPROGRAMSARBPROC glDeleteProgramsARB_func;
42
43 /* These must match the indexes used in the fragment program */
44 #define LIGHTPOS 3
45
46 /* Set to one to test ARB_fog_linear program option */
47 #define DO_FRAGMENT_FOG 0
48
49 static void normalize (GLfloat *dst, const GLfloat *src)
50 {
51 GLfloat len = sqrt (src[0] * src[0] + src[1] * src[1] + src[2] * src[2]);
52 dst[0] = src[0] / len;
53 dst[1] = src[1] / len;
54 dst[2] = src[2] / len;
55 }
56
57 static void Redisplay( void )
58 {
59 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
60
61 if (PixelLight) {
62 GLfloat pos[4];
63
64 normalize( pos, LightPos );
65 pos[3] = LightPos[3];
66 glProgramLocalParameter4fvARB_func(GL_FRAGMENT_PROGRAM_ARB,
67 LIGHTPOS, pos);
68 glEnable(GL_FRAGMENT_PROGRAM_ARB);
69 glEnable(GL_VERTEX_PROGRAM_ARB);
70 glDisable(GL_LIGHTING);
71 }
72 else {
73 glLightfv(GL_LIGHT0, GL_POSITION, LightPos);
74 glDisable(GL_FRAGMENT_PROGRAM_ARB);
75 glDisable(GL_VERTEX_PROGRAM_ARB);
76 glEnable(GL_LIGHTING);
77 }
78
79 glPushMatrix();
80 glRotatef(Xrot, 1, 0, 0);
81 glRotatef(Yrot, 0, 1, 0);
82 glutSolidSphere(2.0, 10, 5);
83 glPopMatrix();
84
85 glutSwapBuffers();
86
87 Frames++;
88
89 if (Anim) {
90 GLint t = glutGet(GLUT_ELAPSED_TIME);
91 if (t - T0 >= 5000) {
92 GLfloat seconds = (t - T0) / 1000.0;
93 GLfloat fps = Frames / seconds;
94 printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames, seconds, fps);
95 fflush(stdout);
96 T0 = t;
97 Frames = 0;
98 }
99 }
100 }
101
102
103 static void Idle(void)
104 {
105 LightPos[0] += Delta;
106 if (LightPos[0] > 25.0)
107 Delta = -1.0;
108 else if (LightPos[0] <- 25.0)
109 Delta = 1.0;
110 glutPostRedisplay();
111 }
112
113
114 static void Reshape( int width, int height )
115 {
116 glViewport( 0, 0, width, height );
117 glMatrixMode( GL_PROJECTION );
118 glLoadIdentity();
119 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
120 glMatrixMode( GL_MODELVIEW );
121 glLoadIdentity();
122 glTranslatef( 0.0, 0.0, -15.0 );
123 }
124
125
126 static void Key( unsigned char key, int x, int y )
127 {
128 (void) x;
129 (void) y;
130 switch (key) {
131 case ' ':
132 case 'a':
133 Anim = !Anim;
134 if (Anim)
135 glutIdleFunc(Idle);
136 else
137 glutIdleFunc(NULL);
138 break;
139 case 'x':
140 LightPos[0] -= 1.0;
141 break;
142 case 'X':
143 LightPos[0] += 1.0;
144 break;
145 case 'w':
146 Wire = !Wire;
147 if (Wire)
148 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
149 else
150 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
151 break;
152 case 'p':
153 PixelLight = !PixelLight;
154 if (PixelLight) {
155 printf("Per-pixel lighting\n");
156 }
157 else {
158 printf("Conventional lighting\n");
159 }
160 break;
161 case 27:
162 glDeleteProgramsARB_func(1, &VertProg);
163 glDeleteProgramsARB_func(1, &FragProg);
164 glutDestroyWindow(Win);
165 exit(0);
166 break;
167 }
168 glutPostRedisplay();
169 }
170
171 static void SpecialKey( int key, int x, int y )
172 {
173 const GLfloat step = 3.0;
174 (void) x;
175 (void) y;
176 switch (key) {
177 case GLUT_KEY_UP:
178 Xrot -= step;
179 break;
180 case GLUT_KEY_DOWN:
181 Xrot += step;
182 break;
183 case GLUT_KEY_LEFT:
184 Yrot -= step;
185 break;
186 case GLUT_KEY_RIGHT:
187 Yrot += step;
188 break;
189 }
190 glutPostRedisplay();
191 }
192
193
194 /* A helper for finding errors in program strings */
195 static int FindLine( const char *program, int position )
196 {
197 int i, line = 1;
198 for (i = 0; i < position; i++) {
199 if (program[i] == '\n')
200 line++;
201 }
202 return line;
203 }
204
205
206 static void Init( void )
207 {
208 GLint errorPos;
209
210 /* Yes, this could be expressed more efficiently */
211 static const char *fragProgramText =
212 "!!ARBfp1.0\n"
213 #if DO_FRAGMENT_FOG
214 "OPTION ARB_fog_linear; \n"
215 #endif
216 "PARAM Diffuse = state.material.diffuse; \n"
217 "PARAM Specular = state.material.specular; \n"
218 "PARAM LightPos = program.local[3]; \n"
219 "TEMP normal, len; \n"
220 "TEMP dotProd, specAtten; \n"
221 "TEMP diffuseColor, specularColor; \n"
222
223 "# Compute normalized normal \n"
224 "DP3 len.x, fragment.texcoord[0], fragment.texcoord[0]; \n"
225 "RSQ len.y, len.x; \n"
226 "MUL normal.xyz, fragment.texcoord[0], len.y; \n"
227
228 "# Compute dot product of light direction and normal vector\n"
229 "DP3_SAT dotProd.x, LightPos, normal; # limited to [0,1]\n"
230
231 "MUL diffuseColor.xyz, Diffuse, dotProd.x; # diffuse attenuation\n"
232
233 "POW specAtten.x, dotProd.x, {20.0}.x; # specular exponent\n"
234
235 "MUL specularColor.xyz, Specular, specAtten.x; # specular attenuation\n"
236
237 "MOV result.color.w, Diffuse; \n"
238 #if DO_FRAGMENT_FOG
239 "# need to clamp color to [0,1] before fogging \n"
240 "ADD_SAT result.color.xyz, diffuseColor, specularColor; # add colors\n"
241 #else
242 "# clamping will be done after program's finished \n"
243 "ADD result.color.xyz, diffuseColor, specularColor; # add colors\n"
244 #endif
245 "END \n"
246 ;
247
248 static const char *vertProgramText =
249 "!!ARBvp1.0\n"
250 "ATTRIB pos = vertex.position; \n"
251 "ATTRIB norm = vertex.normal; \n"
252 "PARAM modelview[4] = { state.matrix.modelview }; \n"
253 "PARAM modelviewProj[4] = { state.matrix.mvp }; \n"
254 "PARAM invModelview[4] = { state.matrix.modelview.invtrans }; \n"
255
256 "# typical modelview/projection transform \n"
257 "DP4 result.position.x, pos, modelviewProj[0]; \n"
258 "DP4 result.position.y, pos, modelviewProj[1]; \n"
259 "DP4 result.position.z, pos, modelviewProj[2]; \n"
260 "DP4 result.position.w, pos, modelviewProj[3]; \n"
261
262 "# transform normal by inv transpose of modelview, put in tex0 \n"
263 "DP3 result.texcoord[0].x, norm, invModelview[0]; \n"
264 "DP3 result.texcoord[0].y, norm, invModelview[1]; \n"
265 "DP3 result.texcoord[0].z, norm, invModelview[2]; \n"
266 "DP3 result.texcoord[0].w, norm, invModelview[3]; \n"
267
268 #if DO_FRAGMENT_FOG
269 "# compute fog coordinate = vertex eye-space Z coord (negated)\n"
270 "DP4 result.fogcoord, -pos, modelview[2]; \n"
271 #endif
272 "END\n";
273 ;
274
275 if (!glutExtensionSupported("GL_ARB_vertex_program")) {
276 printf("Sorry, this demo requires GL_ARB_vertex_program\n");
277 exit(1);
278 }
279 if (!glutExtensionSupported("GL_ARB_fragment_program")) {
280 printf("Sorry, this demo requires GL_ARB_fragment_program\n");
281 exit(1);
282 }
283
284 /*
285 * Get extension function pointers.
286 */
287 glProgramLocalParameter4fvARB_func = (PFNGLPROGRAMLOCALPARAMETER4FVARBPROC) glutGetProcAddress("glProgramLocalParameter4fvARB");
288 assert(glProgramLocalParameter4fvARB_func);
289
290 glProgramLocalParameter4dARB_func = (PFNGLPROGRAMLOCALPARAMETER4DARBPROC) glutGetProcAddress("glProgramLocalParameter4dARB");
291 assert(glProgramLocalParameter4dARB_func);
292
293 glGetProgramLocalParameterdvARB_func = (PFNGLGETPROGRAMLOCALPARAMETERDVARBPROC) glutGetProcAddress("glGetProgramLocalParameterdvARB");
294 assert(glGetProgramLocalParameterdvARB_func);
295
296 glGenProgramsARB_func = (PFNGLGENPROGRAMSARBPROC) glutGetProcAddress("glGenProgramsARB");
297 assert(glGenProgramsARB_func);
298
299 glProgramStringARB_func = (PFNGLPROGRAMSTRINGARBPROC) glutGetProcAddress("glProgramStringARB");
300 assert(glProgramStringARB_func);
301
302 glBindProgramARB_func = (PFNGLBINDPROGRAMARBPROC) glutGetProcAddress("glBindProgramARB");
303 assert(glBindProgramARB_func);
304
305 glIsProgramARB_func = (PFNGLISPROGRAMARBPROC) glutGetProcAddress("glIsProgramARB");
306 assert(glIsProgramARB_func);
307
308 glDeleteProgramsARB_func = (PFNGLDELETEPROGRAMSARBPROC) glutGetProcAddress("glDeleteProgramsARB");
309 assert(glDeleteProgramsARB_func);
310
311 /*
312 * Fragment program
313 */
314 glGenProgramsARB_func(1, &FragProg);
315 assert(FragProg > 0);
316 glBindProgramARB_func(GL_FRAGMENT_PROGRAM_ARB, FragProg);
317 glProgramStringARB_func(GL_FRAGMENT_PROGRAM_ARB,
318 GL_PROGRAM_FORMAT_ASCII_ARB,
319 strlen(fragProgramText),
320 (const GLubyte *) fragProgramText);
321 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorPos);
322 if (glGetError() != GL_NO_ERROR || errorPos != -1) {
323 int l = FindLine(fragProgramText, errorPos);
324 printf("Fragment Program Error (pos=%d line=%d): %s\n", errorPos, l,
325 (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB));
326 exit(0);
327 }
328 assert(glIsProgramARB_func(FragProg));
329
330 /*
331 * Do some sanity tests
332 */
333 {
334 GLdouble v[4];
335 glProgramLocalParameter4dARB_func(GL_FRAGMENT_PROGRAM_ARB, 8,
336 10.0, 20.0, 30.0, 40.0);
337 glGetProgramLocalParameterdvARB_func(GL_FRAGMENT_PROGRAM_ARB, 8, v);
338 assert(v[0] == 10.0);
339 assert(v[1] == 20.0);
340 assert(v[2] == 30.0);
341 assert(v[3] == 40.0);
342 }
343
344 /*
345 * Vertex program
346 */
347 glGenProgramsARB_func(1, &VertProg);
348 assert(VertProg > 0);
349 glBindProgramARB_func(GL_VERTEX_PROGRAM_ARB, VertProg);
350 glProgramStringARB_func(GL_VERTEX_PROGRAM_ARB,
351 GL_PROGRAM_FORMAT_ASCII_ARB,
352 strlen(vertProgramText),
353 (const GLubyte *) vertProgramText);
354 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorPos);
355 if (glGetError() != GL_NO_ERROR || errorPos != -1) {
356 int l = FindLine(vertProgramText, errorPos);
357 printf("Vertex Program Error (pos=%d line=%d): %s\n", errorPos, l,
358 (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB));
359 exit(0);
360 }
361 assert(glIsProgramARB_func(VertProg));
362
363 /*
364 * Misc init
365 */
366 glClearColor(0.3, 0.3, 0.3, 0.0);
367 glEnable(GL_DEPTH_TEST);
368 glEnable(GL_LIGHT0);
369 glEnable(GL_LIGHTING);
370 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Diffuse);
371 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, Specular);
372 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 20.0);
373
374 #if DO_FRAGMENT_FOG
375 {
376 /* Green-ish fog color */
377 static const GLfloat fogColor[4] = {0.5, 1.0, 0.5, 0};
378 glFogfv(GL_FOG_COLOR, fogColor);
379 glFogf(GL_FOG_START, 5.0);
380 glFogf(GL_FOG_END, 25.0);
381 }
382 #endif
383
384 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
385 printf("Press p to toggle between per-pixel and per-vertex lighting\n");
386 }
387
388
389 int main( int argc, char *argv[] )
390 {
391 glutInitWindowSize( 200, 200 );
392 glutInit( &argc, argv );
393 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
394 Win = glutCreateWindow(argv[0]);
395 glutReshapeFunc( Reshape );
396 glutKeyboardFunc( Key );
397 glutSpecialFunc( SpecialKey );
398 glutDisplayFunc( Redisplay );
399 if (Anim)
400 glutIdleFunc(Idle);
401 Init();
402 glutMainLoop();
403 return 0;
404 }