JOGL z-buffer rendering problems

25 Views Asked by At

I wrote a Java/JOGL program to build a wall with lots of bricks. I wrote this program in 2009 with JOGL 1.x I updated it this year with JOGL 2.5 I have big display problems, as if there was a problem with the z-buffer configuration. I am unable to resolve this problem !

We can see triangles emerging.

enter image description here

enter image description here

enter image description here

Here is my OpenGL configuration :

@Override
public void init(GLAutoDrawable glDrawable) {
    final GL2 gl = glDrawable.getGL().getGL2();
    
    gl.glShadeModel(GL2.GL_SMOOTH); // Enable Smooth Shading
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
    gl.glClearDepth(1.0f); // Depth Buffer Setup
    gl.glClearStencil(0); // Stencil Buffer Setup

    gl.glEnable(GL2.GL_DEPTH_TEST); // Enables Depth Testing
    gl.glDepthFunc(GL2.GL_LEQUAL); // The Type Of Depth Testing To Do
    gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST); // Really Nice Perspective Calculations
    //gl.glDepthRange(0,1);

    
    final float LIGHT_POS[] = { 50.0f, 100.0f, 50.0f, 1.0f }; // Light Position
    FloatBuffer lightPos = FloatBuffer.wrap(LIGHT_POS);
    gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, lightPos); // Set Light1 Position
    
    
    final float LIGHT_AMB[] = { 0.4f, 0.4f, 0.4f, 1.0f }; // Ambient Light Values
    FloatBuffer lightAmb = FloatBuffer.wrap(LIGHT_AMB);
    gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, lightAmb); // Set Light1 Ambience
    
    final float LIGHT_DIF[] = { 0.5f, 0.5f, 0.5f, 1.0f };
    FloatBuffer lightDif = FloatBuffer.wrap(LIGHT_DIF);
    gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, lightDif); // Set Light1 Diffuse

    final float LIGHT_SPC[] = { 0.8f, 0.8f, 0.8f, 1.0f };
    FloatBuffer lightSpc = FloatBuffer.wrap(LIGHT_SPC);
    gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, lightSpc); // Set Light1 Specular
    gl.glEnable(GL2.GL_LIGHT0); // Enable Light0
    gl.glEnable(GL2.GL_LIGHTING); // Enable Lighting

    // specification de la reflexion sur les materiaux
    gl.glEnable(GL2.GL_COLOR_MATERIAL);
    gl.glColorMaterial(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE);
    
    final float AMBIENT[] = { 0.15f, 0.15f, 0.15f, 1.0f };
    FloatBuffer ambient = FloatBuffer.wrap(AMBIENT);
    gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_AMBIENT, ambient);

    
    // on peut le faire avant chaque objet
    final float DIFFUSE[] = { 0.5f, 0.5f, 0.5f, 1.0f };
    FloatBuffer diffuse = FloatBuffer.wrap(DIFFUSE);
    gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_DIFFUSE, diffuse);

    final float SPECULAR_REFLEXION[] = { 0.8f, 0.8f, 0.8f, 1.0f };
    FloatBuffer specular_reflexion = FloatBuffer.wrap(SPECULAR_REFLEXION);
    gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_SPECULAR, specular_reflexion);

    // si on veut qu'ils aient des caracteristiques
    int shiny_obj = 128;
    gl.glMateriali(GL2.GL_FRONT_AND_BACK, GL2.GL_SHININESS, shiny_obj);

    gl.glEnable(GL2.GL_BLEND);
    gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);
}

Here is the definition of a brick:

        gl.glColor4f(red, green, blue, alpha);

    final Point p0 = bl.getPoint(0);
    final Point p1 = bl.getPoint(1);
    final Point p2 = bl.getPoint(2);
    final Point p3 = bl.getPoint(3);
    final Point p4 = bl.getPoint(4);
    final Point p5 = bl.getPoint(5);
    final Point p6 = bl.getPoint(6);
    final Point p7 = bl.getPoint(7);

    gl.glBegin(GL2.GL_QUADS);

    // Bottom face
    {
        Point bottom = bl.getNormalVectorBottomFace();
        gl.glNormal3f(bottom.getX(), bottom.getY(), bottom.getZ());
        gl.glVertex3f(p0.getX(), p0.getY(), p0.getZ());
        gl.glVertex3f(p1.getX(), p1.getY(), p1.getZ());
        gl.glVertex3f(p2.getX(), p2.getY(), p2.getZ());
        gl.glVertex3f(p3.getX(), p3.getY(), p3.getZ());
    }

    // Top face
    {
        Point top = bl.getNormalVectorTopFace();
        gl.glNormal3f(top.getX(), top.getY(), top.getZ());
        gl.glVertex3f(p4.getX(), p4.getY(), p4.getZ());
        gl.glVertex3f(p5.getX(), p5.getY(), p5.getZ());
        gl.glVertex3f(p6.getX(), p6.getY(), p6.getZ());
        gl.glVertex3f(p7.getX(), p7.getY(), p7.getZ());


    }

    // Left face
    {
        Point left = bl.getNormalVectorLeftFace();
        gl.glNormal3f(left.getX(), left.getY(), left.getZ());
        gl.glVertex3f(p0.getX(), p0.getY(), p0.getZ());
        gl.glVertex3f(p3.getX(), p3.getY(), p3.getZ());
        gl.glVertex3f(p7.getX(), p7.getY(), p7.getZ());
        gl.glVertex3f(p4.getX(), p4.getY(), p4.getZ());
    }

    // Right face
    {
        Point right = bl.getNormalVectorRightFace();
        gl.glNormal3f(right.getX(), right.getY(), right.getZ());
        gl.glVertex3f(p1.getX(), p1.getY(), p1.getZ());
        gl.glVertex3f(p5.getX(), p5.getY(), p5.getZ());
        gl.glVertex3f(p6.getX(), p6.getY(), p6.getZ());
        gl.glVertex3f(p2.getX(), p2.getY(), p2.getZ());
    }

    // Front face
    {
        Point front = bl.getNormalVectorFrontFace();
        gl.glNormal3f(front.getX(), front.getY(), front.getZ());
        gl.glVertex3f(p3.getX(), p3.getY(), p3.getZ());
        gl.glVertex3f(p2.getX(), p2.getY(), p2.getZ());
        gl.glVertex3f(p6.getX(), p6.getY(), p6.getZ());
        gl.glVertex3f(p7.getX(), p7.getY(), p7.getZ());
    }

    // Back face
    {
        Point back = bl.getNormalVectorBackFace();
        gl.glNormal3f(back.getX(), back.getY(), back.getZ());
        gl.glVertex3f(p0.getX(), p0.getY(), p0.getZ());
        gl.glVertex3f(p4.getX(), p4.getY(), p4.getZ());
        gl.glVertex3f(p5.getX(), p5.getY(), p5.getZ());
        gl.glVertex3f(p1.getX(), p1.getY(), p1.getZ());
    }

    gl.glEnd();
0

There are 0 best solutions below