MATLAB Answers

Matlab OpenGL 3D rendering

32 views (last 30 days)
Sébastien Tosi
Sébastien Tosi on 19 May 2016
Answered: Vivek Jadye on 8 Jun 2016
I'm trying to understand how Matlab loads texture to GPU (low level) when adding images to OpenGL rendered plots. I'm thinking of stacking 2D images with some transparency to get some sort of interactive alpha blended 3D rendering. This is essentially what popular scripts such as Vol3D v2 (MatlabExchange) are doing, at least in 2D texture mode. I'm looking for a way to minimize GPU memory usage when rendering rather large 3D image stacks with this technique. What format is used internally to load the images as texture on the GPU (conversion from Matlab double to some fixed point format?). Is this approach very unefficient as compared to writing a dedicated mex file that would perform it in pure OpenGL, or should it be reasonably efficient? Here I'm not so concerned by rendering quality, rather speed and memory footprint. Also I would like to implement some advanced features such as overlaying ROIs (possibly as transparent surfaces) and being able to point click them... anybody explored this?

  2 Comments

Vivek Jadye
Vivek Jadye on 7 Jun 2016
Hi Sebastien,
MATLAB converts textures to an intermediate format (RGBA of unsigned char) before uploading them to the GPU. If you use uint8 arrays in MATLAB there should be no overhead. However, because you are trying to overlay transparent textures, the overhead will be due to the way MATLAB API accepts color and alpha data through separate channels.
Here is example code snippet that does similar stacking of textures. Note that I use surface object because it provides an easy interface to create texturemapped quadrilateral.
for i = 1 : 10,
%create a surface with
%color data as uint8 m*n*3 texture
%alphadata as uint8 m*n*1 array
s = surface(1:2,1:2,i*ones(2),...
'EdgeColor','none',...
'FaceColor','texturemap',...
'CData',uint8(255*rand(10,10,3)),...
'FaceAlpha','texturemap',...
'AlphaData',uint8(255*rand(10,10,1)));
end
This could be a good starting point for you.
Also, MATLAB uses deferred rendering for resolving transparency in the scene. If you only wish to render these textures in a certain order, you need to set SortMethod property on the axes. Please refer to Axes sortmethod property for more information.
Does that answer your query?
Walter Roberson
Walter Roberson on 8 Jun 2016
Vivek, you should post that as an Answer.

Sign in to comment.

Answers (1)

Vivek Jadye
Vivek Jadye on 8 Jun 2016
Copying my comment into the answer section.
MATLAB converts textures to an intermediate format (RGBA of unsigned char) before uploading them to the GPU. If you use uint8 arrays in MATLAB there should be no overhead. However, because you are trying to overlay transparent textures, the overhead will be due to the way MATLAB API accepts color and alpha data through separate channels.
Here is example code snippet that does similar stacking of textures. Note that I use surface object because it provides an easy interface to create texturemapped quadrilateral.
for i = 1 : 10,
%create a surface with
%color data as uint8 m*n*3 texture
%alphadata as uint8 m*n*1 array
s = surface(1:2,1:2,i*ones(2),...
'EdgeColor','none',...
'FaceColor','texturemap',...
'CData',uint8(255*rand(10,10,3)),...
'FaceAlpha','texturemap',...
'AlphaData',uint8(255*rand(10,10,1)));
end
This could be a good starting point for you.
Also, MATLAB uses deferred rendering for resolving transparency in the scene. If you only wish to render these textures in a certain order, you need to set SortMethod property on the axes. Please refer to Axes sortmethod property for more information.

  0 Comments

Sign in to comment.

Sign in to answer this question.