[英]Extracting vectors from Structure in header file
這是有問題的 header 文件:
namespace osc {
using namespace gdt;
struct TriangleMesh {
std::vector<vec3f> vertex;
std::vector<vec3f> normal;
std::vector<vec2f> texcoord;
std::vector<vec3i> index;
// material data:
vec3f diffuse;
};
struct Model {
~Model()
{ for (auto mesh : meshes) delete mesh; }
std::vector<TriangleMesh *> meshes;
//! bounding box of all vertices in the model
box3f bounds;
};
Model *loadOBJ(const std::string &objFile);
}
我已經成功地能夠使用此header文件來導入.obj
model在我的主要C++中使用loadOBJ()
8834004065888888888888888888888888888888年8月8888888888888888年8月888888888888888年8月888888888888888年其中,頂點位於 header 文件中定義的結構中。 如何從結構中提取這些頂點向量(並顯示它們)?
loadOBJ()
給你一個指向Model
object 的指針。
Model
有一個名為meshes
的std::vector
(動態數組)成員,它包含指向TriangleMesh
對象的指針。
TriangleMesh
有一個名為vertex
的std::vector
成員, vec3f
對象。
您可以像這樣迭代各種vector
:
osc::Model *model = osc::loadOBJ(...);
if (!model) ... // error handling
for (auto&& mesh : model->meshes)
{
cout << "vertex:\n";
for (auto&& vtx : mesh->vertex)
{
cout << "x=" << vtx.x << ", y=" << vtx.y << "z=" << vtx.z << "\n";
}
cout << "\n";
// repeat for mesh->normal, mesh->texcoord, mesh->index, etc...
}
delete model;
std::vector
是 c++ 中的一個動態數組
如果你想枚舉
std::vector<vec3f> vertex;
你做
for(auto vert: vertex)
{
// here vert is of type vec3F
}
你也可以像數組一樣訪問它
vec3f v2 = vertex[2];
當然你必須確保頂點至少有 2 個元素。 您可以使用vertex.size()
找到長度
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.