Skip to content

Commit 71b517c

Browse files
committed
backtouvec3face
1 parent 27134cd commit 71b517c

File tree

4 files changed

+25
-54
lines changed

4 files changed

+25
-54
lines changed

include/OBJ.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55

66
struct OBJ {
77
std::vector<glm::vec3> vertices;
8-
std::vector<glm::vec2> uvs;
9-
std::vector<glm::vec3> normals;
10-
std::vector<glm::umat3x3> faces;
8+
std::vector<glm::uvec3> faces;
119

1210
void load_obj(std::string path);
1311
void draw_obj();

src/Game.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ void Game::set_window(GLFWwindow *window) {
2727
}
2828

2929
void Game::initialize() {
30-
/* m_private->monkey.load_obj("/home/bate/Codes/opengltutor/assets/monkey.obj"); */
31-
m_private->monkey.load_obj("/home/bate/Codes/opengltutor/assets/cube.obj");
30+
m_private->monkey.load_obj("/home/bate/Codes/opengltutor/assets/monkey.obj");
31+
/* m_private->monkey.load_obj("/home/bate/Codes/opengltutor/assets/cube.obj"); */
3232
CHECK_GL(glEnable(GL_DEPTH_TEST));
33-
CHECK_GL(glEnable(GL_MULTISAMPLE));
33+
CHECK_GL(glDisable(GL_MULTISAMPLE));
3434
CHECK_GL(glEnable(GL_BLEND));
3535
CHECK_GL(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
3636
CHECK_GL(glEnable(GL_LIGHTING));

src/OBJ.cpp

Lines changed: 20 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
7255
void 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());

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ int main() {
1111
std::cerr << "failed to initialize GLFW: " << errmsg << '\n';
1212
return -1;
1313
}
14-
14+
1515
// Hint the version required: OpenGL 2.0
1616
constexpr int version = 20;
1717
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API);

0 commit comments

Comments
 (0)