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.

1164 lines
31 KiB

  1. /*
  2. * This program renders a wireframe Bezier surface,
  3. * using two-dimensional evaluators.
  4. */
  5. #include "testres.h"
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <math.h>
  9. #include <malloc.h>
  10. #include <windows.h>
  11. #include <GL/gl.h>
  12. #include <GL/glu.h>
  13. #include <GL/glaux.h>
  14. #include "auxtea.h"
  15. #include "vector.h"
  16. /****************************************************************/
  17. /* Macros */
  18. /****************************************************************/
  19. #define MIN_SAMPLE 5
  20. #define IMAGE_WIDTH 64
  21. #define IMAGE_HEIGHT 64
  22. #define POLY_TEAPOT 11
  23. #define BEZ_TEAPOT 12
  24. #define COORD_SYS 14
  25. #define MAX_NUM_FRAMES 24
  26. #define __glPi ((GLfloat) 3.14159265358979323846)
  27. #define __glDegreesToRadians (__glPi / (GLfloat) 180.0)
  28. /****************************************************************/
  29. /* Typedefs */
  30. /****************************************************************/
  31. typedef struct
  32. {
  33. GLdouble x, y, z;
  34. } Vertex;
  35. enum _render_type {POLYGON_TEAPOT, GLAUX_TEAPOT, BEZIER_TEAPOT,
  36. GLAUX_SSPHERE, GLAUX_WSPHERE, TRIANGLES, LINE, LINES,
  37. SQUARE, EVAL};
  38. /****************************************************************/
  39. /* Globals */
  40. /****************************************************************/
  41. HWND g_hWnd;
  42. HMENU g_hMenu;
  43. HANDLE OutputHandle;
  44. GLenum shade_model = GL_PHONG_WIN;
  45. GLenum phong_hint = GL_NICEST;
  46. GLenum cull_face = GL_BACK;
  47. GLenum front_face = GL_CCW;
  48. BOOL cull_enable = FALSE;
  49. BOOL depth_mode = TRUE;
  50. BOOL timing_enable = FALSE;
  51. BOOL bValidConsole = FALSE;
  52. BOOL doSpin = FALSE;
  53. GLfloat curr_angle=0.0, scale, scale2;
  54. GLint sample = MIN_SAMPLE;
  55. GLint sampleEval = 32;
  56. char drawwithpolys=GL_FALSE ;
  57. BOOL bTexEnable = GL_FALSE, bLightEnable = GL_TRUE;
  58. BOOL bTwoSided = FALSE;
  59. BOOL bColorMaterial = FALSE;
  60. BOOL bNormalize = TRUE;
  61. BOOL bAntiAlias = FALSE;
  62. BOOL bAux = GL_FALSE;
  63. GLfloat sc_x = 1.0, sc_y = 1.0, sc_z = 1.0;
  64. GLint width = IMAGE_WIDTH, height = IMAGE_HEIGHT ;
  65. GLubyte image[3*IMAGE_WIDTH*IMAGE_HEIGHT] ;
  66. GLdouble VTemp ;
  67. GLfloat ctrlpoints[4][4][3] = {
  68. {{-1.5, -1.5, 0.0}, {-0.5, -1.5, 1.0}, {0.5, -1.5, 1.0}, {1.5, -1.5, 0.0}},
  69. {{-1.5, -0.5, 1.0}, {-0.5, -0.5, 3.0}, {0.5, -0.5, 3.0}, {1.5, -0.5, 0.0}},
  70. {{-1.5, 0.5, 1.0}, {-0.5, 0.5, 3.0}, {0.5, 0.5, 3.0}, {1.5, 0.5, 0.0}},
  71. {{-1.5, 1.5, 0.0}, {-0.5, 1.5, 1.0}, {0.5, 1.5, 1.0}, {1.5, 1.5, 0.0}}
  72. };
  73. #if 0
  74. GLfloat ctrlpoints[4][4][3] = {
  75. {{-1.5, -1.5, 4.0}, {-0.5, -1.5, 2.0},
  76. {0.5, -1.5, -1.0}, {1.5, -1.5, 2.0}},
  77. {{-1.5, -0.5, 1.0}, {-0.5, -0.5, 3.0},
  78. {0.5, -0.5, 0.0}, {1.5, -0.5, -1.0}},
  79. {{-1.5, 0.5, 4.0}, {-0.5, 0.5, 0.0},
  80. {0.5, 0.5, 3.0}, {1.5, 0.5, 4.0}},
  81. {{-1.5, 1.5, -2.0}, {-0.5, 1.5, -2.0},
  82. {0.5, 1.5, 0.0}, {1.5, 1.5, -1.0}}
  83. };
  84. #endif
  85. static DWORD startelapsed, endelapsed;
  86. float bsecs=0.0, bavg=0.0, bcum=0.0;
  87. DWORD bnum_frames=0;
  88. float psecs=0.0, pavg=0.0, pcum=0.0;
  89. DWORD pnum_frames=0;
  90. GLfloat texpts[2][2][2] = {{{0.0, 0.0}, {0.0, 1.0}},
  91. {{1.0, 0.0}, {1.0, 1.0}}} ;
  92. enum _render_type render_type = TRIANGLES;
  93. /****************************************************************/
  94. /* Prototypes */
  95. /****************************************************************/
  96. void display() ;
  97. void BezTeapot(GLfloat) ;
  98. void MouseWheel(AUX_MOUSEWHEEL_EVENTREC *);
  99. void CustomizeWnd (HINSTANCE);
  100. LONG APIENTRY MyWndProc(HWND, UINT, UINT, LONG);
  101. void SubclassWindow (HWND, WNDPROC);
  102. ULONG DbgPrint(PSZ Format, ...);
  103. void DrawTris(void);
  104. void DrawSquare(void);
  105. void DrawLines(void);
  106. void DrawEval(void);
  107. void initEval(void);
  108. void init_texture(void);
  109. /****************************************************************/
  110. /* Code */
  111. /****************************************************************/
  112. void
  113. MouseWheel(AUX_MOUSEWHEEL_EVENTREC *mw)
  114. //MouseWheel (int fwKeys, int zDelta, int xPos, int yPos)
  115. {
  116. if (mw->zDelta > 0)
  117. {
  118. sc_x *=1.1;
  119. sc_y *=1.1;
  120. sc_z *=1.1;
  121. }
  122. else
  123. {
  124. sc_x *=0.9;
  125. sc_y *=0.9;
  126. sc_z *=0.9;
  127. }
  128. }
  129. void loadImage(void)
  130. {
  131. int i, j ;
  132. GLubyte col = 0 ;
  133. for(i=0; i<width; i++)
  134. for(j=0; j<height; j++)
  135. if( ((j/8)%2 && (i/8)%2) || (!((j/8)%2) && !((i/8)%2)) )
  136. {
  137. image[(i*height+j)*3] = 127 ;
  138. image[(i*height+j)*3+1] = 127 ;
  139. image[(i*height+j)*3+2] = 127 ;
  140. }
  141. else
  142. {
  143. image[(i*height+j)*3] = 0 ;
  144. image[(i*height+j)*3+1] = 0 ;
  145. image[(i*height+j)*3+2] = 0 ;
  146. }
  147. }
  148. void PolyTeapot(GLfloat scale)
  149. {
  150. FILE *fd;
  151. Vertex *vertices;
  152. int index[3];
  153. int i, dummy;
  154. int num_vertices, num_triangles;
  155. Vertex v1, v2, normal ;
  156. fd = fopen("teapot.geom","r");
  157. fscanf(fd, "%d %d %d", &num_vertices, &num_triangles, &dummy);
  158. vertices = (Vertex *)malloc(num_vertices*sizeof(Vertex));
  159. for (i=0; i < num_vertices; i++)
  160. fscanf(fd, "%lf %lf %lf", &vertices[i].x, &vertices[i].y,
  161. &vertices[i].z);
  162. glNewList(POLY_TEAPOT, GL_COMPILE) ;
  163. glPushMatrix ();
  164. glScalef (scale, scale, scale);
  165. glTranslatef (0.0, -1.4, 0.0);
  166. for (i=0; i < num_triangles; i++)
  167. {
  168. //glBegin(GL_POLYGON);
  169. glBegin(GL_TRIANGLES);
  170. fscanf(fd, "%d %d %d %d", &dummy, &index[0], &index[1], &index[2]);
  171. index[0]--; index[1]--; index[2]--;
  172. VSub(v1, vertices[index[1]], vertices[index[0]]) ;
  173. VSub(v2, vertices[index[2]], vertices[index[1]]) ;
  174. VCross(normal, v2, v1) ;
  175. VNormalize(normal, normal) ;
  176. glNormal3d(normal.x, normal.y, normal.z) ;
  177. glVertex3d(vertices[index[0]].x, vertices[index[0]].y,
  178. vertices[index[0]].z);
  179. glVertex3d(vertices[index[1]].x, vertices[index[1]].y,
  180. vertices[index[1]].z);
  181. glVertex3d(vertices[index[2]].x, vertices[index[2]].y,
  182. vertices[index[2]].z);
  183. glEnd();
  184. }
  185. glPopMatrix ();
  186. glEndList() ;
  187. DbgPrint("Done, Initializing the displaylist\n") ;
  188. fclose(fd);
  189. }
  190. void spin(void)
  191. {
  192. curr_angle+=10.0 ;
  193. if(curr_angle > 360.0) curr_angle-=360.0 ;
  194. display() ;
  195. if (drawwithpolys)
  196. {
  197. pnum_frames++;
  198. pcum += psecs;
  199. if (pnum_frames == MAX_NUM_FRAMES)
  200. {
  201. pavg = pcum/(float)MAX_NUM_FRAMES;
  202. DbgPrint("Average over %d poly frames: %f secs per frame\n",
  203. MAX_NUM_FRAMES, pavg);
  204. pnum_frames = 0;
  205. pcum = 0.0; pavg = 0.0;
  206. }
  207. }
  208. else
  209. {
  210. bnum_frames++;
  211. bcum += bsecs;
  212. if (bnum_frames == MAX_NUM_FRAMES)
  213. {
  214. bavg = bcum/(float)MAX_NUM_FRAMES;
  215. DbgPrint("Average over %d poly frames: %f secs per frame\n",
  216. MAX_NUM_FRAMES, bavg);
  217. bnum_frames = 0;
  218. bcum = 0.0; bavg = 0.0;
  219. }
  220. }
  221. }
  222. void toggle(void)
  223. {
  224. doSpin = !(doSpin) ;
  225. if (doSpin)
  226. auxIdleFunc(spin) ;
  227. else
  228. auxIdleFunc(NULL) ;
  229. }
  230. void scale_up(void)
  231. {
  232. sc_x *=1.1;
  233. sc_y *=1.1;
  234. sc_z *=1.1;
  235. }
  236. void scale_down(void)
  237. {
  238. sc_x *=0.9;
  239. sc_y *=0.9;
  240. sc_z *=0.9;
  241. }
  242. void beztype(void)
  243. {
  244. bAux = !(bAux) ;
  245. }
  246. void cull (void)
  247. {
  248. if (cull_enable)
  249. glEnable(GL_CULL_FACE);
  250. else
  251. glDisable(GL_CULL_FACE);
  252. }
  253. void depth(void)
  254. {
  255. if (depth_mode)
  256. {
  257. glEnable(GL_DEPTH_TEST);
  258. glDepthFunc(GL_LESS);
  259. glDepthMask(GL_TRUE);
  260. }
  261. else
  262. {
  263. glDisable(GL_DEPTH_TEST);
  264. glDepthMask(GL_FALSE);
  265. }
  266. }
  267. void lighting(void)
  268. {
  269. if (bLightEnable)
  270. {
  271. glEnable(GL_LIGHTING);
  272. glEnable(GL_LIGHT0);
  273. }
  274. else
  275. {
  276. glDisable(GL_LIGHTING);
  277. glDisable(GL_LIGHT0);
  278. }
  279. }
  280. void texture(void)
  281. {
  282. if (bTexEnable)
  283. glEnable(GL_TEXTURE_2D);
  284. else
  285. glDisable(GL_TEXTURE_2D);
  286. }
  287. void init_texture(void)
  288. {
  289. /* Texture stuff */
  290. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL) ;
  291. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) ;
  292. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) ;
  293. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) ;
  294. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) ;
  295. glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB,
  296. GL_UNSIGNED_BYTE, image) ;
  297. texture();
  298. }
  299. void initlights(void)
  300. {
  301. //GLfloat ambient[] = { 0.2, 0.2, 0.2, 1.0 };
  302. GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  303. GLfloat position[] = { 0.0, 0.0, 2.0, 0.0 };
  304. //GLfloat position[] = { 0.0, 0.0, 2.0, 0.0 };
  305. GLfloat mat_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  306. GLfloat mat_diffuse[] = { 0.6, 0.6, 0.6, 1.0 };
  307. GLfloat mat_diffuse1[] = { 0.8, 0.5, 0.2, 1.0 };
  308. GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  309. GLfloat mat_shininess[] = { 60.0 };
  310. glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  311. glLightfv(GL_LIGHT0, GL_POSITION, position);
  312. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
  313. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
  314. //glMaterialfv(GL_BACK_AND_BACK, GL_DIFFUSE, mat_diffuse1);
  315. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
  316. glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
  317. }
  318. void DrawCoordSys(GLfloat size)
  319. {
  320. glNewList(COORD_SYS, GL_COMPILE) ;
  321. glDisable(GL_LIGHTING);
  322. glDisable(GL_LIGHT0);
  323. glDisable(GL_TEXTURE_2D);
  324. glBegin(GL_LINES) ;
  325. glColor3f(1.0, 0.0, 0.0) ;
  326. glVertex3f(0.0, 0.0, 0.0) ;
  327. glVertex3f(1.0*size, 0.0, 0.0) ;
  328. glColor3f(0.0, 1.0, 0.0) ;
  329. glVertex3f(0.0, 0.0, 0.0) ;
  330. glVertex3f(0.0, 1.0*size, 0.0) ;
  331. glColor3f(0.0, 0.0, 1.0) ;
  332. glVertex3f(0.0, 0.0, 0.0) ;
  333. glVertex3f(0.0, 0.0, 1.0*size) ;
  334. glEnd() ;
  335. glEndList() ;
  336. }
  337. void display(void)
  338. {
  339. int i, j;
  340. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  341. glPushMatrix ();
  342. //glRotatef(curr_angle, 1.0, 1.0, 1.0);
  343. glRotatef(curr_angle, 0.0, 0.0, 1.0);
  344. glScalef (sc_x, sc_y, sc_z);
  345. if (timing_enable)
  346. startelapsed = GetTickCount();
  347. switch (render_type)
  348. {
  349. case GLAUX_TEAPOT:
  350. auxSolidTeapot(scale2);
  351. break;
  352. case BEZIER_TEAPOT:
  353. BezTeapot(scale) ;
  354. break;
  355. case POLYGON_TEAPOT:
  356. glCallList(POLY_TEAPOT) ;
  357. break;
  358. case GLAUX_SSPHERE:
  359. auxSolidSphere(scale2);
  360. break;
  361. case GLAUX_WSPHERE:
  362. auxWireSphere(scale2);
  363. break;
  364. case TRIANGLES:
  365. DrawTris();
  366. break;
  367. case SQUARE:
  368. DrawSquare();
  369. break;
  370. case LINES:
  371. DrawLines();
  372. break;
  373. case EVAL:
  374. DrawEval();
  375. break;
  376. default:
  377. break;
  378. };
  379. glFlush() ;
  380. if (timing_enable)
  381. {
  382. endelapsed = GetTickCount();
  383. bsecs = (endelapsed - startelapsed) / (float)1000;
  384. DbgPrint ("Time to draw bezier tpot frame = %f\n", bsecs);
  385. }
  386. glPopMatrix ();
  387. auxSwapBuffers();
  388. }
  389. void myinit(void)
  390. {
  391. glClearColor (0.7, 0.3, 0.1, 1.0);
  392. glColor3f(0.2, 0.5, 0.8);
  393. loadImage() ;
  394. //DrawCoordSys(5.0) ;
  395. // BezTeapot(scale) ;
  396. PolyTeapot(1.0) ;
  397. auxSolidTeapot(scale2);
  398. glEnable(GL_AUTO_NORMAL);
  399. if (bNormalize)
  400. glEnable(GL_NORMALIZE);
  401. else
  402. glDisable(GL_NORMALIZE);
  403. glShadeModel(shade_model);
  404. //glHint (GL_PHONG_HINT_WIN, phong_hint);
  405. glFrontFace(front_face);
  406. glCullFace(cull_face);
  407. if (bAntiAlias)
  408. {
  409. glEnable (GL_POLYGON_SMOOTH);
  410. glEnable (GL_LINE_SMOOTH);
  411. glEnable (GL_POINT_SMOOTH);
  412. }
  413. else
  414. {
  415. glDisable (GL_POLYGON_SMOOTH);
  416. glDisable (GL_LINE_SMOOTH);
  417. glDisable (GL_POINT_SMOOTH);
  418. }
  419. depth ();
  420. cull ();
  421. initlights ();
  422. lighting ();
  423. init_texture ();
  424. initEval ();
  425. if (bTwoSided)
  426. glLightModeli (GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
  427. else
  428. glLightModeli (GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
  429. glColorMaterial (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
  430. if (bColorMaterial)
  431. glEnable(GL_COLOR_MATERIAL);
  432. else
  433. glDisable(GL_COLOR_MATERIAL);
  434. }
  435. void myReshape(GLsizei w, GLsizei h)
  436. {
  437. glViewport(0, 0, w, h);
  438. glMatrixMode(GL_PROJECTION);
  439. glLoadIdentity();
  440. if (w <= h)
  441. glOrtho(-4.0, 4.0, -4.0*(GLfloat)h/(GLfloat)w,
  442. 4.0*(GLfloat)h/(GLfloat)w, -4.0, 4.0);
  443. else
  444. glOrtho(-4.0*(GLfloat)w/(GLfloat)h,
  445. 4.0*(GLfloat)w/(GLfloat)h, -4.0, 4.0, -4.0, 4.0);
  446. glMatrixMode(GL_MODELVIEW);
  447. glLoadIdentity();
  448. }
  449. void initEval (void)
  450. {
  451. glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4,
  452. 0, 1, 12, 4, &ctrlpoints[0][0][0]);
  453. glEnable(GL_MAP2_VERTEX_3);
  454. glMapGrid2f((GLfloat)sampleEval, 0.0, 1.0, (GLfloat)sampleEval, 0.0, 1.0);
  455. glEnable(GL_AUTO_NORMAL);
  456. }
  457. BOOL CheckExtension(char *extName, char *extString)
  458. {
  459. /*
  460. ** Search for extName in the extensions string. Use of strstr()
  461. ** is not sufficient because extension names can be prefixes of
  462. ** other extension names. Could use strtok() but the constant
  463. ** string returned by glGetString can be in read-only memory.
  464. */
  465. char *p = (char *)extString;
  466. char *end;
  467. int extNameLen;
  468. extNameLen = strlen(extName);
  469. end = p + strlen(p);
  470. while (p < end) {
  471. int n = strcspn(p, " ");
  472. if ((extNameLen == n) && (strncmp(extName, p, n) == 0)) {
  473. return GL_TRUE;
  474. }
  475. p += (n + 1);
  476. }
  477. return GL_FALSE;
  478. }
  479. //*------------------------------------------------------------------------
  480. //| WinMain:
  481. //| Parameters:
  482. //| hInstance - Handle to current Data Segment
  483. //| hPrevInstance - Always NULL in Win32
  484. //| lpszCmdLine - Pointer to command line info
  485. //| nCmdShow - Integer value specifying how to start app.,
  486. //| (Iconic [7] or Normal [1,5])
  487. //*------------------------------------------------------------------------
  488. int WINAPI WinMain (HINSTANCE hInstance,
  489. HINSTANCE hPrevInstance,
  490. LPSTR lpszCmdLine,
  491. int nCmdShow)
  492. {
  493. int nReturn = 0;
  494. char *exts;
  495. const char *ext_string;
  496. int new_ext_supported = GL_FALSE;
  497. scale = 1.0;
  498. scale2 = 2.0;
  499. auxInitDisplayMode (AUX_RGB | AUX_DEPTH16 | AUX_DOUBLE);
  500. //auxInitDisplayMode (AUX_RGB | AUX_DEPTH16 | AUX_SINGLE);
  501. auxInitPosition (10, 10, 500, 500);
  502. auxInitWindow ("Test");
  503. myinit();
  504. exts = (char *) glGetString (GL_EXTENSIONS);
  505. DbgPrint ("The extensions available are : %s\n", exts);
  506. if ( CheckExtension("GL_EXT_bgra", exts) )
  507. DbgPrint ("GL_EXT_new_extension available\n");
  508. else
  509. DbgPrint ("GL_EXT_new_extension not available\n");
  510. CustomizeWnd(hInstance);
  511. //bValidConsole = AllocConsole();
  512. if (bValidConsole)
  513. OutputHandle = GetStdHandle( STD_OUTPUT_HANDLE );
  514. auxReshapeFunc (myReshape);
  515. if (doSpin)
  516. auxIdleFunc(spin) ;
  517. else
  518. auxIdleFunc(NULL) ;
  519. auxKeyFunc(AUX_p, toggle) ;
  520. auxKeyFunc(AUX_l, lighting) ;
  521. auxKeyFunc(AUX_t, texture) ;
  522. auxKeyFunc(AUX_a, beztype) ;
  523. auxKeyFunc(AUX_j, scale_up) ;
  524. auxKeyFunc(AUX_k, scale_down) ;
  525. auxMouseWheelFunc(MouseWheel) ;
  526. auxMainLoop(display);
  527. return (0);
  528. }
  529. void BezTeapot(GLfloat scale)
  530. {
  531. float p[4][4][3], q[4][4][3], r[4][4][3], s[4][4][3];
  532. long i, j, k, l;
  533. //glNewList(BEZ_TEAPOT, GL_COMPILE);
  534. glPushMatrix ();
  535. glRotatef(270.0, 1.0, 0.0, 0.0);
  536. glScalef (scale, scale, scale);
  537. glTranslatef ((GLfloat)0.0, (GLfloat)0.0, (GLfloat)-1.5);
  538. for (i = 0; i < 10; i++)
  539. {
  540. for (j = 0; j < 4; j++)
  541. for (k = 0; k < 4; k++)
  542. for (l = 0; l < 3; l++)
  543. {
  544. p[j][k][l] = cpdata[patchdata[i][j*4+k]][l];
  545. q[j][k][l] = cpdata[patchdata[i][j*4+(3-k)]][l];
  546. if (l == 1) q[j][k][l] *= (float)-1.0;
  547. if (i < 6) {
  548. r[j][k][l] = cpdata[patchdata[i][j*4+(3-k)]][l];
  549. if (l == 0) r[j][k][l] *= (float)-1.0;
  550. s[j][k][l] = cpdata[patchdata[i][j*4+k]][l];
  551. if (l == 0) s[j][k][l] *= (float)-1.0;
  552. if (l == 1) s[j][k][l] *= (float)-1.0;
  553. }
  554. }
  555. glMap2f(GL_MAP2_TEXTURE_COORD_2, 0, 1, 2, 2, 0, 1, 4, 2, &texpts[0][0][0]);
  556. glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &p[0][0][0]);
  557. glEnable(GL_MAP2_VERTEX_3); glEnable(GL_MAP2_TEXTURE_COORD_2);
  558. glMapGrid2f((GLfloat)sample, 0.0, 1.0, (GLfloat)sample, 0.0, 1.0);
  559. glEvalMesh2(GL_FILL, 0, sample, 0, sample);
  560. glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &q[0][0][0]);
  561. glEvalMesh2(GL_FILL, 0, sample, 0, sample);
  562. if (i < 6) {
  563. glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &r[0][0][0]);
  564. glEvalMesh2(GL_FILL, 0, sample, 0, sample);
  565. glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &s[0][0][0]);
  566. glEvalMesh2(GL_FILL, 0, sample, 0, sample);
  567. }
  568. }
  569. glDisable(GL_MAP2_VERTEX_3); glDisable(GL_MAP2_TEXTURE_COORD_2);
  570. glPopMatrix ();
  571. // glEndList();
  572. }
  573. void CustomizeWnd(HINSTANCE hInstance)
  574. {
  575. if ((g_hWnd = auxGetHWND()) == NULL)
  576. {
  577. OutputDebugString("auxGetHWND() failed\n");
  578. return;
  579. }
  580. SubclassWindow (g_hWnd, (WNDPROC) MyWndProc);
  581. SendMessage(g_hWnd, WM_USER, 0L, 0L);
  582. g_hMenu = LoadMenu(GetModuleHandle(NULL), MAKEINTRESOURCE(APPMENU));
  583. SetMenu(g_hWnd, g_hMenu);
  584. DrawMenuBar(g_hWnd);
  585. return;
  586. }
  587. /**************************************************************************\
  588. * function: SubclassWindow
  589. *
  590. * input parameters:
  591. * hwnd - window handle to be subclassed,
  592. * SubclassWndProc - the new window procedure.
  593. *
  594. \**************************************************************************/
  595. VOID SubclassWindow (HWND hwnd, WNDPROC SubclassWndProc)
  596. {
  597. LONG pfnOldProc;
  598. pfnOldProc = GetWindowLong (hwnd, GWL_WNDPROC);
  599. SetWindowLong (hwnd, GWL_USERDATA, (LONG) pfnOldProc);
  600. SetWindowLong (hwnd, GWL_WNDPROC, (LONG) SubclassWndProc);
  601. }
  602. /**************************************************************************\
  603. *
  604. * function: MyWndProc
  605. *
  606. * input parameters: normal window procedure parameters.
  607. *
  608. \**************************************************************************/
  609. LONG APIENTRY MyWndProc(HWND hwnd, UINT message, UINT wParam, LONG lParam)
  610. {
  611. WNDPROC pfnOldProc;
  612. static UINT uiTmID = 0;
  613. pfnOldProc = (WNDPROC) GetWindowLong (hwnd, GWL_USERDATA);
  614. switch (message) {
  615. case WM_INITMENUPOPUP:
  616. CheckMenuItem (g_hMenu, MENU_FLAT,
  617. ((shade_model == GL_FLAT)? MF_CHECKED:MF_UNCHECKED));
  618. CheckMenuItem (g_hMenu, MENU_GOURAUD,
  619. ((shade_model == GL_SMOOTH)? MF_CHECKED:MF_UNCHECKED));
  620. CheckMenuItem (g_hMenu, MENU_PHONG_NICEST,
  621. (((shade_model == GL_PHONG_WIN) &&
  622. (phong_hint == GL_NICEST)) ? MF_CHECKED:MF_UNCHECKED));
  623. CheckMenuItem (g_hMenu, MENU_PHONG_FASTEST,
  624. (((shade_model == GL_PHONG_WIN) &&
  625. (phong_hint == GL_FASTEST)) ? MF_CHECKED:MF_UNCHECKED));
  626. CheckMenuItem (g_hMenu, MENU_PHONG_DONT_CARE,
  627. (((shade_model == GL_PHONG_WIN) &&
  628. (phong_hint == GL_DONT_CARE)) ? MF_CHECKED:MF_UNCHECKED));
  629. CheckMenuItem (g_hMenu, MENU_LIGHTING,
  630. ((bLightEnable)? MF_CHECKED:MF_UNCHECKED));
  631. if (!bLightEnable)
  632. {
  633. EnableMenuItem (g_hMenu, MENU_TWO_SIDED, MF_GRAYED);
  634. EnableMenuItem (g_hMenu, MENU_COLORMATERIAL, MF_GRAYED);
  635. EnableMenuItem (g_hMenu, MENU_NORMALIZE, MF_GRAYED);
  636. }
  637. else
  638. {
  639. EnableMenuItem (g_hMenu, MENU_TWO_SIDED, MF_ENABLED);
  640. EnableMenuItem (g_hMenu, MENU_COLORMATERIAL, MF_ENABLED);
  641. EnableMenuItem (g_hMenu, MENU_NORMALIZE, MF_ENABLED);
  642. }
  643. CheckMenuItem (g_hMenu, MENU_TWO_SIDED,
  644. ((bTwoSided)? MF_CHECKED:MF_UNCHECKED));
  645. CheckMenuItem (g_hMenu, MENU_COLORMATERIAL,
  646. ((bColorMaterial)? MF_CHECKED:MF_UNCHECKED));
  647. CheckMenuItem (g_hMenu, MENU_NORMALIZE,
  648. ((bNormalize)? MF_CHECKED:MF_UNCHECKED));
  649. CheckMenuItem (g_hMenu, MENU_POLY,
  650. ((render_type == POLYGON_TEAPOT)?
  651. MF_CHECKED:MF_UNCHECKED));
  652. CheckMenuItem (g_hMenu, MENU_BEZ,
  653. ((render_type == BEZIER_TEAPOT)?
  654. MF_CHECKED:MF_UNCHECKED));
  655. CheckMenuItem (g_hMenu, MENU_AUX,
  656. ((render_type == GLAUX_TEAPOT)?
  657. MF_CHECKED:MF_UNCHECKED));
  658. CheckMenuItem (g_hMenu, MENU_SOLID_SPHERE,
  659. ((render_type == GLAUX_SSPHERE)?
  660. MF_CHECKED:MF_UNCHECKED));
  661. CheckMenuItem (g_hMenu, MENU_WIRE_SPHERE,
  662. ((render_type == GLAUX_WSPHERE)?
  663. MF_CHECKED:MF_UNCHECKED));
  664. CheckMenuItem (g_hMenu, MENU_SQUARE,
  665. ((render_type == SQUARE)?
  666. MF_CHECKED:MF_UNCHECKED));
  667. CheckMenuItem (g_hMenu, MENU_TRIANGLES,
  668. ((render_type == TRIANGLES)?
  669. MF_CHECKED:MF_UNCHECKED));
  670. CheckMenuItem (g_hMenu, MENU_LINES,
  671. ((render_type == LINES)?
  672. MF_CHECKED:MF_UNCHECKED));
  673. CheckMenuItem (g_hMenu, MENU_EVAL,
  674. ((render_type == EVAL)?
  675. MF_CHECKED:MF_UNCHECKED));
  676. CheckMenuItem (g_hMenu, MENU_DEPTH,
  677. ((depth_mode)? MF_CHECKED:MF_UNCHECKED));
  678. CheckMenuItem (g_hMenu, MENU_ANTIALIAS,
  679. ((bAntiAlias)? MF_CHECKED:MF_UNCHECKED));
  680. CheckMenuItem (g_hMenu, MENU_TIMING,
  681. ((timing_enable)? MF_CHECKED:MF_UNCHECKED));
  682. CheckMenuItem (g_hMenu, MENU_TEXTURE_TOGGLE,
  683. ((bTexEnable)? MF_CHECKED:MF_UNCHECKED));
  684. EnableMenuItem (g_hMenu, MENU_TEXTURE_SWAP, MF_GRAYED);
  685. EnableMenuItem (g_hMenu, MENU_POINT_FILTER, MF_GRAYED);
  686. EnableMenuItem (g_hMenu, MENU_LINEAR_FILTER, MF_GRAYED);
  687. if (!cull_enable)
  688. {
  689. EnableMenuItem (g_hMenu, MENU_FRONTFACE, MF_GRAYED);
  690. EnableMenuItem (g_hMenu, MENU_BACKFACE, MF_GRAYED);
  691. CheckMenuItem (g_hMenu, MENU_CULL, MF_UNCHECKED);
  692. }
  693. else
  694. {
  695. EnableMenuItem (g_hMenu, MENU_FRONTFACE, MF_ENABLED);
  696. EnableMenuItem (g_hMenu, MENU_BACKFACE, MF_ENABLED);
  697. CheckMenuItem (g_hMenu, MENU_CULL, MF_CHECKED);
  698. }
  699. CheckMenuItem (g_hMenu, MENU_BACKFACE,
  700. ((cull_face==GL_BACK)? MF_CHECKED:MF_UNCHECKED));
  701. CheckMenuItem (g_hMenu, MENU_FRONTFACE,
  702. ((cull_face==GL_FRONT)? MF_CHECKED:MF_UNCHECKED));
  703. CheckMenuItem (g_hMenu, MENU_CCW,
  704. ((front_face==GL_CCW)? MF_CHECKED:MF_UNCHECKED));
  705. CheckMenuItem (g_hMenu, MENU_CW,
  706. ((front_face==GL_CW)? MF_CHECKED:MF_UNCHECKED));
  707. break;
  708. case WM_COMMAND:
  709. switch (LOWORD(wParam))
  710. {
  711. case MENU_CULL:
  712. cull_enable = !cull_enable;
  713. cull ();
  714. break;
  715. case MENU_BACKFACE:
  716. cull_face = GL_BACK;
  717. if (cull_enable) glCullFace (cull_face);
  718. break;
  719. case MENU_FRONTFACE:
  720. cull_face = GL_FRONT;
  721. if (cull_enable) glCullFace (cull_face);
  722. break;
  723. case MENU_CCW:
  724. front_face = GL_CCW;
  725. glFrontFace (front_face);
  726. break;
  727. case MENU_CW:
  728. front_face = GL_CW;
  729. glFrontFace (front_face);
  730. break;
  731. case MENU_FLAT:
  732. shade_model = GL_FLAT;
  733. glShadeModel (GL_FLAT);
  734. break;
  735. case MENU_GOURAUD:
  736. shade_model = GL_SMOOTH;
  737. glShadeModel (GL_SMOOTH);
  738. break;
  739. case MENU_PHONG_NICEST:
  740. shade_model = GL_PHONG_WIN;
  741. phong_hint = GL_NICEST;
  742. glShadeModel (GL_PHONG_WIN);
  743. glHint (GL_PHONG_HINT_WIN, GL_NICEST);
  744. break;
  745. case MENU_PHONG_FASTEST:
  746. shade_model = GL_PHONG_WIN;
  747. phong_hint = GL_FASTEST;
  748. glShadeModel (GL_PHONG_WIN);
  749. glHint (GL_PHONG_HINT_WIN, GL_FASTEST);
  750. break;
  751. case MENU_PHONG_DONT_CARE:
  752. shade_model = GL_PHONG_WIN;
  753. phong_hint = GL_DONT_CARE;
  754. glShadeModel (GL_PHONG_WIN);
  755. glHint (GL_PHONG_HINT_WIN, GL_DONT_CARE);
  756. break;
  757. case MENU_LIGHTING:
  758. bLightEnable = !bLightEnable;
  759. lighting ();
  760. break;
  761. case MENU_COLORMATERIAL:
  762. bColorMaterial = !bColorMaterial;
  763. if (bColorMaterial)
  764. glEnable (GL_COLOR_MATERIAL);
  765. else
  766. glDisable (GL_COLOR_MATERIAL);
  767. break;
  768. case MENU_NORMALIZE:
  769. bNormalize = !bNormalize;
  770. if (bNormalize)
  771. glEnable (GL_NORMALIZE);
  772. else
  773. glDisable (GL_NORMALIZE);
  774. break;
  775. case MENU_TWO_SIDED:
  776. bTwoSided = !bTwoSided;
  777. if (bTwoSided)
  778. glLightModeli (GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
  779. else
  780. glLightModeli (GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
  781. case MENU_DEPTH:
  782. depth_mode = !depth_mode;
  783. depth ();
  784. break;
  785. case MENU_ANTIALIAS:
  786. bAntiAlias = !bAntiAlias;
  787. if (bAntiAlias)
  788. {
  789. glEnable (GL_POLYGON_SMOOTH);
  790. glEnable (GL_LINE_SMOOTH);
  791. glEnable (GL_POINT_SMOOTH);
  792. }
  793. else
  794. {
  795. glDisable (GL_POLYGON_SMOOTH);
  796. glDisable (GL_LINE_SMOOTH);
  797. glDisable (GL_POINT_SMOOTH);
  798. }
  799. break;
  800. case MENU_TEXTURE_TOGGLE:
  801. bTexEnable = !bTexEnable;
  802. texture ();
  803. break;
  804. case MENU_TIMING:
  805. timing_enable = !timing_enable;
  806. break;
  807. case MENU_POLY:
  808. render_type = POLYGON_TEAPOT;
  809. break;
  810. case MENU_AUX:
  811. render_type = GLAUX_TEAPOT;
  812. break;
  813. case MENU_SOLID_SPHERE:
  814. render_type = GLAUX_SSPHERE;
  815. break;
  816. case MENU_WIRE_SPHERE:
  817. render_type = GLAUX_WSPHERE;
  818. break;
  819. case MENU_TRIANGLES:
  820. render_type = TRIANGLES;
  821. break;
  822. case MENU_SQUARE:
  823. render_type = SQUARE;
  824. break;
  825. case MENU_LINES:
  826. render_type = LINES;
  827. break;
  828. case MENU_EVAL:
  829. render_type = EVAL;
  830. break;
  831. case MENU_BEZ:
  832. render_type = BEZIER_TEAPOT;
  833. break;
  834. default:
  835. MessageBox (NULL, "Not yet implemented\r\n", "Warning", MB_OK);
  836. return 0;
  837. }
  838. if (!doSpin)
  839. InvalidateRect (hwnd, NULL, TRUE);
  840. break;
  841. case WM_USER:
  842. case WM_DESTROY:
  843. default:
  844. return (pfnOldProc)(hwnd, message, wParam, lParam);
  845. } /* end switch */
  846. //DoGlStuff ();
  847. return 0;
  848. }
  849. /******************************Public*Routine******************************\
  850. * DbgPrint
  851. *
  852. * Formatted string output to the debugger.
  853. *
  854. \**************************************************************************/
  855. ULONG
  856. DbgPrint(PCH DebugMessage, ...)
  857. {
  858. va_list ap;
  859. char buffer[256];
  860. BOOL success;
  861. DWORD num_written;
  862. va_start(ap, DebugMessage);
  863. vsprintf(buffer, DebugMessage, ap);
  864. if (!bValidConsole)
  865. OutputDebugStringA(buffer);
  866. else
  867. success = WriteConsole (OutputHandle, buffer, strlen(buffer),
  868. &num_written, NULL);
  869. va_end(ap);
  870. return(0);
  871. }
  872. void
  873. DrawTris(void)
  874. {
  875. GLfloat mat_specular[] = { 1.0, 0.0, 0.0, 0.0 };
  876. glBegin (GL_TRIANGLES);
  877. glColor3f(1.0, 0.0, 0.0);
  878. //glNormal3d (-0.75, 0.01, 0.0);
  879. glNormal3d (0.0, 0.0, 1.0);
  880. glVertex3f (-3.7, 0.1, -0.1);
  881. glColor3f(0.0, 1.0, 0.0);
  882. //glNormal3d (0.8, 0.0, 0.0);
  883. glNormal3d (0.0, -1.0, 0.0);
  884. glVertex3f (-3.7, -4.0, -0.1);
  885. glColor3f(0.0, 0.0, 1.0);
  886. //glNormal3d (0.0, 0.0, 1.0);
  887. glNormal3d (0.0, 0.0, 1.0);
  888. glVertex3f (0.0, 0.1, -0.1);
  889. glEnd ();
  890. #if 0
  891. //glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
  892. glBegin (GL_TRIANGLES);
  893. glColor3f(0.0, 1.0, 0.0);
  894. //glNormal3d (-0.75, 0.01, 0.0);
  895. glVertex3f (3.7, 0.1, -0.1);
  896. glColor3f(0.0, 1.0, 0.0);
  897. glNormal3d (0.8, 0.0, 0.0);
  898. glVertex3f (3.7, -4.1, -0.1);
  899. glColor3f(0.0, 1.0, 0.0);
  900. glNormal3d (0.0, 0.0, 1.0);
  901. glVertex3f (0.0, 0.1, -0.1);
  902. glEnd ();
  903. #endif
  904. glColor3f(0.2, 0.5, 0.8);
  905. }
  906. //Drawn CCW
  907. void
  908. DrawSquare(void)
  909. {
  910. GLfloat mat_specular[] = { 1.0, 0.0, 0.0, 0.0 };
  911. GLfloat lx, ly, lz;
  912. GLdouble angle = 60.000 * __glDegreesToRadians;
  913. lz = (GLfloat) cos (angle);
  914. lx = ly = (GLfloat) sin (angle) * 0.7071067;
  915. glBegin (GL_POLYGON);
  916. //glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
  917. glColor3f(1.0, 0.0, 0.0);
  918. glTexCoord2f (0.0, 0.0);
  919. glNormal3f (-lx, ly, lz);
  920. glVertex3f (-1.5, 1.5, -0.1);
  921. //glNormal3d (0.0, 0.0, 1.0);
  922. glColor3f(0.0, 1.0, 0.0);
  923. glTexCoord2f (0.0, 1.0);
  924. glNormal3f (-lx, -ly, lz);
  925. glVertex3f (-1.5, -1.5, -0.1);
  926. //glNormal3d (0.0, -1.0, 0.0);
  927. glColor3f(0.0, 1.0, 0.0);
  928. glTexCoord2f (1.0, 1.0);
  929. glNormal3f (lx, -ly, lz);
  930. glVertex3f (1.5, -1.5, -0.1);
  931. //glNormal3d (0.0, -1.0, 0.0);
  932. glColor3f(0.0, 1.0, 0.0);
  933. glTexCoord2f (1.0, 0.0);
  934. glNormal3f (lx, ly, lz);
  935. glVertex3f (1.5, 1.5, -0.1);
  936. //glNormal3d (0.0, 0.0, 1.0);
  937. glEnd ();
  938. glColor3f(0.2, 0.5, 0.8);
  939. }
  940. #if 0
  941. void
  942. DrawSquare(void)
  943. {
  944. GLfloat mat_specular[] = { 1.0, 0.0, 0.0, 0.0 };
  945. glBegin (GL_POLYGON);
  946. //glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
  947. glColor3f(1.0, 0.0, 0.0);
  948. //glNormal3d (-0.5, 0.5, 0.0);
  949. glNormal3d (0.0, 0.0, 1.0);
  950. glVertex3f (-3.7, 3.1, -0.1);
  951. glColor3f(0.0, 1.0, 0.0);
  952. glNormal3d (0.0, -1.0, 0.0);
  953. //glNormal3d (-0.5, -0.5, 0.1);
  954. glVertex3f (-3.7, -3.1, -0.1);
  955. glColor3f(0.0, 1.0, 0.0);
  956. glNormal3d (0.0, -1.0, 0.0);
  957. //glNormal3d (0.5, -0.5, 0.0);
  958. glVertex3f (3.7, -3.1, -0.1);
  959. glColor3f(0.0, 1.0, 0.0);
  960. glNormal3d (0.0, 0.0, 1.0);
  961. //glNormal3d (0.5, 0.5, 0.0);
  962. glVertex3f (3.7, 3.1, -0.1);
  963. glEnd ();
  964. glColor3f(0.2, 0.5, 0.8);
  965. }
  966. #endif
  967. void
  968. DrawLines(void)
  969. {
  970. glBegin (GL_LINES);
  971. glColor3f(1.0, 0.0, 0.0);
  972. glNormal3d(0.0, 1.0, 0.0);
  973. glVertex3f (-1.7, 0.1, -0.1);
  974. glColor3f(0.0, 1.0, 0.0);
  975. glNormal3d(0.0, 0.0, 1.0);
  976. glVertex3f (-1.7, -1.1, -0.1);
  977. glColor3f(0.0, 0.0, 1.0);
  978. glNormal3d(0.0, 1.0, 0.0);
  979. glVertex3f (0.0, 0.0, -0.1);
  980. glColor3f(0.0, 0.0, 1.0);
  981. glNormal3d(0.0, 0.0, 1.0);
  982. glVertex3f (5.0, 0.0, -0.1);
  983. glColor3f(0.0, 1.0, 0.0);
  984. glNormal3d(0.0, 1.0, 0.0);
  985. glVertex3f (0.0, 0.0, -0.1);
  986. glColor3f(0.0, 1.0, 0.0);
  987. glNormal3d(0.0, 0.0, 1.0);
  988. glVertex3f (0.0, 5.0, -0.1);
  989. glColor3f(1.0, 0.0, 0.0);
  990. glNormal3d(0.0, 1.0, 0.0);
  991. glVertex3f (0.0, 0.0, -0.1);
  992. glColor3f(0.0, 0.0, 1.0);
  993. glNormal3d(0.0, 0.0, 1.0);
  994. glVertex3f (0.0, 0.0, -5.1);
  995. glEnd ();
  996. glColor3f(0.2, 0.5, 0.8);
  997. }
  998. void
  999. DrawEval (void)
  1000. {
  1001. //glMapGrid2f((GLfloat)sampleEval, 0.0, 1.0, (GLfloat)sampleEval, 0.0, 1.0);
  1002. initEval ();
  1003. glEvalMesh2(GL_FILL,0,sampleEval,0,sampleEval) ;
  1004. }