obj ファイルに入っている情報は、各ポリゴンの各頂点ごとの「座標 (x, y, z)」「法線 (x, y, z)」「色 (r, g, b)」「テクスチャ座標 (x, y)」です。
基本的にこのポリゴンは三角形であり、複雑なオブジェクトも三角形が集まってできています。
なので、まずは以下のコードを作成してみて、それを objloader で取得できた三角形分繰り返すようにあとで変更すればいいかと思います。
- 三角形のポリゴンを描画する OpenGL のコード
- 三角形のポリゴンにテクスチャを適用して描画する OpenGL のコード
- 透視投影変換やライトの設定
tinyobjloader で各ポリゴンの情報を取得する例
1つの頂点は複数の面で共有されているため、各頂点の情報にはインデックス経由でアクセスします。
(同じ頂点でも面ごとに情報を持つようにすると、重複して無駄なため、obj ファイルフォーマットではそうなっている。)
cpp
1tinyobj::attrib_t attrib;
2std::vector<tinyobj::shape_t> shapes;
3std::vector<tinyobj::material_t> materials;
4std::string error;
5
6bool ret = tinyobj::LoadObj(
7 &attrib,
8 &shapes,
9 &materials,
10 &error,
11 R"(test.obj)");
12
13if (!error.empty())
14 std::cerr << error << std::endl;
15
16if (!ret)
17 exit(1);
18
19for (const auto &shape : shapes) {
20 // shape の名前
21 std::cout << shape.name << std::endl;
22
23 size_t index_offset = 0; // インデントのオフセット
24 for (size_t f = 0; f < shape.mesh.num_face_vertices.size(); f++) {
25 // 面 f を構成sする頂点の数
26 int num_vertices = shape.mesh.num_face_vertices[f];
27
28 glBegin(GL_TRIANGLES);
29 for (size_t v = 0; v < num_vertices; v++) {
30 // access to vertex
31 tinyobj::index_t idx = shape.mesh.indices[index_offset + v];
32
33 // 頂点の座標
34 float vx = attrib.vertices[3 * idx.vertex_index + 0];
35 float vy = attrib.vertices[3 * idx.vertex_index + 1];
36 float vz = attrib.vertices[3 * idx.vertex_index + 2];
37
38 // 頂点の法線
39 float nx = attrib.normals[3 * idx.normal_index + 0];
40 float ny = attrib.normals[3 * idx.normal_index + 1];
41 float nz = attrib.normals[3 * idx.normal_index + 2];
42
43 // 頂点のテクスチャ座標
44 tinyobj::real_t tx =
45 attrib.texcoords[2 * idx.texcoord_index + 0];
46 tinyobj::real_t ty =
47 attrib.texcoords[2 * idx.texcoord_index + 1];
48
49 // 頂点の色
50 float red = attrib.colors[3 * idx.vertex_index + 0];
51 float green = attrib.colors[3 * idx.vertex_index + 1];
52 float blue = attrib.colors[3 * idx.vertex_index + 2];
53 }
54 index_offset += num_vertices;
55
56 // per-face material
57 shape.mesh.material_ids[f];
58 }
59}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。