2 * (C) Copyright IBM Corporation 2007
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:
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
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
26 * \file arraytexture.c
29 * \author Ian Romanick <idr@us.ibm.com>
38 #include "glut_wrap.h"
40 #if !defined(GL_EXT_texture_array) && !defined(GL_MESA_texture_array)
41 # error "This demo requires enums for either GL_EXT_texture_array or GL_MESA_texture_array to build."
46 #define GL_CHECK_ERROR() \
48 GLenum err = glGetError(); \
50 printf("%s:%u: %s (0x%04x)\n", __FILE__, __LINE__, \
51 gluErrorString(err), err); \
55 static const char *const textures
[] = {
56 DEMOS_DATA_DIR
"girl.rgb",
57 DEMOS_DATA_DIR
"girl2.rgb",
58 DEMOS_DATA_DIR
"arch.rgb",
59 DEMOS_DATA_DIR
"s128.rgb",
61 DEMOS_DATA_DIR
"tree3.rgb",
62 DEMOS_DATA_DIR
"bw.rgb",
63 DEMOS_DATA_DIR
"reflect.rgb",
64 DEMOS_DATA_DIR
"wrs_logo.rgb",
68 static const char frag_prog
[] =
70 "OPTION MESA_texture_array;\n"
71 "TEX result.color, fragment.texcoord[0], texture[0], ARRAY2D;\n"
74 static GLfloat Xrot
= 0, Yrot
= -30, Zrot
= 0;
75 static GLfloat texZ
= 0.0;
76 static GLfloat texZ_dir
= 0.01;
77 static GLint num_layers
;
81 PrintString(const char *s
)
84 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
90 static void Idle(void)
92 static int lastTime
= 0;
93 int t
= glutGet(GLUT_ELAPSED_TIME
);
97 else if (t
- lastTime
< 10)
103 if ((texZ
< 0.0) || ((GLint
) texZ
> num_layers
)) {
104 texZ_dir
= -texZ_dir
;
111 static void Display(void)
115 glClear(GL_COLOR_BUFFER_BIT
);
117 glMatrixMode(GL_PROJECTION
);
119 glOrtho(-1, 1, -1, 1, -1, 1);
120 glMatrixMode(GL_MODELVIEW
);
123 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, 0);
125 glRasterPos3f(-0.9, -0.9, 0.0);
126 sprintf(str
, "Texture Z coordinate = %4.1f", texZ
);
129 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, 1);
131 glEnable(GL_TEXTURE_2D_ARRAY_EXT
);
134 glMatrixMode(GL_PROJECTION
);
136 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
137 glMatrixMode(GL_MODELVIEW
);
139 glTranslatef(0.0, 0.0, -8.0);
142 glRotatef(Xrot
, 1, 0, 0);
143 glRotatef(Yrot
, 0, 1, 0);
144 glRotatef(Zrot
, 0, 0, 1);
147 glTexCoord3f(0.0, 0.0, texZ
); glVertex2f(-1.0, -1.0);
148 glTexCoord3f(2.0, 0.0, texZ
); glVertex2f(1.0, -1.0);
149 glTexCoord3f(2.0, 2.0, texZ
); glVertex2f(1.0, 1.0);
150 glTexCoord3f(0.0, 2.0, texZ
); glVertex2f(-1.0, 1.0);
155 glDisable(GL_TEXTURE_2D_ARRAY_EXT
);
157 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, 0);
164 static void Reshape(int width
, int height
)
166 glViewport(0, 0, width
, height
);
170 static void Key(unsigned char key
, int x
, int y
)
183 static void SpecialKey(int key
, int x
, int y
)
185 const GLfloat step
= 3.0;
206 static int FindLine(const char *program
, int position
)
209 for (i
= 0; i
< position
; i
++) {
210 if (program
[i
] == '\n')
218 compile_fragment_program(GLuint id
, const char *prog
)
224 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, id
);
225 glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
226 strlen(prog
), (const GLubyte
*) prog
);
228 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &errorPos
);
230 if (err
!= GL_NO_ERROR
|| errorPos
!= -1) {
231 int l
= FindLine(prog
, errorPos
);
233 printf("Fragment Program Error (err=%d, pos=%d line=%d): %s\n",
235 (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB
));
241 static void require_extension(const char *ext
)
243 if (!glutExtensionSupported(ext
)) {
244 printf("Sorry, %s not supported by this renderer.\n", ext
);
250 static void Init(void)
252 const char *const ver_string
= (const char *) glGetString(GL_VERSION
);
255 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
256 printf("GL_VERSION = %s\n", ver_string
);
258 require_extension("GL_ARB_fragment_program");
259 require_extension("GL_MESA_texture_array");
260 require_extension("GL_SGIS_generate_mipmap");
262 for (num_layers
= 0; textures
[num_layers
] != NULL
; num_layers
++)
265 glBindTexture(GL_TEXTURE_2D_ARRAY_EXT
, 1);
266 glTexImage3D(GL_TEXTURE_2D_ARRAY_EXT
, 0, GL_RGB8
,
267 256, 256, num_layers
, 0, GL_RGB
, GL_UNSIGNED_BYTE
, NULL
);
270 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT
, GL_GENERATE_MIPMAP_SGIS
,
273 for (i
= 0; textures
[i
] != NULL
; i
++) {
277 GLubyte
*image
= LoadRGBImage(textures
[i
], &width
, &height
, &format
);
279 printf("Error: could not load texture image %s\n", textures
[i
]);
283 /* resize to 256 x 256 */
284 if (width
!= 256 || height
!= 256) {
285 GLubyte
*newImage
= malloc(256 * 256 * 4);
286 gluScaleImage(format
, width
, height
, GL_UNSIGNED_BYTE
, image
,
287 256, 256, GL_UNSIGNED_BYTE
, newImage
);
292 glTexSubImage3D(GL_TEXTURE_2D_ARRAY_EXT
, 0,
293 0, 0, i
, 256, 256, 1,
294 format
, GL_UNSIGNED_BYTE
, image
);
299 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
300 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
301 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
303 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
305 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
308 compile_fragment_program(1, frag_prog
);
313 int main(int argc
, char *argv
[])
315 glutInit(&argc
, argv
);
316 glutInitWindowPosition(0, 0);
317 glutInitWindowSize(350, 350);
318 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
);
319 glutCreateWindow("Array texture test");
321 glutReshapeFunc(Reshape
);
322 glutKeyboardFunc(Key
);
323 glutSpecialFunc(SpecialKey
);
324 glutDisplayFunc(Display
);