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.

597 lines
14 KiB

  1. #include "viewer.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #include "trackbal.h"
  6. #define Z_DELTA 1.0
  7. #define Y_DELTA 1.0
  8. #define X_DELTA 1.0
  9. /************************************************************************/
  10. /******************* Function Prototypes ********************************/
  11. /************************************************************************/
  12. void Reshape (GLsizei, GLsizei);
  13. void InitGL(void);
  14. void DoGlStuff(void);
  15. void spin(void);
  16. void InitMatrix (void);
  17. void InitLighting(int, int, GLfloat, BOOL, int, int, int, BOOL);
  18. void InitTexture (void);
  19. void InitDrawing(void);
  20. static void SetViewing(GLsizei, GLsizei, BOOL);
  21. void SetViewWrap(GLsizei, GLsizei);
  22. void initlights(void);
  23. void EnableLighting (void);
  24. void DisableLighting (void);
  25. void PrintStuff (void);
  26. void SetDistance( void );
  27. void Key_up (void);
  28. void Key_down (void);
  29. void Key_i (void);
  30. void Key_x (void);
  31. void Key_X (void);
  32. void Key_y (void);
  33. void Key_Y (void);
  34. void Key_z (void);
  35. void Key_Z (void);
  36. /************************************************************************/
  37. /******************* Globals ********************************************/
  38. /************************************************************************/
  39. static unsigned int stipple[32] = {
  40. 0xAAAAAAAA,
  41. 0x55555555,
  42. 0xAAAAAAAA,
  43. 0x55555555,
  44. 0xAAAAAAAA,
  45. 0x55555555,
  46. 0xAAAAAAAA,
  47. 0x55555555,
  48. 0xAAAAAAAA,
  49. 0x55555555,
  50. 0xAAAAAAAA,
  51. 0x55555555,
  52. 0xAAAAAAAA,
  53. 0x55555555,
  54. 0xAAAAAAAA,
  55. 0x55555555,
  56. 0xAAAAAAAA,
  57. 0x55555555,
  58. 0xAAAAAAAA,
  59. 0x55555555,
  60. 0xAAAAAAAA,
  61. 0x55555555,
  62. 0xAAAAAAAA,
  63. 0x55555555,
  64. 0xAAAAAAAA,
  65. 0x55555555,
  66. 0xAAAAAAAA,
  67. 0x55555555,
  68. 0xAAAAAAAA,
  69. 0x55555555,
  70. 0xAAAAAAAA,
  71. 0x55555555
  72. };
  73. GLfloat zTrans = 0.0;
  74. /************************************************************************/
  75. /******************* Code ***********************************************/
  76. /************************************************************************/
  77. void InitGL(void)
  78. {
  79. /* Initialize the State */
  80. if (linesmooth_enable)
  81. {
  82. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  83. glEnable(GL_BLEND);
  84. glEnable(GL_LINE_SMOOTH);
  85. }
  86. if (polysmooth_enable)
  87. {
  88. glClearColor(0.0F, 0.0F, 0.0F, 0.0F);
  89. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  90. glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
  91. glBlendFunc(sblendfunc, dblendfunc);
  92. glEnable(GL_BLEND);
  93. glEnable(GL_POLYGON_SMOOTH);
  94. }
  95. if (blend_enable)
  96. {
  97. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  98. glColor4f(1.0F, 1.0F, 1.0F, 0.5F);
  99. glBlendFunc(sblendfunc, dblendfunc);
  100. glEnable(GL_BLEND);
  101. }
  102. else
  103. {
  104. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
  105. glColor3f(1.0F, 1.0F, 1.0F);
  106. }
  107. if (dither_enable)
  108. {
  109. glEnable(GL_DITHER);
  110. }
  111. else
  112. {
  113. glDisable(GL_DITHER);
  114. }
  115. glPolygonMode(GL_FRONT, polymodefront);
  116. glPolygonMode(GL_BACK, polymodeback);
  117. if (linestipple_enable)
  118. {
  119. glLineStipple(1, 0xf0f0);
  120. glEnable(GL_LINE_STIPPLE);
  121. }
  122. if (polystipple_enable)
  123. {
  124. glPolygonStipple((const GLubyte *) stipple);
  125. glEnable(GL_POLYGON_STIPPLE);
  126. }
  127. glShadeModel(shade_model);
  128. #if 1
  129. if (depth_mode)
  130. {
  131. glEnable(GL_DEPTH_TEST);
  132. glDepthFunc(GL_LESS);
  133. glDepthMask(GL_TRUE);
  134. }
  135. else
  136. {
  137. glDisable(GL_DEPTH_TEST);
  138. glDepthMask(GL_FALSE);
  139. }
  140. #endif
  141. glCullFace(cull_face);
  142. if (cull_enable)
  143. glEnable(GL_CULL_FACE);
  144. if (fog_enable)
  145. {
  146. glFogf(GL_FOG_START, 0);
  147. glFogf(GL_FOG_END, 1);
  148. glFogf(GL_FOG_MODE, GL_LINEAR);
  149. glEnable(GL_FOG);
  150. }
  151. InitMatrix ();
  152. if (light_enable)
  153. InitLighting (numInfLights, numLocalLights, localviewmode,
  154. lighttwoside, cmenable, cmface, cmmode, blend_enable);
  155. glFrontFace(front_face);
  156. glDrawBuffer(GL_BACK);
  157. glClearColor ((GLfloat)0.0, (GLfloat)0.3, (GLfloat)0.5, (GLfloat)0.0);
  158. //glClearColor ((GLfloat)0.0, (GLfloat)0.0, (GLfloat)0.0, (GLfloat)0.0);
  159. if (tex_enable)
  160. InitTexture ();
  161. InitDrawing();
  162. SetViewing(g_wi.wSize.cx, g_wi.wSize.cy, TRUE);
  163. }
  164. void SetViewWrap(GLsizei w, GLsizei h)
  165. {
  166. if (!h) return;
  167. SetViewing( w, h, TRUE);
  168. }
  169. void SetViewing(GLsizei w, GLsizei h, BOOL resize)
  170. {
  171. GLfloat l_zoom[3];
  172. if (resize)
  173. {
  174. g_wi.wSize.cx = (int) w;
  175. g_wi.wSize.cy = (int) h;
  176. g_wi.wCenter.x = (int) w/2;
  177. g_wi.wCenter.y = (int) h/2;
  178. g_s.aspect_ratio = g_wi.wSize.cx / g_wi.wSize.cy;
  179. }
  180. for (int i=0; i<3; i++)
  181. l_zoom[i] = (g_s.from[i] - g_s.to[i]) * g_s.zoom + g_s.to[i];
  182. glViewport(0, 0, g_wi.wSize.cx, g_wi.wSize.cy);
  183. glMatrixMode(GL_PROJECTION);
  184. glLoadIdentity();
  185. gluPerspective(g_s.fov, g_s.aspect_ratio,
  186. g_s.hither, g_s.yon * g_s.zoom);
  187. glMatrixMode(GL_MODELVIEW);
  188. glLoadIdentity();
  189. gluLookAt(l_zoom[0], l_zoom[1], l_zoom[2],
  190. g_s.to[0], g_s.to[1], g_s.to[2],
  191. g_s.up[0], g_s.up[1], g_s.up[2]);
  192. }
  193. void InitDrawing(void)
  194. {
  195. initlights();
  196. if (light_enable) EnableLighting ();
  197. else DisableLighting();
  198. //glClearColor ((GLfloat)0.0, (GLfloat)0.3, (GLfloat)0.5, (GLfloat)0.0);
  199. glClearColor ((GLfloat)0.0, (GLfloat)0.0, (GLfloat)0.0, (GLfloat)0.0);
  200. glColor3f(0.2, 0.5, 0.8);
  201. }
  202. void CALLBACK InitMatrix (void)
  203. {
  204. glMatrixMode(GL_PROJECTION);
  205. glLoadIdentity();
  206. glMatrixMode(GL_MODELVIEW);
  207. glLoadIdentity();
  208. }
  209. void CALLBACK InitTexture (void)
  210. {
  211. }
  212. void PrintStuff (void)
  213. {
  214. }
  215. static void ComputeLatestMVMatrix ()
  216. {
  217. POINT pt;
  218. static GLfloat del_x = 0.0, del_y = 0.0;
  219. float matRot[4][4];
  220. if (g_wi.rmouse_down)
  221. {
  222. if (GetCursorPos(&pt))
  223. {
  224. // Subtract current window center to convert to
  225. // window coordinates.
  226. pt.x -= (g_wi.wPosition.x);
  227. pt.y -= (g_wi.wPosition.y);
  228. if (pt.x != (int)g_wi.rmouseX || pt.y != (int)g_wi.rmouseY)
  229. {
  230. del_x = (float) (X_DELTA * (pt.x - (int)g_wi.rmouseX) /
  231. (float) g_wi.wSize.cx);
  232. del_y = (float) (Y_DELTA * (pt.y - (int)g_wi.rmouseY) /
  233. (float) g_wi.wSize.cy);
  234. }
  235. g_s.trans[0] += del_x;
  236. g_s.trans[1] -= del_y;
  237. }
  238. }
  239. if (g_wi.lmouse_down)
  240. {
  241. // Convert to window coordinates.
  242. pt.x -= (g_wi.wPosition.x);
  243. pt.y -= (g_wi.wPosition.y);
  244. if (GetCursorPos(&pt))
  245. {
  246. if (pt.x != (int)g_wi.lmouseX || pt.y != (int)g_wi.lmouseY)
  247. {
  248. trackball(curquat,
  249. 2.0*(g_wi.lmouseX)/g_wi.wSize.cx-1.0,
  250. 2.0*(g_wi.lmouseY)/g_wi.wSize.cy-1.0,
  251. 2.0*(pt.x)/g_wi.wSize.cx-1.0,
  252. 2.0*(g_wi.wSize.cy-pt.y)/g_wi.wSize.cy-1.0);
  253. }
  254. }
  255. }
  256. glTranslatef (g_s.trans[0], g_s.trans[1], g_s.trans[2]);
  257. build_rotmatrix(matRot, curquat);
  258. glMultMatrixf(&(matRot[0][0]));
  259. //glRotatef(g_s.angle, (GLfloat)0.0, (GLfloat)1.0, (GLfloat)0.0);
  260. //trackball_CalcRotMatrix( matRot );
  261. //glMultMatrixf( &(matRot[0][0]) );
  262. // This defines how far away we're looking from
  263. //glRotatef(g_s.angle, (GLfloat)1.0, (GLfloat)1.0, (GLfloat)1.0);
  264. }
  265. void
  266. DoGlStuff( void )
  267. {
  268. HRESULT hr;
  269. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  270. glPushMatrix ();
  271. ComputeLatestMVMatrix ();
  272. if (!pm_ready)
  273. {
  274. if (filled_mode)
  275. auxSolidTeapot (1.0);
  276. if (edge_mode)
  277. auxWireTeapot (1.01);
  278. }
  279. else
  280. {
  281. hr = pPMeshGL->Render ();
  282. if (hr != S_OK)
  283. MessageBox (NULL, "Render failed", "Error",
  284. MB_OK);
  285. }
  286. glPopMatrix ();
  287. glFlush ();
  288. auxSwapBuffers();
  289. PrintStuff ();
  290. }
  291. void CALLBACK Reshape(GLsizei w, GLsizei h)
  292. {
  293. //trackball_Resize( w, h );
  294. trackball (curquat, 0.0, 0.0, 0.0, 0.0);
  295. #if 1
  296. glViewport(0, 0, (GLint)w, (GLint)h);
  297. glMatrixMode(GL_PROJECTION);
  298. glLoadIdentity();
  299. glFrustum(-1, 1, -1, 1, -1, 1000);
  300. // glOrtho(-1, 1, -1, 1, -1, 1000);
  301. #else
  302. if (!h) return;
  303. glViewport(0, 0, w, h);
  304. glMatrixMode(GL_PROJECTION);
  305. glLoadIdentity();
  306. if (w <= h)
  307. glOrtho(-5.0, 5.0, -5.0*(GLfloat)h/(GLfloat)w,
  308. 5.0*(GLfloat)h/(GLfloat)w, -5.0, 5.0);
  309. else
  310. glOrtho(-5.0*(GLfloat)w/(GLfloat)h,
  311. 5.0*(GLfloat)w/(GLfloat)h, -5.0, 5.0, -5.0, 5.0);
  312. #endif
  313. glMatrixMode(GL_MODELVIEW);
  314. glLoadIdentity();
  315. g_wi.wSize.cx = (int) w;
  316. g_wi.wSize.cy = (int) h;
  317. }
  318. void spin(void)
  319. {
  320. DoGlStuff();
  321. }
  322. void
  323. InitLighting(int infiniteLights, int localLights,
  324. GLfloat localViewer, BOOL twoSidedLighting,
  325. int cmenable, int cmface, int cmmode, BOOL blendOn)
  326. {
  327. int i;
  328. GLfloat normFactor;
  329. /* Material settings */
  330. GLfloat materialAmbientColor[4] = {
  331. 0.5F, 0.5F, 0.5F, 1.0F
  332. };
  333. GLfloat materialDiffuseColor[4] = {
  334. 0.7F, 0.7F, 0.7F, 1.0F
  335. };
  336. GLfloat materialSpecularColor[4] = {
  337. 1.0F, 1.0F, 1.0F, 1.0F
  338. };
  339. GLfloat materialShininess[1] = {
  340. 128
  341. };
  342. /* Lighting settings */
  343. GLfloat lightPosition[8][4] = {
  344. { 1.0F, 1.0F, 1.0F, 1.0F },
  345. { 1.0F, 0.0F, 1.0F, 1.0F },
  346. { 1.0F, -1.0F, 1.0F, 1.0F },
  347. { 0.0F, -1.0F, 1.0F, 1.0F },
  348. { -1.0F, -1.0F, 1.0F, 1.0F },
  349. { -1.0F, 0.0F, 1.0F, 1.0F },
  350. { -1.0F, 1.0F, 1.0F, 1.0F },
  351. { 0.0F, 1.0F, 1.0F, 1.0F }
  352. };
  353. GLfloat lightDiffuseColor[8][4] = {
  354. { 1.0F, 1.0F, 1.0F, 1.0F },
  355. { 0.0F, 1.0F, 1.0F, 1.0F },
  356. { 1.0F, 0.0F, 1.0F, 1.0F },
  357. { 1.0F, 1.0F, 0.0F, 1.0F },
  358. { 1.0F, 0.0F, 0.0F, 1.0F },
  359. { 0.0F, 1.0F, 0.0F, 1.0F },
  360. { 0.0F, 0.0F, 1.0F, 1.0F },
  361. { 1.0F, 1.0F, 1.0F, 1.0F }
  362. };
  363. GLfloat lightAmbientColor[4] = {
  364. 0.1F, 0.1F, 0.1F, 1.0F
  365. };
  366. GLfloat lightSpecularColor[4] = {
  367. 1.0F, 1.0F, 1.0F, 1.0F
  368. };
  369. GLfloat lightModelAmbient[4] = {
  370. 0.5F, 0.5F, 0.5F, 1.0F
  371. };
  372. GLfloat alpha = blendOn ? 0.5F : 1.0F;
  373. if (infiniteLights + localLights == 0)
  374. return;
  375. normFactor = 1.0F / (GLfloat)(infiniteLights + localLights);
  376. materialDiffuseColor[3] = alpha;
  377. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, materialDiffuseColor);
  378. materialAmbientColor[3] = alpha;
  379. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, materialAmbientColor);
  380. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, materialSpecularColor);
  381. glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, materialShininess);
  382. lightAmbientColor[0] *= normFactor;
  383. lightAmbientColor[1] *= normFactor;
  384. lightAmbientColor[2] *= normFactor;
  385. for (i = 0; i < localLights + infiniteLights; i++) {
  386. lightPosition[i][3] = (GLfloat)(i < localLights);
  387. lightDiffuseColor[i][0] *= normFactor;
  388. lightDiffuseColor[i][1] *= normFactor;
  389. lightDiffuseColor[i][2] *= normFactor;
  390. glLightfv(GL_LIGHT0 + i, GL_POSITION, lightPosition[i]);
  391. glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lightDiffuseColor[i]);
  392. glLightfv(GL_LIGHT0 + i, GL_AMBIENT, lightAmbientColor);
  393. glLightfv(GL_LIGHT0 + i, GL_SPECULAR, lightSpecularColor);
  394. glEnable(GL_LIGHT0 + i);
  395. }
  396. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lightModelAmbient);
  397. glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, localViewer);
  398. glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, twoSidedLighting);
  399. if (cmenable) {
  400. glColorMaterial(cmface, cmmode);
  401. glEnable(GL_COLOR_MATERIAL);
  402. }
  403. glEnable(GL_LIGHTING);
  404. }
  405. void EnableLighting (void)
  406. {
  407. glEnable(GL_AUTO_NORMAL);
  408. glEnable(GL_NORMALIZE);
  409. // glShadeModel(shade_model);
  410. glEnable(GL_LIGHTING);
  411. glEnable(GL_LIGHT0);
  412. }
  413. void DisableLighting (void)
  414. {
  415. glDisable(GL_AUTO_NORMAL);
  416. glDisable(GL_NORMALIZE);
  417. // glShadeModel(shade_model);
  418. glDisable(GL_LIGHTING);
  419. glDisable(GL_LIGHT0);
  420. }
  421. void initlights(void)
  422. {
  423. GLfloat ambient[] = { 0.2, 0.2, 0.2, 1.0 };
  424. GLfloat position[] = { 0.0, 0.0, 2.0, 0.0 };
  425. GLfloat mat_diffuse[] = { 0.6, 0.6, 0.6, 1.0 };
  426. GLfloat mat_diffuse1[] = { 0.8, 0.5, 0.2, 1.0 };
  427. GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  428. GLfloat mat_shininess[] = { 30.0 };
  429. glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  430. glLightfv(GL_LIGHT0, GL_POSITION, position);
  431. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  432. glMaterialfv(GL_BACK, GL_DIFFUSE, mat_diffuse1);
  433. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  434. glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  435. }
  436. void CALLBACK Key_i (void)
  437. {
  438. g_s.trans[0] = 0.0;
  439. g_s.trans[1] = 0.0;
  440. g_s.trans[2] = 0.0;
  441. g_s.zoom = 1.0;
  442. SetViewing( g_wi.wSize.cx, g_wi.wSize.cy, FALSE);
  443. DoGlStuff ();
  444. }
  445. void CALLBACK Key_z (void)
  446. {
  447. g_s.trans[2] += Z_DELTA;
  448. DoGlStuff ();
  449. }
  450. void CALLBACK Key_Z (void)
  451. {
  452. g_s.trans[2] -= Z_DELTA;
  453. DoGlStuff ();
  454. }
  455. void CALLBACK Key_y (void)
  456. {
  457. g_s.trans[1] += Y_DELTA;
  458. DoGlStuff ();
  459. }
  460. void CALLBACK Key_Y (void)
  461. {
  462. g_s.trans[1] -= Y_DELTA;
  463. DoGlStuff ();
  464. }
  465. void CALLBACK Key_x (void)
  466. {
  467. g_s.trans[0] += X_DELTA;
  468. DoGlStuff ();
  469. }
  470. void CALLBACK Key_X (void)
  471. {
  472. g_s.trans[0] -= X_DELTA;
  473. DoGlStuff ();
  474. }
  475. void CALLBACK Key_up (void)
  476. {
  477. //zTrans += Z_DELTA;
  478. g_s.zoom *= .8f;
  479. if (g_s.zoom < 1.0f && g_s.zoom > .8f)
  480. g_s.zoom = 1.0f;
  481. SetViewing( g_wi.wSize.cx, g_wi.wSize.cy, FALSE);
  482. DoGlStuff ();
  483. }
  484. void CALLBACK Key_down (void)
  485. {
  486. //zTrans -= Z_DELTA;
  487. g_s.zoom *= 1.25f;
  488. if (g_s.zoom > 1.0f && g_s.zoom < 1.25f)
  489. g_s.zoom = 1.0f;
  490. SetViewing( g_wi.wSize.cx, g_wi.wSize.cy, FALSE);
  491. DoGlStuff ();
  492. }