OpenGL: Using VBO with std::vector

我只是一个虾纸丫 提交于 2019-11-30 16:21:21

You want to do:

unsigned int vbo;
vector<float> vert;

...
vert.push_back(i*size);
vert.push_back(height*h);
vert.push_back(j*size);
...

glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vert.size() * sizeof(float), vert.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

It is always good to read the documentation. Also, I would suggest you pick up a good C++ book, which probably would be a good way for you to avoid doing mistakes like these.

Remember to check if you have C++11 or vert.data won't compile.

If you look at the std::vector doc you'll that std::vector::data() is tagged with C++11.

http://www.cplusplus.com/reference/vector/vector/data/

If you don't have C++11 you can do the following:

unsigned int vbo;
vector<float> vert;

...
vert.push_back(i*size);
vert.push_back(height*h);
vert.push_back(j*size);
...

glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vert.size(), &vert[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

Taking a reference to the first element of a vector gives you a pointer to the data itself.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!