How to use Multitexture like this?

Started by
12 comments, last by long_xz 17 years ago
I want to use multitexture like this Cv = Cf*C0+Cf+C1 Cf: the fragment color C0: texture0 C1: texture1 Cv is the final color how should I configure my texutre environment
Advertisement
Hello,

Basicly, you could write your equation like this: Cv = Cf(C0+C1)
That way, you can do the following:

// Setup the color on TEXUNIT0
glActiveTextureARB(GL_TEXTURE0_ARB);
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_REPLACE); // try GL_MODULATE also I'm not sure
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_PRIMARY_COLOR_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
//Setup texture unit 1
...
//Setup texture unit 2
...
// Activate TEXUNIT1 again
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_ADD);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE1);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_TEXTURE2);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);

Hope I've been able to help,
Jeroen

[Edited by - godmodder on April 7, 2007 6:27:41 AM]
sorry,I have done as you said
but the result is
Cv = Cf*C0+C1(modulate)
Cv = C0+C1(replace)
Sorry, I've made a mistake. Here's the correct sequence, however I haven't verified the code because I'm currently not on my home pc:

// Setup the color using glColor
...
// Setup texture unit 2
...
// Setup combining for texture unit 2 --> (Cf*C0)
glActiveTextureARB(GL_TEXTURE2_ARB);
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_PRIMARY_COLOR_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_TEXTURE2); // or GL_TEXTURE?
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);

// Setup texture unit 1
...
// Activate TEXUNIT1 again --> (Cf+C1)
glActiveTextureARB(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_ADD);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_PRIMARY_COLOR_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_TEXTURE1); // or GL_TEXTURE?
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);

// Activate TEXUNIT0
glActiveTextureARB(GL_TEXTURE0_ARB); --> (Cf*C0)+(Cf+C1)
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_ADD);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE0);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_TEXTURE1);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);

I think you have to disable TEXUNIT 1/2 after the combining to prevent them from being modulated with the final result in TEXUNIT0.

Jeroen
thank you for your help,but i still have some question
Is that means that if i want to map two textures,then i need to use 3 texture units?
can you use just 2 units to achieve this?Because the number of the textures used to map is dynamic.
Hello,

unless there's some clever trick I don't yet know about, I don't think you can do it without a third texunit because you have to store 2 intermediate results and use them in another texture unit. If you are working with lightmaps or greyscale images, you could save some texture units by storing grayscale images in the alpha channels of the other textures, but for the rest I don't know. There's some further reading in the GL_ARB_tex_env_combine extension specification about the behavior if some texture units aren't intialized correctly. Maybe you can find the answer to your last question there...

Good luck,
Jeroen
Thank you again.
But i have another question.
Given i use GL_TEXTURE0 and GL_TEXTURE1 to store 2 intermediate results.
and combine them in GL_TEXTURE2,
how shoule i set the texture coordinate in GL_TEXTURE2,while using glMultiTexCoord2f
Hello,

to be frank with you, I hadn't really thought of that. I'm afraid I don't know that myself either. What happens when you don't supply them?

Jeroen
GL_TEXTURE2 will not work.
Hello,

I understand your problem: you want to blend three things: a color value that hasn't got any coordinates associated with it, a texture "1" that has it's own coordinates and a texture "2" that has it's own coordinates. You store (Cf*C0) in TEXUNIT2 and (Cf + C1) in TEXUNIT1. Then you want to add the two together and use the result in TEXUNIT0. I honestly don't know if this is even possible in this way, and which coordinates you should use then. But I find this problem quite interesting, so I will devote some research to it. I'll get back to you when I find a solution.

EDIT: maybe you should just use a glDisableClientState(GL_TEXTURE0_ARB); because you supply the other texcoords when doing the combining already.

Jeroen

[Edited by - godmodder on April 9, 2007 7:12:38 AM]

This topic is closed to new replies.

Advertisement