Rheel
Rheel

Reputation: 420

GL11 Texture rendering wrong

I'm trying to render an image on a basic quad, I took a look at the Space Invaders Example Game for the code, and implemented that code into mine. The image gets renderer on screen, with the right colors, but the image seems shifted. This is the image I'm trying to render:

http://img203.imageshack.us/img203/5264/testwq.png

This is how it renders:

http://img593.imageshack.us/img593/6849/test2uh.png

The image is 128x128, and so is the quad.

Here is my code:

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;
       }
   }
}

And the actual quad:

        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();

Upvotes: 1

Views: 593

Answers (1)

Quonux
Quonux

Reputation: 2991

    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);

must be

    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);

because it are texture coordinates from 0.0f to 1.0f (0.0f ist the one side and 1.0f is the other, that way it is not resolution dependent)

Upvotes: 3

Related Questions