OpenGL立方体顶点是错误的

时间:2013-09-22 16:06:43

标签: java opengl rendering lwjgl

我一直试图解决我的立方体顶点不正确的问题,现在没有太多运气。我尝试了大约10个不同的顶点数组,但没有一个有效。问题是,我真的不明白有人如何找出数字在哪里,所以我不能自己调试它。

这是我目前的代码。我认为对熟悉OpenGL的人来说相对简单,但是如果你有任何问题请问。

编辑:现在的问题是纹理显示错误。代码和结果图片已经过编辑以反映这种变化。

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

如果您需要更多信息,请告诉我,我一直在努力解决这个问题。我知道GL_QUADS已被弃用,但我真的很想让它运行起来。

1 个答案:

答案 0 :(得分:0)

您正在使用GL_QUADS进行绘制,因此几何体必须定义四边形。

这不是四边形。第3个坐标交叉回x = 1.0,对角线分割四边形。你需要以逆时针顺序制作一个四边形的形状。

// 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

如果你交换顶点2和顶点3,你将有一个四边形。

我认为您可能会使用三角形条复制示例?但是认真地......拿一支铅笔和一些纸,然后画出来并自己动手制作几何图形。这比从网络上复制随机顶点数组要快得多。如果订单错误(逆时针),请先关闭面部剔除。

请注意,在较新的opengl版本中不推荐使用GL_QUADS。三角形或三角形条是首选。

纹理坐标:

  • 0.0,0.0是左下角
  • 1.0,1.0是右上角

第一个四边形的纹理坐标将是:

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