我正在尝试在基本四边形上渲染图像,我查看了Space Invaders Example Game的代码,并将该代码实现到我的中。图像在屏幕上以正确的颜色呈现,但图像似乎发生了变化。这是我要渲染的图像:
http://img203.imageshack.us/img203/5264/testwq.png
这是它的渲染方式:
http://img593.imageshack.us/img593/6849/test2uh.png
图像是 128x128,四边形也是。
这是我的代码:
public class RenderEngine
{
private IntBuffer intbuf = BufferUtils.createIntBuffer(1);
private ColorModel glAlphaColorModel;
private ColorModel glColorModel;
public RenderEngine()
{
this.glAlphaColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 8 }, true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
this.glColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 0 }, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
}
public void bindTexture(String filename)
{
try
{
File file = new File(CivilPolitica.instance.getDir(), "resources/" + filename);
FileInputStream fis = new FileInputStream(file);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, this.getTexture(fis));
fis.close();
}
catch (Exception e)
{
e.printStackTrace();
System.exit(0);
}
}
private int getTexture(InputStream in)
{
try
{
GL11.glGenTextures(this.intbuf);
int id = this.intbuf.get(0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
BufferedImage bi = ImageIO.read(in);
int format = bi.getColorModel().hasAlpha() ? GL11.GL_RGBA : GL11.GL_RGB;
ByteBuffer texData;
WritableRaster raster;
BufferedImage texImage;
int texWidth = 2;
int texHeight = 2;
while (texWidth < bi.getWidth())
{
texWidth *= 2;
}
while (texHeight < bi.getHeight())
{
texHeight *= 2;
}
if (bi.getColorModel().hasAlpha())
{
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, texWidth, texHeight, 4, null);
texImage = new BufferedImage(this.glAlphaColorModel, raster, false, new Hashtable<String, Object>());
}
else
{
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, texWidth, texHeight, 3, null);
texImage = new BufferedImage(this.glColorModel, raster, false, new Hashtable<String, Object>());
}
Graphics g = texImage.getGraphics();
g.setColor(new Color(0f, 0f, 0f, 0f));
g.fillRect(0, 0, texWidth, texHeight);
g.drawImage(bi, 0, 0, null);
byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer()).getData();
texData = ByteBuffer.allocateDirect(data.length);
texData.order(ByteOrder.nativeOrder());
texData.put(data, 0, data.length);
texData.flip();
glTexParameteri(GL11.GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
glTexParameteri(GL11.GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, bi.getWidth(), bi.getHeight(), 0, format, GL_UNSIGNED_BYTE, texData);
return id;
}
catch (Exception e)
{
e.printStackTrace();
System.exit(0);
return 0;
}
}
}
而实际的四边形:
CivilPolitica.instance.renderer.bindTexture("test.png");
GL11.glPushMatrix();
GL11.glTranslatef(128, 128, 0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2i(0, 0);
GL11.glTexCoord2f(0, 127);
GL11.glVertex2i(0, 128);
GL11.glTexCoord2f(127, 127);
GL11.glVertex2i(128, 128);
GL11.glTexCoord2f(127, 0);
GL11.glVertex2i(128, 0);
GL11.glEnd();
GL11.glPopMatrix();