具有std :: vector的VBO [英] VBOs with std::vector

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

问题描述

我已经用C ++和OpenGL编写了一个模型加载器.我已经使用std::vector来存储我的顶点数据,但是现在我想将其传递给glBufferData(),但是数据类型却大不相同.我想知道是否有一种方法可以在std::vector到记录的const GLvoid *之间为glBufferData()进行转换.

I've written a model loader in C++ an OpenGL. I've used std::vectors to store my vertex data, but now I want to pass it to glBufferData(), however the data types are wildly different. I want to know if there's a way to convert between std::vector to the documented const GLvoid * for glBufferData().

typedef struct
{
    float x, y, z;
    float nx, ny, nz;
    float u, v;
}
Vertex;

vector<Vertex> vertices;

glBufferData()调用

glBufferData(GL_ARRAY_BUFFER, vertices.size() * 3 * sizeof(float), vertices, GL_STATIC_DRAW);

我收到以下(预期)错误:

I get the following (expected) error:

error: cannot convert ‘std::vector<Vertex>’ to ‘const GLvoid*’ in argument passing

如何将向量转换为与glBufferData()兼容的类型?

How can I convert the vector to a type compatible with glBufferData()?

NB.我现在不在乎正确的内存分配. vertices.size() * 3 * sizeof(float)最有可能出现段错误,但我想首先解决类型错误.

NB. I don't care about correct memory allocation at the moment; vertices.size() * 3 * sizeof(float) will most likely segfault, but I want to solve the type error first.

推荐答案

如果您有std::vector<T> v,则可以使用T*指向连续数据的起始位置(这是OpenGL之后的内容).表达式&v[0].

If you have a std::vector<T> v, you may obtain a T* pointing to the start of the contiguous data (which is what OpenGL is after) with the expression &v[0].

在您的情况下,这意味着将Vertex*传递给glBufferData:

In your case, this means passing a Vertex* to glBufferData:

glBufferData(
   GL_ARRAY_BUFFER,
   vertices.size() * sizeof(Vertex),
   &vertices[0],
   GL_STATIC_DRAW
);

还是这样,这是相同的:

Or like this, which is the same:

glBufferData(
   GL_ARRAY_BUFFER,
   vertices.size() * sizeof(Vertex),
   &vertices.front(),
   GL_STATIC_DRAW
);


您可以在此处依靠从Vertex*void const*的隐式转换;不会有什么问题.


You can rely on implicit conversion from Vertex* to void const* here; that should not pose a problem.

这篇关于具有std :: vector的VBO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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