Opengl depth render in opencv

I use opengl in opencv to render 3d points to depth image, code is as follows:

glViewport(0, 0, params.frameSize.width, params.frameSize.height);


float fovX = params.frameSize.width/params.intr(0, 0); 
float fovY = params.frameSize.height/params.intr(1, 1); 

Vec3f t;
t = params.volumePose.translation();

double nearZ = t[2];
double farZ = params.volumeDims[2] * params.voxelSize + nearZ;

// Define viewing volume
std::cout<<"nearZ:"<<nearZ<<" fovX:"<<fovX<<" fovY:"<<fovY<<" farZ:"<<farZ<<std::endl;
glFrustum(-nearZ*fovX/2, nearZ*fovX/2, -nearZ*fovY/2, nearZ*fovY/2, nearZ, farZ);

glScalef(1.f, 1.f, -1.f); //Flip Z as camera points towards -ve Z axis

ogl::render(arr, idx, ogl::TRIANGLES);

Mat depthData(params.frameSize.height, params.frameSize.width, CV_32F);
Mat shadeData(params.frameSize.height, params.frameSize.width, CV_32FC3);
glReadPixels(0, 0, params.frameSize.width, params.frameSize.height, GL_DEPTH_COMPONENT, GL_FLOAT, depthData.ptr());
glReadPixels(0, 0, params.frameSize.width, params.frameSize.height, GL_RGB, GL_FLOAT, shadeData.ptr());

// linearise depth
for(auto it = depthData.begin<float>(); it != depthData.end<float>(); ++it)
    *it = farZ * nearZ / ((*it)*(nearZ - farZ) + farZ);

    if(*it >= farZ)
        *it = std::numeric_limits<float>::quiet_NaN();


the rendered depth image is strange, only top left conner is rendered rightly.(see left in the picture)

if i modify code glViewport(0, 0, params.frameSize.width, params.frameSize.height) to glViewport(0, 0, params.frameSize.width/2, params.frameSize.height/2), the object is rendered fully in the left top conner, but it should be rendered in the hole image.(see right in the picture)

please help to see what’s wrong with it?

If you’re using a framebuffer object, are all of the attached surface (textures or renderbuffers) large enough?

Is a scissor rectangle active?

That’s right, Thank you . There are something wrong with the opencv framebuffer settings.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.