summaryrefslogtreecommitdiff
path: root/progs/tests/texcmp.c
blob: fe3f58cf347dc441509cef74ee7c6d8c9005a9f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 * Compressed texture demo.  Written by Daniel Borca.
 * This program is in the public domain.
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define GL_GLEXT_PROTOTYPES 1
#include <GL/glut.h>

#include "readtex.c" /* I know, this is a hack. */
#define TEXTURE_FILE "../images/tree2.rgba"


static float Rot = 0.0;
static GLboolean Anim = 1;

typedef struct {
        GLubyte *data;
        GLuint size;
        GLenum format;
        GLuint w, h;

        GLenum TC;

        GLubyte *cData;
        GLuint cSize;
        GLenum cFormat;
} TEXTURE;

static TEXTURE *Tx, t1, t2, t3;
static GLboolean fxt1, dxtc, s3tc;


static const char *TextureName (GLenum TC)
{
 switch (TC) {
        case GL_RGB:
             return "RGB";
        case GL_RGBA:
             return "RGBA";
        case GL_COMPRESSED_RGB:
             return "COMPRESSED_RGB";
        case GL_COMPRESSED_RGBA:
             return "COMPRESSED_RGBA";
        case GL_COMPRESSED_RGB_FXT1_3DFX:
             return "GL_COMPRESSED_RGB_FXT1_3DFX";
        case GL_COMPRESSED_RGBA_FXT1_3DFX:
             return "GL_COMPRESSED_RGBA_FXT1_3DFX";
        case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
             return "GL_COMPRESSED_RGB_S3TC_DXT1_EXT";
        case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
             return "GL_COMPRESSED_RGBA_S3TC_DXT1_EXT";
        case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
             return "GL_COMPRESSED_RGBA_S3TC_DXT3_EXT";
        case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
             return "GL_COMPRESSED_RGBA_S3TC_DXT5_EXT";
        case GL_RGB_S3TC:
             return "GL_RGB_S3TC";
        case GL_RGB4_S3TC:
             return "GL_RGB4_S3TC";
        case GL_RGBA_S3TC:
             return "GL_RGBA_S3TC";
        case GL_RGBA4_S3TC:
             return "GL_RGBA4_S3TC";
        default:
             return "?";
 }
}


static void
PrintString(const char *s)
{
   while (*s) {
      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
      s++;
   }
}


static void Idle( void )
{
   float t = glutGet(GLUT_ELAPSED_TIME) * 0.001;  /* in seconds */
   Rot = t * 360 / 4;  /* 1 rotation per 4 seconds */
   glutPostRedisplay();
}


static void Display( void )
{
   /* draw background gradient */
   glDisable(GL_TEXTURE_2D);
   glBegin(GL_POLYGON);
   glColor3f(1.0, 0.0, 0.2); glVertex2f(-1.5, -1.0);
   glColor3f(1.0, 0.0, 0.2); glVertex2f( 1.5, -1.0);
   glColor3f(0.0, 0.0, 1.0); glVertex2f( 1.5,  1.0);
   glColor3f(0.0, 0.0, 1.0); glVertex2f(-1.5,  1.0);
   glEnd();

   glPushMatrix();
   glRotatef(Rot, 0, 0, 1);

   glEnable(GL_TEXTURE_2D);
   glBegin(GL_POLYGON);
   glTexCoord2f(0, 1);  glVertex2f(-1, -0.5);
   glTexCoord2f(1, 1);  glVertex2f( 1, -0.5);
   glTexCoord2f(1, 0);  glVertex2f( 1,  0.5);
   glTexCoord2f(0, 0);  glVertex2f(-1,  0.5);
   glEnd();

   glPopMatrix();

   /* info */
   glColor4f(1, 1, 1, 1);

   glRasterPos3f(-1.2, -0.7, 0);
   PrintString("Selected: ");
   PrintString(TextureName(Tx->TC));
   if (Tx->cData) {
      char tmp[64];
      glRasterPos3f(-1.2, -0.8, 0);
      PrintString("Internal: ");
      PrintString(TextureName(Tx->cFormat));
      glRasterPos3f(-1.2, -0.9, 0);
      PrintString("Size    : ");
      sprintf(tmp, "%d (%d%% of %d)", Tx->cSize, Tx->cSize * 100 / Tx->size, Tx->size);
      PrintString(tmp);
   }

   glutSwapBuffers();
}


