0
\$\begingroup\$

I finally got around to applying textures to my game, and I noticed that the areas that are transparent on the .png of the texture are not transparent on the actual model.It is illustrated in the image below. enter image description here

I want the areas of the model that are supposed to be transparent to be transparent. The white areas are the areas that should be transparent. the white areas are transparent on the actual .png. Is there any way to do this using glfw? Is there some sort of transparency function GLFW has? I thought changing RGB to RGBA on the texture generator would fix it but it did not. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); The data for the actual model is below (its formatted in json but still shows the vertices). The first 3 entries on each row are the coordinates, then 3 entries for the color, then the texture mapping.

{"vertices":[
0.00,0.00,0.0,0.0,0.0,0.0,0.0,0.0,
500.00,0.00,0.0,0.0,0.0,0.0,1.0,0.0,
500.00,500.00,0.0,0.0,0.0,0.0,1.0,1.0,
0.00,500.00,0.0,0.0,0.0,0.0,0.0,1.0

],
"indices":[
0,1,2,
0,3,2
],

"texturePath": "textures/planet1.png"}

Do I have to map the coordinates of the texture to a circular object rather than a square one like it is formatted now. I am hoping I don't, because that would make my life so much simpler.

\$\endgroup\$
1
  • \$\begingroup\$ For starters, you're using GL_RGB as the internal format; as I recall, you need to change that to GL_RGBA. There's multiple things that effect the end result of transparency in OpenGL though such as enabling blend & setting a blend function, so that one correction might not be enough. If you fix the internal format & it still doesn't work, edit your post to include an MCVE so we can see the rest of the code. \$\endgroup\$ – Pikalek Jan 23 at 5:25
0
\$\begingroup\$

I figured it out using this lovely tutorial.

I changed my fragment shader from this:

out vec4 FragColor;

in vec3 ourColor;
in vec2 TexCoord;

uniform sampler2D ourTexture;

void main()
{
    FragColor = texture(ourTexture, TexCoord);
}

to this:

out vec4 FragColor;

in vec3 ourColor;
in vec2 TexCoord;

uniform sampler2D ourTexture;

void main()
{
    vec4 texColor = texture(ourTexture, TexCoord);
    if(texColor.a < 0.1)
        discard;
    FragColor = texColor;
}

I never told the shader to ignore the transparent areas of the model in the first place.

\$\endgroup\$
1
  • \$\begingroup\$ This is a technique called Alpha Testing. It has a downside that it can't render partial translucency, so if you have a gradual fade-off at the edge of your sprite (like a slightly fuzzy anti-aliased line), what you'll see is a hard cut-off, and sometimes an unsightly fringe that doesn't match your background. If you want to avoid this, you can use Alpha Blending instead. That requires setting the blend mode before drawing, so the graphics card knows not to simply replace the background pixel colour with your shader's output, but to combine them using some formula that you configure. \$\endgroup\$ – DMGregory Jan 23 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.