0
votes

I have the below fragment shader which currently returns the color of a pixel from texture:

varying vec2      v_texCoord;
uniform sampler2D s_texture;
uniform vec4      vColor4;

void main()
{
  gl_FragColor = texture2D( s_texture, v_texCoord );
  //gl_FragColor = vColor4;
}

How do I change the fragment shader so that it returns a fixed color when a pixel is on and transparency when a pixel is off?

The TGA file I'm using contains transparency but a green and brown image (its a basic tree image - green top, brown trunk). What I'm trying to do is render the shape of the tree in one particular color (like a silhouette) instead of green, brown and transparency. If I change gl_FragColor to a set color, then the image paints as a solid rectangle -not what I want.

Is it just a matter of assigning the fixed colour and using the alpha (.a?) value returned by texture2D perhaps?

The line below gives me a white silhouette but the alpha channel is ignored so I get a white tree in a black rectangle:

  gl_FragColor = texture2D( s_texture, v_texCoord ).aaaa;

UPDATE: I tried the following but it is still rendering a rectangle. It appears that the transparency isn't being loaded into my texture. It is transparent for sure with GIMP but transparency is rendering as white for me for some reason.

  vec4 tmpColor = texture2D( s_texture, v_texCoord );

  tmpColor.r = vColor4.r;
  tmpColor.g = vColor4.g;
  tmpColor.b = vColor4.b;

//  gl_FragColor = texture2D( s_texture, v_texCoord );
  //gl_FragColor = vColor4;
  gl_FragColor = tmpColor;

My LoadTexture() function uses the following parameters:

  glActiveTexture( GL_TEXTURE0 );
  glBindTexture( GL_TEXTURE_2D, TexHandle ); // Set our Tex handle as current

  // Specify filtering and edge actions
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );

  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );

  //glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE );

  glTexImage2D( GL_TEXTURE_2D,0,GL_RGBA, Img.GetWidth(),Img.GetHeight(),0,
                 GL_RGBA,GL_UNSIGNED_BYTE,Img.GetImg() );

Bits per pixel (BPP) is definitely 32 (R,G,B,A).

1
Do you have blending enabled (glEnable(GL_BLEND))? What is your blending function (glBlendEquation)?Colonel Thirty Two

1 Answers

1
votes

Sounds like you need to enable blending and a blending function (possibly specificyand equation also using glBlendEquation).

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

A great tester to see different blending equation results is here