简体   繁体   English

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

[英]How can I draw a texture on an OpenGL/LWJGL display list?

I have got a display list. 我有一个显示列表。 (Yes, i get the fact that they are depricated already. I have reasons for using them.) (是的,我知道它们已经被贬低了。我有使用它们的理由。)

How can I display textures on the display list? 如何在显示列表上显示纹理?

While I would prefer to have different textures at different locations, actually HAVING A TEXTURE on the display list would be a great start and for a game in closed alpha testing might actually be good enough until a better solution was found. 虽然我希望在不同的位置使用不同的纹理,但实际上在显示列表上具有纹理将是一个不错的开始,对于封闭式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();

If you can't read my poorly formatted code, then I am using a GL_TRIANGLE_STRIP to render stuff from a .bmp file, if it makes any difference. 如果您无法阅读格式错误的代码,那么我将使用GL_TRIANGLE_STRIP来渲染.bmp文件中的内容(如果有任何区别)。

  1. Use GL_NEAREST / GL_LINEAR for GL_TEXTURE_MIN_FILTER . GL_NEAREST / GL_LINEAR用于GL_TEXTURE_MIN_FILTER Or provide some mipmaps. 或提供一些mipmap。 Failure to do one of those two will result in an incomplete texture . 不做这两个之一将导致纹理不完整
  2. glEnable(GL_TEXTURE_2D) . glEnable(GL_TEXTURE_2D) Without that OpenGL will continue to ignore any bound textures. 否则,OpenGL将继续忽略任何绑定的纹理。
  3. Provide some texture coordinates before each glVertex() call, perhaps via glTexCoord() . 在每次调用glVertex()之前,可能要通过glTexCoord()提供一些纹理坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM