-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
80 lines (68 loc) · 1.87 KB
/
Model.cpp
File metadata and controls
80 lines (68 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Model.cpp
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <stdexcept>
class Model {
public:
Model(const std::string& filePath);
void loadModel();
void displayModelInfo() const;
private:
std::string filePath;
std::vector<std::string> vertices;
std::vector<std::string> textures;
std::vector<std::string> normals;
void parseLine(const std::string& line);
};
Model::Model(const std::string& filePath) : filePath(filePath) {}
void Model::loadModel() {
std::ifstream file(filePath);
if (!file.is_open()) {
throw std::runtime_error("Could not open file: " + filePath);
}
std::string line;
while (std::getline(file, line)) {
parseLine(line);
}
file.close();
}
void Model::parseLine(const std::string& line) {
std::istringstream iss(line);
std::string prefix;
iss >> prefix;
if (prefix == "v") {
std::string vertex;
while (iss >> vertex) {
vertices.push_back(vertex);
}
} else if (prefix == "vt") {
std::string texture;
while (iss >> texture) {
textures.push_back(texture);
}
} else if (prefix == "vn") {
std::string normal;
while (iss >> normal) {
normals.push_back(normal);
}
}
}
void Model::displayModelInfo() const {
std::cout << "Model Information:" << std::endl;
std::cout << "Vertices: " << vertices.size() << std::endl;
std::cout << "Textures: " << textures.size() << std::endl;
std::cout << "Normals: " << normals.size() << std::endl;
}
int main() {
try {
Model fighterJetModel("fighter_jet_model.obj");
fighterJetModel.loadModel();
fighterJetModel.displayModelInfo();
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
return 0;
}