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>
41 #if !defined(GL_EXT_texture_array) && !defined(GL_MESA_texture_array)
42 # error "This demo requires enums for either GL_EXT_texture_array or GL_MESA_texture_array to build."
47 #define GL_CHECK_ERROR() \
49 GLenum err = glGetError(); \
51 printf("%s:%u: %s (0x%04x)\n", __FILE__, __LINE__, \
52 gluErrorString(err), err); \
56 static const char *const textures
[] = {
58 "../images/girl2.rgb",
62 "../images/tree3.rgb",
64 "../images/reflect.rgb",
65 "../images/wrs_logo.rgb",
69 static const char frag_prog
[] =
71 "OPTION MESA_texture_array;\n"
72 "TEX result.color, fragment.texcoord[0], texture[0], ARRAY2D;\n"
75 static GLfloat Xrot
= 0, Yrot
= -30, Zrot
= 0;
76 static GLfloat texZ
= 0.0;
77 static GLfloat texZ_dir
= 0.01;
78 static GLint num_layers
;
80 static PFNGLBINDPROGRAMARBPROC bind_program
;
81 static PFNGLPROGRAMSTRINGARBPROC program_string
;
82 static PFNGLGENPROGRAMSARBPROC gen_programs
;
86 PrintString(const char *s
)
89 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
95 static void Idle(void)
97 static int lastTime
= 0;
98 int t
= glutGet(GLUT_ELAPSED_TIME
);
102 else if (t
- lastTime
< 10)
108 if ((texZ
< 0.0) || ((GLint
) texZ
> num_layers
)) {
109 texZ_dir
= -texZ_dir
;
116 static void Display(void)
120 glClear(GL_COLOR_BUFFER_BIT
);
122 glMatrixMode(GL_PROJECTION
);
124 glOrtho(-1, 1, -1, 1, -1, 1);
125 glMatrixMode(GL_MODELVIEW
);
128 (*bind_program
)(GL_FRAGMENT_PROGRAM_ARB
, 0);
130 glRasterPos3f(-0.9, -0.9, 0.0);
131 sprintf(str
, "Texture Z coordinate = %4.1f", texZ
);
134 (*bind_program
)(GL_FRAGMENT_PROGRAM_ARB
, 1);
136 glEnable(GL_TEXTURE_2D_ARRAY_EXT
);
139 glMatrixMode(GL_PROJECTION
);
141 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
142 glMatrixMode(GL_MODELVIEW
);
144 glTranslatef(0.0, 0.0, -8.0);
147 glRotatef(Xrot
, 1, 0, 0);
148 glRotatef(Yrot
, 0, 1, 0);
149 glRotatef(Zrot
, 0, 0, 1);
152 glTexCoord3f(0.0, 0.0, texZ
); glVertex2f(-1.0, -1.0);
153 glTexCoord3f(2.0, 0.0, texZ
); glVertex2f(1.0, -1.0);
154 glTexCoord3f(2.0, 2.0, texZ
); glVertex2f(1.0, 1.0);
155 glTexCoord3f(0.0, 2.0, texZ
); glVertex2f(-1.0, 1.0);
160 glDisable(GL_TEXTURE_2D_ARRAY_EXT
);
162 (*bind_program
)(GL_FRAGMENT_PROGRAM_ARB
, 0);
169 static void Reshape(int width
, int height
)
171 glViewport(0, 0, width
, height
);
175 static void Key(unsigned char key
, int x
, int y
)
188 static void SpecialKey(int key
, int x
, int y
)
190 const GLfloat step
= 3.0;
211 static int FindLine(const char *program
, int position
)
214 for (i
= 0; i
< position
; i
++) {
215 if (program
[i
] == '\n')
223 compile_fragment_program(GLuint id
, const char *prog
)
229 (*bind_program
)(GL_FRAGMENT_PROGRAM_ARB
, id
);
230 (*program_string
)(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
231 strlen(prog
), (const GLubyte
*) prog
);
233 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &errorPos
);
235 if (err
!= GL_NO_ERROR
|| errorPos
!= -1) {
236 int l
= FindLine(prog
, errorPos
);
238 printf("Fragment Program Error (err=%d, pos=%d line=%d): %s\n",
240 (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB
));
246 static void require_extension(const char *ext
)
248 if (!glutExtensionSupported(ext
)) {
249 printf("Sorry, %s not supported by this renderer.\n", ext
);
255 static void Init(void)
257 const char *const ver_string
= (const char *const) glGetString(GL_VERSION
);
260 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
261 printf("GL_VERSION = %s\n", ver_string
);
263 require_extension("GL_ARB_fragment_program");
264 require_extension("GL_MESA_texture_array");
265 require_extension("GL_SGIS_generate_mipmap");
267 bind_program
= glutGetProcAddress("glBindProgramARB");
268 program_string
= glutGetProcAddress("glProgramStringARB");
269 gen_programs
= glutGetProcAddress("glGenProgramsARB");
272 for (num_layers
= 0; textures
[num_layers
] != NULL
; num_layers
++)
275 glBindTexture(GL_TEXTURE_2D_ARRAY_EXT
, 1);
276 glTexImage3D(GL_TEXTURE_2D_ARRAY_EXT
, 0, GL_RGB8
,
277 256, 256, num_layers
, 0, GL_RGB
, GL_UNSIGNED_BYTE
, NULL
);
280 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT
, GL_GENERATE_MIPMAP_SGIS
,
283 for (i
= 0; textures
[i
] != NULL
; i
++) {
287 GLubyte
*image
= LoadRGBImage(textures
[i
], &width
, &height
, &format
);
289 printf("Error: could not load texture image %s\n", textures
[i
]);
293 /* resize to 256 x 256 */
294 if (width
!= 256 || height
!= 256) {
295 GLubyte
*newImage
= malloc(256 * 256 * 4);
296 gluScaleImage(format
, width
, height
, GL_UNSIGNED_BYTE
, image
,
297 256, 256, GL_UNSIGNED_BYTE
, newImage
);
302 glTexSubImage3D(GL_TEXTURE_2D_ARRAY_EXT
, 0,
303 0, 0, i
, 256, 256, 1,
304 format
, GL_UNSIGNED_BYTE
, image
);
309 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
310 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
311 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
313 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
315 glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
318 compile_fragment_program(1, frag_prog
);
323 int main(int argc
, char *argv
[])
325 glutInit(&argc
, argv
);
326 glutInitWindowPosition(0, 0);
327 glutInitWindowSize(350, 350);
328 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
);
329 glutCreateWindow("Array texture test");
331 glutReshapeFunc(Reshape
);
332 glutKeyboardFunc(Key
);
333 glutSpecialFunc(SpecialKey
);
334 glutDisplayFunc(Display
);