Using OpenGL in Matlab with Java?

In matlab i have

import javax.media.opengl.GL; 

How to use OpenGL now? Can someone provide a very small sample?

Please note: if it was not in Matlab, that would be easy. But the question is specifically related to using this in Matlab.

+6
source share
2 answers

MATLAB comes with the JOGL 1.x libraries available on its static class path, so you need to compile the source code (with these JAR files in the class path) and then run the program inside MATLAB.

The following is an example of OpenGL "hello world" in Java. I show how to compile and run it directly from within 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 can try calling Java commands directly in MATLAB (as @DarkByte showed), but at some point you need to handle OpenGL events by implementing the GLEventListener interface GLEventListener : init, display, reshape, etc. As you cannot define Java classes directly in MATLAB, you could also write everything in Java, like me.

+8
source

Some information from this link :

  • In Matlab you do not need to introduce β€œ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 without input, placing () after its name. This is optional in Matlab.

A small 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.

0
source

Source: https://habr.com/ru/post/898075/


All Articles