Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [solved] [lwjgl] [glsl] Blending & depth buffer problems  (Read 566 times)
0 Members and 1 Guest are viewing this topic.
Offline Kwauhn

Senior Newbie


Exp: 3 years


I like cats


« Posted 2014-05-15 05:15:01 »

SOLVED EDIT:


My suspicions about textures not being properly mapped to fragments were correct! Using shaders provided by this (http://lwjgl.org/wiki/index.php?title=GLSL_Tutorial:_Texturing) page and then discarding fragments with alpha worked!

Vertex shader:
1  
2  
3  
4  
5  
void main()
{
   gl_Position = ftransform();
   gl_TexCoord[0] = gl_MultiTexCoord0;
}


Fragment shader:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
uniform sampler2D tex;

void main()
{
   gl_FragColor = texture2D(tex, gl_TexCoord[0].st);
   if(gl_FragColor.a < 1.0)
   {
      discard;
   }
}


ORIGINAL PROBLEM:

Hello everyone!

Yesterday I came across an issue that I first thought would be an easy work-around. When OpenGL uses blending it mixes fragments already written to the colour buffer, and rewrites fragments written to the depth buffer. Unfortunately this causes really annoying artefacts where there is plain alpha in a texture, and upon research I found out a few solutions. The first thing is to use blending like I already am and simply order rendering of transparent objects from back to front, but this seems over-complex for a problem so mundane. The second option, is to use a fragment shader that simply discards the fragment all together if it contains alpha. As I have read, the second option seems to be very simple and eliminates the need to order back-to-front as well as the need to use blending.

However...

It's not working. I used the shader provided by this (http://www.opengl.org/wiki/Transparency_Sorting) page and many variants constructed from other tutorials, even some blatant copy & pastes. Still, discard doesn't seem to work at all. As I understand, discard should literally cut out holes in the quads where there's alpha on the textures, but it doesn't. I even tried to discard any fragment that has a green value greater than 0, the "discard" command does nothing. I'm really tearing my hair out over this because not only would it solve my problem with blending, it would remove alpha fragments from the depth buffer and make shadow mapping possible.

Here is the problem:


And here's what it should look like (accept with depth testing):


My questions are: is there ultimately a better approach to transparency sorting that will allow for shadow mapping in the future? Am I misinterpreting the use of the "discard" command? If the latter, how can I properly use it to achieve transparency sorting with out the obvious front-to-back procedure?

Thanks for reading, and sorry for the long post! I just don't understand what to do, and tutorials don't actually come out and explain the steps to solving this.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2014-05-15 05:24:58 »

Share your shader with us.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Kwauhn

Senior Newbie


Exp: 3 years


I like cats


« Reply #2 - Posted 2014-05-15 05:31:14 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
in vec2 coord;
out vec4 color;

uniform sampler2D tex;

void main()
{
   vec4 texel = texture(tex, coord);
   if(texel.a < 1)
   {
      discard;
   }
   color = texel;
}


This is the same one as on the OpenGL wiki, but every other variant I've tried fails just like this one.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2014-05-15 05:32:19 »

Are you sure the shader is bound?

Why don't you write to gl_FragColor ? is that deprecated already?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline SHC
« Reply #4 - Posted 2014-05-15 05:33:08 »

1  
if(texel.a < 1)

This is the problem. The alpha ranges between 0 and 1. And you are discarding all the colors if it has transparency.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2014-05-15 05:33:40 »

That's what he wants: discard all fragments, to see whether discard actually works.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2014-05-15 05:34:24 »

My guess is that the fragment shader is invalid, fails to compile/link, or is not bound.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Kwauhn

Senior Newbie


Exp: 3 years


I like cats


« Reply #7 - Posted 2014-05-15 05:36:06 »

The fragment shader works, if I change it to the following everything becomes red.

1  
2  
3  
4  
void main()
{
   gl_FragColor = vec4(1, 0, 0, 1);
}


EDIT: Using glGetShaderi(GL_COMPILE_STATUS) returns a clean compile too Sad
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2014-05-15 05:45:29 »

Well, work from something that works, and slowly work towards the end result.

1  
2  
3  
4  
5  
void main()
{
  if(0.0 < 1.0) discard;
   gl_FragColor = vec4(1, 0, 0, 1);
}


Is the scene empty now?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Kwauhn

Senior Newbie


Exp: 3 years


I like cats


« Reply #9 - Posted 2014-05-15 05:47:52 »

Yes, does it make a difference if I write the number one as 1 or 1.0?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2014-05-15 05:51:10 »

Depends on the driver, vendor and GLSL version.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Kwauhn

Senior Newbie


Exp: 3 years


I like cats


« Reply #11 - Posted 2014-05-15 05:55:24 »

Yeah, there are no errors.

If I apply textures in my Java program do fragments take on the texel's colour, or do I have to re-map textures in the shader? Because maybe it's determining colours before the textures have been applied, as if they were on a solid white plane?
Offline Kwauhn

Senior Newbie


Exp: 3 years


I like cats


« Reply #12 - Posted 2014-05-15 06:28:45 »

In case this is an issue with the shader loading it's self, here is the class I made for handling shaders:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
package core;

import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL11.*;
import java.io.BufferedReader;
import java.io.FileReader;

public class Shader
{
   private int programHandle;
   private int vertHandle;
   private int fragHandle;
   
   public Shader(String res)
   {
      programHandle = glCreateProgram();
      vertHandle = glCreateShader(GL_VERTEX_SHADER);
      fragHandle = glCreateShader(GL_FRAGMENT_SHADER);
     
      StringBuilder vertSource = new StringBuilder();
      StringBuilder fragSource = new StringBuilder();
      try
      {
         BufferedReader vertReader = new BufferedReader(new FileReader("src/res/" + res + ".vs"));
         BufferedReader fragReader = new BufferedReader(new FileReader("src/res/" + res + ".fs"));
         String line;
         while((line = vertReader.readLine()) != null)
         {
            vertSource.append(line).append('\n');
         }
         while((line = fragReader.readLine()) != null)
         {
            fragSource.append(line).append('\n');
         }
         vertReader.close();
         fragReader.close();
      }
      catch(Exception e)
      {
         System.err.println(e);
      }
     
      glShaderSource(vertHandle, vertSource);
      glShaderSource(fragHandle, fragSource);
      glCompileShader(vertHandle);
      glCompileShader(fragHandle);
      if(glGetShaderi(vertHandle, GL_COMPILE_STATUS) == GL_FALSE)
      {
         System.err.println("Vertex shader failed to compile");
      }
      if(glGetShaderi(fragHandle, GL_COMPILE_STATUS) == GL_FALSE)
      {
         System.err.println("Fragment shader failed to compile");
      }
      glAttachShader(programHandle, vertHandle);
      glAttachShader(programHandle, fragHandle);
      glLinkProgram(programHandle);
      glValidateProgram(programHandle);
   }
   
   public void bind()
   {
      glUseProgram(programHandle);
   }
   
   public void release()
   {
      glUseProgram(0);
   }
   
   public void destroy()
   {
      glDeleteProgram(programHandle);
      glDeleteShader(vertHandle);
      glDeleteShader(fragHandle);
   }
}


EDIT: It's getting late here, I'm going in a few minutes, I'll check back tomorrow, and thanks for all the quick replies! I'll have to ask my programming teacher about this, it appears as though "discard" should be working properly, so I'm unsure as to what I should do next.
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #13 - Posted 2014-05-15 12:56:47 »

Why are you guys talking about shaders?
Isnt this problem clearly caused by drawing order (depth buffer does not know about discards)?
Offline Kwauhn

Senior Newbie


Exp: 3 years


I like cats


« Reply #14 - Posted 2014-05-15 15:10:18 »

The shader is a possible elegant solution to the problem that would also allow for shadow mapping that includes the alpha in textures. Also, if I could avoid the heavy process of manually sorting with a fast program that runs on the GPU it'd save a lot time and processing power.
Offline pitbuller
« Reply #15 - Posted 2014-05-15 17:14:31 »

Why are you guys talking about shaders?
Isnt this problem clearly caused by drawing order (depth buffer does not know about discards)?
Discard prevent depth writes.
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (37 views)
2014-10-17 03:59:02

Norakomi (28 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (27 views)
2014-10-15 16:18:58

TehJavaDev (52 views)
2014-10-14 00:39:48

TehJavaDev (54 views)
2014-10-14 00:35:47

TehJavaDev (42 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (77 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!