Skip to main content
gamedev.net
🔒 Locked

[java] gl4java and GLAnimJPanel

Started by Candle 8 replies 950+ views
Original Post
Candle
Candle
I am trying to get a GLAnimJPanel to draw a coloured triangle, (this is a test that I plan to build on!) there are no errors when I compile it, but I get a NullPointerException:
F:\java\projects\GLAnimJPanelTest>javac GLAnimJPanelTest.java

F:\java\projects\GLAnimJPanelTest>java GLAnimJPanelTest

set fps    := 20.0 -> 50 [ms/frame]
Exception in thread "main" java.lang.NullPointerException
        at GLtest.display(GLAnimJPanelTest.java:83)
        at GLtest.(GLAnimJPanelTest.java:33)
        at GLAnimJPanelTest.main(GLAnimJPanelTest.java:17)
 
2 questions: 1) why is this happening? 2) how do I fix it? here is my code:
// awt packages
import java.awt.event.*;
import java.awt.Component;

//GL4Java classes
import gl4java.GLContext;
import gl4java.swing.GLAnimJPanel;
import gl4java.drawable.*;

// swing packages
import javax.swing.*;

public class GLAnimJPanelTest {
	public static void main(String[] args) {
		JFrame f = new JFrame("GLAnimJPanelTest");
		GLtest p = new GLtest();

		f.getContentPane().add(p);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		p.run();
		f.pack();
		f.show();
	}
}

class GLtest extends GLAnimJPanel implements KeyListener, MouseListener {
	/*----------[]--------------------------------------------------------------------------------*/
	GLtest() {
		super(true);
		init();
		display();
	}
	/*----------[]--------------------------------------------------------------------------------*/
	public void init() {

		// I don''t want stereo view
		stereoView = false;
		
		// Enable Texture Mapping
		gl.glEnable(GL_TEXTURE_2D);

		// This Will Clear The Background Color To Black
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		// Enables Clearing Of The Depth Buffer
		gl.glClearDepth(1.0);
		// The Type Of Depth Test To Do
		gl.glDepthFunc(GL_LESS);
		// Enables Depth Testing
		gl.glEnable(GL_DEPTH_TEST);
		// Enables Smooth Color Shading
		gl.glShadeModel(GL_SMOOTH);
		// Select The Projection Matrix
		gl.glMatrixMode(GL_PROJECTION);
		// Reset The Projection Matrix
		gl.glLoadIdentity();
		// Calculate The Aspect Ratio Of The Window
		glu.gluPerspective(45.0f, (float)getSize().width / (float)getSize().height, 0.1f, 100.0f);
		// Select The Modelview Matrix
		gl.glMatrixMode(GL_MODELVIEW);
	}
	/*----------[]--------------------------------------------------------------------------------*/
	public void reshape(int width, int height) {
		// Reset The Current Viewport And Perspective Transformation
		gl.glViewport(0, 0, width, height);
		// Select The Projection Matrix
		gl.glMatrixMode(GL_PROJECTION);
		// Reset The Projection Matrix
		gl.glLoadIdentity();
		// Calculate The Aspect Ratio Of The Window
		glu.gluPerspective(45.0f, (float)getSize().width / (float)getSize().height, 0.1f, 100.0f);
		// Select The Modelview Matrix
		gl.glMatrixMode(GL_MODELVIEW);
	}
	/*----------[]--------------------------------------------------------------------------------*/
	public void doCleanup() {
		cvsDispose();
	}
	/*----------[]--------------------------------------------------------------------------------*/
	public void display() {

 		if (glj.gljMakeCurrent() == false) return;
 	//	glj.gljMakeCurrent();

		gl.glTranslatef(0, 0, -8);

		gl.glBegin(GL_TRIANGLES);
		 gl.glColor3f(0f,1f,1f);
		 gl.glVertex3f(0f, 2f, 0f);
		 gl.glColor3f(1f,0f,1f);
		 gl.glVertex3f(-2f, -1f, 0f);
		 gl.glColor3f(1f,1f,0f);
		 gl.glVertex3f(2f, -1f, 0f);
		gl.glEnd();

		//Swap buffers
		glj.gljSwap();
		glj.gljFree(true);
	}
	/*----------[]--------------------------------------------------------------------------------*/
	public void keyTyped(KeyEvent e) {
		//We are not handling any keyboard events yet
	}
	public void keyPressed(KeyEvent e) {
		//We are not handling any keyboard events yet
	}
	public void keyReleased(KeyEvent e) {
		//We are not handling any keyboard events yet
	}
	
	public void mouseExited( MouseEvent evt ) { }
	public void mousePressed( MouseEvent evt ) {
		Component comp = evt.getComponent();
		if( comp.equals(this ) ) {
			requestFocus();
		}
	}
	public void mouseReleased( MouseEvent evt ) { }
	public void mouseClicked( MouseEvent evt ) {
		Component comp = evt.getComponent();
		if( comp.equals(this ) ) {
			requestFocus();
		}
	}
	/*----------[]--------------------------------------------------------------------------------*/
}
 
