我在将std::向量给另一个类时遇到了问题。我将数据放入std::vector中,并将其放入一个名为"Mesh“的类中。“网格”就变成了“模型”。
// Store the vertices
std::vector<float> positionVertices;
positionVertices.push_back(-0.5f);
positionVertices.push_back(-0.5f);
positionVertices.push_back( 0.5f);
positionVertices.push_back(-0.5f);
positionVertices.push_back(-0.5f);
positionVertices.push_back( 0.5f);
// Put them into a mesh and the mesh into a model
Mesh mesh = Mesh(positionVertices);
Model model = Model(mesh);在模型类中,我获取网格的位置顶点,并将其转换为float[]。但是看起来,我分配std::向量的方式是错误的,因为当检查模型类中的std::向量时,它的大小为0。
// Store the vertices
float* dataPtr = &data[0];
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), dataPtr, GL_STATIC_DRAW);如何将数据正确地导入到其他类中?
我也不确定mesh类的构造函数是如何工作的。救世主:
// Mesh.h
class Mesh
{
public:
std::vector<float> positionVertices;
Mesh(std::vector<float>);
~Mesh();
};Mesh.cpp:
// Mesh.cpp
Mesh::Mesh(std::vector<float> positionVertices) : positionVertices(Mesh::positionVertices)
{
}模型h:
// Model.h
class Model
{
public:
Mesh mesh;
unsigned int vertexArray;
unsigned int vertexCount;
Model(Mesh);
~Model();
void storeData(std::vector<float> data, const unsigned int index, const unsigned int size);
};Model.cpp:
// Model.cpp
Model::Model(Mesh mesh) : mesh(Model::mesh)
{ ... }发布于 2019-03-21 23:05:09
// Mesh.cpp
Mesh::Mesh(std::vector<float> positionVertices) :
positionVertices(Mesh::positionVertices) // Here's the problem
{
}初始化程序列表中的positionVertices是Mesh::positionVertices,所以您要将它赋值给自己。
使用
positionVertices(positionVertices)另外,改变
Mesh::Mesh(std::vector<float> positionVertices) :至
Mesh::Mesh(const std::vector<float>& positionVertices) :所以你不会做不必要的矢量拷贝。
https://stackoverflow.com/questions/55290328
复制相似问题