Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
3d graphics in Matlab vs OpenGL

Subject: 3d graphics in Matlab vs OpenGL

From: ryan

Date: 21 Feb, 2006 08:40:02

Message: 1 of 8

All,

I'm planning on looking into doing a 3d graphics playback. I was
wondering how to best accomplish this. A friend said use openGL. I
saw some cool matlab stuff done in the file exchange, and we work
with matlab anyways.

1. Can I use matlab to interface with openGL directly?
   1a. If not can I use matlab to interface with C/C++ and then use
openGL?

2. Would Matlab be able to support a 3d moving objects and such with
the same speed/quality as openGL?

Thanks

Subject: 3d graphics in Matlab vs OpenGL

From: Eric Carlson

Date: 21 Feb, 2006 19:19:59

Message: 2 of 8

Ryan wrote:
> All,
>
> I'm planning on looking into doing a 3d graphics playback. I was
> wondering how to best accomplish this. A friend said use openGL. I
> saw some cool matlab stuff done in the file exchange, and we work
> with matlab anyways.
>
> 1. Can I use matlab to interface with openGL directly?
> 1a. If not can I use matlab to interface with C/C++ and then use
> openGL?
>
> 2. Would Matlab be able to support a 3d moving objects and such with
> the same speed/quality as openGL?
>
> Thanks

In my experience MATLAB can not come close to direct programming with
OpenGL. In theory, you should have little problem interfacing to the
external routines through mex programming.

Depending on what you plan to do, MATLAB may be fast enough. Until I
start getting into scenes with hundreds of thousands of triangles, I
notice little difference.

You will NEVER find an easier system for doing interesting 3D things
than MATLAB, but this ease comes with a performance hit at this point
(for reasons I do not understand). Once you catch on to the basics of 3D
in MATLAB, you can do some very elaborate models very quickly - So
quickly that it's worth your while to test it in MATLAB before you go to
the trouble of direct OpenGL coding.

The MATLAB scene manipulation is limited compared to OpenGL (for example
in bump or texture mapping), but you can also generate some
photorealistic things.

Cheers,
Eric Carlson

Subject: 3d graphics in Matlab vs OpenGL

From: G. Dibujo

Date: 22 Feb, 2006 23:03:47

Message: 3 of 8

Eric Carlson wrote:
> ..........
> Depending on what you plan to do, MATLAB may be fast enough. Until
> I
> start getting into scenes with hundreds of thousands of triangles,
> I
> notice little difference.
>
> .........

If a scene has hundreds of thousands of triangles, then it will
certainly have over a handred thousand vertices. So my question is
how it possible to create a very large matrix like
"zeros(100000,100000)" when you try to calculate for instance the
edge matrix of a triangle mesh. Allocating memory for such a huge
matrix requires at least 8 GB of memory.

Any suggestions. Thanks.

Subject: 3d graphics in Matlab vs OpenGL

From: Eric Carlson

Date: 24 Feb, 2006 09:33:27

Message: 4 of 8

G. Dibujo wrote:
> Eric Carlson wrote:
>
>>..........
>>Depending on what you plan to do, MATLAB may be fast enough. Until
>>I
>>start getting into scenes with hundreds of thousands of triangles,
>>I
>>notice little difference.
>>
>>.........
>
>
> If a scene has hundreds of thousands of triangles, then it will
> certainly have over a handred thousand vertices. So my question is
> how it possible to create a very large matrix like
> "zeros(100000,100000)" when you try to calculate for instance the
> edge matrix of a triangle mesh. Allocating memory for such a huge
> matrix requires at least 8 GB of memory.
>
> Any suggestions. Thanks.
Hello,
Sorry for the slow reply - been away.

I am not sure what you mean exactly. My experience with MATLAB 3D
graphics deals primarily with the use of the patch command and the
associated face-vertex structure. Variations of the FV structure are
used throughout 3D graphics. Using the FV structure, you specify each
vertex X_k=(x_k y_k z_k) and associate it with index k. Each triangle
face is specified as an ordered triplet of vertex indices T = [l m n].
If you want consistent rendering behavior, then you also need to make
sure that the order of the vertices is such that cross(X_m-X_l,X_n-X_m)
is consistently pointing on one side of the surface. Not all of the
surface routines in MATLAB are consistent this way, which leads to very
aggravating coloring effects at times.

For a hypothetical case with 100,000 vertices and 100,000 triangles, you
will need 100K*3*8+100k*3*8= 4800K = 4.8 M.

These structures and more are explained in Ch 11 of my book Foundations
of Engineering with MATLAB 7 at www.otfringe.com (and will be available
as an e-book later this Spring)

Cheers,
Eric Carlson

Subject: 3d graphics in Matlab vs OpenGL

From: G. Dibujo

