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>
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
[] = {
57 "../images/girl2.rgb",
61 "../images/tree3.rgb",
63 "../images/reflect.rgb",
64 "../images/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
;
79 static PFNGLBINDPROGRAMARBPROC bind_program
;
80 static PFNGLPROGRAMSTRINGARBPROC program_string
;
81 static PFNGLGENPROGRAMSARBPROC gen_programs
;
85 PrintString(const char *s
)
88 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
94 static void Idle(void)
96 static int lastTime
= 0;
97 int t
= glutGet(GLUT_ELAPSED_TIME
);
101 else if (t
- lastTime
< 10)
107 if ((texZ
< 0.0) || ((GLint
) texZ
> num_layers
)) {
108 texZ_dir
= -texZ_dir
;
115 static void Display(void)
119 glClear(GL_COLOR_BUFFER_BIT
);
121 glMatrixMode(GL_PROJECTION
);
123 glOrtho(-1, 1, -1, 1, -1, 1);
124 glMatrixMode(GL_MODELVIEW
);
127 (*bind_program
)(GL_FRAGMENT_PROGRAM_ARB
, 0);
129 glRasterPos3f(-0.9, -0.9, 0.0);
130 sprintf(str
, "Texture Z coordinate = %4.1f", texZ
);
133 (*bind_program
)(GL_FRAGMENT_PROGRAM_ARB
, 1);
135 glEnable(GL_TEXTURE_2D_ARRAY_EXT
);
138 glMatrixMode(GL_PROJECTION
);
140 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
141 glMatrixMode(GL_MODELVIEW
);
143 glTranslatef(0.0, 0.0, -8.0);
146 glRotatef(Xrot
, 1, 0, 0);
147 glRotatef(Yrot
, 0, 1, 0);
148 glRotatef(Zrot
, 0, 0, 1);
151 glTexCoord3f(0.0, 0.0, texZ
); glVertex2f(-1.0, -1.0);
152 glTexCoord3f(2.0, 0.0, texZ
); glVertex2f(1.0, -1.0);
153 glTexCoord3f(2.0, 2.0, texZ
); glVertex2f(1.0, 1.0);
154 glTexCoord3f(0.0, 2.0, texZ
); glVertex2f(-1.0, 1.0);
159 glDisable(GL_TEXTURE_2D_ARRAY_EXT
);
161 (*bind_program
)(GL_FRAGMENT_PROGRAM_ARB
, 0);
168 static void Reshape(int width
, int height
)
170 glViewport(0, 0, width
, height
);
174 static void Key(unsigned char key
, int x
, int y
)
187 static void SpecialKey(int key
, int x
, int y
)
189 const GLfloat step
= 3.0;
210 static int FindLine(const char *program
, int position
)
213 for (i
= 0; i
< position
; i
++) {
214 if (program
[i
] == '\n')
222 compile_fragment_program(GLuint id
, const char *prog
)
228 (*bind_program
)(GL_FRAGMENT_PROGRAM_ARB
, id
);
229 (*program_string
)(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
230 strlen(prog
), (const GLubyte
*) prog
);
232 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &errorPos
);
234 if (err
!= GL_NO_ERROR
|| errorPos
!= -1) {
235 int l
= FindLine(prog
, errorPos
);
237 printf("Fragment Program Error (err=%d, pos=%d line=%d): %s\n",
239 (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB
));
245 static void require_extension(const char *ext
)
247 if (!glutExtensionSupported(ext
)) {
248 printf("Sorry, %s not supported by this renderer.\n", ext
);
254 static void Init(void)
256 const char *const ver_string
= (const char *const) glGetString(GL_VERSION
);
259 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
260 printf("GL_VERSION = %s\n", ver_string
);
262 require_extension("GL_ARB_fragment_program");
263 require_extension("GL_MESA_texture_array");
264 require_extension("GL_SGIS_generate_mipmap");
266 bind_program
= glutGetProcAddress("glBindProgramARB");
267 program_string
= glutGetProcAddress("glProgramStringARB");
268 gen_programs
= glutGetProcAddress("glGenProgramsARB");
271 for (num_layers
= 0; textures
[num_layers
] != NULL
; num_layers
++)
274 glBindTexture(GL_TEXTURE_2D_ARRAY_EXT
, 1);
275 glTexImage3D(GL_TEXTURE_2D_ARRAY_EXT
, 0, GL_RGB8
,
276 256, 256, num_layers
, 0, GL_RGB
, GL_UNSIGNED_BYTE
, NULL
);
279 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT
, GL_GENERATE_MIPMAP_SGIS
,
282 for (i
= 0; textures
[i
] != NULL
; i
++) {
286 GLubyte
*image
= LoadRGBImage(textures
[i
], &width
, &height
, &format
);
288 printf("Error: could not load texture image %s\n", textures
[i
]);
292 /* resize to 256 x 256 */
293 if (width
!= 256 || height
!= 256) {
294 GLubyte
*newImage
= malloc(256 * 256 * 4);
295 gluScaleImage(format
, width
, height
, GL_UNSIGNED_BYTE
, image
,
296 256, 256, GL_UNSIGNED_BYTE
, newImage
);
301 glTexSubImage3D(GL_TEXTURE_2D_ARRAY_EXT
, 0,
302 0, 0, i
, 256, 256, 1,
303 format
, GL_UNSIGNED_BYTE
, image
);
308 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
309 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
310 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
312 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
314 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
317 compile_fragment_program(1, frag_prog
);
322 int main(int argc
, char *argv
[])
324 glutInit(&argc
, argv
);
325 glutInitWindowPosition(0, 0);
326 glutInitWindowSize(350, 350);
327 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
);
328 glutCreateWindow("Array texture test");
329 glutReshapeFunc(Reshape
);
330 glutKeyboardFunc(Key
);
331 glutSpecialFunc(SpecialKey
);
332 glutDisplayFunc(Display
);