Simple Java2D drawings over a GLCanvas/GLJPanel problem

Hi there, i need to draw some simple 2D objects ( lines ) on the top of the
3D drawed canvas. I tried several different solutions without success ( like using tranparency ecc ecc)
without success and i ended up using a style similar to the one showed at this page:
http://www.java-gaming.org/forums/index.php?topic=8241.0 where the autoswapping of the buffer
is disabled. Calling the swapping just before the 2D drawing allow to do it correctly.

I had an old version of JoGL in which swapBuffer was not complained but after i updated two situations happen:

If i use a GLCanvas to make the drawing i “occasionally” get an error similar to:
Xlib: unexpected async reply (sequence 0xb70)!

while if i use GLJPanel as soon as the function setAutoSwapBufferMode during the initialization is called:

canvas_GLJ = new GLJCanvas( caps );
canvas_GLJ.setBounds(150, 150, 255, 255 );
canvas_GLJ.setName(“PANEL_MESH”);
canvas_GLJ.addGLEventListener( this );
canvas_GLJ.setAutoSwapBufferMode( false );

i get the exceptions:

java.lang.NullPointerException
at javax.media.opengl.GLJPanel.setAutoSwapBufferMode(GLJPanel.java:739)
at gui.MyGLPanel.(MyGLPanel.java:58)
at gui.smf_view.initGUI(smf_view.java:433)
at gui.smf_view.(smf_view.java:55)
at gui.smf_view.main(smf_view.java:370)

I have to underline that i don’t need to have high framerates or smooth visualization
since when the 2D content is drawn nothing can move anymore.

Here it is a screenshot of what i am trying to get, obtained using GLCanvas ( in one of the working moments… )
http://img66.imageshack.us/my.php?image=error2gd.jpg

P.S.
an off topic… when i updated jogl with the most recent version, i got several compilation errors asking for a further argument
in most of OpenGL commands. For example:

gl.glVertex3fv( float[] posiiton, int index);

What is this second number??!?

This is the new offset parameter now required by many calls that take an offset parameter. See the sticky topic on migrating from jogl 1.1.1 to jsr-231 for that and other changes that might be required.

I would strongly recommend against trying to use a Graphics or Graphics2D to draw into a GLCanvas. You can legally do this into a GLJPanel, but not a GLCanvas. See the JGears demo for an example of how to draw Java2D over OpenGL results.

However, this is overkill for what you are trying to do. Just disable GL_DEPTH_TEST, change your projection matrix to an orthographic view and reset your modelview matrix to the identity, and draw your lines using OpenGL. No transparency needed.

Sorry about this. The GLJPanel’s behavior isn’t clearly defined in this area. You need to call setAutoSwapBufferMode in your GLEventListener.init() method.

As you can see from the attached code, i tried to do something similar to what it is done in the jogl demo JGears, without success.
The GJLCanvas ( i don’t need prestations AT ALL!! i don’t even have an animator ) is itantiated with doublebuffer as shown here.
My application simply call the repaint() method of the componetn MyGLPanel every time a modification to the model to be displayed is done, for example,
when the 2D mesh is loaded.

GLCapabilities caps = new GLCapabilities();
caps.setDoubleBuffered( true );
caps.setHardwareAccelerated( true );
myGLPanel = new MyGLPanel( caps );
myGLPanel.addGLEventListener( myGLPanel );

Using the automatic buffer swap as in JGears and simply putting the 2D code inside paintComponent in this fashion:

	public void paintComponent(Graphics g) 
	{
		System.out.println("REPAINT CALLED");
		
		super.paintComponent(g); //HERE GL DRAWING IS CALLED
		
		System.out.println("COMPONENT");
		/* TRIANGULATION DRAWING ON THE CURRENT VISUALIZED BUFFER */
		Graphics2D g2 = (Graphics2D) getGraphics();
		g2.setColor( Color.RED );
		if( triangulation != null && Opt.DRAWTRIANGOLATION == true )
			triangulation.draw( g2 );
	}

i still can only see the grid only for a flesh of a second… nothing more…

I tried also to disable the auto buffer swapping, and inside display( GLAutoDrawable drawable ) after displaying opengl
call the swapbuffer and then draw the 2D stuff, but i get the same thing… that is nothing! :frowning:

Hope you can help me fixing this problem that is keeping me awake at night from a couple of days …

Instead of calling getGraphics() inside paintComponent(), try casting the passed-in Graphics object to a Graphics2D and doing your rendering with that.

oooo, thanks man…!
now it works perfectly…! :smiley:

However, what i did wrong? i thaught that getGraphics give simply a pointer to the current context…

What does it change in using the getGraphics and casting the current one?

Bye Andrea

I’m not 100% sure but I’ve run into similar problems before in other situations (the XTrans demo in the jogl-demos workspace in particular). I think that calling getGraphics() interacts with Swing’s internal double-buffering mechanisms somehow.