Transparent texture question
HI!
I've a problem with drawing transparent faces with textures. My model contains RGBA color values in a vertex array and also textures. When I want to draw it with textures, I use white color to get correct results, but I still want to apply the alpha value of the vertices. Is there any special way to use only the alpha channel of my color array for the fragment color? Or if it isn't possible, how can I tell my textures to have the alpha value, so I could modulate with them the fragment's alphas?
Thx
Quote:Original post by BecoYou don't have to do anything special at all. With the texture environment set to GL_MODULATE, the fragment's color is multiplied by the corresponding texel's color. So the fragment's alpha component will be multiplied by the texel's alpha component. Or maybe I didn't understand what you were asking.
HI!
I've a problem with drawing transparent faces with textures. My model contains RGBA color values in a vertex array and also textures. When I want to draw it with textures, I use white color to get correct results, but I still want to apply the alpha value of the vertices. Is there any special way to use only the alpha channel of my color array for the fragment color? Or if it isn't possible, how can I tell my textures to have the alpha value, so I could modulate with them the fragment's alphas?
Thx
Note: RGB textures act the same as RGBA textures where each texel has 1.0 alpha.
Well, maybe I didn't explain my problem correctly. My application has several display modes (it's not a game :)) like color shaded or textured. I have a color array containing RGBA components for each vertices and I use it to draw the simple coloured model without textures. In textured mode I'm using GL_MODULATE function, but the problem is exactly that, it will moddulate not only the alpha values but also the color components. The solution could be if I used white instead of my color array, but then I don't have the alpha values and the faces won't be transparent. I've also tried GL_DECAL function, it works but it killed all my lighting effects and the results were miserable.
I can see the solution in only two ways: if I could use only one channel of my arrays (the alpha) and the color components could be masked out somehow with a constant white. The other is that I don't use color array at all and the texture has an alpha value.
Am I correct?
I can see the solution in only two ways: if I could use only one channel of my arrays (the alpha) and the color components could be masked out somehow with a constant white. The other is that I don't use color array at all and the texture has an alpha value.
Am I correct?
ATM I'm not sure about the GL_MODULATE stuff. I'm currently writing a sprite extension to our engine, using blended textures for that purpose. The texels are RGBA. However, the vertex color is also set-up as an RGBA. If vertex color is white w/ alpha = 1 then the texture has the full alpha control. If vertex color's alpha is driven down to 0 (letting RGB values at 1) then the texture becomes transparent. Hence the alpha of texture and (interpolated) vertex color are multiplied before used for blending.
The code snippet that does our sprite rendering is this:
where background.rgba is the said vertex color (here used as constant for the entire quad).
Perhaps this is something you're looking for?
The code snippet that does our sprite rendering is this:
const bool withBlending = (*texture->image())[Image::Channel::ALPHA];if(withBlending) { ::glEnable(GL_BLEND); ::glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);}// rendering::glColor4fv(background.rgba);::glBegin(GL_QUADS);::glTexCoord2f(u0,v0);::glVertex3fv(quad[0]);::glTexCoord2f(u0,v1);::glVertex3fv(quad[1]);::glTexCoord2f(u1,v1);::glVertex3fv(quad[2]);::glTexCoord2f(u1,v0);::glVertex3fv(quad[3]);::glEnd();// cleaning upif(withBlending) { ::glDisable(GL_BLEND);}
where background.rgba is the said vertex color (here used as constant for the entire quad).
Perhaps this is something you're looking for?
Maybe I can't understand your method exactly, but it seems to me that the vertex color you set is white (background.rgba). Blending can work only in that case without color distorsion.
Quote:Original post by BecoHmm, how are you doing your lighting? With standard OpenGL per-vertex lighting the lighting result is passed through the primary color, so you need to modulate the texture by the primary color anyway to keep your lighting effects. Also, unless you are using color material, the vertex colors are ignored and have no effect on the lighting output so I'm not sure how using white would work. Again I think I'm confused on exactly what your problem is because the texture needs to be modulated by the primary color to keep the lighting results (unless you're doing something other than standard OpenGL lighting).
Well, maybe I didn't explain my problem correctly. My application has several display modes (it's not a game :)) like color shaded or textured. I have a color array containing RGBA components for each vertices and I use it to draw the simple coloured model without textures. In textured mode I'm using GL_MODULATE function, but the problem is exactly that, it will moddulate not only the alpha values but also the color components. The solution could be if I used white instead of my color array, but then I don't have the alpha values and the faces won't be transparent. I've also tried GL_DECAL function, it works but it killed all my lighting effects and the results were miserable.
I can see the solution in only two ways: if I could use only one channel of my arrays (the alpha) and the color components could be masked out somehow with a constant white. The other is that I don't use color array at all and the texture has an alpha value.
Am I correct?
Anyway, your first solution (which will also destroy OpenGL lighting) is possible using texture combiners (more info on texture combiners in the OpenGL Specs or the GL_ARB_texture_env_combine extension specs). You can set the combine mode to GL_REPLACE and set the source RGB to GL_TEXTURE and the source ALPHA to GL_PREVIOUS. Here's an untested source snippet...
//Set combine texture env modeglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);//Set the RGB and alpha combine modes to replaceglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);//Get RGB from textureglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE);//Get alpha from previous texture environment//For tex unit 0 this is the primary color//You could also specify GL_PRIMARY_COLOR to use the primary color//in any texture environment stageglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PREVIOUS);
Cv = CsAv = Ap
where Cv and Av are the color and alpha components output from the texture stage, Cs is the RGB source color from the texture, and Ap is the alpha component of the incoming fragment.Another option is using fragment shaders, but I'll assume you don't want to make fragment shader support a requirement.
One more option is just having two color arrays that you switch between using in the different modes. One array will have the colors you have right now and the other will be just the original alpha values with all the RGB values set to 1.0. I still don't see how that will work with lighting though (unless you're using color material).
OK, now it's getting clear for me that I must use GL_MODULATE if I want to keep lighting effects. I'm afraid I don't know exactly what color texture means but it's sure that if a face has for example a yellow color the texture gets a pale yellow tone by the modulation. I think it's time for me to get absorbed in texturing.
Thanks for all your help.
Thanks for all your help.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement