Color Rendering Probem

Hi, I am trying the NeHe lessons and get to No 5. But here an error occurs: The triangle is shown in blue (last color set), the quad is shown correctly:

package testJOGL; 

import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
 
class Renderer implements GLEventListener 
{
    private GLU glu = new GLU();
 
    public void display(GLAutoDrawable gLDrawable) 
    {
        final GL2 gl = gLDrawable.getGL().getGL2();
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();
        gl.glTranslatef(-1.5f, 0.0f, -6.0f);
        gl.glBegin(GL2.GL_TRIANGLES);		    // Drawing Using Triangles        
          gl.glColor3f(1.0f, 0.0f, 0.0f);   // Set the current drawing color to red
          gl.glVertex3f( 0.0f, 1.0f, 0.0f);	// Top          
          gl.glColor3f(0.0f, 1.0f, 0.0f);   // Set the current drawing color to green
          gl.glVertex3f(-1.0f,-1.0f, 0.0f);	// Bottom Left          
          gl.glColor3f(0.0f, 0.0f, 1.0f);   // Set the current drawing color to blue
          gl.glVertex3f( 1.0f,-1.0f, 0.0f);	// Bottom Right          
        gl.glEnd();				// Finished Drawing The Triangle
        gl.glTranslatef(3.0f, 0.0f, 0.0f);
        gl.glBegin(GL2.GL_QUADS);           	// Draw A Quad
          gl.glColor3f(0.5f, 0.5f, 1.0f);   // Set the current drawing color to light blue
          gl.glVertex3f(-1.0f, 1.0f, 0.0f);	// Top Left
          gl.glVertex3f( 1.0f, 1.0f, 0.0f);	// Top Right
          gl.glVertex3f( 1.0f,-1.0f, 0.0f);	// Bottom Right
          gl.glVertex3f(-1.0f,-1.0f, 0.0f);	// Bottom Left
        gl.glEnd();				// Done Drawing The Quad
        gl.glFlush();
    }
 
 
    public void displayChanged(GLAutoDrawable gLDrawable, boolean modeChanged, boolean deviceChanged) 
    {
    	System.out.println("displayChanged called");
    }
 
    public void init(GLAutoDrawable gLDrawable) 
    {
    	System.out.println("init() called");
        GL2 gl = gLDrawable.getGL().getGL2();
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glShadeModel(GL2.GL_FLAT);
        gl.glClearDepth(1.0f);
        gl.glEnable(GL2.GL_DEPTH_TEST);
        gl.glDepthFunc(GL2.GL_LEQUAL);
        gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);   
    }
 
    public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) 
    {
    	System.out.println("reshape() called: x = "+x+", y = "+y+", width = "+width+", height = "+height);
        final GL2 gl = gLDrawable.getGL().getGL2();
 
        if (height <= 0) // avoid a divide by zero error!
        {
            height = 1;
        }
 
        final float h = (float) width / (float) height;
 
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(45.0f, h, 1.0, 20.0);
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
    }
 
 
	public void dispose(GLAutoDrawable arg0) 
	{
		System.out.println("dispose() called");
	}
}
package testJOGL;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;
 
public class JOGL_Test1 
{
    public static void main(String[] args) 
    {
    	// setup OpenGL Version 2
    	GLProfile profile = GLProfile.get(GLProfile.GL2);
    	GLCapabilities capabilities = new GLCapabilities(profile);
 
    	// The canvas is the widget that's drawn in the JFrame
    	GLCanvas glcanvas = new GLCanvas(capabilities);
    	glcanvas.addGLEventListener(new Renderer());
    	glcanvas.setSize( 640, 480 );
 
        JFrame frame = new JFrame( "Hello World" );
        frame.getContentPane().add( glcanvas);
 
        // shutdown the program on windows close event
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent ev) {
                System.exit(0);
            }
        });
 
        frame.setSize( frame.getContentPane().getPreferredSize() );
        frame.setVisible( true );
    }
}

The code is taken from a tutorial from http://schabby.de and other sources as I’ve tried to fix the problem myself.

Maybe someone could help me? Thx!

JOGL 1.1 is old and abandoned. Try updating to the latest JOGL 2.

I have installed JOGL 2 and imported

import javax.media.opengl.GL2;

Thought that would be enough to make sure it’s JOGL 2.

Hi

This is not caused by a bug in JOGL 2.0 but maybe something is wrong in your driver. I’m quite surprised. Which version of JOGL 2 do you use? Which graphics cards? Which driver?

I have added JOGL-2.0b633 and GLUEGEN-2.0b480 to my project. After all there cannot be a major problem with my system: both LWJGL and another tutorial on JOGL2.0 work absolutely fine.

I have discovered just one real difference in the code working properly and the code posted above:

GLProfile glp = GLProfile.getDefault();

instead of

GLProfile profile = GLProfile.get(GLProfile.GL2);

Well, my assignment is to wirte a method receiving a drawable and pushing some GL2-commands to it. As this works now, I am fine.

Thanks for your offered help, guys! ;D

Of course, GLProfile.getDefault() returns the default profile which can be different of the profile returned by GLProfile.get(GLProfile.GL2) but I’m still surprised by the result. If the default profile is the forward compatible profile, the whole immediate mode shouldn’t work.

Keep in mind that you should never store GL instances, look at JOGL user guide for more information. Good luck.