Access OpenGL from MATLAB
Access OpenGL from MATLAB
Access the OpenGL rendering pipeline directly from matlab.
No mex file, toolbox, or any other external library needed.
Examples
A few examples are included, see doc/GettingStarted.mlx
for a brief description of some of them. You can open the files and see the inner workings.
3D Viewer Example
Fractal Viewer Example
Making your own application
The main starting point is the class GLController
(or glmu.GLController
to use the functions in the glmu package). Set it as a superclass for your class that manages the rendering.
You need to define the following abstract methods. In each of these methods, the gl
argument is the current GL context.
-
InitFcn(obj,gl,varargin)
- Called once when you run
canvas.Init(arg1, arg2, ...)
in your class initialization method. - This should contain your opengl initialization stuff.
- Called once when you run
-
UpdateFcn(obj,gl)
- Called everytime
canvas.Update()
is called. It has some built in stuff to resize when needed and to skip updates that saturate the render process. - This should contain your opengl render pipeline for each frame update requested.
- Called everytime
-
ResizeFcn(obj,gl,sz)
- Called once after initialization, just before the first frame update, and when the user changes the window size.
- Always called just before the
UpdateFcn(...)
, when needed. - This should contain your opengl resize pipeline.
- This one is optionnal. If not set, it defaults to
gl.glViewport(0,0,sz(1),sz(2));
There are two ways to setup the render process.
All in one way
Make a GLController
subclass like so:
classdef myApp < GLController
methods
function obj = myApp()
frame = JFrame('myApp'); % create java frame
canvas = GLCanvas('GL3'); % create glcanvas
frame.add(canvas); % add glcanvas
obj.setGLCanvas(canvas); % set obj as the canvas controller
canvas.Init;
end
function InitFcn(obj,gl)
%...
end
function UpdateFcn(obj,gl)
%...
end
end
end
Separate controller way
If you don't want to end with a huge class, you can make a separate GLController just for the Init, Resize and Update functions.
Make the GLController
subclass:
classdef myController < GLController
methods
function InitFcn(obj,gl)
%...
end
function UpdateFcn(obj,gl)
%...
end
end
end
Then, in the main application, you can create the window with:
frame = JFrame('WindowName'); % create java frame
canvas = GLCanvas('GL3'); % create glcanvas
frame.add(canvas); % add glcanvas
ctrl = myController; % construct the controller
ctrl.setGLCanvas(canvas); % assign the canvas
canvas.Init;
InitFcn
, UpdateFcn
or ResizeFcn
Using gl when not in You can call gl commands when not inside one of these functions. To do so, use [gl,temp] = canvas.getContext
. The temp output argument is the context lock and must be requested. When temp goes out of scope, gl commands will throw errors or run without doing anything.
GL Matlab Utility (glmu)
An utility package is included in gl\+glmu
. To use this package, the controller must be a glmu.GLController
. The more advanced examples use it.
It is not needed to make your own application. However, it helps a lot to abstract some opengl stuff. Feel free to make your own or to contribute to this project!
Addons using matogl
If you make and publish an addon using matogl, I would be happy to add it to this list.
Cite As
Francis Plamondon (2024). Access OpenGL from MATLAB (https://github.com/fr0nkk/matogl/releases/tag/v3.1.2), GitHub. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxTags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
examples
gl
gl/+glmu
gl/+glmu/+drawable
gl/+glmu/+internal
utils
utils/java
doc
Version | Published | Release Notes | |
---|---|---|---|
3.1.2.0 | See release notes for this release on GitHub: https://github.com/fr0nkk/matogl/releases/tag/v3.1.2 |
|
|
3.1.1.0 | See release notes for this release on GitHub: https://github.com/fr0nkk/matogl/releases/tag/v3.1.1 |
|
|
3.0.0.0 | See release notes for this release on GitHub: https://github.com/fr0nkk/matogl/releases/tag/v3.0.0 |
||
2.2.0 | See release notes for this release on GitHub: https://github.com/fr0nkk/matogl/releases/tag/v2.2.0 |
||
2.1.1 | See release notes for this release on GitHub: https://github.com/fr0nkk/matogl/releases/tag/v2.1.1 |
||
2.1.0 | See release notes for this release on GitHub: https://github.com/fr0nkk/matogl/releases/tag/v2.1.0 |
||
2.0.0 | See release notes for this release on GitHub: https://github.com/fr0nkk/matogl/releases/tag/v2.0.0 |
||
1.2.0 | See release notes for this release on GitHub: https://github.com/fr0nkk/matogl/releases/tag/v1.2.0 |
||
1.1.0 | See release notes for this release on GitHub: https://github.com/fr0nkk/matogl/releases/tag/v1.1.0 |
||
1.0.0 |