aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
authorMateja Marić <mail@matejamaric.com>2021-02-19 15:19:45 +0100
committerGitHub <noreply@github.com>2021-02-19 15:19:45 +0100
commit3b89cf20f583a82a3cd5bdbe04c07630838af5c3 (patch)
tree46b427b725795b86e7414111720b547aacfddceb /src/main.cpp
parentea5f50550ea2027043c9c3d0f5d998a1a1d336a5 (diff)
parentcf3e1771a342764ca26b2af843f173d1e994ed9f (diff)
downloaderender-3b89cf20f583a82a3cd5bdbe04c07630838af5c3.tar.gz
erender-3b89cf20f583a82a3cd5bdbe04c07630838af5c3.zip
Merge pull request #7 from MatejaMaric/fix-issue-6
Fix issue #6
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp117
1 files changed, 69 insertions, 48 deletions
diff --git a/src/main.cpp b/src/main.cpp
index f229003..ca4ff1b 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,6 +1,4 @@
-#include<chrono>
-
-#include<SFML/Window.hpp>
+#include<SFML/Window.hpp>
#include<SFML/Graphics.hpp>
#include"Render.h"
@@ -30,54 +28,77 @@ int main()
Render render(width, height);
- sf::Image image;
- image.loadFromFile("model/modelTexture.png");
- Bitmap modelTexture(image.getSize().x, image.getSize().y);
- const sf::Uint8* pp = image.getPixelsPtr();
- for (int i = 0; i < image.getSize().x * image.getSize().y * 4; i += 4) {
- modelTexture.pixels[i + 0] = (unsigned char)pp[i + 0];
- modelTexture.pixels[i + 1] = (unsigned char)pp[i + 1];
- modelTexture.pixels[i + 2] = (unsigned char)pp[i + 2];
- modelTexture.pixels[i + 3] = (unsigned char)pp[i + 3];
- }
+ Bitmap* modelTexture;
+
+ bool textureLoaded = false;
std::chrono::high_resolution_clock::time_point time1 = std::chrono::high_resolution_clock::now();
- ObjModel model("model/model.obj");
+ sf::Image image;
+ textureLoaded = image.loadFromFile("model/modelTexture.png");
+ if (textureLoaded) {
+ modelTexture = new Bitmap(image.getSize().x, image.getSize().y);
+ const sf::Uint8* pp = image.getPixelsPtr();
+ for (int i = 0; i < image.getSize().x * image.getSize().y * 4; i += 4) {
+ modelTexture->pixels[i + 0] = (unsigned char)pp[i + 0];
+ modelTexture->pixels[i + 1] = (unsigned char)pp[i + 1];
+ modelTexture->pixels[i + 2] = (unsigned char)pp[i + 2];
+ modelTexture->pixels[i + 3] = (unsigned char)pp[i + 3];
+ }
+ }
std::chrono::high_resolution_clock::time_point time2 = std::chrono::high_resolution_clock::now();
- std::chrono::duration<double> speed = std::chrono::duration_cast<std::chrono::duration<double>>(time2 - time1);
- std::cout << "Model loading time: " << speed.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();
- }
+ std::chrono::duration<double> modelTextureLoadingTime = std::chrono::duration_cast<std::chrono::duration<double>>(time2 - time1);
+
+ ObjModel model("model/model.obj");
+
+ if (textureLoaded) {
+ std::cout << "Texture loading time: " << modelTextureLoadingTime.count() << "\n";
+ }
+ else {
+ std::cerr << "Couldn't load and/or find texture!\n";
+ }
+
+ 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;
return 0;
}