c++ - 在opengl中将整数存储在单值纹理中不起作用
问题描述
我正在尝试编写一个适用于状态的计算着色器。我想将此状态的初始值存储在单通道纹理中。但是,这不起作用。
我有以下代码:
GLuint tex;
glGenTextures(1, &tex);
int time_horizon = 1000;
std::vector<decltype(time_horizon)> tex_vals(width * height * 4, time_horizon);
for (auto x: tex_vals)
assert(x == time_horizon);
glBindTexture(GL_TEXTURE_2D, tex);
assert(glGetError() == 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32I, width, height, 0, GL_RED_INTEGER, GL_INT, tex_vals.data());
auto err = glGetError();
if (err) {
std::cerr << "gl error is: " << err << '\n';
exit(1);
}
decltype(time_horizon) test[static_cast<size_t>(width*height)];
glBindTexture(GL_TEXTURE_2D, tex);
glReadPixels(0, 0, width, height, GL_RED_INTEGER, GL_SHORT, test);
for (auto x: test) {
std::cerr << x << '\n';
assert(time_horizon == x);
}
对 glGetError 的调用在这两种情况下都返回 0,并且 glReadPixels 调用之后的断言在第一次调用时被触发。纹理由 0 组成。但是,似乎也有一些 1、-1 和随机值块。
我试过存储整数和短裤,但似乎没有任何效果。我还在 glTexImage2D 调用中摆弄了不同的 GL_ENUM,但无济于事。
解决方案
我将把问题评论中已经提供的答案放在这里。
glReadPixels 从当前绑定的帧缓冲区中读取像素。
要从纹理中读取像素,您应该使用glGetTexImage
更现代的版本glGetTextureImage (4.5),它允许直接传递纹理句柄,而无需将纹理绑定到上下文。
推荐阅读
- python - 如果输入的输入被视为“无效”,则尝试继续请求输入
- html - CSS - vh 确实有效,而百分比无效
- vue.js - Vue.js 将 props 数组传递给动态组件
- node.js - AWS SNS 发布仅适用于本地,但不适用于 lambda
- unit-testing - Apache CXF 将 xml 转换为 SoapMessage 以进行单元测试
- android - BottomSheetBehavior_Layout_behavior_hideable 如何翻译成 app:behavior_hideable?
- python - PyQt 将 PuTTY / KiTTY 嵌入应用程序 - 无法删除边框
- ios - 在同一个 Firebase 实例上使用 Firebase 电子邮件链接身份验证登录多个 iOS 应用
- c++ - 具有等轴和自动缩放的 QwtPlot
- php - 根据另一个数组键的顺序对数组键进行排序