Render transparent textures

I have one texture that has some parts that are transparent transparent, which I want to apply over an object whose faces are an opaque material (or color if simpler), but the final object becomes transparent. I want the final object to be completely opaque.

Here is my code:

First I installed stuff:

   glDisable(GL_COLOR_MATERIAL);
   glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT);
   glColor4f(0.00, 0.00, 0.00, 1.00);
   glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
   glColor4f(0.80, 0.80, 0.80, 1.00);
   glColorMaterial(GL_FRONT_AND_BACK, GL_SPECULAR);
   glColor4f(0.01, 0.01, 0.01, 1.00);
   glEnable(GL_COLOR_MATERIAL);

      

Then I install VBOs

   glBindTexture(GL_TEXTURE_2D, object->texture);
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

   glBindBuffer(GL_ARRAY_BUFFER, object->object);
   glVertexPointer(3, GL_FLOAT, sizeof(Vertex), ver_offset);
   glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), tex_offset);
   glNormalPointer(GL_FLOAT, sizeof(Vertex), nor_offset);

      

Finally, I draw the object

   glEnable(GL_BLEND);
   glDisable(GL_DEPTH_TEST);

   glDisable(GL_TEXTURE_2D);
   glBlendFunc(GL_ONE, GL_ZERO);
   glDrawArrays(GL_TRIANGLES, 0, object->num_faces);

   glEnable(GL_TEXTURE_2D);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   glDrawArrays(GL_TRIANGLES, 0, object->num_faces);

   glDisableClientState(GL_VERTEX_ARRAY);
   glDisableClientState(GL_TEXTURE_COORD_ARRAY);

   glDisable(GL_BLEND); 
   glEnable(GL_DEPTH_TEST);

      

I've tried passing different arguments to glBlendFunc () without preemption. I downloaded the source here: http://dpaste.com/83559/

UPDATE I get this , but I want this (or no texture this ).

The second and third images are created with glm . I have looked into the sources, but since my knowledge of OpenGL is limited, I don't understand much.

+2


source to share


5 answers


If you're trying to apply two textures to your object, you really want to set two textures and use multitexturing to achieve that look. Your method is drawing the geometry twice, which is a huge performance loss.



Multitexturing will simply be a sample of two texture units, and only draw the geometry once. You can do it with shaders (how it really should be done) or you can still use a fixed function pipeline (see http://bluevoid.com/opengl/sig00/advanced00/notes/node62.html )

+3


source


AFAIK the blend function accepts fragment colors (as opposed to texture colors). Therefore, if you paint the object a second time while blending, the triangles will become transparent.



What you want to accomplish can be accomplished with multitexturing .

+1


source


This is just a wild guess as you couldn't provide any screenshots of what the real issue is, but why are you turning off depth checking? Surely you want to enable depth checking on the first pass with the standard GL_LESS and then do the second pass with GL_EQUAL?

Edit:

t

glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST);  // ie do not disable
glDepthFunc( GL_LESS );   // only pass polys have a z value less than ones already in the z-buffer (ie are in front of any previous pixels)

glDisable(GL_TEXTURE_2D);
glBlendFunc(GL_ONE, GL_ZERO);
glDrawArrays(GL_TRIANGLES, 0, object->num_faces);

// for the second pass we only want to blend pixels where they occupy the same position 
// as in the previous pass.  Therefore set to equal and only pixels that match the
// previous pass will be blended together.
glDepthFunc( GL_EQUAL );

glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDrawArrays(GL_TRIANGLES, 0, object->num_faces);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

glDisable(GL_BLEND); 

      

0


source


Try turning off blending and drawing one pass with the texture function set to GL_DECAL

instead GL_MODULATE

. This will blend between the vertex color and the texture color based on the texture alpha channel, but leave the alpha channel equal to the vertex color.

Note that this will ignore any lighting applied to the vertex color wherever the texture is opaque, but this is similar to the intended effect based on your description.

0


source


It will be much easier with pixel shaders. otherwise I think you need multi-pass rendering or more than one texture. You can find more details here: http://www.opengl.org/resources/faq/technical/transparency.htm

-1


source







All Articles