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
]);
64 glDisable(GL_TEXTURE_2D
);
71 ColorsEqual(const GLubyte ref
[4], const GLubyte act
[4])
73 if (abs((int) ref
[0] - (int) act
[0]) > 1 ||
74 abs((int) ref
[1] - (int) act
[1]) > 1 ||
75 abs((int) ref
[2] - (int) act
[2]) > 1 ||
76 abs((int) ref
[3] - (int) act
[3]) > 1) {
77 printf("expected %d %d %d %d\n", ref
[0], ref
[1], ref
[2], ref
[3]);
78 printf("found %d %d %d %d\n", act
[0], act
[1], act
[2], act
[3]);
86 TestGetTexImageRTT(GLboolean npot
)
90 printf("Render to texture + glGetTexImage:\n");
92 for (iter
= 0; iter
< 8; iter
++) {
107 glGenTextures(1, &tex
);
108 glGenFramebuffersEXT(1, &fb
);
110 glBindTexture(GL_TEXTURE_2D
, tex
);
111 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
112 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
113 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, w
, h
, 0,
114 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
116 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fb
);
117 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
, GL_COLOR_ATTACHMENT0_EXT
,
118 GL_TEXTURE_2D
, tex
, level
);
120 glViewport(0, 0, w
, h
);
122 printf(" Testing %d x %d tex image\n", w
, h
);
124 static const GLubyte blue
[4] = {0, 0, 255, 255};
126 GLubyte
*data2
= (GLubyte
*) malloc(w
* h
* 4);
129 /* random clear color */
130 for (i
= 0; i
< 4; i
++) {
131 color
[i
] = rand() % 256;
134 glClearColor(color
[0] / 255.0,
139 glClear(GL_COLOR_BUFFER_BIT
);
141 /* draw polygon over top half, in blue */
143 glRectf(0, 0.5, 1.0, 1.0);
146 glGetTexImage(GL_TEXTURE_2D
, level
, GL_RGBA
, GL_UNSIGNED_BYTE
, data2
);
149 for (i
= 0; i
< w
* h
; i
+= 4) {
152 if (!ColorsEqual(color
, data2
+ i
* 4)) {
153 printf("Render to texture failure (expected clear color)!\n");
159 if (!ColorsEqual(blue
, data2
+ i
* 4)) {
160 printf("Render to texture failure (expected blue)!\n");
169 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
170 glDeleteFramebuffersEXT(1, &fb
);
171 glDeleteTextures(1, &tex
);
184 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
186 TestGetTexImage(GL_FALSE
);
187 if (glutExtensionSupported("GL_ARB_texture_non_power_of_two"))
188 TestGetTexImage(GL_TRUE
);
190 if (glutExtensionSupported("GL_EXT_framebuffer_object") ||
191 glutExtensionSupported("GL_ARB_framebuffer_object")) {
192 TestGetTexImageRTT(GL_FALSE
);
193 if (glutExtensionSupported("GL_ARB_texture_non_power_of_two"))
194 TestGetTexImageRTT(GL_TRUE
);
197 glutDestroyWindow(Win
);
205 Reshape(int width
, int height
)
207 glViewport(0, 0, width
, height
);
208 glMatrixMode(GL_PROJECTION
);
210 glOrtho(0, 1, 0, 1, -1, 1);
211 glMatrixMode(GL_MODELVIEW
);
213 glTranslatef(0.0, 0.0, 0.0);
218 Key(unsigned char key
, int x
, int y
)
224 glutDestroyWindow(Win
);
239 main(int argc
, char *argv
[])
241 glutInit(&argc
, argv
);
242 glutInitWindowPosition(0, 0);
243 glutInitWindowSize(400, 400);
244 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
245 Win
= glutCreateWindow(argv
[0]);
247 glutReshapeFunc(Reshape
);
248 glutKeyboardFunc(Key
);
249 glutDisplayFunc(Draw
);