demos: add missing binaries to .gitignore
[mesa-demos.git] / src / tests / texcompress2.c
blob142bba1dc0aacc841705cbddaa0558fc839c801a
1 /*
2 * Test texture compression.
3 */
6 #include <assert.h>
7 #include <stdio.h>
8 #include <GL/glew.h>
9 #include "glut_wrap.h"
10 #include "readtex.c"
12 #define IMAGE_FILE DEMOS_DATA_DIR "arch.rgb"
14 static int ImgWidth, ImgHeight;
15 static GLenum ImgFormat;
16 static GLenum CompFormat;
17 static GLfloat EyeDist = 5.0;
18 static GLfloat Rot = 0.0;
19 const GLenum Target = GL_TEXTURE_2D;
22 static void
23 CheckError(int line)
25 GLenum err = glGetError();
26 if (err) {
27 printf("GL Error %d at line %d\n", (int) err, line);
32 static const char *
33 LookupFormat(GLenum format)
35 switch (format) {
36 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
37 return "GL_COMPRESSED_RGB_S3TC_DXT1_EXT";
38 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
39 return "GL_COMPRESSED_RGBA_S3TC_DXT1_EXT";
40 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
41 return "GL_COMPRESSED_RGBA_S3TC_DXT3_EXT";
42 default:
43 return "other";
48 static void
49 TestSubTex(void)
51 GLboolean all = 0*GL_TRUE;
52 GLubyte *buffer;
53 GLint size, fmt;
55 glGetTexLevelParameteriv(Target, 0,
56 GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB, &size);
57 glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_INTERNAL_FORMAT, &fmt);
59 buffer = (GLubyte *) malloc(size);
60 glGetCompressedTexImageARB(Target, 0, buffer);
62 printf("Testing sub-texture replacement\n");
63 if (all)
64 glCompressedTexImage2DARB(Target, 0,
65 fmt, ImgWidth, ImgHeight, 0,
66 size, buffer);
67 else {
68 /* bottom half */
69 glCompressedTexSubImage2DARB(Target, 0,
70 0, 0, /* pos */
71 ImgWidth, ImgHeight / 2,
72 fmt, size/2, buffer);
73 /* top half */
74 glCompressedTexSubImage2DARB(Target, 0,
75 0, ImgHeight / 2, /* pos */
76 ImgWidth, ImgHeight / 2,
77 fmt, size/2, buffer + size / 2);
80 free(buffer);
84 static void
85 TestGetTex(void)
87 GLubyte *buffer;
89 buffer = (GLubyte *) malloc(3 * ImgWidth * ImgHeight);
91 glGetTexImage(GL_TEXTURE_2D,
93 GL_RGB,
94 GL_UNSIGNED_BYTE,
95 buffer);
97 free(buffer);
101 static void
102 LoadCompressedImage(const char *file)
104 const GLenum filter = GL_LINEAR;
105 GLubyte *image;
106 GLint p;
108 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
109 glPixelStorei(GL_PACK_ALIGNMENT, 1);
112 * Load image and scale if needed.
114 image = LoadRGBImage( file, &ImgWidth, &ImgHeight, &ImgFormat );
115 if (!image) {
116 printf("Couldn't read %s\n", IMAGE_FILE);
117 exit(0);
119 printf("Image is %d x %d\n", ImgWidth, ImgHeight);
121 /* power of two */
122 assert(ImgWidth == 128 || ImgWidth == 256 || ImgWidth == 512);
123 assert(ImgWidth == 128 || ImgHeight == 256 || ImgHeight == 512);
125 if (ImgFormat == GL_RGB)
126 CompFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
127 else
128 CompFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
130 if (ImgFormat == GL_RGBA) {
131 int i, numAlpha = 0;
132 for (i = 0; i < ImgWidth * ImgHeight; i++) {
133 if (image[i*4+3] != 0 && image[i*4+3] != 0xff) {
134 numAlpha++;
136 if (image[i*4+3] == 0)
137 image[i*4+3] = 4 * i / ImgWidth;
139 printf("Num Alpha !=0,255: %d\n", numAlpha);
142 CompFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
146 * Give image to OpenGL and have it compress it.
148 glTexImage2D(Target, 0, CompFormat, ImgWidth, ImgHeight, 0,
149 ImgFormat, GL_UNSIGNED_BYTE, image);
150 CheckError(__LINE__);
152 free(image);
154 glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_INTERNAL_FORMAT, &p);
155 printf("Compressed Internal Format: %s (0x%x)\n", LookupFormat(p), p);
156 assert(p == CompFormat);
158 printf("Original size: %d bytes\n", ImgWidth * ImgHeight * 3);
159 glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB, &p);
160 printf("Compressed size: %d bytes\n", p);
162 glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, filter);
163 glTexParameteri(Target, GL_TEXTURE_MAG_FILTER, filter);
165 if (0)
166 TestSubTex();
167 else
168 TestGetTex();
173 static void
174 Init(const char *file)
176 GLint numFormats, formats[100];
177 GLint p;
179 if (!glutExtensionSupported("GL_ARB_texture_compression")) {
180 printf("Sorry, GL_ARB_texture_compression is required.\n");
181 exit(1);
183 if (!glutExtensionSupported("GL_EXT_texture_compression_s3tc")) {
184 printf("Sorry, GL_EXT_texture_compression_s3tc is required.\n");
185 exit(1);
188 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
189 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
191 glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS_ARB, &numFormats);
192 glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS_ARB, formats);
193 printf("%d supported compression formats: ", numFormats);
194 for (p = 0; p < numFormats; p++)
195 printf("0x%x ", formats[p]);
196 printf("\n");
198 LoadCompressedImage(file);
200 glEnable(GL_TEXTURE_2D);
202 if (ImgFormat == GL_RGBA) {
203 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
204 glEnable(GL_BLEND);
209 static void
210 Reshape( int width, int height )
212 glViewport( 0, 0, width, height );
213 glMatrixMode( GL_PROJECTION );
214 glLoadIdentity();
215 glFrustum(-1, 1, -1, 1, 4, 100);
216 glMatrixMode( GL_MODELVIEW );
217 glLoadIdentity();
221 static void
222 Key( unsigned char key, int x, int y )
224 (void) x;
225 (void) y;
226 switch (key) {
227 case 'd':
228 EyeDist -= 1.0;
229 if (EyeDist < 4.0)
230 EyeDist = 4.0;
231 break;
232 case 'D':
233 EyeDist += 1.0;
234 break;
235 case 'z':
236 Rot += 5.0;
237 break;
238 case 'Z':
239 Rot -= 5.0;
240 break;
241 case 27:
242 exit(0);
243 break;
245 glutPostRedisplay();
249 static void
250 Draw( void )
252 glClearColor(0.3, 0.3, .8, 0);
253 glClear(GL_COLOR_BUFFER_BIT);
255 glPushMatrix();
256 glTranslatef(0, 0, -(EyeDist+0.01));
257 glRotatef(Rot, 0, 0, 1);
258 glBegin(GL_POLYGON);
259 glTexCoord2f(0, 0); glVertex2f(-1, -1);
260 glTexCoord2f(1, 0); glVertex2f( 1, -1);
261 glTexCoord2f(1, 1); glVertex2f( 1, 1);
262 glTexCoord2f(0, 1); glVertex2f(-1, 1);
263 glEnd();
264 glPopMatrix();
266 glutSwapBuffers();
271 main( int argc, char *argv[] )
273 glutInit( &argc, argv );
274 glutInitWindowSize( 600, 600 );
276 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE);
278 glutCreateWindow(argv[0]);
279 glewInit();
281 glutReshapeFunc( Reshape );
282 glutKeyboardFunc( Key );
283 glutDisplayFunc( Draw );
285 if (argc > 1)
286 Init(argv[1]);
287 else
288 Init(IMAGE_FILE);
290 glutMainLoop();
291 return 0;