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.

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