Java OpenGL只绘制一种颜色而不是图像中的所有颜色

时间:2012-06-20 19:08:41

标签: java image opengl

我正在尝试将图片绘制到窗口,但它只绘制了一种颜色。

我的代码发布在下面。

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

消耗: -

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

在你问之前,我没有收到任何错误,代码很好,只是图像。 :d

编辑: - 我无法添加图片! :'(

1 个答案:

答案 0 :(得分:2)

您需要先使用

启用纹理
    glEnable(GL_TEXTURE_2D)

另外:),您没有为OpenGL提供纹理坐标(请参阅此处texturing)。您的绘制调用应如下所示:

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

希望这有帮助。