diff --git a/engine/Screen.cpp b/engine/Screen.cpp index 9b8a308..5390d09 100644 --- a/engine/Screen.cpp +++ b/engine/Screen.cpp @@ -123,9 +123,6 @@ void Screen::drawText(const sf::Text &text) { // OpenGL functions void Screen::glDrawMesh(GLfloat *geometry, GLfloat *view, GLfloat *model, size_t count) { - // OpenGL: - // Make the window the active window for OpenGL calls - _window->setActive(true); glEnable(GL_CULL_FACE); // enable culling face glCullFace(GL_BACK); // cull faces from back @@ -171,9 +168,6 @@ void Screen::glDrawMesh(GLfloat *geometry, GLfloat *view, GLfloat *model, size_t // Draw the mesh glDrawArrays(GL_TRIANGLES, 0, count); - - // Make the window no longer the active window for OpenGL calls - _window->setActive(false); } GLfloat *Screen::glMeshToGLfloatArray(std::shared_ptr mesh, const Vec3D &cameraPosition) { @@ -181,33 +175,36 @@ GLfloat *Screen::glMeshToGLfloatArray(std::shared_ptr mesh, const Vec3D &c auto *geometry = (GLfloat *) malloc(7 * 3 * triangles.size() * sizeof(GLfloat)); + auto model = mesh->model(); + for (size_t i = 0; i < triangles.size(); i++) { int stride = 21 * i; - double dot[3]; - sf::Color ambientColor[3]; - - Triangle MTriangle = triangles[i] * mesh->model(); + Triangle MTriangle = triangles[i] * model; + Vec3D norm = MTriangle.norm(); for (int k = 0; k < 3; k++) { - dot[k] = MTriangle.norm().dot((Vec3D(MTriangle[k]) - cameraPosition).normalized()); + auto& tris = MTriangle[k]; + float dot = norm.dot((Vec3D(tris) - cameraPosition).normalized()); - sf::Color color = triangles[i].color(); - ambientColor[k] = sf::Color(static_cast(color.r * (0.3 * std::abs(dot[k]) + 0.7)), - static_cast(color.g * (0.3 * std::abs(dot[k]) + 0.7)), - static_cast(color.b * (0.3 * std::abs(dot[k]) + 0.7)), - static_cast(color.a)); + sf::Color color = MTriangle.color(); + GLfloat ambientColor[4] = { + color.r * (0.3f * std::fabs(dot) + 0.7f) / 255.0f, + color.g * (0.3f * std::fabs(dot) + 0.7f) / 255.0f, + color.b * (0.3f * std::fabs(dot) + 0.7f) / 255.0f, + color.a / 255.0f + }; - geometry[stride + 7 * k + 0] = static_cast(MTriangle[k].x()); - geometry[stride + 7 * k + 1] = static_cast(MTriangle[k].y()); - geometry[stride + 7 * k + 2] = static_cast(MTriangle[k].z()); + geometry[stride + 7 * k + 0] = static_cast(tris.x()); + geometry[stride + 7 * k + 1] = static_cast(tris.y()); + geometry[stride + 7 * k + 2] = static_cast(tris.z()); - geometry[stride + 7 * k + 3] = static_cast(ambientColor[k].r) / 255.0f; - geometry[stride + 7 * k + 4] = static_cast(ambientColor[k].g) / 255.0f; - geometry[stride + 7 * k + 5] = static_cast(ambientColor[k].b) / 255.0f; - geometry[stride + 7 * k + 6] = static_cast(ambientColor[k].a) / 255.0f; + geometry[stride + 7 * k + 3] = ambientColor[0]; + geometry[stride + 7 * k + 4] = ambientColor[1]; + geometry[stride + 7 * k + 5] = ambientColor[2]; + geometry[stride + 7 * k + 6] = ambientColor[3]; } } return geometry; diff --git a/engine/Triangle.cpp b/engine/Triangle.cpp index 8d25f1a..d488553 100644 --- a/engine/Triangle.cpp +++ b/engine/Triangle.cpp @@ -30,7 +30,7 @@ Vec3D Triangle::norm() const { } } -Vec4D Triangle::operator[](int i) const { +const Vec4D& Triangle::operator[](int i) const { return _points[i]; } diff --git a/engine/Triangle.h b/engine/Triangle.h index 733ba4f..836c1c6 100644 --- a/engine/Triangle.h +++ b/engine/Triangle.h @@ -24,7 +24,7 @@ public: Triangle &operator=(const Triangle &) = default; - [[nodiscard]] Vec4D operator[](int i) const; + [[nodiscard]] const Vec4D& operator[](int i) const; [[nodiscard]] Vec3D norm() const; diff --git a/engine/physics/HitBox.cpp b/engine/physics/HitBox.cpp index 07b3f48..43f2f5c 100644 --- a/engine/physics/HitBox.cpp +++ b/engine/physics/HitBox.cpp @@ -2,6 +2,9 @@ // Created by Иван Ильин on 04.11.2021. // +#include +#include + #include "HitBox.h" #include "../Consts.h" @@ -16,16 +19,12 @@ HitBox::HitBox(const Mesh &mesh) { _hitBox.shrink_to_fit(); } -void HitBox::_addIfUnique(const Vec3D &point) { +void HitBox::_addIfUnique(Vec3D &&point) { bool addPoint = true; - for(const auto& p : _hitBox) { - if((p - point).sqrAbs() < Consts::EPS) { - addPoint = false; - } - } - if(addPoint) { + auto check = [&point](const auto& p) { return (p - point).sqrAbs() < Consts::EPS; }; + + if (std::find_if(std::execution::par, _hitBox.rbegin(), _hitBox.rend(), check) == _hitBox.rend()) _hitBox.push_back(point); - } } HitBox HitBox::Box(const Mesh &mesh) { diff --git a/engine/physics/HitBox.h b/engine/physics/HitBox.h index 3afe05e..5af2725 100644 --- a/engine/physics/HitBox.h +++ b/engine/physics/HitBox.h @@ -11,7 +11,7 @@ class HitBox final { private: std::vector _hitBox; - void _addIfUnique(const Vec3D &point); + void _addIfUnique(Vec3D &&point); public: HitBox() = default; diff --git a/engine/physics/RigidBody.cpp b/engine/physics/RigidBody.cpp index 45b0845..070f8b4 100644 --- a/engine/physics/RigidBody.cpp +++ b/engine/physics/RigidBody.cpp @@ -37,12 +37,11 @@ Vec3D RigidBody::_findFurthestPoint(const Vec3D &direction) { */ for(auto & it : _hitBox) { - auto point = Vec3D(it); - double distance = point.dot(transformedDirection); + double distance = it.dot(transformedDirection); if (distance > maxDistance) { maxDistance = distance; - maxPoint = point; + maxPoint = it; } }