Using OpenGL C functions within Matlab using MEX

5 次查看(过去 30 天)
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 个评论
li yang
li yang 2018-7-23
Hi Alaa,i am wondering if you have solved this problem
Alla
Alla 2018-7-23
编辑:Alla 2018-7-23
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 个)

Mark
Mark 2017-10-12
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."

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by