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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. /* Copyright (c) Mark J. Kilgard, 1994, 1997. */
  2. /* This program is freely distributable without licensing fees
  3. and is provided without guarantee or warrantee expressed or
  4. implied. This program is -not- in the public domain. */
  5. /* Example for PC game developers to show how to *combine* texturing,
  6. reflections, and projected shadows all in real-time with OpenGL.
  7. Robust reflections use stenciling. Robust projected shadows
  8. use both stenciling and polygon offset. PC game programmers
  9. should realize that neither stenciling nor polygon offset are
  10. supported by Direct3D, so these real-time rendering algorithms
  11. are only really viable with OpenGL.
  12. The program has modes for disabling the stenciling and polygon
  13. offset uses. It is worth running this example with these features
  14. toggled off so you can see the sort of artifacts that result.
  15. Notice that the floor texturing, reflections, and shadowing
  16. all co-exist properly. */
  17. /* When you run this program: Left mouse button controls the
  18. view. Middle mouse button controls light position (left &
  19. right rotates light around dino; up & down moves light
  20. position up and down). Right mouse button pops up menu. */
  21. /* Check out the comments in the "redraw" routine to see how the
  22. reflection blending and surface stenciling is done. You can
  23. also see in "redraw" how the projected shadows are rendered,
  24. including the use of stenciling and polygon offset. */
  25. /* This program is derived from glutdino.c */
  26. /* Compile: cc -o dinoshade dinoshade.c -lglut -lGLU -lGL -lXmu -lXext -lX11 -lm */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <math.h> /* for cos(), sin(), and sqrt() */
  31. #include <GL/glut.h> /* OpenGL Utility Toolkit header */
  32. /* Some <math.h> files do not define M_PI... */
  33. #ifndef M_PI
  34. #define M_PI 3.14159265358979323846
  35. #endif
  36. /* Variable controlling various rendering modes. */
  37. static int stencilReflection = 1, stencilShadow = 1, offsetShadow = 1;
  38. static int renderShadow = 1, renderDinosaur = 1, renderReflection = 1;
  39. static int linearFiltering = 0, useMipmaps = 0, useTexture = 1;
  40. static int reportSpeed = 0;
  41. static int animation = 1;
  42. static GLboolean lightSwitch = GL_TRUE;
  43. static int directionalLight = 1;
  44. static int forceExtension = 0;
  45. /* Time varying or user-controled variables. */
  46. static float jump = 0.0;
  47. static float lightAngle = 0.0, lightHeight = 20;
  48. GLfloat angle = -150; /* in degrees */
  49. GLfloat angle2 = 30; /* in degrees */
  50. int moving, startx, starty;
  51. int lightMoving = 0, lightStartX, lightStartY;
  52. enum {
  53. MISSING, EXTENSION, ONE_DOT_ONE
  54. };
  55. int polygonOffsetVersion;
  56. static GLdouble bodyWidth = 3.0;
  57. /* *INDENT-OFF* */
  58. static GLfloat body[][2] = { {0, 3}, {1, 1}, {5, 1}, {8, 4}, {10, 4}, {11, 5},
  59. {11, 11.5}, {13, 12}, {13, 13}, {10, 13.5}, {13, 14}, {13, 15}, {11, 16},
  60. {8, 16}, {7, 15}, {7, 13}, {8, 12}, {7, 11}, {6, 6}, {4, 3}, {3, 2},
  61. {1, 2} };
  62. static GLfloat arm[][2] = { {8, 10}, {9, 9}, {10, 9}, {13, 8}, {14, 9}, {16, 9},
  63. {15, 9.5}, {16, 10}, {15, 10}, {15.5, 11}, {14.5, 10}, {14, 11}, {14, 10},
  64. {13, 9}, {11, 11}, {9, 11} };
  65. static GLfloat leg[][2] = { {8, 6}, {8, 4}, {9, 3}, {9, 2}, {8, 1}, {8, 0.5}, {9, 0},
  66. {12, 0}, {10, 1}, {10, 2}, {12, 4}, {11, 6}, {10, 7}, {9, 7} };
  67. static GLfloat eye[][2] = { {8.75, 15}, {9, 14.7}, {9.6, 14.7}, {10.1, 15},
  68. {9.6, 15.25}, {9, 15.25} };
  69. static GLfloat lightPosition[4];
  70. static GLfloat lightColor[] = {0.8, 1.0, 0.8, 1.0}; /* green-tinted */
  71. static GLfloat skinColor[] = {0.1, 1.0, 0.1, 1.0}, eyeColor[] = {1.0, 0.2, 0.2, 1.0};
  72. /* *INDENT-ON* */
  73. /* Nice floor texture tiling pattern. */
  74. static char *circles[] = {
  75. "....xxxx........",
  76. "..xxxxxxxx......",
  77. ".xxxxxxxxxx.....",
  78. ".xxx....xxx.....",
  79. "xxx......xxx....",
  80. "xxx......xxx....",
  81. "xxx......xxx....",
  82. "xxx......xxx....",
  83. ".xxx....xxx.....",
  84. ".xxxxxxxxxx.....",
  85. "..xxxxxxxx......",
  86. "....xxxx........",
  87. "................",
  88. "................",
  89. "................",
  90. "................",
  91. };
  92. static void
  93. makeFloorTexture(void)
  94. {
  95. GLubyte floorTexture[16][16][3];
  96. GLubyte *loc;
  97. int s, t;
  98. /* Setup RGB image for the texture. */
  99. loc = (GLubyte*) floorTexture;
  100. for (t = 0; t < 16; t++) {
  101. for (s = 0; s < 16; s++) {
  102. if (circles[t][s] == 'x') {
  103. /* Nice green. */
  104. loc[0] = 0x1f;
  105. loc[1] = 0x8f;
  106. loc[2] = 0x1f;
  107. } else {
  108. /* Light gray. */
  109. loc[0] = 0xaa;
  110. loc[1] = 0xaa;
  111. loc[2] = 0xaa;
  112. }
  113. loc += 3;
  114. }
  115. }
  116. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  117. if (useMipmaps) {
  118. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  119. GL_LINEAR_MIPMAP_LINEAR);
  120. gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 16, 16,
  121. GL_RGB, GL_UNSIGNED_BYTE, floorTexture);
  122. } else {
  123. if (linearFiltering) {
  124. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  125. } else {
  126. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  127. }
  128. glTexImage2D(GL_TEXTURE_2D, 0, 3, 16, 16, 0,
  129. GL_RGB, GL_UNSIGNED_BYTE, floorTexture);
  130. }
  131. }
  132. enum {
  133. X, Y, Z, W
  134. };
  135. enum {
  136. A, B, C, D
  137. };
  138. /* Create a matrix that will project the desired shadow. */
  139. void
  140. shadowMatrix(GLfloat shadowMat[4][4],
  141. GLfloat groundplane[4],
  142. GLfloat lightpos[4])
  143. {
  144. GLfloat dot;
  145. /* Find dot product between light position vector and ground plane normal. */
  146. dot = groundplane[X] * lightpos[X] +
  147. groundplane[Y] * lightpos[Y] +
  148. groundplane[Z] * lightpos[Z] +
  149. groundplane[W] * lightpos[W];
  150. shadowMat[0][0] = dot - lightpos[X] * groundplane[X];
  151. shadowMat[1][0] = 0.f - lightpos[X] * groundplane[Y];
  152. shadowMat[2][0] = 0.f - lightpos[X] * groundplane[Z];
  153. shadowMat[3][0] = 0.f - lightpos[X] * groundplane[W];
  154. shadowMat[X][1] = 0.f - lightpos[Y] * groundplane[X];
  155. shadowMat[1][1] = dot - lightpos[Y] * groundplane[Y];
  156. shadowMat[2][1] = 0.f - lightpos[Y] * groundplane[Z];
  157. shadowMat[3][1] = 0.f - lightpos[Y] * groundplane[W];
  158. shadowMat[X][2] = 0.f - lightpos[Z] * groundplane[X];
  159. shadowMat[1][2] = 0.f - lightpos[Z] * groundplane[Y];
  160. shadowMat[2][2] = dot - lightpos[Z] * groundplane[Z];
  161. shadowMat[3][2] = 0.f - lightpos[Z] * groundplane[W];
  162. shadowMat[X][3] = 0.f - lightpos[W] * groundplane[X];
  163. shadowMat[1][3] = 0.f - lightpos[W] * groundplane[Y];
  164. shadowMat[2][3] = 0.f - lightpos[W] * groundplane[Z];
  165. shadowMat[3][3] = dot - lightpos[W] * groundplane[W];
  166. }
  167. /* Find the plane equation given 3 points. */
  168. void
  169. findPlane(GLfloat plane[4],
  170. GLfloat v0[3], GLfloat v1[3], GLfloat v2[3])
  171. {
  172. GLfloat vec0[3], vec1[3];
  173. /* Need 2 vectors to find cross product. */
  174. vec0[X] = v1[X] - v0[X];
  175. vec0[Y] = v1[Y] - v0[Y];
  176. vec0[Z] = v1[Z] - v0[Z];
  177. vec1[X] = v2[X] - v0[X];
  178. vec1[Y] = v2[Y] - v0[Y];
  179. vec1[Z] = v2[Z] - v0[Z];
  180. /* find cross product to get A, B, and C of plane equation */
  181. plane[A] = vec0[Y] * vec1[Z] - vec0[Z] * vec1[Y];
  182. plane[B] = -(vec0[X] * vec1[Z] - vec0[Z] * vec1[X]);
  183. plane[C] = vec0[X] * vec1[Y] - vec0[Y] * vec1[X];
  184. plane[D] = -(plane[A] * v0[X] + plane[B] * v0[Y] + plane[C] * v0[Z]);
  185. }
  186. void
  187. extrudeSolidFromPolygon(GLfloat data[][2], unsigned int dataSize,
  188. GLdouble thickness, GLuint side, GLuint edge, GLuint whole)
  189. {
  190. static GLUtriangulatorObj *tobj = NULL;
  191. GLdouble vertex[3], dx, dy, len;
  192. int i;
  193. int count = (int) (dataSize / (2 * sizeof(GLfloat)));
  194. if (tobj == NULL) {
  195. tobj = gluNewTess(); /* create and initialize a GLU
  196. polygon tesselation object */
  197. gluTessCallback(tobj, GLU_BEGIN, glBegin);
  198. gluTessCallback(tobj, GLU_VERTEX, glVertex2fv); /* semi-tricky */
  199. gluTessCallback(tobj, GLU_END, glEnd);
  200. }
  201. glNewList(side, GL_COMPILE);
  202. glShadeModel(GL_SMOOTH); /* smooth minimizes seeing
  203. tessellation */
  204. gluBeginPolygon(tobj);
  205. for (i = 0; i < count; i++) {
  206. vertex[0] = data[i][0];
  207. vertex[1] = data[i][1];
  208. vertex[2] = 0;
  209. gluTessVertex(tobj, vertex, data[i]);
  210. }
  211. gluEndPolygon(tobj);
  212. glEndList();
  213. glNewList(edge, GL_COMPILE);
  214. glShadeModel(GL_FLAT); /* flat shade keeps angular hands
  215. from being "smoothed" */
  216. glBegin(GL_QUAD_STRIP);
  217. for (i = 0; i <= count; i++) {
  218. /* mod function handles closing the edge */
  219. glVertex3f(data[i % count][0], data[i % count][1], 0.0);
  220. glVertex3f(data[i % count][0], data[i % count][1], thickness);
  221. /* Calculate a unit normal by dividing by Euclidean
  222. distance. We * could be lazy and use
  223. glEnable(GL_NORMALIZE) so we could pass in * arbitrary
  224. normals for a very slight performance hit. */
  225. dx = data[(i + 1) % count][1] - data[i % count][1];
  226. dy = data[i % count][0] - data[(i + 1) % count][0];
  227. len = sqrt(dx * dx + dy * dy);
  228. glNormal3f(dx / len, dy / len, 0.0);
  229. }
  230. glEnd();
  231. glEndList();
  232. glNewList(whole, GL_COMPILE);
  233. glFrontFace(GL_CW);
  234. glCallList(edge);
  235. glNormal3f(0.0, 0.0, -1.0); /* constant normal for side */
  236. glCallList(side);
  237. glPushMatrix();
  238. glTranslatef(0.0, 0.0, thickness);
  239. glFrontFace(GL_CCW);
  240. glNormal3f(0.0, 0.0, 1.0); /* opposite normal for other side */
  241. glCallList(side);
  242. glPopMatrix();
  243. glEndList();
  244. }
  245. /* Enumerants for refering to display lists. */
  246. typedef enum {
  247. RESERVED, BODY_SIDE, BODY_EDGE, BODY_WHOLE, ARM_SIDE, ARM_EDGE, ARM_WHOLE,
  248. LEG_SIDE, LEG_EDGE, LEG_WHOLE, EYE_SIDE, EYE_EDGE, EYE_WHOLE
  249. } displayLists;
  250. static void
  251. makeDinosaur(void)
  252. {
  253. extrudeSolidFromPolygon(body, sizeof(body), bodyWidth,
  254. BODY_SIDE, BODY_EDGE, BODY_WHOLE);
  255. extrudeSolidFromPolygon(arm, sizeof(arm), bodyWidth / 4,
  256. ARM_SIDE, ARM_EDGE, ARM_WHOLE);
  257. extrudeSolidFromPolygon(leg, sizeof(leg), bodyWidth / 2,
  258. LEG_SIDE, LEG_EDGE, LEG_WHOLE);
  259. extrudeSolidFromPolygon(eye, sizeof(eye), bodyWidth + 0.2,
  260. EYE_SIDE, EYE_EDGE, EYE_WHOLE);
  261. }
  262. static void
  263. drawDinosaur(void)
  264. {
  265. glPushMatrix();
  266. /* Translate the dinosaur to be at (0,8,0). */
  267. glTranslatef(-8, 0, -bodyWidth / 2);
  268. glTranslatef(0.0, jump, 0.0);
  269. glMaterialfv(GL_FRONT, GL_DIFFUSE, skinColor);
  270. glCallList(BODY_WHOLE);
  271. glTranslatef(0.0, 0.0, bodyWidth);
  272. glCallList(ARM_WHOLE);
  273. glCallList(LEG_WHOLE);
  274. glTranslatef(0.0, 0.0, -bodyWidth - bodyWidth / 4);
  275. glCallList(ARM_WHOLE);
  276. glTranslatef(0.0, 0.0, -bodyWidth / 4);
  277. glCallList(LEG_WHOLE);
  278. glTranslatef(0.0, 0.0, bodyWidth / 2 - 0.1);
  279. glMaterialfv(GL_FRONT, GL_DIFFUSE, eyeColor);
  280. glCallList(EYE_WHOLE);
  281. glPopMatrix();
  282. }
  283. static GLfloat floorVertices[4][3] = {
  284. { -20.0, 0.0, 20.0 },
  285. { 20.0, 0.0, 20.0 },
  286. { 20.0, 0.0, -20.0 },
  287. { -20.0, 0.0, -20.0 },
  288. };
  289. /* Draw a floor (possibly textured). */
  290. static void
  291. drawFloor(void)
  292. {
  293. glDisable(GL_LIGHTING);
  294. if (useTexture) {
  295. glEnable(GL_TEXTURE_2D);
  296. }
  297. glBegin(GL_QUADS);
  298. glTexCoord2f(0.0, 0.0);
  299. glVertex3fv(floorVertices[0]);
  300. glTexCoord2f(0.0, 16.0);
  301. glVertex3fv(floorVertices[1]);
  302. glTexCoord2f(16.0, 16.0);
  303. glVertex3fv(floorVertices[2]);
  304. glTexCoord2f(16.0, 0.0);
  305. glVertex3fv(floorVertices[3]);
  306. glEnd();
  307. if (useTexture) {
  308. glDisable(GL_TEXTURE_2D);
  309. }
  310. glEnable(GL_LIGHTING);
  311. }
  312. static GLfloat floorPlane[4];
  313. static GLfloat floorShadow[4][4];
  314. static void
  315. redraw(void)
  316. {
  317. int start, end;
  318. if (reportSpeed) {
  319. start = glutGet(GLUT_ELAPSED_TIME);
  320. }
  321. /* Clear; default stencil clears to zero. */
  322. if ((stencilReflection && renderReflection) || (stencilShadow && renderShadow)) {
  323. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  324. } else {
  325. /* Avoid clearing stencil when not using it. */
  326. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  327. }
  328. /* Reposition the light source. */
  329. lightPosition[0] = 12*cos(lightAngle);
  330. lightPosition[1] = lightHeight;
  331. lightPosition[2] = 12*sin(lightAngle);
  332. if (directionalLight) {
  333. lightPosition[3] = 0.0;
  334. } else {
  335. lightPosition[3] = 1.0;
  336. }
  337. shadowMatrix(floorShadow, floorPlane, lightPosition);
  338. glPushMatrix();
  339. /* Perform scene rotations based on user mouse input. */
  340. glRotatef(angle2, 1.0, 0.0, 0.0);
  341. glRotatef(angle, 0.0, 1.0, 0.0);
  342. /* Tell GL new light source position. */
  343. glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  344. if (renderReflection) {
  345. if (stencilReflection) {
  346. /* We can eliminate the visual "artifact" of seeing the "flipped"
  347. dinosaur underneath the floor by using stencil. The idea is
  348. draw the floor without color or depth update but so that
  349. a stencil value of one is where the floor will be. Later when
  350. rendering the dinosaur reflection, we will only update pixels
  351. with a stencil value of 1 to make sure the reflection only
  352. lives on the floor, not below the floor. */
  353. /* Don't update color or depth. */
  354. glDisable(GL_DEPTH_TEST);
  355. glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  356. /* Draw 1 into the stencil buffer. */
  357. glEnable(GL_STENCIL_TEST);
  358. glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
  359. glStencilFunc(GL_ALWAYS, 1, 0xffffffff);
  360. /* Now render floor; floor pixels just get their stencil set to 1. */
  361. drawFloor();
  362. /* Re-enable update of color and depth. */
  363. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  364. glEnable(GL_DEPTH_TEST);
  365. /* Now, only render where stencil is set to 1. */
  366. glStencilFunc(GL_EQUAL, 1, 0xffffffff); /* draw if ==1 */
  367. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  368. }
  369. glPushMatrix();
  370. /* The critical reflection step: Reflect dinosaur through the floor
  371. (the Y=0 plane) to make a relection. */
  372. glScalef(1.0, -1.0, 1.0);
  373. /* Reflect the light position. */
  374. glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  375. /* To avoid our normals getting reversed and hence botched lighting
  376. on the reflection, turn on normalize. */
  377. glEnable(GL_NORMALIZE);
  378. glCullFace(GL_FRONT);
  379. /* Draw the reflected dinosaur. */
  380. drawDinosaur();
  381. /* Disable noramlize again and re-enable back face culling. */
  382. glDisable(GL_NORMALIZE);
  383. glCullFace(GL_BACK);
  384. glPopMatrix();
  385. /* Switch back to the unreflected light position. */
  386. glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  387. if (stencilReflection) {
  388. glDisable(GL_STENCIL_TEST);
  389. }
  390. }
  391. /* Back face culling will get used to only draw either the top or the
  392. bottom floor. This let's us get a floor with two distinct
  393. appearances. The top floor surface is reflective and kind of red.
  394. The bottom floor surface is not reflective and blue. */
  395. /* Draw "bottom" of floor in blue. */
  396. glFrontFace(GL_CW); /* Switch face orientation. */
  397. glColor4f(0.1, 0.1, 0.7, 1.0);
  398. drawFloor();
  399. glFrontFace(GL_CCW);
  400. if (renderShadow) {
  401. if (stencilShadow) {
  402. /* Draw the floor with stencil value 3. This helps us only
  403. draw the shadow once per floor pixel (and only on the
  404. floor pixels). */
  405. glEnable(GL_STENCIL_TEST);
  406. glStencilFunc(GL_ALWAYS, 3, 0xffffffff);
  407. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  408. }
  409. }
  410. /* Draw "top" of floor. Use blending to blend in reflection. */
  411. glEnable(GL_BLEND);
  412. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  413. glColor4f(0.7, 0.0, 0.0, 0.3);
  414. glColor4f(1.0, 1.0, 1.0, 0.3);
  415. drawFloor();
  416. glDisable(GL_BLEND);
  417. if (renderDinosaur) {
  418. /* Draw "actual" dinosaur, not its reflection. */
  419. drawDinosaur();
  420. }
  421. if (renderShadow) {
  422. /* Render the projected shadow. */
  423. if (stencilShadow) {
  424. /* Now, only render where stencil is set above 2 (ie, 3 where
  425. the top floor is). Update stencil with 2 where the shadow
  426. gets drawn so we don't redraw (and accidently reblend) the
  427. shadow). */
  428. glStencilFunc(GL_LESS, 2, 0xffffffff); /* draw if ==1 */
  429. glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
  430. }
  431. /* To eliminate depth buffer artifacts, we use polygon offset
  432. to raise the depth of the projected shadow slightly so
  433. that it does not depth buffer alias with the floor. */
  434. if (offsetShadow) {
  435. switch (polygonOffsetVersion) {
  436. case EXTENSION:
  437. #ifdef GL_EXT_polygon_offset
  438. glEnable(GL_POLYGON_OFFSET_EXT);
  439. break;
  440. #endif
  441. #ifdef GL_VERSION_1_1
  442. case ONE_DOT_ONE:
  443. glEnable(GL_POLYGON_OFFSET_FILL);
  444. break;
  445. #endif
  446. case MISSING:
  447. /* Oh well. */
  448. break;
  449. }
  450. }
  451. /* Render 50% black shadow color on top of whatever the
  452. floor appareance is. */
  453. glEnable(GL_BLEND);
  454. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  455. glDisable(GL_LIGHTING); /* Force the 50% black. */
  456. glColor4f(0.0, 0.0, 0.0, 0.5);
  457. glPushMatrix();
  458. /* Project the shadow. */
  459. glMultMatrixf((GLfloat *) floorShadow);
  460. drawDinosaur();
  461. glPopMatrix();
  462. glDisable(GL_BLEND);
  463. glEnable(GL_LIGHTING);
  464. if (offsetShadow) {
  465. switch (polygonOffsetVersion) {
  466. #ifdef GL_EXT_polygon_offset
  467. case EXTENSION:
  468. glDisable(GL_POLYGON_OFFSET_EXT);
  469. break;
  470. #endif
  471. #ifdef GL_VERSION_1_1
  472. case ONE_DOT_ONE:
  473. glDisable(GL_POLYGON_OFFSET_FILL);
  474. break;
  475. #endif
  476. case MISSING:
  477. /* Oh well. */
  478. break;
  479. }
  480. }
  481. if (stencilShadow) {
  482. glDisable(GL_STENCIL_TEST);
  483. }
  484. }
  485. glPushMatrix();
  486. glDisable(GL_LIGHTING);
  487. glColor3f(1.0, 1.0, 0.0);
  488. if (directionalLight) {
  489. /* Draw an arrowhead. */
  490. glDisable(GL_CULL_FACE);
  491. glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]);
  492. glRotatef(lightAngle * -180.0 / M_PI, 0, 1, 0);
  493. glRotatef(atan(lightHeight/12) * 180.0 / M_PI, 0, 0, 1);
  494. glBegin(GL_TRIANGLE_FAN);
  495. glVertex3f(0, 0, 0);
  496. glVertex3f(2, 1, 1);
  497. glVertex3f(2, -1, 1);
  498. glVertex3f(2, -1, -1);
  499. glVertex3f(2, 1, -1);
  500. glVertex3f(2, 1, 1);
  501. glEnd();
  502. /* Draw a white line from light direction. */
  503. glColor3f(1.0, 1.0, 1.0);
  504. glBegin(GL_LINES);
  505. glVertex3f(0, 0, 0);
  506. glVertex3f(5, 0, 0);
  507. glEnd();
  508. glEnable(GL_CULL_FACE);
  509. } else {
  510. /* Draw a yellow ball at the light source. */
  511. glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]);
  512. glutSolidSphere(1.0, 5, 5);
  513. }
  514. glEnable(GL_LIGHTING);
  515. glPopMatrix();
  516. glPopMatrix();
  517. if (reportSpeed) {
  518. glFinish();
  519. end = glutGet(GLUT_ELAPSED_TIME);
  520. printf("Speed %.3g frames/sec (%d ms)\n", 1000.0/(end-start), end-start);
  521. }
  522. glutSwapBuffers();
  523. }
  524. /* ARGSUSED2 */
  525. static void
  526. mouse(int button, int state, int x, int y)
  527. {
  528. if (button == GLUT_LEFT_BUTTON) {
  529. if (state == GLUT_DOWN) {
  530. moving = 1;
  531. startx = x;
  532. starty = y;
  533. }
  534. if (state == GLUT_UP) {
  535. moving = 0;
  536. }
  537. }
  538. if (button == GLUT_MIDDLE_BUTTON) {
  539. if (state == GLUT_DOWN) {
  540. lightMoving = 1;
  541. lightStartX = x;
  542. lightStartY = y;
  543. }
  544. if (state == GLUT_UP) {
  545. lightMoving = 0;
  546. }
  547. }
  548. }
  549. /* ARGSUSED1 */
  550. static void
  551. motion(int x, int y)
  552. {
  553. if (moving) {
  554. angle = angle + (x - startx);
  555. angle2 = angle2 + (y - starty);
  556. startx = x;
  557. starty = y;
  558. glutPostRedisplay();
  559. }
  560. if (lightMoving) {
  561. lightAngle += (x - lightStartX)/40.0;
  562. lightHeight += (lightStartY - y)/20.0;
  563. lightStartX = x;
  564. lightStartY = y;
  565. glutPostRedisplay();
  566. }
  567. }
  568. /* Advance time varying state when idle callback registered. */
  569. static void
  570. idle(void)
  571. {
  572. static float time = 0.0;
  573. time = glutGet(GLUT_ELAPSED_TIME) / 500.0;
  574. jump = 4.0 * fabs(sin(time)*0.5);
  575. if (!lightMoving) {
  576. lightAngle += 0.03;
  577. }
  578. glutPostRedisplay();
  579. }
  580. enum {
  581. M_NONE, M_MOTION, M_LIGHT, M_TEXTURE, M_SHADOWS, M_REFLECTION, M_DINOSAUR,
  582. M_STENCIL_REFLECTION, M_STENCIL_SHADOW, M_OFFSET_SHADOW,
  583. M_POSITIONAL, M_DIRECTIONAL, M_PERFORMANCE
  584. };
  585. static void
  586. controlLights(int value)
  587. {
  588. switch (value) {
  589. case M_NONE:
  590. return;
  591. case M_MOTION:
  592. animation = 1 - animation;
  593. if (animation) {
  594. glutIdleFunc(idle);
  595. } else {
  596. glutIdleFunc(NULL);
  597. }
  598. break;
  599. case M_LIGHT:
  600. lightSwitch = !lightSwitch;
  601. if (lightSwitch) {
  602. glEnable(GL_LIGHT0);
  603. } else {
  604. glDisable(GL_LIGHT0);
  605. }
  606. break;
  607. case M_TEXTURE:
  608. useTexture = !useTexture;
  609. break;
  610. case M_SHADOWS:
  611. renderShadow = 1 - renderShadow;
  612. break;
  613. case M_REFLECTION:
  614. renderReflection = 1 - renderReflection;
  615. break;
  616. case M_DINOSAUR:
  617. renderDinosaur = 1 - renderDinosaur;
  618. break;
  619. case M_STENCIL_REFLECTION:
  620. stencilReflection = 1 - stencilReflection;
  621. break;
  622. case M_STENCIL_SHADOW:
  623. stencilShadow = 1 - stencilShadow;
  624. break;
  625. case M_OFFSET_SHADOW:
  626. offsetShadow = 1 - offsetShadow;
  627. break;
  628. case M_POSITIONAL:
  629. directionalLight = 0;
  630. break;
  631. case M_DIRECTIONAL:
  632. directionalLight = 1;
  633. break;
  634. case M_PERFORMANCE:
  635. reportSpeed = 1 - reportSpeed;
  636. break;
  637. }
  638. glutPostRedisplay();
  639. }
  640. /* When not visible, stop animating. Restart when visible again. */
  641. static void
  642. visible(int vis)
  643. {
  644. if (vis == GLUT_VISIBLE) {
  645. if (animation)
  646. glutIdleFunc(idle);
  647. } else {
  648. if (!animation)
  649. glutIdleFunc(NULL);
  650. }
  651. }
  652. /* Press any key to redraw; good when motion stopped and
  653. performance reporting on. */
  654. /* ARGSUSED */
  655. static void
  656. key(unsigned char c, int x, int y)
  657. {
  658. if (c == 27) {
  659. exit(0); /* IRIS GLism, Escape quits. */
  660. }
  661. glutPostRedisplay();
  662. }
  663. /* Press any key to redraw; good when motion stopped and
  664. performance reporting on. */
  665. /* ARGSUSED */
  666. static void
  667. special(int k, int x, int y)
  668. {
  669. glutPostRedisplay();
  670. }
  671. static int
  672. supportsOneDotOne(void)
  673. {
  674. const char *version;
  675. int major, minor;
  676. version = (char *) glGetString(GL_VERSION);
  677. if (sscanf(version, "%d.%d", &major, &minor) == 2)
  678. return major >= 1 && minor >= 1;
  679. return 0; /* OpenGL version string malformed! */
  680. }
  681. int
  682. main(int argc, char **argv)
  683. {
  684. int i;
  685. glutInit(&argc, argv);
  686. for (i=1; i<argc; i++) {
  687. if (!strcmp("-linear", argv[i])) {
  688. linearFiltering = 1;
  689. } else if (!strcmp("-mipmap", argv[i])) {
  690. useMipmaps = 1;
  691. } else if (!strcmp("-ext", argv[i])) {
  692. forceExtension = 1;
  693. }
  694. }
  695. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL);
  696. #if 0
  697. /* In GLUT 4.0, you'll be able to do this an be sure to
  698. get 2 bits of stencil if the machine has it for you. */
  699. glutInitDisplayString("samples stencil>=2 rgb double depth");
  700. #endif
  701. glutCreateWindow("Shadowy Leapin' Lizards");
  702. if (glutGet(GLUT_WINDOW_STENCIL_SIZE) <= 1) {
  703. printf("dinoshade: Sorry, I need at least 2 bits of stencil.\n");
  704. exit(1);
  705. }
  706. /* Register GLUT callbacks. */
  707. glutDisplayFunc(redraw);
  708. glutMouseFunc(mouse);
  709. glutMotionFunc(motion);
  710. glutVisibilityFunc(visible);
  711. glutKeyboardFunc(key);
  712. glutSpecialFunc(special);
  713. glutCreateMenu(controlLights);
  714. glutAddMenuEntry("Toggle motion", M_MOTION);
  715. glutAddMenuEntry("-----------------------", M_NONE);
  716. glutAddMenuEntry("Toggle light", M_LIGHT);
  717. glutAddMenuEntry("Toggle texture", M_TEXTURE);
  718. glutAddMenuEntry("Toggle shadows", M_SHADOWS);
  719. glutAddMenuEntry("Toggle reflection", M_REFLECTION);
  720. glutAddMenuEntry("Toggle dinosaur", M_DINOSAUR);
  721. glutAddMenuEntry("-----------------------", M_NONE);
  722. glutAddMenuEntry("Toggle reflection stenciling", M_STENCIL_REFLECTION);
  723. glutAddMenuEntry("Toggle shadow stenciling", M_STENCIL_SHADOW);
  724. glutAddMenuEntry("Toggle shadow offset", M_OFFSET_SHADOW);
  725. glutAddMenuEntry("----------------------", M_NONE);
  726. glutAddMenuEntry("Positional light", M_POSITIONAL);
  727. glutAddMenuEntry("Directional light", M_DIRECTIONAL);
  728. glutAddMenuEntry("-----------------------", M_NONE);
  729. glutAddMenuEntry("Toggle performance", M_PERFORMANCE);
  730. glutAttachMenu(GLUT_RIGHT_BUTTON);
  731. makeDinosaur();
  732. #ifdef GL_VERSION_1_1
  733. if (supportsOneDotOne() && !forceExtension) {
  734. polygonOffsetVersion = ONE_DOT_ONE;
  735. glPolygonOffset(-2.0, -9.0);
  736. } else
  737. #endif
  738. {
  739. #ifdef GL_EXT_polygon_offset
  740. /* check for the polygon offset extension */
  741. if (glutExtensionSupported("GL_EXT_polygon_offset")) {
  742. polygonOffsetVersion = EXTENSION;
  743. glPolygonOffsetEXT(-2.0, -0.002);
  744. } else
  745. #endif
  746. {
  747. polygonOffsetVersion = MISSING;
  748. printf("\ndinoshine: Missing polygon offset.\n");
  749. printf(" Expect shadow depth aliasing artifacts.\n\n");
  750. }
  751. }
  752. glEnable(GL_CULL_FACE);
  753. glEnable(GL_DEPTH_TEST);
  754. glEnable(GL_TEXTURE_2D);
  755. glLineWidth(3.0);
  756. glMatrixMode(GL_PROJECTION);
  757. gluPerspective( /* field of view in degree */ 40.0,
  758. /* aspect ratio */ 1.0,
  759. /* Z near */ 20.0, /* Z far */ 100.0);
  760. glMatrixMode(GL_MODELVIEW);
  761. gluLookAt(0.0, 8.0, 60.0, /* eye is at (0,8,60) */
  762. 0.0, 8.0, 0.0, /* center is at (0,8,0) */
  763. 0.0, 1.0, 0.); /* up is in postivie Y direction */
  764. glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
  765. glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
  766. glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1);
  767. glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05);
  768. glEnable(GL_LIGHT0);
  769. glEnable(GL_LIGHTING);
  770. makeFloorTexture();
  771. /* Setup floor plane for projected shadow calculations. */
  772. findPlane(floorPlane, floorVertices[1], floorVertices[2], floorVertices[3]);
  773. glutMainLoop();
  774. return 0; /* ANSI C requires main to return int. */
  775. }