Accessing openGL object

Started by dunkedbiscuit, November 16, 2011, 09:24:53 PM

Previous topic - Next topic

dunkedbiscuit

Greetings!

I'm writing a level editor using Swing and JPCT. I make a JFrame, and then fill it with a JPanel. The JPanel is provided with a reference to the framebuffer so that it draws its contents whenever it's time to update:

@Override
    public void paintComponent( Graphics g )
    {
        this.fb.display( g );
    }


The framebuffer seems to be initialising an openGL context, since I start it like so:

canvas = fb.enableGLCanvasRenderer();

My question is: how do I then access the openGL object? Reason being, whenever I call openGL code in the IPostProcessor I have attached to the framebuffer, I get a null pointer error. Any thoughts?

EgonOlsen

Which Open GL object? As long as you are using LWJGL, there is no such thing as an OpenGL instance...it's all static. Maybe you can post what you tried to do in the IPostProcessor to clarify this.

dunkedbiscuit

I suppose I am using LWJGL since I am using the openGL renderer? Hm.

Anyway, here is the code for my render function:

fb.clear();

        world.renderScene(fb);
        world.draw(fb);
        fb.update();

        fb.addPostProcessor( new IPostProcessor() {

            private boolean initialised = false;

            public boolean isInitialized()
            {
                return initialised;
            }

            public void dispose()
            {

            }

            public void process()
            {

            }

            public void init( FrameBuffer buffer )
            {
                boolean FBOEnabled = GLContext.getCapabilities().GL_EXT_framebuffer_object;
                System.out.println( FBOEnabled );
            }
        });

        fb.runPostProcessors();


The error comes in the first line of public void init: null pointer error.

EgonOlsen

Can you please post the complete log output until that point?

dunkedbiscuit

Certainly. This is run from NetBeans if that helps...

run:
Java version is: 1.6.0_24
-> support for BufferedImage
Version helper for 1.5+ initialized!
-> using BufferedImage
Software renderer (OpenGL mode) initialized
Using LWJGL's AWTGLCanvas
Loading Texture...box.jpg
New WorldProcessor created using 1 thread(s) and granularity of 1!
Exception in thread "main" java.lang.reflect.InvocationTargetException
        at java.awt.EventQueue.invokeAndWait(EventQueue.java:1043)
        at javax.swing.SwingUtilities.invokeAndWait(SwingUtilities.java:1326)
        at racer.Main.loop(Main.java:171)
        at racer.Main.main(Main.java:64)
Caused by: java.lang.NullPointerException
        at racer.Main$5.init(Main.java:268)
        at com.threed.jpct.FrameBuffer.runPostProcessors(Unknown Source)
        at racer.Main.render(Main.java:278)
        at racer.Main$4.run(Main.java:185)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:199)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:642)
        at java.awt.EventQueue.access$000(EventQueue.java:85)
        at java.awt.EventQueue$1.run(EventQueue.java:603)
        at java.awt.EventQueue$1.run(EventQueue.java:601)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:612)
        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)

EgonOlsen

I *think* that is caused by the way you are using display()...that's actually not the way it's meant to be used when using the AWTGLRenderer. The painting of the canvas happens magically (you might have to call repaint(); on it to actually make it happen). display() doesn't draw anything when used in combination with the AWTGLRenderer. All it does, is to create and buffer the render commands, so that the canvas itself can access them asynchronously in the AWT EDT. In your case, you are making render() and display() asynchronous, which is asking for trouble anyway. Try to remove the call to display() from paintComponent and move it into the render()-method instead. As said, you might have to add a canvas.repaint(); too. Have a look at the AWT-HelloWorld for a simple example.

dunkedbiscuit

Success!  ;D I've removed the paintComponent method from the JPanel I extended, and passed it the canvas object instead of the framebuffer. Thanks to the example I also found the right way to get it to draw: use fb.updateGLOnly() and then canvas.repaint(). Everything else regarding key and mouse events on the JPanel works as before (once I changed the listeners to work on the canvas rather than the JPanel itself). Thanks for your help!!!

But er... one last thing  ;) You wouldn't happen to know how to get the Graphics drawing functions to work on top of the canvas? See, I get the Graphics object from the canvas during the render method, and when I try to run drawString at any point, I cannot see the text. I suppose this has to do with the fb.updateGLOnly() bit.

EgonOlsen

As far as i know, you can't use any Java2D related stuff on top of the GL canvas.