Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load OBJ Close #4 #16

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading