I try to access/print data from a vector3f which results in EXC_BAD_ACCESS:
std::cout << myMesh.faces[1].vertices[1].pos.x;
Code:
struct Vector2f{
float x, y;
};
struct Vector3f{
float x, y, z;
};
struct ObjMeshVertex{
Vector3f pos;
Vector2f texcoord;
Vector3f normal;
};
struct ObjMeshFace{
ObjMeshVertex vertices[3];
ObjMeshFace(){}
ObjMeshFace(const ObjMeshFace& o)
{for (int i=0; i < 3; ++i) vertices[i] = o.vertices[i]; }
};
struct ObjMesh{
std::vector<ObjMeshFace> faces;
};
ObjMesh myMesh;
for(size_t i = 0; i < faces.size(); ++i){
ObjMeshFace face;
for(size_t j = 0; j < 3; ++j){
face.vertices[j].pos = positions[(faces[i].pos_index[j] - 1)];
face.vertices[j].texcoord = texcoords[faces[i].tex_index[j] - 1];
face.vertices[j].normal = normals[faces[i].nor_index[j] - 1];
}
myMesh.faces.push_back(face);
}
The debugger refers to stl_vector.h:
reference
operator[](size_type __n)
{ return *(this->_M_impl._M_start + __n); }
What does this mean? Am i calling out of range?
It’s been a while, but when you do a push_back you are only shallow copying the vertices array
vertices[] = vertices[]. You need a custom copy-ctor in ObjMeshFace. That’s whyvertices[0].pos.xworks andvertices[1].pos.xdoesn’t.