我已经实现了CPU代码,可以将投影纹理复制到3d对象上的更大纹理,如果愿意,可以"贴花烘焙",但现在我需要在GPU上实现它.为此,我希望使用计算着色器,因为在我当前的设置中添加FBO非常困难.
这个问题更多的是关于如何使用Compute着色器,但对于任何感兴趣的人,这个想法是基于我从用户jozxyqk得到的答案,在这里看到:https://stackoverflow.com/a/27124029/2579996
写入的纹理在我的代码中被调用_texture,而投影的纹理是_textureProj
简单的计算着色器
const char *csSrc[] = {
"#version 440\n",
"layout (binding = 0, rgba32f) uniform image2D destTex;\
layout (local_size_x = 16, local_size_y = 16) in;\
void main() {\
ivec2 storePos = ivec2(gl_GlobalInvocationID.xy);\
imageStore(destTex, storePos, vec4(0.0,0.0,1.0,1.0));\
}"
};
Run Code Online (Sandbox Code Playgroud)
如您所见,我目前只想将纹理更新为某种任意(蓝色)颜色.
更新功能
void updateTex(){
glUseProgram(_computeShader);
const GLint location = glGetUniformLocation(_computeShader, "destTex");
if (location == -1){
printf("Could not locate uniform location for texture in CS");
}
// bind texture
glUniform1i(location, 0);
glBindImageTexture(0, *_texture, 0, GL_FALSE, 0, …Run Code Online (Sandbox Code Playgroud)