gl_quads are rendered uncorrectly

Hello Gentleman,

I have tried to render some figures which each consists of five quads.
Unfortunately they are shown wrong.
Does anybody have an idea, what the problem could be?

http://icetea-planet.de/gar/stuff/pic4.jpg

Depth test problem? Lighting with weird normals? Give us code! =D

	    public void init(GLAutoDrawable drawable) 
	    {
	    	w = drawable.getWidth();
	        h = drawable.getHeight();
	        GL2 gl = drawable.getGL().getGL2();
	        glu = new GLU();
	        gl.glEnable(GL2.GL_SMOOTH);
	        gl.glShadeModel(GL2.GL_SMOOTH);
	        gl.glClearColor(0.0f,0.0f,0.0f,1.0f);
	        gl.glClearDepth(1.0f);
	        gl.glClearDepth(1.0f);												// Depth Buffer Setup
	    	gl.glEnable(GL.GL_DEPTH_TEST);										// Enables Depth Testing
	    	gl.glDepthFunc(GL.GL_LEQUAL);
	    	gl.glEnable(GL.GL_LINE_SMOOTH);
	        gl.glEnable(GL.GL_BLEND);
	        gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
	    	gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_DONT_CARE);			// Really Nice Perspective Calculations
	    }

	    public void display(GLAutoDrawable drawable) 
	    {
	        GL2 gl = drawable.getGL().getGL2();
	        
	        w = drawable.getWidth();
	        h = drawable.getHeight();
	        
	        gl.glClear(GL.GL_COLOR_BUFFER_BIT|GL.GL_DEPTH_BUFFER_BIT);			// Clear the colour and depth buffer
	          
	        gl.glViewport(0, 0, w, h);											// Reset The Current Viewport
	        
	        gl.glMatrixMode(GL2.GL_PROJECTION);									// Select The Projection Matrix
	        gl.glLoadIdentity();												// Reset The Projection Matrix
	    	
	        glu.gluPerspective(45.0f,(float)w/(float)h,0.01f,1000000.0f);			// Calculate The Aspect Ratio Of The Window

	        gl.glMatrixMode(GL2.GL_MODELVIEW);                             // Select The Modelview Matrix
	        gl.glLoadIdentity();												// Reset The Modelview Matrix     
	        drawScene(drawable);												// Draw the scene
	    }

		public void reshape(GLAutoDrawable drawable, int x, int y, int w2, int h2) 
	    {
			GL2 gl = drawable.getGL().getGL2();
	        
	        w2 = drawable.getWidth();
	        h2 = drawable.getHeight();
	        
	        gl.glMatrixMode(GL2.GL_MODELVIEW);
	        gl.glLoadIdentity();
	        // perspective view
	        gl.glViewport(0, 0, w, h);
	        gl.glMatrixMode(GL2.GL_PROJECTION);
	        gl.glLoadIdentity();
	        glu.gluPerspective(45.0f,(float)w/(float)h,0.01f,1000000.0f);
	    }

	    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) 
	    {      
	    }
	    
	    public void drawScene(GLAutoDrawable drawable)
	    {
	    	GL2 gl = drawable.getGL().getGL2();
	    	gl.glClear(GL.GL_COLOR_BUFFER_BIT|GL.GL_DEPTH_BUFFER_BIT);
	    	
	    	gl.glRotatef(rotate[0],1f,0f, 0f);
	    	gl.glRotatef(rotate[1],0f,1f, 0f);
	    	gl.glRotatef(rotate[2],0f,0f, 1f);
	    	gl.glTranslatef(trans[0],trans[1],trans[2]);
	    	gl.glScalef(scale[0], scale[1], scale[2]);
	    	float hfloat = 0.1f;
	    	float hcolor = 0;
	    	for(int i=0;i<coords.size();i++){
	    	if(coords.get(i).getRed()>1){
	    		coords.get(i).setRed(1);
	    	}
	    	if(coords.get(i).getGreen()>1){
	    		coords.get(i).setGreen(1);
	    	}
	    	if(coords.get(i).getBlue()>1){
	    		coords.get(i).setBlue(1);
	    	}
	    	if(coords.get(i).getX(1)>=1f||coords.get(i).getZ(3)>=5f){
	    		hfloat = 0;
	    	} else {
	    		hfloat = 5f;
	    	}
	    	if(light==i){
	    		hcolor = 0.5f;
	    	} else {
	    		hcolor = 0;
	    	}
	    	gl.glPushMatrix();
	    	    //Koordinaten
	    		gl.glTranslatef(coords.get(i).getPositionX(),coords.get(i).getPositionY()+0.01f,coords.get(i).getPositionZ());								// Move x,y,z
	    		gl.glBegin(GL2.GL_QUADS);		
	    		//back
	    		gl.glColor3f(coords.get(i).getRed()-0.2f+hcolor,coords.get(i).getGreen()-0.2f+hcolor, coords.get(i).getBlue()-0.2f+hcolor);
	    		gl.glVertex3f(coords.get(i).getX(3)+hfloat,coords.get(i).getY(3),coords.get(i).getZ(3)+hfloat);								
	    		gl.glVertex3f(coords.get(i).getX(7)+hfloat,coords.get(i).getY(7),coords.get(i).getZ(7)+hfloat);								
	    		gl.glVertex3f(coords.get(i).getX(6)-hfloat,coords.get(i).getY(6),coords.get(i).getZ(6)+hfloat);
	    		gl.glVertex3f(coords.get(i).getX(2)-hfloat,coords.get(i).getY(2),coords.get(i).getZ(2)+hfloat);    		
	    		//left
	    		gl.glColor3f(coords.get(i).getRed()-0.3f+hcolor,coords.get(i).getGreen()-0.3f+hcolor, coords.get(i).getBlue()-0.3f+hcolor);
	    		gl.glVertex3f(coords.get(i).getX(4)+hfloat,coords.get(i).getY(4),coords.get(i).getZ(4)-hfloat);							
	    		gl.glVertex3f(coords.get(i).getX(0)+hfloat,coords.get(i).getY(0),coords.get(i).getZ(0)-hfloat);								
	    		gl.glVertex3f(coords.get(i).getX(3)+hfloat,coords.get(i).getY(3),coords.get(i).getZ(3)+hfloat);    		
	    		gl.glVertex3f(coords.get(i).getX(7)+hfloat,coords.get(i).getY(7),coords.get(i).getZ(7)+hfloat);
	    		//right
	    		gl.glColor3f(coords.get(i).getRed()-0.4f+hcolor,coords.get(i).getGreen()-0.4f+hcolor, coords.get(i).getBlue()-0.4f+hcolor);
	    		gl.glVertex3f(coords.get(i).getX(2)-hfloat,coords.get(i).getY(2),coords.get(i).getZ(2)+hfloat);							
	    		gl.glVertex3f(coords.get(i).getX(1)-hfloat,coords.get(i).getY(1),coords.get(i).getZ(1)-hfloat);					
	    		gl.glVertex3f(coords.get(i).getX(5)-hfloat,coords.get(i).getY(5),coords.get(i).getZ(5)-hfloat);    		
	    		gl.glVertex3f(coords.get(i).getX(6)-hfloat,coords.get(i).getY(6),coords.get(i).getZ(6)+hfloat);
	    		//top
	    		gl.glColor3f(coords.get(i).getRed()-0.5f+hcolor,coords.get(i).getGreen()-0.5f+hcolor, coords.get(i).getBlue()-0.5f+hcolor);
	    		gl.glVertex3f(coords.get(i).getX(4)+hfloat,coords.get(i).getY(4),coords.get(i).getZ(4)-hfloat);							
	    		gl.glVertex3f(coords.get(i).getX(5)-hfloat,coords.get(i).getY(5),coords.get(i).getZ(5)-hfloat);							
	    		gl.glVertex3f(coords.get(i).getX(6)-hfloat,coords.get(i).getY(6),coords.get(i).getZ(6)+hfloat);
	    		gl.glVertex3f(coords.get(i).getX(7)+hfloat,coords.get(i).getY(7),coords.get(i).getZ(7)+hfloat); 
	    		//front
	    		gl.glColor3f(coords.get(i).getRed()-hfloat+hcolor,coords.get(i).getGreen()-0.1f+hcolor, coords.get(i).getBlue()-0.1f+hcolor);
	    		gl.glVertex3f(coords.get(i).getX(1)-hfloat,coords.get(i).getY(1),coords.get(i).getZ(1)-hfloat);								
	    		gl.glVertex3f(coords.get(i).getX(0)+hfloat,coords.get(i).getY(0),coords.get(i).getZ(0)-hfloat);							
	    		gl.glVertex3f(coords.get(i).getX(4)+hfloat,coords.get(i).getY(4),coords.get(i).getZ(4)-hfloat);
	    		gl.glVertex3f(coords.get(i).getX(5)-hfloat,coords.get(i).getY(5),coords.get(i).getZ(5)-hfloat);
	    		gl.glEnd();														
	    	gl.glPopMatrix();

	    	}
	    }

