PDA

View Full Version : How to split up a large texture ?



ewan_s
05-25-2001, 02:30 AM
Here's the problem, I need to support very large textures for use as backgrounds in my application. The textures are usually aerial photos and sizes of 2000+ pixels are not uncommon. Also a 2^x ratio is not guaranteed.

I know that the best solution to this problem would be to read the single large texture, split it up in to X manageable tiles (all of which conform to 2^x), bind each tile to a texture object and then draw the scene using the multiple tiles.

My problem is how to split-up / scale the single large texture (says its size is 2000 x 2100) in to 16 smaller textures of say 512 x 512. I'm stuck on the actual process/code required to split up the one big image into several smaller tiles, I just can't get my head around it !

Any help, links, code, are appreciated.

Thanks
Ewan

Bob
05-25-2001, 03:17 AM
for(y=0; y<512; y++)
{
for(x=0; x<512; x++)
{
subImage[x][y] = origImage[x+subStartX][y+subStartY]
}
}

subStartX and -Y is the startoffset for the subimage.

This will extract a 512x512 image from the original image. Just increase subStartX and -Y with 512 for each subimage you create.

ewan_s
05-25-2001, 09:12 AM
Ok is see what you mean here, but when I read in the image data from the texture file I use the code below to read and bind the image data to a texture object.

-----------------------------------------------------------
void read_rgb_24bit(FILE *s, int width, int height)
{
unsigned char *rgb;
unsigned char temp;
int bread;
int i;
int size = width*height;

rgb = malloc_test(sizeof(int) * size * 3, "Texture Data", TextureMemTag);

if (rgb == NULL) return 0;

bread = fread (rgb, sizeof (unsigned char), size * 3, s);
if (bread != size * 3)
{
free_tag(TextureMemTag);
return 0;
}


for (i = 0; i < size * 3; i += 3)
{
temp = rgb[i];
rgb[i] = rgb[i + 2];
rgb[i + 2] = temp;
}

TextureFormat = GL_RGB;

bind_texture(1, width, height, TextureFormat, rgb);
}

void bind_texture(int id, int pix_width, int pix_height, GLenum format,
unsigned char *data)
{
/* dull openGl stuff */
glBindTexture(GL_TEXTURE_2D, id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

/* scaling a streching */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

/* bind the texture */
glTexImage2D(GL_TEXTURE_2D, 0, format, pix_width, pix_height,
0, format, GL_UNSIGNED_BYTE, data);
}
-----------------------------------------------------------

So my image data is in an unsigned char *rgb not a 2D char array. Maybe I don't understand how the image data is actually stored/used by openGL but how to I get from an unsigned char *rgb to the 'origImage[X][y]' in your code ? Then how to I get back to the unsigned char *data for binding from each 'subImage[x][y]' ?

I think that this is the bit I'm struggling to understand. Then if you add in the need to scale/pad the 'origImage' data if the texture is not a power of 2, I'm really confused ?

Any further help is appreciated,
Thanks
Ewan

Deiussum
05-27-2001, 06:25 AM
Converting the indices from a 2d array to a flat array is really pretty easy. It just takes a little bit of math. The basic formula would be this...

index = ((y*width)+x)*3;