static void Reshape( int width, int height )
{
   glViewport( 0, 0, width, height );
   glMatrixMode( GL_PROJECTION );
   glLoadIdentity();
   glOrtho( -1.5, 1.5, -1.0, 1.0, -1.0, 1.0 );
   glMatrixMode( GL_MODELVIEW );
   glLoadIdentity();
}


static void ReInit( GLenum TC, TEXTURE *Tx )
{
   GLint rv;

   if ((Tx->TC == TC) && (Tx->cData != NULL)) {
      glCompressedTexImage2DARB(GL_TEXTURE_2D, /* target */
	                        0,             /* level */
	                        Tx->cFormat,   /* real format */
	                        Tx->w,         /* original width */
	                        Tx->h,         /* original height */
	                        0,             /* border */
	                        Tx->cSize,     /* compressed size*/
	                        Tx->cData);    /* compressed data*/
   } else {
      glTexImage2D(GL_TEXTURE_2D,    /* target */
                   0,                /* level */
                   TC,               /* internal format */
                   Tx->w, Tx->h,     /* width, height */
                   0,                /* border */
                   Tx->format,       /* texture format */
                   GL_UNSIGNED_BYTE, /* texture type */
                   Tx->data);        /* the texture */

      /* okay, now cache the compressed texture */
      Tx->TC = TC;
      if (Tx->cData != NULL) {
         free(Tx->cData);
         Tx->cData = NULL;
      }
      glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_ARB, &rv);
      if (rv) {
         glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, (GLint *)&Tx->cFormat);
         glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB, (GLint *)&Tx->cSize);
         if ((Tx->cData = malloc(Tx->cSize)) != NULL) {
            glGetCompressedTexImageARB(GL_TEXTURE_2D, 0, Tx->cData);
         }
      }
   }
}


static void Init( void )
{
   /* HEIGHT * WIDTH + 1 (for trailing '\0') */
   static char pattern[8 * 32 + 1] = {"\
                                \
    MMM    EEEE   SSS    AAA    \
   M M M  E      S   S  A   A   \
   M M M  EEEE    SS    A   A   \
   M M M  E         SS  AAAAA   \
   M   M  E      S   S  A   A   \
   M   M   EEEE   SSS   A   A   \
                                "
      };

   GLuint i, j;

   GLubyte (*texture1)[8 * 32][4];
   GLubyte (*texture2)[256][256][4];

   t1.w = 32;
   t1.h = 8;
   t1.size = t1.w * t1.h * 4;
   t1.data = malloc(t1.size);
   t1.format = GL_RGBA;
   t1.TC = GL_RGBA;

   texture1 = (GLubyte (*)[8 * 32][4])t1.data;
   for (i = 0; i < sizeof(pattern) - 1; i++) {
       switch (pattern[i]) {
              default:
              case ' ':
                   (*texture1)[i][0] = 255;
                   (*texture1)[i][1] = 255;
                   (*texture1)[i][2] = 255;
                   (*texture1)[i][3] = 64;
                   break;
              case 'M':
                   (*texture1)[i][0] = 255;
                   (*texture1)[i][1] = 0;
                   (*texture1)[i][2] = 0;
                   (*texture1)[i][3] = 255;
                   break;
              case 'E':
                   (*texture1)[i][0] = 0;
                   (*texture1)[i][1] = 255;
                   (*texture1)[i][2] = 0;
                   (*texture1)[i][3] = 255;
                   break;
              case 'S':
                   (*texture1)[i][0] = 0;
                   (*texture1)[i][1] = 0;
                   (*texture1)[i][2] = 255;
                   (*texture1)[i][3] = 255;
                   break;
              case 'A':
                   (*texture1)[i][0] = 255;
                   (*texture1)[i][1] = 255;
                   (*texture1)[i][2] = 0;
                   (*texture1)[i][3] = 255;
                   break;
       }
   }

   t2.w = 256;
   t2.h = 256;
   t2.size = t2.w * t2.h * 4;
   t2.data = malloc(t2.size);
   t2.format = GL_RGBA;
   t2.TC = GL_RGBA;

   texture2 = (GLubyte (*)[256][256][4])t2.data;
   for (j = 0; j < t2.h; j++) {
      for (i = 0; i < t2.w; i++) {
         (*texture2)[j][i][0] = sqrt(i * j * 255 * 255 / (t2.w * t2.h));
         (*texture2)[j][i][1] = 0;
         (*texture2)[j][i][2] = 0;
         (*texture2)[j][i][3] = 255;
      }
   }

   t3.data = LoadRGBImage(TEXTURE_FILE, (GLint *)&t3.w, (GLint *)&t3.h, &t3.format);
   t3.size = t3.w * t3.h * ((t3.format == GL_RGB) ? 3 : 4);
   t3.TC = GL_RGBA;

   ReInit(GL_RGBA, Tx = &t1);

   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
   glEnable(GL_TEXTURE_2D);

   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}