Date: 24 Feb, 2006 14:42:04

Message: 5 of 8

Eric Carlson wrote:
>
>
> G. Dibujo wrote:
>> Eric Carlson wrote:
>>
>>>..........
>>>Depending on what you plan to do, MATLAB may be fast enough.
> Until
>>>I
>>>start getting into scenes with hundreds of thousands of
> triangles,
>>>I
>>>notice little difference.
>>>
>>>.........
>>
>>
>> If a scene has hundreds of thousands of triangles, then it will
>> certainly have over a handred thousand vertices. So my question
> is
>> how it possible to create a very large matrix like
>> "zeros(100000,100000)" when you try to calculate for instance
the
>> edge matrix of a triangle mesh. Allocating memory for such a
huge
>> matrix requires at least 8 GB of memory.
>>
>> Any suggestions. Thanks.
> Hello,
> Sorry for the slow reply - been away.
>
> I am not sure what you mean exactly. My experience with MATLAB 3D
> graphics deals primarily with the use of the patch command and the
> associated face-vertex structure. Variations of the FV structure
> are
> used throughout 3D graphics. Using the FV structure, you specify
> each
> vertex X_k=(x_k y_k z_k) and associate it with index k. Each
> triangle
> face is specified as an ordered triplet of vertex indices T = [l m
> n].
> If you want consistent rendering behavior, then you also need to
> make
> sure that the order of the vertices is such that
> cross(X_m-X_l,X_n-X_m)
> is consistently pointing on one side of the surface. Not all of the
>
> surface routines in MATLAB are consistent this way, which leads to
> very
> aggravating coloring effects at times.
>
> For a hypothetical case with 100,000 vertices and 100,000
> triangles, you
> will need 100K*3*8+100k*3*8= 4800K = 4.8 M.
>
> These structures and more are explained in Ch 11 of my book
> Foundations
> of Engineering with MATLAB 7 at www.otfringe.com (and will be
> available
> as an e-book later this Spring)
>
> Cheers,
> Eric Carlson
>

I agree with you that a large number of vertices and triangles is not
a problem when you use the MATLAB patch command to display a 3D
triangle mesh. The problem arises when you want to compute for
instance the adjacency matrix of the mesh. An adjaceny matrix A =
(a_{ij}) is a concept from graph theory and is defined between the
mesh vertices as follows: a_{ii}=0 and a_{ij} = 1 if vertex_i is
connected to vertex_j.
Now let's assume that we have a 3D model with 100,000 vertices as
illustrated in the code segment below. My question is how to
pre-allocate a zero matrix of size 100,000x100,000 which requires 80
GB of memory:

%1st step: load a 3D model:
% Copyright: <http://lodbook.com/models/>

>> load dragon
>> whos
  Name Size Bytes Class

  faces 202520x3 4860480 double array
  vertices 100207x3 2404968 double array

% 2nd step: How to calculate the adjacency matrix

nvert = length(vertices);
nface = length(faces);
%pre-allocate a zero matrix of size nvert x nvert
A = zeros(nvert,nvert); % Requires 80 GB?
for i=1:nface
    A(faces(i,1),faces(i,2)) = 1;
    A(faces(i,2),faces(i,3)) = 1;
    A(faces(i,3),faces(i,1)) = 1;
    % make sure symmetry holds
    A(faces(i,2),faces(i,1)) = 1;
    A(faces(i,3),faces(i,2)) = 1;
    A(faces(i,1),faces(i,3)) = 1;
end
A = sparse(A);

Subject: 3d graphics in Matlab vs OpenGL

From: Eric Carlson

Date: 24 Feb, 2006 14:39:16

Message: 6 of 8

