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.

arraytexture.c 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * (C) Copyright IBM Corporation 2007
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * on the rights to use, copy, modify, merge, publish, distribute, sub
  9. * license, and/or sell copies of the Software, and to permit persons to whom
  10. * the Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  19. * IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. /**
  25. * \file arraytexture.c
  26. *
  27. *
  28. * \author Ian Romanick <idr@us.ibm.com>
  29. */
  30. #include <assert.h>
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <math.h>
  35. #include <GL/glew.h>
  36. #include <GL/glut.h>
  37. #include <GL/glext.h>
  38. #if !defined(GL_EXT_texture_array) && !defined(GL_MESA_texture_array)
  39. # error "This demo requires enums for either GL_EXT_texture_array or GL_MESA_texture_array to build."
  40. #endif
  41. #include "readtex.h"
  42. #define GL_CHECK_ERROR() \
  43. do { \
  44. GLenum err = glGetError(); \
  45. if (err) { \
  46. printf("%s:%u: %s (0x%04x)\n", __FILE__, __LINE__, \
  47. gluErrorString(err), err); \
  48. } \
  49. } while (0)
  50. static const char *const textures[] = {
  51. "../images/girl.rgb",
  52. "../images/girl2.rgb",
  53. "../images/arch.rgb",
  54. "../images/s128.rgb",
  55. "../images/tree3.rgb",
  56. "../images/bw.rgb",
  57. "../images/reflect.rgb",
  58. "../images/wrs_logo.rgb",
  59. NULL
  60. };
  61. static const char frag_prog[] =
  62. "!!ARBfp1.0\n"
  63. "OPTION MESA_texture_array;\n"
  64. "TEX result.color, fragment.texcoord[0], texture[0], ARRAY2D;\n"
  65. "END\n";
  66. static GLfloat Xrot = 0, Yrot = -30, Zrot = 0;
  67. static GLfloat texZ = 0.0;
  68. static GLfloat texZ_dir = 0.01;
  69. static GLint num_layers;
  70. static void
  71. PrintString(const char *s)
  72. {
  73. while (*s) {
  74. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  75. s++;
  76. }
  77. }
  78. static void Idle(void)
  79. {
  80. static int lastTime = 0;
  81. int t = glutGet(GLUT_ELAPSED_TIME);
  82. if (lastTime == 0)
  83. lastTime = t;
  84. else if (t - lastTime < 10)
  85. return;
  86. lastTime = t;
  87. texZ += texZ_dir;
  88. if ((texZ < 0.0) || ((GLint) texZ > num_layers)) {
  89. texZ_dir = -texZ_dir;
  90. }
  91. glutPostRedisplay();
  92. }
  93. static void Display(void)
  94. {
  95. char str[100];
  96. glClear(GL_COLOR_BUFFER_BIT);
  97. glMatrixMode(GL_PROJECTION);
  98. glLoadIdentity();
  99. glOrtho(-1, 1, -1, 1, -1, 1);
  100. glMatrixMode(GL_MODELVIEW);
  101. glLoadIdentity();
  102. glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, 0);
  103. glColor3f(1,1,1);
  104. glRasterPos3f(-0.9, -0.9, 0.0);
  105. sprintf(str, "Texture Z coordinate = %4.1f", texZ);
  106. PrintString(str);
  107. glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, 1);
  108. GL_CHECK_ERROR();
  109. glEnable(GL_TEXTURE_2D_ARRAY_EXT);
  110. GL_CHECK_ERROR();
  111. glMatrixMode(GL_PROJECTION);
  112. glLoadIdentity();
  113. glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
  114. glMatrixMode(GL_MODELVIEW);
  115. glLoadIdentity();
  116. glTranslatef(0.0, 0.0, -8.0);
  117. glPushMatrix();
  118. glRotatef(Xrot, 1, 0, 0);
  119. glRotatef(Yrot, 0, 1, 0);
  120. glRotatef(Zrot, 0, 0, 1);
  121. glBegin(GL_QUADS);
  122. glTexCoord3f(0.0, 0.0, texZ); glVertex2f(-1.0, -1.0);
  123. glTexCoord3f(2.0, 0.0, texZ); glVertex2f(1.0, -1.0);
  124. glTexCoord3f(2.0, 2.0, texZ); glVertex2f(1.0, 1.0);
  125. glTexCoord3f(0.0, 2.0, texZ); glVertex2f(-1.0, 1.0);
  126. glEnd();
  127. glPopMatrix();
  128. glDisable(GL_TEXTURE_2D_ARRAY_EXT);
  129. GL_CHECK_ERROR();
  130. glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, 0);
  131. GL_CHECK_ERROR();
  132. glutSwapBuffers();
  133. }
  134. static void Reshape(int width, int height)
  135. {
  136. glViewport(0, 0, width, height);
  137. }
  138. static void Key(unsigned char key, int x, int y)
  139. {
  140. (void) x;
  141. (void) y;
  142. switch (key) {
  143. case 27:
  144. exit(0);
  145. break;
  146. }
  147. glutPostRedisplay();
  148. }
  149. static void SpecialKey(int key, int x, int y)
  150. {
  151. const GLfloat step = 3.0;
  152. (void) x;
  153. (void) y;
  154. switch (key) {
  155. case GLUT_KEY_UP:
  156. Xrot -= step;
  157. break;
  158. case GLUT_KEY_DOWN:
  159. Xrot += step;
  160. break;
  161. case GLUT_KEY_LEFT:
  162. Yrot -= step;
  163. break;
  164. case GLUT_KEY_RIGHT:
  165. Yrot += step;
  166. break;
  167. }
  168. glutPostRedisplay();
  169. }
  170. static int FindLine(const char *program, int position)
  171. {
  172. int i, line = 1;
  173. for (i = 0; i < position; i++) {
  174. if (program[i] == '\n')
  175. line++;
  176. }
  177. return line;
  178. }
  179. static void
  180. compile_fragment_program(GLuint id, const char *prog)
  181. {
  182. int errorPos;
  183. int err;
  184. err = glGetError();
  185. glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, id);
  186. glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
  187. strlen(prog), (const GLubyte *) prog);
  188. glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorPos);
  189. err = glGetError();
  190. if (err != GL_NO_ERROR || errorPos != -1) {
  191. int l = FindLine(prog, errorPos);
  192. printf("Fragment Program Error (err=%d, pos=%d line=%d): %s\n",
  193. err, errorPos, l,
  194. (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB));
  195. exit(0);
  196. }
  197. }
  198. static void require_extension(const char *ext)
  199. {
  200. if (!glutExtensionSupported(ext)) {
  201. printf("Sorry, %s not supported by this renderer.\n", ext);
  202. exit(1);
  203. }
  204. }
  205. static void Init(void)
  206. {
  207. const char *const ver_string = (const char *const) glGetString(GL_VERSION);
  208. unsigned i;
  209. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  210. printf("GL_VERSION = %s\n", ver_string);
  211. require_extension("GL_ARB_fragment_program");
  212. require_extension("GL_MESA_texture_array");
  213. require_extension("GL_SGIS_generate_mipmap");
  214. for (num_layers = 0; textures[num_layers] != NULL; num_layers++)
  215. /* empty */ ;
  216. glBindTexture(GL_TEXTURE_2D_ARRAY_EXT, 1);
  217. glTexImage3D(GL_TEXTURE_2D_ARRAY_EXT, 0, GL_RGB8,
  218. 256, 256, num_layers, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
  219. GL_CHECK_ERROR();
  220. glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_GENERATE_MIPMAP_SGIS,
  221. GL_TRUE);
  222. for (i = 0; textures[i] != NULL; i++) {
  223. GLint width, height;
  224. GLenum format;
  225. GLubyte *image = LoadRGBImage(textures[i], &width, &height, &format);
  226. if (!image) {
  227. printf("Error: could not load texture image %s\n", textures[i]);
  228. exit(1);
  229. }
  230. /* resize to 256 x 256 */
  231. if (width != 256 || height != 256) {
  232. GLubyte *newImage = malloc(256 * 256 * 4);
  233. gluScaleImage(format, width, height, GL_UNSIGNED_BYTE, image,
  234. 256, 256, GL_UNSIGNED_BYTE, newImage);
  235. free(image);
  236. image = newImage;
  237. }
  238. glTexSubImage3D(GL_TEXTURE_2D_ARRAY_EXT, 0,
  239. 0, 0, i, 256, 256, 1,
  240. format, GL_UNSIGNED_BYTE, image);
  241. free(image);
  242. }
  243. GL_CHECK_ERROR();
  244. glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_WRAP_S, GL_REPEAT);
  245. glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_WRAP_T, GL_REPEAT);
  246. glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  247. glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  248. GL_CHECK_ERROR();
  249. glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  250. GL_CHECK_ERROR();
  251. compile_fragment_program(1, frag_prog);
  252. GL_CHECK_ERROR();
  253. }
  254. int main(int argc, char *argv[])
  255. {
  256. glutInit(&argc, argv);
  257. glutInitWindowPosition(0, 0);
  258. glutInitWindowSize(350, 350);
  259. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  260. glutCreateWindow("Array texture test");
  261. glewInit();
  262. glutReshapeFunc(Reshape);
  263. glutKeyboardFunc(Key);
  264. glutSpecialFunc(SpecialKey);
  265. glutDisplayFunc(Display);
  266. glutIdleFunc(Idle);
  267. Init();
  268. glutMainLoop();
  269. return 0;
  270. }