Taylor Golden
Taylor Golden

Reputation: 41

Java OpenGL only drawing one colour instead of all the colours from an image

I am trying to draw a picture to the window but it is only painting one colour of it.

My code is posted below.

TextureManager:-

package oregon.src;

import oregon.client.*;

import java.io.*;
import java.util.*;

import org.newdawn.slick.opengl.*;

public class TextureManager {
    private static HashMap<String, Texture> textures = new HashMap<String, Texture>();

    public static Oregon oregon = new Oregon();

    public static boolean loadTexture(String path, String name) {
        Texture texture = null;

        try {
            if ((texture = TextureLoader.getTexture("PNG", new FileInputStream(path))) != null) {
                textures.put(name, texture);

                return true;
            }
        } catch (FileNotFoundException e) {
            oregon.stop(e);
        } catch (IOException e1) {
            oregon.stop(e1);
        }

        return false;
    }

    public static Texture getTexture(String name) {
        if (textures.containsKey(name)) {
            return textures.get(name);
        }

        return null;
    }
}

Draw:-

package oregon.src;

import static org.lwjgl.opengl.GL11.*;

public class Draw {
    public static Settings settings = new Settings();

    public static void renderBlock(String path, String name, int coord1, int coord2) {
        if (settings.testing) {
            path = settings.pathWhilstTesting + path;
        } else if (!settings.testing) {
            path = settings.pathWhilstUsing + path;
        }

        TextureManager.loadTexture(path, name);

        glBindTexture(GL_TEXTURE_2D, TextureManager.getTexture(name).getTextureID());
        glBegin(GL_QUADS);
            glVertex2i(coord1, coord1);
            glVertex2i(coord1, coord2);
            glVertex2i(coord2, coord2);
            glVertex2i(coord2, coord1);
        glEnd();
    }
}

Before you ask, I'm not getting any errors, the code is fine, it's just the image. :D

EDIT:- I can't add images! :'(

Upvotes: 0

Views: 185

Answers (1)

Ani
Ani

Reputation: 10906

You need to first enable texturing using

    glEnable(GL_TEXTURE_2D)

Also :), You aren't supplying OpenGL with texture coordinates (see texturing here). Your draw call should look like this:

    glBegin(GL_QUADS);
        glTexcoord2f(0, 0);
        glVertex2i(coord1, coord1);

        glTexcoord2f(0, 1);
        glVertex2i(coord1, coord2);

        glTexcoord2f(1, 1);
        glVertex2i(coord2, coord2);

        glTexcoord2f(1, 0);
        glVertex2i(coord2, coord1);
    glEnd();

Hope this helps.

Upvotes: 2

Related Questions