Archived

This topic is now archived and is closed to further replies.

MV

Multitexturing

Recommended Posts

Hi, I would like to know if it is possible, using ARB extension, to blend 2 textures "following" a mask being a third texture. For example using texture C (greyscale), Opengl should blend texture A and B, using texture A for black values of C, texture B for white values and 50% of A and B for grey values. And if it is possible, how to do it ?

Share this post


Link to post
Share on other sites
Yes, you can do it using ARB_texture_env_combine and ARB_texture_env_crossbar or NV_texture_env_combine4 extension:

With ARB_texture_env_combine you have the following operation GL_INTERPOLATE_ARB available. You can use it the following way:


glActiveTextureARB(GL_TEXTURE0_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, your_texture_A);

glActiveTextureARB(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, your_texture_B);

glActiveTextureARB(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, your_greyscale_texture);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_TEXTURE_ENV_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COMBINE, GL_INTERPOLATE_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE0_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_TEXTURE1_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE2_ARB);


The Interpolation here is done with:
result_color = color_of_texture_A * greyscale + color_of_texture_B * (1 - greyscale)

For more information see
http://oss.sgi.com/projects/ogl-sample/registry/ARB/texture_env_combine.txt

Hope this will help!

--------------------------------------------------------

"If it looks good, it is good computer graphics"
"If it looks like computer graphics, it is bad computer graphics"

Corrail
corrail@gmx.at
ICQ#59184081

Share this post


Link to post
Share on other sites
That is certainly a bad idea, because that''s exactly what we''re doing for our terrain engine

In fact, it''s very good as long as you have to blend between two textures (grass<->rock), but when it comes to blend three textures (grass<->rock<->sand<->grass) this isn''t enough.

jm2c

Share this post


Link to post
Share on other sites
Heis ppl...

First of All: Corrail, you have a little bug in your code:

glActiveTextureARB(GL_TEXTURE0_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, your_texture_A);
glActiveTextureARB(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, your_texture_B);
glActiveTextureARB(GL_TEXTURE1_ARB); => should be GL_TEXTURE2_ARB
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, your_greyscale_texture);

But thats ok....

Ok, this is using 3 textures and 3 slots! But I thinck that is possible using the same efect when the slot2 texture is 32 bits! How to do alpha blend between textures?

Share this post


Link to post
Share on other sites
My last question, but I think Vincoof has answered : how many textures can be used for multitexturing ?

Should 3 textures be used with rgb chanels of a 4th texture indicating the percentage of the blending ?
r -> % of 1st texture
g -> % of 2nd texture
b -> % of 3rd texture

Share this post


Link to post
Share on other sites
@vincoof :-P

@Filami
*damn* My codes never will be perfect... :-)

I think you can realize this with only 2 textures using GL_OPERAND_RGB_ARB:

texture_A: 32 Bit RGBA texture where the RGB values represent the 24 Bit texture and the 8 Bit Alpha represents your grayscale texture
texture_B: normal 24 Bit RGB texture

// Texture Stage 0
glActiveTextureARB(GL_TEXTURE0_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture_A);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_TEXTURE_ENV_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COMBINE, GL_REPLACE_ARB);

// copying the alpha value in the first texture environment
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE0_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERANT0_RGB_ARB, GL_SRC_ALPHA);

// Texture Stage 1
glActiveTextureARB(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture_B);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_TEXTURE_ENV_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COMBINE, GL_INTERPOLATE_ARB);

// Using Alpha value from previous texture environment
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE0_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_TEXTURE1_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_PREVIOUS_ARB);


Hope this will work!

--------------------------------------------------------

"If it looks good, it is good computer graphics"
"If it looks like computer graphics, it is bad computer graphics"

Corrail
corrail@gmx.at
ICQ#59184081

[edited by - Corrail on October 23, 2003 10:39:29 AM]

[edited by - Corrail on October 23, 2003 10:40:38 AM]

Share this post


Link to post
Share on other sites
> My last question, but I think Vincoof has answered :
> how many textures can be used for multitexturing ?
That depends on your hardware. You can count at least on two texture units for almost any kind of current hardware.
Better hardware like GeForce3/4 can support 4 texture units, Radeon8500 can support 6 texture units.

> Should 3 textures be used with rgb chanels of a 4th texture
> indicating the percentage of the blending ?
> r -> % of 1st texture
> g -> % of 2nd texture
> b -> % of 3rd texture
No you can't do it with texture combiners. At best you could use the alpha channel of each image like Corrail described nicely once again.

[edit: what Corrail proposed can be done in a single texture stage in fact]

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_TEXTURE_ENV_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COMBINE, GL_INTERPOLATE_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE0_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_TEXTURE1_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE0_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);


[edited by - vincoof on October 25, 2003 7:59:39 AM]

Share this post


Link to post
Share on other sites
Yeah, this might be faster than my version! :-)
But you still need 2 Texture Units...

--------------------------------------------------------

"If it looks good, it is good computer graphics"
"If it looks like computer graphics, it is bad computer graphics"

Corrail
corrail@gmx.at
ICQ#59184081

Share this post


Link to post
Share on other sites
quote:
Original post by Corrail
Yeah, this might be faster than my version! :-)
But you still need 2 Texture Units...

Yes obviously
But with this you have a free texture stage that can perform post-texturing color operations, which happen to be handy in many effects such as brightening/darkening, color masks, inversion, etc

Share this post


Link to post
Share on other sites