Demo Request, Swing and jogl

Could someone of the advanced users hack together a simpe demo that looks like …


Window Layout:

----------------------
| Opengl Window           |
----------------------
| some swing buttons |
----------------------

I want to write some opengl stuff and when i click on the buttons the contents of the gl window should change.

I have modified nehes lesson 3 that it looks what i wanted to look:


/*
 * Lesson03.java
 *
 * Created on July 14, 2003, 12:35 PM
 */

import java.awt.*;
import java.awt.event.*;

import net.java.games.jogl.*;

import javax.swing.*;

/** Port of the NeHe OpenGL Tutorial (Lesson 3: Colors)
 * to Java using the Jogl interface to OpenGL.  Jogl can be obtained
 * at http://jogl.dev.java.net/
 *
 * @author Kevin Duling (jattier@hotmail.com)
 */
public class Lesson03 {
    static class Renderer
            implements GLEventListener,
            KeyListener {
        /** Called by the drawable to initiate OpenGL rendering by the client.
         * After all GLEventListeners have been notified of a display event, the
         * drawable will swap its buffers if necessary.
         * @param gLDrawable The GLDrawable object.
         */
        public void display(GLDrawable gLDrawable) {
            final GL gl = gLDrawable.getGL();
            gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
            gl.glLoadIdentity();
            gl.glTranslatef(-1.5f, 0.0f, -6.0f);
            gl.glBegin(GL.GL_TRIANGLES);                // Drawing Using Triangles
            gl.glColor3f(1.0f, 0.0f, 0.0f);   // Set the current drawing color to red
            gl.glVertex3f(0.0f, 1.0f, 0.0f);      // Top
            gl.glColor3f(0.0f, 1.0f, 0.0f);   // Set the current drawing color to green
            gl.glVertex3f(-1.0f, -1.0f, 0.0f);      // Bottom Left
            gl.glColor3f(0.0f, 0.0f, 1.0f);   // Set the current drawing color to blue
            gl.glVertex3f(1.0f, -1.0f, 0.0f);      // Bottom Right
            gl.glEnd();                        // Finished Drawing The Triangle
            gl.glTranslatef(3.0f, 0.0f, 0.0f);
            gl.glBegin(GL.GL_QUADS);                 // Draw A Quad
            gl.glColor3f(0.5f, 0.5f, 1.0f);   // Set the current drawing color to light blue
            gl.glVertex3f(-1.0f, 1.0f, 0.0f);      // Top Left
            gl.glVertex3f(1.0f, 1.0f, 0.0f);      // Top Right
            gl.glVertex3f(1.0f, -1.0f, 0.0f);      // Bottom Right
            gl.glVertex3f(-1.0f, -1.0f, 0.0f);      // Bottom Left
            gl.glEnd();                        // Done Drawing The Quad
            gl.glFlush();
        }


        /** Called when the display mode has been changed.  <B>!! CURRENTLY UNIMPLEMENTED IN JOGL !!</B>
         * @param gLDrawable The GLDrawable object.
         * @param modeChanged Indicates if the video mode has changed.
         * @param deviceChanged Indicates if the video device has changed.
         */
        public void displayChanged(GLDrawable gLDrawable, boolean modeChanged, boolean deviceChanged) {
        }

        /** Called by the drawable immediately after the OpenGL context is
         * initialized for the first time. Can be used to perform one-time OpenGL
         * initialization such as setup of lights and display lists.
         * @param gLDrawable The GLDrawable object.
         */
        public void init(GLDrawable gLDrawable) {
            final GL gl = gLDrawable.getGL();
            gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
            gLDrawable.addKeyListener(this);
        }


        /** Called by the drawable during the first repaint after the component has
         * been resized. The client can update the viewport and view volume of the
         * window appropriately, for example by a call to
         * GL.glViewport(int, int, int, int); note that for convenience the component
         * has already called GL.glViewport(int, int, int, int)(x, y, width, height)
         * when this method is called, so the client may not have to do anything in
         * this method.
         * @param gLDrawable The GLDrawable object.
         * @param x The X Coordinate of the viewport rectangle.
         * @param y The Y coordinate of the viewport rectanble.
         * @param width The new width of the window.
         * @param height The new height of the window.
         */
        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();
        }

