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. T0 = t;
  79. Frames = 0;
  80. }
  81. }
  82. }
  83. static void Idle(void)
  84. {
  85. LightPos[0] += Delta;
  86. if (LightPos[0] > 25.0)
  87. Delta = -1.0;
  88. else if (LightPos[0] <- 25.0)
  89. Delta = 1.0;
  90. glutPostRedisplay();
  91. }
  92. static void Reshape( int width, int height )
  93. {
  94. glViewport( 0, 0, width, height );
  95. glMatrixMode( GL_PROJECTION );
  96. glLoadIdentity();
  97. glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
  98. glMatrixMode( GL_MODELVIEW );
  99. glLoadIdentity();
  100. glTranslatef( 0.0, 0.0, -15.0 );
  101. }
  102. static void Key( unsigned char key, int x, int y )
  103. {
  104. (void) x;
  105. (void) y;
  106. switch (key) {
  107. case ' ':
  108. case 'a':
  109. Anim = !Anim;
  110. if (Anim)
  111. glutIdleFunc(Idle);
  112. else
  113. glutIdleFunc(NULL);
  114. break;
  115. case 'x':
  116. LightPos[0] -= 1.0;
  117. break;
  118. case 'X':
  119. LightPos[0] += 1.0;
  120. break;
  121. case 'w':
  122. Wire = !Wire;
  123. if (Wire)
  124. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  125. else
  126. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  127. break;
  128. case 'p':
  129. PixelLight = !PixelLight;
  130. if (PixelLight) {
  131. printf("Per-pixel lighting\n");
  132. }
  133. else {
  134. printf("Conventional lighting\n");
  135. }
  136. break;
  137. case 27:
  138. glDeleteProgramsARB_func(1, &VertProg);
  139. glDeleteProgramsARB_func(1, &FragProg);
  140. glutDestroyWindow(Win);
  141. exit(0);
  142. break;
  143. }
  144. glutPostRedisplay();
  145. }
  146. static void SpecialKey( int key, int x, int y )
  147. {
  148. const GLfloat step = 3.0;
  149. (void) x;
  150. (void) y;
  151. switch (key) {
  152. case GLUT_KEY_UP:
  153. Xrot -= step;
  154. break;
  155. case GLUT_KEY_DOWN:
  156. Xrot += step;
  157. break;
  158. case GLUT_KEY_LEFT:
  159. Yrot -= step;
  160. break;
  161. case GLUT_KEY_RIGHT:
  162. Yrot += step;
  163. break;
  164. }
  165. glutPostRedisplay();
  166. }
  167. /* A helper for finding errors in program strings */
  168. static int FindLine( const char *program, int position )
  169. {
  170. int i, line = 1;
  171. for (i = 0; i < position; i++) {
  172. if (program[i] == '\n')
  173. line++;
  174. }
  175. return line;
  176. }
  177. static void Init( void )
  178. {
  179. GLint errorPos;
  180. /* Yes, this could be expressed more efficiently */
  181. static const char *fragProgramText =
  182. "!!ARBfp1.0\n"
  183. #if DO_FRAGMENT_FOG
  184. "OPTION ARB_fog_linear; \n"
  185. #endif
  186. "PARAM Diffuse = state.material.diffuse; \n"
  187. "PARAM Specular = state.material.specular; \n"
  188. "PARAM LightPos = program.local[3]; \n"
  189. "TEMP normal, len; \n"
  190. "TEMP dotProd, specAtten; \n"
  191. "TEMP diffuseColor, specularColor; \n"
  192. "# Compute normalized normal \n"
  193. "DP3 len.x, fragment.texcoord[0], fragment.texcoord[0]; \n"
  194. "RSQ len.y, len.x; \n"
  195. "MUL normal.xyz, fragment.texcoord[0], len.y; \n"
  196. "# Compute dot product of light direction and normal vector\n"
  197. "DP3_SAT dotProd.x, LightPos, normal; # limited to [0,1]\n"
  198. "MUL diffuseColor.xyz, Diffuse, dotProd.x; # diffuse attenuation\n"
  199. "POW specAtten.x, dotProd.x, {20.0}.x; # specular exponent\n"
  200. "MUL specularColor.xyz, Specular, specAtten.x; # specular attenuation\n"
  201. "MOV result.color.w, Diffuse; \n"
  202. #if DO_FRAGMENT_FOG
  203. "# need to clamp color to [0,1] before fogging \n"
  204. "ADD_SAT result.color.xyz, diffuseColor, specularColor; # add colors\n"
  205. #else
  206. "# clamping will be done after program's finished \n"
  207. "ADD result.color.xyz, diffuseColor, specularColor; # add colors\n"
  208. #endif
  209. "END \n"
  210. ;
  211. static const char *vertProgramText =
  212. "!!ARBvp1.0\n"
  213. "ATTRIB pos = vertex.position; \n"
  214. "ATTRIB norm = vertex.normal; \n"
  215. "PARAM modelview[4] = { state.matrix.modelview }; \n"
  216. "PARAM modelviewProj[4] = { state.matrix.mvp }; \n"
  217. "PARAM invModelview[4] = { state.matrix.modelview.invtrans }; \n"
  218. "# typical modelview/projection transform \n"
  219. "DP4 result.position.x, pos, modelviewProj[0]; \n"
  220. "DP4 result.position.y, pos, modelviewProj[1]; \n"
  221. "DP4 result.position.z, pos, modelviewProj[2]; \n"
  222. "DP4 result.position.w, pos, modelviewProj[3]; \n"
  223. "# transform normal by inv transpose of modelview, put in tex0 \n"
  224. "DP3 result.texcoord[0].x, norm, invModelview[0]; \n"
  225. "DP3 result.texcoord[0].y, norm, invModelview[1]; \n"
  226. "DP3 result.texcoord[0].z, norm, invModelview[2]; \n"
  227. "DP3 result.texcoord[0].w, norm, invModelview[3]; \n"
  228. #if DO_FRAGMENT_FOG
  229. "# compute fog coordinate = vertex eye-space Z coord (negated)\n"
  230. "DP4 result.fogcoord, -pos, modelview[2]; \n"
  231. #endif
  232. "END\n";
  233. ;
  234. if (!glutExtensionSupported("GL_ARB_vertex_program")) {
  235. printf("Sorry, this demo requires GL_ARB_vertex_program\n");
  236. exit(1);
  237. }
  238. if (!glutExtensionSupported("GL_ARB_fragment_program")) {
  239. printf("Sorry, this demo requires GL_ARB_fragment_program\n");
  240. exit(1);
  241. }
  242. /*
  243. * Get extension function pointers.
  244. */
  245. glProgramLocalParameter4fvARB_func = (PFNGLPROGRAMLOCALPARAMETER4FVARBPROC) glutGetProcAddress("glProgramLocalParameter4fvARB");
  246. assert(glProgramLocalParameter4fvARB_func);
  247. glProgramLocalParameter4dARB_func = (PFNGLPROGRAMLOCALPARAMETER4DARBPROC) glutGetProcAddress("glProgramLocalParameter4dARB");
  248. assert(glProgramLocalParameter4dARB_func);
  249. glGetProgramLocalParameterdvARB_func = (PFNGLGETPROGRAMLOCALPARAMETERDVARBPROC) glutGetProcAddress("glGetProgramLocalParameterdvARB");
  250. assert(glGetProgramLocalParameterdvARB_func);
  251. glGenProgramsARB_func = (PFNGLGENPROGRAMSARBPROC) glutGetProcAddress("glGenProgramsARB");
  252. assert(glGenProgramsARB_func);
  253. glProgramStringARB_func = (PFNGLPROGRAMSTRINGARBPROC) glutGetProcAddress("glProgramStringARB");
  254. assert(glProgramStringARB_func);
  255. glBindProgramARB_func = (PFNGLBINDPROGRAMARBPROC) glutGetProcAddress("glBindProgramARB");
  256. assert(glBindProgramARB_func);
  257. glIsProgramARB_func = (PFNGLISPROGRAMARBPROC) glutGetProcAddress("glIsProgramARB");
  258. assert(glIsProgramARB_func);
  259. glDeleteProgramsARB_func = (PFNGLDELETEPROGRAMSARBPROC) glutGetProcAddress("glDeleteProgramsARB");
  260. assert(glDeleteProgramsARB_func);
  261. /*
  262. * Fragment program
  263. */
  264. glGenProgramsARB_func(1, &FragProg);
  265. assert(FragProg > 0);
  266. glBindProgramARB_func(GL_FRAGMENT_PROGRAM_ARB, FragProg);
  267. glProgramStringARB_func(GL_FRAGMENT_PROGRAM_ARB,
  268. GL_PROGRAM_FORMAT_ASCII_ARB,
  269. strlen(fragProgramText),
  270. (const GLubyte *) fragProgramText);
  271. glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorPos);
  272. if (glGetError() != GL_NO_ERROR || errorPos != -1) {
  273. int l = FindLine(fragProgramText, errorPos);
  274. printf("Fragment Program Error (pos=%d line=%d): %s\n", errorPos, l,
  275. (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB));
  276. exit(0);
  277. }
  278. assert(glIsProgramARB_func(FragProg));
  279. /*
  280. * Do some sanity tests
  281. */
  282. {
  283. GLdouble v[4];
  284. glProgramLocalParameter4dARB_func(GL_FRAGMENT_PROGRAM_ARB, 8,
  285. 10.0, 20.0, 30.0, 40.0);
  286. glGetProgramLocalParameterdvARB_func(GL_FRAGMENT_PROGRAM_ARB, 8, v);
  287. assert(v[0] == 10.0);
  288. assert(v[1] == 20.0);
  289. assert(v[2] == 30.0);
  290. assert(v[3] == 40.0);
  291. }
  292. /*
  293. * Vertex program
  294. */
  295. glGenProgramsARB_func(1, &VertProg);
  296. assert(VertProg > 0);
  297. glBindProgramARB_func(GL_VERTEX_PROGRAM_ARB, VertProg);
  298. glProgramStringARB_func(GL_VERTEX_PROGRAM_ARB,
  299. GL_PROGRAM_FORMAT_ASCII_ARB,
  300. strlen(vertProgramText),
  301. (const GLubyte *) vertProgramText);
  302. glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorPos);
  303. if (glGetError() != GL_NO_ERROR || errorPos != -1) {
  304. int l = FindLine(vertProgramText, errorPos);
  305. printf("Vertex Program Error (pos=%d line=%d): %s\n", errorPos, l,
  306. (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB));
  307. exit(0);
  308. }
  309. assert(glIsProgramARB_func(VertProg));
  310. /*
  311. * Misc init
  312. */
  313. glClearColor(0.3, 0.3, 0.3, 0.0);
  314. glEnable(GL_DEPTH_TEST);
  315. glEnable(GL_LIGHT0);
  316. glEnable(GL_LIGHTING);
  317. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Diffuse);
  318. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, Specular);
  319. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 20.0);
  320. #if DO_FRAGMENT_FOG
  321. {
  322. /* Green-ish fog color */
  323. static const GLfloat fogColor[4] = {0.5, 1.0, 0.5, 0};
  324. glFogfv(GL_FOG_COLOR, fogColor);
  325. glFogf(GL_FOG_START, 5.0);
  326. glFogf(GL_FOG_END, 25.0);
  327. }
  328. #endif
  329. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  330. printf("Press p to toggle between per-pixel and per-vertex lighting\n");
  331. }
  332. int main( int argc, char *argv[] )
  333. {
  334. glutInit( &argc, argv );
  335. glutInitWindowPosition( 0, 0 );
  336. glutInitWindowSize( 200, 200 );
  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. }