I want to be able to render to a texture (for shaders, fonts), but I've got a problem a problem to position the quad properly (the framebuffer itself does what it should do). The resulting (copied) texture shows in most cases the upper left corder, the rest gets cropped. That's worser for rectangular textures, nearly no effect for square textures (so I happen to not recognize this behaviour for days)
Example code:
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());
}
Example image:
The uppers are the original images as rendered, below the copied version
What am I doing wrong?
Update: Now it seems like the texture gets downsampled