Leaked source code of windows server 2003
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.

380 lines
12 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: genexpld.c
  3. *
  4. * The Explode style of the 3D Flying Objects screen saver.
  5. *
  6. * Simulation of a sphere that occasionally explodes.
  7. *
  8. * Copyright (c) 1994 Microsoft Corporation
  9. *
  10. \**************************************************************************/
  11. #include <windows.h>
  12. #include <math.h>
  13. #include <d3dx8.h>
  14. #include "D3DSaver.h"
  15. #include "FlyingObjects.h"
  16. #include "mesh.h"
  17. #define RADIUS 0.3
  18. #define STEPS 30
  19. #define MAXPREC 20
  20. static MATRIX *faceMat;
  21. static float *xstep;
  22. static float *ystep;
  23. static float *zstep;
  24. static float *xrot;
  25. static float *yrot;
  26. static float *zrot;
  27. static MESH explodeMesh;
  28. static int iPrec = 10;
  29. // Data type accepted by glInterleavedArrays
  30. typedef struct _POINT_N3F_V3F {
  31. POINT3D normal;
  32. POINT3D vertex;
  33. } POINT_N3F_V3F;
  34. static POINT_N3F_V3F *pN3V3;
  35. static FLOAT matl1Diffuse[] = {1.0f, 0.8f, 0.0f, 1.0f};
  36. static FLOAT matl2Diffuse[] = {0.8f, 0.8f, 0.8f, 1.0f};
  37. static FLOAT matlSpecular[] = {1.0f, 1.0f, 1.0f, 1.0f};
  38. static FLOAT light0Pos[] = {100.0f, 100.0f, 100.0f, 0.0f};
  39. void genExplode()
  40. {
  41. int i;
  42. POINT3D circle[MAXPREC+1];
  43. double angle;
  44. double step = -PI / (float)(iPrec - 1);
  45. double start = PI / 2.0;
  46. for (i = 0, angle = start; i < iPrec; i++, angle += step) {
  47. circle[i].x = (float) (RADIUS * cos(angle));
  48. circle[i].y = (float) (RADIUS * sin(angle));
  49. circle[i].z = 0.0f;
  50. }
  51. revolveSurface(&explodeMesh, circle, iPrec);
  52. for (i = 0; i < explodeMesh.numFaces; i++) {
  53. ss_matrixIdent(&faceMat[i]);
  54. xstep[i] = (float)(((float)(rand() & 0x3) * PI) / ((float)STEPS + 1.0));
  55. ystep[i] = (float)(((float)(rand() & 0x3) * PI) / ((float)STEPS + 1.0));
  56. zstep[i] = (float)(((float)(rand() & 0x3) * PI) / ((float)STEPS + 1.0));
  57. xrot[i] = 0.0f;
  58. yrot[i] = 0.0f;
  59. zrot[i] = 0.0f;
  60. }
  61. }
  62. BOOL initExplodeScene()
  63. {
  64. iPrec = (int)(fTesselFact * 10.5);
  65. if (iPrec < 5)
  66. iPrec = 5;
  67. if (iPrec > MAXPREC)
  68. iPrec = MAXPREC;
  69. faceMat = (MATRIX *)SaverAlloc((iPrec * iPrec) *
  70. (4 * 4 * sizeof(float)));
  71. if( faceMat == NULL )
  72. return FALSE;
  73. xstep = (float*)SaverAlloc(iPrec * iPrec * sizeof(float));
  74. if( xstep == NULL )
  75. return FALSE;
  76. ystep = (float*)SaverAlloc(iPrec * iPrec * sizeof(float));
  77. if( ystep == NULL )
  78. return FALSE;
  79. zstep = (float*)SaverAlloc(iPrec * iPrec * sizeof(float));
  80. if( zstep == NULL )
  81. return FALSE;
  82. xrot = (float*)SaverAlloc(iPrec * iPrec * sizeof(float));
  83. if( xrot == NULL )
  84. return FALSE;
  85. yrot = (float*)SaverAlloc(iPrec * iPrec * sizeof(float));
  86. if( yrot == NULL )
  87. return FALSE;
  88. zrot = (float*)SaverAlloc(iPrec * iPrec * sizeof(float));
  89. if( zrot == NULL )
  90. return FALSE;
  91. genExplode();
  92. /*
  93. // Find out the OpenGL version that we are running on.
  94. bOpenGL11 = ss_fOnGL11();
  95. */
  96. // Setup the data arrays.
  97. pN3V3 = (POINT_N3F_V3F*)SaverAlloc(explodeMesh.numFaces * 4 * sizeof(POINT_N3F_V3F));
  98. /*
  99. // If we are running on OpenGL 1.1, use the new vertex array functions.
  100. if (bOpenGL11) {
  101. glInterleavedArrays(GL_N3F_V3F, 0, pN3V3);
  102. }
  103. */
  104. myglMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, matl1Diffuse);
  105. myglMaterialfv(GL_FRONT, GL_SPECULAR, matlSpecular);
  106. myglMaterialf(GL_FRONT, GL_SHININESS, 100.0f);
  107. /*
  108. glMaterialfv(GL_BACK, GL_AMBIENT_AND_DIFFUSE, matl2Diffuse);
  109. glMaterialfv(GL_BACK, GL_SPECULAR, matlSpecular);
  110. glMaterialf(GL_BACK, GL_SHININESS, 60.0f);
  111. */
  112. /*
  113. D3DXMATRIX matProj;
  114. D3DXMatrixPerspectiveLH( &matProj, 0.66f, 0.66f, 0.3f, 3.0f );
  115. m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  116. */
  117. SetProjectionMatrixInfo( FALSE, 0.66f, 0.66f, 0.3f, 3.0f );
  118. D3DXMATRIX matView;
  119. D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
  120. D3DXVECTOR3 vEyePt(0, 0, 1.5f);
  121. D3DXVECTOR3 vLookatPt(0, 0, 0);
  122. D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
  123. m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
  124. m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
  125. return TRUE;
  126. }
  127. void delExplodeScene()
  128. {
  129. delMesh(&explodeMesh);
  130. SaverFree(faceMat);
  131. SaverFree(xstep);
  132. SaverFree(ystep);
  133. SaverFree(zstep);
  134. SaverFree(xrot);
  135. SaverFree(yrot);
  136. SaverFree(zrot);
  137. SaverFree(pN3V3);
  138. }
  139. void updateExplodeScene(int flags, FLOAT fElapsedTime)
  140. {
  141. static float maxR;
  142. static float r = 0.0f;
  143. static float rChange = 0.0f;
  144. static float rChangePrev = 1.0f;
  145. static float rotZ = 0.0f;
  146. static int count = 0;
  147. static float fCount = 0.0f;
  148. static int direction = 1;
  149. static FLOAT fRestCount = 0.0f;
  150. static float lightSpin = 0.0f;
  151. static float spinDelta = 5.0f;
  152. static FLOAT fH = 0.0f;
  153. static RGBA color;
  154. if( fElapsedTime > 0.25f )
  155. fElapsedTime = 0.25f;
  156. FLOAT fTimeFactor = fElapsedTime * 20.0f;
  157. int i;
  158. MFACE *faces;
  159. POINT_N3F_V3F *pn3v3;
  160. D3DXMATRIX mat1, mat2, mat3, mat4, mat5, matFinal;
  161. static FLOAT fTimer = 0.0f;
  162. /*
  163. // Only update 50x per second
  164. fTimer += fElapsedTime;
  165. if( fTimer < 1.0f/50.0f )
  166. return;
  167. fTimer = 0.0f;
  168. */
  169. if (bColorCycle || fH == 0.0f)
  170. {
  171. ss_HsvToRgb(fH, 1.0f, 1.0f, &color);
  172. myglMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, (FLOAT *) &color);
  173. fH += fTimeFactor;
  174. if( fH >= 360.0f )
  175. fH -= 360.0f;
  176. }
  177. /*
  178. glMatrixMode(GL_MODELVIEW);
  179. glPushMatrix();
  180. glRotatef(-lightSpin, 0.0f, 1.0f, 0.0f);
  181. glLightfv(GL_LIGHT0, GL_POSITION, light0Pos);
  182. lightSpin += spinDelta * fTimeFactor;
  183. if ((lightSpin > 90.0) || (lightSpin < 0.0))
  184. spinDelta = -spinDelta;
  185. */
  186. /*
  187. glPopMatrix();
  188. if (!bOpenGL11) {
  189. glBegin(GL_QUADS);
  190. }
  191. */
  192. for(
  193. i = 0, faces = explodeMesh.faces, pn3v3 = pN3V3;
  194. i < explodeMesh.numFaces;
  195. i++, faces++, pn3v3 += 4
  196. )
  197. {
  198. int a, b, c, d;
  199. int j;
  200. POINT3D vector;
  201. ss_matrixIdent(&faceMat[i]);
  202. ss_matrixRotate(&faceMat[i], xrot[i], yrot[i], zrot[i]);
  203. if (fRestCount > 0.0f)
  204. ;
  205. else {
  206. xrot[i] += (xstep[i]) * fTimeFactor;
  207. yrot[i] += (ystep[i]) * fTimeFactor;
  208. zrot[i] += (zstep[i]) * fTimeFactor;
  209. }
  210. a = faces->p[0];
  211. b = faces->p[1];
  212. c = faces->p[3];
  213. d = faces->p[2];
  214. memcpy(&pn3v3[0].vertex, (explodeMesh.pts + a), sizeof(POINT3D));
  215. memcpy(&pn3v3[1].vertex, (explodeMesh.pts + b), sizeof(POINT3D));
  216. memcpy(&pn3v3[2].vertex, (explodeMesh.pts + c), sizeof(POINT3D));
  217. memcpy(&pn3v3[3].vertex, (explodeMesh.pts + d), sizeof(POINT3D));
  218. vector.x = pn3v3[0].vertex.x;
  219. vector.y = pn3v3[0].vertex.y;
  220. vector.z = pn3v3[0].vertex.z;
  221. for (j = 0; j < 4; j++) {
  222. pn3v3[j].vertex.x -= vector.x;
  223. pn3v3[j].vertex.y -= vector.y;
  224. pn3v3[j].vertex.z -= vector.z;
  225. ss_xformPoint((POINT3D *)&pn3v3[j].vertex, (POINT3D *)&pn3v3[j].vertex, &faceMat[i]);
  226. pn3v3[j].vertex.x += vector.x + (vector.x * r);
  227. pn3v3[j].vertex.y += vector.y + (vector.y * r);
  228. pn3v3[j].vertex.z += vector.z + (vector.z * r);
  229. }
  230. if (bSmoothShading) {
  231. memcpy(&pn3v3[0].normal, (explodeMesh.norms + a), sizeof(POINT3D));
  232. memcpy(&pn3v3[1].normal, (explodeMesh.norms + b), sizeof(POINT3D));
  233. memcpy(&pn3v3[2].normal, (explodeMesh.norms + c), sizeof(POINT3D));
  234. memcpy(&pn3v3[3].normal, (explodeMesh.norms + d), sizeof(POINT3D));
  235. for (j = 0; j < 4; j++)
  236. ss_xformNorm((POINT3D *)&pn3v3[j].normal, (POINT3D *)&pn3v3[j].normal, &faceMat[i]);
  237. } else {
  238. memcpy(&pn3v3[0].normal, &faces->norm, sizeof(POINT3D));
  239. ss_xformNorm((POINT3D *)&pn3v3[0].normal, (POINT3D *)&pn3v3[0].normal, &faceMat[i]);
  240. memcpy(&pn3v3[1].normal, &pn3v3[0].normal, sizeof(POINT3D));
  241. memcpy(&pn3v3[2].normal, &pn3v3[0].normal, sizeof(POINT3D));
  242. memcpy(&pn3v3[3].normal, &pn3v3[0].normal, sizeof(POINT3D));
  243. }
  244. }
  245. {
  246. m_pd3dDevice->SetVertexShader( D3DFVF_MYVERTEX );
  247. static WORD s_indexArray[5000];
  248. static MYVERTEX s_vertexArray[5000];
  249. INT numPrims = 0;
  250. INT numIndices = 0;
  251. INT numVertices = 0;
  252. WORD iVertexA, iVertexB, iVertexC, iVertexD;
  253. INT a,b,c,d;
  254. HRESULT hr;
  255. for( int iFace = 0; iFace < explodeMesh.numFaces; iFace++ )
  256. {
  257. a = iFace * 4 + 0;
  258. b = iFace * 4 + 1;
  259. c = iFace * 4 + 2;
  260. d = iFace * 4 + 3;
  261. s_vertexArray[numVertices].p = pN3V3[a].vertex;
  262. s_vertexArray[numVertices].n = pN3V3[a].normal;
  263. iVertexA = numVertices++;
  264. s_vertexArray[numVertices].p = pN3V3[b].vertex;
  265. s_vertexArray[numVertices].n = pN3V3[b].normal;
  266. iVertexB = numVertices++;
  267. s_vertexArray[numVertices].p = pN3V3[c].vertex;
  268. s_vertexArray[numVertices].n = pN3V3[c].normal;
  269. iVertexC = numVertices++;
  270. s_vertexArray[numVertices].p = pN3V3[d].vertex;
  271. s_vertexArray[numVertices].n = pN3V3[d].normal;
  272. iVertexD = numVertices++;
  273. s_indexArray[numIndices++] = iVertexA;
  274. s_indexArray[numIndices++] = iVertexB;
  275. s_indexArray[numIndices++] = iVertexC;
  276. numPrims++;
  277. s_indexArray[numIndices++] = iVertexA;
  278. s_indexArray[numIndices++] = iVertexC;
  279. s_indexArray[numIndices++] = iVertexD;
  280. numPrims++;
  281. }
  282. hr = m_pd3dDevice->DrawIndexedPrimitiveUP( D3DPT_TRIANGLELIST, 0, numVertices,
  283. numPrims, s_indexArray, D3DFMT_INDEX16, s_vertexArray, sizeof(MYVERTEX) );
  284. }
  285. /*
  286. if (bOpenGL11) {
  287. glDrawArrays(GL_QUADS, 0, explodeMesh.numFaces * 4);
  288. } else {
  289. glEnd();
  290. }
  291. */
  292. if (fRestCount > 0.0f) {
  293. fRestCount -= fTimeFactor;
  294. goto resting;
  295. }
  296. rChange += fTimeFactor;
  297. while( (INT)rChangePrev < (INT)rChange )
  298. {
  299. rChangePrev += 1.0;
  300. if (direction) {
  301. maxR = r;
  302. r += (float) (0.3 * pow((double)(STEPS - count) / (double)STEPS, 4.0));
  303. } else {
  304. r -= (float) (maxR / (double)(STEPS));
  305. }
  306. }
  307. fCount += fTimeFactor;
  308. count = (INT)fCount;
  309. if (count > STEPS) {
  310. direction ^= 1;
  311. count = 0;
  312. fCount = 0.0f;
  313. if (direction == 1) {
  314. fRestCount = 10.0f;
  315. r = 0.0f;
  316. rChange = 0.0f;
  317. rChangePrev = 1.0f;
  318. for (i = 0; i < explodeMesh.numFaces; i++) {
  319. ss_matrixIdent(&faceMat[i]);
  320. xstep[i] = (float) (((float)(rand() & 0x3) * PI) / ((float)STEPS + 1.0));
  321. ystep[i] = (float) (((float)(rand() & 0x3) * PI) / ((float)STEPS + 1.0));
  322. zstep[i] = (float) (((float)(rand() & 0x3) * PI) / ((float)STEPS + 1.0));
  323. xrot[i] = 0.0f;
  324. yrot[i] = 0.0f;
  325. zrot[i] = 0.0f;
  326. }
  327. }
  328. }
  329. resting:
  330. ;
  331. }