LWJGL VBO三角形纹理坐标不工作

时间:2014-01-30 19:49:12

标签: textures coordinates lwjgl vbo

由于使用三角形渲染比使用四边形渲染更快,因此我决定将6个四边形面分成12个总三角形。我坚持的部分是我不知道如何将四边形纹理应用于三角形表面。下图是我的尝试,但看起来很奇怪。任何人都可以解释发生了什么,并告诉我如何解决这个问题?

屏幕截图:http://i.snag.gy/2ZenI.jpg

BlockFace类:

public class BlockFace {
    private final int amountOfVertices = 3;
    private final int vertexSize = 3;
    private final int textureSize = 2;
    private final int amountOfFaces = 2;

    private int vbo; // vertices
    private int tbo; // texture coordinates

    public BlockFace(float[] vertices) {
        FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize * amountOfFaces);
        vertexData.put(vertices);
        vertexData.flip();

        FloatBuffer textureData = BufferUtils.createFloatBuffer(4 * textureSize * amountOfFaces);
        textureData.put(new float[] {
                0, 0,
                1, 0,
                1, 1,
                0, 1,

                0, 0,
                1, 0,
                1, 1,
                0, 1
        });
        textureData.flip();

        vbo = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        tbo = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, tbo);
        glBufferData(GL_ARRAY_BUFFER, textureData, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }

    public void draw(Texture texture) {
        texture.bind();

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);

        glBindBuffer(GL_ARRAY_BUFFER, tbo);
        glTexCoordPointer(textureSize, GL_FLOAT, 0, 0L);

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glDrawArrays(GL_TRIANGLES, 0, amountOfVertices * 2);
        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    }
}

BlockRenderer类:

public class BlockRenderer {
    private BlockFace topBlockFace;
    private BlockFace bottomBlockFace;
    private BlockFace frontBlockFace;
    private BlockFace backBlockFace;
    private BlockFace leftBlockFace;
    private BlockFace rightBlockFace;

    public BlockRenderer() {
        topBlockFace = new BlockFace(new float[] {
            0, 1, 0,
            1, 1, 0,
            0, 1, 1,

            1, 1, 1,
            1, 1, 0,
            0, 1, 1
        });
        bottomBlockFace = new BlockFace(new float[] {
            0, 0, 0,
            1, 0, 0,
            0, 0, 1,

            1, 0, 1,
            1, 0, 0,
            0, 0, 1
        });
        frontBlockFace = new BlockFace(new float[] {
            0, 0, 1,
            0, 1, 1,
            1, 1, 1,

            0, 0, 1,
            1, 0, 1,
            1, 1, 1
        });
        backBlockFace = new BlockFace(new float[] {
            0, 0, 0,
            0, 1, 0,
            1, 1, 0,

            0, 0, 0,
            1, 0, 0,
            1, 1, 0
        });
        leftBlockFace = new BlockFace(new float[] {
            0, 1, 0,
            0, 1, 1,
            0, 0, 1,

            0, 1, 0,
            0, 0, 0,
            0, 0, 1
        });
        rightBlockFace = new BlockFace(new float[] {
            1, 1, 0,
            1, 1, 1,
            1, 0, 1,

            1, 1, 0,
            1, 0, 0,
            1, 0, 1
        });
    }

    public void renderBlock(Block block, float x, float y, float z) {
        glPushMatrix();
        glTranslatef(x, y, z);

        topBlockFace.draw(block.getTexture());
        bottomBlockFace.draw(block.getTexture());
        frontBlockFace.draw(block.getTexture());
        backBlockFace.draw(block.getTexture());
        leftBlockFace.draw(block.getTexture());
        rightBlockFace.draw(block.getTexture());

        glPopMatrix();
    }
}

1 个答案:

答案 0 :(得分:0)

如果将顶点划分为三角形,则必须将纹理坐标划分为三角形。这意味着,每个面需要6个纹理坐标,就像每个面有6个顶点一样。

对于您的示例,您需要更改为:

FloatBuffer textureData = BufferUtils.createFloatBuffer(amountOfVertices * textureSize * amountOfFaces);
for(int i = 0; i < amountOfFaces; i++) {
    textureData.put(new float[] {
            0, 0,
            1, 0,
            0, 1,

            1, 1,
            1, 0,
            0, 1
    });
}
textureData.flip();

我进一步看到,每个四边形的一个三角形是顺时针定义的,而另一个是逆时针定义的。如果使用剔除面,则只能看到其中一个三角形。