View Full Version : matlab MEX files + OpenGL

Marco Zuliani
04-18-2003, 01:45 PM
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...]



glPixelStorei(GL_PACK_ALIGNMENT, 1);
glClearColor(0.0, 0.0, 0.0, 0.0);

glViewport(0, 0, (GLsizei)camera->w, (GLsizei)camera->h);

[... render my surface ...]

// read from the back buffer

//glRasterPos2d(0, 0);
glRasterPos3d(0, 0, 0);
glDrawPixels(3, 1, GL_RGB, GL_UNSIGNED_BYTE, pixels);


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:

invalid operation

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,

04-18-2003, 03:31 PM
Yes, you need to create a valid OpenGL context and make it current, even if you are rendering to memory and not a window. You probably would have gotten an answer sooner if you had posted it in the beginner section.