user1870238
user1870238

Reputation: 457

Texture not displaying properly OpenGL

I'm trying to load a simple background texture for the menu of a game I'm creating, but the texture isn't displayed properly.

For example, if I use this texture: http://s15.postimage.org/mqvuhq463/Super_Mario_Galazy_Background.png the entire background is blue and if I use this texture: http://www.highresolutiontextures.com/wp-content/uploads/2010/11/food-textures-01-bowtie-pasta-texture.jpg the entire background is a shade of orange.

This is the code I've written:

public class Menu extends Painter
{
    public Menu(GLCanvas canvas, Animator animator, GameWindow window)
    {
        super(canvas, animator, window);
    }
    @Override
    public void display(GLAutoDrawable drawable) 
    {
        GL gl = drawable.getGL();

        drawBackground(gl);

        gl.glFlush();
    }
    private void drawBackground(GL gl)
    {
        Texture background = textureLoader.getTexture("background");

        background.enable();
        background.bind();

        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(0f, 0f);
        gl.glVertex2f(0f, window.getHeight());
        gl.glVertex2f(window.getWidth(), window.getHeight());
        gl.glVertex2f(window.getWidth(), 0);
        gl.glEnd();

        background.disable();
    }
    @Override
    public void init(GLAutoDrawable drawable) 
    {
        GL gl = drawable.getGL();
        textureLoader = new TextureLoader("menu textures/", gl);

        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        // set ortho to the size of the background
        gl.glOrtho(0, 800, 800, 0, -1, 1);

        gl.glMatrixMode(GL.GL_MODELVIEW_MATRIX);
        gl.glClearColor(0f, 0f, 0f, 0f);  
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
    }
}

I haven't got the slightest clue how this is even possible, so any help would be greatly appreciated.

Upvotes: 0

Views: 349

Answers (1)

datenwolf
datenwolf

Reputation: 162164

You don't supply texture coordinates. Without texture coordinates all vertices receive the default texture coordinate, which is (0,0,0,0), which is the bottom left corner. If you look at your texture images the colors you see, are the colors of the bottom left pixel of each picture.

Solution: Supply texture coordinates. Texture coordinates are in the range [0; 1].

Upvotes: 1

Related Questions