Advertisement

Alpha maps in the frame buffer

Started by July 22, 2004 07:29 PM
18 comments, last by vincoof 20 years, 4 months ago
I can't get them to work. Here's the code I'm using to create the alpha maps:

void RandomAlphaMap()
{
#define szm 32
GLubyte map[szm*szm];
	
char v = (rand()%255);
		
for(int x=0;x<szm;x++)
 for(int y=0;y<szm;y++)
   alphamap[y*szm+x] = (rand()%255);		

glActiveTextureARB(GL_TEXTURE1_ARB);

glGenTextures(1,(GLuint*)↦_tex);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glBindTexture(GL_TEXTURE_2D,map_tex);
	
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D,4,szm,szm,GL_ALPHA,GL_UNSIGNED_BYTE,data);
	
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
}
I'm expecting random alpha distribution, but what I'm getting is solid alpha when using the alpha map:

glActiveTextureARB(GL_TEXTURE0_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,some_tex);
glActiveTextureARB(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,map_tex);

//draw geometry
Anybody see anything wrong or something I might be forgetting?
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
How do you map texture coordinates for texture unit #1 ?
using glMultiTexCoord, glTexGen, vertex arrays, or vertex programs ?

Do you define a seed for the random function (for instance, in the beginning of the program) ? If so, does the "solid alpha" look differently when you run the application multiple times ?

As a last note, GL_LINEAR_MIPMAP_LINEAR is not a valid magnification filter. You should stick to :
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
Advertisement
It seems being away from OpenGL does take its toll on one's ability to see the simplest of things - I was using glTexCoord... (the last time I programmed anything in OpenGL was well over a year ago).

Anyway - I'm now creating two test alpha maps like this:

map1[y*szm+x] = (rand()%255);map2[y*szm+x] = 255 - map1[y*szm+x];


which in my good faith should give me summed alpha of 255 at all points, but when splatting the whole thing, I get this:



Notice the transparency, which becomes evident on the far side of the hill (the transparency is also constant, which kind of implies that the sum of all alpha values in the alpha maps is something like 0.5f rather than 1.f).

Anyway - thanks for taking a million guesses in your previous post :)
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
It's pretty hard to define if alpha is really constant or not because your decal map is some kind of noisy.

Could you please replace the RGB output of the second texture unit by
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_CONSTANT);

Also, when you mean glTexCoord, in fact you mean glMultiTexCoord when it comes to specify texture coordinates for GL_TEXTURE1_ARB, isn't it ?
>> Could you please replace the RGB output of the second texture unit by glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_CONSTANT);

Do you mean replace:

glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);

with:

glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_CONSTANT);

for the second alphamap?

>> Also, when you mean glTexCoord, in fact you mean glMultiTexCoord when it comes to specify texture coordinates for GL_TEXTURE1_ARB, isn't it ?

I meant that I was using glTexCoord before your last post (which means that I wasn't specifying texture coordinates for TU1 at all...) - I'm now using glMultiTexCoord and texture mapping is working just fine - it's the blending that has me confused.
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Quote: Original post by Crispy
>> Could you please replace the RGB output of the second texture unit by glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_CONSTANT);

Do you mean replace:

glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);

with:

glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_CONSTANT);

for the second alphamap?

Yes.

As for your scheme, I'm sorry but I don't quite understand the point of it ^^'
Advertisement
Right - I think there's a little misunderstanding here. I'll explain what I'm trying to accomplish.

Basically what I'm after is simple texture splatting. According to the algorithm you create a set of alpha maps that sum to 1 at all points (this should get rid of the transparency), regardless of the number of alpha maps used or the alpha values in any of the maps, eg, if using 2 maps: m1[n] = 0.2, m2[n] = 0.8; or if using 4 maps m1[n] = 0.1, m2[n] = 0.45, m3[n] = 0.2, m4[n] = 0.25.

I'm using small alpha maps to get huge splats (for testing purposes only). Currently I'm using two textures, plus their respective alpha maps which are created as unsigned byte values. Setting map1[n] to m where 0 <= m <= 255 and map2[n] to 255 - m should give me a constant alpha value of 255 (1 in floating point format) for all textels on the alpha maps when they are combined.

I am using the same texture coordinates for both alpha maps, which results in rather nice splatting (visible in the image I posted above). However, I can't understand why tha alpha values don't sum to 1 (this causes the terrain to be semi-transparent).

I'm also having trouble understanding the point of what you do not understand :). Anyway - the docs for glTexEnvi pretty clearly state that GL_CONSTANT isn't one of the available values for the third argument and using it will result in the terrain being pitch black...

I hope it all made a little bit more sense this time...
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Quote: Original post by Crispy
I'm using small alpha maps to get huge splats (for testing purposes only). Currently I'm using two textures, plus their respective alpha maps which are created as unsigned byte values. Setting map1[n] to m where 0 <= m <= 255 and map2[n] to 255 - m should give me a constant alpha value of 255 (1 in floating point format) for all textels on the alpha maps when they are combined.


Ok,so basically you use two RGBA textures.How many rendering passes you perform?What is the BlendFunc?How do you setup the TexEnv for the two texture stages?
Quote: Original post by Crispy
I'm also having trouble understanding the point of what you do not understand :). Anyway - the docs for glTexEnvi pretty clearly state that GL_CONSTANT isn't one of the available values for the third argument

Yes it is, according to the ARB_texture_env_combine specification.

Quote: Original post by Crispy
and using it will result in the terrain being pitch black...

Sure, that's what I want to see. If the terrain color is uniform, then the only shades that will appear on screen will represent the alpha component of your texture.
In reference to the code you posted:

Crispy I am not sure, but I think you are just made a careless mistake in your code. You are generating the alpha map in the "map" array. But when you call

gluBuild2DMipmaps(GL_TEXTURE_2D,4,szm,szm,GL_ALPHA,GL_UNSIGNED_BYTE,data);

you are passing some array "data" instead of "map"

maybe i just don't understand something?

This topic is closed to new replies.

Advertisement