2 * Test texture compression.
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
;
25 GLenum err
= glGetError();
27 printf("GL Error %d at line %d\n", (int) err
, line
);
33 LookupFormat(GLenum 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";
51 GLboolean all
= 0*GL_TRUE
;
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");
64 glCompressedTexImage2DARB(Target
, 0,
65 fmt
, ImgWidth
, ImgHeight
, 0,
69 glCompressedTexSubImage2DARB(Target
, 0,
71 ImgWidth
, ImgHeight
/ 2,
74 glCompressedTexSubImage2DARB(Target
, 0,
75 0, ImgHeight
/ 2, /* pos */
76 ImgWidth
, ImgHeight
/ 2,
77 fmt
, size
/2, buffer
+ size
/ 2);
89 buffer
= (GLubyte
*) malloc(3 * ImgWidth
* ImgHeight
);
91 glGetTexImage(GL_TEXTURE_2D
,
102 LoadCompressedImage(const char *file
)
104 const GLenum filter
= GL_LINEAR
;
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
);
116 printf("Couldn't read %s\n", IMAGE_FILE
);
119 printf("Image is %d x %d\n", ImgWidth
, ImgHeight
);
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
;
128 CompFormat
= GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
;
130 if (ImgFormat
== GL_RGBA
) {
132 for (i
= 0; i
< ImgWidth
* ImgHeight
; i
++) {
133 if (image
[i
*4+3] != 0 && image
[i
*4+3] != 0xff) {
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__
);
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
);
174 Init(const char *file
)
176 GLint numFormats
, formats
[100];
179 if (!glutExtensionSupported("GL_ARB_texture_compression")) {
180 printf("Sorry, GL_ARB_texture_compression is required.\n");
183 if (!glutExtensionSupported("GL_EXT_texture_compression_s3tc")) {
184 printf("Sorry, GL_EXT_texture_compression_s3tc is required.\n");
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
]);
198 LoadCompressedImage(file
);
200 glEnable(GL_TEXTURE_2D
);
202 if (ImgFormat
== GL_RGBA
) {
203 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
210 Reshape( int width
, int height
)
212 glViewport( 0, 0, width
, height
);
213 glMatrixMode( GL_PROJECTION
);
215 glFrustum(-1, 1, -1, 1, 4, 100);
216 glMatrixMode( GL_MODELVIEW
);
222 Key( unsigned char key
, int x
, int y
)
252 glClearColor(0.3, 0.3, .8, 0);
253 glClear(GL_COLOR_BUFFER_BIT
);
256 glTranslatef(0, 0, -(EyeDist
+0.01));
257 glRotatef(Rot
, 0, 0, 1);
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);
271 main( int argc
, char *argv
[] )
273 glutInit( &argc
, argv
);
274 glutInitWindowSize( 600, 600 );
276 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
278 glutCreateWindow(argv
[0]);
281 glutReshapeFunc( Reshape
);
282 glutKeyboardFunc( Key
);
283 glutDisplayFunc( Draw
);