gpt4 book ai didi

java - GL11 纹理渲染错误

转载 作者:行者123 更新时间:2023-11-29 05:45:56 25 4
gpt4 key购买 nike

我正在尝试在基本四边形上渲染图像,我查看了 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();

最佳答案

    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.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex2i(0, 0);

GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex2i(0, 128);

GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex2i(128, 128);

GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex2i(128, 0);

因为它是从 0.0f 到 1.0f 的纹理坐标(0.0f 是一侧,1.0f 是另一侧,这样它就与分辨率无关)

关于java - GL11 纹理渲染错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15855072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com