Skip to main content
GameDev.net gamedev.net
🔒 Locked

What's wrong in my code ( Texture compression )

Started by Frag91 Apr 18, 2008 at 3:50 PM 7 replies 5.1k views
Original Post
Frag91
Frag91
Hi, i'm tryng to create an engine, and now i want to add the texture compression loader in my engine. So, that's the code: COMPRESSER: /* bla bla bla TGA loading */ ... ... if ( status = true ) { nTexture = textureId++; glGenTextures (1, &nTexture); glBindTexture (GL_TEXTURE_2D, nTexture); glTexImage2D (GL_TEXTURE_2D, 0, GL_COMPRESSED_RGB, texti->width, texti->height, 0, texti->type, GL_UNSIGNED_BYTE, texti->data); glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_ARB, &compressed); if ( compressed == GL_TRUE ) { GLubyte *comp_data; //Qui mettiamo i dati compressi della texture GLuint width, height; //Larghezza e altezza della texture GLint internal_format; //Formato di compressione della texture GLint imageSize; //Grandezza della texture glGetTexLevelParameteriv(GL_TEXTURE_2D,0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB, &imageSize); cout << "Grandezza immagine dopo compressione: " << imageSize << " Bytes" << endl; comp_data = (GLubyte*)malloc(imageSize*sizeof(GLubyte)); //Otteniamo il formato interno dell'immagine glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &internal_format); cout << "Formato interno: " << internal_format << endl; glGetCompressedTexImage( GL_TEXTURE_2D, 0, comp_data ); width = texti->width; height = texti->height; cout << "Inizio scrittura del nuovo file..." << endl; FILE *f1; //Nuova texture f1 = fopen ( "new.mtx", "wb" ); /* STRUTTURA FILE TEXTURE // WIDTH // HEIGHT // INTERNAL FORMAT // SIZE // DATA */ fprintf( f1, "%d %d %X %d",width, height, internal_format, imageSize ); fwrite ( comp_data, 1, imageSize, f1); fclose (f1); ----------------------------------------------------------------------- And that's the loader: bool CTexture::loadMtx(FILE *fTGA, texture *texti) { cout << "Carico un MTX" << endl; fscanf(fTGA, "%d %d %X %d",&texti->width, &texti->height, &texti->internal_format, &texti->size ); cout << texti->width << " " << texti->height << " " << texti->internal_format << " " << texti->size << endl; texti->data = (GLubyte*)malloc(texti->size); fread ( texti->data, 1, texti->size, fTGA); return true; } /* bla bla, mtx loading */ GLint compressed; GLint imageSize; if ( status = true ) { nTexture = textureId++; glGenTextures (1, &nTexture); glBindTexture (GL_TEXTURE_2D, nTexture); glCompressedTexImage2D(GL_TEXTURE_2D, 0, texti->internal_format, texti->width, texti->height, 0, texti->size, texti->data); //glTexImage2D (GL_TEXTURE_2D, 0, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, texti->width, texti->height, 0, texti->type, GL_UNSIGNED_BYTE, texti->data); //Setta i filtri e crea il mipmap glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); // gluBuild2DMipmaps( GL_TEXTURE_2D, 3, texti->width, texti->height, texti->type, GL_UNSIGNED_BYTE, texti->data); } ----------------------------------------------------------------------- Well, i get a white square... why? What's wrong? For the extensions, i'm using glee compiled within the project.. If someone wants, i can give him the entire source code, it's too big for postint it here. Thanks and sorry for my bad english
Kalidor
Kalidor
You're setting a mipmapping minification filter, but only providing level 0 of the texture (ie: no mipmaps). In this case it is as if texturing were disabled for the corresponding texture unit. See section 3.8.10 in the OpenGL Spec. There may be other issues, but this is the one I always look for first (it is a very common mistake).

Also, GL_LINEAR_MIPMAP_LINEAR is not a valid magnification filter. See glTexParameter for more info.
Frag91
Frag91
Well, there was some progress :)
But it's still shi**

I've changed some stuff to the exporter, now it only uses the gluBuild2dMipmaps:

------------------------------------------------------------------------------------------

gluBuild2DMipmaps(GL_TEXTURE_2D, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, texti->width, texti->height, GL_RGB, GL_UNSIGNED_BYTE, texti->data);

GLubyte *comp_data; //Qui mettiamo i dati compressi della texture
GLuint width, height; //Larghezza e altezza della texture
GLint internal_format; //Formato di compressione della texture
GLint imageSize; //Grandezza della texture

glGetTexLevelParameteriv(GL_TEXTURE_2D,0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB, &imageSize);
cout << "Grandezza immagine dopo compressione: " << imageSize << " Bytes" << endl;
comp_data = (GLubyte*)malloc(imageSize*sizeof(GLubyte));

//Otteniamo il formato interno dell'immagine
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &internal_format);
cout << "Formato interno: " << internal_format << endl;

glGetCompressedTexImage( GL_TEXTURE_2D, 0, comp_data );

width = texti->width;
height = texti->height;

cout << "Inizio scrittura del nuovo file..." << endl;

FILE *f1; //Nuova texture
f1 = fopen ( "new.mtx", "wb" );

/* STRUTTURA FILE TEXTURE
// WIDTH
// HEIGHT
// INTERNAL FORMAT
// SIZE
// DATA
*/
fprintf( f1, "%d %d %X %d",width, height, internal_format, imageSize );
fwrite ( comp_data, 1, imageSize, f1);
fclose (f1);

