OpenGL的 - 增加顶点 [英] OpenGL - add vertices

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

问题描述

我有一个子功能读取的数据流,并根据在该顶点数据的数组。主函数反复调用这一子功能,并更新顶点数据阵列,其然后结合至缓冲器和绘制。到现在为止还挺好。但是,我想不出如何添加顶点。 C ++不会让您重新分配或调整整个阵列。因为OpenGL函数采取的阵列,而不是媒介我不能用向量。

I have a subfunction that reads a data stream and creates an array of vertex data based on that. The main function calls this subfunction repeatedly and updates the vertex data array, which is then bound to a buffer and drawn. So far, so good. However, I cannot figure out how to add vertices. C++ does not let you reassign or resize entire arrays. I can't use vectors because the OpenGL functions take in arrays, not vectors.

推荐答案

您可以使用向量来填充OpenGL的顶点缓冲。在矢量值保证是连续的。例如,见对相关语言标准的详细信息这些讨论:

You can use vectors to populate an OpenGL vertex buffer. The values in a vector are guaranteed to be contiguous. See for example these discussions for details on the related language standards:

  • Are std::vector elements guaranteed to be contiguous?
  • Is it safe to assume that STL vector storage is always contiguous?

这意味着code像下面是安全的:

This means that code like the following is safe:

std::vector<GLfloat> vertexPositions;
// Populate vector with vertex positions.
GLuint bufId = 0;
glGenBuffers(1, &bufId);
glBindBuffer(GL_ARRAY_BUFFER, bufId);
glBufferData(GL_ARRAY_BUFFER, vertexPositions.size() * sizeof(GLfloat),
             &vertexPositions[0], GL_STATIC_DRAW);

微妙而关键的部分是你在第一个元素的地址传递的载体,而不是矢量对象的地址。

The subtle but critical part is that you pass in the address of the first element of the vector, not the address of the vector object.

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

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