OpenGL立方体顶点错误 [英] OpenGL cube vertices are wrong

查看:116
本文介绍了OpenGL立方体顶点错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试解决我的多维数据集顶点不正确的问题,这已经有一天多了,运气不佳.我已经尝试了大约10种不同的顶点数组,但是没有一个奏效.问题是我不太了解某人如何弄清楚数字到底去哪里了,所以我自己也无法真正调试它.

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.

此刻是我的代码.我认为对于熟悉OpenGL的人来说,这相对简单,但是如果您有任何问题要问.

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.

现在的问题是纹理显示不正确.对代码和结果图片进行了编辑以反映此更改.

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

输出看起来像这样.

如果您需要更多信息,请告诉我,我一直在为此苦苦挣扎.我知道GL_QUADS已过时,但我真的很想运行它.

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.

推荐答案

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

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

这不是四边形.第三个坐标交叉回到x = 1.0,对角线分割四边形.您实际上需要按逆时针顺序制作一个四边形的形状.

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

如果交换顶点2和顶点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).

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

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

纹理坐标:

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

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

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

这篇关于OpenGL立方体顶点错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