JOGL Applets and JOGLAppletLauncher

Hi all,
I was wondering if anyone could give me a bit of help with JOGL applets. I’ve been looking on jogl-demos, but I can’t really find any good example code for what I’m trying to do. It’s fairly basic really: I have a JOGL app that looks like this:


public class Viewer extends Frame implements GLEventListener
{    
	public static void main(String[] args) 
    {
    	new Viewer();
    }    
    
    public Viewer() 
    {
    	GLCanvas canvas = new GLCanvas();
        canvas.addGLEventListener(this);
        add(canvas);
        setSize(100,100);
        setLocation(200,0);
        new Animator(canvas).start();
	}

	public void init(GLAutoDrawable drawable)
	{
		GL gl = drawable.getGL();
		gl.glEnable(GL.GL_DEPTH_TEST);
		...
	}
    
    public void reshape(GLAutoDrawable d, int x, int y, int w, int h) 
    {
    	GL gl = d.getGL();
        ...
        gl.glViewport(0,0,w,h);
    }
    
    public void display(GLAutoDrawable drawable)
    {
    	GL gl = drawable.getGL();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		...
	}
}

And I want to convert this into an applet.
Firstly I guess I need to derive Viewer from JOGLAppletLauncher instead of Frame, and then implement init(), start(), stop() and destroy(). My problem is that I don’t quite understand how to map what I’ve got now onto that.
Firstly, I guess that I should attach the canvas in init():


	public void init()
	{
		GLCanvas canvas = new GLCanvas();
        canvas.addGLEventListener(this);
        add(canvas);
        setSize(100,100);
        setLocation(200,0);
        new Animator(canvas).start();
	}

However, when it comes to init(GLAutoDrawable drawable), how should this be implemented, in particular how do I get a reference to the GL object? The same with display() and reshape(), again, how do I access the GL object? I’m also unsure if the Animator will work correctly in this way, maybe not…?

One problem I have is that I’m not getting much debug/output from running the applet through a browser, so it’s difficult to know why things aren’t working. Has anyone got any tips for doing this?

I’m pretty sure I’ve got the html correct and the jars are loading etc, but I really just need a few tips on the basic structure of the code just to get me started.

Thanks a lot,

Chris.

Take a look at demos.applets.GearsApplet in the jogl-demos workspace. It’s simpler to make a JOGL-based applet than you think – no subclassing of JOGLAppletLauncher necessary. You should however refactor your OpenGL code so that you have a separate GLEventListener class which can be attached to an arbitrary GLAutoDrawable – don’t include it in the Component subclass. See demos.gears.Gears and how it’s reused in several ways in the jogl-demos workspace.

For debugging, you can enable the Java Console and/or logging in the Java Control Panel under the Advanced tab.

Also, avoid putting too much heavy code in the constructor, and rely on the applet standard design patterns (init(), start(), stop(), destroy() )

Lilian :slight_smile:

Hi,

Thanks for your help. I’ve got the Gears applet working now, so I should be able to work most things out from there. I have a couple of questions though:

I’m running the gears applet through the Eclipse applet viewer. The FPSAnimator is set to 60fps, but I noticed the applet seemed to run very slow (looks like much less than 60 fps, more like about 15). I increased the FPS to 100 which sped things up a bit (now looks more like 60fps), but now every time i move the mouse over the window, the animation will pause until I remove the mouse from the window. Also, irrespective of the FPS value, whenever I moved the window there is a long pause before things start working again. The window also moves very slowly and the CPU goes up to 100%.

Any ideas on this one? Mabye this is something to do with Eclipse - I shall try running it through a browser as well…

Cheers,
Chris

I just tried running https://jogl-demos.dev.java.net/applettest.html through firefox and I am seeing pretty much the same thing. The applet runs quite slowly - eg around 10 fps (when running the non-applet version the frame rate is around 150 fps) and when I move the browser around there are long pauses in the applet and the window movement is very slow.
Is there anything that can be done about this?
Cheers.

Sounds like the speed of application is dependant on the refresh speed of whatever container it is in. What you need to do is to make your application’s logic run at a particular speed and then have it interface with another object that communicates with the frame output.


public Application{
   public void addFrameListener ( FrameListener f ) {
      frameListeners.add(f);
      f.setUpdateBuffer(buffer); // inform the frame listener of the buffer (for example with an int [] RGB buffer the listener can create a MemoryImageSource
   }
   private void renderFrame(){
      foreach (frameListener f) {
         f.informFrameUpdate();
      }
   }
}

I find a good way to handle this is to have one thread that informs listeners of the frame which will wait until notified; so instead of that foreach loop you notify a thread that does that.

As for moving the browser around, is that expected behaviour, will your user be using your app while moving the window?

Ok, it sounds about right that the refresh rate would be dependent on the container app refresh rate. I will definitely make sure that the app logic is separate from the frame refresh.
However, this still doesn’t explain why a specified 60fps appears as only around about 10fps, and why if the frame rate is increased then the applet freezes when the mouse is moved over it.
Regarding the problems moving the browser around - well, there’s no particular need or reason for the user to do that, but inevitably somebody will! It would be nice if the entire window didn’t freeze and drag around very slowly!

Did you see the note in the JOGLAppletLauncher javadoc on disabling the Talkback agent in Mozilla / Firefox? Does doing that help?

Yes, I did. I’m pretty sure I don’t have talkback enabled/installed - although I’m not 100% sure how to check this! I’m using Firefox 2 and I though it was in “Add-ons” and it certainly isn’t there in my browser. Also I have no talkback.exe.
Let me know if I’m missing something.

Just as another addition to the discussion:
I’ve now got my own app running as an applet through the eclipse applet viewer and have exactly the same problem. It runs at about 75% the frame rate it would as a standalone app and as soon as I put the mouse over the window the frame rate drops to about 10% that of a standalone app. Strange…

Yes, it should not be doing that, but it all depends on how you are working the application. If you are always telling the frame that it must be displaying new pixels then when you move the mouse over it the mouse routine (in windows) has to store your 1,000 frames per second because that’s what windows has to do.

Just a thought.

Yes, possibly something like that is happening.
I have got slightly further now though. I’ve managed to get my app running in Firefox. This time the app runs at approx. the right speed (a bit slow, but not too bad, say 80% frame rate), however it only does this for about a second, then it pauses for a few seconds, then runs for a second etc etc.
All very strange!!

(And the same thing happens in IE too)

In my experience, in some machines, difference running an applet in FFox or IE6 is 400% faster IE6 than FFox or even applet viewer . In these machines, answering no to the question of DDraw, accelerates JOGL too…

All of this is a little confusing .

Hmm, things like that used to happen 2 years ago with old ATI cards (or drivers). are you using one of these ?

Lilian :slight_smile:

I’m a complete inexpert (and complete dummie) with Ubuntu. I’m testing my Applet build with JOGL in Ubuntu Feisty Fawn with FFox. It cannot find my main class, but it cannot neither run applet Gears Demo . It cannot find gears class. What is wrong???

You probably installed JOGL into jre/lib/ext. This is not supported. See the JOGL Users’ Guide.

Hello,
Apologies for the late reply. It seems like that was indeed the problem.
My laptop manufacturer is pretty useless when it comes to releasing new drivers so mine were about 2 years old. Installing the latest Catalyst drivers with Mobility Modder fixed the problem.
Cheers,
Chris.