Sully
Sully
Please use glj.gljFree() rather than glj.gljFree(true).

Also, in your code you''ll need a gl.glLoadIdentity() prior to your translate call, otherwise after a few frames your triangle will have translated past the far clip plane and disappear!

-Ron
http://web.hypersurf.com/~sully/OpenGL/DemoBox.html
Candle
Candle
quote:
Original post by Sully
Please use glj.gljFree() rather than glj.gljFree(true).


why? what is the differance between the two? (I was just experimenting, trying to find something that works!)

edit:
This doesn't really explain why I'm getting a NullPointerException in this line:

if (glj.gljMakeCurrent() == false) return;


ok, after some more experimentation:
1) added "gl.glLoadIdentity();"
2) added "gl.glRotatef(rot =+ 5f, 0f, 1f, 0f);" to get some animation! ("rot" is a float in the scope of the class)
3) changed "glj.gljFree(true)" to "glj.gljFree()"
4) commented out the three "glj.*" lines in "display()" - it doesn't give the run-time NullPointerException but there is no visable animation. (this sorta negates change no. 3 though!)
5) after no. 4, I get a stack overflow when "cvsDispose()" is called in "doCleanup()" this is rather impressive - i.e. 3000+ lines of:
 
java.lang.StackOverflowError
at gl4java.GLContext.gljFree(GLContext.java:2649)
at gl4java.swing.GLJPanel.cvsDispose(GLJPanel.java:983)

and then this repeating *lots* of times!

at GLtest.doCleanup(GLAnimJPanelTest.java:84)
at gl4java.swing.GLJPanel.cvsDispose(GLJPanel.java:984)

so I commented the line out, and get this error on resize/exit

gljDestroy failed (free)


I would apprecate it if someone compiles and runs this code on their machine, this is to test the hypothesis that my hardware/software could quite easily be screwed (knowing my luck!).

current code is here:
www11.brinkster.com/candle12121/GLAnimJPanelTest.java

[edited by - Candle on May 11, 2003 4:51:18 AM]
Sully
Sully
Ah, you are using the swing JPanel version, so MakeCurrent, Swap, Free, etc are handled for you. Do not call them yourself!
You need to call these when using the Canvas version.

You need to add start() (typically the last line) in your init code to start the animation, and I found a typo in your rotation code, should be:

gl.glRotatef(rot += 5f, 0f, 1f, 0f);

Other than that, it works fine, with no errors (even none on exit).


(Using the source code that you linked to).
-Ron
Candle
Candle
thankyou very much :D

yeah I''m using the JPanel version... it fits better with the rest of my project! I was using GLAninCanvas, but it being awt, was swamping the rest of my window so after seeing the JPanal version...

I still get no. 5 (see my last post!), but it handles that ok (I have a GF2MX if that explanins it?)

I''d NEVER have seen that typo!

Sully
Sully
Unfortunately, the JPanel version is slow and draws the scene with a Y axis mirror, so if you can live with that, go for it.

I could be wrong, but I seriously think you should NOT be calling cvsDispose from within the context, but rather outside it. So you''d have to rework your code to add a window listener and when you detect the window closing, call cvsDispose on the JPanel. I''m pretty sure this is how it is supposed to be.

Although, like I said before, I had no errors when exiting...

-Ron
Candle
Candle
I had subconsciously noticed that:
quote:
... draws the scene with a Y axis mirror ...


but WHY?!?! it doesn't make sense!

there an OGL function that lets me multiply EVERYTHING by a matrix?

e.g.

( 1 0 0 )
( 0 -1 0 )
( 0 0 1 )

(reflect in the plane y = 0)



[edited by - Candle on May 11, 2003 9:10:01 AM]
Sully
Sully
Well...........

Here''s the deal. gl4java creates a native buffer to draw all of the ogl stuff. Then it copies this buffer to the swing component. During the copy process, the Y axis got flipped. To correct this problem, in your display section, after loading in the identity matrix, call glScalef(1,-1,1) and that should fix it. Or, better yet, put this correction in the projection matrix (just after you use gluPerspective). This way you don''t have to fix it every frame.

The copying is the main reason why it is slow as well, as it uses glCopyPixels to copy the framebuffer to the swing buffer.

If you really need the speed, you might as well give up on the swing component and work with a canvas (which has it''s own problems being a heavyweight component....)

-Ron
Sully
Sully
Hmmm, I should point out that the Y mirroring is not exactly gl4java''s fault. What you have to keep in mind is that we are dealing with two different coordinate systems here. An increasing Y value in the ogl system would head up towards the top of your screen, where as in swing graphics, an increase in the Y value heads down towards the bottom of your screen.

So, taking an image that is meant to be viewed with +Y pointing up, and copying it to a system where +Y points down....obviously the image will appear mirrored.

I hope this explains it a little better.
-Ron
Candle
Candle
thanks a lot Sully - you''ve hepled me a lot

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.