How to: Getting started with JOGL

  1. I think putting this code in the reshape() will do what you want:

      public void reshape(GLDrawable gLDrawable, int x, int y, int width, int height) {
            final GL gl = gLDrawable.getGL();
            final GLU glu = gLDrawable.getGLU();

            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(GL.GL_PROJECTION);
            gl.glLoadIdentity();
            glu.gluPerspective(45.0f, h, 1.0, 20.0);
            gl.glMatrixMode(GL.GL_MODELVIEW);
            gl.glLoadIdentity();
      }

  1. The example code in this thread is extremely simple and it seems though no perspective rendering has been set up, so it could be (I didn’t check so I’m not sure at all) that it defaults to orthogonal rendering. That way you would see no depth and therefore no difference when moving the object along the z-axes until it hits either the near or far clipping planes.

  2. Just normal Java types as far as my experience goes (which isn’t that much).

I have been doing OpenGL in C++ for some time now and when I saw this site I thought about how cool it would be to make 3D applets, however I ran in to some issues.
I got it to work by putting the DLLs in the bin directory for my java run time, however it only works inside my IDE. When I make an applet and try and view it in a browser it has an error loading.
How can I make an applet with JOGL that I can put on a web page that will run seamlessly with no special installation knowledge necessary by the visitor?

The best way that I’ve found to deploy cross-platform programs using JOGL is as applications launched with Java Web Start. See for example the jogl-demos web page. I don’t have a lot of experience writing applets but I think that in order to make JOGL work in an applet it is necessary to write some kind of signed installer that will drop the dlls into the right place in the user’s JRE. Java Web Start takes care of all of this automatically and doesn’t require you to sign your application, since we sign the JOGL jars. For followups to this topic, please start a new thread.

Gregory wrote: “At this point you might want to take a look at some OpenGL resources as it won’t matter whether or not you’re coding in C++ or Java as OpenGL is OpenGL and it will be simple to tell what needs to be syntactically changed to work with JOGL (or LWJGL for that matter).”

I have been looking around but its a jungle… Could some one please guide as to where I should begin? I have done the tutorial on http://www.genedavissoftware.com/books/jogl/index.html and I understand it all… I just dont know where to go from here

I have no 3D experience - but its 3D I want to learn! basics first… Where do I begin???

You should probably get an introductory computer graphics textbook. I was taught from Foley and Van Dam’s Computer Graphics: Principles and Practice. The OpenGL “red book” is also a good introduction to using OpenGL for most common operations. Reviewing trigonometry and some linear algebra for matrix and vector manipulation is helpful. I found the most difficult thing about 3D was understanding frames of reference, but once you have an intuition about how transforms work you should be able to get new projects working quickly.

It is a simple example, and I want to say thank you and that it maks me clear.

I am Chinese student , studied in USCT(University of Science&Technology Of China).Now ,I am working in Artificial Intelligence And Calculator Application Study Room,USTC.Now, I am study the implementation of 3d turning of the Arifiical Phisical Experiment with jogl.Beleive ,I hava many chances to interchange ideas with you, and I am preparing for GRE and TOFEL.Maybe we can have a meet in some time later.

[s]This is my MSN :tonywang@mail.ustc.edu.cn

Associate with me ,and help me to promote my level ,thank you. :wink:

Argh I’m going insane, what should be completely simply totally eludes me ~.~

I am on Windows XP and use TextPad to write my java code.
I have jogl.jar and the .dll’s in C:\jogl
I have added C:\jogl\jogl.jar to CLASSPATH
When i do System.out.println(System.getProperty(“java.library.path”)); C:\jogl\ is listed (so I assume that Java can find it)
But I cannot compile anything because it won’t let me do - import net.java.games.jogl.;
I just get
"C:\jogl\JOGL2DBasics.java:6: package net.java.games.jogl does not exist
import net.java.games.jogl.
;"

Please please please could somebody help, I just don’t know what I’m doing wrong!
Thanks

May you update the tutorial to the jogl jsr-231 standard?
Thanks!

