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.

cubemap.c 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* $Id: cubemap.c,v 1.3 2000/06/27 17:04:43 brianp Exp $ */
  2. /*
  3. * GL_ARB_texture_cube_map demo
  4. *
  5. * Brian Paul
  6. * May 2000
  7. *
  8. *
  9. * Copyright (C) 2000 Brian Paul All Rights Reserved.
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a
  12. * copy of this software and associated documentation files (the "Software"),
  13. * to deal in the Software without restriction, including without limitation
  14. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15. * and/or sell copies of the Software, and to permit persons to whom the
  16. * Software is furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included
  19. * in all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  22. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  24. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  25. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  26. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. /*
  29. * This is a pretty minimalistic demo for now. Eventually, use some
  30. * interesting cube map textures and 3D objects.
  31. * For now, we use 6 checkerboard "walls" and a sphere (good for
  32. * verification purposes).
  33. */
  34. #include <math.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include "GL/glut.h"
  39. static GLfloat Xrot = 0, Yrot = 0;
  40. static void draw( void )
  41. {
  42. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  43. glMatrixMode(GL_TEXTURE);
  44. glLoadIdentity();
  45. glRotatef(Xrot, 1, 0, 0);
  46. glRotatef(Yrot, 0, 1, 0);
  47. glutSolidSphere(2.0, 20, 20);
  48. glMatrixMode(GL_MODELVIEW);
  49. glutSwapBuffers();
  50. }
  51. static void idle(void)
  52. {
  53. Yrot += 5.0;
  54. glutPostRedisplay();
  55. }
  56. static void set_mode(GLuint mode)
  57. {
  58. if (mode == 0) {
  59. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
  60. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
  61. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
  62. printf("GL_REFLECTION_MAP_ARB mode\n");
  63. }
  64. else if (mode == 1) {
  65. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
  66. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
  67. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
  68. printf("GL_NORMAL_MAP_ARB mode\n");
  69. }
  70. glEnable(GL_TEXTURE_GEN_S);
  71. glEnable(GL_TEXTURE_GEN_T);
  72. glEnable(GL_TEXTURE_GEN_R);
  73. }
  74. static void key(unsigned char k, int x, int y)
  75. {
  76. static GLboolean anim = GL_TRUE;
  77. static GLuint mode = 0;
  78. (void) x;
  79. (void) y;
  80. switch (k) {
  81. case ' ':
  82. anim = !anim;
  83. if (anim)
  84. glutIdleFunc(idle);
  85. else
  86. glutIdleFunc(NULL);
  87. break;
  88. case 'm':
  89. mode = !mode;
  90. set_mode(mode);
  91. break;
  92. case 27:
  93. exit(0);
  94. }
  95. glutPostRedisplay();
  96. }
  97. static void specialkey(int key, int x, int y)
  98. {
  99. GLfloat step = 10;
  100. (void) x;
  101. (void) y;
  102. switch (key) {
  103. case GLUT_KEY_UP:
  104. Xrot -= step;
  105. break;
  106. case GLUT_KEY_DOWN:
  107. Xrot += step;
  108. break;
  109. case GLUT_KEY_LEFT:
  110. Yrot -= step;
  111. break;
  112. case GLUT_KEY_RIGHT:
  113. Yrot += step;
  114. break;
  115. }
  116. glutPostRedisplay();
  117. }
  118. /* new window size or exposure */
  119. static void reshape(int width, int height)
  120. {
  121. glViewport(0, 0, (GLint)width, (GLint)height);
  122. glMatrixMode(GL_PROJECTION);
  123. glLoadIdentity();
  124. glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
  125. glMatrixMode(GL_MODELVIEW);
  126. glLoadIdentity();
  127. glTranslatef( 0.0, 0.0, -8.0 );
  128. }
  129. static void init( void )
  130. {
  131. #define CUBE_TEX_SIZE 64
  132. GLubyte image[CUBE_TEX_SIZE][CUBE_TEX_SIZE][3];
  133. static const GLubyte colors[6][3] = {
  134. { 255, 0, 0 },
  135. { 0, 255, 255 },
  136. { 0, 255, 0 },
  137. { 255, 0, 255 },
  138. { 0, 0, 255 },
  139. { 255, 255, 0 }
  140. };
  141. static const GLenum targets[6] = {
  142. GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
  143. GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
  144. GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
  145. GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
  146. GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
  147. GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
  148. };
  149. GLint i, j, f;
  150. /* check for extension */
  151. {
  152. char *exten = (char *) glGetString(GL_EXTENSIONS);
  153. if (!strstr(exten, "GL_ARB_texture_cube_map")) {
  154. printf("Sorry, this demo requires GL_ARB_texture_cube_map\n");
  155. exit(0);
  156. }
  157. }
  158. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  159. /* make colored checkerboard cube faces */
  160. for (f = 0; f < 6; f++) {
  161. for (i = 0; i < CUBE_TEX_SIZE; i++) {
  162. for (j = 0; j < CUBE_TEX_SIZE; j++) {
  163. if ((i/4 + j/4) & 1) {
  164. image[i][j][0] = colors[f][0];
  165. image[i][j][1] = colors[f][1];
  166. image[i][j][2] = colors[f][2];
  167. }
  168. else {
  169. image[i][j][0] = 255;
  170. image[i][j][1] = 255;
  171. image[i][j][2] = 255;
  172. }
  173. }
  174. }
  175. glTexImage2D(targets[f], 0, GL_RGB, CUBE_TEX_SIZE, CUBE_TEX_SIZE, 0,
  176. GL_RGB, GL_UNSIGNED_BYTE, image);
  177. }
  178. #if 1
  179. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  180. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  181. #else
  182. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  183. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  184. #endif
  185. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  186. glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  187. glEnable(GL_TEXTURE_CUBE_MAP_ARB);
  188. glClearColor(.3, .3, .3, 0);
  189. glColor3f( 1.0, 1.0, 1.0 );
  190. set_mode(0);
  191. }
  192. static void usage(void)
  193. {
  194. printf("keys:\n");
  195. printf(" SPACE - toggle animation\n");
  196. printf(" CURSOR KEYS - rotation\n");
  197. printf(" m - toggle texgen reflection mode\n");
  198. }
  199. int main( int argc, char *argv[] )
  200. {
  201. glutInitWindowPosition(0, 0);
  202. glutInitWindowSize(300, 300);
  203. glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
  204. glutCreateWindow("Texture Cube Maping");
  205. init();
  206. glutReshapeFunc( reshape );
  207. glutKeyboardFunc( key );
  208. glutSpecialFunc( specialkey );
  209. glutIdleFunc( idle );
  210. glutDisplayFunc( draw );
  211. usage();
  212. glutMainLoop();
  213. return 0;
  214. }