gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > [SOLVED] Dynamic texture creation, formats and palette

#167295 - kiniou - Sun Mar 08, 2009 12:56 pm

Hello,

I'm trying to create a dynamic texture with glTexImage2D to apply a sort of animation on a 3D model. I manage to create one in GL_RGB format with function RGB15(). Here is my code:

Code:

    int size_max = 128;
    int col = 16;
    int size = 0;
    u8  c = 0;
   
    while(size<size_max) {

      //c is the colour components between 0 and 32
      //this is a 16 columns and 8 lines texture
      //so the final result is a greyscale texture starting left from black to white
                c = (u8)( (size%col) / (float)col * 32.0 );
                this->dynamic_texture[size] = RGB15(c,c,c);
                size++;
    }
   
    glGenTextures(1, &this->textureID[2]);
    glBindTexture(0, this->textureID[2]);
    glTexImage2D(0, 0, GL_RGB, TEXTURE_SIZE_16 , TEXTURE_SIZE_8, 0, TEXGEN_TEXCOORD , (uint8 *) this->dynamic_texture);


Then I write some crappy code to shift this greyscale texture and create new ones to have an animated texture.
I've made a test with 16 textures and it works but it takes a lot of texture memory and it's not efficient.

Now I'm looking to change this crappy code to use 16 palettes instead of 16 textures.
I tried to use GL_RGB16 thinking of each texel was a number between 0 and 15 but it doesn't seems to work and can't find how it must be formatted.
Code:

int size_max = 128;
    int col = 16;
    int size = 0;
    uint8  c = 0;
   
    while(size<size_max) {

        //c is the colour components between 0 and 16
        //this is a 16 columns and 8 lines texture
        //so the final result is a greyscale texture starting left from black to white
        c = (u8)( (size%col) );
        this->dynamic_texture[size] = c;
        size++;
    }
   
    glGenTextures(1, &this->textureID[2]);
    glBindTexture(0, this->textureID[2]);
    glTexImage2D(0, 0, GL_RGB16, TEXTURE_SIZE_16 , TEXTURE_SIZE_8, 0, TEXGEN_TEXCOORD , (uint8 *) this->dynamic_texture);
   

    for(uint16 i=0;i<16;i++) {
        this->dynamic_palette[i]=RGB15(i*2,i*2,i*2);
    }
   
    this->pal_addr = gluTexLoadPal((const u16 *) &this->dynamic_palette,16,GL_RGB16);


You can download the packed files for this test Here .

Thanks for any help or explanations.
_________________
It's time to code...


Last edited by kiniou on Mon Mar 23, 2009 5:04 pm; edited 1 time in total

#167298 - Kath - Sun Mar 08, 2009 3:05 pm

The texture formats with palettes are usually packed, for example:

Format 3: 16-Color Palette Texture
Each Texel occupies 4bit, the 1st Texel is located in LSBs of 1st byte.

You can read about the texture formats here.

#167717 - kiniou - Mon Mar 23, 2009 5:03 pm

Thanks Kath for your reply.

I've already read that part but I didn't understood the packed formats and after reading it again it became clearer.

Now my problem is solved, I post the code in case someone wants to reuse it:

Code:

    int size_max = 128; // 16 columns * 8 lines = 128 pixels
    int col = 16;
    uint8  c1 = 0;
    uint8  c2 = 0;
    u8 c = 0;

    for(int i=(size_max/2 -1);i>=0;i--) {

        //c1 and c2 are the color components between 0 and 16
        //packed into a uint8

            c  = (u8)( (i*2) % col );
            c1 = c ;


            c  = (u8)( (i*2 + 1) % col );
            c2 = c << 4 ;

            this->dynamic_texture[i] =( c1 | c2 ) ;
    }

    glGenTextures(1, &this->textureID[2]);
    glBindTexture(0, this->textureID[2]);
    glTexImage2D(0, 0, GL_RGB16, TEXTURE_SIZE_16 , TEXTURE_SIZE_8, 0, TEXGEN_TEXCOORD , this->dynamic_texture);


    //creation of 16 palettes RGB16
    uint16 k = 0;
    for (uint16 j=0;j<16;j++) {
        for(uint16 i=0;i<16;i++) {
            k = (16 - (i+j)%16) * 2;
            this->dynamic_palette[i]=RGB15(k,k,k);
        }

        this->pal_addr[j] = gluTexLoadPal((const u16 *) &this->dynamic_palette,16,GL_RGB16);
    }

_________________
It's time to code...