Skip to content

Commit 94631b0

Browse files
committed
addcube.obj
1 parent 5c86d95 commit 94631b0

File tree

3 files changed

+34
-34
lines changed

3 files changed

+34
-34
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ out/
77
CMakeSettings.json
88
.vscode/
99
.~*
10+
# clion files
11+
cmake-build-debug/
12+
cmake-build-release/
13+
.idea/
14+
.clang-format

assets/cube.obj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Blender 3.5.0
2+
# www.blender.org
3+
o Cube
4+
v 1.000000 1.000000 -1.000000
5+
v 1.000000 -1.000000 -1.000000
6+
v 1.000000 1.000000 1.000000
7+
v 1.000000 -1.000000 1.000000
8+
v -1.000000 1.000000 -1.000000
9+
v -1.000000 -1.000000 -1.000000
10+
v -1.000000 1.000000 1.000000
11+
v -1.000000 -1.000000 1.000000
12+
s 0
13+
f 1 5 7 3
14+
f 4 3 7 8
15+
f 8 7 5 6
16+
f 6 2 4 8
17+
f 2 1 3 4
18+
f 6 5 1 2

src/main.cpp

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
#include <sstream>
1111
#include <vector>
1212

13-
static std::vector<glm::vec3> vertices;
14-
static std::vector<glm::uvec3> faces;
13+
struct Vertex {
14+
float x, y, z;
15+
};
16+
17+
static std::vector<Vertex> vertices;
1518

1619
static void load_obj(std::string path) {
1720
std::ifstream file(path);
@@ -24,47 +27,21 @@ static void load_obj(std::string path) {
2427
while (std::getline(file, line)) {
2528
if (line.substr(0, 2) == "v ") {
2629
std::istringstream s(line.substr(2));
27-
glm::vec3 vertex;
30+
Vertex vertex;
2831
s >> vertex.x >> vertex.y >> vertex.z;
2932
vertices.push_back(vertex);
30-
} else if (line.substr(0, 2) == "f ") {
31-
std::istringstream s(line.substr(2));
32-
std::string splitted;
33-
std::vector<unsigned int> indices;
34-
while (std::getline(s, splitted, ' ')) {
35-
unsigned int index;
36-
std::istringstream(splitted) >> index;
37-
indices.push_back(index - 1);
38-
}
39-
for (size_t i = 2; i < indices.size(); i++) {
40-
glm::uvec3 face = {indices[0], indices[i - 1], indices[i]};
41-
faces.push_back(face);
42-
}
4333
}
4434
}
4535

4636
file.close();
47-
std::cout << "Loaded " << vertices.size() << " vertices and " << faces.size() << " faces.\n";
48-
}
49-
50-
static glm::vec3 colorAt(glm::vec3 const &pos) {
51-
return (pos + 1.0f) / 2.0f;
37+
std::cout << "Loaded " << vertices.size() << " vertices.\n";
5238
}
5339

5440
static void draw_obj() {
55-
glBegin(GL_TRIANGLES);
56-
57-
for (auto const &face : faces) {
58-
auto const &a = vertices.at(face.x);
59-
auto const &b = vertices.at(face.y);
60-
auto const &c = vertices.at(face.z);
61-
62-
glColor3fv(glm::value_ptr(colorAt(a)));
63-
glVertex3fv(glm::value_ptr(a));
64-
glColor3fv(glm::value_ptr(colorAt(b)));
65-
glVertex3fv(glm::value_ptr(b));
66-
glColor3fv(glm::value_ptr(colorAt(c)));
67-
glVertex3fv(glm::value_ptr(c));
41+
glBegin(GL_POINTS);
42+
43+
for (auto const &vertex : vertices) {
44+
glVertex3f(vertex.x, vertex.y, vertex.z);
6845
}
6946

7047
CHECK_GL(glEnd());

0 commit comments

Comments
 (0)