Skip to content

Commit 5c86d95

Browse files
committed
colorize
1 parent ea055f8 commit 5c86d95

File tree

1 file changed

+70
-34
lines changed

1 file changed

+70
-34
lines changed

src/main.cpp

Lines changed: 70 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,77 @@
66
#include <iostream>
77
#include <cstring>
88
#include <cstdlib>
9+
#include <fstream>
10+
#include <sstream>
11+
#include <vector>
912

10-
static void render() {
13+
static std::vector<glm::vec3> vertices;
14+
static std::vector<glm::uvec3> faces;
15+
16+
static void load_obj(std::string path) {
17+
std::ifstream file(path);
18+
if (!file.is_open()) {
19+
std::cerr << "Failed to open file: " << path << '\n';
20+
return;
21+
}
22+
23+
std::string line;
24+
while (std::getline(file, line)) {
25+
if (line.substr(0, 2) == "v ") {
26+
std::istringstream s(line.substr(2));
27+
glm::vec3 vertex;
28+
s >> vertex.x >> vertex.y >> vertex.z;
29+
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+
}
43+
}
44+
}
45+
46+
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;
52+
}
53+
54+
static void draw_obj() {
1155
glBegin(GL_TRIANGLES);
12-
glColor3f(1.0f, 0.0f, 0.0f);
13-
glVertex3f(0.0f, 0.5f, 0.0f);
14-
glColor3f(0.0f, 1.0f, 0.0f);
15-
glVertex3f(-0.5f, -0.5f, 0.0f);
16-
glColor3f(0.0f, 0.0f, 1.0f);
17-
glVertex3f(0.5f, -0.5f, 0.0f);
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));
68+
}
69+
1870
CHECK_GL(glEnd());
19-
/* glBegin(GL_TRIANGLES); */
20-
/* constexpr int n = 100; */
21-
/* constexpr float pi = 3.1415926535897f; */
22-
/* float radius = 0.5f; */
23-
/* float inner_radius = 0.25f; */
24-
/* static int x = 0; */
25-
/* x++; */
26-
/* if (x > n) */
27-
/* x -= n; */
28-
/* for (int i = 0; i < x; i++) { */
29-
/* float angle = i / (float)n * pi * 2; */
30-
/* float angle_next = (i + 1) / (float)n * pi * 2; */
31-
/* glVertex3f(0.0f, 0.0f, 0.0f); */
32-
/* glVertex3f(radius * sinf(angle), radius * cosf(angle), 0.0f); */
33-
/* glVertex3f(radius * sinf(angle_next), radius * cosf(angle_next), 0.0f); */
34-
/* glVertex3f(inner_radius * sinf(angle), inner_radius * cosf(angle), 0.0f); */
35-
/* glVertex3f(inner_radius * sinf(angle_next), inner_radius * cosf(angle_next), 0.0f); */
36-
/* glVertex3f(inner_radius * sinf(angle), inner_radius * cosf(angle), 0.0f); */
37-
/* glVertex3f(radius * sinf(angle_next), radius * cosf(angle_next), 0.0f); */
38-
/* } */
39-
/* CHECK_GL(glEnd()); */
71+
}
72+
73+
static void initialize() {
74+
load_obj("/home/bate/Codes/zeno_assets/assets/monkey.obj");
75+
CHECK_GL(glEnable(GL_DEPTH_TEST));
76+
}
77+
78+
static void render() {
79+
draw_obj();
4080
}
4181

4282
int main() {
@@ -99,15 +139,11 @@ int main() {
99139
}
100140
std::cerr << "OpenGL version: " << glGetString(GL_VERSION) << '\n';
101141

102-
CHECK_GL(glEnable(GL_POINT_SMOOTH));
103-
CHECK_GL(glEnable(GL_BLEND));
104-
CHECK_GL(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
105-
CHECK_GL(glPointSize(64.0f));
106-
142+
initialize();
107143
// start main game loop
108144
while (!glfwWindowShouldClose(window)) {
109145
// render graphics
110-
CHECK_GL(glClear(GL_COLOR_BUFFER_BIT));
146+
CHECK_GL(glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT));
111147
render();
112148
// refresh screen
113149
glfwSwapBuffers(window);

0 commit comments

Comments
 (0)