我正在尝试使用 FBO 来提高性能并通过尝试新的东西来获得知识,但我遇到了以下问题:当我将一些纹理渲染到后台缓冲区时,它工作得很好(由于多层而有点慢纹理相互叠加)但是当我尝试首先使用 FBO 将纹理绘制到另一个纹理上时,新纹理始终保持白色。
现在到代码 这是我的 FBOsetup 函数(在开始时调用(在创建 Opengl 上下文之后)):
int FBOId;
int FBOTexId;
void setupFBO(){
IntBuffer buffer = ByteBuffer.allocateDirect(1 * 4).order(ByteOrder.nativeOrder()).asIntBuffer();
glGenFramebuffersEXT(buffer);
FBOId= buffer.get();
FBOTexId=glGenTextures();
glBindTexture(GL_TEXTURE_2D, FBOTexId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, tileSize, tileSize, 0, GL_RGB, GL_UNSIGNED_BYTE, ByteBuffer.allocateDirect(4*tileSize*tileSize));
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBOId);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, FBOTexId, 0);
}
现在渲染函数:
void redraw(){
glPushMatrix();
glPushAttrib(GL_VIEWPORT_BIT | GL_COLOR_BUFFER_BIT);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBOId);
glLoadIdentity();
glOrtho(0, tileSize, tileSize, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
Main.drawTexture(0, 0, tileSize, tileSize, textureRotation, textureId);
glBindFramebufferEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0);
glPopAttrib();
glPopMatrix();
}
最后绘制函数:
void draw(){
Main.drawTexture(x*tileSize, y*tileSize, tileSize, tileSize, FBOTexId);
}
对于所有想要它的人,我的 drawTexture 函数:
public static void drawTexture(float x, float y, float xSize, float ySize, float rot, int textureId){
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);
GL11.glMatrixMode(GL11.GL_TEXTURE);
GL11.glLoadIdentity();
GL11.glTranslatef(0.5f,0.5f,0.0f);
GL11.glRotatef(rot,0.0f,0.0f,1.0f);
GL11.glTranslatef(-0.5f,-0.5f,0.0f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f(x,y);
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(x+xSize,y);
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(x+xSize,y+ySize);
GL11.glTexCoord2f(0,1);
GL11.glVertex2f(x,y+ySize);
GL11.glEnd();
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
如果我用它替换 draw() 函数中的 Main.draw 行,Main.drawTexture(x*tileSize, y*tileSize, tileSize, tileSize, textureRotation, textureId);
它会直接将纹理绘制到屏幕上的正确位置,但问题是我总是必须重新绘制所有纹理层,并使用我绘制的 FBO每次发生变化时,所有图层都会发生变化,然后只从 FBO 中绘制纹理。