G. Dibujo wrote:
> Eric Carlson wrote:
>
>>
>>G. Dibujo wrote:
>>
>>>Eric Carlson wrote:
>>>
>>>
>>>>..........
>>>>Depending on what you plan to do, MATLAB may be fast enough.
>>
>>Until
>>
>>>>I
>>>>start getting into scenes with hundreds of thousands of
>>
>>triangles,
>>
>>>>I
>>>>notice little difference.
>>>>
>>>>.........
>>>
>>>
>>>If a scene has hundreds of thousands of triangles, then it will
>>>certainly have over a handred thousand vertices. So my question
>>
>>is
>>
>>>how it possible to create a very large matrix like
>>>"zeros(100000,100000)" when you try to calculate for instance
>
> the
>
>>>edge matrix of a triangle mesh. Allocating memory for such a
>
> huge
>
>>>matrix requires at least 8 GB of memory.
>>>
>>>Any suggestions. Thanks.
>>
>>Hello,
>>Sorry for the slow reply - been away.
>>
>>I am not sure what you mean exactly. My experience with MATLAB 3D
>>graphics deals primarily with the use of the patch command and the
>>associated face-vertex structure. Variations of the FV structure
>>are
>>used throughout 3D graphics. Using the FV structure, you specify
>>each
>>vertex X_k=(x_k y_k z_k) and associate it with index k. Each
>>triangle
>>face is specified as an ordered triplet of vertex indices T = [l m
>>n].
>>If you want consistent rendering behavior, then you also need to
>>make
>>sure that the order of the vertices is such that
>>cross(X_m-X_l,X_n-X_m)
>>is consistently pointing on one side of the surface. Not all of the
>>
>>surface routines in MATLAB are consistent this way, which leads to
>>very
>>aggravating coloring effects at times.
>>
>>For a hypothetical case with 100,000 vertices and 100,000
>>triangles, you
>>will need 100K*3*8+100k*3*8= 4800K = 4.8 M.
>>
>>These structures and more are explained in Ch 11 of my book
>>Foundations
>>of Engineering with MATLAB 7 at www.otfringe.com (and will be
>>available
>>as an e-book later this Spring)
>>
>>Cheers,
>>Eric Carlson
>>
>
>
> I agree with you that a large number of vertices and triangles is not
> a problem when you use the MATLAB patch command to display a 3D
> triangle mesh. The problem arises when you want to compute for
> instance the adjacency matrix of the mesh. An adjaceny matrix A =
> (a_{ij}) is a concept from graph theory and is defined between the
> mesh vertices as follows: a_{ii}=0 and a_{ij} = 1 if vertex_i is
> connected to vertex_j.
> Now let's assume that we have a 3D model with 100,000 vertices as
> illustrated in the code segment below. My question is how to
> pre-allocate a zero matrix of size 100,000x100,000 which requires 80
> GB of memory:
>
> %1st step: load a 3D model:
> % Copyright: <http://lodbook.com/models/>
>
>>>load dragon
>>>whos
>
> Name Size Bytes Class
>
> faces 202520x3 4860480 double array
> vertices 100207x3 2404968 double array
>
> % 2nd step: How to calculate the adjacency matrix
>
> nvert = length(vertices);
> nface = length(faces);
> %pre-allocate a zero matrix of size nvert x nvert
> A = zeros(nvert,nvert); % Requires 80 GB?
> for i=1:nface
> A(faces(i,1),faces(i,2)) = 1;
> A(faces(i,2),faces(i,3)) = 1;
> A(faces(i,3),faces(i,1)) = 1;
> % make sure symmetry holds
> A(faces(i,2),faces(i,1)) = 1;
> A(faces(i,3),faces(i,2)) = 1;
> A(faces(i,1),faces(i,3)) = 1;
> end
> A = sparse(A);
The total number of nonzeros from this matrix will be bounded by
N_triangles*2*3 (three edges per triangle X 2 for symmetry), so the
total size of A need not exceed this amount. I don't do much with
sparse matrices in MATLAB but you might try something like:

%first column IA, second JA
IA_JA = [faces(:,1) faces(:,2)
          faces(:,1) faces(: 3)
          faces(:,2) faces(:,3)
          faces(:,2) faces(:,1)
          faces(:,3) faces(:,1)
          faces(:,3) faces(:,2)];
IA_JA = unique(IA_JA, 'rows');

A=sparse(IA_JA(:,1), IA_JA(:,2),1);

Subject: 3d graphics in Matlab vs OpenGL

From: Muhammad

Date: 25 Jun, 2013 12:45:09

Message: 7 of 8

