有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java OpenGL/LWJGL纹理加载,但纹理不会在四边形上渲染

我开始在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纹理的纹理对象(但在本例中是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();
}

共 (1) 个答案

  1. # 1 楼答案

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

    • 你使用哪个opengl版本glBegin()glEnd()从较新的版本(3.2以后)中删除。相反,您需要将顶点数据上载到顶点缓冲区对象。然后使用glvertexattributepointer告诉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;
    }
    

    希望这对你有帮助