OpenGL索引数组

时间:2015-02-12 20:53:44

标签: c++ opengl grid terrain

我有一个类terrain,它创建了一个Quad的网格。我是这样做的

for(int z=0; z<_length;z++){
            for(int x=0; x<_width;x++){
                vertices.push_back(vec3((float)x*250, 0.f, (float)z*250));
    }
}
for(int z=0; z<(_length-1);++z){
    for(int x=0; x<(_width-1);++x){
        int index = z*_width+x; 
        Vertex _vertices[] = {
            Vertex(vertices.at(index),vec3(0, 0, 0)),
            Vertex(vertices.at(index+1),vec3(0, 0, 0)),
            Vertex(vertices.at(index+_width),vec3(0, 0, 0)),
            Vertex(vertices.at(index+1+_width),vec3(0,0,0))
        };

        unsigned short indices[]= {index,index + 1,index + 
            _width,index + 1,index +  _width,index +  _width + 1};
        Quad quad(_vertices, 4, indices, 6);
        squares.push_back(quad);
        i++;
    }
}

顶点和逻辑是正确的,但由于某种原因,指数不是。这是此代码的输出: enter image description here 但是当我将这个指数改为:

            unsigned short indices[]= {0,1,2,1,2,3}; 

效果很好: enter image description here

问题是我不明白为什么这一行

        unsigned short indices[]= {index,index + 1,index + 
            _width,index + 1,index +  _width,index +  _width + 1};

不起作用。如果它工作,我的网格将消耗更少的资源。如果有人能解释我为什么它不起作用,那就太棒了,谢谢你。 如果你需要知道我如何画一个Quad,这里是代码:

class Quad{
public:
    Quad(Vertex *_vertices, int _n, unsigned short * _indices, unsigned short _numIndices){
        for(int i=0; i < _numIndices; i++){
            indices.push_back(_indices[i]);
        } 

        for(int i=0; i<_n; i++){
            vec3 v = vec3(_vertices[i].position, _lengthPower);
            position.push_back(v);                     
        }
        glGenVertexArrays(1, &mVertexArray);
        glBindVertexArray(mVertexArray);

        glGenBuffers(1, &mPositionBuffer);
        glBindBuffer(GL_ARRAY_BUFFER, mPositionBuffer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vec3)*position.size(), position.data(), GL_STATIC_DRAW); 

        glGenBuffers(1, &mIndicesBuffer);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndicesBuffer);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short)*indices.size(), indices.data(), GL_STATIC_DRAW); 

    }

    void draw(){
        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, mPositionBuffer);
        glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndicesBuffer);
        glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_SHORT, 0);
        glDisableVertexAttribArray(0);

    }
    ~Quad(){

    }
private:

    std::vector<unsigned short> indices; 
    std::vector<vec3> position; 
    GLuint mVertexArray; 
    GLuint mPositionBuffer; 
    GLuint mIndicesBuffer; 
};

我正在使用,OpenGL,glm,glfw等。

1 个答案:

答案 0 :(得分:2)

发布的代码使用两种解决方案的一部分来绘制地形。每个人本身都可以正常工作,但这是中途。

单独的四边形

显示的大部分代码都将地形视为一组独立的四边形。在这种情况下,您有length - 1width - 1个四元实例,每个实例有4个顶点和6个索引。

这正是您的Quad类实现的。它在自己的一对VBO中存储4个顶点和6个索引,并设置具有属性状态的VAO。然后为地形中的每个方格实例化Quad

由于每个Quad的顶点都存储在自己的缓冲区中,这意味着索引引用了此缓冲区中的顶点。这意味着索引在0到3的范围内,这正是您发现的工作原理。

这种选择的缺点是对大型地形来说效率低下。你将拥有很多物体(每个四边形有2个VBO和1个VBO),需要一个单独的绘制调用来渲染每个四边形。您也没有共享顶点,在整个数据结构中拥有4个大多数顶点的副本。

您实际上可以删除此方法的索引,并使用glDrawArrays(GL_TRIANGLE_STRIP, 4)调用来绘制四边形。但其他缺点仍然存在。

共享顶点

更有效的方法是将整个地形存储在单个VBO中。您的代码在此处开始执行此操作:

for(int z=0; z<_length;z++){
    for(int x=0; x<_width;x++){
        vertices.push_back(vec3((float)x*250, 0.f, (float)z*250));
    }
}

但是你没有坚持下去。您需要做的是将这些vertices存储在整个地形的单个VBO中。

由于索引将根据顶点缓冲区中包含所有顶点的顶点缓冲区中的位置来引用顶点,因此索引计算开始变得更有意义:

int index = z*_width+x; 
unsigned short indices[]= {index,index + 1,index + 
    _width,index + 1,index +  _width,index +  _width + 1};

这将是整个VBO中四元组的指数。您可能希望更改此选项以便为整个地形构建索引数组。这看起来像这样:

for(int z = 0; z < _length - 1; ++z) {
    for(int x = 0; x < _width; ++x) {
        indices.push_back(z * width + x);
        indices.push_back((z + 1) * width + x);
    }
}

然后可以使用length - 1三角形条渲染。每个都有2 * _width个索引。公共顶点是共享的,这使整个事情更加有效。您可以使用稍微更高级的功能(如原始重启)将渲染缩减为单个绘制调用。

唯一的缺点是,对于每个四边形没有对象,它可能看起来不那么面向对象。但无论如何,这似乎有点人为。您有一个由顶点网格组成的地形。我认为使用包含整个网格的terrain对象没有任何问题。我和下一个人一样喜欢课堂和对象(有些人说太多了......)。