c++ - 是否可以在片段着色器中写入特定的 mipmap 级别?

标签 c++ opengl glsl

我创建了一个经典的 2D 纹理,并渲染成一些值,在我的例子中是深度。在 c++ 代码中,我使用 glGenerateMipmap() 函数自动生成 mipmap。在片段着色器中,我可以访问纹理,就像普通的 sampler2D 制服一样,我想写入给定的 mipmap 级别,特别是我想根据级别 r 的值计算 r+1 级别的值,并写入其中.是否有一些有用的工具可以实现这一目标?使用 GLSL 4.30

例如:

// That's how I get the value, I need some kind of method to set
float current_value = textureLod(texture, tex_coords, MIP_LVL).y;

最佳答案

是的,使用函数 glFramebufferTexture2D 并在最后一个参数上指定 mipmap 级别。

附加完所需的所有内容后,在特定的 mipmap 渲染您想要的图像。

注意:不要忘记更改为 glViewport,这样它将呈现为适当的 mipmap 大小!

这是一个简单的例子:

unsigned int Texture = 0;
int MipmapLevel = 0;
glGenFramebuffers(1, &FBO);
glGenRenderbuffers(1, &RBO);
glGenTextures(1, &Texture);

glBindTexture(GL_TEXTURE_2D, Texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, TextureWidth, TextureHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
// glTexParameteri here

// Use shader and setup uniforms

glViewport(0, 0, TextureWidth, TextureHeight);

glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glBindRenderbuffer(GL_RENDERBUFFER, RBO);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, TextureWidth, TextureHeight);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, Texture, MipmapLevel); // Here, change the last parameter to the mipmap level you want to write to
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Render to texture

// Restore all states (glViewport, Framebuffer, etc..)

编辑:

如果你想修改一个生成的mipmap(用glGenerateMipmap生成或外部生成),你可以使用相同的方法,使用glFramebufferTexture2D并选择你想修改的mipmap .

完成所有设置后(如上面的示例),您可以完全渲染一个新的 mipmap,或者您可以发送原始纹理,并在片段着色器中修改它。

关于c++ - 是否可以在片段着色器中写入特定的 mipmap 级别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56733241/

相关文章:

c++ - 使用 openGL 着色器倾斜图像

C++:非内联模板方法的放置

c++如何释放和删除指向对象的二维数组

c++ - 作为 std::vector 包装器的用户定义容器应该继承还是包含 std::vector?

c++ - 在 Ubuntu 14.04 中使用 SOIL

c++ - 在 openGL + SDL 2.0 中绘制四面体

opengl - SPIR-V 字节码是否提供混淆?

opengl - GLSL 中可以进行可变参数宏扩展吗?

c++ - 使用异步调用模板函数

opengl - Assimp 骨骼动画 : Bones associated with a Vertex