【问题标题】:LWJGL Screwed texturesLWJGL 螺旋纹理
【发布时间】:2017-04-10 23:49:15
【问题描述】:

我目前正在使用 java 和 lwjgl 开发游戏,并在我的主菜单中尝试绘制背景图像。出于某种原因,无论我使用什么纹理加载和绘制技术,图像都会完全搞砸。

会发生这样的事情:

这就是它应该的样子:

这是我加载纹理的代码:

  private int loadTexture(String imgName) {
    try {
        BufferedImage img = ImageIO.read(JarStreamLoader.load(imgName));
        ByteBuffer buffer = BufferUtils.createByteBuffer(img.getWidth() * img.getHeight() * 3);
        for (int x = 0; x < img.getWidth(); x++) {
            for (int y = 0; y < img.getHeight(); y++) {
                Color color = new Color(img.getRGB(x, y));
                buffer.put((byte) color.getRed());
                buffer.put((byte) color.getGreen());
                buffer.put((byte) color.getBlue());
            }
        }
        buffer.flip();
        int textureId = glGenTextures();
        glBindTexture(GL_TEXTURE_2D, textureId);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.getWidth(), img.getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, buffer);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        return textureId;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

这是我的渲染代码:

public static void drawRect(int x, int y, int width, int height, Color color) {
    glColor4f(color.getRed() / 255, color.getGreen() / 255, color.getBlue() / 255, 1.0F);
    glBegin(GL_QUADS);

    glTexCoord2f(0.0f, 0.0f);
    glVertex2d(x, y);

    glTexCoord2f(0.0f, 1.0F);
    glVertex2d(x, y + height);

    glTexCoord2f(1.0F, 1.0F);
    glVertex2d(x + width, y + height);

    glTexCoord2f(1.0F, 0.0f);
    glVertex2d(x + width, y);

    glEnd();
}

有什么想法吗?

【问题讨论】:

    标签: java opengl lwjgl


    【解决方案1】:

    您以错误的顺序添加像素。您需要按以下顺序进行:

    for (int y = 0; y < img.getHeight(); y++)
        for (int x = 0; x < img.getWidth(); x++)
    

    请注意,OpenGL 的原点位于左下角,因此您可能还需要在 y 轴上翻转图像:

    Color color = new Color(img.getRGB(x, img.getHeight() - y - 1));
    

    【讨论】:

      猜你喜欢
      • 2014-07-02
      • 1970-01-01
      • 2015-07-12
      • 2014-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-27
      相关资源
      最近更新 更多