Thanks for the info about getting started. I have ran into a bit of trouble and could use some help getting around it. My problem is that when I tried to download the jogl jar etc and import it into Eclipse I kept getting stuck and have not found a clue on my own as to what I am doing wrong. I tried to do as the java download page suggested and instead the java download manager software and do it that way but for some reason i had no success either. i guess i need a bit of hand holding. if someone could post the steps to download the jogl jar and get it correctly imported into Eclipse i would appreciate it.
thanks
edward

try javax.media.opengl.*;

Hi there,
sorry for the huge post.

This code works perfectly, but, if I change the block


       gl.glBegin( GL.GL_TRIANGLES );
           gl.glVertex3f( 0.0f, 0.0f, 0.0f );
           gl.glVertex3f( 1.0f, 0.0f, 0.0f );
           gl.glVertex3f( 1.0f, 1.0f, 0.0f );
       gl.glEnd();

for


		gl.glBegin( gl.GL_QUADS );
			gl.glVertex2i(100, 150);
			gl.glVertex2i(100, 100);
			gl.glColor3f(0.0f, 0.0f, 1.0f);
			gl.glVertex2i(150, 100);
			gl.glVertex2i(150, 150);			
		gl.glEnd();

it just doen´t do nothing … (nothing but a black screen)
The only thing that changed was the fact that it is no longer a GL_TRIANGLE and it is glVertex2i instead of glVertex3f.
I am a newbie on OpenGL, so I would think it is wierd if I missed something on the rest of the code …

Thanks for your attention and time,

Have a nice day,

  • Inácio Ferrarini.

Here’s some help for you:

http://www.opengl.org/documentation/red_book_1.0/

Im not giving you the straight answer because it’s very easy to find it in the book above.

I’ve used JOGL for about an hour, and I have no experience with OpenGL so some of the changes I made may be totally screwy. Please take them with a grain of salt, or better yet, post an improvement. I’ve attempted to bring Gregory Pierce’s original example up to date with the JSR-231 release of JOGL as Pegasus requested.

Gregory (back in July 2003) used a factory to get the GLCanvas. I am using the latest release (JSR-231 beta 02 build) and these methods no longer exist. In addition, the demos that are distributed with this release use a simple default constructor call to instantiate a GLCanvas object. So I did that also.

The original code also stored the GL object in an instance variable. According to the JOGL User’s Guide this isn’t recommended because of potential threading conflicts. I realize that the initial code example won’t have any multithreading issues, but it is a simple change that may help noobies use best practices from the start. Here’s the snippet from the user’s guide:

[quote]It is strongly recommended that applications always refetch the GL object out of the GLAutoDrawable upon each call to the init(), display() and reshape() methods and pass the GL object down on the stack to any drawing routines, as opposed to storing the GL in a field and referencing it from there. The reason is that multithreading issues inherent to the AWT toolkit make it difficult to reason about which threads certain operations are occurring on, and if the GL object is stored in a field it is unfortunately too easy to accidentally make OpenGL calls from a thread that does not have a current context. This will usually cause the application to crash.
[/quote]
Here is the revised code. Copy it into a file named Trangle.java.


import java.awt.*;
import java.awt.event.*;
import java.util.logging.*;
import javax.media.opengl.*;

import com.sun.opengl.utils.Animator;

public class Triangle implements GLEventListener {

	// Statics -----------------------------------------------------------------

	private final Logger LOG = Logger.getLogger(Triangle.class.getName());

