loading a part of an image

Started by
7 comments, last by subflood 18 years, 10 months ago
Hello, how can I load a part of a bitmap? I have an 800 x 600 bitmap image and I only want to load half of it. I tried loading it like this: gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 400, 300, GL_BGR_EXT, GL_UNSIGNED_BYTE, TextureImg[0]->pixels); but when I rendered the scene the texture looked like it was zoomed into and it had these horizontal lines going throught it.
Advertisement
I think this is what you're looking for:
glTexSubImage2D
I'm having some trouble figuring out how to use this function. I need this for loading multiple sprites from one file instead of having hundreads of bitmap files I plan to have a few that have multiple sprites. But like I said I can't seem to figure out how to do this.

Here is how I used the function:

glTexSubImage2D(GL_TEXTURE_2D, 0, 1024, 1024,
TextureImg[0]->w, TextureImg[0]->h, GL_BGR_EXT,
GL_UNSIGNED_BYTE, TextureImg[0]->pixels);

all I'm getting is a white texture.
Well, it's hard to know exactly what's happening without seeing your image, but are you sure you want to have 1024, 1024 as the starting position?

Just an idea...
-Shane
The image's resolution is 4096 x 1024. I just want to load half of it, that would be 2048 x 512. How would I acomplish this?
Actually, 2048 X 512 is 1/4.

But assuming you wanted to load the left half of the image,
I think it would be something like this:


glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
TextureImg[0]->w / 2, TextureImg[0]->h, GL_BGR_EXT,
GL_UNSIGNED_BYTE, TextureImg[0]->pixels);

So you would go from 0 -> 2048 on the x direction,
and from 0 -> 1024 in the y direction.

Hope that helps

-Shane
Here is my current code:

       glGenTextures(1, &texture[0]);               glBindTexture(GL_TEXTURE_2D, texture[0]);               glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImg[0]->w, TextureImg[0]->h, 0,                    GL_BGR_EXT, GL_UNSIGNED_BYTE, TextureImg[0]->pixels);                                     glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,                        TextureImg[0]->w / 2, TextureImg[0]->h, GL_BGR_EXT,                        GL_UNSIGNED_BYTE, TextureImg[0]->pixels);                              glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );       glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); 


I used the 256 x 256 nehe logo

Here is what I got:

Free Image Hosting at www.ImageShack.us
Alright disregard the previous posts. :)

Here's how you do it:

glPixelStorei(GL_UNPACK_ALIGNMENT,4);

// this skips the first 512 colums from the left
glPixelStorei(GL_UNPACK_SKIP_PIXELS,512);

// this skips the first 512 rows from the bottom
glPixelStorei(GL_UNPACK_SKIP_ROWS,512);

// this is the total width of the image
glPixelStorei(GL_UNPACK_ROW_LENGTH,1024);

// so this would load the upper right quarter of a bitmap that is 1024 x 1024
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,512,512,0,GL_RGB,GL_UNSIGNED_BYTE,pBitmap->data);

// you'll have to change the params to fit your image.

You can look here for what the GL_UNPACK enums represent.

Hopefully that works for you.

Some help from this post.

-Shane
Iam forever in your debt.

This topic is closed to new replies.

Advertisement