PDA

View Full Version : run through a texture



09-11-2004, 12:22 AM
Hello,

I would like to know if its possible to run through a texture stored in an AUX_RGBImageRec type, to access the single pixels of it.
I want to add some kind of color-keying to my textures through setting pixels with a special color-value to an 0-alpha-value.

I took the Nehe Lesson 6 for loading Textures.

Can you help me?
Marco

09-12-2004, 12:32 PM
marco, it is possible. if you peek inside GLAux.h, you'll see a structure like this:


typedef struct _AUX_RGBImageRec {
GLint sizeX, sizeY;
unsigned char *data;
} AUX_RGBImageRec;since aux is no longer documented in .net 2k3, and since i have never personally used this structure (or aux for that matter), i make no guarantess about the following code.

assuming you have successfully loaded your image with auxDIBImageLoad, you can plow through the bits with this:


unsigned char* data = image->data;
for( int y = 0; y < image->sizeY; y++ )
{
for( int x = 0; x < image->sizeX; x++ )
{
// get or set here
unsigned char red = data[0];
unsigned char green = data[1];
unsigned char blue = data[2];

// advance to next rgb triple
// !!! assumes packed rgb
data += 3;
}
}

09-13-2004, 04:48 AM
Thank you very much for your answer... I'll try it that way. :)

09-13-2004, 07:06 AM
Sorry for double-posting, just wanted to say that it worked great. I got it after a while :)
Thank you for your help.

However I've another question ;)
I want to make it possible to load non-squared textures like 140x60 pixels, instead of 128x128... I think it works when I load the .bmp and copy it to a 128x128 texture, and the fill-up the empty parts with black. I think it should be no problem to code, however here are to questions:

- How can I check the nearest square-size to the size of the bitmap... eg, how can I tell my code that a 256x256 pixel-texture should be created when there's a 210x280 pixel bitmap?

- How can I set the texture-coordinates to a QUAD that it gets only textured with the part of the texture that is filled?

If you want to know why I need it: I'm coding a 2D-engine with OpenGL, and I want that I can load free-sized bitmaps :)
I set up the orthographic mode, so one coordinate-unit is 1 pixel.

Aeluned
09-13-2004, 07:29 AM
macro,

rather than scaling your images to power of two dimensions you can use texture rectangles which allow textures of arbitrary dimensions.
check out: http://oss.sgi.com/projects/ogl-sample/registry/ARB/texture_rectangle.txt

using this approach you also eat up less texture memory for your textures (generally).
there are some limitations (no GL_REPEAT texture mode for one)...

if you decide to use power of two textures you can adapt the code below to find the nearest power of two given the native resolution of the image



int nextPow2(int i)
{
//get to the nearest power of two dimension
//less than i.
int pow2 = 2;
while(pow2 < i)
pow2 *= 2;

//deterimine if we scale up or down now:
//find the power of two which is closest to
//native size
if(abs(i-pow2) < abs(i-(pow2*2)))
return pow2

return pow2*2;
}

09-13-2004, 11:48 AM
hey marco, glad it worked out for you!

yeah, i would do as aeluned suggested. then to get your coords to fit the actual image area, i'd use the coverage percentage.

as per your example:

width = 210;
pow2width = 256;
texture_s = 210 / 256;

height = 280;
pow2height = 512;
texture_t = 280/512;

09-14-2004, 06:13 AM
Thank your for all your friendly help. :)
I could do big progresses through it. :)