Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Post What's on Your Mind

Spoo

Sponsor

He is proof our diversification campaign is going strong. I think there is room for improvement -- I maintain that we can increase our homosexuality output (input too :wink: ) by at least 69% by rebranding.

HBG: Here be Gays.org is the future.
 

Spoo

Sponsor

2lPrq29.jpg


Gotta be specific.
 
I've learnt Java and I've been working on a game using LWJGL and I'm up to using shaders. I've been following a tutorial for the most part, especially for setting up the OpenGL workspace. One thing the tutorial does not cover is how to get alpha blending to work.

I have a fragment shader that receives a Texture object of an RGBA image. What do I do to get transparency working?
Code:
<div id="{CB}" style="font-family: monospace;"><ol>#version 330 core

// output

layout (location = 0) out vec4 color;

 

// receiving texture coordinate data from vertex shader

in DATA

{

    vec2 tc;

} fs_in;

 

// texture object

uniform sampler2D tex;

 

void main()

{

    color = texture(tex,fs_in.tc);

}
 
ZenVirZan":3fk3x331 said:
I've learnt Java and I've been working on a game using LWJGL and I'm up to using shaders. I've been following a tutorial for the most part, especially for setting up the OpenGL workspace. One thing the tutorial does not cover is how to get alpha blending to work.

I have a fragment shader that receives a Texture object of an RGBA image. What do I do to get transparency working?
Code:
<div id="{CB}" style="font-family: monospace;"><ol>#version 330 core

// output

layout (location = 0) out vec4 color;

 

// receiving texture coordinate data from vertex shader

in DATA

{

    vec2 tc;

} fs_in;

 

// texture object

uniform sampler2D tex;

 

void main()

{

    color = texture(tex,fs_in.tc);

}
You need to enable blending with glEnable( GL_BLEND ); on the driver side (In your Java code).

Also set up the alpha blending operation and the blending equation;
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glBlendEquation( GL_FUNC_ADD );
This sets it so the resulting pixel is ( fragment.rgb * fragment.a ) + ( buffer.rgb * ( 1.0 - fragment.a ) ), which is basically a weighted average (a mix operation of the fragment's colour and the buffer's colour using the fragment's alpha). This only works for RGB buffers, not RGBA if you want to know how to do that ask when you start using RGBA FramebufferObjects.

Remember to disable blending with glDisable( GL_BLEND ); after rendering your materials with alpha as alpha blending is an expensive operation; it should only be used where it absolutely needs to be.

Also keep in mind you need to draw your objects with alpha from far to near with depth writing disabled with glDepthMask( GL_FALSE ); otherwise you'll have draw order problems and alpha surfaces that clip will end up having depth read/write issues.



If you want to use alpha surfaces but not want to do blending, you can set it up to discard fragments that have an alpha below 0.5 as such (You want to do this for textures that aren't semi transparent but have transparent holes in them);
C:
<div class="c" id="{CB}" style="font-family: monospace;"><ol>glDisable( GL_BLEND ); // Do not use blending

glEnable( GL_ALPHA_TEST ); // Enable alpha discard

glAlphaFunc( GL_GEQUAL, <span style="color: #cc66cc;">0.5f ); // Only draw fragments with an alpha greater than or equal to 0.5
 
Today I did the first part of my final USMLE, the Step 3. It was a 7 hour marathon. Hopefully, when I'm done with it, I'll get into a residency (this following year). Tomorrow is the second part. It's a 9 hour marathon. Wish me luck, guys. I'm so close to the finishing line, but I'm also very tired, physically and mentally. :biggrin:
 
Good luck Juan :D

@Xi,
I should have specified, I'm only using 2D planes with textures.
I was discarding fragments within the fragment shader if they were transparent initially, but yeah, that's not what I was looking for.
I did what you said without the GL_FUNC_ADD as I can't find it for the life of me, and it seems to work fine. Thanks a bunch.
 
ZenVirZan":26ajwbvr said:
I was discarding fragments within the fragment shader if they were transparent initially, but yeah, that's not what I was looking for.
Don't do this if you are using a depth buffer; modern GPUs will do the depth test first before running a fragment shader to avoid shading a fragment only to have it fail the depth test. The discard keyword disables this early Z optimisation.
ZenVirZan":26ajwbvr said:
I did what you said without the GL_FUNC_ADD as I can't find it for the life of me, and it seems to work fine. Thanks a bunch.
That is because GL_FUNC_ADD is the default setting, it will already be set to that. You want to find this value though as explicitly setting as you need it is very important for state tracking; OpenGame.exe as an example pre-sets a number of states at initialisation so it's very difficult to track state changes as a result and makes adding new visual features more difficult.
 
I was doing fine on my second day of testing until the last part of the test, which I failed miserably. Now I have to hope that the rest of the test will be enough for me to pass, but I'm scared I didn't. :sad:
 

Spoo

Sponsor

My new motherboard and CPU should arrive tomorrow, and the SSD should come tomorrow or Tuesday. I've just got the two GTX 980ti cards to go and I'll have the essential parts of the new rig done. Still haven't decided on a CPU cooler or case yet.
 
Spoo":84s7teiu said:
My new motherboard and CPU should arrive tomorrow, and the SSD should come tomorrow or Tuesday. I've just got the two GTX 980ti cards to go and I'll have the essential parts of the new rig done. Still haven't decided on a CPU cooler or case yet.
What CPU did you get?
You should benchmark with 3Dmark when it's complete and post the results.

As for CPU coolers; Will you be going for an all in one liquid?

The case I have is a phanteks enthoo pro, phanteks are relatively new in the case game but they have a surprisingly awesome and affordable offering that puts older case manufacturers to shame.
 

Spoo

Sponsor

I went with an i7-4790k. Less economical than an i5 right now, but I think I'll get more time out of it. As for the cooler, I'm probably going with a liquid cooler since I'm gonna overclock later on to 4.4ghz or so. Maybe a Corsair H50.

I looked at the Enthoo Pro the other day but I don't remember details of it. If I feel like spending the money or I find a deal I might grab a Corsair Obsidian 500D.

Also yeah I'll post benchmarks once I get my first 980 and again when I get the second.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top