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

Ric*_*d T 3 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;
Run Code Online (Sandbox Code Playgroud)

Nao*_*dar 5

是的,使用该函数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..)
Run Code Online (Sandbox Code Playgroud)

编辑:

如果要修改生成的 mipmap(使用glGenerateMipmap或外部生成),可以使用相同的方法,使用glFramebufferTexture2D并选择要修改的 mipmap。

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