Using OpenGL C functions within Matlab using MEX
7 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Mark
on 12 Oct 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."
0 Comments
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!