0
由於用三角形渲染比用四邊形渲染要快,因此我決定將我的6個四邊形面分成12個總三角形。我堅持的部分是我不知道如何將三角形紋理應用到三角形表面。下圖是我的嘗試,但看起來很奇怪。任何人都可以解釋發生了什麼,並告訴我如何解決這個問題?LWJGL VBO三角形紋理座標不工作
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();
}
}