Search code examples
javaopengllwjglslick2d

Textures not binding in OpenGL (LWJGL/Slick-util)


I have a problem with what seems to be binding the textures to a shape. When I do so, I get a white shape of the correct dimensions, the color given to it by Color.white.bind().

GraphicsManager.java

package com.jackwilsdon.spectrun;

import java.io.IOException;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector2f;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

public class GraphicsManager {

    final static int WIDTH = 800;
    final static int HEIGHT = 600;

    private static Texture cloudTexture = null;

    public static void initDisplay() throws LWJGLException
    {
        Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
        Display.create();
    }

    public static void initOpenGL()
    {     
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, WIDTH, 0, HEIGHT, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
    }

    public static void loadTextures()
    {
        Texture currentObject = null;
        try {
            currentObject = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("./resources/cloud.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Texture loaded: "+currentObject);
        System.out.println(">> Image width: "+currentObject.getImageWidth());
        System.out.println(">> Image height: "+currentObject.getImageHeight());
        System.out.println(">> Texture width: "+currentObject.getTextureWidth());
        System.out.println(">> Texture height: "+currentObject.getTextureHeight());
        System.out.println(">> Texture ID: "+currentObject.getTextureID());
        cloudTexture = currentObject;
    }

    public static void DrawRectangle(int x, int y, int width, int height)
    {
        GL11.glBegin(GL11.GL_QUADS);
            GL11.glVertex2f(x,y);
            GL11.glVertex2f(x+width,y);
            GL11.glVertex2f(x+width,y+height);
            GL11.glVertex2f(x,y+height);
        GL11.glEnd();
    }

    public static void DrawRectangle(int x, int y, int width, int height, int red, int green, int blue)
    {
        GL11.glColor3ub((byte)red, (byte)green, (byte)blue);
        DrawRectangle(x, y, width, height);
    }

    public static Vector2f DrawCloud(int x, int y)
    {
        cloudTexture.bind();
        int width = cloudTexture.getImageWidth();
        int height = cloudTexture.getImageHeight();
        GL11.glBegin(GL11.GL_QUADS);
            GL11.glVertex2f(x,y);
            GL11.glVertex2f(x+width,y);
            GL11.glVertex2f(x+width,y+height);
            GL11.glVertex2f(x,y+height);
        GL11.glEnd();
        return new Vector2f(cloudTexture.getImageWidth(), cloudTexture.getImageHeight());
    }
}

I suspect it has something to do with my initOpenGL() method, as in an example I have looked at (http://ninjacave.com/slickutil1), theirs is very different. Just copying theirs into mine gives unexpected results (black screen). Is something missing from my initOpenGL() method?

EDIT: The loadTextures() method outputs the correct image dimensions, but the texture's dimensions are different, what does this mean?

EDIT 2: The PNG is not the problem, I've tried 2 others to find the same result.


A new problem has arisen after fixing my code, by setting the texture coordinates. I now get a weird effect, like this; i.imgur.com/5lr79.png. The image is no longer full size, and has a black box to the bottom left of it.

GraphicsManager.java

package com.jackwilsdon.spectrun;

import java.io.IOException;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector2f;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

public class GraphicsManager {

    final static int WIDTH = 800;
    final static int HEIGHT = 600;

    private static Texture cloudTexture = null;

    public static void initDisplay() throws LWJGLException
    {
        Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
        Display.create();
    }

    public static void initOpenGL()
    {    
        GL11.glEnable(GL11.GL_TEXTURE);  
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, WIDTH, 0, HEIGHT, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
    }

    public static void loadTextures()
    {
        Texture currentObject = null;
        try {
            currentObject = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("./resources/cloud.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Texture loaded: "+currentObject);
        System.out.println(">> Image width: "+currentObject.getImageWidth());
        System.out.println(">> Image height: "+currentObject.getImageHeight());
        System.out.println(">> Texture width: "+currentObject.getTextureWidth());
        System.out.println(">> Texture height: "+currentObject.getTextureHeight());
        System.out.println(">> Texture ID: "+currentObject.getTextureID());
        cloudTexture = currentObject;
    }

    public static void DrawRectangle(int x, int y, int width, int height)
    {
        GL11.glBegin(GL11.GL_QUADS);
            GL11.glVertex2f(x,y);
            GL11.glVertex2f(x+width,y);
            GL11.glVertex2f(x+width,y+height);
            GL11.glVertex2f(x,y+height);
        GL11.glEnd();
    }

    public static void DrawRectangle(int x, int y, int width, int height, int red, int green, int blue)
    {
        GL11.glColor3ub((byte)red, (byte)green, (byte)blue);
        DrawRectangle(x, y, width, height);
    }

    public static Vector2f DrawCloud(int x, int y)
    {
        cloudTexture.bind();
        int width = cloudTexture.getImageWidth();
        int height = cloudTexture.getImageHeight();
        GL11.glBegin(GL11.GL_QUADS);
            GL11.glTexCoord2f(1,1);
            GL11.glVertex2f(x,y);
            GL11.glTexCoord2f(0,1);
            GL11.glVertex2f(x+width,y);
            GL11.glTexCoord2f(0,0);
            GL11.glVertex2f(x+width,y+height);
            GL11.glTexCoord2f(1,0);
            GL11.glVertex2f(x,y+height);
        GL11.glEnd();
        return new Vector2f(cloudTexture.getImageWidth(), cloudTexture.getImageHeight());
    }
}

Solution

  • It doesn't help that you aren't calling GL11.glEnable( GL11.GL_TEXTURE ) and you also aren't setting any texture coordinates.

    Both of these will rather mess up your ability to see a texture on the quad ...