Even more Jogl and JavaFX

Posted By Hoyt Summers Pittman

Previously, I have talked about how to get a GLCanvas in Java FX and how to set up some simple declarative scene creation. Since then, I have figured out how to add geometry and bind attributes for animation using the JavaFX keyframing and timelining.

Firstly, lets look at some geometry. So far, I have only implemented one OpenGl primitive wrapper, the Quad.

public class Quad {
    public attribute color : FXGLColor;
    public attribute verticies :FXGLVertex[];

    public function display(drawable: GLAutoDrawable) : Void {
        var gl:GL = drawable.getGL();
        gl.glBegin(GL.GL_QUADS);
            gl.glColor3f(color.r,color.g,color.b);
            for (vert: FXGLVertex in verticies ) {
                gl.glVertex4f(vert.x,vert.y,vert.z,vert.w);
            }

        gl.glEnd();
    }

}

I have removed alot of the boiler plate from the code for this example. This code fairly straight forward, but to call the display method I extended the previous GLEventListener we implemented to iterate through a collection of elements and call display on each one.

public class DrawableEventListener extends JavaFXGLEventListener {

    public attribute drawables : Quad[];

    override attribute onDisplay  =
        function (drawable)  {
            var gl:GL = drawable.getGL();
            for (quad:Quad in drawables) {
                gl.glPushMatrix();
                quad.display(drawable);
                gl.glPopMatrix();
            }

        };

}

Currently, the listener is hard coded to want Quads, but this will soon change to be a more generic element.

Finally, we can put all three together into a simple demo.

var win:GLFrame = GLFrame {
    title:"Hello World",
    visible:true,
    glEventListeners : [
          DrawableEventListener {
                  drawables : [ Quad{
                          color: FXGLColor {
                              r:1.0.floatValue(),
                              g:0.0.floatValue(),
                              b:0.0.floatValue(),
                              a:1.0.floatValue()
                          }
                          verticies : [FXGLVertex{
                                  x:0.2.floatValue(),
                                  y:0.2.floatValue(),
                                  z:0.2.floatValue()
                              },FXGLVertex{
                                  x:0.0.floatValue(),
                                  y:0.2.floatValue(),
                                  z:0.2.floatValue()
                              },FXGLVertex{
                                  x:0.0.floatValue(),
                                  y:0.0.floatValue(),
                                  z:0.2.floatValue()
                              },FXGLVertex{
                                  x:0.2.floatValue(),
                                  y:0.0.floatValue(),
                                  z:0.2.floatValue()
                              }
                        ]                   

                      }
                    ]
                  onInit :function (drawable: GLAutoDrawable ) : Void {
                          var gl:GL = drawable.getGL();
                          gl.setSwapInterval(0);
                          gl.glClearColor(0.0.floatValue(), 1.0.floatValue(), 0.0.floatValue(),1.0.floatValue());
                          gl.glClear(GL.GL_COLOR_BUFFER_BIT);
                          gl.glClear(GL.GL_DEPTH_BUFFER_BIT);
                          gl.glColor3d(0.0, 1.0, 0.0);

                  }

          }

    ],
    width:100,
    height:100,
    onClose : function() { java.lang.System.exit( 0 ); }
}

There isn’t much magic, just a few more wrappers. The next post will have some magic however, animation!

Aug 16th, 2008

No Comments! Be The First!

Leave a Reply