I used different deph test functions. I didn’t help.

You’re getting z-bleeding. Change the near-z and far-z in gluPerspective() to something sane, like 0.1f - 1000f.

Hi,
thanks! It is much better now.
Can I do something additional to that, because I think it is not good enough yet.

Do you still have the same problem? >_>

  • Change depth func to GL_LESS. It’s better in pretty much all cases.
  • You can’t use blending with z-buffering/depth testing, or at least you won’t get the result you expect without sorting your triangles which in turn makes depth testing unnecessary.
  • You’re also having a slightly low field of view (45 degrees).
  • Line smoothing doesn’t work since it makes edges slightly transparent based on coverage -> blending -> not with depth testing.
  • glEnable(GL_SMOOTH) is not a valid command.
  • You aren’t using the built-in OpenGL lighting system, so you can remove glShadeModel() too.
  • You have two glClearDepth.
  • The perspective correction hint is not needed since perspective correction became common, which was something like 10 years ago.
  • Since you setup the viewport and the projection matrix every frame (in the display() function), you don’t need to do anything in reshape()
  • You’re “corrupting” the modelview matrix in your renderScene() method. I’d recommend moving the glPushMatrix() to just after glClear(), before your calls to glRotatef(), glTranslate() and glScale().

Other than that, it’s fine. ;D

Thanks.
It works fine now ;D