aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMateja <mail@matejamaric.com>2021-02-19 15:10:53 +0100
committerMateja <mail@matejamaric.com>2021-02-19 15:10:53 +0100
commitcf3e1771a342764ca26b2af843f173d1e994ed9f (patch)
tree46b427b725795b86e7414111720b547aacfddceb
parent79048d1ad84e33d5eacbc5dfbef752948e9a787f (diff)
downloaderender-cf3e1771a342764ca26b2af843f173d1e994ed9f.tar.gz
erender-cf3e1771a342764ca26b2af843f173d1e994ed9f.zip
Add load model check. Don't render if model and/or texture is not
loaded.
-rw-r--r--src/ObjModel.cpp100
-rw-r--r--src/ObjModel.h1
-rw-r--r--src/main.cpp73
3 files changed, 94 insertions, 80 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);
diff --git a/src/ObjModel.h b/src/ObjModel.h
index 5ceed26..63d49ef 100644
--- a/src/ObjModel.h
+++ b/src/ObjModel.h
@@ -22,6 +22,7 @@ public:
std::vector<Texture_Coordinates> uvcoo;
std::vector<Vector4f> normals;
std::vector<Triangle> faces;
+ bool isLoaded;
std::chrono::duration<double> loadingTime;
ObjModel();
ObjModel(std::string fileName);
diff --git a/src/main.cpp b/src/main.cpp
index 11e199d..ca4ff1b 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -57,39 +57,46 @@ int main()
std::cerr << "Couldn't load and/or find texture!\n";
}
- std::cout << "Model loading time: " << model.loadingTime.count() << "s\n";
- std::cout << "Vertices count: " << model.vertices.size() << "\nNumber of normals: " << model.normals.size() << "\nUV count: " << model.uvcoo.size() << "\n";
- std::cout << "Number of faces: " << model.faces.size() << "\n";
-
- while (window.isOpen())
- {
- while (window.pollEvent(event))
- if (event.type == sf::Event::Closed)
- window.close();
-
- time = clock.getElapsedTime();
-
- Vertice a1, b1, c1, d1, e1, f1;
-
- Matrix4f move = Matrix4f::move(0.0f, -150.0f, 600.0f);
- Matrix4f rotate = Matrix4f::rotate(0.0f, time.asSeconds() * 25.0f, 0.0f);
- Matrix4f scale = Matrix4f::scale(1.0f, 1.0f, 1.0f);
- Matrix4f project = Matrix4f::perspective(1.0f, 10000.0f, fov, width / height);
- Matrix4f end_matrix = project * move * rotate * scale;
-
- ObjModel projectedModel = model.multiplyMatrix(end_matrix);
- projectedModel.divideW();
- projectedModel.screenspace(width, height);
-
-
- render.fill(255, 255, 255);
- render.resetZBuf();
- render.renderModel(projectedModel, *modelTexture);
-
- texture.update((sf::Uint8*)render.pixels);
- window.draw(sprite);
- window.display();
- }
+ if (model.isLoaded) {
+ std::cout << "Model loading time: " << model.loadingTime.count() << "s\n";
+ std::cout << "Vertices count: " << model.vertices.size() << "\nNumber of normals: " << model.normals.size() << "\nUV count: " << model.uvcoo.size() << "\n";
+ std::cout << "Number of faces: " << model.faces.size() << "\n";
+ }
+ else {
+ std::cerr << "Couldn't load and/or find model!\n";
+ }
+
+ if (textureLoaded && model.isLoaded) {
+ while (window.isOpen())
+ {
+ while (window.pollEvent(event))
+ if (event.type == sf::Event::Closed)
+ window.close();
+
+ time = clock.getElapsedTime();
+
+ Vertice a1, b1, c1, d1, e1, f1;
+
+ Matrix4f move = Matrix4f::move(0.0f, -150.0f, 600.0f);
+ Matrix4f rotate = Matrix4f::rotate(0.0f, time.asSeconds() * 25.0f, 0.0f);
+ Matrix4f scale = Matrix4f::scale(1.0f, 1.0f, 1.0f);
+ Matrix4f project = Matrix4f::perspective(1.0f, 10000.0f, fov, width / height);
+ Matrix4f end_matrix = project * move * rotate * scale;
+
+ ObjModel projectedModel = model.multiplyMatrix(end_matrix);
+ projectedModel.divideW();
+ projectedModel.screenspace(width, height);
+
+
+ render.fill(255, 255, 255);
+ render.resetZBuf();
+ render.renderModel(projectedModel, *modelTexture);
+
+ texture.update((sf::Uint8*)render.pixels);
+ window.draw(sprite);
+ window.display();
+ }
+ }
delete modelTexture;