2 * Test texture compression.
6 #define GL_GLEXT_PROTOTYPES
13 #define IMAGE_FILE "../images/arch.rgb"
15 static int ImgWidth
, ImgHeight
;
16 static GLenum ImgFormat
;
17 static GLenum CompFormat
;
18 static GLfloat EyeDist
= 5.0;
19 static GLfloat Rot
= 0.0;
20 const GLenum Target
= GL_TEXTURE_2D
;
26 GLenum err
= glGetError();
28 printf("GL Error %d at line %d\n", (int) err
, line
);
34 LookupFormat(GLenum format
)
37 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
38 return "GL_COMPRESSED_RGB_S3TC_DXT1_EXT";
39 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
40 return "GL_COMPRESSED_RGBA_S3TC_DXT1_EXT";
41 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
42 return "GL_COMPRESSED_RGBA_S3TC_DXT3_EXT";
52 GLboolean all
= 0*GL_TRUE
;
57 glGetTexLevelParameteriv(Target
, 0,
58 GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB
, &size
);
59 glGetTexLevelParameteriv(Target
, 0, GL_TEXTURE_INTERNAL_FORMAT
, &fmt
);
61 buffer
= (GLubyte
*) malloc(size
);
62 glGetCompressedTexImageARB(Target
, 0, buffer
);
64 printf("Testing sub-texture replacement\n");
66 glCompressedTexImage2DARB(Target
, 0,
67 fmt
, ImgWidth
, ImgHeight
, 0,
71 glCompressedTexSubImage2DARB(Target
, 0,
73 ImgWidth
, ImgHeight
/ 2,
76 glCompressedTexSubImage2DARB(Target
, 0,
77 0, ImgHeight
/ 2, /* pos */
78 ImgWidth
, ImgHeight
/ 2,
79 fmt
, size
/2, buffer
+ size
/ 2);
87 LoadCompressedImage(const char *file
)
89 const GLenum filter
= GL_LINEAR
;
93 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
94 glPixelStorei(GL_PACK_ALIGNMENT
, 1);
97 * Load image and scale if needed.
99 image
= LoadRGBImage( file
, &ImgWidth
, &ImgHeight
, &ImgFormat
);
101 printf("Couldn't read %s\n", IMAGE_FILE
);
104 printf("Image is %d x %d\n", ImgWidth
, ImgHeight
);
107 assert(ImgWidth
== 128 || ImgWidth
== 256 || ImgWidth
== 512);
108 assert(ImgWidth
== 128 || ImgHeight
== 256 || ImgHeight
== 512);
110 if (ImgFormat
== GL_RGB
)
111 CompFormat
= GL_COMPRESSED_RGB_S3TC_DXT1_EXT
;
113 CompFormat
= GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
;
115 if (ImgFormat
== GL_RGBA
) {
117 for (i
= 0; i
< ImgWidth
* ImgHeight
; i
++) {
118 if (image
[i
*4+3] != 0 && image
[i
*4+3] != 0xff) {
121 if (image
[i
*4+3] == 0)
122 image
[i
*4+3] = 4 * i
/ ImgWidth
;
124 printf("Num Alpha !=0,255: %d\n", numAlpha
);
127 CompFormat
= GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
;
131 * Give image to OpenGL and have it compress it.
133 glTexImage2D(Target
, 0, CompFormat
, ImgWidth
, ImgHeight
, 0,
134 ImgFormat
, GL_UNSIGNED_BYTE
, image
);
135 CheckError(__LINE__
);
139 glGetTexLevelParameteriv(Target
, 0, GL_TEXTURE_INTERNAL_FORMAT
, &p
);
140 printf("Compressed Internal Format: %s (0x%x)\n", LookupFormat(p
), p
);
141 assert(p
== CompFormat
);
143 printf("Original size: %d bytes\n", ImgWidth
* ImgHeight
* 3);
144 glGetTexLevelParameteriv(Target
, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB
, &p
);
145 printf("Compressed size: %d bytes\n", p
);
147 glTexParameteri(Target
, GL_TEXTURE_MIN_FILTER
, filter
);
148 glTexParameteri(Target
, GL_TEXTURE_MAG_FILTER
, filter
);
156 Init(const char *file
)
158 GLint numFormats
, formats
[100];
161 if (!glutExtensionSupported("GL_ARB_texture_compression")) {
162 printf("Sorry, GL_ARB_texture_compression is required.\n");
165 if (!glutExtensionSupported("GL_EXT_texture_compression_s3tc")) {
166 printf("Sorry, GL_EXT_texture_compression_s3tc is required.\n");
170 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
171 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
173 glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS_ARB
, &numFormats
);
174 glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS_ARB
, formats
);
175 printf("%d supported compression formats: ", numFormats
);
176 for (p
= 0; p
< numFormats
; p
++)
177 printf("0x%x ", formats
[p
]);
180 LoadCompressedImage(file
);
182 glEnable(GL_TEXTURE_2D
);
184 if (ImgFormat
== GL_RGBA
) {
185 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
192 Reshape( int width
, int height
)
194 glViewport( 0, 0, width
, height
);
195 glMatrixMode( GL_PROJECTION
);
197 glFrustum(-1, 1, -1, 1, 4, 100);
198 glMatrixMode( GL_MODELVIEW
);
204 Key( unsigned char key
, int x
, int y
)
234 glClearColor(0.3, 0.3, .8, 0);
235 glClear(GL_COLOR_BUFFER_BIT
);
238 glTranslatef(0, 0, -(EyeDist
+0.01));
239 glRotatef(Rot
, 0, 0, 1);
241 glTexCoord2f(0, 0); glVertex2f(-1, -1);
242 glTexCoord2f(1, 0); glVertex2f( 1, -1);
243 glTexCoord2f(1, 1); glVertex2f( 1, 1);
244 glTexCoord2f(0, 1); glVertex2f(-1, 1);
253 main( int argc
, char *argv
[] )
255 glutInit( &argc
, argv
);
256 glutInitWindowSize( 600, 600 );
258 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
260 glutCreateWindow(argv
[0]);
262 glutReshapeFunc( Reshape
);
263 glutKeyboardFunc( Key
);
264 glutDisplayFunc( Draw
);