18 TestGetTexImage(GLboolean npot
)
21 GLubyte
*data
= (GLubyte
*) malloc(1024 * 1024 * 4);
22 GLubyte
*data2
= (GLubyte
*) malloc(1024 * 1024 * 4);
24 glEnable(GL_TEXTURE_2D
);
26 printf("glTexImage2D + glGetTexImage:\n");
28 for (iter
= 0; iter
< 8; iter
++) {
29 GLint p
= (iter
% 8) + 3;
30 GLint w
= npot
? (p
* 20) : (1 << p
);
31 GLint h
= npot
? (p
* 10) : (1 << p
);
35 printf(" Testing %d x %d tex image\n", w
, h
);
38 for (i
= 0; i
< w
* h
* 4; i
++) {
43 glTexImage2D(GL_TEXTURE_2D
, level
, GL_RGBA
, w
, h
, 0,
44 GL_RGBA
, GL_UNSIGNED_BYTE
, data
);
51 glGetTexImage(GL_TEXTURE_2D
, level
, GL_RGBA
, GL_UNSIGNED_BYTE
, data2
);
54 for (i
= 0; i
< w
* h
* 4; i
++) {
55 if (data2
[i
] != data
[i
]) {
56 printf("glTexImage + glGetTexImage failure!\n");
57 printf("Expected value %d, found %d\n", data
[i
], data2
[i
]);
63 glGetTexImage(GL_TEXTURE_2D
, level
, GL_BGRA
, GL_UNSIGNED_BYTE
, data2
);
67 const GLubyte
*rgba
= (GLubyte
*) data
;
68 const GLubyte
*bgra
= (GLubyte
*) data2
;
69 for (i
= 0; i
< w
* h
; i
+= 4) {
70 if (rgba
[i
+0] != bgra
[i
+2] ||
71 rgba
[i
+1] != bgra
[i
+1] ||
72 rgba
[i
+2] != bgra
[i
+0] ||
73 rgba
[i
+3] != bgra
[i
+3]) {
74 printf("glTexImage + glGetTexImage(GL_BGRA) failure!\n");
75 printf("Expected value %d, found %d\n", data
[i
], data2
[i
]);
84 glDisable(GL_TEXTURE_2D
);
91 ColorsEqual(const GLubyte ref
[4], const GLubyte act
[4])
93 if (abs((int) ref
[0] - (int) act
[0]) > 1 ||
94 abs((int) ref
[1] - (int) act
[1]) > 1 ||
95 abs((int) ref
[2] - (int) act
[2]) > 1 ||
96 abs((int) ref
[3] - (int) act
[3]) > 1) {
97 printf("expected %d %d %d %d\n", ref
[0], ref
[1], ref
[2], ref
[3]);
98 printf("found %d %d %d %d\n", act
[0], act
[1], act
[2], act
[3]);
106 TestGetTexImageRTT(GLboolean npot
)
110 printf("Render to texture + glGetTexImage:\n");
112 for (iter
= 0; iter
< 8; iter
++) {
127 glGenTextures(1, &tex
);
128 glGenFramebuffersEXT(1, &fb
);
130 glBindTexture(GL_TEXTURE_2D
, tex
);
131 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
132 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
133 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, w
, h
, 0,
134 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
136 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fb
);
137 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
, GL_COLOR_ATTACHMENT0_EXT
,
138 GL_TEXTURE_2D
, tex
, level
);
140 glViewport(0, 0, w
, h
);
142 printf(" Testing %d x %d tex image\n", w
, h
);
144 static const GLubyte blue
[4] = {0, 0, 255, 255};
146 GLubyte
*data2
= (GLubyte
*) malloc(w
* h
* 4);
149 /* random clear color */
150 for (i
= 0; i
< 4; i
++) {
151 color
[i
] = rand() % 256;
154 glClearColor(color
[0] / 255.0,
159 glClear(GL_COLOR_BUFFER_BIT
);
161 /* draw polygon over top half, in blue */
163 glRectf(0, 0.5, 1.0, 1.0);
166 glGetTexImage(GL_TEXTURE_2D
, level
, GL_RGBA
, GL_UNSIGNED_BYTE
, data2
);
169 for (i
= 0; i
< w
* h
; i
+= 4) {
172 if (!ColorsEqual(color
, data2
+ i
* 4)) {
173 printf("Render to texture failure (expected clear color)!\n");
179 if (!ColorsEqual(blue
, data2
+ i
* 4)) {
180 printf("Render to texture failure (expected blue)!\n");
189 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
190 glDeleteFramebuffersEXT(1, &fb
);
191 glDeleteTextures(1, &tex
);
204 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
206 TestGetTexImage(GL_FALSE
);
207 if (glutExtensionSupported("GL_ARB_texture_non_power_of_two"))
208 TestGetTexImage(GL_TRUE
);
210 if (glutExtensionSupported("GL_EXT_framebuffer_object") ||
211 glutExtensionSupported("GL_ARB_framebuffer_object")) {
212 TestGetTexImageRTT(GL_FALSE
);
213 if (glutExtensionSupported("GL_ARB_texture_non_power_of_two"))
214 TestGetTexImageRTT(GL_TRUE
);
217 glutDestroyWindow(Win
);
225 Reshape(int width
, int height
)
227 glViewport(0, 0, width
, height
);
228 glMatrixMode(GL_PROJECTION
);
230 glOrtho(0, 1, 0, 1, -1, 1);
231 glMatrixMode(GL_MODELVIEW
);
233 glTranslatef(0.0, 0.0, 0.0);
238 Key(unsigned char key
, int x
, int y
)
244 glutDestroyWindow(Win
);
259 main(int argc
, char *argv
[])
261 glutInit(&argc
, argv
);
262 glutInitWindowPosition(0, 0);
263 glutInitWindowSize(400, 400);
264 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
265 Win
= glutCreateWindow(argv
[0]);
267 glutReshapeFunc(Reshape
);
268 glutKeyboardFunc(Key
);
269 glutDisplayFunc(Draw
);