	public static void main(String[] args) {
		Frame frame = new Frame("Triangle Demo");

		GLCanvas canvas = new GLCanvas();
		canvas.addGLEventListener(new Triangle());

		frame.add(canvas);
		frame.setSize(512, 512);

		final Animator animator = new Animator(canvas);
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				new Thread(new Runnable() {
					public void run() {
						animator.stop();
						System.exit(0);
					}
				}).start();
			}
		});
		frame.setVisible(true);
		animator.start();
	}

	// Constructors ------------------------------------------------------------

	public Triangle() {
	}

	// GLEventListener ---------------------------------------------------------

	public void init(GLAutoDrawable drawable) {
		GL gl = drawable.getGL();
		if (LOG.isLoggable(Level.FINE))
			LOG.fine("Init GL is " + gl.getClass().getName());
	}

	public void display(GLAutoDrawable drawable) {
		GL gl = drawable.getGL();
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity();
		gl.glColor3f(1F, 0F, 0F);

		gl.glBegin(GL.GL_TRIANGLES);

		gl.glVertex3f(0.0F, 0.0F, 0.0F);
		gl.glVertex3f(1.0F, 0.0F, 0.0F);
		gl.glVertex3f(1.0F, 1.0F, 0.0F);

		gl.glEnd();
	}

	public void reshape(GLAutoDrawable drawable, int i, int i1, int i2, int i3) {
	}

	public void displayChanged(GLAutoDrawable drawable, boolean b, boolean b1) {
	}
}

Some people were experiencing difficulties getting the code compiled or running. I downloaded the latest version of JOGL which was JSR-231 beta 02 build at the time I wrote this. Download jogj.jar to your local system.

Also, download the native library that is specific to your runtime environment. I’m using Windows, so I downloaded jogl-natives-win32.jar. This jar file contains DLL’s that the Java Runtime environment must be able to load. I’m not sure why they are distributed as a jar file since that typically implies that the archive should remain compressed, however in our case, we want to expand the DLLs from within this archive. As far as I know, Java cannot load library files (DLLs in this case) from inside an archive. Use Java’s jar utility to expand the contents of the native archive into a directory somewhere. You can also use any tool that can extract files from a ZIP archive. I ended up extracting 3 DLL files from the archive and put them in a local directory.

At this point, you have the jogl.jar (mine is in c:\jogl) and some native files in a directory (mine are in c:\jogl\native). You also should have the source code example in a file called Triangle.java (mine is in c:\jogl\examples\Triangle.java). To compile the source code from the command line (again in Windows). I’m in the c:\jogl\examples directory when I execute this:


javac -classpath c:\jogl\jogl.jar Triangle.java

When the compile finishes, there are 3 class files in the directory now. The main class is Triangle.class, and the other two classes are inner classes. If you followed Gregory Pierce’s original suggestion and placed you native files in your Java environment’s extension directory you can simply run the application using the following:


java -classpath c:\jogl\jogl.jar ;. Triangle

Usually, three things happen at this point. The app runs, hurrah. Or, the app fails with a “Exception in thread “main” java.lang.NoClassDefFoundError: Triangle” message. Or the application fails with a “Exception in thread “main” java.lang.UnsatisfiedLinkError: no jogl in java.library.path” error.

If the app ran fine the first time, congratulations. You have caught up with me and we are both about to wade waist deep into the exciting and sometimes scary world of 3D development.

If you received the “Exception in thread “main” java.lang.NoClassDefFoundError: Triangle” error, fear not. You probably didn’t notice the “;.” I added on to the classpath attribute. In order for the Java runtime to discover the Triangle.class file, we have to add the directory this file resides in to the classpath. On Linux and Mac machines, the directories on the classpath are separated with a colon “:” instead of a semi-colon “;”.

If you received the “Exception in thread “main” java.lang.UnsatisfiedLinkError: no jogl in java.library.path” error, it indicates that the Java runtime was not able to find the native files that JOGL required. It also means that you didn’t copy these files into your Java environment’s extension directory. I don’t usually do that either. I like to explicitly reference native libraries instead of the system finding them by magic. It especially helps to be explicit when you have multiple versions of native libraries in use like you might as the JOGL libraries evolve over time.

The Java runtime looks for the native files in the directories specified by the system property named “java.libary.path”. This system library path can be set in many ways, but the easiest is on the command line as you execute the program:


\java -Djava.library.path=c:\jogl\native -classpath c:\jogl\jogl.jar ;. Triangle

