我正在使用用于 java 的 LWJGL OpenGL 绑定,我想将纹理绘制到帧缓冲区,这样我就可以渲染始终同时使用的纹理。
我按照 LWJGL wiki 上关于使用帧缓冲区对象的教程进行操作。起初我什么也看不到,然后我创建了一个准系统测试类。
这是我的精灵的样子:链接
这是输出的样子:link
这是代码(它使用 slickutil 进行纹理加载):
public class ComboSpriteTest {
public static void main(String[] args) {
try {
Display.setDisplayMode(new DisplayMode(400, 300));
Display.create();
Display.setFullscreen(false);
} catch (LWJGLException e) {
e.printStackTrace();
}
initOGL();
// use slickUtil to load a textre
Texture tex;
try {
tex = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("walkcycle.png"));
} catch (IOException e) {
e.printStackTrace();
return;
}
int texId = createFBOAandDraw(tex);
GL11.glClearColor(1, 0, 0, 1);
while (!Display.isCloseRequested()) {
try {
Thread.sleep(1000 / 30);
} catch (InterruptedException e) {
}
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, texId);
glLoadIdentity();
glBegin(GL_QUADS);
glTexCoord2d(0, 0);
glVertex2d(0, 0);
glTexCoord2d(0, 1);
glVertex2d(0, tex.getTextureHeight());
glTexCoord2d(1, 1);
glVertex2d(tex.getTextureWidth(), tex.getTextureHeight());
glTexCoord2d(1, 0);
glVertex2d(tex.getTextureWidth(), 0);
glEnd();
Display.update();
}
Display.destroy();
}
public static void initOGL() {
Display.setTitle("FramebufferTest");
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 400, 300, 0, -1, 1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glViewport(0, 0, 400, 300);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
}
public static int createFBOAandDraw(Texture texture) {
int framebufferID = glGenFramebuffersEXT();
int colorTextureID = glGenTextures();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebufferID);
glBindTexture(GL_TEXTURE_2D, colorTextureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texture.getTextureWidth(), texture.getTextureHeight(),
0, GL_RGBA, GL_INT, (java.nio.ByteBuffer) null);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorTextureID, 0);
glPushAttrib(GL_VIEWPORT_BIT | GL_TRANSFORM_BIT);
glViewport(0, 0, texture.getTextureWidth(),texture.getTextureHeight());
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebufferID);
glBindTexture(GL_TEXTURE_2D, texture.getTextureID());
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(texture.getWidth(), 0);
glVertex2f(texture.getImageWidth(), 0);
glTexCoord2f(texture.getWidth(), texture.getHeight());
glVertex2f(texture.getImageWidth(), texture.getImageHeight());
glTexCoord2f(0, texture.getHeight());
glVertex2f(0, texture.getImageHeight());
glEnd();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glPopAttrib();
return colorTextureID;
}
}
纹理看起来缩放到其原始高度的 1/8 左右并且上下颠倒。关于它为什么呈现这样的任何见解?