diff --git a/3rdParty/tiny_obj_loader.h b/3rdParty/tiny_obj_loader.h index c23acc0..e5ea947 100644 --- a/3rdParty/tiny_obj_loader.h +++ b/3rdParty/tiny_obj_loader.h @@ -2134,6 +2134,7 @@ void LoadMtl(std::map *material_map, has_d = false; has_tr = false; + has_kd = false; // set new mtl name token += 7; @@ -2708,7 +2709,7 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, sw.vertex_id = vid; - while (!IS_NEW_LINE(token[0])) { + while (!IS_NEW_LINE(token[0]) && token[0] != '#') { real_t j, w; // joint_id should not be negative, weight may be negative // TODO(syoyo): # of elements check @@ -2749,7 +2750,7 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, __line_t line; - while (!IS_NEW_LINE(token[0])) { + while (!IS_NEW_LINE(token[0]) && token[0] != '#') { vertex_index_t vi; if (!parseTriple(&token, static_cast(v.size() / 3), static_cast(vn.size() / 3), @@ -2780,7 +2781,7 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, __points_t pts; - while (!IS_NEW_LINE(token[0])) { + while (!IS_NEW_LINE(token[0]) && token[0] != '#') { vertex_index_t vi; if (!parseTriple(&token, static_cast(v.size() / 3), static_cast(vn.size() / 3), @@ -2815,7 +2816,7 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, face.smoothing_group_id = current_smoothing_id; face.vertex_indices.reserve(3); - while (!IS_NEW_LINE(token[0])) { + while (!IS_NEW_LINE(token[0]) && token[0] != '#') { vertex_index_t vi; if (!parseTriple(&token, static_cast(v.size() / 3), static_cast(vn.size() / 3), @@ -2951,7 +2952,7 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, std::vector names; - while (!IS_NEW_LINE(token[0])) { + while (!IS_NEW_LINE(token[0]) && token[0] != '#') { std::string str = parseString(&token); names.push_back(str); token += strspn(token, " \t\r"); // skip tag @@ -3245,7 +3246,7 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, token += strspn(token, " \t"); indices.clear(); - while (!IS_NEW_LINE(token[0])) { + while (!IS_NEW_LINE(token[0]) && token[0] != '#') { vertex_index_t vi = parseRawTriple(&token); index_t idx; @@ -3360,7 +3361,7 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, if (token[0] == 'g' && IS_SPACE((token[1]))) { names.clear(); - while (!IS_NEW_LINE(token[0])) { + while (!IS_NEW_LINE(token[0]) && token[0] != '#') { std::string str = parseString(&token); names.push_back(str); token += strspn(token, " \t\r"); // skip tag diff --git a/Engine/Graphics/3DRenderer.cpp b/Engine/Graphics/3DRenderer.cpp index 90fd526..c483d3e 100644 --- a/Engine/Graphics/3DRenderer.cpp +++ b/Engine/Graphics/3DRenderer.cpp @@ -7,6 +7,9 @@ #define SF_COLOR_4CHEX(h) sf::Color((uint32_t)h) +#define FAR_Z (100.f) +#define NEAR_Z (0.1f) + //#define DISABLE_AABB_CLIPPING //#define DISABLE_TRIANGLE_CLIPPING //#define DISABLE_WIREFRAME_MODE @@ -82,7 +85,7 @@ Graphic3DRenderer::~Graphic3DRenderer() {} void Graphic3DRenderer::SetRTSize(unsigned int w, unsigned int h) { mRTSize.x = w; mRTSize.y = h; - mMainCamera->SetFrustrum(75.0f, mRTSize.x/mRTSize.y, 1.0f, 100.f); + mMainCamera->SetFrustrum(75.0f, mRTSize.x/mRTSize.y, NEAR_Z, FAR_Z); } void Graphic3DRenderer::UpdateCamera(CAMERA_MOVE type, const float value) { @@ -172,8 +175,8 @@ float Graphic3DRenderer::ComputeSGRatio() { // The triangle made by the ground plane intersection with the frustum. This intersection // cross the far plane at some point. Instead of computing the coordinate of the point, we // directly use the far plane length to get the corresponding ratio for the screen. - sgRatio = -(altitude * fovCos - 1000.f * fovThetaSin) - / (2.f * 1000.f * fovSin * thetaCos); + sgRatio = -(altitude * fovCos - FAR_Z * fovThetaSin) + / (2.f * FAR_Z * fovSin * thetaCos); } // Clamp @@ -317,14 +320,12 @@ void Graphic3DRenderer::DrawSceneObjects(sf::RenderTexture& context) { V3 = M3D_V4Divide(V3, M3D_V4SplatW(V3)); // Finally project from NDC to the screen - V1 = M3D_V3TransformNDCToViewport(V1, 0.f, 0.f, mRTSize.x, mRTSize.y, 1.f, 100.f); - V2 = M3D_V3TransformNDCToViewport(V2, 0.f, 0.f, mRTSize.x, mRTSize.y, 1.f, 100.f); - V3 = M3D_V3TransformNDCToViewport(V3, 0.f, 0.f, mRTSize.x, mRTSize.y, 1.f, 100.f); + V1 = M3D_V3TransformNDCToViewport(V1, 0.f, 0.f, mRTSize.x, mRTSize.y, NEAR_Z, FAR_Z); + V2 = M3D_V3TransformNDCToViewport(V2, 0.f, 0.f, mRTSize.x, mRTSize.y, NEAR_Z, FAR_Z); + V3 = M3D_V3TransformNDCToViewport(V3, 0.f, 0.f, mRTSize.x, mRTSize.y, NEAR_Z, FAR_Z); // Simplified back-face culling on 2D viewport triangle if (M3D_V4GetX(M3D_Tri2DNormal(V1,V2,V3)) > 0) { - - // Set pixels color depending of frustrum clipping type - debug purpose if (ri.frustrumClipType == DISJOINT) { drawPoints[0].color = drawPoints[1].color = drawPoints[2].color = sf::Color::Red; diff --git a/Engine/Graphics/UI.cpp b/Engine/Graphics/UI.cpp index da90897..ab01c9c 100644 --- a/Engine/Graphics/UI.cpp +++ b/Engine/Graphics/UI.cpp @@ -10,6 +10,8 @@ #include +#define SF_COLOR_4CHEX(h) sf::Color((uint32_t)h) + void UI::SetRTSize(unsigned int w, unsigned int h, bool recreateCanvas) { mRTSize.x = w; mRTSize.y = h; diff --git a/Engine/Utils/MeshHelper.hpp b/Engine/Utils/MeshHelper.hpp index 5c423d7..43d60f8 100644 --- a/Engine/Utils/MeshHelper.hpp +++ b/Engine/Utils/MeshHelper.hpp @@ -26,6 +26,7 @@ struct Vertex { } M3D_F3 pos = {0.0f, 0.0f, 0.0f}; + int32_t norm_index = -1; sf::Color color = sf::Color::White; }; @@ -61,6 +62,7 @@ private: struct Mesh { std::vector vertices; + std::vector normales; std::vector parts; inline constexpr size_t GetVertexStride() const noexcept { return sizeof(Vertex); } diff --git a/Engine/World/WorldObject.hpp b/Engine/World/WorldObject.hpp index 1e6b803..9889cf2 100644 --- a/Engine/World/WorldObject.hpp +++ b/Engine/World/WorldObject.hpp @@ -23,6 +23,7 @@ public: const M3D_BoundingBox& GetAABB() const noexcept { return aabb; } + //TODO: recompute AABB when changing these parameters void SetPosition(M3D_F3& _pos) noexcept { pos = _pos; } void SetPosition(float _x, float _y, float _z) noexcept { pos = M3D_F3(_x, _y, _z); } void SetRotation(M3D_F3& _rot) noexcept { rot = _rot; } @@ -34,7 +35,7 @@ public: protected: WorldObject() = default; - M3D_BoundingBox aabb; + inline static M3D_BoundingBox aabb; M3D_F3 scale = M3D_F3(1.0f, 1.0f, 1.0f); M3D_F3 rot = M3D_F3(0.0f, 0.0f, 0.0f); diff --git a/Engine/World/WorldObject.tpp b/Engine/World/WorldObject.tpp index 4e51d80..87b4acf 100644 --- a/Engine/World/WorldObject.tpp +++ b/Engine/World/WorldObject.tpp @@ -30,6 +30,10 @@ void WorldObjectAbstract::LoadMeshFromObjFile(std::string file) { for (size_t i = 0; i < attrib.vertices.size(); i += 3) mMesh.vertices.push_back(Vertex(attrib.vertices[i], attrib.vertices[i+1], attrib.vertices[i+2])); + // Reinterprete normales to our format + for (size_t n = 0; n < attrib.normals.size(); n += 3) + mMesh.normales.push_back(M3D_F3(attrib.normals[n], attrib.normals[n+1], attrib.normals[n+2])); + // Loop over shapes/subparts for (size_t s = 0; s < shapes.size(); s++) { MeshPart subpart; @@ -47,9 +51,12 @@ void WorldObjectAbstract::LoadMeshFromObjFile(std::string file) { // Loop over indices in the face. for (size_t v = 0; v < fv; v++) { - subpart.indices.push_back(shapes[s].mesh.indices[index_offset + v].vertex_index); + tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v]; + subpart.indices.push_back(idx.vertex_index); + // Check if `normal_index` is zero or positive. negative = no normal data - //if (idx.normal_index >= 0) {} + if (idx.normal_index >= 0) + mMesh.vertices[idx.vertex_index].norm_index = idx.normal_index; // Check if `texcoord_index` is zero or positive. negative = no texcoord data //if (idx.texcoord_index >= 0) {}