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

标签 java image opengl

我试图在窗口上绘制一幅画,但它只绘制了其中的一种颜色。

我的代码发布在下面。

纹理管理器:-

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

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

最佳答案

您需要首先使用启用纹理

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

希望这有帮助。

关于Java OpenGL 仅绘制一种颜色而不是图像中的所有颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11126489/

相关文章:

java - 添加具有多个 Arrylists 的类对象

java - 带分页的 API 的 Stream 与 Iterator

java - 无法让 DrawImage 在 Java 中工作

performance - 替换现有 VAO 中的 VBO

c++ - Ubuntu 14.04 中的 OpenGL 4.3 开发设置

java - 将 TextureView 添加到 main.xml : Android

java - java中以_命名的字符串声明

java - 在单个图像中检测多个图像

python - 如何检测 table 的水平线和垂直线并消除噪音?

opengl - 着色器 - 镜面闪光 - 闪闪发光的效果