View Full Version : dynamic alpha textures

Nick's a Novice
04-19-2006, 08:53 PM
hey I have a question about alpha textures. I have a 5x5 array of alpha values from 0-1 that I am generating on the fly. I need to create an alpha texture from this. Then once I've created this alpha texture, I need to apply it to a texture file I loaded form a bmp. I'm sure it must be posible to do this but I have no idea how. Anyone have any idea to do this...oh but one more thing I have to do it at real time. Thanks folks!

Serge K
04-19-2006, 11:50 PM
Originally posted by Nick's a Novice:
I have a 5x5 array of alpha values from 0-1 that I am generating on the fly. I need to create an alpha texture from this. Then once I've created this alpha texture, I need to apply it to a texture file I loaded form a bmp.Does that mean that you have 2 textures (1st - Alpha, 2nd - RGB) of different size?
And how exactly do you want to "apply" them?

Nick's a Novice
04-22-2006, 02:04 AM
I have one texture: an rgb image loaded from a bitmap file. It is size 200 x 200. I also have a 5x5 array of float values that I want to convert into an alpha texture. The alpha texture could be whatever size is required to be combined as an alpha layer to the rgb image. But what I want to use it for is to vary the alpha of my rgb image. (each of the 5x5 blocks can be a 40x40 block in the corresponding 200x200 alpha texture if required.) so whether its loading a new alpha image or modifying one either way would work, but it needs to be modified at run time. Any thoughts?

04-22-2006, 04:33 AM
When loading the image, just apply the matching alpha value. I don't see any other solution.

Nick's a Novice
04-22-2006, 05:12 PM
the alpha value will change throughout runtime. and it contains 25 seperate alpha values for each texture. How could i apply them all, and then change them at runtime?

04-22-2006, 08:22 PM
The OpenGL programming guide covers texturing. I suggest that you read it.


Nick's a Novice
04-23-2006, 10:07 AM
I have, but I can't find anything that deals with my particular problem. There is nothing in the opengl programming guide that covers dynamicaly creating an alpha texture from an array of float variables in real time then applying them to my single polygon. I can see an easy way of doing it using 25 seperate quads but I'm realy trying to avoid that as the overhead for that would be unsatisfactory. I'm thinking there should be a way to do it using texture environment and GL_MODULATE, but aside from the openGL spec, which has good info but lacking in examples, I can't seem to find a realy good example of how to use these. Also there still is my problem of creating an alpha only texture in the first place.

04-23-2006, 01:07 PM
Create two textures, one for the color and one for the alpha. Use glTexSubImage2D to upload the new texture data at runtime. Bind both textures to different texture units, using GL_REPLACE for the alpha texture. This will replace whatever alpha value a fragment had before with the value from the texture.

It could look roughly like this:

GLuint ctex, atex;
float alpha[5*5];

void init(void) {
char* colordata = load_color_bitmap();

/* create textures objects */
glGenTextures(1, &ctex);
glGenTextures(1, &atex);

/* initialize color texture */
glBindTexture(GL_TEXTURE_2D, ctex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); // create power-of-two sized texture
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 200, 200, GL_RGB, GL_UNSIGNED_BYTE, colordata); // upload color data

/* initialize alpha texture */
glBindTexture(GL_TEXTURE_2D, atex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 8, 8, 0, GL_ALPHA, GL_FLOAT, NULL); // create power-of-two sized texture

/* whatever else */

void display(void) {
/* do usual stuff */

/* upload new alpha data */
glBindTexture(GL_TEXTURE_2D, atex);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 5, 5, GL_ALPHA, GL_FLOAT, alpha);

/* bind color texture to unit 0 */
glBindTexture(GL_TEXTURE_2D, ctex);

/* bind alpha texture to unit 1 */
glBindTexture(GL_TEXTURE_2D, atex);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // this will replace only the alpha value

/* draw quad */
glMultiTexCoord2f(GL_TEXTURE_0, 0.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE_1, 0.0, 0.0);
glVertex2f(0.0, 0.0);

glMultiTexCoord2f(GL_TEXTURE_0, 200.0/256.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE_1, 5.0/8.0, 0.0);
glVertex2f(1.0, 0.0);

glMultiTexCoord2f(GL_TEXTURE_0, 200.0/256, 200.0/256.0);
glMultiTexCoord2f(GL_TEXTURE_1, 5.0/8.0, 5.0/8.0);
glVertex2f(1.0, 1.0);

glMultiTexCoord2f(GL_TEXTURE_0, 0.0, 200.0/256.0);
glMultiTexCoord2f(GL_TEXTURE_1, 0.0, 5.0/8.0);
glVertex2f(0.0, 1.0);

/* do other usual stuff */

Nick's a Novice
04-23-2006, 02:23 PM
thanks! I'll give that a go this afternoon!