使用 glDrawElements 绘制一个 std::vector
Drawing a std::vector with glDrawElements
我正在尝试用我拥有的高度图绘制地形。通过使用 std::vector,我现在有一个位置数组和一个位置索引数组,以便在绘制地形时使用glDrawElements(GL_TRIANGLE_STRIP,...)
。但是,由于某种原因,我的简单代码不起作用。请注意,我正在使用glm作为数学库。
以下是 VAO、VBO 和 EBO 的配置。我仔细检查了顶点数据和索引数据,它们都是正确的。
// positions and indices
vector<glm::vec3> positions;
vector<int> indices;
for (int z = 0; z < gridZNum; z++) {
for (int x = 0; x < gridXNum; x++) {
positions.push_back(glm::vec3(x, _terrain->getHeight(x,z),z));
//normals.push_back(_terrain->getNormal(x, z));
indices.push_back(z*gridXNum + (x+1));
indices.push_back(z*gridXNum + (x+1) + gridXNum);
if (x == gridXNum-1 && z != gridZNum-1) {
indices.push_back(z*gridXNum + 2*gridXNum);
indices.push_back((z+1)*gridXNum+1);
}
}
}
// VAO, VBO configuration
unsigned int VAO, VBO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindBuffer(GL_ARRAY_BUFFER, VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ARRAY_BUFFER, positions.size() * sizeof(glm::vec3), &positions[0] , GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(int), &indices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3* sizeof(float), 0);
最后,这是我的绘图函数
//Inside the render loop I call
drawScene(shader, VAO, indices.size());
//......
void drawScene(Shader &shader, unsigned int VAO, unsigned int numIndices) //draw the scene with the parameter data.
{
glm::mat4 model;
//model = glm::translate(model, glm::vec3(-(_terrain->width())/2, 0.0f, -(_terrain->length())/2));
glm::mat4 view = camera.GetViewMatrix();
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
shader.use();
shader.setMat4("model", model);
shader.setMat4("view", view);
shader.setMat4("projection", projection);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLE_STRIP, numIndices, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
我假设大小参数等参数有问题,但目前我找不到任何问题。
在定义通用顶点属性数据数组之前,必须绑定顶点数组对象。请参阅顶点数组对象:
glBindVertexArray(VAO);
顶点缓冲区对象的名称是VBO
而不是VAO
:
glBindBuffer(GL_ARRAY_BUFFER, VBO);
以某种方式更改您的代码:
unsigned int VAO, VBO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, positions.size() * sizeof(glm::vec3), &positions[0] , GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(int), &indices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3* sizeof(float), 0);
相关文章:
- 库函数需要一个 std::function<void(void)>,如何传入类函数?
- 将 std::array 移动到另一个 std::array
- 为什么 Clang std::ostream 写一个 std::istream 无法读取的双精度?
- 访问 std:vector 的类成员 std:vector 在一个类中与另一个 std:vector
- 是否有一个 std::set 函数来确定不超过数字 x 的最大元素?
- 除了 std::vector 之外,是否有一个 std 容器不会复制和销毁作为类的元素?
- 我正在将一个 std::string 传递给一个 boost 函数,该函数对该类型进行常量引用,但该值发生了变化
- 在线程 A 中创建一个 std::thread 对象,在线程 B 中连接
- 使用 glDrawElements 绘制一个 std::vector
- 从 C 字符串构造 std::string 与从另一个 std::string 构造 std::string 不一致
- 我可以得到一个字符 * 到一个 std::sregex_iterator 匹配 str() 吗?
- 如何有效地将(一些)项目从一个std::map移动到另一个std::map
- 我可以制作一个std::set的constexpr对象吗
- 打印一个带有静态 int 的函数,有一个 std::cout 和多个 std::cout 有什么区别?
- Visual Studio 2017 STL 可视化工具失败了一个 std::map<MyIntrusivePtr, std::tuple<....> >
- 如何设置一个 std::vector 与另一个,其中两个是不同类的向量?
- SWIG:传递一个 std::vector< std::vector <double> >指向 python 的指针
- 如何构造一个 std::variant 类型对象,其自身 Templated 和构造函数转发参数
- 如何声明一个 std::用不同值内联初始化的结构数组
- 将对象从一个 std::d eque 移动到另一个的更好方法