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
Jan
2017년 6월 7일
How do you compile this? In which line does this crash - you simply check this by inserting some return commands?
Alla
2017년 6월 7일
Jan
2017년 6월 7일
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.
Walter Roberson
2017년 6월 7일
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
Alla
2017년 6월 8일
Jan
2017년 6월 8일
Perhaps this works from inside Matlab also: see https://www.mathworks.com/matlabcentral/fileexchange/26619-dvcrender
li yang
2018년 7월 23일
Hi Alaa,i am wondering if you have solved this problem
답변 (1개)
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."
카테고리
도움말 센터 및 File Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!