Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

java - OpenGL cube vertices are wrong

I've been trying to solve the problem of my cube vertices being incorrect for well over a day now without much luck. I've tried about 10 different vertex arrays, but none have worked. The problem is that I don't really understand how someone goes about figuring out what number goes where, so I can't really debug it myself.

Here is my code at the moment. I think it's relatively straightforward for someone familiar with OpenGL, but if you have any questions ask.

EDIT: The problem now is that the texture is showing up wrong.The code and result picture have been edited to reflect this change.

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)

    };
    System.out.println(vertices.length);
    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){
    if(amt == 0){
        amt = 1;
    }else if(!canDraw){
        return;
    }

    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();
}

The output looks something like this.

If you need any more information please let me know, I've been struggling with this a lot. I know GL_QUADS is deprecated but I'd really like to get this running.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are drawing using GL_QUADS so your geometry must define quads.

This is not a quad. The 3rd coordinate crosses back to x = 1.0 splitting the quad diagonally. You need to actually make a shape of a quad in counter clockwise order.

// 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 2
-1.0f, -1.0f, 1.0f,        // vertex 3

If you swap vertex 2 and vertex 3, you will have a quad for that side.

I think you might be copying from examples using triangle strips? But seriously.. get a pencil and some paper and just draw it and manually make the geometry yourself. That's much faster than just copying random vertex arrays from the web. Turn off face culling initially in case you get the order wrong (counter clockwise).

Note that GL_QUADS are deprecated in newer opengl versions. Triangles or triangle strips are preferred.

Texture coordinates :

  • 0.0, 0.0 is the lower left corner
  • 1.0, 1.0 is the upper right corner

Texture coordinates for the first quad will then be :

1.0, 1.0
0.0, 1.0
0.0, 0.0
1.0, 0.0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...