LWJGL VBO 三角形纹理坐标不起作用
Posted
技术标签:
【中文标题】LWJGL VBO 三角形纹理坐标不起作用【英文标题】:LWJGL VBO Triangle Texture Coordinates Not Working 【发布时间】:2014-01-30 19:49:12 【问题描述】:由于使用三角形渲染比使用四边形渲染更快,我决定将我的 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】:如果将顶点划分为三角形,则必须将纹理坐标也划分为三角形。这意味着,每个面需要 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();
我还看到,每个四边形的一个三角形是顺时针定义的,而另一个是逆时针定义的。如果您使用剔除面,您将只能看到其中一个三角形。
【讨论】:
以上是关于LWJGL VBO 三角形纹理坐标不起作用的主要内容,如果未能解决你的问题,请参考以下文章