【问题标题】:OpenGL / LWJGL texture loads, but texture won't render on quadOpenGL / LWJGL 纹理加载,但纹理不会在四边形上渲染
【发布时间】:2018-05-15 07:04:45
【问题描述】:

我开始在 Java 中使用 LWJGL/OpenGL 来创建 2D 游戏,但是我在渲染一个简单的 32x32 四边形(正方形)时遇到了困难。我创建了一个纹理加载系统,并按照正确的纹理说明进行操作,但纹理不会显示。我的代码如下:

devBlock64.bind();

glEnable(GL_TEXTURE_2D);

GL11.glBegin(GL11.GL_QUADS);
    GL11.glTexCoord2f(0, 0);
    GL11.glVertex2f(0, 0);

    GL11.glTexCoord2f(0, 1);
    GL11.glVertex2f(32, 0);

    GL11.glTexCoord2f(1, 1);
    GL11.glVertex2f(32, 32);

    GL11.glTexCoord2f(1, 0);
    GL11.glVertex2f(0, 32);
GL11.glEnd();

glDisable(GL_TEXTURE_2D);

以上是每次调用“render”时的代码。 “devBlock64”只是一个加载了 64x64 纹理的 Texture 对象(但在本例中是 32x32,因为我将其保存为错误的大小)

此外,这是我在加载纹理并生成其纹理 ID 后调用的选项和函数:

this.bind();

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

图片加载正常,但不会显示。 this.bind(); 调用我的函数来执行此操作:glBindTexture(GL_TEXTURE_2D, id):

而且,如果有人好奇的话,我的游戏循环,其他一切都是根据 OpenGL 的设置窗口教程完成的:

public void loop() {
    // Binds GLFW with OpenGL
    GL.createCapabilities();

    glOrtho(0f, 800, 600, 0f, 0f, 1f);

    glClearColor(1.0f, 1f, 1f, 1f);
    //glLoadIdentity();

    world.loadTextures();

    while(!hasWindowRequestedClose()) {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        world.onUpdate();
        world.render();

        glfwSwapBuffers(handle);

        glfwPollEvents();
    }

    destroy();
}

【问题讨论】:

  • 您希望将四边形安装在窗口中吗?
  • 不,我想对四边形进行纹理处理。我编辑了标题以消除任何混乱。
  • 使用 BufferUtils 加载的 ByteBuffer。

标签: java opengl textures lwjgl texturing


【解决方案1】:

嘿,我检查了您的代码,我的一切似乎都很好。我有一些建议。

  • 您使用哪个 opengl 版本? glBegin()glEnd() 已从较新版本(3.2 及更高版本)中删除。相反,您需要将顶点数据上传到顶点缓冲区对象。然后使用 glVertexAttribPointer 告诉 openGL 数据是如何布局的。

  • 更好的方法是使用着色器并将纹理绑定到着色器。

这是我加载纹理的代码。

public static int loadTexture(String path) {
 int[] pixels = null;
 int width = 0;
 int height = 0;
 try {
  InputStream resourceBuff = Loader.class.getResourceAsStream(path);
  BufferedImage image = ImageIO.read(resourceBuff);
  width = image.getWidth();
  height = image.getHeight();
  pixels = new int[width * height];
  image.getRGB(0, 0, width, height, pixels, 0, width);
 } catch (IOException e) {
  e.printStackTrace();
 }

 int[] data = new int[width * height];
 for (int i = 0; i < width * height; i++) {
  int a = (pixels[i] & 0xff000000) >> 24;
  int r = (pixels[i] & 0xff0000) >> 16;
  int g = (pixels[i] & 0xff00) >> 8;
  int b = (pixels[i] & 0xff);

  data[i] = a << 24 | b << 16 | g << 8 | r;
 }

 int result = glGenTextures();
 glBindTexture(GL_TEXTURE_2D, result);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
    storeDataInIntBuffer((data)));
 glBindTexture(GL_TEXTURE_2D, 0);
 return result;
}

希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    相关资源
    最近更新 更多