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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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/glut.h>
  36. #include <GL/glext.h>
  37. #if !defined(GL_EXT_texture_array) && !defined(GL_MESA_texture_array)
  38. # error "This demo requires enums for either GL_EXT_texture_array or GL_MESA_texture_array to build."
  39. #endif
  40. #include "readtex.h"
  41. #define GL_CHECK_ERROR() \
  42. do { \
  43. GLenum err = glGetError(); \
  44. if (err) { \
  45. printf("%s:%u: %s (0x%04x)\n", __FILE__, __LINE__, \
  46. gluErrorString(err), err); \
  47. } \
  48. } while (0)
  49. static const char *const textures[] = {
  50. "../images/girl.rgb",
  51. "../images/girl2.rgb",
  52. "../images/arch.rgb",
  53. "../images/s128.rgb",
  54. "../images/tree3.rgb",
  55. "../images/bw.rgb",
  56. "../images/reflect.rgb",
  57. "../images/wrs_logo.rgb",
  58. NULL
  59. };
  60. static const char frag_prog[] =
  61. "!!ARBfp1.0\n"
  62. "OPTION MESA_texture_array;\n"
  63. "TEX result.color, fragment.texcoord[0], texture[0], ARRAY2D;\n"
  64. "END\n";
  65. static GLfloat Xrot = 0, Yrot = -30, Zrot = 0;
  66. static GLfloat texZ = 0.0;
  67. static GLfloat texZ_dir = 0.01;
  68. static GLint num_layers;
  69. static PFNGLBINDPROGRAMARBPROC bind_program;
  70. static PFNGLPROGRAMSTRINGARBPROC program_string;
  71. static PFNGLGENPROGRAMSARBPROC gen_programs;
  72. static void
  73. PrintString(const char *s)
  74. {
  75. while (*s) {
  76. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  77. s++;
  78. }
  79. }
  80. static void Idle(void)
  81. {
  82. static int lastTime = 0;
  83. int t = glutGet(GLUT_ELAPSED_TIME);
  84. if (lastTime == 0)
  85. lastTime = t;
  86. else if (t - lastTime < 10)
  87. return;
  88. lastTime = t;
  89. texZ += texZ_dir;
  90. if ((texZ < 0.0) || ((GLint) texZ > num_layers)) {
  91. texZ_dir = -texZ_dir;
  92. }
  93. glutPostRedisplay();
  94. }
  95. static void Display(void)
  96. {
  97. char str[100];
  98. glClear(GL_COLOR_BUFFER_BIT);
  99. glMatrixMode(GL_PROJECTION);
  100. glLoadIdentity();
  101. glOrtho(-1, 1, -1, 1, -1, 1);
  102. glMatrixMode(GL_MODELVIEW);
  103. glLoadIdentity();
  104. (*bind_program)(GL_FRAGMENT_PROGRAM_ARB, 0);
  105. glColor3f(1,1,1);
  106. glRasterPos3f(-0.9, -0.9, 0.0);
  107. sprintf(str, "Texture Z coordinate = %4.1f", texZ);
  108. PrintString(str);
  109. (*bind_program)(GL_FRAGMENT_PROGRAM_ARB, 1);
  110. GL_CHECK_ERROR();
  111. glEnable(GL_TEXTURE_2D_ARRAY_EXT);
  112. GL_CHECK_ERROR();
  113. glMatrixMode(GL_PROJECTION);
  114. glLoadIdentity();
  115. glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
  116. glMatrixMode(GL_MODELVIEW);
  117. glLoadIdentity();
  118. glTranslatef(0.0, 0.0, -8.0);
  119. glPushMatrix();
  120. glRotatef(Xrot, 1, 0, 0);
  121. glRotatef(Yrot, 0, 1, 0);
  122. glRotatef(Zrot, 0, 0, 1);
  123. glBegin(GL_QUADS);
  124. glTexCoord3f(0.0, 0.0, texZ); glVertex2f(-1.0, -1.0);
  125. glTexCoord3f(2.0, 0.0, texZ); glVertex2f(1.0, -1.0);
  126. glTexCoord3f(2.0, 2.0, texZ); glVertex2f(1.0, 1.0);
  127. glTexCoord3f(0.0, 2.0, texZ); glVertex2f(-1.0, 1.0);
  128. glEnd();
  129. glPopMatrix();
  130. glDisable(GL_TEXTURE_2D_ARRAY_EXT);
  131. GL_CHECK_ERROR();
  132. (*bind_program)(GL_FRAGMENT_PROGRAM_ARB, 0);
  133. GL_CHECK_ERROR();
  134. glutSwapBuffers();
  135. }
  136. static void Reshape(int width, int height)
  137. {
  138. glViewport(0, 0, width, height);
  139. }
  140. static void Key(unsigned char key, int x, int y)
  141. {
  142. (void) x;
  143. (void) y;
  144. switch (key) {
  145. case 27:
  146. exit(0);
  147. break;
  148. }
  149. glutPostRedisplay();
  150. }
  151. static void SpecialKey(int key, int x, int y)
  152. {
  153. const GLfloat step = 3.0;
  154. (void) x;
  155. (void) y;
  156. switch (key) {
  157. case GLUT_KEY_UP:
  158. Xrot -= step;
  159. break;
  160. case GLUT_KEY_DOWN:
  161. Xrot += step;
  162. break;
  163. case GLUT_KEY_LEFT:
  164. Yrot -= step;
  165. break;
  166. case GLUT_KEY_RIGHT:
  167. Yrot += step;
  168. break;
  169. }
  170. glutPostRedisplay();
  171. }
  172. static int FindLine(const char *program, int position)
  173. {
  174. int i, line = 1;
  175. for (i = 0; i < position; i++) {
  176. if (program[i] == '\n')
  177. line++;
  178. }
  179. return line;
  180. }
  181. static void
  182. compile_fragment_program(GLuint id, const char *prog)
  183. {
  184. int errorPos;
  185. int err;
  186. err = glGetError();
  187. (*bind_program)(GL_FRAGMENT_PROGRAM_ARB, id);
  188. (*program_string)(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
  189. strlen(prog), (const GLubyte *) prog);
  190. glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorPos);
  191. err = glGetError();
  192. if (err != GL_NO_ERROR || errorPos != -1) {
  193. int l = FindLine(prog, errorPos);
  194. printf("Fragment Program Error (err=%d, pos=%d line=%d): %s\n",
  195. err, errorPos, l,
  196. (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB));
  197. exit(0);
  198. }
  199. }
  200. static void require_extension(const char *ext)
  201. {
  202. if (!glutExtensionSupported(ext)) {
  203. printf("Sorry, %s not supported by this renderer.\n", ext);
  204. exit(1);
  205. }
  206. }
  207. static void Init(void)
  208. {
  209. const char *const ver_string = (const char *const) glGetString(GL_VERSION);
  210. unsigned i;
  211. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  212. printf("GL_VERSION = %s\n", ver_string);
  213. require_extension("GL_ARB_fragment_program");
  214. require_extension("GL_MESA_texture_array");
  215. require_extension("GL_SGIS_generate_mipmap");
  216. bind_program = glutGetProcAddress("glBindProgramARB");
  217. program_string = glutGetProcAddress("glProgramStringARB");
  218. gen_programs = glutGetProcAddress("glGenProgramsARB");
  219. for (num_layers = 0; textures[num_layers] != NULL; num_layers++)
  220. /* empty */ ;
  221. glBindTexture(GL_TEXTURE_2D_ARRAY_EXT, 1);
  222. glTexImage3D(GL_TEXTURE_2D_ARRAY_EXT, 0, GL_RGB8,
  223. 256, 256, num_layers, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
  224. GL_CHECK_ERROR();
  225. glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_GENERATE_MIPMAP_SGIS,
  226. GL_TRUE);
  227. for (i = 0; textures[i] != NULL; i++) {
  228. GLint width, height;
  229. GLenum format;
  230. GLubyte *image = LoadRGBImage(textures[i], &width, &height, &format);
  231. if (!image) {
  232. printf("Error: could not load texture image %s\n", textures[i]);
  233. exit(1);
  234. }
  235. /* resize to 256 x 256 */
  236. if (width != 256 || height != 256) {
  237. GLubyte *newImage = malloc(256 * 256 * 4);
  238. gluScaleImage(format, width, height, GL_UNSIGNED_BYTE, image,
  239. 256, 256, GL_UNSIGNED_BYTE, newImage);
  240. free(image);
  241. image = newImage;
  242. }
  243. glTexSubImage3D(GL_TEXTURE_2D_ARRAY_EXT, 0,
  244. 0, 0, i, 256, 256, 1,
  245. format, GL_UNSIGNED_BYTE, image);
  246. free(image);
  247. }
  248. GL_CHECK_ERROR();
  249. glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_WRAP_S, GL_REPEAT);
  250. glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_WRAP_T, GL_REPEAT);
  251. glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  252. glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  253. GL_CHECK_ERROR();
  254. glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  255. GL_CHECK_ERROR();
  256. compile_fragment_program(1, frag_prog);
  257. GL_CHECK_ERROR();
  258. }
  259. int main(int argc, char *argv[])
  260. {
  261. glutInit(&argc, argv);
  262. glutInitWindowPosition(0, 0);
  263. glutInitWindowSize(350, 350);
  264. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  265. glutCreateWindow("Array texture test");
  266. glutReshapeFunc(Reshape);
  267. glutKeyboardFunc(Key);
  268. glutSpecialFunc(SpecialKey);
  269. glutDisplayFunc(Display);
  270. glutIdleFunc(Idle);
  271. Init();
  272. glutMainLoop();
  273. return 0;
  274. }