我正在尝试渲染一组带纹理的四边形。
我可以渲染彩色四边形,但不能渲染纹理四边形(屏幕显示为空。)
我正在使用 LWJGL 和 PNGDecoder。
初始化我的 OGL 的代码:
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 0, 600, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glEnable(GL11.GL_TEXTURE_2D);
解码我的图像的代码:
ByteBuffer buffer = null;
InputStream in = ClassLoader.getSystemResourceAsStream(filename);
try {
buffer = decodeStreamToBuffer(in);
} finally {
in.close();
}
return buffer;
我的decodeStreamToBuffer(InputStream in)
:
PNGDecoder decoder;
ByteBuffer buf = null;
try {
decoder = new PNGDecoder(in);
buf = ByteBuffer.allocateDirect(4*decoder.getWidth()*decoder.getHeight());
decoder.decode(buf, decoder.getWidth()*4, Format.RGBA);
buf.flip();
} catch (Exception e) {
e.printStackTrace();
}
return buf;
我的渲染代码:
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glColor3f(0.5f, 0.5f, 1.0f); //Still there to test color quads.
// draw quad
GL11.glPushMatrix();
GL11.glTranslatef(screencenter.getX(), screencenter.getY(), 0);
GL11.glScalef(1f, 0.5f, 1f);
GL11.glRotatef(camRotation, 0f, 0f, 1f);
GL11.glTranslatef(-screencenter.getX(), -screencenter.getY(), 0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
GL11.glBegin(GL11.GL_TEXTURE_2D);
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex2f(0, 0);
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex2f(32, 0);
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex2f(32, 32);
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex2f(0, 32);
GL11.glEnd();
GL11.glPopMatrix();
当我离开纹理绑定并将其更改GL_TEXTURE_2D
为GL_MODELVIEW
一切正常...但使用颜色而不是纹理。我在这里犯了一个菜鸟错误吗?