Using OpenGL C functions within Matlab using MEX
Ältere Kommentare anzeigen
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 Kommentare
Jan
am 7 Jun. 2017
How do you compile this? In which line does this crash - you simply check this by inserting some return commands?
Alla
am 7 Jun. 2017
Jan
am 7 Jun. 2017
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
am 7 Jun. 2017
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
am 8 Jun. 2017
Jan
am 8 Jun. 2017
Perhaps this works from inside Matlab also: see https://www.mathworks.com/matlabcentral/fileexchange/26619-dvcrender
li yang
am 23 Jul. 2018
Hi Alaa,i am wondering if you have solved this problem
Antworten (1)
Mark
am 12 Okt. 2017
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."
Kategorien
Mehr zu Write C Functions Callable from MATLAB (MEX Files) finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!