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.

554 lines
10 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 <sys/types.h>
  40. #include <sys/stat.h>
  41. #include <fcntl.h>
  42. //#include <unistd.h>
  43. #include <math.h>
  44. #include <time.h>
  45. #include <sys\types.h>
  46. #include <sys\timeb.h>
  47. #include <stdlib.h>
  48. #include "tk.h"
  49. GLenum doubleBuffer, directRender;
  50. char *texFileName = 0;
  51. TK_RGBImageRec *image;
  52. float *minFilter, *magFilter, *sWrapMode, *tWrapMode;
  53. float decal[] = {GL_DECAL};
  54. float modulate[] = {GL_MODULATE};
  55. float repeat[] = {GL_REPEAT};
  56. float clamp[] = {GL_CLAMP};
  57. float nr[] = {GL_NEAREST};
  58. float ln[] = {GL_LINEAR};
  59. float nr_mipmap_nr[] = {GL_NEAREST_MIPMAP_NEAREST};
  60. float nr_mipmap_ln[] = {GL_NEAREST_MIPMAP_LINEAR};
  61. float ln_mipmap_nr[] = {GL_LINEAR_MIPMAP_NEAREST};
  62. float ln_mipmap_ln[] = {GL_LINEAR_MIPMAP_LINEAR};
  63. GLint sphereMap[] = {GL_SPHERE_MAP};
  64. GLboolean displayFrameRate = GL_FALSE;
  65. GLint frames, curFrame = 0, nextFrame = 0;
  66. GLint frameCount = 0;
  67. GLenum doDither = GL_TRUE;
  68. GLenum doSphere = GL_FALSE;
  69. float xRotation = 0.0, yRotation = 0.0, zTranslate = -3.125;
  70. GLint cube;
  71. float c[6][4][3] = {
  72. {
  73. {
  74. 1.0, 1.0, -1.0
  75. },
  76. {
  77. -1.0, 1.0, -1.0
  78. },
  79. {
  80. -1.0, -1.0, -1.0
  81. },
  82. {
  83. 1.0, -1.0, -1.0
  84. }
  85. },
  86. {
  87. {
  88. 1.0, 1.0, 1.0
  89. },
  90. {
  91. 1.0, 1.0, -1.0
  92. },
  93. {
  94. 1.0, -1.0, -1.0
  95. },
  96. {
  97. 1.0, -1.0, 1.0
  98. }
  99. },
  100. {
  101. {
  102. -1.0, 1.0, 1.0
  103. },
  104. {
  105. 1.0, 1.0, 1.0
  106. },
  107. {
  108. 1.0, -1.0, 1.0
  109. },
  110. {
  111. -1.0, -1.0, 1.0
  112. }
  113. },
  114. {
  115. {
  116. -1.0, 1.0, -1.0
  117. },
  118. {
  119. -1.0, 1.0, 1.0
  120. },
  121. {
  122. -1.0, -1.0, 1.0
  123. },
  124. {
  125. -1.0, -1.0, -1.0
  126. }
  127. },
  128. {
  129. {
  130. -1.0, 1.0, 1.0
  131. },
  132. {
  133. -1.0, 1.0, -1.0
  134. },
  135. {
  136. 1.0, 1.0, -1.0
  137. },
  138. {
  139. 1.0, 1.0, 1.0
  140. }
  141. },
  142. {
  143. {
  144. -1.0, -1.0, -1.0
  145. },
  146. {
  147. -1.0, -1.0, 1.0
  148. },
  149. {
  150. 1.0, -1.0, 1.0
  151. },
  152. {
  153. 1.0, -1.0, -1.0
  154. }
  155. }
  156. };
  157. static float n[6][3] = {
  158. {
  159. 0.0, 0.0, -1.0
  160. },
  161. {
  162. 1.0, 0.0, 0.0
  163. },
  164. {
  165. 0.0, 0.0, 1.0
  166. },
  167. {
  168. -1.0, 0.0, 0.0
  169. },
  170. {
  171. 0.0, 1.0, 0.0
  172. },
  173. {
  174. 0.0, -1.0, 0.0
  175. }
  176. };
  177. static float t[6][4][2] = {
  178. {
  179. {
  180. 1.1, 1.1
  181. },
  182. {
  183. -0.1, 1.1
  184. },
  185. {
  186. -0.1, -0.1
  187. },
  188. {
  189. 1.1, -0.1
  190. }
  191. },
  192. {
  193. {
  194. 1.1, 1.1
  195. },
  196. {
  197. -0.1, 1.1
  198. },
  199. {
  200. -0.1, -0.1
  201. },
  202. {
  203. 1.1, -0.1
  204. }
  205. },
  206. {
  207. {
  208. -0.1, 1.1
  209. },
  210. {
  211. 1.1, 1.1
  212. },
  213. {
  214. 1.1, -0.1
  215. },
  216. {
  217. -0.1, -0.1
  218. }
  219. },
  220. {
  221. {
  222. 1.1, 1.1
  223. },
  224. {
  225. -0.1, 1.1
  226. },
  227. {
  228. -0.1, -0.1
  229. },
  230. {
  231. 1.1, -0.1
  232. }
  233. },
  234. {
  235. {
  236. 1.1, 1.1
  237. },
  238. {
  239. -0.1, 1.1
  240. },
  241. {
  242. -0.1, -0.1
  243. },
  244. {
  245. 1.1, -0.1
  246. }
  247. },
  248. {
  249. {
  250. 1.1, 1.1
  251. },
  252. {
  253. -0.1, 1.1
  254. },
  255. {
  256. -0.1, -0.1
  257. },
  258. {
  259. 1.1, -0.1
  260. }
  261. },
  262. };
  263. static void BuildCube(void)
  264. {
  265. GLint i;
  266. glNewList(cube, GL_COMPILE);
  267. for (i = 0; i < 6; i++) {
  268. glBegin(GL_POLYGON);
  269. glNormal3fv(n[i]); glTexCoord2fv(t[i][0]); glVertex3fv(c[i][0]);
  270. glNormal3fv(n[i]); glTexCoord2fv(t[i][1]); glVertex3fv(c[i][1]);
  271. glNormal3fv(n[i]); glTexCoord2fv(t[i][2]); glVertex3fv(c[i][2]);
  272. glNormal3fv(n[i]); glTexCoord2fv(t[i][3]); glVertex3fv(c[i][3]);
  273. glEnd();
  274. }
  275. glEndList();
  276. }
  277. static void BuildLists(void)
  278. {
  279. cube = glGenLists(1);
  280. BuildCube();
  281. }
  282. static void Init(void)
  283. {
  284. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  285. gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image->sizeX, image->sizeY,
  286. GL_RGB, GL_UNSIGNED_BYTE, image->data);
  287. glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, decal);
  288. glEnable(GL_TEXTURE_2D);
  289. glFrontFace(GL_CCW);
  290. glCullFace(GL_FRONT);
  291. glEnable(GL_CULL_FACE);
  292. BuildLists();
  293. glClearColor(0.0, 0.0, 0.0, 0.0);
  294. magFilter = nr;
  295. minFilter = nr;
  296. sWrapMode = repeat;
  297. tWrapMode = repeat;
  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. // gluPerspective(145.0, 1.0, 0.01, 1000);
  305. // gluPerspective(90.0, 1.0, 0.01, 1000);
  306. glOrtho(-3.0, 3.0, -3.0, 3.0, -5.0, 5.0);
  307. glMatrixMode(GL_MODELVIEW);
  308. }
  309. static GLenum Key(int key, GLenum mask)
  310. {
  311. switch (key) {
  312. case TK_ESCAPE:
  313. tkQuit();
  314. case TK_LEFT:
  315. yRotation -= 0.5;
  316. break;
  317. case TK_RIGHT:
  318. yRotation += 0.5;
  319. break;
  320. case TK_UP:
  321. xRotation -= 0.5;
  322. break;
  323. case TK_DOWN:
  324. xRotation += 0.5;
  325. break;
  326. case TK_f:
  327. displayFrameRate = !displayFrameRate;
  328. frameCount = 0;
  329. break;
  330. case TK_d:
  331. doDither = !doDither;
  332. (doDither) ? glEnable(GL_DITHER) : glDisable(GL_DITHER);
  333. break;
  334. case TK_T:
  335. zTranslate += 0.25;
  336. break;
  337. case TK_t:
  338. zTranslate -= 0.25;
  339. break;
  340. case TK_s:
  341. doSphere = !doSphere;
  342. if (doSphere) {
  343. glTexGeniv(GL_S, GL_TEXTURE_GEN_MODE, sphereMap);
  344. glTexGeniv(GL_T, GL_TEXTURE_GEN_MODE, sphereMap);
  345. glEnable(GL_TEXTURE_GEN_S);
  346. glEnable(GL_TEXTURE_GEN_T);
  347. } else {
  348. glDisable(GL_TEXTURE_GEN_S);
  349. glDisable(GL_TEXTURE_GEN_T);
  350. }
  351. break;
  352. case TK_0:
  353. magFilter = nr;
  354. break;
  355. case TK_1:
  356. magFilter = ln;
  357. break;
  358. case TK_2:
  359. minFilter = nr;
  360. break;
  361. case TK_3:
  362. minFilter = ln;
  363. break;
  364. case TK_4:
  365. minFilter = nr_mipmap_nr;
  366. break;
  367. case TK_5:
  368. minFilter = nr_mipmap_ln;
  369. break;
  370. case TK_6:
  371. minFilter = ln_mipmap_nr;
  372. break;
  373. case TK_7:
  374. minFilter = ln_mipmap_ln;
  375. break;
  376. default:
  377. return GL_FALSE;
  378. }
  379. return GL_TRUE;
  380. }
  381. static void Draw(void)
  382. {
  383. glClear(GL_COLOR_BUFFER_BIT);
  384. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, sWrapMode);
  385. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tWrapMode);
  386. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  387. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  388. glPushMatrix();
  389. glTranslatef(0.0, 0.0, zTranslate);
  390. glRotatef(xRotation, 1, 0, 0);
  391. glRotatef(yRotation, 0, 1, 0);
  392. glCallList(cube);
  393. glPopMatrix();
  394. glFlush();
  395. if (doubleBuffer) {
  396. tkSwapBuffers();
  397. }
  398. }
  399. static GLenum Args(int argc, char **argv)
  400. {
  401. GLint i;
  402. doubleBuffer = GL_FALSE;
  403. directRender = GL_FALSE;
  404. for (i = 1; i < argc; i++) {
  405. if (strcmp(argv[i], "-sb") == 0) {
  406. doubleBuffer = GL_FALSE;
  407. } else if (strcmp(argv[i], "-db") == 0) {
  408. doubleBuffer = GL_TRUE;
  409. } else if (strcmp(argv[i], "-dr") == 0) {
  410. directRender = GL_TRUE;
  411. } else if (strcmp(argv[i], "-ir") == 0) {
  412. directRender = GL_FALSE;
  413. } else if (strcmp(argv[i], "-f") == 0) {
  414. if (i+1 >= argc || argv[i+1][0] == '-') {
  415. printf("-f (No file name).\n");
  416. return GL_FALSE;
  417. } else {
  418. texFileName = argv[++i];
  419. }
  420. } else {
  421. printf("%s (Bad option).\n", argv[i]);
  422. return GL_FALSE;
  423. }
  424. }
  425. return GL_TRUE;
  426. }
  427. static void Animate(void)
  428. {
  429. static struct _timeb thisTime, baseTime;
  430. double elapsed, frameRate, deltat;
  431. if (curFrame++ >= 10) {
  432. if( !frameCount ) {
  433. _ftime( &baseTime );
  434. }
  435. else {
  436. if( displayFrameRate ) {
  437. _ftime( &thisTime );
  438. elapsed = thisTime.time + thisTime.millitm/1000.0 -
  439. (baseTime.time + baseTime.millitm/1000.0);
  440. if( elapsed == 0.0 )
  441. printf( "Frame rate = unknown\n" );
  442. else {
  443. frameRate = frameCount / elapsed;
  444. printf( "Frame rate = %5.2f fps\n", frameRate );
  445. }
  446. }
  447. }
  448. frameCount += 10;
  449. curFrame = 0;
  450. }
  451. glClear(GL_COLOR_BUFFER_BIT);
  452. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, sWrapMode);
  453. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tWrapMode);
  454. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  455. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  456. glPushMatrix();
  457. glTranslatef(0.0, 0.0, zTranslate);
  458. glRotatef(xRotation, 1, 0, 0);
  459. glRotatef(yRotation, 0, 1, 0);
  460. glCallList(cube);
  461. glPopMatrix();
  462. glFlush();
  463. if (doubleBuffer) {
  464. tkSwapBuffers();
  465. }
  466. xRotation += 10.0;
  467. yRotation += 10.0;
  468. }
  469. void main(int argc, char **argv)
  470. {
  471. GLenum type;
  472. if (Args(argc, argv) == GL_FALSE) {
  473. tkQuit();
  474. }
  475. if (texFileName == 0) {
  476. printf("No image file.\n");
  477. tkQuit();
  478. }
  479. image = tkRGBImageLoad(texFileName);
  480. tkInitPosition(0, 0, 300, 300);
  481. type = TK_RGB;
  482. type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  483. type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  484. tkInitDisplayMode(type);
  485. if (tkInitWindow("Texture Test") == GL_FALSE) {
  486. tkQuit();
  487. }
  488. Init();
  489. tkExposeFunc(Reshape);
  490. tkReshapeFunc(Reshape);
  491. tkKeyDownFunc(Key);
  492. tkDisplayFunc(Draw);
  493. tkIdleFunc(Animate);
  494. tkExec();
  495. }