Trouble getting GLDrawableFactory createGLCanvas(GLCapabilities) to work

Hi, I’m new to jogl and having trouble getting the createGLCanvas(GLCapabilities) method to work
My request for a GLCanvas:

GLCapabilities capabilities = new GLCapabilities();
GLCanvas canvas =
    GLDrawableFactory.getFactory().createGLCanvas(capabilities);

Does not work cause the GLDrawableFactory class doesn’t seem to include a createGLCanvas method.

Not sure if the concrete factory returned has that method…
haven’t found anything with that method in the jogl library…yet

Can anybody help? It would be much appreciated!

if you only want to create a GLCanvas with custom capabilities you don’t have to use the GLDrawableFactory for this. Simple pass the capabilities to the GLCanvas(glCaps) constructor.

GLCanvas javadoc

I believe the ‘createGLCanvas’ is old JOGL… as bienator said, use the GLCanvas declaration instead…or GLJPanel if necessary.

Oh, yeah I’m trying to pass a new GLCapabilities object to the constructor…
Should I initialize the GLCapabilities object with some special settings first or is just generating a new one good enough?

I can run it now using:


GLCanvas = new GLCanvas(new GLCapabilities());

I get a white screen,
but it does not draw anything.
It should though, since I created a JOGLEventListener class which implements the GLEventListener,
implementing the display function, which is as follows:


public void display(GLAutoDrawable glDrawable) {
   final GL gl = glDrawable.getGL();
   
   gl.glClear(GL.GL_COLOR_BUFFER_BIT);
   gl.glBegin(GL.GL_POINTS);
   gl.glVertex2i(100, 50);
   gl.glVertex2i(100. 130);
   gl.glVertex2i(150, 130);
   gl.glEnd();
}

I added the canvas to my frame,

bound the EventListeners to my canvas

so the display method should be invoked when I call frame.show, right?

My problem may be with how I installed the jogl library…
I am using NetBeans 6.5 on Windows XP Pro,
all I did was add JAR/Folder to my Libraries in my Project, and added the jogl.jar and the gluegen-rt.jar.
I dropped the .dlls in with my source files (.java)
I haven’t used java for a while…whats the best way to install these? I read that dropping them into the JDK lib folder may work but its dangerous to do so, so I haven’t tried it just yet, I want to avoid this if it is dangerous.

here’s the output with some exceptions:

[i]
Exception in thread “AWT-EventQueue-0” java.lang.UnsatisfiedLinkError: no jogl_awt in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1709)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1030)
at com.sun.opengl.impl.NativeLibLoader.loadLibraryInternal(NativeLibLoader.java:189)
at com.sun.opengl.impl.NativeLibLoader.access$000(NativeLibLoader.java:49)
at com.sun.opengl.impl.NativeLibLoader$DefaultAction.loadLibrary(NativeLibLoader.java:80)
at com.sun.opengl.impl.NativeLibLoader.loadLibrary(NativeLibLoader.java:103)
at com.sun.opengl.impl.NativeLibLoader.access$200(NativeLibLoader.java:49)
at com.sun.opengl.impl.NativeLibLoader$2.run(NativeLibLoader.java:132)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.opengl.impl.NativeLibLoader.loadAWTImpl(NativeLibLoader.java:118)
at com.sun.opengl.impl.JAWT.getJAWT(JAWT.java:91)
at com.sun.opengl.impl.windows.WindowsOnscreenGLDrawable.lockSurface(WindowsOnscreenGLDrawable.java:163)
at com.sun.opengl.impl.windows.WindowsOnscreenGLContext.makeCurrentImpl(WindowsOnscreenGLContext.java:57)
at com.sun.opengl.impl.GLContextImpl.makeCurrent(GLContextImpl.java:134)
at com.sun.opengl.impl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:182)
at javax.media.opengl.GLCanvas.maybeDoSingleThreadedWorkaround(GLCanvas.java:412)
at javax.media.opengl.GLCanvas.display(GLCanvas.java:244)
at javax.media.opengl.GLCanvas.paint(GLCanvas.java:277)
at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
at sun.awt.RepaintArea.paint(RepaintArea.java:224)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:306)
at java.awt.Component.dispatchEventImpl(Component.java:4577)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

[/i]

Thanks for helpin me thru this guys!

Okay, well I got rid of the exceptions in the output by dropping the gluegen-rt.dll, jogl_awt.dll, and jogl.cg.dll in the C:\Sun\SDK\bin folder.

But still do not display anything…

here’s my code as it stands, can anyone find any issues?

Main.java


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


public class Main {

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

        GLCanvas canvas = new GLCanvas(new GLCapabilities());
        frame.add(canvas);

        frame.setSize(300, 300);
        frame.setBackground(Color.WHITE);

        frame.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });

        // make a new JOGLEventListener that is tied to this cavnas
        JOGLEventListener listener = new JOGLEventListener(canvas);
        canvas.addGLEventListener(listener);
        canvas.addKeyListener(listener);
        canvas.addMouseListener(listener);
        canvas.addMouseMotionListener(listener);

        frame.show();
        
    }

}

JOGLEventListener.java


package graphtest1;

import java.awt.event.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;


public class JOGLEventListener implements GLEventListener, KeyListener, MouseListener, MouseMotionListener {

    // keep pointer to associated canvas so we can refresh the screen (equivalent to glutPostRedisplay())
    private GLCanvas canvas;

