From 6f52386199bec404ac8b11c969d9f1b9744f1b8d Mon Sep 17 00:00:00 2001 From: Meoweg Date: Mon, 23 Nov 2015 13:30:23 +0000 Subject: [PATCH] tmp --- examples/demo/main.cpp | 2 +- src/meshes/mesh.cpp | 19 +++++++++++++++++++ src/meshes/mesh.hpp | 4 ++++ src/models/raw.cpp | 7 ++----- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/examples/demo/main.cpp b/examples/demo/main.cpp index 4e7baeb..c3d0d5a 100644 --- a/examples/demo/main.cpp +++ b/examples/demo/main.cpp @@ -75,7 +75,7 @@ int main() sun.ambient = glm::vec3(0.2, 0.2, 0.2); sun.diffuse = glm::vec3(1.0, 1.0, 1.0); sun.specular = glm::vec3(1.0, 1.0, 1.0); - sun.direction = glm::vec3(0.0, 0.0, -1.0); + sun.direction = glm::vec3(0.0, -1.0, 0.0); camera.projection = glm::perspective(45.0f, (float)640 / (float)480, 0.1f, 100.0f); diff --git a/src/meshes/mesh.cpp b/src/meshes/mesh.cpp index 103a7a0..00b6d99 100644 --- a/src/meshes/mesh.cpp +++ b/src/meshes/mesh.cpp @@ -1,5 +1,7 @@ #include "mesh.hpp" +#include + void Mesh::add(const glm::vec3 &position, const glm::vec3 &normal) { elements.push_back(positions.size()); @@ -7,3 +9,20 @@ void Mesh::add(const glm::vec3 &position, const glm::vec3 &normal) positions.push_back(position); normals.push_back(normal); } + +void Mesh::add3(const std::array &positions, const glm::vec3 &normal) +{ + for (int i = 0; i < 3; ++i) + add(positions[i], normal); +} + +void Mesh::add3_with_auto_normals(const std::array &positions, const glm::vec3 &origin) +{ + const glm::vec3 sample = glm::normalize((positions[1] - positions[0]) * (positions[2] - positions[0])); + // TODO Handle if direction is zero vector (origin is in vertex 0) - use some other vertex + const glm::vec3 direction = glm::normalize(positions[0] - origin); + const float angle = glm::angle(direction, sample); + const glm::vec3 normal = angle <= glm::radians(90.0f) ? sample : -sample; + + add3(positions, normal); +} diff --git a/src/meshes/mesh.hpp b/src/meshes/mesh.hpp index 6dea894..4556a8d 100644 --- a/src/meshes/mesh.hpp +++ b/src/meshes/mesh.hpp @@ -3,6 +3,7 @@ #include "../gl.hpp" +#include #include #include @@ -15,6 +16,9 @@ struct Mesh std::vector elements; void add(const glm::vec3 &position, const glm::vec3 &normal); + + void add3(const std::array &positions, const glm::vec3 &normal); + void add3_with_auto_normals(const std::array &positions, const glm::vec3 &origin = glm::vec3(0.0f, 0.0f, 0.0f)); }; #endif // _MESH_HPP_ diff --git a/src/models/raw.cpp b/src/models/raw.cpp index 072a26c..bcd7781 100644 --- a/src/models/raw.cpp +++ b/src/models/raw.cpp @@ -35,15 +35,12 @@ Raw::Raw(const Adapter &adapter, const std::string &name) while (!feof(file)) { - glm::vec3 v[3]; + std::array v; for (int i = 0; i < 3; ++i) fscanf(file, "%f %f %f", &v[i].x, &v[i].y, &v[i].z); - glm::vec3 normal = (v[0] - v[1]) * (v[2] - v[1]); - - for (int i = 0; i < 3; ++i) - add(v[i], normal); + add3_with_auto_normals(v, glm::vec3(0.0f, -10.0f, 0.0f)); } fclose(file);