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.

293 lines
8.2 KiB

  1. /*
  2. * (c) copyright 1993, silicon graphics, inc.
  3. * ALL RIGHTS RESERVED
  4. * Permission to use, copy, modify, and distribute this software for
  5. * any purpose and without fee is hereby granted, provided that the above
  6. * copyright notice appear in all copies and that both the copyright notice
  7. * and this permission notice appear in supporting documentation, and that
  8. * the name of Silicon Graphics, Inc. not be used in advertising
  9. * or publicity pertaining to distribution of the software without specific,
  10. * written prior permission.
  11. *
  12. * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13. * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14. * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15. * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
  16. * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17. * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18. * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19. * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20. * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
  21. * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22. * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23. * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24. *
  25. * US Government Users Restricted Rights
  26. * Use, duplication, or disclosure by the Government is subject to
  27. * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28. * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29. * clause at DFARS 252.227-7013 and/or in similar or successor
  30. * clauses in the FAR or the DOD or NASA FAR Supplement.
  31. * Unpublished-- rights reserved under the copyright laws of the
  32. * United States. Contractor/manufacturer is Silicon Graphics,
  33. * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
  34. *
  35. * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36. */
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <stdlib.h>
  40. #include <math.h>
  41. #include <assert.h>
  42. #include "wave.hxx"
  43. #define PI 3.14159265358979323846f
  44. #define GETWCOORD(frame, x, y) (&(mesh.coords[frame*mesh.numCoords+(x)+(y)*(iWidthX+1)]))
  45. #define GETFACET(frame, x, y) (&(mesh.facets[frame*mesh.numFacets+(x)+(y)*iWidthX]))
  46. #ifndef cosf
  47. #define cosf( x ) \
  48. ( (float) cos( (x) ) )
  49. #define sinf( x ) \
  50. ( (float) sin( (x) ) )
  51. #endif
  52. #ifndef sqrtf
  53. #define sqrtf( x ) \
  54. ( (float) sqrt( (x) ) )
  55. #endif
  56. /******************** WAVE *************************************************
  57. *
  58. * Creates a moving wave in the x-y plane, with varying height along the z-axis.
  59. *
  60. * The x,y ranges are confined to (-.5, .5). Height can be any positive number.
  61. * A number of frames are generated, which are drawn sequentially on Draw calls.
  62. * It can run in either smooth or flat shaded modes. In smooth mode, the
  63. * materials are generated with glMaterialColor.
  64. * Two of the opposite corners of the wave are pinned down, while the wave moves
  65. * diagonally between the other corners.
  66. *
  67. * It looks like there is only one period of the wave visible at any one time,
  68. * and no way to adjust this. (mf: we need more periods)
  69. *
  70. * In the standard GL reference axis scheme, the normals for wave all have
  71. * normals pointing in the -z direction. Looking at wave from that direction,
  72. * the quad orientation is CW. So need to rotate by 180 around y if viewing
  73. * down the -z axis. Alternatively, could construct the wave differently
  74. ***************************************************************************/
  75. WAVE::WAVE()
  76. {
  77. nFrames = 10;
  78. iWidthX = 10;
  79. iWidthY = 10;
  80. iCheckerSize = 2;
  81. fHeight = 0.2f;
  82. iCurFrame = 0;
  83. InitMaterials();
  84. InitMesh();
  85. }
  86. WAVE::~WAVE()
  87. {
  88. if( mesh.coords )
  89. free( mesh.coords );
  90. if( mesh.facets )
  91. free( mesh.facets );
  92. }
  93. void
  94. WAVE::Draw()
  95. {
  96. //mf: could be a candidate for vertex array ??
  97. WCOORD *coord;
  98. FACET *facet;
  99. float *lastColor;
  100. float *thisColor;
  101. GLint i, j;
  102. // Set any state required (caller will have to 'push' this state), or ?
  103. // should do it here ?
  104. //mf: need to set front face, etc. Caller can probably do this, since it's
  105. // going to need to know how the wave is constructed anyways.
  106. if (bSmooth) {
  107. glShadeModel(GL_SMOOTH);
  108. } else {
  109. glShadeModel(GL_FLAT);
  110. }
  111. glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
  112. glEnable(GL_COLOR_MATERIAL);
  113. iCurFrame++;
  114. // Draw the wave frame
  115. if (iCurFrame >= nFrames) {
  116. iCurFrame = 0;
  117. }
  118. for (i = 0; i < iWidthX; i++) {
  119. glBegin(GL_QUAD_STRIP);
  120. lastColor = NULL;
  121. for (j = 0; j < iWidthY; j++) {
  122. facet = GETFACET(iCurFrame, i, j);
  123. if (!bSmooth && bLighting) {
  124. glNormal3fv(facet->normal);
  125. }
  126. if (bLighting) {
  127. thisColor = facet->color;
  128. glColor3fv(facet->color);
  129. } else {
  130. thisColor = facet->color;
  131. glColor3fv(facet->color);
  132. }
  133. if (!lastColor || (thisColor[0] != lastColor[0] && bSmooth)) {
  134. if (lastColor) {
  135. glEnd();
  136. glBegin(GL_QUAD_STRIP);
  137. }
  138. coord = GETWCOORD(iCurFrame, i, j);
  139. if (bSmooth && bLighting) {
  140. glNormal3fv(coord->normal);
  141. }
  142. glVertex3fv(coord->vertex);
  143. coord = GETWCOORD(iCurFrame, i+1, j);
  144. if (bSmooth && bLighting) {
  145. glNormal3fv(coord->normal);
  146. }
  147. glVertex3fv(coord->vertex);
  148. }
  149. coord = GETWCOORD(iCurFrame, i, j+1);
  150. if (bSmooth && bLighting) {
  151. glNormal3fv(coord->normal);
  152. }
  153. glVertex3fv(coord->vertex);
  154. coord = GETWCOORD(iCurFrame, i+1, j+1);
  155. if (bSmooth && bLighting) {
  156. glNormal3fv(coord->normal);
  157. }
  158. glVertex3fv(coord->vertex);
  159. lastColor = thisColor;
  160. }
  161. glEnd();
  162. }
  163. }
  164. void
  165. WAVE::InitMesh()
  166. {
  167. WCOORD *coord;
  168. FACET *facet;
  169. float dp1[3], dp2[3];
  170. float *pt1, *pt2, *pt3;
  171. float angle, d, x, y;
  172. GLint numFacets, numCoords, frameNum, i, j;
  173. numFacets = iWidthX * iWidthY;
  174. numCoords = (iWidthX + 1) * (iWidthY + 1);
  175. mesh.numCoords = numCoords;
  176. mesh.numFacets = numFacets;
  177. mesh.coords = (WCOORD *)malloc(nFrames*numCoords*
  178. sizeof(WCOORD));
  179. mesh.facets = (FACET *)malloc(nFrames*numFacets*
  180. sizeof(FACET));
  181. assert( mesh.coords != NULL || mesh.facets != NULL);
  182. for (frameNum = 0; frameNum < nFrames; frameNum++) {
  183. for (i = 0; i <= iWidthX; i++) {
  184. x = i / (float)iWidthX;
  185. for (j = 0; j <= iWidthY; j++) {
  186. y = j / (float)iWidthY;
  187. d = (float) sqrtf(x*x+y*y);
  188. if (d == 0.0f) {
  189. d = 0.0001f;
  190. }
  191. angle = 2 * PI * d + (2 * PI / nFrames * frameNum);
  192. coord = GETWCOORD(frameNum, i, j);
  193. coord->vertex[0] = x - 0.5f;
  194. coord->vertex[1] = y - 0.5f;
  195. coord->vertex[2] = (fHeight - fHeight * d) * cosf(angle);
  196. coord->normal[0] = -(fHeight / d) * x * ((1 - d) * 2 * PI *
  197. sinf(angle) + cosf(angle));
  198. coord->normal[1] = -(fHeight / d) * y * ((1 - d) * 2 * PI *
  199. sinf(angle) + cosf(angle));
  200. coord->normal[2] = -1.0f;
  201. d = 1.0f / sqrtf(coord->normal[0]*coord->normal[0]+
  202. coord->normal[1]*coord->normal[1]+1);
  203. coord->normal[0] *= d;
  204. coord->normal[1] *= d;
  205. coord->normal[2] *= d;
  206. }
  207. }
  208. for (i = 0; i < iWidthX; i++) {
  209. for (j = 0; j < iWidthY; j++) {
  210. facet = GETFACET(frameNum, i, j);
  211. if (((i/iCheckerSize)%2)^(j/iCheckerSize)%2) {
  212. facet->color[0] = 1.0f;
  213. facet->color[1] = 0.2f;
  214. facet->color[2] = 0.2f;
  215. } else {
  216. facet->color[0] = 0.2f;
  217. facet->color[1] = 1.0f;
  218. facet->color[2] = 0.2f;
  219. }
  220. pt1 = GETWCOORD(frameNum, i, j)->vertex;
  221. pt2 = GETWCOORD(frameNum, i, j+1)->vertex;
  222. pt3 = GETWCOORD(frameNum, i+1, j+1)->vertex;
  223. dp1[0] = pt2[0] - pt1[0];
  224. dp1[1] = pt2[1] - pt1[1];
  225. dp1[2] = pt2[2] - pt1[2];
  226. dp2[0] = pt3[0] - pt2[0];
  227. dp2[1] = pt3[1] - pt2[1];
  228. dp2[2] = pt3[2] - pt2[2];
  229. facet->normal[0] = dp1[1] * dp2[2] - dp1[2] * dp2[1];
  230. facet->normal[1] = dp1[2] * dp2[0] - dp1[0] * dp2[2];
  231. facet->normal[2] = dp1[0] * dp2[1] - dp1[1] * dp2[0];
  232. d = 1.0f / sqrtf(facet->normal[0]*facet->normal[0]+
  233. facet->normal[1]*facet->normal[1]+
  234. facet->normal[2]*facet->normal[2]);
  235. facet->normal[0] *= d;
  236. facet->normal[1] *= d;
  237. facet->normal[2] *= d;
  238. }
  239. }
  240. }
  241. }
  242. #if 0
  243. if (bLighting) {
  244. glEnable(GL_LIGHTING);
  245. glEnable(GL_LIGHT0);
  246. glEnable(GL_COLOR_MATERIAL);
  247. } else {
  248. glDisable(GL_LIGHTING);
  249. glDisable(GL_LIGHT0);
  250. glDisable(GL_COLOR_MATERIAL);
  251. }
  252. #endif