FBO 的反射纹理得到正确的图像
Reflection texture from FBO getting to correct image
我正在使用 A FBO 捕捉四边形表面上方的反射。前提是把相机移到水面下然后渲染场景到fbo,然后像这样恢复到原来的观看位置:
WaterFrameBuffer fbos;
Water test(fbos.getReflectionTexture(), fbos.getRefractionTexture());
render->addWater(&test);
while (!glfwWindowShouldClose(window))
{
GLfloat currentFrame = (float)glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
gameController->update(deltaTime);
//reflection buffer
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
fbos.bindReflectionFrameBuffer();
float distance = 2 * (gameController->getGameCamera()->GetCameraPosition().y - 0);
gameController->getGameCamera()->GetCameraPosition().y -= distance;
gameController->getGameCamera()->flipPitch();
render->renderScene();
gameController->getGameCamera()->GetCameraPosition().y += distance;
gameController->getGameCamera()->flipPitch();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
//refraction buffer
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
fbos.bindRefractionFrameBuffer();
render->renderScene();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
render->renderScene();
render->renderWater();
glfwSwapBuffers(window);
}
但是我得到的结果是:
折射图像正确,但反射图像不正确。这是自己的反射
这是我正在使用的片段着色器:
#version 330 core
in vec3 ourColor;
in vec4 clipSpace;
in vec2 texC;
out vec4 color;
// Texture samplers
uniform sampler2D reflectTex;
uniform sampler2D refractTex;
//uniform sampler2D dudvTex;
void main()
{
vec3 ndc = (clipSpace.xyz / clipSpace.w)/ 2.0 + 0.5;
vec2 reflectTexCoords = vec2(ndc.x, -ndc.y);
vec2 refractTexCoords = vec2(ndc.x, ndc.y);
//vec2 distortion1 = texture(dudvTex, vec2(texC.x, texC.y)).rg*2.0 - 1.0;
//reflectTexCoords += distortion1;
//refractTexCoords += distortion1;
vec4 reflect = texture(reflectTex, reflectTexCoords);
vec4 refract = texture(reflectTex, refractTexCoords);
color = reflect;//mix(reflect,refract,0.5);
}
顶点着色器:
#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
layout (location = 2) in vec2 texCoord;
out vec3 ourColor;
out vec4 clipSpace;
out vec2 texC;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
const float tiling = 6.0;
void main()
{
clipSpace = projection*view*model *vec4(position, 1.0f);
gl_Position = clipSpace;
ourColor = color;
texC = texCoord;
}
关于如何解决这个问题或获取反射图像的替代方法有什么想法吗?
我假设您已将 GL_TEXTURE_WRAP_S
和 GL_TEXTURE_WRAP_T
的纹理环绕参数设置为 GL_CLAMP_TO_EDGE
。参见 glTexParameter
。
这会导致任何纹理坐标都被限制在 [0+1/(2*texturSize), 1-1/(2*textureSize)]
范围内。
要解决您的问题,您必须使用纹理环绕参数 GL_REPEAT
,或者您必须确保纹理坐标在 [0.0, 1.0] 范围内。
上图中的问题是纹理坐标(-ndc.y
)的y
分量取反,导致纹理坐标在[0.0,-1.0]范围内。将坐标移动到 [1.0, 0.0] 范围内并使用 1.0 - ndc.y
代替:
vec2 reflectTexCoords = vec2(ndc.x, 1.0 - ndc.y);
我正在使用 A FBO 捕捉四边形表面上方的反射。前提是把相机移到水面下然后渲染场景到fbo,然后像这样恢复到原来的观看位置:
WaterFrameBuffer fbos;
Water test(fbos.getReflectionTexture(), fbos.getRefractionTexture());
render->addWater(&test);
while (!glfwWindowShouldClose(window))
{
GLfloat currentFrame = (float)glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
gameController->update(deltaTime);
//reflection buffer
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
fbos.bindReflectionFrameBuffer();
float distance = 2 * (gameController->getGameCamera()->GetCameraPosition().y - 0);
gameController->getGameCamera()->GetCameraPosition().y -= distance;
gameController->getGameCamera()->flipPitch();
render->renderScene();
gameController->getGameCamera()->GetCameraPosition().y += distance;
gameController->getGameCamera()->flipPitch();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
//refraction buffer
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
fbos.bindRefractionFrameBuffer();
render->renderScene();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
render->renderScene();
render->renderWater();
glfwSwapBuffers(window);
}
但是我得到的结果是:
折射图像正确,但反射图像不正确。这是自己的反射
这是我正在使用的片段着色器:
#version 330 core
in vec3 ourColor;
in vec4 clipSpace;
in vec2 texC;
out vec4 color;
// Texture samplers
uniform sampler2D reflectTex;
uniform sampler2D refractTex;
//uniform sampler2D dudvTex;
void main()
{
vec3 ndc = (clipSpace.xyz / clipSpace.w)/ 2.0 + 0.5;
vec2 reflectTexCoords = vec2(ndc.x, -ndc.y);
vec2 refractTexCoords = vec2(ndc.x, ndc.y);
//vec2 distortion1 = texture(dudvTex, vec2(texC.x, texC.y)).rg*2.0 - 1.0;
//reflectTexCoords += distortion1;
//refractTexCoords += distortion1;
vec4 reflect = texture(reflectTex, reflectTexCoords);
vec4 refract = texture(reflectTex, refractTexCoords);
color = reflect;//mix(reflect,refract,0.5);
}
顶点着色器:
#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
layout (location = 2) in vec2 texCoord;
out vec3 ourColor;
out vec4 clipSpace;
out vec2 texC;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
const float tiling = 6.0;
void main()
{
clipSpace = projection*view*model *vec4(position, 1.0f);
gl_Position = clipSpace;
ourColor = color;
texC = texCoord;
}
关于如何解决这个问题或获取反射图像的替代方法有什么想法吗?
我假设您已将 GL_TEXTURE_WRAP_S
和 GL_TEXTURE_WRAP_T
的纹理环绕参数设置为 GL_CLAMP_TO_EDGE
。参见 glTexParameter
。
这会导致任何纹理坐标都被限制在 [0+1/(2*texturSize), 1-1/(2*textureSize)]
范围内。
要解决您的问题,您必须使用纹理环绕参数 GL_REPEAT
,或者您必须确保纹理坐标在 [0.0, 1.0] 范围内。
上图中的问题是纹理坐标(-ndc.y
)的y
分量取反,导致纹理坐标在[0.0,-1.0]范围内。将坐标移动到 [1.0, 0.0] 范围内并使用 1.0 - ndc.y
代替:
vec2 reflectTexCoords = vec2(ndc.x, 1.0 - ndc.y);