提示コードは.objファイルをロードしてそれぞれの頂点データを配列に入れるて描画するローダーなのですがglDrawElements()関数を使った描画に対応させたいのですがその場合どういった変更を行えばいいのでしょうか? 頂点座標{1,1,2 }とあったら{1,2}として配列に入れてほしいです。
頂点インデックスとは参考サイト: http://marupeke296.com/DXG_No30_VertexIndexBuffer.html
現状
.objファイルの頂点情報をベクター配列に入れる関数
やりたい事
glDrawElements()
関数に対応させるためfの要素をどのように格納すればいいのか知りたい。
obj
1# Blender v2.83.4 OBJ File: 'cloud.blend' 2# www.blender.org 3mtllib cloud.mtl 4o 7_armguard_0_0_0 5v 0.267984 1.136365 -0.005862 6v 0.181013 1.093678 -0.007669 7v 0.309287 1.067712 0.092497 8v 0.214476 1.021204 0.097445 9v 0.312156 1.068274 -0.103130 10v 0.217549 1.021802 -0.112007 11v 0.379954 1.091695 -0.003916 12v 0.173889 0.971327 -0.008270 13vt 0.000000 2.000000 14vt 0.000000 2.000000 15vt 0.000000 2.000000 16vt 0.000000 2.000000 17vt 0.000000 2.000000 18vt 0.000000 2.000000 19vt 0.000000 2.000000 20vt 0.000000 2.000000 21vn -0.1063 0.9943 0.0016 22vn -0.7802 0.6254 -0.0110 23vn -0.0802 0.8085 0.5830 24vn -0.6428 0.5196 0.5629 25vn -0.0596 0.8122 -0.5804 26vn -0.6244 0.5238 -0.5795 27vn 0.3704 0.9288 0.0095 28vn -0.9982 0.0582 -0.0151 29usemtl Material.025 30s 1 31f 1/1/1 2/2/2 3/3/3 32f 3/3/3 2/2/2 4/4/4 33f 2/2/2 1/1/1 5/5/5 34f 6/6/6 2/2/2 5/5/5 35f 5/5/5 1/1/1 7/7/7 36f 1/1/1 3/3/3 7/7/7 37f 2/2/2 8/8/8 4/4/4 38f 8/8/8 2/2/2 6/6/6 39
cpp
1 2// ##################################### .objファイル読み込み ##################################### 3std::shared_ptr<std::vector<FrameWork::Vertex>> GetVertex(const char* path) 4{ 5 6 std::vector<FrameWork::Vertex> polygon; 7 8 std::vector<int> vertexIndex; 9 std::vector<int> uvIndex; 10 std::vector<int> normalIndex; 11 12 std::vector<glm::vec3> vertex; 13 std::vector<glm::vec2> uv; 14 std::vector<glm::vec3> normal; 15 16 FILE* file; 17 fopen_s(&file, path, "r"); 18 19 bool b = false; 20 if (file == NULL) 21 { 22 std::cerr << ".objファイルが開けません: " << path << std::endl; 23 assert(0); 24 } 25 else 26 { 27 FrameWork::vertex temp; 28 bool first = false; 29 while (true) 30 { 31 32 char line[500] = {'\0'}; 33 34 int res = fscanf_s(file, "%s", line,(unsigned int)LINE_BUFFER); 35 36 if (res == EOF) 37 { 38 break; 39 } 40 41 if (strcmp(line, "o") == 0) 42 { 43 char str[LINE_BUFFER] = {'\0'}; 44 fscanf_s(file, "%s", str, (unsigned int)LINE_BUFFER); 45// printf("%s\n",str); 46 47 if (first == true) 48 { 49 polygon.push_back(temp); 50 temp.attribute.resize(0); 51 } 52 53 if (first == false) 54 { 55 first = true; 56 } 57 } 58 else if (strcmp(line, "v") == 0) 59 { 60 glm::vec3 v; 61 fscanf_s(file, "%f %f %f", &v.x,&v.y,&v.z); 62 63 vertex.push_back(v); 64// printf("v %f %f %f \n", v.x, v.y, v.z); 65 } 66 else if (strcmp(line, "vt") == 0) 67 { 68 glm::vec2 u; 69 fscanf_s(file, "%f %f", &u.x, &u.y); 70 71 uv.push_back(u); 72// printf("vt %f %f \n", u.x, u.y); 73 } 74 else if (strcmp(line, "vn") == 0) 75 { 76 glm::vec3 n; 77 fscanf_s(file, "%f %f %fn", &n.x, &n.y, &n.z); 78 79 normal.push_back(n); 80// printf("vn %f %f %f \n",n.x,n.y,n.z); 81 } 82 else if (strcmp(line, "f") == 0) 83 { 84 unsigned int v[3], u[3], n[3]; 85 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]); 86 87 vertexIndex.push_back(v[0]); 88 vertexIndex.push_back(v[1]); 89 vertexIndex.push_back(v[2]); 90 91 uvIndex.push_back(u[0]); 92 uvIndex.push_back(u[1]); 93 uvIndex.push_back(u[2]); 94 95 normalIndex.push_back(n[0]); 96 normalIndex.push_back(n[1]); 97 normalIndex.push_back(n[2]); 98 } 99 else if (strcmp(line, "usemtl") == 0) 100 { 101 char str[LINE_BUFFER] = { '\0' }; 102 char string[LINE_BUFFER] = { '\0' }; 103 fscanf_s(file, "%s", str, (unsigned int)LINE_BUFFER); 104 strncpy_s(string, str + strlen("Material."), LINE_BUFFER); 105 int num = atoi(string); 106 107 temp.materialNumber = num; 108// printf("newmtl Material.%d\n", num); 109 } 110 else if (strcmp(line, "s") == 0) 111 { 112 char str[LINE_BUFFER]; 113 char number[LINE_BUFFER]; 114 fscanf_s(file, "%s", str, (unsigned int)LINE_BUFFER); 115 strncpy_s(number, str, LINE_BUFFER); 116 int num = atoi(number); 117 118 temp.shading = num; 119// printf("s %d\n", num); 120 } 121 } 122/////////////////////////////////////////////////////////////////////////////////////////////////////// 123 for (unsigned int i = 0; i < vertexIndex.size(); i++) 124 { 125 unsigned int vi = vertexIndex[i]; 126 unsigned int ui = uvIndex[i]; 127 unsigned int ni = normalIndex[i]; 128 129 glm::vec3 v = vertex[vi - 1]; 130 glm::vec2 u = uv[ui - 1]; 131 glm::vec3 n = normal[ni - 1]; 132 133 FrameWork::VertexAttribute attrib; 134 135 attrib.position[0] = v.x; 136 attrib.position[1] = v.y; 137 attrib.position[2] = v.z; 138 139 attrib.uv[0] = u.x; 140 attrib.uv[1] = u.y; 141 142 attrib.normal[0] = n.x; 143 attrib.normal[1] = n.y; 144 attrib.normal[2] = n.z; 145 146 temp.attribute.push_back(attrib); 147 } 148////////////////////////////////////////////////////////////////////////////////////////////////// 149 } 150 151 152 153 return std::make_shared<std::vector<FrameWork::Vertex>>(polygon); 154}
あなたの回答
tips
プレビュー