Example jogl RCP application and plug-in

I’ve been playing around with using jogl with Eclipse RCP applications. One of the things that I did was to repackage the jogl 1.0.0 binary distrobution into an Eclipse plug-in. This makes it a lot easier to develop and run RCP applications, for example, eliminating the need to wory about native library paths, etc. If anyone wants to play with it, you can download it from: http://web.mac.com/mardy/iWeb/jogl/jogl_1.0.0.jar It includes native libs for Mac (Universal), Windows and 32bit Linux. I also put together a source plug-in so that the jogl source can be viewed from within Eclipse. I also included the gluegen generated javax.media.opengl classes. The link is: http://web.mac.com/mardy/iWeb/jogl/jogl.source_1.0.0.zip As an example of how to use all this cool stuff, I compiled a couple of classic example, torus and gears along with the SceneGrip class from Bo Majewski’s article into a simple RCP application (see attached pic). The link to the project is: http://web.mac.com/mardy/iWeb/jogl/demo.zip Just unzip it and import into an Eclipse workspace.

If anyone has any feedback to offer, please send it my way.

Enjoy!

If you’re doing serious Eclipse development, you’ll be better off using our native port of JOGL to the SWT environment. In addition to proper SWT support, I’ve just in the last few days also added Draw2D support so that you can use JOGL as a Figure instance, (eg inside GEF).

More details here:

http://opengl.j3d.org/swt/

It’s been a while, but I’ve finally gotten back to this. I took Mithrandir’s advice and tried his native SWT version of JOGL and it worked quite well. Also, some of the rendering problems that I was having before have gone away. One problem that I did run into, and have not been able to resolve is that in my test application, the canvas does not display until I resize the window once. After that, it works fine. I’m sure that it’s a newbie problem, but I’m stumped. Here is the core code:


public class TorusView extends ViewPart {
	public static final String ID = "Torus.view";

	private GLCapabilities capabilities;
	public static GLCanvas canvas;
	private static GLContext context;
        private static GLDrawable drawable;
	
	private static int rot = 0;

	public void createPartControl(Composite parent) {
		parent.setLayout(new FillLayout());
                capabilities = new GLCapabilities();
                capabilities.setDoubleBuffered(true);
                canvas = new GLCanvas(parent, SWT.NONE, capabilities, null, null);
		drawable = canvas.getGLDrawable();
		context = canvas.getGLContext();
	    
		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent event) {
				render();
			}
		});
		
		canvas.addListener(SWT.Resize, new Listener() {
			public void handleEvent(Event event) {
				Rectangle bounds = canvas.getBounds();
				float fAspect = (float) bounds.width / (float) bounds.height;
				context.makeCurrent();
				GL gl = context.getGL ();
				gl.glViewport(0, 0, bounds.width, bounds.height);
				gl.glMatrixMode(GL.GL_PROJECTION);
				gl.glLoadIdentity();
				GLU glu = new GLU();
				glu.gluPerspective(45.0f, fAspect, 0.5f, 400.0f);
				gl.glMatrixMode(GL.GL_MODELVIEW);
				gl.glLoadIdentity();
				context.release();
			}
		});
		
		context.makeCurrent();
		GL gl = context.getGL ();
		gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
		gl.glColor3f(1.0f, 0.0f, 0.0f);
		gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
		gl.glClearDepth(1.0);
		gl.glLineWidth(1);
		gl.glEnable(GL.GL_DEPTH_TEST);
		context.release();
	}

	public void setFocus() {
	}
	
	public static void render() {
		if (!canvas.isDisposed()) {
			context.makeCurrent();
			GL gl = context.getGL ();
			gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
			gl.glClearColor(.3f, .5f, .8f, 1.0f);
			gl.glLoadIdentity();
			gl.glTranslatef(0.0f, 0.0f, -10.0f);
			float frot = rot;
			gl.glRotatef(0.15f * rot, 2.0f * frot, 10.0f * frot, 1.0f);
			gl.glRotatef(0.3f * rot, 3.0f * frot, 1.0f * frot, 1.0f);
			rot++;
			gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);
			gl.glColor3f(0.9f, 0.9f, 0.9f);
			drawTorus(gl, 1, 1.9f + ((float) Math.sin((0.004f * frot))), 45, 45);
			drawable.swapBuffers();
			context.release();
		}
	}

	static void drawTorus(GL gl, float r, float R, int nsides, int rings) {
		float ringDelta = 2.0f * (float) Math.PI / rings;
		float sideDelta = 2.0f * (float) Math.PI / nsides;
		float theta = 0.0f, cosTheta = 1.0f, sinTheta = 0.0f;
		for (int i = rings - 1; i >= 0; i--) {
			float theta1 = theta + ringDelta;
			float cosTheta1 = (float) Math.cos(theta1);
			float sinTheta1 = (float) Math.sin(theta1);
			gl.glBegin(GL.GL_QUAD_STRIP);
			float phi = 0.0f;
			for (int j = nsides; j >= 0; j--) {
				phi += sideDelta;
				float cosPhi = (float) Math.cos(phi);
				float sinPhi = (float) Math.sin(phi);
				float dist = R + r * cosPhi;
				gl.glNormal3f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi);
				gl.glVertex3f(cosTheta1 * dist, -sinTheta1 * dist, r * sinPhi);
				gl.glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi);
				gl.glVertex3f(cosTheta * dist, -sinTheta * dist, r * sinPhi);
			}
			gl.glEnd();
			theta = theta1;
			cosTheta = cosTheta1;
			sinTheta = sinTheta1;
		}
	}
}