I’ve uploaded an example of what you should see on your screen when this runs successfully. If you are as green as I am, the next thing to learn is why is the triangle positioned there? Why do we see it from this direction? How do we rotate a viewpoint around the camera. I’ve got so much to learn! Be sure to download the demos and their source. I feel that this will be an invaluable resource.

Other starting points that I am about to read are:
The OpenGL Redbook
Some intro code snippets at NeonHelium
and these forums, of course.

Jim Cook

thanks oravecz, helped alot, im just starting JOGL today with not much experience of OpenGL.

I ve fixed some of those old tutorials on nehe (replaced that GLAutoDrawable).

All works fine except one thing:when i try to use glTranslateF and translate on z axis, i cant see anything. I translate bothj backwards and forwards, but nothing is visible.

The rest of the code worked fine with old jogl(gltranslatef 2)-examples 6 and 7.

Is there something else i m missing?

Think of your GLCanvas as a cube. If you use eye coordinates, it will always stretch from -1.0 to 1.0 in every direction you can think of. Therefore, anything drawn outside of that area will be clipped off of the viewspace.

But wait! Don’t the NeHe tutorials translate the eye over to -1.5, and then to 1.5!? Yup.

So what’s missing?

In a word, glOrtho() is missing. What this function effectively does is widen the cube in which you can draw.

In short, you’re drawing outside the box. glOrtho will make the box larger. :slight_smile:

Just write “GLU glu = new GLU();” at the “GLU declaration” point in your program, then use it wherever necessary.

Take a look at the source code for the InfiniteShadowVolumes and other demos in the jogl-demos project.

Slightly revised so that the package name corresponds to the usage with the latest beta 05 release.


import java.awt.*;
import java.awt.event.*;
import java.util.logging.*;
import javax.media.opengl.*;

import com.sun.opengl.util.Animator;

public class Triangle implements GLEventListener {

	// Statics -----------------------------------------------------------------

	private final Logger LOG = Logger.getLogger(Triangle.class.getName());

	public static void main(String[] args) {
		Frame frame = new Frame("Triangle Demo");

		GLCanvas canvas = new GLCanvas();
		canvas.addGLEventListener(new Triangle());

		frame.add(canvas);
		frame.setSize(512, 512);

		final Animator animator = new Animator(canvas);
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				new Thread(new Runnable() {
					public void run() {
						animator.stop();
						System.exit(0);
					}
				}).start();
			}
		});
		frame.setVisible(true);
		animator.start();
	}

	// Constructors ------------------------------------------------------------

	public Triangle() {
	}

	// GLEventListener ---------------------------------------------------------

	public void init(GLAutoDrawable drawable) {
		GL gl = drawable.getGL();
		if (LOG.isLoggable(Level.FINE))
			LOG.fine("Init GL is " + gl.getClass().getName());
	}

	public void display(GLAutoDrawable drawable) {
		GL gl = drawable.getGL();
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity();
		gl.glColor3f(1F, 0F, 0F);

		gl.glBegin(GL.GL_TRIANGLES);

		gl.glVertex3f(0.0F, 0.0F, 0.0F);
		gl.glVertex3f(1.0F, 0.0F, 0.0F);
		gl.glVertex3f(1.0F, 1.0F, 0.0F);

		gl.glEnd();
	}

	public void reshape(GLAutoDrawable drawable, int i, int i1, int i2, int i3) {
	}

	public void displayChanged(GLAutoDrawable drawable, boolean b, boolean b1) {
	}
}

I recently discovered that change from Jogl to Jogl JSR 231 myself. The old advice with GL and GLU handles was not to cache them but to refresh them from the GLDrawable wherever the GLEventListener methods were called. Now that the GLU handle is not sourced from the GLDrawable, can it be cached now or should it be created fresh wherever it is used?

It can be cached but you need to keep in mind that the GLU implementation is not thread-safe so you should make sure the GLU instance can not be shared between threads. In general this probably means one GLU per GLEventListener instance. The new GLU now fetches the OpenGL pipeline from the current context using GLU.getCurrentGL() which is how it was decoupled from the GLDrawable without adding GL parameters everywhere to the method signatures.