简体   繁体   English

GlReadPixels始终取自深度缓冲区0

[英]GlReadPixels always takes from the depth buffer 0

情况是,有许多相交的表面,并且无需单击鼠标即可确定光标位置的实际坐标(如果它指向的是什么或表面),为此,我想使用glReadPixels ,在整个着色器上绘制,最终glReadPixels给我当前像素的深度始终等于0 ,因为这可能是这样,并且可能具有新版opengl的替代功能中的任何功能,在论坛上寻找信息,但是发现我可以帮助,在此先感谢。

As far as I understood the question, you want to find coordinates of interscetion of some objects. 据我了解的问题,您想找到某些对象的相交坐标。 Correct? 正确?

You can't do any of these with glReadPixels, because this fuction returns color of pixel. 使用glReadPixels不能执行任何这些操作,因为此功能会返回像素的颜色。 Pixels are flat (as your computer screen is), so there is no depth written. 像素是平坦的(就像您的计算机屏幕一样),因此没有书写深度。 It just gets info from the result image on the screen. 它只是从屏幕上的结果图像中获取信息。

Here is nice quote: "OpenGL is not a scene management library. It is just a drawing API that draws things onto the screen and then forgets about them." 这是一个很好的报价:“ OpenGL并不是场景管理库。它只是一个绘图API,将事物绘制到屏幕上,然后将其忽略。”

So my guess is that you would have to do some tricks in your code and calculate everything with your own functions according to how your code works. 所以我的猜测是,您将不得不在代码中做一些技巧,并根据代码的工作方式使用自己的函数计算所有内容。

I am writing in Qt using openGl a 3D widget, which displays a variety of objects. 我正在使用OpenGl在Qt中编写3D小部件,其中显示了各种对象。 It is necessary to find the coordinates of the point (which of course will be on-site) under the cursor, if it points to the coca-object respectively. 如果光标分别指向古柯对象,则必须找到该点的坐标(当然该坐标将在现场)。 All rendering goes through shaders. 所有渲染都通过着色器。 I want to get the depth value of the depth buffer in a given pixel, a lot of which shows how to do this, and I do the same, but glReadPixels always tells me that the depth of any pixel is 0. 我想获取给定像素中深度缓冲区的深度值,其中很多内容显示了如何执行此操作,我也这样做,但是glReadPixels总是告诉我任何像素的深度均为0。

enter code here

initializeOpenGLFunctions();

glEnable(GL_TEXTURE_2D);

//корректное отображение перспективы
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER,0);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);

// включение обновление буфера глубины
glEnable(GL_DEPTH_TEST);

// очистка буфера
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

GLint viewport[4];
GLdouble modelview[16];
GLdouble projectionmtr[16];
GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;

viewport[0] = 0;
viewport[1] = 0;
viewport[2] = geometry().width();
viewport[3] = geometry().height();
QMatrix4x4 mtr =  mCameraMatr * mObjectMatr * mScale * mCentrTr * translate;

int k = 0;
for(int i = 0; i<4; ++i)
{
    for(int j = 0; j<4; ++j)
    {
        modelview[k] = mtr(j,i);
        ++k;
    }
}

k = 0;
for(int i = 0; i<4; ++i)
{
    for(int j = 0; j<4; ++j)
    {
        projectionmtr[k] = projection(j,i);
        ++k;
    }

winX = x;
winY = viewport[3] - y;

glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );// Here is always written in the variable winZ 0

gluUnProject( winX, winY, winZ, modelview, projectionmtr, viewport, &posX, &posY, &posZ);

return QVector3D(posX, posY, posZ);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM