Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
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  
  Nothing written to depth buffer  (Read 962 times)
0 Members and 1 Guest are viewing this topic.
Offline trollwarrior1
« Posted 2016-03-05 20:59:29 »

Trying to implement depth testing for 2d isometric game. I have this sample, and it doesn't seem that depth values are written to the depth buffer.

The effect I'm trying to achieve is when second.png is drawn on top of first.png, only red is displayed on screen.
But what I get is that second.png is drawn normally and green part is visible.

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  
   private SpriteBatch mBatch;

   private Texture mSprite1;
   private Texture mSprite2;

   @Override
   public void create() {

      mBatch = new SpriteBatch();
      mBatch.setShader(new ShaderProgram(Gdx.files.internal("test.vsh"), Gdx.files.internal("test.fsh")));

      mSprite1 = new Texture("first.png");
      mSprite2 = new Texture("second.png");

      Gdx.gl20.glEnable(GL20.GL_DEPTH_TEST);
      Gdx.gl20.glDepthFunc(GL20.GL_LESS);
      Gdx.gl20.glDepthMask(true);

   }

   float mx = 0.0f;
   float my = 0.0f;

   private void tick() {
      float speed = 100.0f * Gdx.graphics.getDeltaTime();

      if (Gdx.input.isKeyPressed(Keys.A))
         mx -= speed;
      if (Gdx.input.isKeyPressed(Keys.D))
         mx += speed;
      if (Gdx.input.isKeyPressed(Keys.W))
         my += speed;
      if (Gdx.input.isKeyPressed(Keys.S))
         my -= speed;
   }

   @Override
   public void render() {

      tick();

      Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

      mBatch.begin();

      float scale = 4.0f;

      float x = Gdx.graphics.getWidth() / 2;
      float y = Gdx.graphics.getHeight() / 2;

      mBatch.draw(mSprite1, x - mSprite1.getWidth() / 2 * scale, y - mSprite1.getHeight() / 2 * scale,
            mSprite1.getWidth() * scale, mSprite1.getHeight() * scale);

      mBatch.flush();

      mBatch.draw(mSprite2, mx + x - mSprite2.getWidth() / 2 * scale, my + y - mSprite2.getHeight() / 2 * scale,
            mSprite2.getWidth() * scale, mSprite2.getHeight() * scale);

      mBatch.end();

      int width = Gdx.graphics.getWidth();
      int height = Gdx.graphics.getHeight();
      FloatBuffer buffer = BufferUtils.newFloatBuffer(width * height);
      Gdx.gl20.glReadPixels(0, 0, width, height, GL20.GL_DEPTH_COMPONENT, GL20.GL_FLOAT,
            buffer);

      for (int i = 0; i < width * height; i++) {
         float pixel = buffer.get(i);
         if (pixel != 1.0f) {
            throw new IllegalStateException("OMG IT WORKS!!");
         }
      }
     
      if (Gdx.gl20.glGetError()!=0) {
         throw new Error("OPENGL ERROR: " + Gdx.gl20.glGetError());
      }
   }


vertex shader
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
#ifdef GL_ES
precision mediump float;
#endif

attribute vec3 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;

uniform mat4 u_projTrans;
varying vec4 v_color;
varying vec2 v_texCoord;

void main()
{
   gl_Position = u_projTrans * vec4(a_position, 1);
   v_color = a_color * 2.0;
   v_texCoord = a_texCoord0;
}


fragment shader
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  
#ifdef GL_ES
precision mediump float;
#endif

uniform sampler2D u_texture;

varying vec4 v_color;
varying vec2 v_texCoord;

void main()
{

   vec4 texel = v_color * texture2D(u_texture, v_texCoord);

   if (texel.r > 0)
   {
      gl_FragDepth = 0.0;
   }
   else
   {
      gl_FragDepth = 0.5;
   }


   gl_FragColor = texel;
}


first.png


second.png
Offline trollwarrior1
« Reply #1 - Posted 2016-03-06 09:31:20 »

FINALLY found the problem.

SpriteBatch.begin() call glDepthMask(false) which in turn disables writing to depth buffer.

If I call glDepthMask(true) after SpriteBatch.begin(), everything works fine!!

Yay!  Grin
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (110 views)
2017-09-30 02:57:34

theagentd (136 views)
2017-09-26 18:23:31

cybrmynd (245 views)
2017-08-02 12:28:51

cybrmynd (241 views)
2017-08-02 12:19:43

cybrmynd (240 views)
2017-08-02 12:18:09

Sralse (254 views)
2017-07-25 17:13:48

Archive (864 views)
2017-04-27 17:45:51

buddyBro (1008 views)
2017-04-05 03:38:00

CopyableCougar4 (1568 views)
2017-03-24 15:39:42

theagentd (1373 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!