Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (797)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (864)
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  
  Depth testing isn't working  (Read 4960 times)
0 Members and 1 Guest are viewing this topic.
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Posted 2014-11-23 20:29:05 »

Click to Play


Also,
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  
@Override
public void init() throws Throwable {
   super.init();
   GL11.glClearColor(0.2f, 0.2f, 0.2f, 1);
   GL11.glClearDepth(1d);
   rootObject.init();
   
   GL11.glEnable(GL11.GL_DEPTH_TEST);
   
   ambient = new Program();
   ambient.attachShader((ShaderAsset)getLoader().getAsset("program-world-ambient-fs"));
   ambient.attachShader((ShaderAsset)getLoader().getAsset("program-world-ambient-vs"));
   ambient.link();
}

@Override
public void render() throws Throwable {
   super.render();
   GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
   ambient.use();
   
   rootObject.render(ambient);
   
   GL11.glEnable(GL11.GL_BLEND);
   GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE);
   GL11.glDepthMask(false);
   GL11.glDepthFunc(GL11.GL_EQUAL);
   
   for(int i = 0; i < lights.size(); i++){
      lights.get(i).use();
      rootObject.render(lights.get(i).getProgram());
   }
   
   GL11.glDepthFunc(GL11.GL_LEQUAL);
   GL11.glDepthMask(true);
   GL11.glDisable(GL11.GL_BLEND);
}


Produces some wierd problems like this:
Click to Play

Offline basil_

« JGO Bitwise Duke »


Medals: 418
Exp: 13 years



« Reply #1 - Posted 2014-11-23 20:43:42 »

it's related to
glDepthMask
(write to depth buffer) and draw order.

looks like it's not writing depth but still testing, which is valid, but probably not what you wanted Smiley
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #2 - Posted 2014-11-23 20:48:38 »

it's related to
glDepthMask
(write to depth buffer) and draw order.

looks like it's not writing depth but still testing, which is valid, but probably not what you wanted Smiley

I've heard about draw orders before... Somewere.

How would I go about fixing this? This is my first time having this problem, and I don't know how I got it :O

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #3 - Posted 2014-11-23 21:01:10 »

Click to Play


One step forward, two steps back. Wink

Offline basil_

« JGO Bitwise Duke »


Medals: 418
Exp: 13 years



« Reply #4 - Posted 2014-11-23 21:10:11 »

depends on what image you want to make Wink

what if you enabled depth test and depth write .. and disable backface culling
GL11.glDisable(GL11.GL_CULL_FACE);
? the sphere should not be visible then when camera is below the plane.

*edit*

draw order would not be important then anymore. just affecting performance but not the output image.

it's only important if you do depth-testing with scenes which contains objects which are not writing to the depth buffer ... or the other way around.
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #5 - Posted 2014-11-23 21:12:27 »

Click to Play


No effect... It looks like the ball is transparent from before too.

Offline basil_

« JGO Bitwise Duke »


Medals: 418
Exp: 13 years



« Reply #6 - Posted 2014-11-23 21:16:59 »

sounds like shader/blending mismatch.

try
1  
2  
GL14.glBlendEquation(GL14.GL_FUNC_ADD);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA,GL11.GL_ONE_MINUS_SRC_ALPHA);
which is "normal alpha blending" and write alpha
1.0
in the shader.

yet, the sphere should be occluded (camera below plane) with depth testing and writing enabled O_o.
Offline basil_

« JGO Bitwise Duke »


Medals: 418
Exp: 13 years



« Reply #7 - Posted 2014-11-23 21:18:35 »

oh wait .. maybe it's the depth compare function. try
GL11.glDepthFunc(GL11.GL_LEQUAL);
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #8 - Posted 2014-11-23 21:30:50 »

Heres my code so far,

Init:
1  
2  
3  
4  
5  
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthMask(true);
GL11.glDepthRange(0.0f, 1.0f);
GL11.glDisable(GL11.GL_CULL_FACE);
GL11.glDepthFunc(GL11.GL_LEQUAL);


Render:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glEnable(GL11.GL_BLEND);
GL14.glBlendEquation(GL14.GL_FUNC_ADD);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA,GL11.GL_ONE_MINUS_SRC_ALPHA);

ambient.use();

rootObject.render(ambient);

GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE);
GL11.glDepthMask(false);
GL11.glDepthFunc(GL11.GL_EQUAL);

for(int i = 0; i < lights.size(); i++){
   lights.get(i).use();
   rootObject.render(lights.get(i).getProgram());
}

GL11.glDepthFunc(GL11.GL_LESS);
GL11.glDepthMask(true);
GL11.glDisable(GL11.GL_DEPTH_TEST);


Click to Play

Offline basil_

« JGO Bitwise Duke »


Medals: 418
Exp: 13 years



« Reply #9 - Posted 2014-11-23 21:57:22 »

remove line 10 - 12 and 19 - 21 from the 2nd code block Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #10 - Posted 2014-11-23 22:12:57 »

