-1

I'm trying to render a model in OpenGL. I'm on Day 4 of C++ and OpenGL (Yes, I have learned this quickly) and I'm at a bit of a stop with textures.

I'm having a bit of trouble making my texture alpha work. In this image, I have this character from Spiral Knights. As you can see on the top of his head, there's those white portions.Spiral Knights character with no transparency

I've got Blending enabled and my blend function set to glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

What I'm assuming here, and this is why I ask this question, is that the texture transparency is working, but the triangles behind the texture are still showing.

How do I make those triangles invisible but still show my texture?

Thanks.

  • 1
    Threre are no "triangles behind textures". GL doesn;t draw textures, it draws primitives. It is totally unclear what exactly goes on (what akoha values your texture has, how you combine the texture to the final fragment color, and so on). YOu sould also be aware that blending will require to draw the primitives in the correct order. In this particular case, you might even be able to get around this by not using blending, but just by discarding based on alpha. – derhass Feb 12 '17 at 19:18
  • You say _"just by discarding based on alpha."_ - I did some research on discarding and such but from what I've read it can be kind of slow. Is there an alternate method? I did some googling for alternate methods and enabled alpha testing, then set the alpha function to `glAlphaFunc(GL_GREATER, 0.0);` but that doesn't seem to be working. – Eti the Spirit Feb 12 '17 at 19:45
  • the alpha test is deprecated. modern approach is discard in the shader. what "slow" really means in this context is hard to tell in advance. you might fall back to late depth test, and you get a certain degree of non-uniform control flow. However, you gain the order independency in return. – derhass Feb 12 '17 at 19:56

1 Answers1

0

There are two important things to be done when using blending:

  1. You must sort primitives back to front and render in that order (order independent transparency in depth buffer based renderers is still an ongoing research topic).

  2. When using textures to control the alpha channel you must either write a shader that somehow gets the texture's alpha values passed down to the resulting fragment color, or – if you're using the fixed function pipeline – you have to use GL_MODULATE texture env mode, or GL_DECAL with the primitive color alpha value set to 0, or use GL_REPLACE.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • As for the second part on task #2, I know that `glTexEnvi` takes 3 arguments. After reading [this page](https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTexEnv.xml) I went ahead and tried doing what you said based on the info from that page. I set my internal format to GL_RGBA (within the `glTexImage2D` func) and I'm using `glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);`, but that doesn't quite seem to be working. Am I missing something? – Eti the Spirit Feb 12 '17 at 19:56
  • @XantheDragon: Hard to tell without seeing your code. – datenwolf Feb 12 '17 at 23:09