static void Key( unsigned char key, int x, int y )
{
   (void) x;
   (void) y;
   switch (key) {
      case 27:
         exit(0);
         break;
      case ' ':
         Anim = !Anim;
         if (Anim)
            glutIdleFunc( Idle );
         else
            glutIdleFunc( NULL );
         break;
      case 't':
         if (Tx == &t1) {
            Tx = &t2;
         } else if (Tx == &t2) {
            Tx = &t3;
         } else {
            Tx = &t1;
         }
         ReInit(Tx->TC, Tx);
         break;
      case '9':
         ReInit(GL_RGB, Tx);
         break;
      case '0':
         ReInit(GL_RGBA, Tx);
         break;
      case '1':
         ReInit(GL_COMPRESSED_RGB, Tx);
         break;
      case '2':
         ReInit(GL_COMPRESSED_RGBA, Tx);
         break;
      case '3':
         if (fxt1) ReInit(GL_COMPRESSED_RGB_FXT1_3DFX, Tx);
         break;
      case '4':
         if (fxt1) ReInit(GL_COMPRESSED_RGBA_FXT1_3DFX, Tx);
         break;
      case '5':
         if (dxtc) ReInit(GL_COMPRESSED_RGB_S3TC_DXT1_EXT, Tx);
         break;
      case '6':
         if (dxtc) ReInit(GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, Tx);
         break;
      case '7':
         if (dxtc) ReInit(GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, Tx);
         break;
      case '8':
         if (dxtc) ReInit(GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, Tx);
         break;
      case 'a':
         if (s3tc) ReInit(GL_RGB_S3TC, Tx);
         break;
      case 's':
         if (s3tc) ReInit(GL_RGB4_S3TC, Tx);
         break;
      case 'd':
         if (s3tc) ReInit(GL_RGBA_S3TC, Tx);
         break;
      case 'f':
         if (s3tc) ReInit(GL_RGBA4_S3TC, Tx);
         break;
   }
   glutPostRedisplay();
}


int main( int argc, char *argv[] )
{
   glutInit( &argc, argv );
   glutInitWindowPosition( 0, 0 );
   glutInitWindowSize( 400, 300 );

   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );

   if (glutCreateWindow(argv[0]) <= 0) {
      printf("Couldn't create window\n");
      exit(0);
   }

   if (!glutExtensionSupported("GL_ARB_texture_compression")) {
      printf("Sorry, GL_ARB_texture_compression not supported\n");
      exit(0);
   }
   if (glutExtensionSupported("GL_3DFX_texture_compression_FXT1")) {
      fxt1 = GL_TRUE;
   }
   if (glutExtensionSupported("GL_EXT_texture_compression_s3tc")) {
      dxtc = GL_TRUE;
   }
   if (glutExtensionSupported("GL_S3_s3tc")) {
      s3tc = GL_TRUE;
   }

   Init();

   glutReshapeFunc( Reshape );
   glutKeyboardFunc( Key );
   glutDisplayFunc( Display );
   if (Anim)
      glutIdleFunc( Idle );

   glutMainLoop();
   return 0;
}