Source code of Windows XP (NT5)
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.

315 lines
8.9 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <GL/gl.h>
  7. #include "glaux.h"
  8. #include "miscflt.h"
  9. #include "miscutil.h"
  10. #ifndef M_PI
  11. #define M_PI 3.1415926539
  12. #endif
  13. #define TWO_PI ((float)(2*M_PI))
  14. typedef struct lightRec {
  15. float amb[4];
  16. float diff[4];
  17. float spec[4];
  18. float pos[4];
  19. float spotDir[3];
  20. float spotExp;
  21. float spotCutoff;
  22. float atten[3];
  23. float trans[3];
  24. float rot[3];
  25. float swing[3];
  26. float arc[3];
  27. float arcIncr[3];
  28. } Light;
  29. static int useSAME_AMB_SPEC = 1;
  30. static float modelAmb[4] = { POINT_TWO, POINT_TWO, POINT_TWO, ONE };
  31. static float matAmb[4] = { POINT_TWO, POINT_TWO, POINT_TWO, ONE };
  32. static float matDiff[4] = { POINT_EIGHT, POINT_EIGHT, POINT_EIGHT, ONE };
  33. static float matSpec[4] = { POINT_FOUR, POINT_FOUR, POINT_FOUR, ONE };
  34. static float matEmission[4] = { ZERO, ZERO, ZERO, ONE };
  35. static char *geometry = NULL;
  36. #define NUM_LIGHTS 3
  37. static Light spots[] = {
  38. {
  39. { POINT_TWO , ZERO, ZERO, ONE }, /* ambient */
  40. { POINT_EIGHT, ZERO, ZERO, ONE }, /* diffuse */
  41. { POINT_FOUR , ZERO, ZERO, ONE }, /* specular */
  42. { ZERO, ZERO , ZERO, ONE }, /* position */
  43. { ZERO, -ONE , ZERO }, /* direction */
  44. { 20.0f }, { 60.0f }, /* exponent, cutoff */
  45. { ONE, ZERO, ZERO }, /* attenuation */
  46. { ZERO, 1.25f, ZERO }, /* translation */
  47. { ZERO, ZERO, ZERO }, /* rotation */
  48. { 20.0f, ZERO, 40.0f }, /* swing */
  49. { ZERO, ZERO, ZERO }, /* arc */
  50. { (float)(TWO_PI/70.0), ZERO, (float)(TWO_PI/140.0) } /* arc increment */
  51. },
  52. {
  53. { ZERO, POINT_TWO, ZERO, ONE }, /* ambient */
  54. { ZERO, POINT_EIGHT, ZERO, ONE }, /* diffuse */
  55. { ZERO, POINT_FOUR, ZERO, ONE }, /* specular */
  56. { ZERO, ZERO, ZERO, ONE }, /* position */
  57. { ZERO, -ONE, ZERO }, /* direction */
  58. { 20.0f }, { 60.0f }, /* exponent, cutoff */
  59. { ONE, ZERO, ZERO }, /* attenuation */
  60. { ZERO, 1.25f, ZERO }, /* translation */
  61. { ZERO, ZERO, ZERO }, /* rotation */
  62. { 20.0f, ZERO, 40.0f }, /* swing */
  63. { ZERO, ZERO, ZERO }, /* arc */
  64. { (float)(TWO_PI/120.0), ZERO, (float)(TWO_PI/60.0) } /* arc increment */
  65. },
  66. {
  67. { ZERO, ZERO, POINT_TWO, ONE }, /* ambient */
  68. { ZERO, ZERO, POINT_EIGHT, ONE }, /* diffuse */
  69. { ZERO, ZERO, POINT_FOUR, ONE }, /* specular */
  70. { ZERO, ZERO, ZERO, ONE }, /* position */
  71. { ZERO, -ONE, ZERO }, /* direction */
  72. { 20.0f }, { 60.0f }, /* exponent, cutoff */
  73. { ONE, ZERO, ZERO }, /* attenuation */
  74. { ZERO, 1.25f, ZERO }, /* translation */
  75. { ZERO, ZERO, ZERO }, /* rotation */
  76. { 20.0f, ZERO, 40.0f }, /* swing */
  77. { ZERO, ZERO, ZERO }, /* arc */
  78. { (float)(TWO_PI/50.0), ZERO, (float)(TWO_PI/100.0) } /* arc increment */
  79. }
  80. };
  81. void Display( void );
  82. void MyReshape( GLsizei Width, GLsizei Height );
  83. static void
  84. usage(int argc, char **argv)
  85. {
  86. printf("\n");
  87. printf("usage: %s [options]\n", argv[0]);
  88. printf("\n");
  89. printf(" Options:\n");
  90. printf(" -geometry Specify size and position WxH+X+Y\n");
  91. printf(" -lm Toggle lighting(SPECULAR and AMBIENT are/not same\n");
  92. printf("\n");
  93. exit(EXIT_FAILURE);
  94. }
  95. static void
  96. initLights(void)
  97. {
  98. int k;
  99. for (k=0; k<NUM_LIGHTS; ++k) {
  100. int lt = GL_LIGHT0+k;
  101. Light *light = &spots[k];
  102. glEnable(lt);
  103. glLightfv(lt, GL_AMBIENT, light->amb);
  104. glLightfv(lt, GL_DIFFUSE, light->diff);
  105. if (useSAME_AMB_SPEC)
  106. glLightfv(lt, GL_SPECULAR, light->amb);
  107. else
  108. glLightfv(lt, GL_SPECULAR, light->spec);
  109. glLightf(lt, GL_SPOT_EXPONENT, light->spotExp);
  110. glLightf(lt, GL_SPOT_CUTOFF, light->spotCutoff);
  111. glLightf(lt, GL_CONSTANT_ATTENUATION, light->atten[0]);
  112. glLightf(lt, GL_LINEAR_ATTENUATION, light->atten[1]);
  113. glLightf(lt, GL_QUADRATIC_ATTENUATION, light->atten[2]);
  114. }
  115. }
  116. static void
  117. aimLights(void)
  118. {
  119. int k;
  120. for (k=0; k<NUM_LIGHTS; ++k) {
  121. Light *light = &spots[k];
  122. light->rot[0] = light->swing[0] * SINF(light->arc[0]);
  123. light->arc[0] += light->arcIncr[0];
  124. if (light->arc[0] > TWO_PI) light->arc[0] -= TWO_PI;
  125. light->rot[1] = light->swing[1] * SINF(light->arc[1]);
  126. light->arc[1] += light->arcIncr[1];
  127. if (light->arc[1] > TWO_PI) light->arc[1] -= TWO_PI;
  128. light->rot[2] = light->swing[2] * SINF(light->arc[2]);
  129. light->arc[2] += light->arcIncr[2];
  130. if (light->arc[2] > TWO_PI) light->arc[2] -= TWO_PI;
  131. }
  132. }
  133. static void
  134. setLights(void)
  135. {
  136. int k;
  137. for (k=0; k<NUM_LIGHTS; ++k) {
  138. int lt = GL_LIGHT0+k;
  139. Light *light = &spots[k];
  140. glPushMatrix();
  141. glTranslatef(light->trans[0], light->trans[1], light->trans[2]);
  142. glRotatef(light->rot[0], ONE, ZERO, ZERO);
  143. glRotatef(light->rot[1], ZERO, ONE, ZERO);
  144. glRotatef(light->rot[2], ZERO, ZERO, ONE);
  145. glLightfv(lt, GL_POSITION, light->pos);
  146. glLightfv(lt, GL_SPOT_DIRECTION, light->spotDir);
  147. glPopMatrix();
  148. }
  149. }
  150. static void
  151. drawLights(void)
  152. {
  153. int k;
  154. glDisable(GL_LIGHTING);
  155. for (k=0; k<NUM_LIGHTS; ++k) {
  156. Light *light = &spots[k];
  157. glColor4fv(light->diff);
  158. glPushMatrix();
  159. glTranslatef(light->trans[0], light->trans[1], light->trans[2]);
  160. glRotatef(light->rot[0], ONE, ZERO, ZERO);
  161. glRotatef(light->rot[1], ZERO, ONE, ZERO);
  162. glRotatef(light->rot[2], ZERO, ZERO, ONE);
  163. glBegin(GL_LINES);
  164. glVertex3f(light->pos[0], light->pos[1], light->pos[2]);
  165. glVertex3f(light->spotDir[0], light->spotDir[1], light->spotDir[2]);
  166. glEnd();
  167. glPopMatrix();
  168. }
  169. glEnable(GL_LIGHTING);
  170. }
  171. static void
  172. drawPlane(int w, int h)
  173. {
  174. int i, j;
  175. float dw = ONE/w;
  176. float dh = ONE/h;
  177. glNormal3f(ZERO, ZERO, ONE);
  178. for (j=0; j<h; ++j) {
  179. glBegin(GL_TRIANGLE_STRIP);
  180. for (i=0; i<=w; ++i) {
  181. glVertex2f(dw * i, dh * (j+1));
  182. glVertex2f(dw * i, dh * j);
  183. }
  184. glEnd();
  185. }
  186. }
  187. int
  188. main(int argc, char **argv)
  189. {
  190. int width = 300, height = 300;
  191. int left = 50, top = 50;
  192. int i;
  193. /* process commmand line args */
  194. for (i = 1; i < argc; ++i)
  195. {
  196. if (!strcmp("-geometry", argv[i]))
  197. {
  198. i++;
  199. if ( !miscParseGeometry(argv[i],
  200. &left,
  201. &top,
  202. &width,
  203. &height )
  204. )
  205. {
  206. usage(argc, argv);
  207. }
  208. }
  209. else if (!strcmp("-lm", argv[i]))
  210. {
  211. useSAME_AMB_SPEC = !useSAME_AMB_SPEC;
  212. }
  213. else
  214. {
  215. usage(argc, argv);
  216. }
  217. }
  218. auxInitPosition( left, top, width, height );
  219. auxInitDisplayMode( AUX_RGBA | AUX_DOUBLE );
  220. auxInitWindow( "spotlight swing" );
  221. auxIdleFunc( Display );
  222. auxReshapeFunc( MyReshape );
  223. glMatrixMode(GL_PROJECTION);
  224. glFrustum(-1.0, 1.0, -1.0, 1.0, 2.0, 6.0);
  225. glMatrixMode(GL_MODELVIEW);
  226. glTranslatef(ZERO, ZERO, -THREE);
  227. glRotatef(45.0f, ONE, ZERO, ZERO);
  228. glEnable(GL_LIGHTING);
  229. glEnable(GL_NORMALIZE);
  230. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, modelAmb);
  231. glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, (float)GL_TRUE);
  232. glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, (float)GL_FALSE);
  233. glMaterialfv(GL_FRONT, GL_AMBIENT, matAmb);
  234. glMaterialfv(GL_FRONT, GL_DIFFUSE, matDiff);
  235. glMaterialfv(GL_FRONT, GL_SPECULAR, matSpec);
  236. glMaterialfv(GL_FRONT, GL_EMISSION, matEmission);
  237. glMaterialf(GL_FRONT, GL_SHININESS, TEN);
  238. initLights();
  239. auxMainLoop( Display );
  240. return(0);
  241. }
  242. void
  243. Display( void )
  244. {
  245. static float spin = ZERO;
  246. glClear(GL_COLOR_BUFFER_BIT);
  247. glPushMatrix();
  248. glRotatef(spin += 0.5f, ZERO, ONE, ZERO);
  249. if (spin > 360.0f) spin -= 360.0f;
  250. aimLights();
  251. setLights();
  252. glPushMatrix();
  253. glRotatef(-NINETY, ONE, ZERO, ZERO);
  254. glScalef(1.9f, 1.9f, ONE);
  255. glTranslatef(-HALF, -HALF, ZERO);
  256. drawPlane(16, 16);
  257. glPopMatrix();
  258. drawLights();
  259. glPopMatrix();
  260. auxSwapBuffers();
  261. }
  262. void
  263. MyReshape( GLsizei Width, GLsizei Height )
  264. {
  265. glViewport(0, 0, Width, Height);
  266. }