I managed to do the first step that I wanted to begin with, the previous messaged helped a lot so I got this bit of code:
M3dView active_view = M3dView::active3dView();
MImage img;
active_view.pushViewport(0, 0, active_view.portWidth(), active_view.portHeight());
active_view.refresh();
_OPENMAYA_DEPRECATION_PUSH_AND_DISABLE_WARNING
active_view.readColorBuffer(img);
_OPENMAYA_POP_WARNING
active_view.popViewport();
img.verticalFlip();
Now the next I am trying to do is to get the image from another camera, a camera that I am not looking from. I am not sure the viewport 2.0 has function that allows for rendering a viewport image from a specific camera, so I decided to render my own image through OpenGL and so far this the code I have:
MImage img;
_OPENMAYA_DEPRECATION_PUSH_AND_DISABLE_WARNING
MGLFunctionTable *gGLFT = MHardwareRenderer::theRenderer()->glFunctionTable();
if (!gGLFT) {
return;
}
int size = 512;
MGLuint texID, fboID, rboID;
gGLFT->glDisable(MGL_BLEND);
gGLFT->glGenTextures(1, &texID);
gGLFT->glBindTexture(MGL_TEXTURE_2D, texID);
gGLFT->glTexImage2D(MGL_TEXTURE_2D, 0, MGL_RGBA8, size, size, 0, MGL_RGBA, MGL_UNSIGNED_BYTE, 0);
gGLFT->glBindTexture(MGL_TEXTURE_2D, 0);
gGLFT->glGenFramebuffersEXT(1, &fboID);
gGLFT->glBindFramebufferEXT(MGL_FRAMEBUFFER, fboID);
gGLFT->glFramebufferTexture2DEXT(MGL_FRAMEBUFFER, MGL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texID, 0);
gGLFT->glViewport(0, 0, size, size);
gGLFT->glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
gGLFT->glClear(MGL_COLOR_BUFFER_BIT | MGL_DEPTH_BUFFER_BIT);
gGLFT->glBegin(MGL_TRIANGLES);
gGLFT->glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
gGLFT->glVertex2f(-0.5f, -0.5f);
gGLFT->glVertex2f(0.5f, -0.5f, 0.0f);
gGLFT->glVertex2f(0.0f, 0.5f, 0.0f);
gGLFT->glEnd();
unsigned char *image_buffer = new unsigned char[size * size * 4];
gGLFT->glReadPixels(0, 0, size, size, MGL_RGBA, MGL_UNSIGNED_BYTE, image_buffer);
img.setPixels(image_buffer, size, size);
img.writeToFile("......../test.jpg", "jpg");
gGLFT->glBindFramebufferEXT(MGL_FRAMEBUFFER, MGL_NONE);
_OPENMAYA_POP_WARNING
This code is executed inside a node that I am creating, but the output image changes if I have the hypershade open:
It looks like it picks up an already existing viewport with its coordinates system, so my question is, is that possible to create another, invisible, OpenGL viewport with default projection, modelview… matrices so I can render the image from it ?
Thank you for your help
Cheers !