我正在尝试遵循ThinMatrix 的水教程。为此,我需要创建一个 FBO 并将其渲染为纹理。
我正在使用直接从教程中提供的源代码(从链接复制)。
基本上,我创建了一个 FrameBuffer:
public FrameBufferObject() {//call when loading the game
initialiseReflectionFrameBuffer();
initialiseRefractionFrameBuffer(); //Let's ignore refraction for now, as I only use reflection at the moment.
}
private void initialiseReflectionFrameBuffer() {
reflectionFrameBuffer = createFrameBuffer();
reflectionTexture = createTextureAttachment(REFLECTION_WIDTH,REFLECTION_HEIGHT);
reflectionDepthBuffer = createDepthBufferAttachment(REFLECTION_WIDTH,REFLECTION_HEIGHT);
unbindCurrentFrameBuffer();
}
然后我创建一个纹理附件:
private int createTextureAttachment( int width, int height) {
int texture = GL11.glGenTextures();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, width, height,
0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL32.glFramebufferTexture(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0,
texture, 0);
return texture;
}
我还创建了一个深度缓冲区附件:
private int createDepthBufferAttachment(int width, int height) {
int depthBuffer = GL30.glGenRenderbuffers();
GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depthBuffer);
GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL11.GL_DEPTH_COMPONENT, width,
height);
GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT,
GL30.GL_RENDERBUFFER, depthBuffer);
return depthBuffer;
}
然后,我将对象渲染到帧缓冲区对象:
Main.TerrainDemo.shader.start();
fbos.bindReflectionFrameBuffer();
for (Grass g : Main.TerrainDemo.toDraw){
g.render();
}
fbos.unbindCurrentFrameBuffer();
我像这样绑定帧缓冲区:
private void bindFrameBuffer(int frameBuffer, int width, int height){
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);//To make sure the texture isn't bound
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBuffer);
GL11.glViewport(0, 0, width, height);
System.out.println("Bound");
if(GL30.glCheckFramebufferStatus(GL30.GL_FRAMEBUFFER) == GL30.GL_FRAMEBUFFER_COMPLETE) {
System.out.println("Frame buffer setup is complete: " + GL30.glCheckFramebufferStatus(GL30.GL_FRAMEBUFFER));
}
System.out.println("Error: " + GL11.glGetError());
}
我打印 glGetError() 时的“错误”是正常的0
。“帧缓冲区设置”消息会打印出来。
之后,我希望调用fbos.getReflectionTexture()
会返回一个纹理 ID……确实如此!它成功返回纹理 ID 12。但是,当我绑定它时,纹理是完全黑色的。
public int getReflectionTexture() {//get the resulting texture
return reflectionTexture; //Remember, this was our original texture attachment.
}
reflectionTexture = createTextureAttachment(REFLECTION_WIDTH,REFLECTION_HEIGHT);
我无法弄清楚出了什么问题,以及为什么纹理没有显示任何渲染给它的东西。
我知道的事情没有错:
我绝对正确地绘制和纹理水本身。我可以使用任何预加载的纹理和纹理水就好了:
此外,渲染到 FBO 的对象具有正确的平移、旋转等。如果我不绑定任何帧缓冲区,则用于 FBO 的树叶会正确地绘制(并看到)在我的屏幕上。