我想要实现的目标:嗯,我有一个地形和天空盒,还有一个简单的水网格,仍然没有任何特别之处。我想通过使用帧缓冲区在水面上进行反射,将翻转的场景渲染到纹理上,并在水着色器中使用此纹理以及凹凸贴图、折射等...
水顶点着色器:
#version 330
uniform mat4 projection_matrix;
uniform mat4 modelview_matrix;
in vec3 a_Vertex;
out vec2 texCoord0;
out vec3 v_pos;
void main()
{
gl_Position = projection_matrix * modelview_matrix * vec4(a_Vertex,1.0);
texCoord0 = vec2(gl_Position.x,gl_Position.z);
}
水片段着色器:
#version 330
uniform sampler2D color_texture;
in vec2 texCoord0;
in vec3 v_pos;
void main(void)
{
gl_FragColor = vec4(1,1,1,0.6)*texture(color_texture,texCoord0.st);
}
他们仍在进行测试。
渲染反射:
glBindFramebuffer(GL_FRAMEBUFFER,pndheightmap.waterFrameBufferID);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
terrainShader->bindShader();
glScalef(1,-1,1);
GLfloat modelviewMatrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX,modelviewMatrix);
terrainShader->sendUniform4x4("scaling_matrix",modelviewMatrix);
glFrontFace(GL_CW);
pndheightmap.p_rendermap_h();
glFrontFace(GL_CCW);
glScalef(1,-1,1);
glGetFloatv(GL_MODELVIEW_MATRIX,modelviewMatrix);
terrainShader->sendUniform4x4("scaling_matrix",modelviewMatrix);
glEnable(GL_DEPTH_TEST);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
帧缓冲区生成:
glGenFramebuffers(1, &waterFrameBufferID);
glBindFramebuffer(GL_FRAMEBUFFER, waterFrameBufferID);
glGenTextures(1, &waterReflectionBufferID);
glBindTexture(GL_TEXTURE_2D, waterReflectionBufferID);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, mapwidth, mapheight,0,GL_RGB, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glGenRenderbuffers(1, &depthRenderBufferID);
glBindRenderbuffer(GL_RENDERBUFFER, depthRenderBufferID);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, mapwidth, mapheight);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,waterReflectionBufferID, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderBufferID);
GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, DrawBuffers);
glBindRenderbuffer(GL_RENDERBUFFER,0);
glBindFramebuffer(GL_FRAMEBUFFER,0);
我的问题: 首先,我知道我没有正确设置我的纹理坐标。它在水面上的 1*1 网格(我的 texcoords)上渲染场景翻转,但我不知道如何在整个水面上缩放它,因为我缩放它的方式很糟糕 - 角落整个水面都是 texcoords - 它扭曲和缩放太多,因此反射不在正确的位置。
图片: http: //postimg.org/image/br87iuhp3/
如您所见,它反映了视锥中的翻转屏幕。
问题:是否有其他方法可以将其渲染为纹理,但没有指定复杂的纹理坐标,或者在片段着色器/眼睛或相机空间中计算它们。