将C ++容器提供给OpenGL?(Giving C++ containers to OpenGL?)

我创建了一个着色器存储缓冲区对象,以便为我的顶点着色器提供信息。 它绑定到包含单个结构的缓冲区。 但有一个问题。 我在这个结构中保存的数据不是一个简单的数组,它是一个std::vector 。

这是结构:

typedef struct { glm::mat4 modelMatrix; } entityInfo; typedef struct { std::vector<entityInfo> entityInfoBuffer; } shaderData;

mat4只是一个简单的4x4浮点矩阵。

如果我把它交给OpenGL,传输到GPU的数据将只是一堆关于矩阵/向量的指针和信息,而不是它们中的数据。

一种解决方案是仅为OpenGL创建一个结构:

typedef struct { float* modelMatrix; } SSBOentityInfo; typedef struct { SSBOentityInfo* entityInfoBuffer; } SSBOshaderData;

但我必须复制对应于容器数据的eeeeevery指针。

有没有更好的方法呢?

I created a shader storage buffer object to give information to my vertex shader. It is bound to a buffer containing a single structure. But there is a problem. The data I keep in this structure is not a simple array, it's a std::vector.

Here is the structure :

typedef struct { glm::mat4 modelMatrix; } entityInfo; typedef struct { std::vector<entityInfo> entityInfoBuffer; } shaderData;

mat4 is just a simple 4x4 float matrix.

If I give this to OpenGL, the data transmitted to the GPU will be just a bunch of pointers and info about the matrix/vector, and not the data in them.

One solution would be to create a structure only for OpenGL:

typedef struct { float* modelMatrix; } SSBOentityInfo; typedef struct { SSBOentityInfo* entityInfoBuffer; } SSBOshaderData;

But I would have to copy eeeeevery pointer corresponding to the data of the containers.

Is there a better way to do it?

最满意答案

对于所有意图和目的, glm::mat4是一个包含16个float的数组。 不是指向 16个浮点数的指针 ,一个实际的float[16]数组。 16个float ,一个接一个。

因此, vector<glm::mat4>管理连续的元素数组,其中每个元素是16个浮点数。 这是您要存储在缓冲区对象中的数据。 所以......那样做。

vector管理连续的数据数组,因此获取指向此数组的指针是完全合法的。 这就是vector.data()的用途。 所以这是你给glBufferSubData或其他什么的源指针。 大小将是数组中元素的数量( vector.size() )乘以sizeof计算的每个元素的sizeof 。

glm::mat4 is, for all intents and purposes, an array of 16 floats. Not a pointer to 16 floats, an actual float[16] array. 16 floats, one after the other.

Therefore, a vector<glm::mat4> manages a contiguous array of elements, where each element is 16 floats. It is that data that you want to store in your buffer object. So... do that.

vector manages a contiguous array of data, so it's perfectly legal to get a pointer to this array. That's what vector.data() is for. So that's the source pointer you give to glBufferSubData or whatever. The size would be the number of elements in the array (vector.size()) times the size of each element as computed by sizeof.

将C ++容器提供给OpenGL?(Giving C++ containers to OpenGL?)

我创建了一个着色器存储缓冲区对象,以便为我的顶点着色器提供信息。 它绑定到包含单个结构的缓冲区。 但有一个问题。 我在这个结构中保存的数据不是一个简单的数组,它是一个std::vector 。

这是结构:

typedef struct { glm::mat4 modelMatrix; } entityInfo; typedef struct { std::vector<entityInfo> entityInfoBuffer; } shaderData;

mat4只是一个简单的4x4浮点矩阵。

如果我把它交给OpenGL,传输到GPU的数据将只是一堆关于矩阵/向量的指针和信息,而不是它们中的数据。

一种解决方案是仅为OpenGL创建一个结构:

typedef struct { float* modelMatrix; } SSBOentityInfo; typedef struct { SSBOentityInfo* entityInfoBuffer; } SSBOshaderData;

但我必须复制对应于容器数据的eeeeevery指针。

有没有更好的方法呢?

I created a shader storage buffer object to give information to my vertex shader. It is bound to a buffer containing a single structure. But there is a problem. The data I keep in this structure is not a simple array, it's a std::vector.

Here is the structure :

typedef struct { glm::mat4 modelMatrix; } entityInfo; typedef struct { std::vector<entityInfo> entityInfoBuffer; } shaderData;

mat4 is just a simple 4x4 float matrix.

If I give this to OpenGL, the data transmitted to the GPU will be just a bunch of pointers and info about the matrix/vector, and not the data in them.

One solution would be to create a structure only for OpenGL:

typedef struct { float* modelMatrix; } SSBOentityInfo; typedef struct { SSBOentityInfo* entityInfoBuffer; } SSBOshaderData;

But I would have to copy eeeeevery pointer corresponding to the data of the containers.

Is there a better way to do it?

最满意答案

对于所有意图和目的, glm::mat4是一个包含16个float的数组。 不是指向 16个浮点数的指针 ,一个实际的float[16]数组。 16个float ,一个接一个。

因此, vector<glm::mat4>管理连续的元素数组,其中每个元素是16个浮点数。 这是您要存储在缓冲区对象中的数据。 所以......那样做。

vector管理连续的数据数组,因此获取指向此数组的指针是完全合法的。 这就是vector.data()的用途。 所以这是你给glBufferSubData或其他什么的源指针。 大小将是数组中元素的数量( vector.size() )乘以sizeof计算的每个元素的sizeof 。

glm::mat4 is, for all intents and purposes, an array of 16 floats. Not a pointer to 16 floats, an actual float[16] array. 16 floats, one after the other.

Therefore, a vector<glm::mat4> manages a contiguous array of elements, where each element is 16 floats. It is that data that you want to store in your buffer object. So... do that.

vector manages a contiguous array of data, so it's perfectly legal to get a pointer to this array. That's what vector.data() is for. So that's the source pointer you give to glBufferSubData or whatever. The size would be the number of elements in the array (vector.size()) times the size of each element as computed by sizeof.