PDA

View Full Version : Multitexture - Texture with alpha plus environment map



Andrew Jameson
03-15-2006, 11:36 PM
I need to apply a texture (actually a letter) onto the face of an object and then apply an spherical environment map to the object. I have arranged the alpha to be zero on the first texture over the area covered by the letter and need this area to be excluded from the environment map.

I can achieve this in a multipass render but I can't afford the performance hit. I have eventually found the following code works but it uses GL_COMBINE4_NV and I believe that this is specific and only supported by NVidia cards.

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE4_NV);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_ADD);

glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_EXT, GL_PREVIOUS_EXT);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_EXT, GL_SRC_COLOR);

glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_EXT, GL_ZERO);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_EXT, GL_ONE_MINUS_SRC_COLOR);

glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_EXT, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_EXT, GL_SRC_COLOR);

glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE3_RGB_EXT, GL_TEXTURE0_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND3_RGB_EXT, GL_SRC_ALPHA);

I'm working to a very tight deadline and wondered if what I want can be achieved and if so how ?

Whilst not exactly new to OpenGL, my experience with multitexturing is new and I would appreciate any links that might help me in my understanding of this subject.

Many thanks,

Andrew

sqrt[-1]
03-16-2006, 01:54 AM
This is core OpenGL and should be kinda what you are looking for:

glActiveTextureARB(GL_TEXTURE0)
glEnable(GL_TEXTURE_2D)
//Bind diffuse with alpha here
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_CO MBINE)
glTexEnvi(GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_MODULAT E)
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE0_RGB,GL_PREVIOU S)
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND0_RGB,GL_SRC_CO LOR)
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE1_RGB,GL_TEXTURE )
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND1_RGB,GL_SRC_CO LOR)
glTexEnvi(GL_TEXTURE_ENV,GL_COMBINE_ALPHA,GL_MODUL ATE)
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE0_ALPHA,GL_PREVI OUS)
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND0_ALPHA,GL_SRC_ ALPHA)
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE1_ALPHA,GL_TEXTU RE)
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND1_ALPHA,GL_SRC_ ALPHA)

glActiveTextureARB(GL_TEXTURE1)
glEnable(GL_TEXTURE_2D)
//Bind sphere map here
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_CO MBINE)
glTexEnvi(GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_INTERPO LATE)
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE0_RGB,GL_TEXTURE )
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND0_RGB,GL_SRC_CO LOR)
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE1_RGB,GL_PREVIOU S)
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND1_RGB,GL_SRC_CO LOR)
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE2_RGB,GL_PREVIOU S)
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND2_RGB,GL_SRC_AL PHA)
glTexEnvi(GL_TEXTURE_ENV,GL_COMBINE_ALPHA,GL_REPLA CE)
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE0_ALPHA,GL_PRIMA RY_COLOR)
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND0_ALPHA,GL_SRC_ ALPHA)

Don't forget to reset you OpenGL texture states when done!

(eg:
//Reset texture stage 1
glActiveTextureARB(GL_TEXTURE1)
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MO DULATE);
glDisable(GL_TEXTURE_2D)
...

//Reset texture stage 0
glActiveTextureARB(GL_TEXTURE0)
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MO DULATE);
...
)

Andrew Jameson
03-16-2006, 02:51 AM
Wow ! ... that works very well but for the fact that the original diffuse texture color is lost by the predominance of that of the spherical map.

(can you recommend any reading on the subject ... at the moment it's more akin to cracking a combination lock rather than programming !)

Andrew

sqrt[-1]
03-16-2006, 11:15 PM
I am not sure what you mean by "original diffuse texture color is lost". Is there some part of the origional scene you want this blended over? (in which case just use some alpha blending mode)

The red book(OpenGL Programming Guide) (4th edition or higher) has some nice descriptions on how texture combiners work.
http://www.amazon.com/gp/product/0321335...5Fencoding=UTF8 (http://www.amazon.com/gp/product/0321335732/sr=8-1/qid=1142586687/ref=pd_bbs_1/104-6012383-5143102?%5Fencoding=UTF8)

I might do some descriptions in the new OpenGL wiki if you want to be my "beginner" test case and do some proof-reading.