I am trying to use openGL inside a matlab MEX function. I need to render a textured mapped surface and get the RGB data of the renderd image. Inside the MEX file I have access to all the proper information (vertex, normals, texture and texture coordinates). Note that I don’t need any graphical output, but just to dump the framebuffer after OpenGL has rendered my surface.
The main routine for rendering (that is called inside the gateway routine that handles the parameter exchange with matlab) has this structure:
void Render(const mxArray *Iout, struct Blob_point *blob, struct Camera *camera,
const mxArray *tri, const mxArray *Iin, const mxArray *c_G_b_matlab)
[… some initializations not involving OpenGL…]
glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); glEnable(GL_CULL_FACE); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glFrontFace(GL_CCW); glCullFace(GL_BACK); glShadeModel(GL_SMOOTH); glPixelStorei(GL_PACK_ALIGNMENT, 1); glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, (GLsizei)camera->w, (GLsizei)camera->h);
[… render my surface …]
// read from the back buffer glReadBuffer(GL_FRONT_LEFT); //glRasterPos2d(0, 0); glRasterPos3d(0, 0, 0); glDrawPixels(3, 1, GL_RGB, GL_UNSIGNED_BYTE, pixels); glFlush(); glReadPixels(0, 0, camera->w, camera->h, GL_RGB, GL_UNSIGNED_BYTE, (GLuint *)mxGetData(Iout));
The main problem that I am encountering is that if I test the Error Status of OpenGL, NO MATTER WHERE, I always get the error:
My feeling is that since I am not using GLUT I need to somehow “manually initialize” OpenGL before starting the rendering pipeline (allocate the frame buffer?). Unfortunately I don’t know if my feeling is correct and, if this is the case, I don’t know how to do that.
I wonder if somebody has any experience in using OpenGL inside MEX files and can help me solving this problem.
Thanks and regards,