Eric Carlson <ecarlson@coe.eng.ua.edu> wrote in message <V9KLf.1089$IV4.14@fe05.lga>...
> G. Dibujo wrote:
> > Eric Carlson wrote:
> >
> >>
> >>G. Dibujo wrote:
> >>
> >>>Eric Carlson wrote:
> >>>
> >>>
> >>>>..........
> >>>>Depending on what you plan to do, MATLAB may be fast enough.
> >>
> >>Until
> >>
> >>>>I
> >>>>start getting into scenes with hundreds of thousands of
> >>
> >>triangles,
> >>
> >>>>I
> >>>>notice little difference.
> >>>>
> >>>>.........
> >>>
> >>>
> >>>If a scene has hundreds of thousands of triangles, then it will
> >>>certainly have over a handred thousand vertices. So my question
> >>
> >>is
> >>
> >>>how it possible to create a very large matrix like
> >>>"zeros(100000,100000)" when you try to calculate for instance
> >
> > the
> >
> >>>edge matrix of a triangle mesh. Allocating memory for such a
> >
> > huge
> >
> >>>matrix requires at least 8 GB of memory.
> >>>
> >>>Any suggestions. Thanks.
> >>
> >>Hello,
> >>Sorry for the slow reply - been away.
> >>
> >>I am not sure what you mean exactly. My experience with MATLAB 3D
> >>graphics deals primarily with the use of the patch command and the
> >>associated face-vertex structure. Variations of the FV structure
> >>are
> >>used throughout 3D graphics. Using the FV structure, you specify
> >>each
> >>vertex X_k=(x_k y_k z_k) and associate it with index k. Each
> >>triangle
> >>face is specified as an ordered triplet of vertex indices T = [l m
> >>n].
> >>If you want consistent rendering behavior, then you also need to
> >>make
> >>sure that the order of the vertices is such that
> >>cross(X_m-X_l,X_n-X_m)
> >>is consistently pointing on one side of the surface. Not all of the
> >>
> >>surface routines in MATLAB are consistent this way, which leads to
> >>very
> >>aggravating coloring effects at times.
> >>
> >>For a hypothetical case with 100,000 vertices and 100,000
> >>triangles, you
> >>will need 100K*3*8+100k*3*8= 4800K = 4.8 M.
> >>
> >>These structures and more are explained in Ch 11 of my book
> >>Foundations
> >>of Engineering with MATLAB 7 at www.otfringe.com (and will be
> >>available
> >>as an e-book later this Spring)
> >>
> >>Cheers,
> >>Eric Carlson
> >>
> >
> >
> > I agree with you that a large number of vertices and triangles is not
> > a problem when you use the MATLAB patch command to display a 3D
> > triangle mesh. The problem arises when you want to compute for
> > instance the adjacency matrix of the mesh. An adjaceny matrix A =
> > (a_{ij}) is a concept from graph theory and is defined between the
> > mesh vertices as follows: a_{ii}=0 and a_{ij} = 1 if vertex_i is
> > connected to vertex_j.
> > Now let's assume that we have a 3D model with 100,000 vertices as
> > illustrated in the code segment below. My question is how to
> > pre-allocate a zero matrix of size 100,000x100,000 which requires 80
> > GB of memory:
> >
> > %1st step: load a 3D model:
> > % Copyright: <http://lodbook.com/models/>
> >
> >>>load dragon
> >>>whos
> >
> > Name Size Bytes Class
> >
> > faces 202520x3 4860480 double array
> > vertices 100207x3 2404968 double array
> >
> > % 2nd step: How to calculate the adjacency matrix
> >
> > nvert = length(vertices);
> > nface = length(faces);
> > %pre-allocate a zero matrix of size nvert x nvert
> > A = zeros(nvert,nvert); % Requires 80 GB?
> > for i=1:nface
> > A(faces(i,1),faces(i,2)) = 1;
> > A(faces(i,2),faces(i,3)) = 1;
> > A(faces(i,3),faces(i,1)) = 1;
> > % make sure symmetry holds
> > A(faces(i,2),faces(i,1)) = 1;
> > A(faces(i,3),faces(i,2)) = 1;
> > A(faces(i,1),faces(i,3)) = 1;
> > end
> > A = sparse(A);
> The total number of nonzeros from this matrix will be bounded by
> N_triangles*2*3 (three edges per triangle X 2 for symmetry), so the
> total size of A need not exceed this amount. I don't do much with
> sparse matrices in MATLAB but you might try something like:
>
> %first column IA, second JA
> IA_JA = [faces(:,1) faces(:,2)
> faces(:,1) faces(: 3)
> faces(:,2) faces(:,3)
> faces(:,2) faces(:,1)
> faces(:,3) faces(:,1)
> faces(:,3) faces(:,2)];
> IA_JA = unique(IA_JA, 'rows');
>
> A=sparse(IA_JA(:,1), IA_JA(:,2),1);
>
>
>
Hi Eric,

Just a quick question for you or anyone else reading this... Is there a book/tutorial to learn graphics in matlab. I searched online, however all i could get was how to visualize data.

Is there specific reference material for Graphics similar to OpenGL in matlab??

Thanks in advance.

Asad

Subject: 3d graphics in Matlab vs OpenGL

From: Steven_Lord

Date: 25 Jun, 2013 14:12:09

Message: 8 of 8



"Muhammad " <muhammad.asad.2@city.ac.uk> wrote in message
news:kqc3cl$s1m$1@newscl01ah.mathworks.com...

*snip*

> Hi Eric,
>
> Just a quick question for you or anyone else reading this... Is there a
> book/tutorial to learn graphics in matlab. I searched online, however all
> i could get was how to visualize data.
> Is there specific reference material for Graphics similar to OpenGL in
> matlab??

By "similar to OpenGL" are you looking for Handle Graphics?

http://www.mathworks.com/help/matlab/graphics-objects.html

If not, could you clarify what you mean by "similar to OpenGL?"

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us