2D LWJGL with OpenGL - Enabling Depth Checking for Splitting 2D Textures?

What do I need to add to my initGL init method to enable depth testing, and how would I use it for texturing?

I would need to expand the last glOrtho parameter to be more extreme than -1, and of course deep testing glEnable. Then, to use it, I can only assume that I am changing the third glVertex parameter to something that is not 0 in order to send it in front of / behind the other textures.

I try this and the damn textures don't even show. xD I must be missing something.


EDIT: RE: Tim's answer

whenever I made the z image more extreme than -1 it didn't show that the screen was only black.

void initGL(){

GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glEnable(GL11.GL_DEPTH_TEST); //depth test enabled

    GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glOrtho(-width/2, width/2, -height/2, height/2, 1, -10);//far changed to -10
    GL11.glMatrixMode(GL11.GL_MODELVIEW);

}

      

and

void loadBG(int theLoadedOne){

GL11.glBindTexture(GL11.GL_TEXTURE_2D, theLoadedOne);
GL11.glBegin(GL11.GL_QUADS);
    GL11.glTexCoord2f(0,0);
GL11.glVertex3f(-width/2,height/2, -2);//new z value

    GL11.glTexCoord2f(1,0);
GL11.glVertex3f(width/2,height/2,-2);//new z value

    GL11.glTexCoord2f(1,1);
GL11.glVertex3f(width/2,-height/2,-2);//new z value

    GL11.glTexCoord2f(0,1);
GL11.glVertex3f(-width/2,-height/2,-2);//new z value

GL11.glEnd();
GL11.glFlush();

}

      

and

while(!Display.isCloseRequested()){
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

...

            for(int i=0;i<1;i++){  //dont mind this for loop
                bg.loadThisBG(0);  //its here for reasons
            }
            updateFPS();

        Display.update();

    } Display.destroy();

}

      

+3


source to share


2 answers


It seems that you have switched to the far and distant plane. Have a look at gluOrtho2D . It just calls glOrtho

with near=-1

and far=+1

, resulting in a coordinate toggle sign z ( m33=-2/(far-near)

). However, with the above values, m33=-2/(-10-1)

is positive and the z-axis is changed to a standard workflow.

These consequences are seen from the back in the quadrant.

OpenGL's matrix manipulation methods don't care what you feed them; unless the values ​​result in division by zero.




Assuming there is no model transformation and only one matrix facilitating projection, here's what I think is going on:

Converting z-value from world to NDC space is z_ndc = -9/11 * z_w + 2/11

(set close and far into spelling matrix and occupies the third row). Now z_w=-2

, and therefore z_ndc = 20/11

. This goes beyond the boundaries of NDC space and is discarded.

Okay, I'm guessing this test is implicitly enabled / disabled by the Z test itself. The next suspect would be back surface dropping ...

+1


source


If your context includes a depth buffer (not sure about creating lwjgl buffer ...)

All you need should be:



  • Call glEnable(GL_DEPTH_TEST)

    during initialization
  • Add a bit of depth buffer to glClear glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  • Determine the z coordinate between the near and far values ​​of the spelling matrix.
+1


source







All Articles