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.

451 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 "tk.h"
  41. #define PI 3.141592654
  42. #define BLACK 0
  43. #define GRAY 128
  44. #define WHITE 255
  45. #define RD 0xA40000FF
  46. #define WT 0xFFFFFFFF
  47. #define brickImageWidth 16
  48. #define brickImageHeight 16
  49. GLenum rgb, doubleBuffer, directRender;
  50. float black[3] = {
  51. 0.0, 0.0, 0.0
  52. };
  53. float blue[3] = {
  54. 0.0, 0.0, 1.0
  55. };
  56. float gray[3] = {
  57. 0.5, 0.5, 0.5
  58. };
  59. float white[3] = {
  60. 1.0, 1.0, 1.0
  61. };
  62. GLenum doDither = GL_TRUE;
  63. GLenum shade = GL_TRUE;
  64. // mf
  65. GLenum texture = GL_FALSE;
  66. float xRotation = 30.0, yRotation = 30.0, zRotation = 0.0;
  67. GLint radius1, radius2;
  68. GLdouble angle1, angle2;
  69. GLint slices, stacks;
  70. GLint height;
  71. GLint orientation = GLU_OUTSIDE;
  72. GLint whichQuadric;
  73. GLUquadricObj *quadObj;
  74. GLuint brickImage[brickImageWidth*brickImageHeight] = {
  75. RD, RD, RD, RD, RD, RD, RD, RD, RD, WT, RD, RD, RD, RD, RD, RD,
  76. RD, RD, RD, RD, RD, RD, RD, RD, RD, WT, RD, RD, RD, RD, RD, RD,
  77. RD, RD, RD, RD, RD, RD, RD, RD, RD, WT, RD, RD, RD, RD, RD, RD,
  78. RD, RD, RD, RD, RD, RD, RD, RD, RD, WT, RD, RD, RD, RD, RD, RD,
  79. WT, WT, WT, WT, WT, WT, WT, WT, WT, WT, WT, WT, WT, WT, WT, WT,
  80. RD, RD, RD, WT, RD, RD, RD, RD, RD, RD, RD, RD, RD, WT, RD, RD,
  81. RD, RD, RD, WT, RD, RD, RD, RD, RD, RD, RD, RD, RD, WT, RD, RD,
  82. RD, RD, RD, WT, RD, RD, RD, RD, RD, RD, RD, RD, RD, WT, RD, RD,
  83. RD, RD, RD, WT, RD, RD, RD, RD, RD, RD, RD, RD, RD, WT, RD, RD,
  84. WT, WT, WT, WT, WT, WT, WT, WT, WT, WT, WT, WT, WT, WT, WT, WT,
  85. RD, RD, RD, RD, RD, RD, RD, WT, RD, RD, RD, RD, RD, RD, RD, RD,
  86. RD, RD, RD, RD, RD, RD, RD, WT, RD, RD, RD, RD, RD, RD, RD, RD,
  87. RD, RD, RD, RD, RD, RD, RD, WT, RD, RD, RD, RD, RD, RD, RD, RD,
  88. RD, RD, RD, RD, RD, RD, RD, WT, RD, RD, RD, RD, RD, RD, RD, RD,
  89. WT, WT, WT, WT, WT, WT, WT, WT, WT, WT, WT, WT, WT, WT, WT, WT,
  90. RD, RD, RD, RD, WT, RD, RD, RD, RD, RD, RD, RD, RD, RD, WT, RD
  91. };
  92. char *texFileName = 0;
  93. static void ErrorHandler(GLenum which)
  94. {
  95. fprintf(stderr, "Quad Error: %s\n", gluErrorString(which));
  96. }
  97. static void Init(void)
  98. {
  99. static GLint colorIndexes[3] = {0, 200, 255};
  100. static float ambient[] = {0.1, 0.1, 0.1, 1.0};
  101. static float diffuse[] = {0.5, 1.0, 1.0, 1.0};
  102. static float position[] = {90.0, 90.0, 150.0, 0.0};
  103. static float front_mat_shininess[] = {30.0};
  104. static float front_mat_specular[] = {0.2, 0.2, 0.2, 1.0};
  105. static float front_mat_diffuse[] = {0.5, 0.28, 0.38, 1.0};
  106. static float back_mat_shininess[] = {50.0};
  107. static float back_mat_specular[] = {0.5, 0.5, 0.2, 1.0};
  108. static float back_mat_diffuse[] = {1.0, 1.0, 0.2, 1.0};
  109. static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
  110. static float lmodel_twoside[] = {GL_TRUE};
  111. static float decal[] = {GL_DECAL};
  112. static float modulate[] = {GL_MODULATE};
  113. static float repeat[] = {GL_REPEAT};
  114. static float nearest[] = {GL_NEAREST};
  115. TK_RGBImageRec *image;
  116. if (!rgb) {
  117. tkSetGreyRamp();
  118. }
  119. glClearColor(0.0, 0.0, 0.0, 0.0);
  120. glDepthFunc(GL_LEQUAL);
  121. glEnable(GL_DEPTH_TEST);
  122. glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  123. glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  124. glLightfv(GL_LIGHT0, GL_POSITION, position);
  125. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  126. glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  127. glEnable(GL_LIGHTING);
  128. glEnable(GL_LIGHT0);
  129. glMaterialfv(GL_FRONT, GL_SHININESS, front_mat_shininess);
  130. glMaterialfv(GL_FRONT, GL_SPECULAR, front_mat_specular);
  131. glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
  132. glMaterialfv(GL_BACK, GL_SHININESS, back_mat_shininess);
  133. glMaterialfv(GL_BACK, GL_SPECULAR, back_mat_specular);
  134. glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
  135. if (!rgb) {
  136. glMaterialiv( GL_FRONT_AND_BACK, GL_COLOR_INDEXES, colorIndexes);
  137. }
  138. glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, decal);
  139. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, repeat);
  140. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, repeat);
  141. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, nearest);
  142. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, nearest);
  143. if (texFileName) {
  144. image = tkRGBImageLoad(texFileName);
  145. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  146. gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image->sizeX, image->sizeY,
  147. GL_RGB, GL_UNSIGNED_BYTE, image->data);
  148. } else {
  149. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  150. glTexImage2D(GL_TEXTURE_2D, 0, 4, brickImageWidth, brickImageHeight,
  151. 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)brickImage);
  152. }
  153. quadObj = gluNewQuadric();
  154. gluQuadricCallback(quadObj, GLU_ERROR, ErrorHandler);
  155. radius1 = 10;
  156. radius2 = 5;
  157. angle1 = 90;
  158. angle2 = 180;
  159. slices = 16;
  160. stacks = 10;
  161. height = 20;
  162. }
  163. static void Reshape(int width, int height)
  164. {
  165. glViewport(0, 0, (GLint)width, (GLint)height);
  166. glMatrixMode(GL_PROJECTION);
  167. glLoadIdentity();
  168. glFrustum(-1, 1, -1, 1, 1, 10);
  169. gluLookAt(2, 2, 2, 0, 0, 0, 0, 0, 1);
  170. glMatrixMode(GL_MODELVIEW);
  171. }
  172. static GLenum Key(int key, GLenum mask)
  173. {
  174. switch (key) {
  175. case TK_ESCAPE:
  176. tkQuit();
  177. case TK_LEFT:
  178. yRotation += 5;
  179. break;
  180. case TK_RIGHT:
  181. yRotation -= 5;
  182. break;
  183. case TK_UP:
  184. xRotation += 5;
  185. break;
  186. case TK_DOWN:
  187. xRotation -= 5;
  188. break;
  189. case TK_X:
  190. zRotation += 5;
  191. break;
  192. case TK_x:
  193. zRotation -= 5;
  194. break;
  195. case TK_1:
  196. gluQuadricDrawStyle(quadObj, GLU_FILL);
  197. break;
  198. case TK_2:
  199. gluQuadricDrawStyle(quadObj, GLU_POINT);
  200. break;
  201. case TK_3:
  202. gluQuadricDrawStyle(quadObj, GLU_LINE);
  203. break;
  204. case TK_4:
  205. gluQuadricDrawStyle(quadObj, GLU_SILHOUETTE);
  206. break;
  207. case TK_0:
  208. shade = !shade;
  209. if (shade) {
  210. glShadeModel(GL_SMOOTH);
  211. gluQuadricNormals(quadObj, GLU_SMOOTH);
  212. } else {
  213. glShadeModel(GL_FLAT);
  214. gluQuadricNormals(quadObj, GLU_FLAT);
  215. }
  216. break;
  217. case TK_A:
  218. stacks++;
  219. break;
  220. case TK_a:
  221. stacks--;
  222. break;
  223. case TK_S:
  224. slices++;
  225. break;
  226. case TK_s:
  227. slices--;
  228. break;
  229. case TK_d:
  230. switch(orientation) {
  231. case GLU_OUTSIDE:
  232. orientation = GLU_INSIDE;
  233. break;
  234. case GLU_INSIDE:
  235. default:
  236. orientation = GLU_OUTSIDE;
  237. break;
  238. }
  239. gluQuadricOrientation(quadObj, orientation);
  240. break;
  241. case TK_f:
  242. whichQuadric = whichQuadric >= 3 ? 0 : whichQuadric + 1;
  243. break;
  244. case TK_G:
  245. radius1 += 1;
  246. break;
  247. case TK_g:
  248. radius1 -= 1;
  249. break;
  250. case TK_J:
  251. radius2 += 1;
  252. break;
  253. case TK_j:
  254. radius2 -= 1;
  255. break;
  256. case TK_H:
  257. height += 2;
  258. break;
  259. case TK_h:
  260. height -= 2;
  261. break;
  262. case TK_K:
  263. angle1 += 5;
  264. break;
  265. case TK_k:
  266. angle1 -= 5;
  267. break;
  268. case TK_L:
  269. angle2 += 5;
  270. break;
  271. case TK_l:
  272. angle2 -= 5;
  273. break;
  274. case TK_z:
  275. texture = !texture;
  276. if (texture) {
  277. gluQuadricTexture(quadObj, GL_TRUE);
  278. glEnable(GL_TEXTURE_2D);
  279. } else {
  280. gluQuadricTexture(quadObj, GL_FALSE);
  281. glDisable(GL_TEXTURE_2D);
  282. }
  283. break;
  284. case TK_q:
  285. glDisable(GL_CULL_FACE);
  286. break;
  287. case TK_w:
  288. glEnable(GL_CULL_FACE);
  289. glCullFace(GL_FRONT);
  290. break;
  291. case TK_e:
  292. glEnable(GL_CULL_FACE);
  293. glCullFace(GL_BACK);
  294. break;
  295. case TK_r:
  296. glFrontFace(GL_CW);
  297. break;
  298. case TK_t:
  299. glFrontFace(GL_CCW);
  300. break;
  301. case TK_y:
  302. doDither = !doDither;
  303. (doDither) ? glEnable(GL_DITHER) : glDisable(GL_DITHER);
  304. break;
  305. default:
  306. return GL_FALSE;
  307. }
  308. return GL_TRUE;
  309. }
  310. static void Draw(void)
  311. {
  312. glLoadIdentity();
  313. glRotatef(xRotation, 1, 0, 0);
  314. glRotatef(yRotation, 0, 1, 0);
  315. glRotatef(zRotation, 0, 0, 1);
  316. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  317. glColor3f(1.0, 1.0, 1.0);
  318. switch (whichQuadric) {
  319. case 0:
  320. glTranslatef(0, 0, -height/20.0);
  321. gluCylinder(quadObj, radius1/10.0, radius2/10.0, height/10.0,
  322. slices, stacks);
  323. break;
  324. case 1:
  325. gluSphere(quadObj, radius1/10.0, slices, stacks);
  326. break;
  327. case 2:
  328. gluPartialDisk(quadObj, radius2/10.0, radius1/10.0, slices,
  329. stacks, angle1, angle2);
  330. break;
  331. case 3:
  332. gluDisk(quadObj, radius2/10.0, radius1/10.0, slices, stacks);
  333. break;
  334. }
  335. glFlush();
  336. if (doubleBuffer) {
  337. tkSwapBuffers();
  338. }
  339. }
  340. static GLenum Args(int argc, char **argv)
  341. {
  342. GLint i;
  343. rgb = GL_TRUE;
  344. doubleBuffer = GL_FALSE;
  345. directRender = GL_FALSE;
  346. for (i = 1; i < argc; i++) {
  347. if (strcmp(argv[i], "-ci") == 0) {
  348. rgb = GL_FALSE;
  349. } else if (strcmp(argv[i], "-rgb") == 0) {
  350. rgb = GL_TRUE;
  351. } else if (strcmp(argv[i], "-sb") == 0) {
  352. doubleBuffer = GL_FALSE;
  353. } else if (strcmp(argv[i], "-db") == 0) {
  354. doubleBuffer = GL_TRUE;
  355. } else if (strcmp(argv[i], "-dr") == 0) {
  356. directRender = GL_TRUE;
  357. } else if (strcmp(argv[i], "-ir") == 0) {
  358. directRender = GL_FALSE;
  359. } else if (strcmp(argv[i], "-f") == 0) {
  360. if (i+1 >= argc || argv[i+1][0] == '-') {
  361. printf("-f (No file name).\n");
  362. return GL_FALSE;
  363. } else {
  364. texFileName = argv[++i];
  365. }
  366. } else {
  367. printf("%s (Bad option).\n", argv[i]);
  368. return GL_FALSE;
  369. }
  370. }
  371. return GL_TRUE;
  372. }
  373. void main(int argc, char **argv)
  374. {
  375. GLenum type;
  376. if (Args(argc, argv) == GL_FALSE) {
  377. tkQuit();
  378. }
  379. tkInitPosition(0, 0, 300, 300);
  380. type = TK_DEPTH16;
  381. type |= (rgb) ? TK_RGB : TK_INDEX;
  382. type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  383. type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  384. tkInitDisplayMode(type);
  385. if (tkInitWindow("Quad Test") == GL_FALSE) {
  386. tkQuit();
  387. }
  388. Init();
  389. tkExposeFunc(Reshape);
  390. tkReshapeFunc(Reshape);
  391. tkKeyDownFunc(Key);
  392. tkDisplayFunc(Draw);
  393. tkExec();
  394. }