Skip to content

Commit

Permalink
Load OBJ Close #4 (#16)
Browse files Browse the repository at this point in the history
* .obj loader

* chore: formatter

---------

Co-authored-by: Júlio Pinto <[email protected]>
  • Loading branch information
gramosomi and JulioJPinto authored Mar 7, 2024
1 parent f9cbe06 commit 3017ed3
Show file tree
Hide file tree
Showing 6 changed files with 9,935 additions and 606 deletions.
6 changes: 6 additions & 0 deletions engine/include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ typedef struct Point {
// Function to convert Point to string
std::vector<Point> parseFile(std::string filename);

// Function to parse OBJ file
std::vector<Point> parseOBJfile(std::string filename, std::string type);

// Function to parse 3D file
std::vector<Point> parse3Dfile(std::string filename);

#endif // UTILS_HPP
6 changes: 2 additions & 4 deletions engine/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,12 @@ void processNormalKeys(unsigned char key, int x, int y) {
zoom = 1.0f;
break;
case 'o':
if (zoom > 0.2) {
if (zoom > 0.1) {
zoom -= 0.1;
}
break;
case 'i':
if (zoom < 2.5) {
zoom += 0.1;
}
zoom += 0.1;
break;
default:
break;
Expand Down
53 changes: 52 additions & 1 deletion engine/src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,69 @@
#include <vector>

std::vector<Point> parseFile(std::string filename) {
std::vector<Point> points;
std::ifstream file(filename);
std::vector<Point> points;

if (!file.is_open()) {
std::cerr << "Error: Unable to open file " << filename << std::endl;
return points;
}

printf("filename: %s\n", filename.c_str());
if (filename.find(".obj") != std::string::npos) {
points = parseOBJfile(filename, "obj");
} else if (filename.find(".3d") != std::string::npos) {
points = parse3Dfile(filename);
} else {
std::cerr << "Error: File format not supported" << std::endl;
}

file.close();

return points;
}

std::vector<Point> parseOBJfile(std::string filename, std::string type) {
std::vector<Point> points;
std::vector<Point> sorted_points;
std::ifstream file(filename);

std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
std::string type;
iss >> type;
if (type == "v") {
Point point;
iss >> point.x >> point.y >> point.z;
points.push_back(point);
} else if (type == "f") {
std::string vertex;
std::vector<int> vertex_indices;
while (iss >> vertex) {
std::string index = vertex.substr(0, vertex.find("/"));
vertex_indices.push_back(std::stoi(index) - 1);
}
for (int i = 0; i < vertex_indices.size(); i++) {
sorted_points.push_back(points[vertex_indices[i]]);
}
}
}
file.close();

return sorted_points;
}

std::vector<Point> parse3Dfile(std::string filename) {
std::vector<Point> points;
std::ifstream file(filename);

Point point;
while (file >> point.x >> point.y >> point.z) {
points.push_back(point);
}

file.close();

return points;
}
Loading

0 comments on commit 3017ed3

Please sign in to comment.