diff options
author | Mateja <mail@matejamaric.com> | 2021-02-19 15:10:53 +0100 |
---|---|---|
committer | Mateja <mail@matejamaric.com> | 2021-02-19 15:10:53 +0100 |
commit | cf3e1771a342764ca26b2af843f173d1e994ed9f (patch) | |
tree | 46b427b725795b86e7414111720b547aacfddceb /src/ObjModel.cpp | |
parent | 79048d1ad84e33d5eacbc5dfbef752948e9a787f (diff) | |
download | erender-cf3e1771a342764ca26b2af843f173d1e994ed9f.tar.gz erender-cf3e1771a342764ca26b2af843f173d1e994ed9f.zip |
Add load model check. Don't render if model and/or texture is not
loaded.
Diffstat (limited to 'src/ObjModel.cpp')
-rw-r--r-- | src/ObjModel.cpp | 100 |
1 files changed, 53 insertions, 47 deletions
diff --git a/src/ObjModel.cpp b/src/ObjModel.cpp index aa81cec..8933da8 100644 --- a/src/ObjModel.cpp +++ b/src/ObjModel.cpp @@ -10,53 +10,59 @@ ObjModel::ObjModel(std::string fileName) int hlp = 0;
std::ifstream f(fileName);
- std::string s0;
- while (getline(f, s0)) {
- std::stringstream ss0(s0);
- std::string s1 = "";
- ss0 >> s1;
- if (s1.compare(0, s1.length(), "v") == 0) {
- ss0 >> s1;
- float x = std::stof(s1);
- ss0 >> s1;
- float y = std::stof(s1);
- ss0 >> s1;
- float z = std::stof(s1);
- Vertice t(x, y, z, 1.0f, 0.0f, 0.0f);
- vertices.push_back(t);
- }
- else if (s1.compare(0, s1.length(), "vn") == 0) {
- ss0 >> s1;
- float x = std::stof(s1);
- ss0 >> s1;
- float y = std::stof(s1);
- ss0 >> s1;
- float z = std::stof(s1);
- Vector4f v(x, y, z, 0.0f);
- normals.push_back(v);
- }
- else if (s1.compare(0, s1.length(), "vt") == 0) {
- ss0 >> s1;
- float u = std::stof(s1);
- ss0 >> s1;
- float v = std::stof(s1);
- Texture_Coordinates kt;
- kt.u = u; kt.v = v;
- uvcoo.push_back(kt);
- }
- else if (s1.compare(0, s1.length(), "f") == 0) {
- Triangle pt;
- for (int i = 0; i < 3; i++) {
- ss0 >> s1;
- std::stringstream ss1(s1);
- std::string s2;
- for (int j = 0; std::getline(ss1, s2, '/'); j++) {
- pt.p[i][j] = std::stoi(s2);
- }
- }
- faces.push_back(pt);
- }
- }
+ if (f.is_open()) {
+ std::string s0;
+ while (getline(f, s0)) {
+ std::stringstream ss0(s0);
+ std::string s1 = "";
+ ss0 >> s1;
+ if (s1.compare(0, s1.length(), "v") == 0) {
+ ss0 >> s1;
+ float x = std::stof(s1);
+ ss0 >> s1;
+ float y = std::stof(s1);
+ ss0 >> s1;
+ float z = std::stof(s1);
+ Vertice t(x, y, z, 1.0f, 0.0f, 0.0f);
+ vertices.push_back(t);
+ }
+ else if (s1.compare(0, s1.length(), "vn") == 0) {
+ ss0 >> s1;
+ float x = std::stof(s1);
+ ss0 >> s1;
+ float y = std::stof(s1);
+ ss0 >> s1;
+ float z = std::stof(s1);
+ Vector4f v(x, y, z, 0.0f);
+ normals.push_back(v);
+ }
+ else if (s1.compare(0, s1.length(), "vt") == 0) {
+ ss0 >> s1;
+ float u = std::stof(s1);
+ ss0 >> s1;
+ float v = std::stof(s1);
+ Texture_Coordinates kt;
+ kt.u = u; kt.v = v;
+ uvcoo.push_back(kt);
+ }
+ else if (s1.compare(0, s1.length(), "f") == 0) {
+ Triangle pt;
+ for (int i = 0; i < 3; i++) {
+ ss0 >> s1;
+ std::stringstream ss1(s1);
+ std::string s2;
+ for (int j = 0; std::getline(ss1, s2, '/'); j++) {
+ pt.p[i][j] = std::stoi(s2);
+ }
+ }
+ faces.push_back(pt);
+ }
+ }
+ isLoaded = true;
+ }
+ else {
+ isLoaded = false;
+ }
std::chrono::high_resolution_clock::time_point time2 = std::chrono::high_resolution_clock::now();
loadingTime = std::chrono::duration_cast<std::chrono::duration<double>>(time2 - time1);
|