如何在不同的墙壁上放置不同的纹理? LWJGL

时间:2014-05-27 14:09:41

标签: java opengl textures lwjgl

到目前为止,我已经拥有它,所以你在一个盒子里跑来跑去,但是所有的墙都是相同的纹理!我已经为墙壁加载了其他纹理(我希望墙壁的纹理与地板不同)但似乎它被忽略了......这就是我的代码:

        int floorTexture = glGenTextures();
    {
        InputStream in = null;
        try {               
            in = new FileInputStream("floor.png");
            PNGDecoder decoder = new PNGDecoder(in);
            ByteBuffer buffer = BufferUtils.createByteBuffer(4 * decoder.getWidth() * decoder.getHeight());
            decoder.decode(buffer, decoder.getWidth() * 4, Format.RGBA);
            buffer.flip();
            glBindTexture(GL_TEXTURE_2D, floorTexture);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA,
                    GL_UNSIGNED_BYTE, buffer);
            glBindTexture(GL_TEXTURE_2D, floorTexture);


        } catch (FileNotFoundException ex) {
            System.err.println("Failed to find the texture files.");
            ex.printStackTrace();
            Display.destroy();
            System.exit(1);
        } catch (IOException ex) {
            System.err.println("Failed to load the texture files.");
            ex.printStackTrace();
            Display.destroy();
            System.exit(1);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    int wallTexture = glGenTextures();
    {
        InputStream in = null;
        try {               
            in = new FileInputStream("walls.png");
            PNGDecoder decoder = new PNGDecoder(in);
            ByteBuffer buffer = BufferUtils.createByteBuffer(4 * decoder.getWidth() * decoder.getHeight());
            decoder.decode(buffer, decoder.getWidth() * 4, Format.RGBA);
            buffer.flip();
            glBindTexture(GL_TEXTURE_2D, wallTexture);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA,
                    GL_UNSIGNED_BYTE, buffer);
            glBindTexture(GL_TEXTURE_2D, wallTexture);


        } catch (FileNotFoundException ex) {
            System.err.println("Failed to find the texture files.");
            ex.printStackTrace();
            Display.destroy();
            System.exit(1);
        } catch (IOException ex) {
            System.err.println("Failed to load the texture files.");
            ex.printStackTrace();
            Display.destroy();
            System.exit(1);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }       
    int ceilingDisplayList = glGenLists(1);
    glNewList(ceilingDisplayList, GL_COMPILE);
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glVertex3f(-gridSize, ceilingHeight, -gridSize);
    glTexCoord2f(gridSize * 10 * tileSize, 0);
    glVertex3f(gridSize, ceilingHeight, -gridSize);
    glTexCoord2f(gridSize * 10 * tileSize, gridSize * 10 * tileSize);
    glVertex3f(gridSize, ceilingHeight, gridSize);
    glTexCoord2f(0, gridSize * 10 * tileSize);
    glVertex3f(-gridSize, ceilingHeight, gridSize);
    glEnd();
    glEndList();

    int wallDisplayList = glGenLists(1);
    glNewList(wallDisplayList, GL_COMPILE);

    glBegin(GL_QUADS);

    // North wall

    glTexCoord2f(0, 0);
    glVertex3f(-gridSize, floorHeight, -gridSize);
    glTexCoord2f(0, gridSize * 10 * tileSize);
    glVertex3f(gridSize, floorHeight, -gridSize);
    glTexCoord2f(gridSize * 10 * tileSize, gridSize * 10 * tileSize);
    glVertex3f(gridSize, ceilingHeight, -gridSize);
    glTexCoord2f(gridSize * 10 * tileSize, 0);
    glVertex3f(-gridSize, ceilingHeight, -gridSize);

    // West wall

    glTexCoord2f(0, 0);
    glVertex3f(-gridSize, floorHeight, -gridSize);
    glTexCoord2f(gridSize * 10 * tileSize, 0);
    glVertex3f(-gridSize, ceilingHeight, -gridSize);
    glTexCoord2f(gridSize * 10 * tileSize, gridSize * 10 * tileSize);
    glVertex3f(-gridSize, ceilingHeight, +gridSize);
    glTexCoord2f(0, gridSize * 10 * tileSize);
    glVertex3f(-gridSize, floorHeight, +gridSize);

    // East wall

    glTexCoord2f(0, 0);
    glVertex3f(+gridSize, floorHeight, -gridSize);
    glTexCoord2f(gridSize * 10 * tileSize, 0);
    glVertex3f(+gridSize, floorHeight, +gridSize);
    glTexCoord2f(gridSize * 10 * tileSize, gridSize * 10 * tileSize);
    glVertex3f(+gridSize, ceilingHeight, +gridSize);
    glTexCoord2f(0, gridSize * 10 * tileSize);
    glVertex3f(+gridSize, ceilingHeight, -gridSize);

    // South wall

    glTexCoord2f(0, 0);
    glVertex3f(-gridSize, floorHeight, +gridSize);
    glTexCoord2f(gridSize * 10 * tileSize, 0);
    glVertex3f(-gridSize, ceilingHeight, +gridSize);
    glTexCoord2f(gridSize * 10 * tileSize, gridSize * 10 * tileSize);
    glVertex3f(+gridSize, ceilingHeight, +gridSize);
    glTexCoord2f(0, gridSize * 10 * tileSize);
    glVertex3f(+gridSize, floorHeight, +gridSize);

    glEnd();

    glEndList();

    int floorDisplayList = glGenLists(1);
    glNewList(floorDisplayList, GL_COMPILE);
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glVertex3f(-gridSize, floorHeight, -gridSize);
    glTexCoord2f(0, gridSize * 10 * tileSize);
    glVertex3f(-gridSize, floorHeight, gridSize);
    glTexCoord2f(gridSize * 10 * tileSize, gridSize * 10 * tileSize);
    glVertex3f(gridSize, floorHeight, gridSize);
    glTexCoord2f(gridSize * 10 * tileSize, 0);
    glVertex3f(gridSize, floorHeight, -gridSize);
    glEnd();
    glEndList();

1 个答案:

答案 0 :(得分:2)

您需要在相应的glBegin()之前绑定要用于给定批量几何的纹理。

除非您告诉OpenGL,否则将使用最新的绑定(在您的情况下为glBindTexture(GL_TEXTURE_2D, wallTexture))。

这样的事情:

int floorDisplayList = glGenLists(1);
glNewList(floorDisplayList, GL_COMPILE);
glBindTexture(GL_TEXTURE_2D, floorTexture);  // the important bit
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3f(-gridSize, floorHeight, -gridSize);
glTexCoord2f(0, gridSize * 10 * tileSize);
glVertex3f(-gridSize, floorHeight, gridSize);
glTexCoord2f(gridSize * 10 * tileSize, gridSize * 10 * tileSize);
glVertex3f(gridSize, floorHeight, gridSize);
glTexCoord2f(gridSize * 10 * tileSize, 0);
glVertex3f(gridSize, floorHeight, -gridSize);
glEnd();
glEndList();