Problem with textures (newbie in need of help)

I am quite new to OpenGL, and obviously LWJGL for that matter… Can anyone hint on why is my texture rendered like this (I’m texturing a simple quad):

[img=http://img140.imageshack.us/img140/6205/screenshotlh0.th.png]

The weird quad is supposed to be a sprite :smiley:

Here goes some (relevant) code:


public static Texture loadTexture(String name, boolean flip) {
        
        Texture texture = null;
        
        try {
            
            ByteBuffer imageData = null;
            int ilImageHandle;
            int oglImageHandle;
            
            IntBuffer scratch = BufferUtils.createIntBuffer(1);
            
            IL.ilGenImages(scratch);
            IL.ilBindImage(scratch.get(0));
            ilImageHandle = scratch.get(0);
            
            if (!IL.ilLoadFromURL(Thread.currentThread().
                    getContextClassLoader().getResource(name))) {
                return null;
            }
            
            IL.ilConvertImage(IL.IL_RGBA, IL.IL_BYTE);
            
            if (flip) {
                ILU.iluFlipImage();
            }
            
            int width = IL.ilGetInteger(IL.IL_IMAGE_WIDTH);
            int height = IL.ilGetInteger(IL.IL_IMAGE_HEIGHT);
            
            int textureWidthSize = getNextPowerOfTwo(width);
            int textureHeightSize = getNextPowerOfTwo(height);
            
            if (textureWidthSize != width || textureHeightSize != height) {
                imageData = BufferUtils.createByteBuffer(textureWidthSize * textureHeightSize * 4);
                IL.ilCopyPixels(0, 0, 0, textureWidthSize, textureHeightSize, 1, IL.IL_RGBA, IL.IL_BYTE, imageData);
            } else {
                imageData = IL.ilGetData();
            }
            
            GL11.glGenTextures(scratch);
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, scratch.get(0));
            oglImageHandle = scratch.get(0);
            
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
            GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, textureWidthSize, textureHeightSize,
                    0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, imageData);
            
            if (textureWidthSize != width || textureHeightSize != height) {
                texture = new Texture(oglImageHandle, width, height, (width / (float) textureWidthSize),
                        (height / (float) textureHeightSize), textureWidthSize, textureHeightSize);
            } else {
                texture = new Texture(oglImageHandle, width, height);
            }
            
            scratch.put(0, ilImageHandle);
            
            IL.ilDeleteImages(scratch);
            
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return texture;
    }

To draw the texture:


public void draw(int x, int y) {
        
        GL11.glPushMatrix();
        
        texture.bind();
        
        GL11.glTranslatef(x, y, 1.0f);
        
        GL11.glColor3f(1,1,1);
        GL11.glBegin(GL11.GL_QUADS);
        {
            GL11.glTexCoord2f(0, 0);
            GL11.glVertex2f(0, 0);
            GL11.glTexCoord2f(0, texture.getHeight());
            GL11.glVertex2f(0, height);
            GL11.glTexCoord2f(texture.getWidth(), texture.getHeight());
            GL11.glVertex2f(width, height);
            GL11.glTexCoord2f(texture.getWidth(), 0);
            GL11.glVertex2f(width, 0);
        }
        GL11.glEnd();
        GL11.glPopMatrix();
    }

I believe it is something to do with the projection matrix, so here goes the code I call in a method, which is called before the rendering loop:


        GL11.glEnable(GL11.GL_BLEND); 
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glDisable(GL11.GL_DEPTH_TEST);
        GL11.glShadeModel(GL11.GL_SMOOTH);
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, width, height, 0, -1, 1);
        GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);

I’m puzzled why it doesn’t work, it must be something real stupid on my behalf ;D

Thanks in advance,
Rodrigo

Texture coords are normalised - that is, (0,0) is always the texture origin, and (1,1) is the opposite corner. You’re using (texWidth, texHeight) where you need (1,1). Numbers larger than 1 make the texture repeat, so if you’ve got a 64x64 texture then your current code is repeating the texture 64 times on each axis.

     GL11.glBegin(GL11.GL_QUADS);
        {
            GL11.glTexCoord2f(0, 0);
            GL11.glVertex2f(0, 0);
            GL11.glTexCoord2f(0, 1.0f);
            GL11.glVertex2f(0, height);
            GL11.glTexCoord2f(1.0f, 1.0f);
            GL11.glVertex2f(width, height);
            GL11.glTexCoord2f(1.0f, 0);
            GL11.glVertex2f(width, 0);
        }
        GL11.glEnd();

Oh, lord, of course! This isn’t swing :stuck_out_tongue:

My bad, thanks for the help, dude!