Sep 23, 2009

Oops! Matlab gives OpenGL Error !!!

In matlab, do you get error when you prompt:
[MATLAB] >> opengl info
If yes, you have to link your opengl libraries. Firstly, look at your libraries :
[TERMINAL] >> cd /usr/lib
[TERMINAL] >> ls *GL*

you have some libGL, libGLU libraries. Here is my results:
libEGL.so libGL.so libQtOpenGL.prl
libGLcore.so.1 libGL.so.1 libQtOpenGL.so
libGLcore.so.177.82 libGL.so.177.82 libQtOpenGL.so.4
libGLESv2.so libGLU.a libQtOpenGL.so.4.4
libGLEW.so.1.5 libGLU.so libQtOpenGL.so.4.4.3
libGLEW.so.1.5.0 libGLU.so.1
libGL.la libGLU.so.1.3.070200

You can get the correct version of these libraries by setting up MATLAB to use your native Linux "libGL.so" file and the generic "libGLU.so" file that is shipped with MATLAB in the $MATLABROOT/sys/opengl/lib/glnx86 directory (where $MATLAB is the root MATLAB directory).

Consult your system documentation to identify the location of your native Linux OpenGL libraries. They are files or symbolic links called "libGL.so" and they are usually located in a directory such as /usr/lib or a similar directory. This native library may indicate Mesa software OpenGL, or may indicate a library that is specifically accelerated for your hardware. Later in this resolution, you will see how to determine which MATLAB is using.

Once you have identified these libraries, for example: /usr/lib/libGL.so and [YOUR_MATLAB_PATH]/sys/opengl/lib/glnx86/libGLU.so

you can create symbolic links in some_directory to these respective libraries using the UNIX linking utility. For example, assuming your native OpenGL Linux library "libGL.so" is located in /usr/lib and your MATLAB is installed in /usr/matlab, you would then do something such as this:
[TERMINAL] >> cd /usr/MATLAB_OpenGL
[TERMINAL] >> ln -s /usr/lib/libGL.so libGL.so
[TERMINAL] >> ln -s [YOUR_MATLAB_PATH]/sys/opengl/lib/glnx86/libGLU.so libGLU.so

And then edit your matlab configuration file where it is in [YOUR_MATLAB_PATH]/bin/matlab


[TERMINAL] >> sudo nano [YOUR_MATLAB_PATH]/bin/matlab

Find the line : # Determine for each platform and find the first export LD_LIBRARY_PATH , before this line , add

LD_LIBRARY_PATH=/usr/MATLAB_OpenGL:$LD_LIBRARY_PATH

And, then you can verify which OpenGL library file MATLAB is using by using the "ldd" command:

[TERMINAL] >> ldd /usr/matlab/bin/glnx86/glren.so

You can verify which renderer the libraries actually indicate by using the following command in MATLAB:

[MATLAB] >> opengl info

After you have determined what you are using for hardware, check which XVisual your MATLAB figure is using. You can use the following MATLAB commands:

[MATLAB] >> hf=figure;
set(hf,'renderer','opengl');
get(hf,'xvisual')
Output:
ans =
0x23 (TrueColor, depth 24, RGB mask 0xff0000 0xff00 0x00ff)

Now cross-reference this number against the output of the UNIX command, glxinfo:

[MATLAB] >> !glxinfo
Output:

visual x bf lv rg d st colorbuffer ax dp st accumbuffer ms cav
id dep cl sp sz l ci b ro r g b a bf th cl r g b a ns b eat
----------------------------------------------------------------------
0x23 24 tc 0 24 0 r y . 8 8 8 8 0 0 0 0 0 0 0 0 0 None
0x24 24 tc 0 24 0 r . . 8 8 8 8 0 0 0 0 0 0 0 0 0 None
0x25 24 tc 0 24 0 r y . 8 8 8 8 0 24 8 0 0 0 0 0 0 None
0x26 24 tc 0 24 0 r . . 8 8 8 8 0 24 8 0 0 0 0 0 0 None
0x27 24 tc 0 24 0 r y . 8 8 8 8 0 0 0 16 16 16 0 0 0 Slow
0x28 24 tc 0 24 0 r . . 8 8 8 8 0 0 0 16 16 16 0 0 0 Slow
0x29 24 tc 0 24 0 r y . 8 8 8 8 0 24 8 16 16 16 0 0 0 Slow
0x2a 24 tc 0 24 0 r . . 8 8 8 8 0 24 8 16 16 16 0 0 0 Slow
0x2b 24 dc 0 24 0 r y . 8 8 8 8 0 0 0 0 0 0 0 0 0 None
0x2c 24 dc 0 24 0 r . . 8 8 8 8 0 0 0 0 0 0 0 0 0 None
0x2d 24 dc 0 24 0 r y . 8 8 8 8 0 24 8 0 0 0 0 0 0 None
0x2e 24 dc 0 24 0 r . . 8 8 8 8 0 24 8 0 0 0 0 0 0 None
0x2f 24 dc 0 24 0 r y . 8 8 8 8 0 0 0 16 16 16 0 0 0 Slow
0x30 24 dc 0 24 0 r . . 8 8 8 8 0 0 0 16 16 16 0 0 0 Slow
0x31 24 dc 0 24 0 r y . 8 8 8 8 0 24 8 16 16 16 0 0 0 Slow
0x32 24 dc 0 24 0 r . . 8 8 8 8 0 24 8 16 16 16 0 0 0 Slow

You can see that a visual id of 0x23 has a depth ("dp th") of 0. This indicates that the patches do not get rendered in the proper order. The renderer is not paying attention to their depth within the monitor.

To fix this, choose the best TrueColor visual that has a depth and that has double buffering. A TrueColor visual has a "visual cl" value of "tc", and a double buffering visual has a "d b" value of "y".

In this case visual 0x25 has TrueColor, double buffering, 24bit Zbuffer ("bfsz"), and is not "slow".

The following code shows how to set this visual to all MATLAB figures. You may wish to put this line of code in a "startup.m" file:

[MATLAB] >> set(0,'defaultfigurexvisual','0x25');
and well done ! =)

References:
http://onurs.wordpress.com/tag/linux/
http://www.mathworks.com/support/solutions/en/data/1-18N21/index.html?solution=1-18N21

No comments:

Post a Comment

Visual Studio Keyboard Shortcuts

Playing with keyboard shortcuts is very interesting and reduce the headache of using the mouse again and again while programming with visu...