Clone of mesa.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

spectex.c 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * GLUT demonstration of texturing with specular highlights.
  3. *
  4. * When drawing a lit, textured surface one usually wants the specular
  5. * highlight to override the texture colors. However, OpenGL applies
  6. * texturing after lighting so the specular highlight is modulated by
  7. * the texture.
  8. *
  9. * The solution here shown here is a two-pass algorithm:
  10. * 1. Draw the textured surface without specular lighting.
  11. * 2. Enable blending to add the next pass:
  12. * 3. Redraw the surface with a matte white material and only the
  13. * specular components of light sources enabled.
  14. *
  15. * Brian Paul February 1997
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <math.h>
  20. #include <GL/glut.h>
  21. static GLUquadricObj *Quadric;
  22. static GLuint Sphere;
  23. static GLfloat LightPos[4] = {10.0, 10.0, 10.0, 1.0};
  24. static GLfloat Delta = 1.0;
  25. static GLint Mode = 0;
  26. /*static GLfloat Blue[4] = {0.0, 0.0, 1.0, 1.0};*/
  27. /*static GLfloat Gray[4] = {0.5, 0.5, 0.5, 1.0};*/
  28. static GLfloat Black[4] = {0.0, 0.0, 0.0, 1.0};
  29. static GLfloat White[4] = {1.0, 1.0, 1.0, 1.0};
  30. static void Idle( void )
  31. {
  32. LightPos[0] += Delta;
  33. if (LightPos[0]>15.0)
  34. Delta = -1.0;
  35. else if (LightPos[0]<-15.0)
  36. Delta = 1.0;
  37. glutPostRedisplay();
  38. }
  39. static void Display( void )
  40. {
  41. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  42. glLightfv(GL_LIGHT0, GL_POSITION, LightPos);
  43. glPushMatrix();
  44. glRotatef(90.0, 1.0, 0.0, 0.0);
  45. if (Mode==0) {
  46. /* Typical method: diffuse + specular + texture */
  47. glEnable(GL_TEXTURE_2D);
  48. glLightfv(GL_LIGHT0, GL_DIFFUSE, White); /* enable diffuse */
  49. glLightfv(GL_LIGHT0, GL_SPECULAR, White); /* enable specular */
  50. #ifdef GL_VERSION_1_2
  51. glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SINGLE_COLOR);
  52. #endif
  53. glCallList(Sphere);
  54. }
  55. else if (Mode==1) {
  56. /* just specular highlight */
  57. glDisable(GL_TEXTURE_2D);
  58. glLightfv(GL_LIGHT0, GL_DIFFUSE, Black); /* disable diffuse */
  59. glLightfv(GL_LIGHT0, GL_SPECULAR, White); /* enable specular */
  60. #ifdef GL_VERSION_1_2
  61. glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SINGLE_COLOR);
  62. #endif
  63. glCallList(Sphere);
  64. }
  65. else if (Mode==2) {
  66. /* diffuse textured */
  67. glEnable(GL_TEXTURE_2D);
  68. glLightfv(GL_LIGHT0, GL_DIFFUSE, White); /* enable diffuse */
  69. glLightfv(GL_LIGHT0, GL_SPECULAR, Black); /* disable specular */
  70. #ifdef GL_VERSION_1_2
  71. glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SINGLE_COLOR);
  72. #endif
  73. glCallList(Sphere);
  74. }
  75. else if (Mode==3) {
  76. /* 2-pass: diffuse textured then add specular highlight*/
  77. glEnable(GL_TEXTURE_2D);
  78. glLightfv(GL_LIGHT0, GL_DIFFUSE, White); /* enable diffuse */
  79. glLightfv(GL_LIGHT0, GL_SPECULAR, Black); /* disable specular */
  80. #ifdef GL_VERSION_1_2
  81. glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SINGLE_COLOR);
  82. #endif
  83. glCallList(Sphere);
  84. /* specular highlight */
  85. glDepthFunc(GL_EQUAL); /* redraw same pixels */
  86. glDisable(GL_TEXTURE_2D);
  87. glEnable(GL_BLEND); /* add */
  88. glLightfv(GL_LIGHT0, GL_DIFFUSE, Black); /* disable diffuse */
  89. glLightfv(GL_LIGHT0, GL_SPECULAR, White); /* enable specular */
  90. glCallList(Sphere);
  91. glDepthFunc(GL_LESS);
  92. glDisable(GL_BLEND);
  93. }
  94. else if (Mode==4) {
  95. /* OpenGL 1.2's separate diffuse and specular color */
  96. glEnable(GL_TEXTURE_2D);
  97. glLightfv(GL_LIGHT0, GL_DIFFUSE, White); /* enable diffuse */
  98. glLightfv(GL_LIGHT0, GL_SPECULAR, White); /* enable specular */
  99. #ifdef GL_VERSION_1_2
  100. glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
  101. #endif
  102. glCallList(Sphere);
  103. }
  104. glPopMatrix();
  105. glutSwapBuffers();
  106. }
  107. static void Reshape( int width, int height )
  108. {
  109. glViewport( 0, 0, width, height );
  110. glMatrixMode( GL_PROJECTION );
  111. glLoadIdentity();
  112. glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
  113. glMatrixMode( GL_MODELVIEW );
  114. glLoadIdentity();
  115. glTranslatef( 0.0, 0.0, -12.0 );
  116. }
  117. static void Key( unsigned char key, int x, int y )
  118. {
  119. (void) x;
  120. (void) y;
  121. switch (key) {
  122. case 27:
  123. exit(0);
  124. break;
  125. }
  126. glutPostRedisplay();
  127. }
  128. static void SpecialKey( int key, int x, int y )
  129. {
  130. (void) x;
  131. (void) y;
  132. switch (key) {
  133. case GLUT_KEY_UP:
  134. break;
  135. case GLUT_KEY_DOWN:
  136. break;
  137. }
  138. glutPostRedisplay();
  139. }
  140. static void Init( void )
  141. {
  142. int i, j;
  143. GLubyte texImage[64][64][3];
  144. glEnable(GL_LIGHTING);
  145. glEnable(GL_LIGHT0);
  146. glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 0);
  147. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, Black);
  148. glMaterialfv(GL_FRONT, GL_DIFFUSE, White);
  149. glMaterialfv(GL_FRONT, GL_SPECULAR, White);
  150. glMaterialf(GL_FRONT, GL_SHININESS, 20.0);
  151. /* Actually, these are set again later */
  152. glLightfv(GL_LIGHT0, GL_DIFFUSE, White);
  153. glLightfv(GL_LIGHT0, GL_SPECULAR, White);
  154. Quadric = gluNewQuadric();
  155. gluQuadricTexture( Quadric, GL_TRUE );
  156. Sphere= glGenLists(1);
  157. glNewList( Sphere, GL_COMPILE );
  158. gluSphere( Quadric, 1.0, 24, 24 );
  159. glEndList();
  160. glEnable(GL_DEPTH_TEST);
  161. glEnable(GL_CULL_FACE);
  162. for (i=0;i<64;i++) {
  163. for (j=0;j<64;j++) {
  164. int k = ((i>>3)&1) ^ ((j>>3)&1);
  165. texImage[i][j][0] = 255*k;
  166. texImage[i][j][1] = 255*(1-k);
  167. texImage[i][j][2] = 0;
  168. }
  169. }
  170. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  171. glTexImage2D( GL_TEXTURE_2D,
  172. 0,
  173. 3,
  174. 64, 64,
  175. 0,
  176. GL_RGB, GL_UNSIGNED_BYTE,
  177. texImage );
  178. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  179. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  180. glEnable(GL_TEXTURE_2D);
  181. glBlendFunc(GL_ONE, GL_ONE);
  182. }
  183. static void ModeMenu(int entry)
  184. {
  185. if (entry==99)
  186. exit(0);
  187. Mode = entry;
  188. }
  189. int main( int argc, char *argv[] )
  190. {
  191. glutInit( &argc, argv );
  192. glutInitWindowPosition( 0, 0 );
  193. glutInitWindowSize( 300, 300 );
  194. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  195. glutCreateWindow( "spectex" );
  196. Init();
  197. glutReshapeFunc( Reshape );
  198. glutKeyboardFunc( Key );
  199. glutSpecialFunc( SpecialKey );
  200. glutDisplayFunc( Display );
  201. glutIdleFunc( Idle );
  202. glutCreateMenu( ModeMenu );
  203. glutAddMenuEntry("1-pass lighting + texturing", 0);
  204. glutAddMenuEntry("specular lighting", 1);
  205. glutAddMenuEntry("diffuse lighting + texturing", 2);
  206. glutAddMenuEntry("2-pass lighting + texturing", 3);
  207. #ifdef GL_VERSION_1_2
  208. glutAddMenuEntry("OpenGL 1.2 separate specular", 4);
  209. #endif
  210. glutAddMenuEntry("Quit", 99);
  211. glutAttachMenu(GLUT_RIGHT_BUTTON);
  212. glutMainLoop();
  213. return 0;
  214. }