6

In Matlab I have

import javax.media.opengl.GL;

How do I now use OpenGL? Can anyone provide a very small sample?

Please note: If this wasnt in Matlab then it would be easy. But the question specifically relates to using this in Matlab.

twerdster
  • 4,977
  • 3
  • 40
  • 70
  • `import` is java keyword not matlab – Eng.Fouad Sep 26 '11 at 14:10
  • @Eng.Fouad: It is a valid command in Matlab: http://www.mathworks.com/help/techdoc/ref/import.html – Jonas Sep 26 '11 at 14:22
  • 2
    @Eng.Fouad: As Jonas pointed out it is a valid keyword. Java can happily be used on the command line in scripts in Matlab making it easier to do code-kungfu in Matlab when the need arizes :) And the need has arizen ... – twerdster Sep 26 '11 at 14:36

2 Answers2

8

MATLAB comes with the JOGL 1.x libraries available on its static classpath, so it's a matter of compiling your source code (with those JAR files on the classpath), then running the program inside MATLAB.

Below is a "hello world" OpenGL example in Java. I show how to compile and run it directly from inside MATLAB:

HelloWorld.java

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;

public class HelloWorld implements GLEventListener {

    public static void main(String[] args) {
        Frame frame = new Frame("JOGL HelloWorld");
        GLCanvas canvas = new GLCanvas();
        canvas.addGLEventListener(new HelloWorld());
        frame.add(canvas);
        frame.setSize(300, 300);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    public void display(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
        gl.glColor3f(1.0f, 1.0f, 1.0f);
        gl.glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

        gl.glBegin(GL.GL_POLYGON);
        gl.glVertex2f(-0.5f, -0.5f);
        gl.glVertex2f(-0.5f, 0.5f);
        gl.glVertex2f(0.5f, 0.5f);
        gl.glVertex2f(0.5f, -0.5f);
        gl.glEnd();

        gl.glFlush();
    }

    public void init(GLAutoDrawable drawable) {
    }
    public void reshape(GLAutoDrawable drawable, 
        int x, int y, int width, int height) {
    }
    public void displayChanged(GLAutoDrawable drawable, 
        boolean modeChanged, boolean deviceChanged) {
    }
}

HelloWorld_compile_run.m

%# compile the Java code
jPath = fullfile(matlabroot,'java','jarext',computer('arch'));
cp = [fullfile(jPath,'jogl.jar') pathsep fullfile(jPath,'gluegen-rt.jar')];
cmd = ['javac -cp "' cp '" HelloWorld.java'];
system(cmd,'-echo')
javaaddpath(pwd)

%# run it
javaMethodEDT('main','HelloWorld','')

screenshot

You could try calling Java commands directly in MATLAB (as @DarkByte has shown), but at some point, you have to handle OpenGL events by implementing GLEventListener interface methods: init, display, reshape, etc.. As you can't define Java classes directly in MATLAB, you might as well write the whole thing in Java as I did.

Amro
  • 123,847
  • 25
  • 243
  • 454
  • Nice answer. Thanks. I was hoping this would give me some way to access the underlying z buffer of a Matlab figure but I cant see how. I asked that in another question here: http://stackoverflow.com/questions/5963193/matlab-z-buffer-for-simulating-kinect – twerdster Sep 26 '11 at 21:37
  • @twerdster: Yes, that's a very different question. – Ben Voigt Sep 26 '11 at 21:40
  • @Ben Voigt, I dont fully agree. I think the only way to access the buffer is using a method similar to the one Amro has just described so its at least related. Ive added the link to my comment now. – twerdster Sep 26 '11 at 21:42
  • I don't think that's quite possible, though you could create a separate question, maybe someone has a different idea. BTW, I did show in a [previous answer](http://stackoverflow.com/questions/7502422/matlab-how-to-set-pixels-on-screen-efficiently/7522217#7522217) how to simulate a "drawing canvas" using an image... – Amro Sep 26 '11 at 21:48
-1

Some info from this link:

  • In matlab, you do not need to type "new"--objects are created as needed.
  • In matlab, you use 'single quotes' where you use "double quotes" in java.
  • In java you call a routine with no inputs by putting () after its name. This is unnecessary in matlab.

Little example:

import javax.swing.*

J = JFrame('Hi there')
L = JLabel('A Label');
P = J.getContentPane
P.add(L)
J.setSize(200,200);
J.setVisible(1)

And please check here for the MathWorks documentation.

DarkByte
  • 192
  • 5