void MeshRenderer :: renderToImage(cv::Mat4b& image, const Pose3D& pose, int flags) { m_pbuffer->makeCurrent(); glMatrixMode (GL_MODELVIEW); glLoadIdentity (); if (flags & LIGHTING) { GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat mat_shininess[] = { 10.0 }; GLfloat light_position[] = { 1, 1, 1.0, 0.0 }; glEnable(GL_LIGHTING); glShadeModel (GL_SMOOTH); glEnable(GL_COLOR_MATERIAL); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glEnable(GL_LIGHT0); } computeProjectionMatrix(image, pose); glClearColor(1.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (flags & WIREFRAME) glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); // draw the display list glCallList(m_list_index); glFlush(); computeDepthBuffer(); QImage qimage = m_pbuffer->toImage(); for (int r = 0; r < qimage.height(); ++r) for (int c = 0; c < qimage.width(); ++c) { QRgb pixel = qimage.pixel(c,r); Vec4b color (qBlue(pixel), qGreen(pixel), qRed(pixel), qAlpha(pixel)); m_color_buffer(r,c) = color; float a = qAlpha(pixel)/255.f; if (a > 0) { Vec4b old_color = image(r,c); image(r,c) = Vec4b(old_color[0]*(1-a) + color[0]*a, old_color[1]*(1-a) + color[1]*a, old_color[2]*(1-a) + color[2]*a, 255); } } }
void MeshRenderer :: renderToImage(cv::Mat4b& image, int flags) { ntk_assert(m_vertex_buffer_object.initialized, "Renderer not initialized! Call setPose and setMesh."); ntk::TimeCount tc_gl_current("make_current", 2); m_pbuffer->makeCurrent(); tc_gl_current.stop(); ntk::TimeCount tc_gl_render("gl_render", 2); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (flags & WIREFRAME) glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); VertexBufferObject& vbo = m_vertex_buffer_object; glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo.vertex_id); if (vbo.has_texcoords) { glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, vbo.texture_id); } else { glDisable(GL_TEXTURE_2D); } if (vbo.has_texcoords) glEnableClientState(GL_TEXTURE_COORD_ARRAY); if (vbo.has_color) glEnableClientState(GL_COLOR_ARRAY); else glColor3f(1.0f,0.f,0.f); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, 0, 0); if (vbo.has_color) glColorPointer(3, GL_UNSIGNED_BYTE, 0, ((char*) NULL) + vbo.color_offset); if (vbo.has_texcoords) glTexCoordPointer(2, GL_FLOAT, 0, ((char*) NULL) + vbo.texture_offset); if (vbo.has_faces) { glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vbo.faces_id); glNormal3f(0, 0, 1); glDrawElements(GL_TRIANGLES, vbo.nb_faces*3, GL_UNSIGNED_INT, 0); if (flags & OUTLINE) { // Draw again for outline. glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); glDrawElements(GL_TRIANGLES, vbo.nb_faces*3, GL_UNSIGNED_INT, 0); glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); } } else { glDrawArrays(GL_POINTS, 0, vbo.nb_vertices); } glDisableClientState(GL_VERTEX_ARRAY); if (vbo.has_color) glDisableClientState(GL_COLOR_ARRAY); if (vbo.has_texcoords) glDisableClientState(GL_TEXTURE_COORD_ARRAY); // bind with 0, so, switch back to normal pointer operation glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); if (vbo.has_faces) { glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); } glFinish(); tc_gl_render.stop(); ntk::TimeCount tc_image("to_image", 2); QImage qimage = m_pbuffer->toImage(); tc_image.stop(); ntk::TimeCount tc_depth_buffer("compute_depth_buffer", 2); computeDepthBuffer(); tc_depth_buffer.stop(); ntk::TimeCount tc_convert("convert_to_cv", 2); for (int r = 0; r < qimage.height(); ++r) for (int c = 0; c < qimage.width(); ++c) { QRgb pixel = qimage.pixel(c,r); Vec4b color (qBlue(pixel), qGreen(pixel), qRed(pixel), qAlpha(pixel)); m_color_buffer(r,c) = color; float a = qAlpha(pixel)/255.f; if (a > 0) { Vec4b old_color = image(r,c); image(r,c) = Vec4b(old_color[0]*(1-a) + color[0]*a, old_color[1]*(1-a) + color[1]*a, old_color[2]*(1-a) + color[2]*a, 255); } } tc_convert.stop(); }