0

我正在尝试在窗户上画一幅画,但它只画了一种颜色。

我的代码发布在下面。

纹理管理器:-

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

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

4

1 回答 1

2

您需要首先启用纹理使用

    glEnable(GL_TEXTURE_2D)

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

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

希望这可以帮助。

于 2012-06-20T20:13:59.500 回答