|
6 | 6 | #include <iostream> |
7 | 7 | #include <cstring> |
8 | 8 | #include <cstdlib> |
| 9 | +#include <fstream> |
| 10 | +#include <sstream> |
| 11 | +#include <vector> |
9 | 12 |
|
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() { |
11 | 55 | 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 | + |
18 | 70 | 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(); |
40 | 80 | } |
41 | 81 |
|
42 | 82 | int main() { |
@@ -99,15 +139,11 @@ int main() { |
99 | 139 | } |
100 | 140 | std::cerr << "OpenGL version: " << glGetString(GL_VERSION) << '\n'; |
101 | 141 |
|
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(); |
107 | 143 | // start main game loop |
108 | 144 | while (!glfwWindowShouldClose(window)) { |
109 | 145 | // render graphics |
110 | | - CHECK_GL(glClear(GL_COLOR_BUFFER_BIT)); |
| 146 | + CHECK_GL(glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT)); |
111 | 147 | render(); |
112 | 148 | // refresh screen |
113 | 149 | glfwSwapBuffers(window); |
|
0 commit comments