Clone of mesa.
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.

texture.c 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and
  5. * its documentation for any purpose is hereby granted without fee, provided
  6. * that (i) the above copyright notices and this permission notice appear in
  7. * all copies of the software and related documentation, and (ii) the name of
  8. * Silicon Graphics may not be used in any advertising or
  9. * publicity relating to the software without the specific, prior written
  10. * permission of Silicon Graphics.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13. * ANY KIND,
  14. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22. * OF THIS SOFTWARE.
  23. */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <math.h>
  27. #include <stdlib.h>
  28. #include <GL/glut.h>
  29. #include "loadppm.c"
  30. GLenum doubleBuffer;
  31. char *texFileName = 0;
  32. PPMImage *image;
  33. float *minFilter, *magFilter, *sWrapMode, *tWrapMode;
  34. float decal[] = {GL_DECAL};
  35. float modulate[] = {GL_MODULATE};
  36. float repeat[] = {GL_REPEAT};
  37. float clamp[] = {GL_CLAMP};
  38. float nr[] = {GL_NEAREST};
  39. float ln[] = {GL_LINEAR};
  40. float nr_mipmap_nr[] = {GL_NEAREST_MIPMAP_NEAREST};
  41. float nr_mipmap_ln[] = {GL_NEAREST_MIPMAP_LINEAR};
  42. float ln_mipmap_nr[] = {GL_LINEAR_MIPMAP_NEAREST};
  43. float ln_mipmap_ln[] = {GL_LINEAR_MIPMAP_LINEAR};
  44. GLint sphereMap[] = {GL_SPHERE_MAP};
  45. GLenum doSphere = GL_FALSE;
  46. float xRotation = 0.0, yRotation = 0.0, zTranslate = -3.125;
  47. GLint cube;
  48. float c[6][4][3] = {
  49. {
  50. {
  51. 1.0, 1.0, -1.0
  52. },
  53. {
  54. -1.0, 1.0, -1.0
  55. },
  56. {
  57. -1.0, -1.0, -1.0
  58. },
  59. {
  60. 1.0, -1.0, -1.0
  61. }
  62. },
  63. {
  64. {
  65. 1.0, 1.0, 1.0
  66. },
  67. {
  68. 1.0, 1.0, -1.0
  69. },
  70. {
  71. 1.0, -1.0, -1.0
  72. },
  73. {
  74. 1.0, -1.0, 1.0
  75. }
  76. },
  77. {
  78. {
  79. -1.0, 1.0, 1.0
  80. },
  81. {
  82. 1.0, 1.0, 1.0
  83. },
  84. {
  85. 1.0, -1.0, 1.0
  86. },
  87. {
  88. -1.0, -1.0, 1.0
  89. }
  90. },
  91. {
  92. {
  93. -1.0, 1.0, -1.0
  94. },
  95. {
  96. -1.0, 1.0, 1.0
  97. },
  98. {
  99. -1.0, -1.0, 1.0
  100. },
  101. {
  102. -1.0, -1.0, -1.0
  103. }
  104. },
  105. {
  106. {
  107. -1.0, 1.0, 1.0
  108. },
  109. {
  110. -1.0, 1.0, -1.0
  111. },
  112. {
  113. 1.0, 1.0, -1.0
  114. },
  115. {
  116. 1.0, 1.0, 1.0
  117. }
  118. },
  119. {
  120. {
  121. -1.0, -1.0, -1.0
  122. },
  123. {
  124. -1.0, -1.0, 1.0
  125. },
  126. {
  127. 1.0, -1.0, 1.0
  128. },
  129. {
  130. 1.0, -1.0, -1.0
  131. }
  132. }
  133. };
  134. static float n[6][3] = {
  135. {
  136. 0.0, 0.0, -1.0
  137. },
  138. {
  139. 1.0, 0.0, 0.0
  140. },
  141. {
  142. 0.0, 0.0, 1.0
  143. },
  144. {
  145. -1.0, 0.0, 0.0
  146. },
  147. {
  148. 0.0, 1.0, 0.0
  149. },
  150. {
  151. 0.0, -1.0, 0.0
  152. }
  153. };
  154. static float t[6][4][2] = {
  155. {
  156. {
  157. 1.1, 1.1
  158. },
  159. {
  160. -0.1, 1.1
  161. },
  162. {
  163. -0.1, -0.1
  164. },
  165. {
  166. 1.1, -0.1
  167. }
  168. },
  169. {
  170. {
  171. 1.1, 1.1
  172. },
  173. {
  174. -0.1, 1.1
  175. },
  176. {
  177. -0.1, -0.1
  178. },
  179. {
  180. 1.1, -0.1
  181. }
  182. },
  183. {
  184. {
  185. -0.1, 1.1
  186. },
  187. {
  188. 1.1, 1.1
  189. },
  190. {
  191. 1.1, -0.1
  192. },
  193. {
  194. -0.1, -0.1
  195. }
  196. },
  197. {
  198. {
  199. 1.1, 1.1
  200. },
  201. {
  202. -0.1, 1.1
  203. },
  204. {
  205. -0.1, -0.1
  206. },
  207. {
  208. 1.1, -0.1
  209. }
  210. },
  211. {
  212. {
  213. 1.1, 1.1
  214. },
  215. {
  216. -0.1, 1.1
  217. },
  218. {
  219. -0.1, -0.1
  220. },
  221. {
  222. 1.1, -0.1
  223. }
  224. },
  225. {
  226. {
  227. 1.1, 1.1
  228. },
  229. {
  230. -0.1, 1.1
  231. },
  232. {
  233. -0.1, -0.1
  234. },
  235. {
  236. 1.1, -0.1
  237. }
  238. },
  239. };
  240. static void BuildCube(void)
  241. {
  242. GLint i;
  243. glNewList(cube, GL_COMPILE);
  244. for (i = 0; i < 6; i++) {
  245. glBegin(GL_POLYGON);
  246. glNormal3fv(n[i]); glTexCoord2fv(t[i][0]); glVertex3fv(c[i][0]);
  247. glNormal3fv(n[i]); glTexCoord2fv(t[i][1]); glVertex3fv(c[i][1]);
  248. glNormal3fv(n[i]); glTexCoord2fv(t[i][2]); glVertex3fv(c[i][2]);
  249. glNormal3fv(n[i]); glTexCoord2fv(t[i][3]); glVertex3fv(c[i][3]);
  250. glEnd();
  251. }
  252. glEndList();
  253. }
  254. static void BuildLists(void)
  255. {
  256. cube = glGenLists(1);
  257. BuildCube();
  258. }
  259. static void Init(void)
  260. {
  261. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  262. gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image->sizeX, image->sizeY,
  263. GL_RGB, GL_UNSIGNED_BYTE, image->data);
  264. glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, decal);
  265. glEnable(GL_TEXTURE_2D);
  266. glFrontFace(GL_CCW);
  267. glCullFace(GL_FRONT);
  268. glEnable(GL_CULL_FACE);
  269. BuildLists();
  270. glClearColor(0.0, 0.0, 0.0, 0.0);
  271. magFilter = nr;
  272. minFilter = nr;
  273. sWrapMode = repeat;
  274. tWrapMode = repeat;
  275. }
  276. static void Reshape(int width, int height)
  277. {
  278. glViewport(0, 0, (GLint)width, (GLint)height);
  279. glMatrixMode(GL_PROJECTION);
  280. glLoadIdentity();
  281. gluPerspective(145.0, 1.0, 0.01, 1000);
  282. glMatrixMode(GL_MODELVIEW);
  283. }
  284. static void Key2(int key, int x, int y)
  285. {
  286. switch (key) {
  287. case GLUT_KEY_LEFT:
  288. yRotation -= 0.5;
  289. break;
  290. case GLUT_KEY_RIGHT:
  291. yRotation += 0.5;
  292. break;
  293. case GLUT_KEY_UP:
  294. xRotation -= 0.5;
  295. break;
  296. case GLUT_KEY_DOWN:
  297. xRotation += 0.5;
  298. break;
  299. default:
  300. return;
  301. }
  302. glutPostRedisplay();
  303. }
  304. static void Key(unsigned char key, int x, int y)
  305. {
  306. switch (key) {
  307. case 27:
  308. exit(1);
  309. case 'T':
  310. zTranslate += 0.25;
  311. break;
  312. case 't':
  313. zTranslate -= 0.25;
  314. break;
  315. case 's':
  316. doSphere = !doSphere;
  317. if (doSphere) {
  318. glTexGeniv(GL_S, GL_TEXTURE_GEN_MODE, sphereMap);
  319. glTexGeniv(GL_T, GL_TEXTURE_GEN_MODE, sphereMap);
  320. glEnable(GL_TEXTURE_GEN_S);
  321. glEnable(GL_TEXTURE_GEN_T);
  322. } else {
  323. glDisable(GL_TEXTURE_GEN_S);
  324. glDisable(GL_TEXTURE_GEN_T);
  325. }
  326. break;
  327. case '0':
  328. magFilter = nr;
  329. break;
  330. case '1':
  331. magFilter = ln;
  332. break;
  333. case '2':
  334. minFilter = nr;
  335. break;
  336. case '3':
  337. minFilter = ln;
  338. break;
  339. case '4':
  340. minFilter = nr_mipmap_nr;
  341. break;
  342. case '5':
  343. minFilter = nr_mipmap_ln;
  344. break;
  345. case '6':
  346. minFilter = ln_mipmap_nr;
  347. break;
  348. case '7':
  349. minFilter = ln_mipmap_ln;
  350. break;
  351. default:
  352. return;
  353. }
  354. glutPostRedisplay();
  355. }
  356. static void Draw(void)
  357. {
  358. glClear(GL_COLOR_BUFFER_BIT);
  359. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, sWrapMode);
  360. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tWrapMode);
  361. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  362. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  363. glPushMatrix();
  364. glTranslatef(0.0, 0.0, zTranslate);
  365. glRotatef(xRotation, 1, 0, 0);
  366. glRotatef(yRotation, 0, 1, 0);
  367. glCallList(cube);
  368. glPopMatrix();
  369. glFlush();
  370. if (doubleBuffer) {
  371. glutSwapBuffers();
  372. }
  373. }
  374. static GLenum Args(int argc, char **argv)
  375. {
  376. GLint i;
  377. doubleBuffer = GL_FALSE;
  378. for (i = 1; i < argc; i++) {
  379. if (strcmp(argv[i], "-sb") == 0) {
  380. doubleBuffer = GL_FALSE;
  381. } else if (strcmp(argv[i], "-db") == 0) {
  382. doubleBuffer = GL_TRUE;
  383. } else if (strcmp(argv[i], "-f") == 0) {
  384. if (i+1 >= argc || argv[i+1][0] == '-') {
  385. printf("-f (No file name).\n");
  386. return GL_FALSE;
  387. } else {
  388. texFileName = argv[++i];
  389. }
  390. } else {
  391. printf("%s (Bad option).\n", argv[i]);
  392. return GL_FALSE;
  393. }
  394. }
  395. return GL_TRUE;
  396. }
  397. int main(int argc, char **argv)
  398. {
  399. GLenum type;
  400. glutInit(&argc, argv);
  401. if (Args(argc, argv) == GL_FALSE) {
  402. exit(1);
  403. }
  404. if (texFileName == 0) {
  405. printf("No image file.\n");
  406. exit(1);
  407. }
  408. image = LoadPPM(texFileName);
  409. glutInitWindowPosition(0, 0); glutInitWindowSize( 300, 300);
  410. type = GLUT_RGB;
  411. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  412. glutInitDisplayMode(type);
  413. if (glutCreateWindow("Texture Test") == GL_FALSE) {
  414. exit(1);
  415. }
  416. Init();
  417. glutReshapeFunc(Reshape);
  418. glutKeyboardFunc(Key);
  419. glutSpecialFunc(Key2);
  420. glutDisplayFunc(Draw);
  421. glutMainLoop();
  422. return 0;
  423. }