aboutsummaryrefslogtreecommitdiff
path: root/src/obj_parser.cpp
diff options
context:
space:
mode:
authorvimene <vincent.menegaux@gmail.com>2024-12-31 03:40:14 +0100
committervimene <vincent.menegaux@gmail.com>2024-12-31 03:40:14 +0100
commitcc6fb8c33637566a7b116d46440e6063f016deea (patch)
tree5cc83f84525507994add57df7dd974e6611d675a /src/obj_parser.cpp
parent6b765a85cf81bf4b7162e4c9280dd4054581c611 (diff)
downloadengine-cc6fb8c33637566a7b116d46440e6063f016deea.tar.gz
various improvements
- added quaternions and rewrote all rotations to use them - added transforms to put all object transforms in a single place - added Wavefront .obj file parser - removed frame buffer's abstract class - improved vectors, matrices, triangles, vertices and vertices data by putting all code in header file - added vector's operations - changed from NULL to nullptr - miscellaneous improvements
Diffstat (limited to 'src/obj_parser.cpp')
-rw-r--r--src/obj_parser.cpp81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/obj_parser.cpp b/src/obj_parser.cpp
new file mode 100644
index 0000000..7791678
--- /dev/null
+++ b/src/obj_parser.cpp
@@ -0,0 +1,81 @@
+#include "obj_parser.h"
+
+#include <iostream>
+#include <fstream>
+#include <string>
+#include <vector>
+#include <cstddef>
+#include <array>
+#include "math/vector.h"
+#include "o3d/mesh.h"
+#include "o3d/vertex_data.h"
+
+namespace engine {
+
+namespace {
+
+std::vector<std::string> split(const std::string& s, char sep) {
+ std::vector<std::string> res;
+ std::string::size_type last_ind = 0;
+ for (std::string::size_type ind = 0; ind < s.length(); ind++) {
+ if (s[ind] == sep) {
+ res.push_back(s.substr(last_ind, ind - last_ind));
+ last_ind = ind + 1;
+ }
+ }
+ res.push_back(s.substr(last_ind));
+ return res;
+}
+
+}
+
+o3d::Mesh parse_object(const std::string& obj_path) {
+ o3d::Mesh mesh;
+ mesh.vertices_data.push_back(o3d::VertexData(0.f, 0.f));
+ std::ifstream obj_file(obj_path);
+ std::string line;
+ while (std::getline(obj_file, line)) {
+ if (line.length() == 0 || line[0] == '#')
+ continue;
+ if (line.rfind("o ", 0) == 0) {
+ std::cout << "Object: " << line.substr(2) << std::endl;
+ } else if (line.rfind("v ", 0) == 0) {
+ auto s_coords = split(line.substr(2), ' ');
+ math::Vector3 v{std::stof(s_coords[0]), std::stof(s_coords[1]), std::stof(s_coords[2])};
+ std::cout << "Vertex x: " << v.x << " y: " << v.y << " z: " << v.z << std::endl;
+ mesh.vertices.push_back(v);
+ } else if (line.rfind("vn ", 0) == 0) {
+ auto s_coords = split(line.substr(3), ' ');
+ math::Vector3 vn{std::stof(s_coords[0]), std::stof(s_coords[1]), std::stof(s_coords[2])};
+ std::cout << "Vertex normal x: " << vn.x << " y: " << vn.y << " z: " << vn.z << std::endl;
+ mesh.normals.push_back(vn);
+ } else if (line.rfind("s ", 0) == 0) {
+ auto smooth = false;
+ auto s_smooth = line.substr(2);
+ if (s_smooth == "0" || s_smooth == "off") {
+ smooth = false;
+ } else if (s_smooth == "1" || s_smooth == "on") {
+ smooth = true;
+ }
+ std::cout << "Smooth: " << std::boolalpha << smooth << std::endl;
+ } else if (line.rfind("f ", 0) == 0) {
+ std::array<std::array<std::size_t, 3>, 3> indices;
+ auto line_split = split(line.substr(2), ' ');
+ for (int i = 0; i < 3; i++) {
+ auto indices_s_group = split(line_split[i], '/');
+ indices[i][0] = std::stoi(indices_s_group[0]) - 1;
+ indices[i][1] = std::stoi(indices_s_group[2]) - 1;
+ indices[i][2] = 0;
+ }
+ std::cout << "Face:"
+ << " 1: vertex: " << indices[0][0] << " normal: " << indices[0][1]
+ << " 2: vertex: " << indices[1][0] << " normal: " << indices[1][1]
+ << " 3: vertex: " << indices[2][0] << " normal: " << indices[2][1]
+ << std::endl;
+ mesh.indices.push_back(indices);
+ }
+ }
+ return mesh;
+}
+
+}