    // constructor
    public JOGLEventListener(GLCanvas canvas) {
        this.canvas = canvas;
    }

    // ...
    // event handler functions
    // ...

    public void init(GLAutoDrawable glDrawable) {
        final GL gl = glDrawable.getGL();

        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();

        gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        gl.glColor3f(0.0f, 0.0f, 0.0f);
        gl.glPointSize(4.0f);
    }

    
    public void display(GLAutoDrawable glDrawable) {
        // clear the screen
        final GL gl = glDrawable.getGL();

        gl.glClear(GL.GL_COLOR_BUFFER_BIT);

        gl.glBegin(GL.GL_POINTS);
        gl.glVertex2i(100, 50);
        gl.glVertex2i(100, 130);
        gl.glVertex2i(150, 130);
        gl.glEnd();
    }
    

    
    public void reshape(GLAutoDrawable glDrawable, int x, int y, int width, int height) {
        final GL gl = glDrawable.getGL();
        
        gl.glViewport(0, 0, width, height); // update the viewport
    }

    
    public void displayChanged(GLAutoDrawable gLDrawable, boolean modeChanged, boolean deviceChanged) {
        // do nothing
    }

    
	public void keyPressed(KeyEvent e) {
		//ignore
	}

	
	public void keyReleased(KeyEvent e) {
		//ignore
	}

	
	public void keyTyped(KeyEvent e) {
		//ignore
	}

	
	public void mouseClicked(MouseEvent mouse) {
		//ignore
        
	}

	
	public void mouseEntered(MouseEvent mouse) {
		//ignore
	}

	
	public void mouseExited(MouseEvent mouse) {
		//ignore

	}

	
	public void mousePressed(MouseEvent mouse) {
		//ignore
	}

	
	public void mouseReleased(MouseEvent mouse) {
		//ignore
	}

	
	private void setButton(int button) {
		//ignore
	}

	
	public synchronized void swapColors() {
		//ignore
	}

	
	public void mouseDragged(MouseEvent mouse) {
		//ignore
	}

    
	private int getButton() {
		//return this._button;

        return 0;
	}

	
	public void mouseMoved(MouseEvent mouse) {
		// ignore
	}
}

ok here the basics:

JOGL requires native libs to talk to OpenGL which is part of your graphics driver. To tell the JVM where it should search for the native libs you have to set the library path explicitly via a command line flag.

to do this with NetBeans you will have to create a run configuration.

  1. use the combobox below the menu bar and choose ‘customize…’
  2. add -Djava.library.path=“lib” to the VM Options text field*
  3. Ok

*-Djava.library.path=“lib” sets the path to the folder ‘lib’ it is a relative path to the project folder that means if you create a lib folder in your project folder and dump the libs into it everything should be found when you start your app.

(note; you don’t have to do this if you have the NB OpenGL Pack installed, this will be done automatically)

here a sample application from the NetBeans OpenGL Pack which does all opengl setup and renders two shapes (there are around 100 other samples to try out if you install the pack). You should take a look esp. at the methods init, and reshape. Reshape is important since it handles the setup of the viewport and the modelview matrix etc.

just ask if you have further questions.

package org.yourorghere;

import com.sun.opengl.util.Animator;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;



/**
 * SimpleJOGL.java 

 * author: Brian Paul (converted to Java by Ron Cemer and Sven Goethel) <P>
 *
 * This version is equal to Brian Paul's version 1.2 1999/10/21
 */
public class SimpleJOGL implements GLEventListener {

    public static void main(String[] args) {
        Frame frame = new Frame("Simple JOGL Application");
        GLCanvas canvas = new GLCanvas();

        canvas.addGLEventListener(new SimpleJOGL());
        frame.add(canvas);
        frame.setSize(640, 480);
        final Animator animator = new Animator(canvas);
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                // Run this on another thread than the AWT event queue to
                // make sure the call to Animator.stop() completes before
                // exiting
                new Thread(new Runnable() {

                    public void run() {
                        animator.stop();
                        System.exit(0);
                    }
                }).start();
            }
        });
        // Center frame
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        animator.start();
    }

    public void init(GLAutoDrawable drawable) {
        // Use debug pipeline
        // drawable.setGL(new DebugGL(drawable.getGL()));

        GL gl = drawable.getGL();
        System.err.println("INIT GL IS: " + gl.getClass().getName());

        // Enable VSync
        gl.setSwapInterval(1);

        // Setup the drawing area and shading mode
        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.
    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        GL gl = drawable.getGL();
        GLU glu = new GLU();

        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();
    }

    public void display(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();

        // Clear the drawing area
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        // Reset the current matrix to the "identity"
        gl.glLoadIdentity();

        // Move the "drawing cursor" around
        gl.glTranslatef(-1.5f, 0.0f, -6.0f);

        // Drawing Using Triangles
        gl.glBegin(GL.GL_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
        // Finished Drawing The Triangle
        gl.glEnd();

        // Move the "drawing cursor" to another position
        gl.glTranslatef(3.0f, 0.0f, 0.0f);
        // Draw A Quad
        gl.glBegin(GL.GL_QUADS);
            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
        // Done Drawing The Quad
        gl.glEnd();

        // Flush all drawing operations to the graphics card
        gl.glFlush();
    }

    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
    }
}


Goodness lordy!!
Thanks Bienator you are the man, thats exactly what I needed. Seems all the tutorials I had found so far were just out of date.
Thanks again

…and don’t forget to remove the jogl/gluegen files you put into your JDK :o