いつもありがとうございます!
#####error
vec.normal[0] = Normal[Face[i + 2]].x; vec.normal[1] = Normal[Face[i + 2]].y; vec.normal[2] = Normal[Face[i + 2]].z;
上記の部分でvector subscript out of range
とエラーが出ました。
調べてみたら
Vertex はsize=14ですが、
Normal はsize=0でした。
std::cout << key << "\n";
std::cout << vec3d.x << "\n" << vec3d.y << "\n" << vec3d.z << "\n";
の部分はコンソールに以下のように表示されるので、データは取れてると思うのですが、、
Normal.push_back(vec3d);のところが何かおかしいのかな?
回答お願いします!!
vn 0.0000 -1.0000 0.0000 0 -1 0 vn 0.0000 1.0000 0.0000 0 1 0 vn 1.0000 0.0000 0.0000 1 0 0 vn -0.0000 -0.0000 1.0000 -0 -0 1 vn -1.0000 -0.0000 -0.0000 -1 -0 -0 vn 0.0000 0.0000 -1.0000 0 0 -1
struct Vector3f { float x; float y; float z; }vec3d; vector <Vector3f> Vertex; vector <Vector3f> Normal; string svertex[3]; std::getline(ifs, key); if (!key.compare(0, 1, "v")) { std::cout << key << "\n"; key.erase(0, 2); std::stringstream ss; ss.str(key); ss >> svertex[0] >> svertex[1] >> svertex[2]; vec3d.x = atof(svertex[0].c_str()); vec3d.y = atof(svertex[1].c_str()); vec3d.z = atof(svertex[2].c_str()); Vertex.push_back(vec3d); std::cout << vec3d.x << "\n" << vec3d.y << "\n" << vec3d.z << "\n"; } if (!key.compare(0, 2, "vn")) { std::cout << key << "\n"; key.erase(0, 3); std::stringstream ss; ss.str(key); ss >> svertex[0] >> svertex[1] >> svertex[2]; vec3d.x = atof(svertex[0].c_str()); vec3d.y = atof(svertex[1].c_str()); vec3d.z = atof(svertex[2].c_str()); Normal.push_back(vec3d); std::cout << vec3d.x << "\n" << vec3d.y << "\n" << vec3d.z << "\n"; }
#include <stdio.h> #include <stdlib.h> #include <vector> #include<iostream> #include<Windows.h> #include <string.h> #include <string> #include <fstream> #include <sstream> using std::vector; using std::string; struct Vector3f { float x; float y; float z; }vec3d; struct Vertex6f { float position[3]; float normal[3]; }vec; int main() { const char *file = "data.txt"; string key; vector <int> Face; vector <Vector3f> Vertex; vector <Vector3f> Normal; string svertex[3]; std::ifstream ifs(file); if (!ifs) { std::cout << "ファイルが開けませんでした。" << std::endl; std::cin.get(); return 0; } ifs.seekg(0, std::ios::beg); while (!ifs.eof()) { std::getline(ifs, key); if (!key.compare(0, 1, "v")) { std::cout << key << "\n"; key.erase(0, 2); std::stringstream ss; ss.str(key); ss >> svertex[0] >> svertex[1] >> svertex[2]; vec3d.x = atof(svertex[0].c_str()); vec3d.y = atof(svertex[1].c_str()); vec3d.z = atof(svertex[2].c_str()); Vertex.push_back(vec3d); std::cout << vec3d.x << "\n" << vec3d.y << "\n" << vec3d.z << "\n"; } if (!key.compare(0, 2, "vn")) { std::cout << key << "\n"; key.erase(0, 3); std::stringstream ss; ss.str(key); ss >> svertex[0] >> svertex[1] >> svertex[2]; vec3d.x = atof(svertex[0].c_str()); vec3d.y = atof(svertex[1].c_str()); vec3d.z = atof(svertex[2].c_str()); Normal.push_back(vec3d); std::cout << vec3d.x << "\n" << vec3d.y << "\n" << vec3d.z << "\n"; } if (!key.compare(0,1,"f")) { std::cout << key << "\n"; key.erase(0, 2); while (!key.empty()) { int i = key.find_first_of("/ "); string head = key.substr(0, i); if (head == "") { Face.push_back(-1); } else { int num = atoi(head.c_str()); num--; Face.push_back(num); } if (i == -1) { key.erase(0, 1); } else { key.erase(0, i + 1); } } } } for (int i = 0;i < Face.size();i++) { std::cout << Face[i] << std::endl; } vector <Vertex6f> vertex; std::cout << Face.size() << std::endl; for (int i = 0;i < Face.size() - 2;i + 3) { vec.position[0] = Vertex[Face[i]].x; vec.position[1] = Vertex[Face[i]].y; vec.position[2] = Vertex[Face[i]].z; vec.normal[0] = Normal[Face[i + 2]].x; vec.normal[1] = Normal[Face[i + 2]].y; vec.normal[2] = Normal[Face[i + 2]].z; vertex.push_back(vec); } getchar(); }
# Blender v2.79 (sub 0) OBJ File: '' # www.blender.org mtllib tesetobj.mtl o Cube v 1.000000 -1.000000 -1.000000 v 1.000000 -1.000000 1.000000 v -1.000000 -1.000000 1.000000 v -1.000000 -1.000000 -1.000000 v 1.000000 1.000000 -0.999999 v 0.999999 1.000000 1.000001 v -1.000000 1.000000 1.000000 v -1.000000 1.000000 -1.000000 vn 0.0000 -1.0000 0.0000 vn 0.0000 1.0000 0.0000 vn 1.0000 0.0000 0.0000 vn -0.0000 -0.0000 1.0000 vn -1.0000 -0.0000 -0.0000 vn 0.0000 0.0000 -1.0000 usemtl Material s off f 1//1 2//1 3//1 f 5//2 8//2 7//2 f 1//3 5//3 6//3 f 2//4 6//4 7//4 f 3//5 7//5 8//5 f 5//6 1//6 4//6
###追記
if (i == -1) { key.erase(0, 1); } else { key.erase(0, i + 1); }
↓
if (i == -1) { int num = atoi(key.c_str()); int digit = 0; while (num != 0) { num /= 10; digit++; } key.erase(0, digit); } else { key.erase(0, i + 1); }
もちっとマシなタイトル付けよ。「配列について!!」てなんやねん。
回答2件
あなたの回答
tips
プレビュー