getteximage.c revision 32001f49
1/** 2 * Test glGetTexImage() 3 * Brian Paul 4 * 9 June 2009 5 */ 6 7 8#include <stdio.h> 9#include <stdlib.h> 10#include <math.h> 11#include <GL/glew.h> 12#include "glut_wrap.h" 13 14static int Win; 15 16 17static void 18TestGetTexImage(GLboolean npot) 19{ 20 GLuint iter; 21 GLubyte *data = (GLubyte *) malloc(1024 * 1024 * 4); 22 GLubyte *data2 = (GLubyte *) malloc(1024 * 1024 * 4); 23 24 glEnable(GL_TEXTURE_2D); 25 26 printf("glTexImage2D + glGetTexImage:\n"); 27 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); 32 GLuint i; 33 GLint level = 0; 34 35 printf(" Testing %d x %d tex image\n", w, h); 36 37 /* fill data */ 38 for (i = 0; i < w * h * 4; i++) { 39 data[i] = i & 0xff; 40 data2[i] = 0; 41 } 42 43 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w, h, 0, 44 GL_RGBA, GL_UNSIGNED_BYTE, data); 45 46 glBegin(GL_POINTS); 47 glVertex2f(0, 0); 48 glEnd(); 49 50 /* get */ 51 glGetTexImage(GL_TEXTURE_2D, level, GL_RGBA, GL_UNSIGNED_BYTE, data2); 52 53 /* compare */ 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]); 58 abort(); 59 } 60 } 61 62 /* get as BGRA */ 63 glGetTexImage(GL_TEXTURE_2D, level, GL_BGRA, GL_UNSIGNED_BYTE, data2); 64 65 /* compare */ 66 { 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]); 76 abort(); 77 } 78 } 79 } 80 81 } 82 83 printf("Passed\n"); 84 glDisable(GL_TEXTURE_2D); 85 free(data); 86 free(data2); 87} 88 89 90static GLboolean 91ColorsEqual(const GLubyte ref[4], const GLubyte act[4]) 92{ 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]); 99 return GL_FALSE; 100 } 101 return GL_TRUE; 102} 103 104 105static void 106TestGetTexImageRTT(GLboolean npot) 107{ 108 GLuint iter; 109 110 printf("Render to texture + glGetTexImage:\n"); 111 112 for (iter = 0; iter < 8; iter++) { 113 114 GLuint fb, tex; 115 GLint w, h; 116 GLint level = 0; 117 118 if (npot) { 119 w = 200 + iter * 40; 120 h = 200 + iter * 12; 121 } 122 else { 123 w = 4 << iter; 124 h = 4 << iter; 125 } 126 127 glGenTextures(1, &tex); 128 glGenFramebuffersEXT(1, &fb); 129 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); 135 136 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb); 137 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, 138 GL_TEXTURE_2D, tex, level); 139 140 glViewport(0, 0, w, h); 141 142 printf(" Testing %d x %d tex image\n", w, h); 143 { 144 static const GLubyte blue[4] = {0, 0, 255, 255}; 145 GLubyte color[4]; 146 GLubyte *data2 = (GLubyte *) malloc(w * h * 4); 147 GLuint i; 148 149 /* random clear color */ 150 for (i = 0; i < 4; i++) { 151 color[i] = rand() % 256; 152 } 153 154 glClearColor(color[0] / 255.0, 155 color[1] / 255.0, 156 color[2] / 255.0, 157 color[3] / 255.0); 158 159 glClear(GL_COLOR_BUFFER_BIT); 160 161 /* draw polygon over top half, in blue */ 162 glColor4ubv(blue); 163 glRectf(0, 0.5, 1.0, 1.0); 164 165 /* get */ 166 glGetTexImage(GL_TEXTURE_2D, level, GL_RGBA, GL_UNSIGNED_BYTE, data2); 167 168 /* compare */ 169 for (i = 0; i < w * h; i += 4) { 170 if (i < w * h / 2) { 171 /* lower half */ 172 if (!ColorsEqual(color, data2 + i * 4)) { 173 printf("Render to texture failure (expected clear color)!\n"); 174 abort(); 175 } 176 } 177 else { 178 /* upper half */ 179 if (!ColorsEqual(blue, data2 + i * 4)) { 180 printf("Render to texture failure (expected blue)!\n"); 181 abort(); 182 } 183 } 184 } 185 186 free(data2); 187 } 188 189 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 190 glDeleteFramebuffersEXT(1, &fb); 191 glDeleteTextures(1, &tex); 192 193 } 194 195 printf("Passed\n"); 196} 197 198 199 200 201static void 202Draw(void) 203{ 204 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 205 206 TestGetTexImage(GL_FALSE); 207 if (glutExtensionSupported("GL_ARB_texture_non_power_of_two")) 208 TestGetTexImage(GL_TRUE); 209 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); 215 } 216 217 glutDestroyWindow(Win); 218 exit(0); 219 220 glutSwapBuffers(); 221} 222 223 224static void 225Reshape(int width, int height) 226{ 227 glViewport(0, 0, width, height); 228 glMatrixMode(GL_PROJECTION); 229 glLoadIdentity(); 230 glOrtho(0, 1, 0, 1, -1, 1); 231 glMatrixMode(GL_MODELVIEW); 232 glLoadIdentity(); 233 glTranslatef(0.0, 0.0, 0.0); 234} 235 236 237static void 238Key(unsigned char key, int x, int y) 239{ 240 (void) x; 241 (void) y; 242 switch (key) { 243 case 27: 244 glutDestroyWindow(Win); 245 exit(0); 246 break; 247 } 248 glutPostRedisplay(); 249} 250 251 252static void 253Init(void) 254{ 255} 256 257 258int 259main(int argc, char *argv[]) 260{ 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]); 266 glewInit(); 267 glutReshapeFunc(Reshape); 268 glutKeyboardFunc(Key); 269 glutDisplayFunc(Draw); 270 Init(); 271 glutMainLoop(); 272 return 0; 273} 274