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;
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(void)
94 glGenTextures(1, &tex
);
95 glGenFramebuffersEXT(1, &fb
);
97 glBindTexture(GL_TEXTURE_2D
, tex
);
98 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
99 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
100 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, w
, h
, 0,
101 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
103 glBindFramebuffer(GL_FRAMEBUFFER_EXT
, fb
);
104 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
, GL_COLOR_ATTACHMENT0_EXT
,
105 GL_TEXTURE_2D
, tex
, level
);
107 printf("Render to texture + glGetTexImage:\n");
108 printf(" Testing %d x %d tex image\n", w
, h
);
109 for (iter
= 0; iter
< 8; iter
++) {
111 GLubyte
*data2
= (GLubyte
*) malloc(w
* h
* 4);
114 /* random clear color */
115 for (i
= 0; i
< 4; i
++) {
116 color
[i
] = rand() % 256;
119 glClearColor(color
[0] / 255.0,
124 glClear(GL_COLOR_BUFFER_BIT
);
127 glGetTexImage(GL_TEXTURE_2D
, level
, GL_RGBA
, GL_UNSIGNED_BYTE
, data2
);
130 for (i
= 0; i
< w
* h
; i
+= 4) {
131 if (!ColorsEqual(color
, data2
+ i
* 4)) {
132 printf("Render to texture failure!\n");
140 glBindFramebuffer(GL_FRAMEBUFFER_EXT
, 0);
141 glDeleteFramebuffersEXT(1, &fb
);
142 glDeleteTextures(1, &tex
);
153 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
157 if (glutExtensionSupported("GL_EXT_framebuffer_object") ||
158 glutExtensionSupported("GL_ARB_framebuffer_object"))
159 TestGetTexImageRTT();
161 glutDestroyWindow(Win
);
169 Reshape(int width
, int height
)
171 glViewport(0, 0, width
, height
);
172 glMatrixMode(GL_PROJECTION
);
174 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
175 glMatrixMode(GL_MODELVIEW
);
177 glTranslatef(0.0, 0.0, -15.0);
182 Key(unsigned char key
, int x
, int y
)
188 glutDestroyWindow(Win
);
203 main(int argc
, char *argv
[])
205 glutInit(&argc
, argv
);
206 glutInitWindowPosition(0, 0);
207 glutInitWindowSize(400, 400);
208 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
209 Win
= glutCreateWindow(argv
[0]);
211 glutReshapeFunc(Reshape
);
212 glutKeyboardFunc(Key
);
213 glutDisplayFunc(Draw
);