【问题标题】:How can I draw a texture on an OpenGL/LWJGL display list?如何在 OpenGL/LWJGL 显示列表上绘制纹理?
【发布时间】:2014-08-11 20:40:25
【问题描述】:

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

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

虽然我更希望在不同的位置有不同的纹理,但实际上在显示列表上显示一个纹理将是一个很好的开始,并且对于封闭 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 文件中的内容,如果它有任何区别的话。

【问题讨论】:

  • 您似乎没有提供纹理坐标启用纹理。
  • @genpfault 请告诉我该怎么做!我已经尝试了所有纹理和坐标的东西,但是我不知道它如何与显示列表一起使用,或者因为我使用 GL_TRIANGLE_STRIP,所以我什至将坐标设置为(或者我将如何设置它们)。我已经尝试了我所知道的一切,所以肯定有一些我不知道的东西。一如既往,感谢您抽出宝贵时间。

标签: java opengl lwjgl


【解决方案1】:
  1. GL_NEAREST/GL_LINEAR 用于GL_TEXTURE_MIN_FILTER。或者提供一些mipmaps。未能执行这两项中的一项将导致incomplete texture
  2. glEnable(GL_TEXTURE_2D)。没有那个 OpenGL 将继续忽略任何绑定的纹理。
  3. 在每个glVertex() 调用之前提供一些纹理坐标,可能通过glTexCoord()

【讨论】:

  • 我认为第 3 步无法使用显示列表?或者是吗?另外,如何在超过 1000 个顶点的 gl 三角形条上设置纹理坐标?
  • 很有可能,glTexCoord() 调用会很好地完成。您正在考虑glTexCoordPointer(),它将是"executed immediately, regardless of the display-list mode"
  • 您一次设置一个坐标,就像您的glVertex() 调用一样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-13
  • 1970-01-01
  • 2018-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多