我一直试图解决我的立方体纹理顶点不正确的问题,现在一天没有多少运气。纹理显示错误,每个边都不一样。OpenGL纹理拉伸和错误。 VBO顶点不正确?
这是我目前的代码。我认为对熟悉OpenGL的人来说相对简单,但是如果您有任何疑问,请提问。
上次我发布这个前几天我是downvoted,没有得到答案;如果我没有给你足够的信息或做错什么,请告诉我。
private boolean hasBeenRendered = false;
private int amountOfVertices;
private int vertexSize;
private int textureSize;
private int vboVertexHandle;
private int vboTextureHandle;
private boolean canDraw = false;
public Block(BlockType type, Location loc) {
this.type = type;
this.loc = loc;
initRendering();
}
private void initRendering() {
amountOfVertices = 24;
vertexSize = 3;
textureSize = 2;
FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
float[] vertices = {
// X Y Z R G B
// face 0:
1.0f, 1.0f, 1.0f, // vertex 0
- 1.0f, 1.0f, 1.0f, // vertex 1
- 1.0f, - 1.0f, 1.0f, // vertex 3
1.0f, - 1.0f, 1.0f, // vertex 2
// face 1:
1.0f, 1.0f, 1.0f, // vertex 0
1.0f, - 1.0f, 1.0f, // vertex 1
1.0f, - 1.0f, - 1.0f, // vertex 3
1.0f, 1.0f, - 1.0f, // vertex 2
// face 2:
1.0f, 1.0f, 1.0f, // vertex 0
1.0f, 1.0f, - 1.0f, // vertex 1
- 1.0f, 1.0f, - 1.0f, // vertex 3
- 1.0f, 1.0f, 1.0f, // vertex 2
// face 3:
1.0f, 1.0f, - 1.0f, // vertex 0
1.0f, - 1.0f, - 1.0f, // vertex 1
- 1.0f, - 1.0f, - 1.0f, // vertex 3
- 1.0f, 1.0f, - 1.0f, // vertex 2
// face 4:
- 1.0f, 1.0f, 1.0f, // vertex 0
- 1.0f, 1.0f, - 1.0f, // vertex 1
- 1.0f, - 1.0f, - 1.0f, // vertex 3
- 1.0f, - 1.0f, 1.0f, // vertex 2
// face 5:
1.0f, - 1.0f, 1.0f, // vertex 0
- 1.0f, - 1.0f, 1.0f, // vertex 1
- 1.0f, - 1.0f, - 1.0f, // vertex 3
1.0f, - 1.0f, - 1.0f, // vertex 2
// 6 faces with 4 vertices with 6 components (floats)
};
vertexData.put(vertices);
vertexData.flip();
FloatBuffer textureData = BufferUtils.createFloatBuffer(amountOfVertices * textureSize);
textureData.put(new float[] {
1, 1, 0, 1, 0, 0, 1, 0,
1, 1, 0, 1, 0, 0, 1, 0,
1, 1, 0, 1, 0, 0, 1, 0,
1, 1, 0, 1, 0, 0, 1, 0,
1, 1, 0, 1, 0, 0, 1, 0,
1, 1, 0, 1, 0, 0, 1, 0,
});
textureData.flip();
vboVertexHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
vboTextureHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboTextureHandle);
glBufferData(GL_ARRAY_BUFFER, textureData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
@Override
public void render() {
// if(! hasBeenRendered) {
canDraw = true;
glPushMatrix();
{
glTranslatef(loc.getX(), loc.getY(), loc.getZ());
//glRotatef(x, 1, 1, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
//glBindTexture(GL_TEXTURE, type.getTexture().getTextureID());
glBindTexture(GL_TEXTURE_2D, type.getTexture().getTextureID());
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);
glBindTexture(GL_ARRAY_BUFFER, vboTextureHandle);
glTexCoordPointer(textureSize, GL_FLOAT, 0, 0L);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_QUADS, 0, amountOfVertices);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
glPopMatrix();
//hasBeenRendered = true; }
}
The output looks something like this。
如果您需要更多信息,请让我知道,我一直在为此付出很多努力。我知道GL_QUADS已被弃用,但我真的想让这个运行。
[OpenGL立方体顶点错误]的可能重复(http://stackoverflow.com/questions/18945859/opengl-cube-vertices-are-wrong) – genpfault
它不是。同一代码不同的问题。即便如此,也不像任何人会回头找到问题并添加答案。 – user2804551
你可以发布你的渲染结果吗? – hiroki