Brian Paul | 46bc595 | 2004-05-12 23:05:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Test packed pixel formats for textures. |
| 3 | * Brian Paul |
| 4 | * 12 May 2004 |
| 5 | */ |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <math.h> |
| 10 | #include <string.h> |
| 11 | #include <GL/glut.h> |
| 12 | |
| 13 | |
| 14 | struct pixel_format { |
| 15 | const char *name; |
| 16 | GLenum format; |
| 17 | GLenum type; |
| 18 | GLint bytes; |
| 19 | GLuint redTexel, greenTexel; |
| 20 | }; |
| 21 | |
| 22 | static const struct pixel_format Formats[] = { |
| 23 | |
| 24 | { "GL_RGBA/GL_UNSIGNED_INT_8_8_8_8", |
| 25 | GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, 4, 0xff000000, 0x00ff0000 }, |
| 26 | { "GL_RGBA/GL_UNSIGNED_INT_8_8_8_8_REV", |
| 27 | GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, 4, 0x000000ff, 0x0000ff00 }, |
| 28 | { "GL_RGBA/GL_UNSIGNED_INT_10_10_10_2", |
| 29 | GL_RGBA, GL_UNSIGNED_INT_10_10_10_2, 4, 0xffc00000, 0x3ff000 }, |
| 30 | { "GL_RGBA/GL_UNSIGNED_INT_2_10_10_10_REV", |
| 31 | GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, 4, 0x3ff, 0xffc00 }, |
| 32 | { "GL_RGBA/GL_UNSIGNED_SHORT_4_4_4_4", |
| 33 | GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 2, 0xf000, 0x0f00 }, |
| 34 | { "GL_RGBA/GL_UNSIGNED_SHORT_4_4_4_4_REV", |
| 35 | GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4_REV, 2, 0x000f, 0x00f0 }, |
| 36 | { "GL_RGBA/GL_UNSIGNED_SHORT_5_5_5_1", |
| 37 | GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, 2, 0xf800, 0x7c0 }, |
| 38 | { "GL_RGBA/GL_UNSIGNED_SHORT_1_5_5_5_REV", |
| 39 | GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 2, 0x1f, 0x3e0 }, |
| 40 | |
| 41 | { "GL_BGRA/GL_UNSIGNED_INT_8_8_8_8", |
| 42 | GL_BGRA, GL_UNSIGNED_INT_8_8_8_8, 4, 0x0000ff00, 0x00ff0000 }, |
| 43 | { "GL_BGRA/GL_UNSIGNED_INT_8_8_8_8_REV", |
| 44 | GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 4, 0x00ff0000, 0x0000ff00 }, |
| 45 | { "GL_BGRA/GL_UNSIGNED_SHORT_4_4_4_4", |
| 46 | GL_BGRA, GL_UNSIGNED_SHORT_4_4_4_4, 2, 0x00f0, 0x0f00 }, |
| 47 | { "GL_BGRA/GL_UNSIGNED_SHORT_4_4_4_4_REV", |
| 48 | GL_BGRA, GL_UNSIGNED_SHORT_4_4_4_4_REV, 2, 0x0f00, 0x00f0 }, |
| 49 | { "GL_BGRA/GL_UNSIGNED_SHORT_5_5_5_1", |
| 50 | GL_BGRA, GL_UNSIGNED_SHORT_5_5_5_1, 2, 0x3e, 0x7c0 }, |
| 51 | { "GL_BGRA/GL_UNSIGNED_SHORT_1_5_5_5_REV", |
| 52 | GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 2, 0x7c00, 0x3e0 }, |
| 53 | |
| 54 | { "GL_ABGR_EXT/GL_UNSIGNED_INT_8_8_8_8", |
| 55 | GL_ABGR_EXT, GL_UNSIGNED_INT_8_8_8_8, 4, 0x000000ff, 0x0000ff00 }, |
| 56 | { "GL_ABGR_EXT/GL_UNSIGNED_INT_8_8_8_8_REV", |
| 57 | GL_ABGR_EXT, GL_UNSIGNED_INT_8_8_8_8_REV, 4, 0xff000000, 0x00ff0000 }, |
| 58 | { "GL_ABGR_EXT/GL_UNSIGNED_SHORT_4_4_4_4", |
| 59 | GL_ABGR_EXT, GL_UNSIGNED_SHORT_4_4_4_4, 2, 0x000f, 0x00f0 }, |
| 60 | { "GL_ABGR_EXT/GL_UNSIGNED_SHORT_4_4_4_4_REV", |
| 61 | GL_ABGR_EXT, GL_UNSIGNED_SHORT_4_4_4_4_REV, 2, 0xf000, 0x0f00 }, |
| 62 | { "GL_ABGR_EXT/GL_UNSIGNED_SHORT_5_5_5_1", |
| 63 | GL_ABGR_EXT, GL_UNSIGNED_SHORT_5_5_5_1, 2, 0x1, 0x3e }, |
| 64 | { "GL_ABGR_EXT/GL_UNSIGNED_SHORT_1_5_5_5_REV", |
| 65 | GL_ABGR_EXT, GL_UNSIGNED_SHORT_1_5_5_5_REV, 2, 0x8000, 0x7c00 }, |
| 66 | |
| 67 | { "GL_RGB/GL_UNSIGNED_SHORT_5_6_5", |
| 68 | GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 2, 0xf800, 0x7e0 }, |
| 69 | { "GL_RGB/GL_UNSIGNED_SHORT_5_6_5_REV", |
| 70 | GL_RGB, GL_UNSIGNED_SHORT_5_6_5_REV, 2, 0x1f, 0x7e0 }, |
| 71 | { "GL_RGB/GL_UNSIGNED_BYTE_3_3_2", |
| 72 | GL_RGB, GL_UNSIGNED_BYTE_3_3_2, 1, 0xe0, 0x1c }, |
| 73 | { "GL_RGB/GL_UNSIGNED_BYTE_2_3_3_REV", |
| 74 | GL_RGB, GL_UNSIGNED_BYTE_2_3_3_REV, 1, 0x7, 0x38 }, |
| 75 | |
| 76 | { NULL, 0, 0, 0, 0, 0 } |
| 77 | }; |
| 78 | |
| 79 | |
| 80 | struct name_format { |
| 81 | const char *name; |
| 82 | GLenum format; |
| 83 | }; |
| 84 | |
| 85 | static const struct name_format IntFormats[] = { |
| 86 | { "GL_RGBA", GL_RGBA }, |
| 87 | { "GL_RGBA2", GL_RGBA2 }, |
| 88 | { "GL_RGBA4", GL_RGBA4 }, |
| 89 | { "GL_RGB5_A1", GL_RGB5_A1 }, |
| 90 | { "GL_RGBA8", GL_RGBA8 }, |
| 91 | { "GL_RGBA12", GL_RGBA12 }, |
| 92 | { "GL_RGBA16", GL_RGBA16 }, |
| 93 | { "GL_RGB10_A2", GL_RGB10_A2 }, |
| 94 | |
| 95 | { "GL_RGB", GL_RGB }, |
| 96 | { "GL_R3_G3_B2", GL_R3_G3_B2 }, |
| 97 | { "GL_RGB4", GL_RGB4 }, |
| 98 | { "GL_RGB5", GL_RGB5 }, |
| 99 | { "GL_RGB8", GL_RGB8 }, |
| 100 | { "GL_RGB10", GL_RGB10 }, |
| 101 | { "GL_RGB12", GL_RGB12 }, |
| 102 | { "GL_RGB16", GL_RGB16 }, |
| 103 | |
| 104 | }; |
| 105 | |
| 106 | #define NUM_INT_FORMATS (sizeof(IntFormats) / sizeof(IntFormats[0])) |
| 107 | static GLuint CurFormat = 0; |
| 108 | |
Brian Paul | 5ec34f0 | 2006-05-19 16:42:01 +0000 | [diff] [blame^] | 109 | static GLboolean Test3D = GL_FALSE; |
| 110 | |
| 111 | |
Brian Paul | 46bc595 | 2004-05-12 23:05:21 +0000 | [diff] [blame] | 112 | |
| 113 | static void |
| 114 | PrintString(const char *s) |
| 115 | { |
| 116 | while (*s) { |
| 117 | glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s); |
| 118 | s++; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | |
| 123 | static void |
| 124 | MakeTexture(const struct pixel_format *format, GLenum intFormat, GLboolean swap) |
| 125 | { |
| 126 | GLubyte texBuffer[1000]; |
| 127 | int i; |
| 128 | |
| 129 | glPixelStorei(GL_UNPACK_SWAP_BYTES, swap); |
| 130 | |
| 131 | if (format->bytes == 1) { |
| 132 | for (i = 0; i < 8; i++) { |
| 133 | texBuffer[i] = format->redTexel; |
| 134 | } |
| 135 | for (i = 8; i < 16; i++) { |
| 136 | texBuffer[i] = format->greenTexel; |
| 137 | } |
| 138 | } |
| 139 | else if (format->bytes == 2) { |
| 140 | GLushort *us = (GLushort *) texBuffer; |
| 141 | for (i = 0; i < 8; i++) { |
| 142 | us[i] = format->redTexel; |
| 143 | } |
| 144 | for (i = 8; i < 16; i++) { |
| 145 | us[i] = format->greenTexel; |
| 146 | } |
| 147 | if (swap) { |
| 148 | for (i = 0; i < 16; i++) |
| 149 | us[i] = (us[i] << 8) | (us[i] >> 8); |
| 150 | } |
| 151 | } |
| 152 | else if (format->bytes == 4) { |
| 153 | GLuint *ui = (GLuint *) texBuffer; |
| 154 | for (i = 0; i < 8; i++) { |
| 155 | ui[i] = format->redTexel; |
| 156 | } |
| 157 | for (i = 8; i < 16; i++) { |
| 158 | ui[i] = format->greenTexel; |
| 159 | } |
| 160 | if (swap) { |
| 161 | for (i = 0; i < 16; i++) { |
| 162 | GLuint b = ui[i]; |
| 163 | ui[i] = (b >> 24) |
| 164 | | ((b >> 8) & 0xff00) |
| 165 | | ((b << 8) & 0xff0000) |
| 166 | | ((b << 24) & 0xff000000); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | else { |
| 171 | abort(); |
| 172 | } |
Brian Paul | 5ec34f0 | 2006-05-19 16:42:01 +0000 | [diff] [blame^] | 173 | |
| 174 | if (Test3D) { |
| 175 | /* 4 x 4 x 4 texture, undefined data */ |
| 176 | glTexImage3D(GL_TEXTURE_3D, 0, intFormat, 4, 4, 4, 0, |
| 177 | format->format, format->type, NULL); |
| 178 | /* fill in Z=1 and Z=2 slices with the real texture data */ |
| 179 | glTexSubImage3D(GL_TEXTURE_3D, 0, |
| 180 | 0, 0, 1, /* offset */ |
| 181 | 4, 4, 1, /* size */ |
| 182 | format->format, format->type, texBuffer); |
| 183 | glTexSubImage3D(GL_TEXTURE_3D, 0, |
| 184 | 0, 0, 2, /* offset */ |
| 185 | 4, 4, 1, /* size */ |
| 186 | format->format, format->type, texBuffer); |
| 187 | } |
| 188 | else { |
| 189 | glTexImage2D(GL_TEXTURE_2D, 0, intFormat, 4, 4, 0, |
| 190 | format->format, format->type, texBuffer); |
| 191 | } |
Brian Paul | 46bc595 | 2004-05-12 23:05:21 +0000 | [diff] [blame] | 192 | |
| 193 | if (glGetError()) { |
| 194 | printf("GL Error for %s\n", format->name); |
| 195 | memset(texBuffer, 255, 1000); |
| 196 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0, |
| 197 | GL_RGB, GL_UNSIGNED_BYTE, texBuffer); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | |
| 202 | |
| 203 | static void |
| 204 | Draw(void) |
| 205 | { |
| 206 | char s[1000]; |
| 207 | int w = 350, h = 20; |
| 208 | int i, swap; |
| 209 | |
| 210 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 211 | |
| 212 | for (swap = 0; swap < 2; swap++) { |
| 213 | for (i = 0; Formats[i].name; i++) { |
| 214 | glPushMatrix(); |
| 215 | glTranslatef(swap * (w + 2), i * (h + 2), 0); |
| 216 | |
| 217 | MakeTexture(Formats + i, IntFormats[CurFormat].format, swap); |
| 218 | |
Brian Paul | 5ec34f0 | 2006-05-19 16:42:01 +0000 | [diff] [blame^] | 219 | if (Test3D) |
| 220 | glEnable(GL_TEXTURE_3D); |
| 221 | else |
| 222 | glEnable(GL_TEXTURE_2D); |
Brian Paul | 46bc595 | 2004-05-12 23:05:21 +0000 | [diff] [blame] | 223 | glBegin(GL_POLYGON); |
Brian Paul | 5ec34f0 | 2006-05-19 16:42:01 +0000 | [diff] [blame^] | 224 | glTexCoord3f(0, 0, 0.5); glVertex2f(0, 0); |
| 225 | glTexCoord3f(1, 0, 0.5); glVertex2f(w, 0); |
| 226 | glTexCoord3f(1, 1, 0.5); glVertex2f(w, h); |
| 227 | glTexCoord3f(0, 1, 0.5); glVertex2f(0, h); |
Brian Paul | 46bc595 | 2004-05-12 23:05:21 +0000 | [diff] [blame] | 228 | glEnd(); |
| 229 | |
Brian Paul | 5ec34f0 | 2006-05-19 16:42:01 +0000 | [diff] [blame^] | 230 | if (Test3D) |
| 231 | glDisable(GL_TEXTURE_3D); |
| 232 | else |
| 233 | glDisable(GL_TEXTURE_2D); |
Brian Paul | 46bc595 | 2004-05-12 23:05:21 +0000 | [diff] [blame] | 234 | glColor3f(0, 0, 0); |
| 235 | glRasterPos2i(8, 6); |
| 236 | PrintString(Formats[i].name); |
| 237 | |
| 238 | glPopMatrix(); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | glPushMatrix(); |
| 243 | glTranslatef(2, i * (h + 2), 0); |
| 244 | glColor3f(1, 1, 1); |
| 245 | glRasterPos2i(8, 6); |
| 246 | PrintString("Normal"); |
| 247 | glRasterPos2i(w + 2, 6); |
| 248 | PrintString("Byte Swapped"); |
| 249 | glPopMatrix(); |
| 250 | |
| 251 | glPushMatrix(); |
| 252 | glTranslatef(2, (i + 1) * (h + 2), 0); |
| 253 | glRasterPos2i(8, 6); |
| 254 | sprintf(s, "Internal Texture Format [f/F]: %s (%d of %d)", |
| 255 | IntFormats[CurFormat].name, CurFormat + 1, NUM_INT_FORMATS); |
| 256 | PrintString(s); |
| 257 | glPopMatrix(); |
| 258 | |
| 259 | glutSwapBuffers(); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | static void |
| 264 | Reshape(int width, int height) |
| 265 | { |
| 266 | glViewport(0, 0, width, height); |
| 267 | glMatrixMode(GL_PROJECTION); |
| 268 | glLoadIdentity(); |
| 269 | glOrtho(0, width, 0, height, -1, 1); |
| 270 | glMatrixMode(GL_MODELVIEW); |
| 271 | glLoadIdentity(); |
| 272 | } |
| 273 | |
| 274 | |
| 275 | static void |
| 276 | Key(unsigned char key, int x, int y) |
| 277 | { |
| 278 | (void) x; |
| 279 | (void) y; |
| 280 | switch (key) { |
| 281 | case 'F': |
| 282 | if (CurFormat == 0) |
| 283 | CurFormat = NUM_INT_FORMATS - 1; |
| 284 | else |
| 285 | CurFormat--; |
| 286 | break; |
| 287 | case 'f': |
| 288 | CurFormat++; |
| 289 | if (CurFormat == NUM_INT_FORMATS) |
| 290 | CurFormat = 0; |
| 291 | break; |
Brian Paul | 5ec34f0 | 2006-05-19 16:42:01 +0000 | [diff] [blame^] | 292 | case '2': |
| 293 | Test3D = GL_FALSE; |
| 294 | printf("Using 2D textures\n"); |
| 295 | break; |
| 296 | case '3': |
| 297 | Test3D = GL_TRUE; |
| 298 | printf("Using 3D textures\n"); |
| 299 | break; |
Brian Paul | 46bc595 | 2004-05-12 23:05:21 +0000 | [diff] [blame] | 300 | case 27: |
| 301 | exit(0); |
| 302 | break; |
| 303 | } |
| 304 | glutPostRedisplay(); |
| 305 | } |
| 306 | |
| 307 | |
| 308 | static void |
| 309 | Init(void) |
| 310 | { |
| 311 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 312 | printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); |
| 313 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 314 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
Brian Paul | 5ec34f0 | 2006-05-19 16:42:01 +0000 | [diff] [blame^] | 315 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 316 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
Brian Paul | 46bc595 | 2004-05-12 23:05:21 +0000 | [diff] [blame] | 317 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 318 | } |
| 319 | |
| 320 | |
| 321 | int |
| 322 | main(int argc, char *argv[]) |
| 323 | { |
| 324 | glutInit(&argc, argv); |
| 325 | glutInitWindowPosition(0, 0); |
| 326 | glutInitWindowSize(700, 800); |
| 327 | glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); |
| 328 | glutCreateWindow(argv[0]); |
| 329 | glutReshapeFunc(Reshape); |
| 330 | glutKeyboardFunc(Key); |
| 331 | glutDisplayFunc(Draw); |
| 332 | Init(); |
| 333 | glutMainLoop(); |
| 334 | return 0; |
| 335 | } |