Using OpenGL C functions within Matlab using MEX

Hello everyone I have been working on a 3D model using Matlab functions and after finishing it I was instructed to "refine" it and make it more realistic in addition to extracting and displaying depth information using OpenGL (C) functions, I have been looking into this for the last few days and it seems possible but I just can't seem to successfully get the simplest Mex files to work because of header problems and missing files and even when I compile the Mex file Matlab crashes when I call it, I want to start by simply compiling a MEX application which creates an OpenGL window, then I assume I'll have to redo the model using OpenGL functions and attempt to display the depth buffer.
I would appreciate any help I can get. Thanks.
EDIT01: Here is an example of a code that I compiled without any problems but crashes MATLAB when I attempt to run it.
#include "mex.h"
#define GL_VIEWPORT 0x0BA2
#define GL_DEPTH_COMPONENT 0x1902
#define GL_FLOAT 0x1406
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int viewport[4], i, x, y;
int colLen;
float *data;
double *matrix;
glGetIntegerv(GL_VIEWPORT, viewport);
data = (float*)malloc(viewport[2] * viewport[3] * sizeof(float));
glReadPixels(0, 0, viewport[2], viewport[3], GL_DEPTH_COMPONENT, GL_FLOAT, data);
plhs[0] = mxCreateNumericMatrix(viewport[3], viewport[2], mxDOUBLE_CLASS, mxREAL);
matrix = mxGetPr(plhs[0]);
colLen = mxGetM(plhs[0]);
for(x = 0; x < viewport[2]; ++ x) {
for(y = 0; y < viewport[3]; ++ y)
matrix[x * colLen + y] = data[x + (viewport[3] - 1 - y) * viewport[2]];
}
free(data);
return;
}

9 个评论

How do you compile this? In which line does this crash - you simply check this by inserting some return commands?
Thnaks for the reply Simon I compile it using MinGW-w64, it crashes when I call the mex file using these simple lines
peaks;
figure(1);
depthData=mexGetDepth;
This is not my code, it's an example I came across and thought I'd try in order to get my head around using Mex with Opengl so if you have any recommendations don't limit your self to this code.
Which command do you use to compile the Mex file? You need some OpenGL libraries, I assume. Which line of the C-Mex does crash? Insert the return commands inside the Mex function, compile and start again. This is the stoneage method of debugging, but for 10 lines this is sufficient.
A few minutes ago I happened to notice a Software Patch Renderer file exchange contribution that is written in C. Perhaps looking at that would help. Perhaps you can use some of the routines there to get the depth information
Simon, for compiling I use
mex filename.c "path to OpenGL32.lib"
and it seems like any gl call crashes matlab, I have had success compiling and running mex files before so maybe there is a problem with the OpenGL functions.
Thank you Simon, the submission you linked me does look very promising, too bad the link to the .NET assemblies is dead, I messaged the author but the submission is seven years old so I'm not holding my breath, I'll look into it more now though. Thank you for you time and valuable input Simon.
Hi Alaa,i am wondering if you have solved this problem
Helllo Li Yang, this was about a year ago, as I recall I could not use the MEX files because of the problems I described in the original question, I used this <https://uk.mathworks.com/matlabcentral/fileexchange/23792-opengl-net-examples?s_tid=prof_contriblnk%20OpenGL%20.NET%20wrapper OpenGL NI wrapper > OpenGL wrapper for a while and got decent results but it is quite restricted. I eventually moved on from using Matlab as a whole.

请先登录,再进行评论。

回答(1 个)

I found you need to run opengl in software mode and not hardware mode. Running the matlab command:
opengl software
before plotting does this. So now mexGetDepth runs without crashing Matlab, but it just plain doesn't work. The viewport never gets set to anything. It's always what I initialize it to before calling glGetIntegerv. If I call glGetError after glGetIntegerv I get 1282 which translates to "invalid command."

类别

帮助中心File Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

提问:

2017-6-7

编辑:

2018-7-23

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by