我希望能够渲染到纹理(用于着色器、字体),但我遇到了一个问题,即正确定位四边形(帧缓冲区本身做了它应该做的事情)。生成的(复制的)纹理在大多数情况下显示在左上角,其余部分被裁剪。这对于矩形纹理来说更糟,对方形纹理几乎没有影响(所以我碰巧几天都没有意识到这种行为)
示例代码:
public Texture copy(final Texture tex) {
final int tempTex = glGenTextures();
Draw.bindTexture(tempTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.getImageWidth(), tex.getImageHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, (ByteBuffer)null);
// Adjust projection matrix
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glViewport(0, 0, tex.getImageWidth(), tex.getImageHeight());
// Change blendmode to ignore the transparent background
Draw.blendingMode(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
// Prepare fbo, add the texture as backend and clear it
final int fbo = glGenFramebuffers();
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tempTex, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Finally draw
Draw.bindTexture(tex.getId());
glBegin(GL_QUADS);
{
glTexCoord2f(0, 0);
glVertex2f(-1, -1);
glTexCoord2f(1, 0);
glVertex2f(1, -1);
glTexCoord2f(1, 1);
glVertex2f(1, 1);
glTexCoord2f(0, 1);
glVertex2f(-1, 1);
}
glEnd();
// Unbind framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Reset projection matrix
glViewport(0, 0, 1920, 1080); // TODO Don't hardcode
glPopMatrix();
return new Texture(tempTex, tex.getImageWidth(), tex.getImageHeight());
}
示例图片:
上面是渲染的原始图像,在复制版本下面
我究竟做错了什么?
更新:现在看起来纹理被下采样了