Click to Play


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glEnable(GL11.GL_BLEND);
GL14.glBlendEquation(GL14.GL_FUNC_ADD);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA,GL11.GL_ONE_MINUS_SRC_ALPHA);

ambient.use();

rootObject.render(ambient);

for(int i = 0; i < lights.size(); i++){
   lights.get(i).use();
   rootObject.render(lights.get(i).getProgram());
}

Offline basil_

« JGO Bitwise Duke »


Medals: 418
Exp: 13 years



« Reply #11 - Posted 2014-11-23 22:19:28 »

... wtf!
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #12 - Posted 2014-11-23 22:20:28 »

My init code if it helps.

1  
2  
3  
4  
5  
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthMask(true);
GL11.glDepthRange(0.0f, 1.0f);
GL11.glDisable(GL11.GL_CULL_FACE);
GL11.glDepthFunc(GL11.GL_LEQUAL);

Offline basil_

« JGO Bitwise Duke »


Medals: 418
Exp: 13 years



« Reply #13 - Posted 2014-11-23 22:27:46 »

looks fine to me.. almost out of ideas. sorry Emo

are you writing depth in the fragment shader ?
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #14 - Posted 2014-11-23 22:32:51 »

looks fine to me.. almost out of ideas. sorry Emo

are you writing depth in the fragment shader ?

Donno...

Ambient shader:
1  
2  
3  
4  
5  
6  
#include "./asset/shader/global.fsh"

vec4 calcPixel()
{
   return texture2D(u_material.map_Kd, v_texcoord);
}


Point Light Shader:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
#include "./asset/shader/light.fsh"

uniform PointLight u_light;

uniform vec3 u_lPosition;
varying vec3 l_position;

vec4 calcLightModule(vec3 p_normal, vec3 p_position)
{
   vec3 l_direction = l_position - p_position;
   float dist = length(l_direction);
   if(dist > u_light.range) return vec4(0, 0, 0, 0);
   
   return lightModule(u_light.base, u_material, normalize(l_direction), p_normal, p_position) /
                                      (u_light.atten.constant + u_light.atten.linear * dist
                                       + u_light.atten.exponent * dist * dist + 0.0001);
}


The #include just takes another file, and loads it into the final string before shader compilation.

Offline basil_

« JGO Bitwise Duke »


Medals: 418
Exp: 13 years



« Reply #15 - Posted 2014-11-23 22:44:04 »

mhm .. what i ment is the part which writes fragment outputs like
out vec4 frag0;
or
gl_FragColor
or
gl_FragDepth
.
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #16 - Posted 2014-11-23 22:46:13 »

I tried outputting just the depth, and it does this:
1  
return vec4(gl_FragDepth, gl_FragDepth, gl_FragDepth, 1);


Click to Play


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthRange(0.0f, 1.0f);
GL11.glDepthMask(true);
GL11.glDepthFunc(GL11.GL_LEQUAL);

ambient.use();

rootObject.render(ambient);


Offline basil_

« JGO Bitwise Duke »


Medals: 418
Exp: 13 years



« Reply #17 - Posted 2014-11-23 22:52:59 »

ah no no. best thing is not to touch gl_FragDepth at all.

if, it's a output variable. but if you write to it you disable a couple GL optimization. https://www.opengl.org/sdk/docs/man/html/gl_FragDepth.xhtml

it would explain the visual glitch. just a guess Smiley
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #18 - Posted 2014-11-23 22:56:24 »

if, it's a output variable. but if you write to it you disable a couple GL optimization.

So what are you saying? What should I do next? I'm still very new to depth testing.

Offline basil_

« JGO Bitwise Duke »


Medals: 418
Exp: 13 years



« Reply #19 - Posted 2014-11-23 23:03:46 »

i suggest not writing to gl_FragDepth Smiley

otherwise, your GL code looks fine to me. maybe somebody else can spot the source.  Stare
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #20 - Posted 2014-11-25 14:17:29 »

I figured it out, turns out whatever transform you do in vecmath is in row-major order, so switching the near and far plane fixed it.

1  
2  
+ new ProjectionTransformComponent(this, Projection.createPerspective(40, (float) Window.getWidth() / Window.getHeight(), 100, 0.01f));
- new ProjectionTransformComponent(this, Projection.createPerspective(40, (float) Window.getWidth() / Window.getHeight(), 0.01f, 100));




wooho

Pages: [1]
  ignore  |  Print  
 
 

 
hadezbladez (3656 views)
2018-11-16 13:46:03

hadezbladez (1301 views)
2018-11-16 13:41:33

hadezbladez (3664 views)
2018-11-16 13:35:35

hadezbladez (701 views)
2018-11-16 13:32:03

EgonOlsen (4006 views)
2018-06-10 19:43:48

EgonOlsen (4534 views)
2018-06-10 19:43:44

EgonOlsen (2694 views)
2018-06-10 19:43:20

DesertCoockie (3552 views)
2018-05-13 18:23:11

nelsongames (3710 views)
2018-04-24 18:15:36

nelsongames (4733 views)
2018-04-24 18:14:32
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
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!