Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

arbfplight.c 12KB

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