提示コードですが下記の.objファイルのv要素の読み込みで///
コメント部内部のパターン1、パターン2、ともにアクセスエラーが発生してしまいます。またコメント部のコードは普通に実行できます。なぜ二つはアクセスエラーになるのでしょうか?
参考サイト: https://programming.pc-note.net/c/file4.html
// ##################################### .objファイル読み込み ##################################### std::shared_ptr<std::vector<FrameWork::Vertex>> FrameWork::GetVertex(const char* path) { #define LINE_BUFFER ((int)(500)) //FrameWork::Object_File object; std::vector<FrameWork::Vertex> polygon; std::vector<int> vertexIndex; std::vector<int> uvIndex; std::vector<int> normalIndex; std::vector<glm::vec3> vertex; std::vector<glm::vec2> uv; std::vector<glm::vec3> normal; FILE* file; fopen_s(&file, path, "r"); bool b = false; if (file == NULL) { std::cerr << ".objファイルが開けません: " << path << std::endl; assert(0); } else { FrameWork::vertex temp; bool b = false; while (true) { char line[500] = {'\0'}; int res = fscanf_s(file, "%s", line,LINE_BUFFER); if (res == EOF) { break; } if (strcmp(line, "o") == 0) { char str[LINE_BUFFER] = {'\0'}; fscanf_s(file, "%s", str,LINE_BUFFER); printf("%s\n",str); if (b == true) { polygon.push_back(temp); temp.attribute.resize(0); } if (b == false) { b = true; } } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// else if (strcmp(line, "v") == 0) { //パターン1 char vert[3][LINE_BUFFER] = { { '\0' } ,{ '\0' } ,{ '\0' } }; fscanf_s(file, "%s %s %s ", vert[0], vert[1], vert[2]); //パターン2 glm::vec3 v = glm::vec3(0, 0, 0); fscanf_s(file, "%f %f %f", v.x, v.y, v..z); /* glm::vec3 v; char vert[LINE_BUFFER] ={'\0'}; fscanf_s(file, "%s", &vert, LINE_BUFFER); v.x = (float)atof(vert); fscanf_s(file, "%s", &vert, LINE_BUFFER); v.y = (float)atof(vert); fscanf_s(file, "%s", &vert, LINE_BUFFER); v.z = (float)atof(vert); vertex.push_back(v); printf("v %f %f %f \n", v.x, v.y, v.z); */ } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// else if (strcmp(line, "vt") == 0) { glm::vec2 u; char vert[LINE_BUFFER] = { '\0' }; fscanf_s(file, "%s", &vert, LINE_BUFFER); u.x = (float)atof(vert); fscanf_s(file, "%s", &vert, LINE_BUFFER); u.y = (float)atof(vert); printf("vt %f %f \n", u.x, u.y); uv.push_back(u); } else if (strcmp(line, "vn") == 0) { glm::vec3 norm; fscanf_s(file, "%f %f %fn", &norm.x, &norm.y, &norm.z); normal.push_back(norm); } else if (strcmp(line, "f") == 0) { unsigned int v[3], u[3], n[3]; int matches = fscanf_s(file, "%d/%d/%d %d/%d/%d %d/%d/%dn", &v[0], &u[0], &n[0], &v[1], &u[1], &n[1], &v[2], &u[2], &n[2]); vertexIndex.push_back(v[0]); vertexIndex.push_back(v[1]); vertexIndex.push_back(v[2]); uvIndex.push_back(u[0]); uvIndex.push_back(u[1]); uvIndex.push_back(u[2]); normalIndex.push_back(n[0]); normalIndex.push_back(n[1]); normalIndex.push_back(n[2]); } else if (strcmp(line, "usemtl") == 0) { char str[LINE_BUFFER]; char number[LINE_BUFFER]; fscanf_s(file, "%s", str, LINE_BUFFER); strncpy_s(number, str + strlen("Material."), LINE_BUFFER); int num = atoi(number); printf("newmtl Material.%d\n",num); temp.materialNumber = num; } else if (strcmp(line, "s") == 0) { char str[LINE_BUFFER]; char number[LINE_BUFFER]; fscanf_s(file, "%s", str, LINE_BUFFER); strncpy_s(number, str, LINE_BUFFER); int num = atoi(number); printf("s %d\n", num); temp.shading = num; //temp.materialNumber = num; } } for (unsigned int i = 0; i < vertexIndex.size(); i++) { unsigned int vi = vertexIndex[i]; unsigned int ui = uvIndex[i]; unsigned int ni = normalIndex[i]; glm::vec3 v = vertex[vi - 1]; glm::vec2 u = uv[ui - 1]; glm::vec3 n = normal[ni - 1]; FrameWork::VertexAttribute attrib; attrib.position[0] = v.x; attrib.position[1] = v.y; attrib.position[2] = v.z; attrib.uv[0] = u.x; attrib.uv[1] = u.y; attrib.normal[0] = n.x; attrib.normal[1] = n.y; attrib.normal[2] = n.z; temp.attribute.push_back(attrib); } } return std::make_shared<std::vector<FrameWork::Vertex>>(polygon); }
.objファイル
# Blender v2.83.4 OBJ File: 'cloud.blend' # www.blender.org mtllib cloud.mtl o 7_armguard_0_0_0 v 0.267984 1.136365 -0.005862 v 0.181013 1.093678 -0.007669 v 0.309287 1.067712 0.092497 v 0.214476 1.021204 0.097445 v 0.312156 1.068274 -0.103130 v 0.217549 1.021802 -0.112007 v 0.379954 1.091695 -0.003916 v 0.173889 0.971327 -0.008270 vt 0.000000 2.000000 vt 0.000000 2.000000 vt 0.000000 2.000000 vt 0.000000 2.000000 vt 0.000000 2.000000 vt 0.000000 2.000000 vt 0.000000 2.000000 vt 0.000000 2.000000 vn -0.1063 0.9943 0.0016 vn -0.7802 0.6254 -0.0110 vn -0.0802 0.8085 0.5830 vn -0.6428 0.5196 0.5629 vn -0.0596 0.8122 -0.5804 vn -0.6244 0.5238 -0.5795 vn 0.3704 0.9288 0.0095 vn -0.9982 0.0582 -0.0151 usemtl Material.025 s 1 f 1/1/1 2/2/2 3/3/3 f 3/3/3 2/2/2 4/4/4 f 2/2/2 1/1/1 5/5/5 f 6/6/6 2/2/2 5/5/5 f 5/5/5 1/1/1 7/7/7 f 1/1/1 3/3/3 7/7/7 f 2/2/2 8/8/8 4/4/4 f 8/8/8 2/2/2 6/6/6
回答2件
あなたの回答
tips
プレビュー