        /** Invoked when a key has been pressed.
         * See the class description for {@link KeyEvent} for a definition of
         * a key pressed event.
         * @param e The KeyEvent.
         */
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
                System.exit(0);
        }

        /** Invoked when a key has been released.
         * See the class description for {@link KeyEvent} for a definition of
         * a key released event.
         * @param e The KeyEvent.
         */
        public void keyReleased(KeyEvent e) {
        }

        /** Invoked when a key has been typed.
         * See the class description for {@link KeyEvent} for a definition of
         * a key typed event.
         * @param e The KeyEvent.
         */
        public void keyTyped(KeyEvent e) {
        }
    }

    /** Program's main entry point
     * @param args command line arguments.
     */
    public static void main(String[] args) {
        JFrame frame = new JFrame("Lesson 3: Colors");
        GLCanvas c = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
        c.addGLEventListener(new Renderer());

        JPanel glpanel = new JPanel(new BorderLayout());
        glpanel.add(c, BorderLayout.CENTER);

        JPanel buttonpanel = new JPanel(new FlowLayout());
        buttonpanel.add(new JButton("Button 1"));
        buttonpanel.add(new JButton("Button 2"));
        buttonpanel.add(new JButton("Button 3"));


        JPanel main = new JPanel(new BorderLayout());
        main.add(glpanel, BorderLayout.CENTER);
        main.add(buttonpanel, BorderLayout.SOUTH);

        frame.getContentPane().add(main);
        frame.setSize(640, 480);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.show();
        c.requestFocus();
    }
}

I’ve been having little luck in using a Swing control in one window to manipulate a scene in a different GL window. I’m trying this to avoid some of the issues mentioned on this group in mixing the two in the same Frame.

I see two problems -

1 - poor performance on OS X, very poor on Win2K. The awt thread does not seem to take much cpu time, but it seems like the repaint of the GLCanvas takes a long long time. Direct manipulation through a mouse in the GL window itself is instantaneous, as is the manipulation of the control by itself.

2 - lack of anything displayed at all on Win2K or XP when there is much object complexity. Once I saw the the GL canvas come alive afer 2-3 minutes when the objects were the three gears from the Gears demo.

The first problem A is shown in a modified form of the example in this thread and posted at:

http://agileimage.com/html/statistics/Lesson03.jnlp

The jar file has the source as well.

The problem with the lack of any display seems to happen both with an Animator running freely and when repaint is used. I’ll put an example of that up when I get time. I’ve set the doubleBuffer capability to false on the PC with no effect.

I’d like to find some way to get this working. Any pointers to parts of jogl to look at would be appreciated. Working demos that use both Swing and jogl and which run well would be even better.

Thanks,
Paul Jensen

As I mentioned in an earlier post, there seem to be problems with GLCanvas and AWT or Swing - even in an independent windows - on a Win2K system.

I’ve posted a simple modification to the Gears demo that instantiates a Swing Slider in another window to control the rotation of the gears. There are Java Web Start files to run it requesting either double or single buffered GL capabilities.

Don’t run it on a system you don’t want to crash. It’s behavior is not repeatable.

I can run it and it looks normal for a few times, but resizing the GL window can hang the PC. Sometimes I get a blank screen, sometme the performance is slow, sometimes everything looks fine. But repeated execution of the same application gives unpredictable results.

The double and single buffered versions are at:

<http://agileimage.com/html/statistics/Gears.jnlp>
<http://agileimage.com/html/statistics/Gears_single_buffered.jnlp>

The source is included in the jar file.

Any help would be appreciated.
pdj

Both those webstart links give me a resource not found for https://jogl.dev.java.net/webstart/jogl.jnlp :-/
Although the real reason seems to be

JNLPException[category: Download Error : Exception: javax.net.ssl.SSLHandshakeException: Couldn't find trusted certificate : LaunchDesc: null ]

      at com.sun.javaws.cache.DownloadProtocol.doDownload(Unknown Source)

      at com.sun.javaws.cache.DownloadProtocol.getExtension(Unknown Source)

      at com.sun.javaws.LaunchDownload.downloadExtensionsHelper(Unknown Source)

      at com.sun.javaws.LaunchDownload.downloadExtensions(Unknown Source)

      at com.sun.javaws.Launcher.downloadResources(Unknown Source)

      at com.sun.javaws.Launcher.handleApplicationDesc(Unknown Source)

      at com.sun.javaws.Launcher.handleLaunchFile(Unknown Source)

      at com.sun.javaws.Launcher.run(Unknown Source)

      at java.lang.Thread.run(Thread.java:536)

Have you perhaps tried V-Script (in sig)? I had problems with the entire app disappearing on canvas resizing, but a later version of Jogl (and perhaps newer drivers) fixed that. Other than that all the windows systems I’ve tried it on (XP and 2k) have been ok. What graphics card are you actually running it on?