-------------------------------------------------------------------------------------------

The loader too only uses the gluBuild2dMipmaps

-----------------------------------------------------------------------------------------

if ( status = true )
{
nTexture = textureId++;
glGenTextures (1, &nTexture);
glBindTexture (GL_TEXTURE_2D, nTexture);

glCompressedTexImage2D(GL_TEXTURE_2D, 0, texti->internal_format, texti->width, texti->height, 0, texti->size, texti->data);

//Setta i filtri e crea il mipmap
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, texti->width, texti->height, GL_RGB, GL_UNSIGNED_BYTE, texti->data);

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

}

-----------------------------------------------------------------------------------------

But.. this is what i get:
http://img294.imageshack.us/img294/4757/schermata1fd8.png
Frag91
Frag91
up T_T
MARS_999
MARS_999
I have not read your entire post, but are you trying to load compressed textures from a file? or you are loading an uncompressed texture, compressing it and saving it out as a compressed file, and then loading it again in the engine as compressed?

Why not load the data in uncompressed use GL to compress it with GL_GENERATE_MIPMAPS set to true in glTexParameteri(), then get the base level mipmap using the glGetCompressedTexImage*() and save that data to file... I am not seeing this as being to difficult, unless I am not following you correctly.

Frag91
Frag91
Well, i'm tryng to.
Load an uncompressed tga, compress it, save to disk and then load in my game engine.
But i don't get what you say.

I think there's something wrong with my loader, relatives to the mipmaps, becouse in the compresser\exporter, i only comprime the image with

gluBuild2DMipmaps(GL_TEXTURE_2D, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, texti->width, texti->height, GL_RGB, GL_UNSIGNED_BYTE, texti->data);

then save it ( with width, height and some stuff too ) into a file.

Then I simply load it with

glCompressedTexImage2D(GL_TEXTURE_2D, 0, texti->internal_format, texti->width, texti->height, 0, texti->size, texti->data);

And set the mipmaps with

gluBuild2DMipmaps( GL_TEXTURE_2D, 3, texti->width, texti->height, GL_RGB, GL_UNSIGNED_BYTE, texti->data);

Instead, what are you telling me to do?
MARS_999
MARS_999
Look here

http://www.starcoder.com/wordpress/2007/09/10/using-compressed-textures-in-opengl/

see if that helps you.
Frag91
Frag91
Thx, i've solved on my own, as kallidor said, there were something wrong with the mipmaps, becouse i didn't create anyone, but i was using a mipmap filter, so there's the code:

EXPORTER\CONVERTER

glGenTextures (1, &nTexture);

glBindTexture (GL_TEXTURE_2D, nTexture);

glTexImage2D(GL_TEXTURE_2D,0, ( texti->type == GL_RGB ) ? GL_COMPRESSED_RGB_S3TC_DXT1_EXT : GL_COMPRESSED_RGBA_S3TC_DXT1_EXT , texti->width, texti->height,0, GL_RGB, GL_UNSIGNED_BYTE, texti->data);


glGetTexLevelParameteriv(GL_TEXTURE_2D,0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB, &imageSize);

cout << "Grandezza immagine dopo compressione: " << imageSize << " Bytes" << endl;

comp_data = (GLubyte*)malloc(imageSize*sizeof(GLubyte));

//Otteniamo il formato interno dell'immagine
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &internal_format);

cout << "Formato interno: " << internal_format << endl;

glGetCompressedTexImage( GL_TEXTURE_2D, 0, comp_data );

width = texti->width;
height = texti->height;

cout << "Inizio scrittura del nuovo file..." << endl;


f1 = fopen ( "new.mtx", "wb" );

/* STRUTTURA FILE TEXTURE
// WIDTH
// HEIGHT
// INTERNAL FORMAT
// SIZE
// DATA
*/
fprintf( f1, "%d %d %X %d",width, height, internal_format, imageSize );
fwrite ( comp_data, 1, imageSize, f1);
fclose (f1);

-------------------------------------------------------------------------------------------

And there's the loader:


if ( status = true )
{
nTexture = textureId++;
glGenTextures (1, &nTexture);
glBindTexture (GL_TEXTURE_2D, nTexture);

if (compFlag)
glCompressedTexImage2D(GL_TEXTURE_2D, 0, texti->internal_format, texti->width, texti->height, 0, texti->size, texti->data);

else
gluBuild2DMipmaps( GL_TEXTURE_2D, texti->type, texti->width, texti->height, texti->type, GL_UNSIGNED_BYTE, texti->data);


glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

------------------------------------------------------------------------------------------

Now i'm working for compriming and using the mipmaps, some help?
Frag91
Frag91
oook, that's just worked fine, with mipmaps in compressed images too!

That's the right code in the loader:

glGenTextures (1, &nTexture);
glBindTexture (GL_TEXTURE_2D, nTexture);

glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE );

if (compFlag)
glCompressedTexImage2D(GL_TEXTURE_2D, 0, texti->internal_format, texti->width, texti->height, 0, texti->size, texti->data);

else
glTexImage2D( GL_TEXTURE_2D, 0, 3, texti->width, texti->height, 0, texti->type, GL_UNSIGNED_BYTE, texti->data );


glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

--------------------------------------------------------------------------------------------


I've only told at openGL with
"glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE );"

to create previously mipmaps and no gluBuild2dMipmaps is more need.
Much faster, much small, much cool!

[OT]

How can I make spoilers?

[/OT]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.