diff --git a/src/engine/progressiverenderer.cpp b/src/engine/progressiverenderer.cpp index 3e7b7f0..2fd5233 100644 --- a/src/engine/progressiverenderer.cpp +++ b/src/engine/progressiverenderer.cpp @@ -1,12 +1,11 @@ #include "progressiverenderer.hpp" -#define TINYOBJLOADER_IMPLEMENTATION // define this in only *one* .cpp -#include "../util/tiny_obj_loader.hpp" - #include "../common.hpp" #include "../pixel.hpp" #include "../display/displayinterface.hpp" +#include "../util/assetloader.hpp" + #include "../definitions/primatives/primative.hpp" #include "../definitions/primatives/triangle.hpp" #include "../definitions/camera.hpp" @@ -22,94 +21,8 @@ void ProgressiveRenderer::Init(DisplayInterface* interface, Scene* scene) { m_scene = scene; } -glm::vec3 getNormal(glm::vec3 p0, glm::vec3 p1, glm::vec3 p2) { - glm::vec3 u = p1 - p0; - glm::vec3 v = p2 - p0; - - glm::vec3 normal; - normal.x = u.y * v.z - u.z * v.y; - normal.y = u.z * v.x - u.x * v.z; - normal.z = u.x * v.y - u.y - v.x; - - return normal; -} - -std::vector loadTriangles(std::string path) { - tinyobj::attrib_t attrib; - std::vector shapes; - std::vector materials; - - std::string warn, err; - - bool canLoad = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, path.c_str()); - - if (!err.empty() || !canLoad) { - std::cerr << "Cannot load obj: '" << path << "': " << err << std::endl; - exit(0); - } - - if (!warn.empty()) { - std::cerr << "Warning from obj loader while loading obj: '" << path << "': " << warn << std::endl; - } - - - std::vector triangles; - - - for (size_t s = 0; s < shapes.size(); s++) { - size_t index_offset = 0; - for (size_t f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++) { - int fv = shapes[s].mesh.num_face_vertices[f]; - if (fv == 3) { - tinyobj::real_t avx[3]; - tinyobj::real_t avy[3]; - tinyobj::real_t avz[3]; - - tinyobj::real_t anx[3]; - tinyobj::real_t any[3]; - tinyobj::real_t anz[3]; - - for (size_t v = 0; v < fv; v++) { - tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v]; - - avx[v] = attrib.vertices[3 * idx.vertex_index + 0]; - avy[v] = attrib.vertices[3 * idx.vertex_index + 1]; - avz[v] = attrib.vertices[3 * idx.vertex_index + 2]; - - anx[v] = attrib.normals[3 * idx.normal_index + 0]; - any[v] = attrib.normals[3 * idx.normal_index + 1]; - anz[v] = attrib.normals[3 * idx.normal_index + 2]; - } - - // tinyobj::material_t material = materials[shapes[s].mesh.material_ids[f]]; - - Triangle* tmp = new Triangle { - {avx[0], avy[0], avz[0]}, - {avx[1], avy[1], avz[1]}, - {avx[2], avy[2], avz[2]}, - - {anx[0], any[0], anz[0]}, - {anx[1], any[1], anz[1]}, - {anx[2], any[2], anz[2]}, - }; - - triangles.push_back(tmp); - } - index_offset += fv; - } - } - - std::vector objects; - - for (const auto& triangle : triangles) - objects.push_back(triangle); - - return objects; -} - - void ProgressiveRenderer::Render() { - m_scene->objects = loadTriangles("/home/ben/programming/inferno/resources/cornell.obj"); + m_scene->objects = LoadTrianglesBasic("/home/ben/programming/inferno/resources/cornell.obj"); for (const auto& object : m_scene->objects) object->Translate({ 0.0f, -1.0f, -3.0f }); @@ -139,7 +52,6 @@ void ProgressiveRenderer::Render() { glm::vec3 normal = hit->SurfaceNormal(hitPoint); Pixel col((normal.x + 1.0f) * 127.5f, (normal.y + 1.0f) * 127.5f, (normal.z + 1.0f) * 127.5f); m_interface->SetPixelSafe(x, y, col.rgb()); - } // Swap framebuffers diff --git a/src/util/assetloader.cpp b/src/util/assetloader.cpp new file mode 100644 index 0000000..79eae81 --- /dev/null +++ b/src/util/assetloader.cpp @@ -0,0 +1,92 @@ +#include "assetloader.hpp" + +#define TINYOBJLOADER_IMPLEMENTATION // define this in only *one* .cpp +#include "tiny_obj_loader.hpp" + +#include "../definitions/primatives/primative.hpp" +#include "../definitions/primatives/triangle.hpp" +#include "../maths.hpp" + +glm::vec3 getNormal(glm::vec3 p0, glm::vec3 p1, glm::vec3 p2) { + glm::vec3 u = p1 - p0; + glm::vec3 v = p2 - p0; + + glm::vec3 normal; + normal.x = u.y * v.z - u.z * v.y; + normal.y = u.z * v.x - u.x * v.z; + normal.z = u.x * v.y - u.y - v.x; + + return normal; +} + +std::vector LoadTrianglesBasic(std::string path) { + tinyobj::attrib_t attrib; + std::vector shapes; + std::vector materials; + + std::string warn, err; + + bool canLoad = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, path.c_str()); + + if (!err.empty() || !canLoad) { + std::cerr << "Cannot load obj: '" << path << "': " << err << std::endl; + exit(0); + } + + if (!warn.empty()) { + std::cerr << "Warning from obj loader while loading obj: '" << path << "': " << warn << std::endl; + } + + std::vector triangles; + + for (size_t s = 0; s < shapes.size(); s++) { + size_t index_offset = 0; + for (size_t f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++) { + int fv = shapes[s].mesh.num_face_vertices[f]; + if (fv == 3) { + tinyobj::real_t avx[3]; + tinyobj::real_t avy[3]; + tinyobj::real_t avz[3]; + + tinyobj::real_t anx[3]; + tinyobj::real_t any[3]; + tinyobj::real_t anz[3]; + + for (size_t v = 0; v < fv; v++) { + tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v]; + + avx[v] = attrib.vertices[3 * idx.vertex_index + 0]; + avy[v] = attrib.vertices[3 * idx.vertex_index + 1]; + avz[v] = attrib.vertices[3 * idx.vertex_index + 2]; + + anx[v] = attrib.normals[3 * idx.normal_index + 0]; + any[v] = attrib.normals[3 * idx.normal_index + 1]; + anz[v] = attrib.normals[3 * idx.normal_index + 2]; + } + + // tinyobj::material_t material = materials[shapes[s].mesh.material_ids[f]]; + + Triangle* tmp = new Triangle { + {avx[0], avy[0], avz[0]}, + {avx[1], avy[1], avz[1]}, + {avx[2], avy[2], avz[2]}, + + {anx[0], any[0], anz[0]}, + {anx[1], any[1], anz[1]}, + {anx[2], any[2], anz[2]}, + }; + + triangles.push_back(tmp); + } + index_offset += fv; + } + } + + std::vector objects; + + for (const auto& triangle : triangles) + objects.push_back(triangle); + + return objects; +} + diff --git a/src/util/assetloader.hpp b/src/util/assetloader.hpp new file mode 100644 index 0000000..365de30 --- /dev/null +++ b/src/util/assetloader.hpp @@ -0,0 +1,13 @@ +#ifndef INFERNO_UTIL_ASSETLOADER_H_ +#define INFERNO_UTIL_ASSETLOADER_H_ + +#include "../common.hpp" + +#include +#include + +class Primative; + +std::vector LoadTrianglesBasic(std::string path); + +#endif