java - 如何在 OpenGL/LWJGL 显示列表上绘制纹理?

标签 java opengl lwjgl

我有一个显示列表。 (是的,我知道它们已经被贬值了。我有使用它们的理由。)

如何在显示列表中显示纹理?

虽然我更喜欢在不同的位置有不同的纹理,但实际上在显示列表上有一个纹理将是一个很好的开始,对于封闭 Alpha 测试的游戏来说实际上可能已经足够好了,直到找到更好的解决方案。

try {
        // Load the heightmap-image from its resource file
        System.out.println("Path: " + new File(System.getProperty("user.dir") + "/res/heightmap.bmp").getAbsolutePath());
        BufferedImage heightmapImage = ImageIO.read(new File(
                System.getProperty("user.dir") + "/res/heightmap.bmp"));

        width = heightmapImage.getWidth();
        height = heightmapImage.getHeight();
        BufferedImage heightmapColour = ImageIO.read(new File(System.getProperty("user.dir") +
                "/res/colours.bmp"));
        data = new float[heightmapImage.getWidth()][heightmapImage
                .getHeight()];

        red = new float[heightmapColour.getWidth()][heightmapColour
                .getHeight()];
        blue = new float[heightmapColour.getWidth()][heightmapColour
                .getHeight()];
        green = new float[heightmapColour.getWidth()][heightmapColour
                .getHeight()];
        Color colour;
        Color colours;
        PNGDecoder decoder = new PNGDecoder(heightmapLookupInputStream);
        ByteBuffer buffer = BufferUtils.createByteBuffer(4
                * decoder.getWidth() * decoder.getHeight());
        decoder.decode(buffer, decoder.getWidth() * 4,
                PNGDecoder.Format.RGBA);
        buffer.flip();
        heightmapLookupInputStream.close();
        lookupTexture = glGenTextures();
        glBindTexture(GL_TEXTURE_2D, lookupTexture);
        // Hand the texture data to OpenGL
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(),
                decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
    } catch (IOException e) {
        e.printStackTrace();

    }
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    heightmapDisplayList = glGenLists(1);
    glNewList(heightmapDisplayList, GL_COMPILE);

    for (int z = 0; z < data.length - 1; z++) {
        // Render a triangle strip for each 'strip'.
        glBegin(GL_TRIANGLE_STRIP);
        for (int x = 0; x < data[z].length; x++) {
            // Take a vertex from the current strip
            if (((blue[z][x] / 255) < 0.4))
                glColor4f((red[z][x] / 255) / 2
                        + (float) (Math.random() / 10), (green[z][x] / 255)
                        / 2 + (float) (Math.random() / 10),
                        (blue[z][x] / 255) / 2
                                + (float) (Math.random() / 10), 1);
            else {
                glColor4f((red[z][x] / 255) / 2
                        + (float) (Math.random() / 10), (green[z][x] / 255)
                        / 2 + (float) (Math.random() / 10),
                        (blue[z][x] / 255) / 2
                                + (float) (Math.random() / 10), 1);
            }
            if (data[z][x] >= WATER_LEVEL -10){

            glVertex3f(x, data[z][x], z);
            glVertex3f(x, data[z + 1][x], z + 1);

            }

        }
        glEnd();
    }
    glEndList();

如果您无法阅读我格式错误的代码,那么我将使用 GL_TRIANGLE_STRIP 来渲染 .bmp 文件中的内容(如果有什么不同的话)。

最佳答案

  1. 使用GL_NEAREST/GL_LINEAR作为GL_TEXTURE_MIN_FILTER。或者提供一些 mipmap。未能执行这两项中的一项将导致 incomplete texture .
  2. glEnable(GL_TEXTURE_2D)。如果没有这个,OpenGL 将继续忽略任何绑定(bind)的纹理。
  3. 在每次调用 glVertex() 之前提供一些纹理坐标,可能通过 glTexCoord() 实现。

关于java - 如何在 OpenGL/LWJGL 显示列表上绘制纹理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25252024/

相关文章:

java - 如果 Edittext 没有输入文本则提示 toast

linux - 如何在 RHEL 5 (Linux) 中卸载然后安装 Java 我遇到错误?

c# - 并排显示图像的一半 - OpenGL

java - 获取接下来三周的最后一天 Java

Java:如何在不使用除法或模运算符的情况下查找整数是否为 2 的倍数

c++ - 纹理模型

c++ - OpenGL VBO批处理最佳做法

java - 在 lwjgl 中删除 VBO

java - 多个纹理绑定(bind)到单个 VBO 并使用着色器渲染 (LWJGL)

java - LWJGL 令人困惑的纹理映射