GL11纹理渲染错误

时间:2013-04-06 19:25:58

标签: java textures render lwjgl

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

1 个答案:

答案 0 :(得分:3)

    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是另一边,这样它不依赖于分辨率)