Error following switch to more recent JOGL build.

I’m running a fairly new MacBook under OS X.4, and I’m afraid I can’t make head nor tail of it myself, so I’ll just post it here:


Exception in thread "Thread-2" javax.media.opengl.GLException: java.lang.ClassCastException: java.nio.DirectByteBuffer
	at javax.media.opengl.Threading.invokeOnOpenGLThread(Threading.java:271)
	at javax.media.opengl.GLCanvas.maybeDoSingleThreadedWorkaround(GLCanvas.java:263)
	at javax.media.opengl.GLCanvas.display(GLCanvas.java:130)
	at com.sun.opengl.util.Animator.display(Animator.java:144)
	at com.sun.opengl.util.Animator$MainLoop.run(Animator.java:181)
	at java.lang.Thread.run(Thread.java:613)
Caused by: java.lang.ClassCastException: java.nio.DirectByteBuffer
	at com.sun.opengl.impl.macosx.MacOSXOnscreenGLDrawable.lockSurface(MacOSXOnscreenGLDrawable.java:180)
	at com.sun.opengl.impl.macosx.MacOSXOnscreenGLContext.makeCurrentImpl(MacOSXOnscreenGLContext.java:57)
	at com.sun.opengl.impl.GLContextImpl.makeCurrent(GLContextImpl.java:134)
	at com.sun.opengl.impl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:182)
	at javax.media.opengl.GLCanvas$DisplayOnEventDispatchThreadAction.run(GLCanvas.java:305)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:199)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Exception in thread "AWT-EventQueue-0" javax.media.opengl.GLException: Attempt to make the same context current twice on thread Thread[AWT-EventQueue-0,6,main]
	at com.sun.opengl.impl.GLContextLock.lock(GLContextLock.java:83)
	at com.sun.opengl.impl.GLContextImpl.makeCurrent(GLContextImpl.java:131)
	at com.sun.opengl.impl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:182)
	at javax.media.opengl.GLCanvas.maybeDoSingleThreadedWorkaround(GLCanvas.java:265)
	at javax.media.opengl.GLCanvas.display(GLCanvas.java:130)
	at javax.media.opengl.GLCanvas.paint(GLCanvas.java:142)
	at sun.awt.RepaintArea.paintComponent(RepaintArea.java:276)
	at sun.awt.RepaintArea.paint(RepaintArea.java:241)
	at apple.awt.ComponentModel.handleEvent(ComponentModel.java:251)
	at java.awt.Component.dispatchEventImpl(Component.java:4126)
	at java.awt.Component.dispatchEvent(Component.java:3885)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

Is this at all familiar? What am I doing wrong?

If it helps any, this is the precise code I was trying to run (it compiles fine)


import javax.media.opengl.* ;
import java.awt.BorderLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.nio.Buffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import javax.swing.JFrame;
import com.sun.opengl.util.BufferUtil ;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.*;

public class VertexArrayTest extends JFrame implements GLEventListener {
    
    private GL gl;
    private GLU glu;
    private GLCanvas canvas;
    private Animator anim;

    
    public VertexArrayTest() {
        canvas = new GLCanvas() ;
        canvas.addGLEventListener(this);
        
        getContentPane().add(canvas, BorderLayout.CENTER);
        setSize(500, 500);
        setTitle("Red Book Example ");
        setVisible(true);
        anim = new Animator(canvas);            // calls display() periodically
        anim.start();        
        
        this.addWindowListener(new WindowListener() {

            public void windowOpened(WindowEvent arg0) {}

            public void windowClosing(WindowEvent arg0) {
                anim.stop();
                setVisible(false);
                System.exit(0);
            }

            public void windowClosed(WindowEvent arg0) {}
            public void windowIconified(WindowEvent arg0) {}
            public void windowDeiconified(WindowEvent arg0) {}
            public void windowActivated(WindowEvent arg0) {}
            public void windowDeactivated(WindowEvent arg0) {}
        });
        
    }    
    
    public static void main(String[] args) {
        VertexArrayTest desk = new VertexArrayTest();
    }

    public void init(GLAutoDrawable arg0) {
        gl = arg0.getGL();
        glu = new GLU();
        
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

        int[] v = new int[] {25, 25,
            100, 325,
            175, 25,
            175, 325,
            250, 25,
            325, 325};
        float[] c = new float[] {
                1.0f, 0.2f, 0.2f,
                0.2f, 0.2f, 1.0f,
                0.8f, 1.0f, 0.2f,
                0.75f, 0.75f, 0.75f,
                0.35f, 0.35f, 0.35f,
                0.5f, 0.5f, 0.5f};

        IntBuffer vertices = BufferUtil.newIntBuffer(6*2);
        FloatBuffer colors = BufferUtil.newFloatBuffer(6*3);
        
        vertices.put(v);
        colors.put(c);

        
        gl.glEnableClientState (GL.GL_COLOR_ARRAY);
        gl.glEnableClientState (GL.GL_VERTEX_ARRAY);        
        
        gl.glColorPointer (3, GL.GL_FLOAT, 0, colors);
        gl.glVertexPointer (2, GL.GL_INT, 0, vertices);        
    }

    public void display(GLAutoDrawable arg0) {
        gl.glClear(GL.GL_COLOR_BUFFER_BIT );
        gl.glDrawArrays (GL.GL_TRIANGLES, 0, 6);
        gl.glFlush ();
    }

    
    public void reshape(GLAutoDrawable drawable, int x, int y, int width,
            int height) {
        gl.glViewport (0, 0, width,  height);
        gl.glMatrixMode (GL.GL_PROJECTION);
        gl.glLoadIdentity ();
        glu.gluOrtho2D (0.0, width, 0.0, height);
    }

    public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {}
    
}

It’s the most recent universal binary build I could get, but I guess this is something unusual… I’ll try running some of the demos and see if that helps.

I tested your code and it gave me a different exception than the one you reported. When you call glVertexPointer and glColorPointer, it reads in data from the buffer using its current position and limit. The way you’ve written your code, each buffer’s position is at its capacity, so it crashed. What you should do is this:


gl.glColorPointer (3, GL.GL_FLOAT, 0, colors.clear());
gl.glVertexPointer (2, GL.GL_INT, 0, vertices.clear()); 

Also, I generally recommend that you don’t hold onto a GL instance as a class variable. In this case I don’t think it’s causing you problems, but it is much safer to always call getGL() in each display(GLAutoDrawable) method.

Thanks for the pointer, but I’m almost certain that would give rise to a different kind of bug- I still get the precise same error message after I implement those fixes- heck, it still pops up if I comment out the display method’s innards entirely!

The exact source of the error message seems to be here:
http://www.koders.com/java/fid6FACC71CCFAB70D7AFF2E93E92E1309C5C36A5F4.aspx

But I don’t know why it’s complaining about more than one GL context here… did you run the app under OSX?

EDIT: The bug also seems very similar to one reported on the Xith3d forums-
http://xith.org/forum/index.php?topic=993.0

If the bug is similar to the one reported in the xith3d forums (which it does seem like), then all I can say is make sure you have up to date versions of both the jars and native libraries (for both jogl and gluegen) and that you haven’t placed anything related to that in your java system directory.

Also, I did test it on OS X (but it was 10.5.7 and not 10.4).

Right. Thanks for the help anyway.

Thing is, this IS the latest version of jogl (for all libs/jars,) whereas my previous version (from early '07) ran fine (albeit very inefficiently for some reason, hence the switch.) I guess I might have to give lwjgl a try at this point.

EDIT: typo

Ah, wait- I see what I did wrong: I was actually referring to the native libraries in an older version of the same project! My mistake.