Rendering problems with jogl NeHe Lesson 05

Hello,

I am trying to write the NeHe opengl tutorials with groovy and JoGL JSR-231 but i am
kind of stuck at lesson 05. The square does not seems to render correctly. I expanded
the square’s sides so that the problem shows more clearly. The green color seems to allways
be in the back of all the others and the triangle seems semi transparent.

I use Kubuntu 7.10 beta … and groovy…

see screenshot http://www.anyplanet.com/lesson05.png

Thanx.

/**

  • Define the callback listener class. This class listens for opengl

  • events and also keyboard events.
    */
    class Renderer implements GLEventListener, KeyListener {

    float rtri = 0.0f
    float rquad = 0.0f

    /**

    • Called by the drawable to initiate OpenGL rendering by the client.
      */
      void display(GLAutoDrawable gLDrawable) {
      def gl = gLDrawable.GL
      gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)

      gl.glLoadIdentity() // Reset The Current Modelview Matrix
      gl.glTranslatef(-1.5f,0.0f,-6.0f) // Move Left 1.5 Units And Into The Screen 6.0
      gl.glRotatef(rtri,0.0f,1.0f,0.0f) // Rotate The Triangle On The Y axis
      gl.glBegin(GL.GL_TRIANGLES) // Start Drawing A Triangle
      gl.glColor3f(1.0f,0.0f,0.0f) // Red
      gl.glVertex3f( 0.0f, 1.0f, 0.0f) // Top Of Triangle (Front)
      gl.glColor3f(0.0f,1.0f,0.0f) // Green
      gl.glVertex3f(-1.0f,-1.0f, 1.0f) // Left Of Triangle (Front)
      gl.glColor3f(0.0f,0.0f,1.0f) // Blue
      gl.glVertex3f( 1.0f,-1.0f, 1.0f) // Right Of Triangle (Front)
      gl.glColor3f(1.0f,0.0f,0.0f) // Red
      gl.glVertex3f( 0.0f, 1.0f, 0.0f) // Top Of Triangle (Right)
      gl.glColor3f(0.0f,0.0f,1.0f) // Blue
      gl.glVertex3f( 1.0f,-1.0f, 1.0f) // Left Of Triangle (Right)
      gl.glColor3f(0.0f,1.0f,0.0f) // Green
      gl.glVertex3f( 1.0f,-1.0f, -1.0f) // Right Of Triangle (Right)
      gl.glColor3f(1.0f,0.0f,0.0f) // Red
      gl.glVertex3f( 0.0f, 1.0f, 0.0f) // Top Of Triangle (Back)
      gl.glColor3f(0.0f,1.0f,0.0f) // Green
      gl.glVertex3f( 1.0f,-1.0f, -1.0f) // Left Of Triangle (Back)
      gl.glColor3f(0.0f,0.0f,1.0f) // Blue
      gl.glVertex3f(-1.0f,-1.0f, -1.0f) // Right Of Triangle (Back)
      gl.glColor3f(1.0f,0.0f,0.0f) // Red
      gl.glVertex3f( 0.0f, 1.0f, 0.0f) // Top Of Triangle (Left)
      gl.glColor3f(0.0f,0.0f,1.0f) // Blue
      gl.glVertex3f(-1.0f,-1.0f,-1.0f) // Left Of Triangle (Left)
      gl.glColor3f(0.0f,1.0f,0.0f) // Green
      gl.glVertex3f(-1.0f,-1.0f, 1.0f) // Right Of Triangle (Left)
      gl.glEnd() // Done Drawing The Pyramid

      gl.glLoadIdentity() // Reset The Current Modelview Matrix
      gl.glTranslatef(1.5f,0.0f,-7.0f) // Move Right 1.5 Units And Into The Screen 7.0
      gl.glRotatef(rquad,1.0f,1.0f,1.0f) // Rotate The Quad On The all axises
      gl.glBegin(GL.GL_QUADS) // Draw A Quad
      gl.glColor3f(0.0f,1.0f,0.0f) // Set The Color To Green
      gl.glVertex3f( 1.0f, 1.0f,-1.0f) // Top Right Of The Quad (Top)
      gl.glVertex3f(-1.0f, 1.0f,-1.0f) // Top Left Of The Quad (Top)
      gl.glVertex3f(-1.0f, 1.0f, 1.0f) // Bottom Left Of The Quad (Top)
      gl.glVertex3f( 1.0f, 1.0f, 1.0f) // Bottom Right Of The Quad (Top)
      gl.glColor3f(1.0f,0.5f,0.0f) // Set The Color To Orange
      gl.glVertex3f( 1.0f,-1.0f, 1.0f) // Top Right Of The Quad (Bottom)
      gl.glVertex3f(-1.0f,-1.0f, 1.0f) // Top Left Of The Quad (Bottom)
      gl.glVertex3f(-1.0f,-1.0f,-1.0f) // Bottom Left Of The Quad (Bottom)
      gl.glVertex3f( 1.0f,-1.0f,-1.0f) // Bottom Right Of The Quad (Bottom)
      gl.glColor3f(1.0f,0.0f,0.0f) // Set The Color To Red
      gl.glVertex3f( 1.0f, 1.0f, 1.0f) // Top Right Of The Quad (Front)
      gl.glVertex3f(-1.0f, 1.0f, 1.0f) // Top Left Of The Quad (Front)
      gl.glVertex3f(-1.0f,-1.0f, 1.0f) // Bottom Left Of The Quad (Front)
      gl.glVertex3f( 1.0f,-1.0f, 1.0f) // Bottom Right Of The Quad (Front)
      gl.glColor3f(1.0f,1.0f,0.0f) // Set The Color To Yellow
      gl.glVertex3f( 1.0f,-1.0f,-1.0f) // Top Right Of The Quad (Back)
      gl.glVertex3f(-1.0f,-1.0f,-1.0f) // Top Left Of The Quad (Back)
      gl.glVertex3f(-1.0f, 1.0f,-1.0f) // Bottom Left Of The Quad (Back)
      gl.glVertex3f( 1.0f, 1.0f,-1.0f) // Bottom Right Of The Quad (Back)
      gl.glColor3f(0.0f,0.0f,1.0f) // Set The Color To Blue
      gl.glVertex3f(-1.0f, 1.0f, 1.0f) // Top Right Of The Quad (Left)
      gl.glVertex3f(-1.0f, 1.0f,-1.0f) // Top Left Of The Quad (Left)
      gl.glVertex3f(-1.0f,-1.0f,-1.0f) // Bottom Left Of The Quad (Left)
      gl.glVertex3f(-1.0f,-1.0f, 1.0f) // Bottom Right Of The Quad (Left)
      gl.glColor3f(1.0f,0.0f,1.0f) // Set The Color To Violet
      gl.glVertex3f( 1.0f, 1.0f,-1.0f) // Top Right Of The Quad (Right)
      gl.glVertex3f( 1.0f, 1.0f, 1.0f) // Top Left Of The Quad (Right)
      gl.glVertex3f( 1.0f,-1.0f, 1.0f) // Bottom Left Of The Quad (Right)
      gl.glVertex3f( 1.0f,-1.0f,-1.0f) // Bottom Right Of The Quad (Right)
      gl.glEnd()

      gl.glFlush()

      rtri += 0.2f // Increase The Rotation Variable For The Triangle
      rquad -= 0.15f // Decrease The Rotation Variable For The Quad
      }

    /**

    • Called by the drawable when the display mode or the display device associated with the GLAutoDrawable has changed.
      */
      void displayChanged(GLAutoDrawable gLDrawable, boolean modeChanged, boolean deviceChanged) {}

    /**

    • Called by the drawable immediately after the OpenGL context is initialized.
      */
      void init(GLAutoDrawable gLDrawable) {
      def gl = gLDrawable.GL
      gl.glShadeModel(GL.GL_SMOOTH) // Enable Smooth Shading
      gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f) // Black Background
      gLDrawable.addKeyListener(this)
      }

    /**

    • Called by the drawable during the first repaint after the component has been resized.
      */
      void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) {
      def gl = gLDrawable.GL
      def glu = new GLU()

      height = (height <= 0 ? 1 : height) // avoid a divide by zero error!

      def float h = (float)width / (float)height // caclulate aspect
      gl.glViewport(0, 0, width, height)

      gl.glMatrixMode(GL.GL_PROJECTION)
      gl.glLoadIdentity()
      glu.gluPerspective(45.0f, h, 1.0, 20.0)

      gl.glMatrixMode(GL.GL_MODELVIEW)
      gl.glLoadIdentity()

    }

    /**

    • Catch the escape key event.
      */
      void keyPressed(KeyEvent e) {
      if (e.keyCode == KeyEvent.VK_ESCAPE) {
      System.exit(0)
      }
      }

    void keyReleased(KeyEvent e) {}

    void keyTyped(KeyEvent e) {}

}[/font]

maybe you forgot enabling depth buffer or choose a wrong method for this one (you must choose GL_LESS for depth buffer test)? GL_ENABLE(GL_DEPH_BUFFER) or something like that ?

Thank you for the help… it seems like i did forget the depth test. I looked in the Java example that uses JoGL and
after that i added the following rows to the init method:
gl.glClearDepth(1.0f)
gl.glEnable(GL.GL_DEPTH_TEST)
gl.glDepthFunc(GL.GL_LEQUAL)
gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST)

Now it works …

Thanks again!!!

I dont really know what depth testing is but i am going to learn :slight_smile: