【发布时间】:2020-06-24 21:28:30
【问题描述】:
我有以下 c++ 代码来加载目标文件,至少是顶点和顶点索引。
bool ObjMeshImporter::from_file(const std::string& filepath, nelems::Mesh* pMesh)
{
std::ifstream in(filepath, std::ios::in);
if (!in)
{
return false;
}
std::vector<glm::vec3> t_vert;
std::string line;
while (std::getline(in, line))
{
if (line.substr(0, 2) == "v ")
{
// read vertices
std::istringstream s(line.substr(2));
glm::vec3 v; s >> v.x; s >> v.y; s >> v.z;
// Add to temporary vertices before indexing
t_vert.push_back(v);
}
else if (line.substr(0, 2) == "f ")
{
// TODO: Store UVs and Normals
unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];
int count_found = sscanf_s(line.substr(2).c_str(),
"%d/%d/%d %d/%d/%d %d/%d/%d\n",
&vertexIndex[0], &uvIndex[0], &normalIndex[0],
&vertexIndex[1], &uvIndex[1], &normalIndex[1],
&vertexIndex[2], &uvIndex[2], &normalIndex[2]);
if (count_found != 9) {
return false;
}
pMesh->add_vertex_index(vertexIndex[0]-1);
pMesh->add_vertex_index(vertexIndex[1]-1);
pMesh->add_vertex_index(vertexIndex[2]-1);
}
}
// Now use the indices to create the concrete vertices for the mesh
for (auto v_idx : pMesh->GetVertexIndices())
{
glm::vec3 vertex = t_vert[v_idx];
pMesh->add_vertex(vertex);
}
return true;
}
它对于像这样的对象(Blender icosphere)非常有效:
这个模型是由三角形组成的。
但是当我加载一个有四边形的时,它不起作用:
当然,因为这是我的面部渲染方法:
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)mVertexIndices.size());
所以我知道,我必须扩展解析器,并为“f”中的每一行检查我有多少索引,最好存储一个 VFace 类,它知道一个面由多少个顶点组成 - 好的.
所以我会将带有 tris 的面渲染为 GL_TRIANGLE,将带有四边形的面渲染为 GL_QUADS...但是 NGONS 呢?以及如何渲染线条?
编辑:我看到有 GL_POLYGON。但是我必须为这些创建单独的顶点缓冲区吗?
【问题讨论】:
-
不,我想用三角形、四边形或 ngons 渲染面。
-
不熟悉 c++,但我注意到您调用 (GLsizei)mVertexIndices.size() 但使用 glDrawArrays。那么它不应该是一个 glDrawElements 调用吗?或者 GLsize 应该指向顶点索引的大小而不是索引。
-
@Kempie :我不这么认为,因为那样我需要一个 GL_ELEMENT_ARRAY 缓冲区
标签: c++ opengl glm-math wavefront