Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenGL索引数组_C++_Opengl_Grid_Terrain - Fatal编程技术网

C++ OpenGL索引数组

C++ OpenGL索引数组,c++,opengl,grid,terrain,C++,Opengl,Grid,Terrain,我有一个类terrain,它创建了一个由Quads组成的网格。我是这样做的 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

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

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++;
    }
}
它非常有效:

问题是我不明白为什么这条线

        unsigned short indices[]= {index,index + 1,index + 
            _width,index + 1,index +  _width,index +  _width + 1};
不起作用。如果它起作用,我的网格将消耗更少的资源。如果有人能解释为什么它不起作用,那就太好了,谢谢。 如果您需要知道我如何绘制四边形,以下是代码:

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; 
};
类四元组{
公众:
四元组(顶点*\u顶点、整数、无符号短*\u索引、无符号短\u numIndices){
对于(int i=0;i<\u numIndices;i++){
指数。向后推(指数[i]);
} 

对于(inti=0;i,发布的代码使用两种解决方案的一部分来绘制地形。每种解决方案本身都可以很好地工作,但这是介于两者之间的一半

分开的四边形 显示的大多数代码将地形视为一组独立的四边形。在本例中,您有
length-1
times
width-1
四边形实例,每个实例有4个顶点和6个索引

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

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

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

实际上,您可以删除这种方法的索引,并使用
glDrawArrays(GL\u TRIANGLE\u 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));
    }
}
这将是整个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);
    }
}
for(int z=0;z<_length-1;++z){
用于(整数x=0;x<_宽度;++x){
指数。推回(z*宽度+x);
指数。后推((z+1)*宽度+x);
}
}
然后可以使用
length-1
三角形条带进行渲染。每个条带都有
2*\u width
索引。共享公共顶点,这使得整个过程更加高效。您可以通过使用稍微高级的功能(如primitive restart)将渲染简化为单个绘图调用


唯一的缺点是,没有每个四边形的对象似乎不太面向对象。但这似乎有点人为。你有一个由顶点网格组成的地形。我认为让一个地形对象包含整个网格没有什么错。我和下一个家伙一样喜欢类和对象(有些人说得太多了…。

非常感谢你的解释。我希望我能对你的答案投更多的票。我想把所有的顶点和索引存储在一个VBO中,但有了你的解释,我确信这是我应该做的。再次感谢你,这真的很有帮助!
int index = z*_width+x; 
unsigned short indices[]= {index,index + 1,index + 
    _width,index + 1,index +  _width,index +  _width + 1};
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);
    }
}