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.

spectex.c 6.9KB

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