OpenGL:关于获取渲染结果的深度信息的问题

23 篇文章 4 订阅
19 篇文章 0 订阅

前面已经记录了从显存中获取渲染结果并保存为图像的方法
OpenGL:如何从缓存中读取颜色、深度信息【转】_六月的翅膀的博客-CSDN博客

并已经验证了上述方法可以完美的保存图像,但是保存深度信息部分还是出错了。

    int WINDOW_WIDTH = (int)_width;
    int WINDOW_HEIGHT = (int)_height;
    int tmpPixelSize = WINDOW_WIDTH * WINDOW_HEIGHT;
    GLfloat* depth_value = new GLfloat[tmpPixelSize];

    //从深度缓冲区读取数据
    glReadPixels(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, GL_DEPTH_COMPONENT, GL_FLOAT, depth_value);
    //输出深度值,数据太多,这里只输出了部分
    for (int i = WINDOW_WIDTH / 2; i < WINDOW_WIDTH / 2 + 1; i++) {
        for (int j = WINDOW_HEIGHT / 2; j < WINDOW_HEIGHT / 2 + 10; j++) {
            qDebug() << depth_value[i * WINDOW_HEIGHT + j];
            qDebug() << "-----";
        }
    }
    delete[] depth_value;

具体出错是所有位置的深度信息都为0。尝试过改变数据类型GLfloat为其他类型,依旧。

终于,在这里找到了一个回答:qt - glReadPixels GL_DEPTH_COMPONENT does not work in mousePressEvent - Stack Overflowicon-default.png?t=M276https://stackoverflow.com/questions/51034014/glreadpixels-gl-depth-component-does-not-work-in-mousepressevent

【OpenGL operations should be performed only when an OpenGL context is active. This is true in the paintGL() method because this is probably set by the framework for you. You can't assume the OpenGL is active in other methods, like in other event responding methods and callbacks as mousePressEvent(), because those methods can also be run by a different thread where the OpenGL context is not active. 】

就是说异步线程里使用的原因。

原因找到了,试一下吧再。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值