@@ -21,46 +21,22 @@ void OBJ::load_obj(std::string path) {
2121 s >> vertex.x >> vertex.y >> vertex.z ;
2222 vertices.push_back (vertex);
2323
24- } else if (line.substr (0 , 3 ) == " vt " ) {
25- std::istringstream s (line.substr (3 ));
26- glm::vec2 uv;
27- s >> uv.x >> uv.y ;
28- uvs.push_back (uv);
29-
30- } else if (line.substr (0 , 3 ) == " vn " ) {
31- std::istringstream s (line.substr (3 ));
32- glm::vec3 normal;
33- s >> normal.x >> normal.y >> normal.z ;
34- normals.push_back (glm::normalize (normal));
35-
3624 } else if (line.substr (0 , 2 ) == " f " ) {
3725 std::istringstream s (line.substr (2 ));
3826 std::string splitted;
39- std::vector<glm::uvec3 > indices;
27+ std::vector<unsigned int > indices;
4028 while (std::getline (s, splitted, ' ' )) {
41- glm::uvec3 index (1 );
42- std::istringstream ss (splitted);
43- std::string slashsplitted;
44- size_t indexsubs = 0 ;
45- while (std::getline (ss, slashsplitted, ' /' ) && indexsubs < 3 ) {
46- std::istringstream (slashsplitted) >> index[indexsubs++];
47- }
48- indices.push_back (index - 1u );
29+ unsigned int index = 1 ;
30+ std::istringstream (splitted) >> index;
31+ indices.push_back (index - 1 );
4932 }
5033 for (size_t i = 2 ; i < indices.size (); i++) {
51- glm::umat3x3 face = glm::umat3x3 (indices[0 ], indices[i - 1 ], indices[i]);
34+ glm::uvec3 face = glm::uvec3 (indices[0 ], indices[i - 1 ], indices[i]);
5235 faces.push_back (face);
5336 }
5437 }
5538 }
5639
57- if (!normals.size ()) {
58- normals.push_back (glm::vec3 (0 , 0 , 1 ));
59- }
60- if (!uvs.size ()) {
61- uvs.push_back (glm::vec3 (0 ));
62- }
63-
6440 file.close ();
6541 std::cout << " Loaded " << vertices.size () << " vertices, " << faces.size () << " faces.\n " ;
6642}
@@ -69,31 +45,28 @@ static glm::vec3 perspective_divide(glm::vec4 pos) {
6945 return glm::vec3 (pos.x / pos.w , pos.y / pos.w , pos.z / pos.w );
7046}
7147
48+ static glm::vec3 compute_normal (glm::vec3 a, glm::vec3 b, glm::vec3 c) {
49+ // jisuan sanjiaoxin faxian
50+ glm::vec3 ab = b - a;
51+ glm::vec3 ac = c - a;
52+ return glm::normalize (glm::cross (ab, ac));
53+ }
54+
7255void OBJ::draw_obj () {
7356 glBegin (GL_TRIANGLES);
7457 glColor3f (0 .9f , 0 .6f , 0 .1f );
7558
7659 for (auto face : faces) {
77- auto const &a_pos = vertices.at (face[0 ][0 ]);
78- auto const &b_pos = vertices.at (face[1 ][0 ]);
79- auto const &c_pos = vertices.at (face[2 ][0 ]);
80- auto const &a_uv = uvs.at (face[0 ][1 ]);
81- auto const &b_uv = uvs.at (face[1 ][1 ]);
82- auto const &c_uv = uvs.at (face[2 ][1 ]);
83- auto const &a_norm = normals.at (face[0 ][2 ]);
84- auto const &b_norm = normals.at (face[1 ][2 ]);
85- auto const &c_norm = normals.at (face[2 ][2 ]);
86-
87- glNormal3fv (glm::value_ptr (a_norm));
88-
89- glVertex3fv (glm::value_ptr (a_pos));
90- glTexCoord2fv (glm::value_ptr (b_uv));
60+ auto const &a = vertices.at (face[0 ]);
61+ auto const &b = vertices.at (face[1 ]);
62+ auto const &c = vertices.at (face[2 ]);
9163
92- glVertex3fv ( glm::value_ptr (b_pos) );
93- glTexCoord2fv (glm::value_ptr (b_uv ));
64+ glm::vec3 norm = compute_normal (a, b, c );
65+ glNormal3fv (glm::value_ptr (norm ));
9466
95- glVertex3fv (glm::value_ptr (c_pos));
96- glTexCoord2fv (glm::value_ptr (c_uv));
67+ glVertex3fv (glm::value_ptr (a));
68+ glVertex3fv (glm::value_ptr (b));
69+ glVertex3fv (glm::value_ptr (c));
9770 }
9871
9972 CHECK_GL (glEnd ());
0 commit comments