Re: NPE when getting GLDrawableFactory
Posted by François Coupal on Mar 07, 2011; 3:23pm
URL: https://forum.jogamp.org/NPE-when-getting-GLDrawableFactory-tp2276026p2646531.html
Now, a working implementation that uses Wade examples. To make it work you need the "OneTriangle" class that is shown in the examples in the wiki.
A you will see, you can "play around" with it to make it crash or make it work just by commenting certains lines, such as "frame.setVisible(true)" (line 72). If this line is missing, an NPE will be thrown. (NPE thown because of a faulty toString method, same as the post above)
Also notice the "GLProfile.initSingleton( true );" (line 31) in the static method. As discussed above, you mileage may vary depending on unknown circumstances.
**************************************
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Collections;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.media.nativewindow.AbstractGraphicsDevice;
import javax.media.opengl.DefaultGLCapabilitiesChooser;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLCapabilitiesChooser;
import javax.media.opengl.GLCapabilitiesImmutable;
import javax.media.opengl.GLContext;
import javax.media.opengl.GLDrawableFactory;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLPbuffer;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
/**
* A minimal program that draws with JOGL in an AWT Frame.
*
* @author Wade Walker
*/
public class OneTriangleAWT {
static {
GLProfile.initSingleton( true );
}
public static void main( String [] args ) {
// GLProfile glprofile = GLProfile.getDefault();
GLProfile glprofile = GLProfile.get(GLProfile.GL2);
GLCapabilities glcapabilities = new GLCapabilities( glprofile );
final GLCanvas glcanvas = new GLCanvas( glcapabilities );
glcanvas.addGLEventListener( new GLEventListener() {
@Override
public void reshape( GLAutoDrawable glautodrawable, int x, int y, int width, int height ) {
OneTriangle.setup( glautodrawable.getGL().getGL2(), width, height );
}
@Override
public void init( GLAutoDrawable glautodrawable ) {
}
@Override
public void dispose( GLAutoDrawable glautodrawable ) {
}
@Override
public void display( GLAutoDrawable glautodrawable ) {
OneTriangle.render( glautodrawable.getGL().getGL2(), glautodrawable.getWidth(), glautodrawable.getHeight() );
}
});
final Frame frame = new Frame( "One Triangle AWT" );
frame.add( glcanvas );
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent windowevent ) {
frame.remove( glcanvas );
frame.dispose();
System.exit( 0 );
}
});
frame.setSize( 640, 480 );
frame.setVisible( true );
GLDrawableFactory usine = glcanvas.getFactory();
GLCapabilitiesImmutable glci = glcanvas.getChosenGLCapabilities();
GLCapabilitiesChooser glcc = new DefaultGLCapabilitiesChooser();
AbstractGraphicsDevice agd = usine.getDefaultDevice();
GLPbuffer pbuffer = usine.createGLPbuffer(agd, glci, glcc, 256, 256, null);
GLContext context = pbuffer.getContext();
context.makeCurrent();
GL2 gl = pbuffer.getContext().getGL().getGL2();
String extensions = gl.glGetString(GL.GL_EXTENSIONS);
String[] tabExtensions = extensions.split(" ");
SortedSet<String> setExtensions = new TreeSet<String>();
Collections.addAll(setExtensions, tabExtensions);
System.out.println(setExtensions);
}
}