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.

479 lines
11 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 "tk.h"
  42. #define VORDER 10
  43. #define CORDER 10
  44. #define TORDER 3
  45. #define VMAJOR_ORDER 2
  46. #define VMINOR_ORDER 3
  47. #define CMAJOR_ORDER 2
  48. #define CMINOR_ORDER 2
  49. #define TMAJOR_ORDER 2
  50. #define TMINOR_ORDER 2
  51. #define VDIM 4
  52. #define CDIM 4
  53. #define TDIM 2
  54. #define ONE_D 1
  55. #define TWO_D 2
  56. #define EVAL 3
  57. #define MESH 4
  58. GLenum doubleBuffer, directRender;
  59. float rotX = 0.0, rotY = 0.0, translateZ = -1.0;
  60. GLenum arrayType = ONE_D;
  61. GLenum colorType = GL_FALSE;
  62. GLenum textureType = GL_FALSE;
  63. GLenum polygonFilled = GL_FALSE;
  64. GLenum lighting = GL_FALSE;
  65. GLenum mapPoint = GL_FALSE;
  66. GLenum mapType = EVAL;
  67. double point1[10*4] = {
  68. -0.5, 0.0, 0.0, 1.0,
  69. -0.4, 0.5, 0.0, 1.0,
  70. -0.3,-0.5, 0.0, 1.0,
  71. -0.2, 0.5, 0.0, 1.0,
  72. -0.1,-0.5, 0.0, 1.0,
  73. 0.0, 0.5, 0.0, 1.0,
  74. 0.1,-0.5, 0.0, 1.0,
  75. 0.2, 0.5, 0.0, 1.0,
  76. 0.3,-0.5, 0.0, 1.0,
  77. 0.4, 0.0, 0.0, 1.0,
  78. };
  79. double cpoint1[10*4] = {
  80. 0.0, 0.0, 1.0, 1.0,
  81. 0.3, 0.0, 0.7, 1.0,
  82. 0.6, 0.0, 0.3, 1.0,
  83. 1.0, 0.0, 0.0, 1.0,
  84. 1.0, 0.3, 0.0, 1.0,
  85. 1.0, 0.6, 0.0, 1.0,
  86. 1.0, 1.0, 0.0, 1.0,
  87. 1.0, 1.0, 0.5, 1.0,
  88. 1.0, 1.0, 1.0, 1.0,
  89. };
  90. double tpoint1[11*4] = {
  91. 0.0, 0.0, 0.0, 1.0,
  92. 0.0, 0.1, 0.0, 1.0,
  93. 0.0, 0.2, 0.0, 1.0,
  94. 0.0, 0.3, 0.0, 1.0,
  95. 0.0, 0.4, 0.0, 1.0,
  96. 0.0, 0.5, 0.0, 1.0,
  97. 0.0, 0.6, 0.0, 1.0,
  98. 0.0, 0.7, 0.0, 1.0,
  99. 0.0, 0.8, 0.0, 1.0,
  100. 0.0, 0.9, 0.0, 1.0,
  101. };
  102. double point2[2*3*4] = {
  103. -0.5, -0.5, 0.5, 1.0,
  104. 0.0, 1.0, 0.5, 1.0,
  105. 0.5, -0.5, 0.5, 1.0,
  106. -0.5, 0.5, -0.5, 1.0,
  107. 0.0, -1.0, -0.5, 1.0,
  108. 0.5, 0.5, -0.5, 1.0,
  109. };
  110. double cpoint2[2*2*4] = {
  111. 0.0, 0.0, 0.0, 1.0,
  112. 0.0, 0.0, 1.0, 1.0,
  113. 0.0, 1.0, 0.0, 1.0,
  114. 1.0, 1.0, 1.0, 1.0,
  115. };
  116. double tpoint2[2*2*2] = {
  117. 0.0, 0.0, 0.0, 1.0,
  118. 1.0, 0.0, 1.0, 1.0,
  119. };
  120. float textureImage[4*2*4] = {
  121. 1.0, 1.0, 1.0, 1.0,
  122. 1.0, 0.0, 0.0, 1.0,
  123. 1.0, 0.0, 0.0, 1.0,
  124. 1.0, 1.0, 1.0, 1.0,
  125. 1.0, 1.0, 1.0, 1.0,
  126. 1.0, 0.0, 0.0, 1.0,
  127. 1.0, 0.0, 0.0, 1.0,
  128. 1.0, 1.0, 1.0, 1.0,
  129. };
  130. static void Init(void)
  131. {
  132. static float ambient[] = {0.1, 0.1, 0.1, 1.0};
  133. static float diffuse[] = {1.0, 1.0, 1.0, 1.0};
  134. static float position[] = {0.0, 0.0, -150.0, 0.0};
  135. static float front_mat_shininess[] = {30.0};
  136. static float front_mat_specular[] = {0.7, 0.7, 0.7, 1.0};
  137. static float front_mat_diffuse[] = {1.0, 0.2, 1.0, 1.0};
  138. static float back_mat_shininess[] = {50.0};
  139. static float back_mat_specular[] = {0.5, 0.5, 0.2, 1.0};
  140. static float back_mat_diffuse[] = {1.0, 1.0, 0.2, 1.0};
  141. static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
  142. static float lmodel_twoside[] = {GL_TRUE};
  143. static float decal[] = {GL_DECAL};
  144. static float modulate[] = {GL_MODULATE};
  145. static float repeat[] = {GL_REPEAT};
  146. static float clamp[] = {GL_CLAMP};
  147. static float nr[] = {GL_NEAREST};
  148. static float ln[] = {GL_LINEAR};
  149. glFrontFace(GL_CCW);
  150. glEnable(GL_DEPTH_TEST);
  151. glDepthFunc(GL_LEQUAL);
  152. glMap1d(GL_MAP1_VERTEX_4, 0.0, 1.0, VDIM, VORDER, point1);
  153. glMap1d(GL_MAP1_COLOR_4, 0.0, 1.0, CDIM, CORDER, cpoint1);
  154. glMap2d(GL_MAP2_VERTEX_4, 0.0, 1.0, VMINOR_ORDER*VDIM, VMAJOR_ORDER, 0.0,
  155. 1.0, VDIM, VMINOR_ORDER, point2);
  156. glMap2d(GL_MAP2_COLOR_4, 0.0, 1.0, CMINOR_ORDER*CDIM, CMAJOR_ORDER, 0.0,
  157. 1.0, CDIM, CMINOR_ORDER, cpoint2);
  158. glMap2d(GL_MAP2_TEXTURE_COORD_2, 0.0, 1.0, TMINOR_ORDER*TDIM,
  159. TMAJOR_ORDER, 0.0, 1.0, TDIM, TMINOR_ORDER, tpoint2);
  160. glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  161. glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  162. glLightfv(GL_LIGHT0, GL_POSITION, position);
  163. glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
  164. glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
  165. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  166. glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  167. glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, decal);
  168. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, repeat);
  169. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, repeat);
  170. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, nr);
  171. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, nr);
  172. glTexImage2D(GL_TEXTURE_2D, 0, 4, 2, 4, 0, GL_RGBA, GL_FLOAT,
  173. (GLvoid *)textureImage);
  174. }
  175. static void DrawPoints1(void)
  176. {
  177. GLint i;
  178. glColor3f(0.0, 1.0, 0.0);
  179. glPointSize(2);
  180. glBegin(GL_POINTS);
  181. for (i = 0; i < VORDER; i++) {
  182. glVertex4dv(&point1[i*4]);
  183. }
  184. glEnd();
  185. }
  186. static void DrawPoints2(void)
  187. {
  188. GLint i, j;
  189. glColor3f(1.0, 0.0, 1.0);
  190. glPointSize(2);
  191. glBegin(GL_POINTS);
  192. for (i = 0; i < VMAJOR_ORDER; i++) {
  193. for (j = 0; j < VMINOR_ORDER; j++) {
  194. glVertex4dv(&point2[i*4*VMINOR_ORDER+j*4]);
  195. }
  196. }
  197. glEnd();
  198. }
  199. static void DrawMapEval1(float du)
  200. {
  201. float u;
  202. glColor3f(1.0, 0.0, 0.0);
  203. glBegin(GL_LINE_STRIP);
  204. for (u = 0.0; u < 1.0; u += du) {
  205. glEvalCoord1d(u);
  206. }
  207. glEvalCoord1d(1.0);
  208. glEnd();
  209. }
  210. static void DrawMapEval2(float du, float dv)
  211. {
  212. float u, v, tmp;
  213. glColor3f(1.0, 0.0, 0.0);
  214. for (v = 0.0; v < 1.0; v += dv) {
  215. glBegin(GL_QUAD_STRIP);
  216. for (u = 0.0; u <= 1.0; u += du) {
  217. glEvalCoord2d(u,v);
  218. tmp = (v + dv < 1.0) ? (v + dv) : 1.0;
  219. glEvalCoord2d(u, tmp);
  220. }
  221. glEvalCoord2d(1.0, v);
  222. glEvalCoord2d(1.0, v+dv);
  223. glEnd();
  224. }
  225. }
  226. static void RenderEval(void)
  227. {
  228. if (colorType) {
  229. glEnable(GL_MAP1_COLOR_4);
  230. glEnable(GL_MAP2_COLOR_4);
  231. } else {
  232. glDisable(GL_MAP1_COLOR_4);
  233. glDisable(GL_MAP2_COLOR_4);
  234. }
  235. if (textureType) {
  236. glEnable(GL_TEXTURE_2D);
  237. glEnable(GL_MAP2_TEXTURE_COORD_2);
  238. } else {
  239. glDisable(GL_TEXTURE_2D);
  240. glDisable(GL_MAP2_TEXTURE_COORD_2);
  241. }
  242. if (polygonFilled) {
  243. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  244. } else {
  245. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  246. }
  247. glShadeModel(GL_SMOOTH);
  248. switch (mapType) {
  249. case EVAL:
  250. switch (arrayType) {
  251. case ONE_D:
  252. glDisable(GL_MAP2_VERTEX_4);
  253. glEnable(GL_MAP1_VERTEX_4);
  254. DrawPoints1();
  255. DrawMapEval1(0.1/VORDER);
  256. break;
  257. case TWO_D:
  258. glDisable(GL_MAP1_VERTEX_4);
  259. glEnable(GL_MAP2_VERTEX_4);
  260. DrawPoints2();
  261. DrawMapEval2(0.1/VMAJOR_ORDER,0.1/VMINOR_ORDER);
  262. break;
  263. }
  264. break;
  265. case MESH:
  266. switch (arrayType) {
  267. case ONE_D:
  268. DrawPoints1();
  269. glDisable(GL_MAP2_VERTEX_4);
  270. glEnable (GL_MAP1_VERTEX_4);
  271. glColor3f(0.0, 0.0, 1.0);
  272. glMapGrid1d(40, 0.0, 1.0);
  273. if (mapPoint) {
  274. glPointSize(2);
  275. glEvalMesh1(GL_POINT, 0, 40);
  276. } else {
  277. glEvalMesh1(GL_LINE, 0, 40);
  278. }
  279. break;
  280. case TWO_D:
  281. DrawPoints2();
  282. glDisable(GL_MAP1_VERTEX_4);
  283. glEnable(GL_MAP2_VERTEX_4);
  284. glColor3f(0.0, 0.0, 1.0);
  285. glMapGrid2d(20, 0.0, 1.0, 20, 0.0, 1.0);
  286. if (mapPoint) {
  287. glPointSize(2);
  288. glEvalMesh2(GL_POINT, 0, 20, 0, 20);
  289. } else if (polygonFilled) {
  290. glEvalMesh2(GL_FILL, 0, 20, 0, 20);
  291. } else {
  292. glEvalMesh2(GL_LINE, 0, 20, 0, 20);
  293. }
  294. break;
  295. }
  296. break;
  297. }
  298. }
  299. static void Reshape(int width, int height)
  300. {
  301. glViewport(0, 0, (GLint)width, (GLint)height);
  302. glMatrixMode(GL_PROJECTION);
  303. glLoadIdentity();
  304. glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 10.0);
  305. glMatrixMode(GL_MODELVIEW);
  306. }
  307. static GLenum Key(int key, GLenum mask)
  308. {
  309. switch (key) {
  310. case TK_ESCAPE:
  311. tkQuit();
  312. case TK_LEFT:
  313. rotY -= 30;
  314. break;
  315. case TK_RIGHT:
  316. rotY += 30;
  317. break;
  318. case TK_UP:
  319. rotX -= 30;
  320. break;
  321. case TK_DOWN:
  322. rotX += 30;
  323. break;
  324. case TK_1:
  325. arrayType = ONE_D;
  326. break;
  327. case TK_2:
  328. arrayType = TWO_D;
  329. break;
  330. case TK_e:
  331. mapType = EVAL;
  332. break;
  333. case TK_m:
  334. mapType = MESH;
  335. break;
  336. case TK_f:
  337. polygonFilled = !polygonFilled;
  338. break;
  339. case TK_p:
  340. mapPoint = !mapPoint;
  341. break;
  342. case TK_c:
  343. colorType = !colorType;
  344. break;
  345. case TK_t:
  346. textureType = !textureType;
  347. break;
  348. case TK_l:
  349. lighting =! lighting;
  350. if (lighting) {
  351. glEnable(GL_LIGHTING);
  352. glEnable(GL_LIGHT0);
  353. glEnable(GL_AUTO_NORMAL);
  354. } else {
  355. glDisable(GL_LIGHTING);
  356. glDisable(GL_LIGHT0);
  357. glDisable(GL_AUTO_NORMAL);
  358. }
  359. break;
  360. default:
  361. return GL_FALSE;
  362. }
  363. return GL_TRUE;
  364. }
  365. static void Draw(void)
  366. {
  367. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  368. glPushMatrix();
  369. glTranslatef(0.0, 0.0 , translateZ);
  370. glRotatef(rotX, 1, 0, 0);
  371. glRotatef(rotY, 0, 1, 0);
  372. RenderEval();
  373. glPopMatrix();
  374. glFlush();
  375. if (doubleBuffer) {
  376. tkSwapBuffers();
  377. }
  378. }
  379. static GLenum Args(int argc, char **argv)
  380. {
  381. GLint i;
  382. doubleBuffer = GL_FALSE;
  383. directRender = GL_FALSE;
  384. for (i = 1; i < argc; i++) {
  385. if (strcmp(argv[i], "-sb") == 0) {
  386. doubleBuffer = GL_FALSE;
  387. } else if (strcmp(argv[i], "-db") == 0) {
  388. doubleBuffer = GL_TRUE;
  389. } else if (strcmp(argv[i], "-dr") == 0) {
  390. directRender = GL_TRUE;
  391. } else if (strcmp(argv[i], "-ir") == 0) {
  392. directRender = GL_FALSE;
  393. } else {
  394. printf("%s (Bad option).\n", argv[i]);
  395. return GL_FALSE;
  396. }
  397. }
  398. return GL_TRUE;
  399. }
  400. void main(int argc, char **argv)
  401. {
  402. GLenum type;
  403. if (Args(argc, argv) == GL_FALSE) {
  404. tkQuit();
  405. }
  406. tkInitPosition(0, 0, 300, 300);
  407. type = TK_RGB | TK_DEPTH16;
  408. type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  409. type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  410. tkInitDisplayMode(type);
  411. if (tkInitWindow("Evaluator Test") == GL_FALSE) {
  412. tkQuit();
  413. }
  414. Init();
  415. tkExposeFunc(Reshape);
  416. tkReshapeFunc(Reshape);
  417. tkKeyDownFunc(Key);
  418. tkDisplayFunc(Draw);
  419. tkExec();
  420. }