Variable Texture Opacity

Started by
7 comments, last by litewarri 19 years, 11 months ago
I have a function to texturize and draw a model. Let''s call it: void draw_model(modeldata passdata) What I want to do is make it like this: void draw_model(modeldata passdata, float opacity) Where opacity is a ranged value from 0.0 to 1.0. The desired effect would be that I could pass 0.5 as opacity and the model would be drawn at 50% over the existing buffer, 0.21 would be drawn at 21% over the existing buffer, etc. I''ve done a couple hours of online research, and the best I could find were instructions for doing variable opacity with non-textured objects, and instructions for doing texture opacity with very limited values (0.0, 0.5, and 1.0 only). Anyone have the lines of code I need?
Advertisement
Alpha blending, and set the alpha channel in the texture to whatever value you want.
quote:Original post by Raduprv
Alpha blending, and set the alpha channel in the texture to whatever value you want.


Okay, deal.

I have:

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);

.. before I start drawing. What function do I use to change the alpha value in the texture before that?
Well, changing the alpha is not very fast, but you can do it by keeping a copy of the texture data (not the OpenGL texture itself) in the memory, and using glTexSubImage2D() to update the texture.
I think there might be better/faster ways to do that, but that''s all I can come up with now.
Well, thanks for your help. Here''s the code I have so far:

if(opacity < 100)
{
if(opacity > 100) opacity = 100;
if(opacity < 0) opacity = 0;
opacity /= 100;

glDisable(GL_DEPTH_TEST);
glColor4f(1.0f,1.0f,1.0f, opacity);
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
glEnable(GL_BLEND);
}

At this point, WHENEVER there''s opacity less than 100 (1.0), it appears about 50% opaque (no matter what value, 23, 45, 75, etc. is in ''opacity''). Perhaps it has something to do with the fact that I''m using JPEG textures?
Well, it depends on how you load your texture. Most of the libraries I''ve seen won''t assign any particular alpha channel to textures made from fiel formats that don''t have alpha.
Try this instead.

glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

You should get your opacity working correctly. You dont need a texture with alpha values if you want your whole model to be uniformly blended.
quote:Original post by GamerSg
Try this instead.

glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

You should get your opacity working correctly. You dont need a texture with alpha values if you want your whole model to be uniformly blended.


glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); is great, but how do I implement the ''opacity'' float in order to make it variable?
I figured it out. Thanks everyone.


float t_ambi[4];
float t_diff[4];
float t_spec[4];

for(int cc(0); cc < 4; cc++)
{
t_ambi[cc] = materials[x].ambient[cc];
t_diff[cc] = materials[x].diffuse[cc];
t_spec[cc] = materials[x].specular[cc];
}

t_ambi[3] = opacity;
t_diff[3] = opacity;
t_spec[3] = opacity;

glMaterialfv( GL_FRONT, GL_AMBIENT, t_ambi);
glMaterialfv( GL_FRONT, GL_DIFFUSE, t_diff);
glMaterialfv( GL_FRONT, GL_SPECULAR, t_spec );
glMaterialfv( GL_FRONT, GL_EMISSION, materials[x].emissive );
glMaterialf( GL_FRONT, GL_SHININESS, materials[x].shininess );

This topic is closed to new replies.

Advertisement