Clone of mesa.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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