Clone of mesa.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

gloss.c 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* $Id: gloss.c,v 1.6 2000/12/24 22:53:54 pesco Exp $ */
  2. /*
  3. * Specular reflection demo. The specular highlight is modulated by
  4. * a sphere-mapped texture. The result is a high-gloss surface.
  5. * NOTE: you really need hardware acceleration for this.
  6. * Also note, this technique can't be implemented with multi-texture
  7. * and separate specular color interpolation because there's no way
  8. * to indicate that the second texture unit (the reflection map)
  9. * should modulate the specular color and not the base color.
  10. * A future multi-texture extension could fix that.
  11. *
  12. * Command line options:
  13. * -info print GL implementation information
  14. *
  15. *
  16. * Brian Paul October 22, 1999 This program is in the public domain.
  17. */
  18. #include <assert.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <math.h>
  22. #include <GL/glut.h>
  23. #include "readtex.c" /* I know, this is a hack. */
  24. #define SPECULAR_TEXTURE_FILE "../images/reflect.rgb"
  25. #define BASE_TEXTURE_FILE "../images/tile.rgb"
  26. /* Menu items */
  27. #define DO_SPEC_TEXTURE 1
  28. #define OBJECT 2
  29. #define ANIMATE 3
  30. #define QUIT 100
  31. static GLuint CylinderObj = 0;
  32. static GLuint TeapotObj = 0;
  33. static GLuint Object = 0;
  34. static GLboolean Animate = GL_TRUE;
  35. static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
  36. static GLfloat DXrot = 1.0, DYrot = 2.5;
  37. static GLfloat Black[4] = { 0, 0, 0, 0 };
  38. static GLfloat White[4] = { 1, 1, 1, 1 };
  39. static GLfloat Diffuse[4] = { .3, .3, 1.0, 1.0 }; /* blue */
  40. static GLfloat Shininess = 6;
  41. static GLuint BaseTexture, SpecularTexture;
  42. static GLboolean DoSpecTexture = GL_TRUE;
  43. /* performance info */
  44. static GLint T0 = 0;
  45. static GLint Frames = 0;
  46. static void Idle( void )
  47. {
  48. if (Animate) {
  49. Xrot += DXrot;
  50. Yrot += DYrot;
  51. glutPostRedisplay();
  52. }
  53. }
  54. static void Display( void )
  55. {
  56. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  57. glPushMatrix();
  58. glRotatef(Xrot, 1.0, 0.0, 0.0);
  59. glRotatef(Yrot, 0.0, 1.0, 0.0);
  60. glRotatef(Zrot, 0.0, 0.0, 1.0);
  61. /* First pass: diffuse lighting with base texture */
  62. glMaterialfv(GL_FRONT, GL_DIFFUSE, Diffuse);
  63. glMaterialfv(GL_FRONT, GL_SPECULAR, Black);
  64. glEnable(GL_TEXTURE_2D);
  65. glBindTexture(GL_TEXTURE_2D, BaseTexture);
  66. glCallList(Object);
  67. /* Second pass: specular lighting with reflection texture */
  68. glBlendFunc(GL_ONE, GL_ONE); /* add */
  69. glEnable(GL_BLEND);
  70. glDepthFunc(GL_LEQUAL);
  71. glMaterialfv(GL_FRONT, GL_DIFFUSE, Black);
  72. glMaterialfv(GL_FRONT, GL_SPECULAR, White);
  73. if (DoSpecTexture) {
  74. glBindTexture(GL_TEXTURE_2D, SpecularTexture);
  75. glEnable(GL_TEXTURE_GEN_S);
  76. glEnable(GL_TEXTURE_GEN_T);
  77. }
  78. else {
  79. glDisable(GL_TEXTURE_2D);
  80. }
  81. glCallList(Object);
  82. glDisable(GL_TEXTURE_GEN_S);
  83. glDisable(GL_TEXTURE_GEN_T);
  84. glDisable(GL_BLEND);
  85. glPopMatrix();
  86. glutSwapBuffers();
  87. if (Animate) {
  88. GLint t = glutGet(GLUT_ELAPSED_TIME);
  89. Frames++;
  90. if (t - T0 >= 5000) {
  91. GLfloat seconds = (t - T0) / 1000.0;
  92. GLfloat fps = Frames / seconds;
  93. printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
  94. T0 = t;
  95. Frames = 0;
  96. }
  97. }
  98. }
  99. static void Reshape( int width, int height )
  100. {
  101. GLfloat h = 30.0;
  102. GLfloat w = h * width / height;
  103. glViewport( 0, 0, width, height );
  104. glMatrixMode( GL_PROJECTION );
  105. glLoadIdentity();
  106. glFrustum( -w, w, -h, h, 150.0, 500.0 );
  107. glMatrixMode( GL_MODELVIEW );
  108. glLoadIdentity();
  109. glTranslatef( 0.0, 0.0, -380.0 );
  110. }
  111. static void ToggleAnimate(void)
  112. {
  113. Animate = !Animate;
  114. if (Animate) {
  115. glutIdleFunc( Idle );
  116. T0 = glutGet(GLUT_ELAPSED_TIME);
  117. Frames = 0;
  118. }
  119. else {
  120. glutIdleFunc( NULL );
  121. }
  122. }
  123. static void ModeMenu(int entry)
  124. {
  125. if (entry==ANIMATE) {
  126. ToggleAnimate();
  127. }
  128. else if (entry==DO_SPEC_TEXTURE) {
  129. DoSpecTexture = !DoSpecTexture;
  130. }
  131. else if (entry==OBJECT) {
  132. if (Object == TeapotObj)
  133. Object = CylinderObj;
  134. else
  135. Object = TeapotObj;
  136. }
  137. else if (entry==QUIT) {
  138. exit(0);
  139. }
  140. glutPostRedisplay();
  141. }
  142. static void Key( unsigned char key, int x, int y )
  143. {
  144. (void) x;
  145. (void) y;
  146. switch (key) {
  147. case 's':
  148. Shininess--;
  149. if (Shininess < 0.0)
  150. Shininess = 0.0;
  151. glMaterialf(GL_FRONT, GL_SHININESS, Shininess);
  152. printf("Shininess = %g\n", Shininess);
  153. break;
  154. case 'S':
  155. Shininess++;
  156. if (Shininess > 128.0)
  157. Shininess = 128.0;
  158. glMaterialf(GL_FRONT, GL_SHININESS, Shininess);
  159. printf("Shininess = %g\n", Shininess);
  160. break;
  161. case ' ':
  162. ToggleAnimate();
  163. break;
  164. case 27:
  165. exit(0);
  166. break;
  167. }
  168. glutPostRedisplay();
  169. }
  170. static void SpecialKey( int key, int x, int y )
  171. {
  172. float step = 3.0;
  173. (void) x;
  174. (void) y;
  175. switch (key) {
  176. case GLUT_KEY_UP:
  177. Xrot += step;
  178. break;
  179. case GLUT_KEY_DOWN:
  180. Xrot -= step;
  181. break;
  182. case GLUT_KEY_LEFT:
  183. Yrot += step;
  184. break;
  185. case GLUT_KEY_RIGHT:
  186. Yrot -= step;
  187. break;
  188. }
  189. glutPostRedisplay();
  190. }
  191. static void Init( int argc, char *argv[] )
  192. {
  193. /* Cylinder object */
  194. {
  195. static GLfloat height = 100.0;
  196. static GLfloat radius = 40.0;
  197. static GLint slices = 24; /* pie slices around Z axis */
  198. static GLint stacks = 10; /* subdivisions along length of cylinder */
  199. static GLint rings = 4; /* rings in the end disks */
  200. GLUquadricObj *q = gluNewQuadric();
  201. assert(q);
  202. gluQuadricTexture(q, GL_TRUE);
  203. CylinderObj = glGenLists(1);
  204. glNewList(CylinderObj, GL_COMPILE);
  205. glPushMatrix();
  206. glTranslatef(0.0, 0.0, -0.5 * height);
  207. glMatrixMode(GL_TEXTURE);
  208. glLoadIdentity();
  209. /*glScalef(8.0, 4.0, 2.0);*/
  210. glMatrixMode(GL_MODELVIEW);
  211. /* cylinder */
  212. gluQuadricNormals(q, GL_SMOOTH);
  213. gluQuadricTexture(q, GL_TRUE);
  214. gluCylinder(q, radius, radius, height, slices, stacks);
  215. /* end cap */
  216. glMatrixMode(GL_TEXTURE);
  217. glLoadIdentity();
  218. glScalef(3.0, 3.0, 1.0);
  219. glMatrixMode(GL_MODELVIEW);
  220. glTranslatef(0.0, 0.0, height);
  221. gluDisk(q, 0.0, radius, slices, rings);
  222. /* other end cap */
  223. glTranslatef(0.0, 0.0, -height);
  224. gluQuadricOrientation(q, GLU_INSIDE);
  225. gluDisk(q, 0.0, radius, slices, rings);
  226. glPopMatrix();
  227. glMatrixMode(GL_TEXTURE);
  228. glLoadIdentity();
  229. glMatrixMode(GL_MODELVIEW);
  230. glEndList();
  231. gluDeleteQuadric(q);
  232. }
  233. /* Teapot */
  234. {
  235. TeapotObj = glGenLists(1);
  236. glNewList(TeapotObj, GL_COMPILE);
  237. glFrontFace(GL_CW);
  238. glutSolidTeapot(40.0);
  239. glFrontFace(GL_CCW);
  240. glEndList();
  241. }
  242. /* show cylinder by default */
  243. Object = CylinderObj;
  244. /* lighting */
  245. glEnable(GL_LIGHTING);
  246. {
  247. GLfloat pos[4] = { 3, 3, 3, 1 };
  248. glLightfv(GL_LIGHT0, GL_AMBIENT, Black);
  249. glLightfv(GL_LIGHT0, GL_DIFFUSE, White);
  250. glLightfv(GL_LIGHT0, GL_SPECULAR, White);
  251. glLightfv(GL_LIGHT0, GL_POSITION, pos);
  252. glEnable(GL_LIGHT0);
  253. glMaterialfv(GL_FRONT, GL_AMBIENT, Black);
  254. glMaterialf(GL_FRONT, GL_SHININESS, Shininess);
  255. glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
  256. }
  257. /* Base texture */
  258. glGenTextures(1, &BaseTexture);
  259. glBindTexture(GL_TEXTURE_2D, BaseTexture);
  260. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  261. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  262. if (!LoadRGBMipmaps(BASE_TEXTURE_FILE, GL_RGB)) {
  263. printf("Error: couldn't load texture image file %s\n", BASE_TEXTURE_FILE);
  264. exit(1);
  265. }
  266. /* Specular texture */
  267. glGenTextures(1, &SpecularTexture);
  268. glBindTexture(GL_TEXTURE_2D, SpecularTexture);
  269. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  270. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  271. #if 1
  272. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  273. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  274. #else
  275. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_NV);
  276. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_NV);
  277. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_NV);
  278. #endif
  279. if (!LoadRGBMipmaps(SPECULAR_TEXTURE_FILE, GL_RGB)) {
  280. printf("Error: couldn't load texture image file %s\n", SPECULAR_TEXTURE_FILE);
  281. exit(1);
  282. }
  283. /* misc */
  284. glEnable(GL_CULL_FACE);
  285. glEnable(GL_TEXTURE_2D);
  286. glEnable(GL_DEPTH_TEST);
  287. glEnable(GL_NORMALIZE);
  288. if (argc > 1 && strcmp(argv[1], "-info")==0) {
  289. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  290. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  291. printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  292. printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
  293. }
  294. }
  295. int main( int argc, char *argv[] )
  296. {
  297. glutInit( &argc, argv );
  298. glutInitWindowSize( 500, 500 );
  299. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  300. glutCreateWindow(argv[0] );
  301. Init(argc, argv);
  302. glutReshapeFunc( Reshape );
  303. glutKeyboardFunc( Key );
  304. glutSpecialFunc( SpecialKey );
  305. glutDisplayFunc( Display );
  306. glutIdleFunc( Idle );
  307. glutCreateMenu(ModeMenu);
  308. glutAddMenuEntry("Toggle Highlight", DO_SPEC_TEXTURE);
  309. glutAddMenuEntry("Toggle Object", OBJECT);
  310. glutAddMenuEntry("Toggle Animate", ANIMATE);
  311. glutAddMenuEntry("Quit", QUIT);
  312. glutAttachMenu(GLUT_RIGHT_BUTTON);
  313. glutMainLoop();
  314. return 0;
  315. }