From 556ca037ecaa5a0d13b3733ceee5257390d2e53d Mon Sep 17 00:00:00 2001 From: Vectozavr <60608292+vectozavr@users.noreply.github.com> Date: Sat, 6 Nov 2021 03:55:42 +0700 Subject: [PATCH] optimizations with converting mesh to GLFloatArray --- engine/Engine.cpp | 3 +-- engine/Mesh.cpp | 39 +++++++++++++++++++++++++++++++++++++++ engine/Mesh.h | 5 +++++ engine/Screen.cpp | 34 ---------------------------------- engine/Screen.h | 2 -- 5 files changed, 45 insertions(+), 38 deletions(-) diff --git a/engine/Engine.cpp b/engine/Engine.cpp index d7ed69e..52de232 100644 --- a/engine/Engine.cpp +++ b/engine/Engine.cpp @@ -61,9 +61,8 @@ void Engine::create(int screenWidth, int screenHeight, const std::string &name, for (auto &it : *world) { if (it.second->isVisible()) { GLfloat *model = it.second->glModel(); - GLfloat *geometry = Screen::glMeshToGLfloatArray(it.second); + GLfloat *geometry = it.second->glFloatArray(); screen->glDrawMesh(geometry, view, model, 3 * it.second->triangles().size()); - delete[] geometry; delete[] model; } } diff --git a/engine/Mesh.cpp b/engine/Mesh.cpp index 43e8f5e..e191c52 100644 --- a/engine/Mesh.cpp +++ b/engine/Mesh.cpp @@ -6,6 +6,7 @@ #include "Mesh.h" #include "ResourceManager.h" +#include "Screen.h" using namespace std; @@ -107,4 +108,42 @@ void Mesh::setTriangles(vector&& t) { Mesh::~Mesh() { _tris.clear(); + delete[] _geometry; +} + +GLfloat *Mesh::glFloatArray() const { + if(_geometry != nullptr) { + return _geometry; + } + _geometry = new GLfloat[7 * 3 * _tris.size()]; + + for (size_t i = 0; i < _tris.size(); i++) { + + unsigned stride = 21 * i; + + Triangle triangle = _tris[i]; + Vec3D norm = (model()*triangle.norm()).normalized(); + float dot = static_cast(norm.dot(Vec3D(0, 1, 2).normalized())); + + for (int k = 0; k < 3; k++) { + sf::Color color = triangle.color(); + GLfloat ambientColor[4] = { + static_cast(color.r) * (0.3f * std::fabs(dot) + 0.7f) / 255.0f, + static_cast(color.g) * (0.3f * std::fabs(dot) + 0.7f) / 255.0f, + static_cast(color.b) * (0.3f * std::fabs(dot) + 0.7f) / 255.0f, + static_cast(color.a) / 255.0f + }; + + _geometry[stride + 7 * k + 0] = static_cast(triangle[k].x()); + _geometry[stride + 7 * k + 1] = static_cast(triangle[k].y()); + _geometry[stride + 7 * k + 2] = static_cast(triangle[k].z()); + + _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/Mesh.h b/engine/Mesh.h index e0d3c89..79a3184 100644 --- a/engine/Mesh.h +++ b/engine/Mesh.h @@ -21,6 +21,8 @@ private: Mesh &operator*=(const Matrix4x4 &matrix4X4); + // OpenGL + mutable GLfloat* _geometry = nullptr; public: explicit Mesh(ObjectNameTag nameTag) : Object(std::move(nameTag)) {}; @@ -56,6 +58,9 @@ public: Mesh static LineTo(ObjectNameTag nameTag, const Vec3D &from, const Vec3D &to, double line_width = 0.1, const sf::Color &color = {150, 150, 150, 100}); + + // OpenGL functions + GLfloat *glFloatArray() const; }; #endif //INC_3DZAVR_MESH_H diff --git a/engine/Screen.cpp b/engine/Screen.cpp index 9edd494..b0818b2 100644 --- a/engine/Screen.cpp +++ b/engine/Screen.cpp @@ -166,37 +166,3 @@ void Screen::glDrawMesh(GLfloat *geometry, GLfloat *view, GLfloat *model, size_t sf::Shader::bind(NULL); } - -GLfloat *Screen::glMeshToGLfloatArray(std::shared_ptr mesh) { - std::vector const &triangles = mesh->triangles(); - - auto *geometry = new GLfloat[7 * 3 * triangles.size()]; - - for (size_t i = 0; i < triangles.size(); i++) { - - int stride = 21 * i; - - Triangle triangle = triangles[i]; - float dot = static_cast(triangle.norm().dot(Vec3D(0, 0.5, 1))); - - for (int k = 0; k < 3; k++) { - sf::Color color = triangle.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(triangle[k].x()); - geometry[stride + 7 * k + 1] = static_cast(triangle[k].y()); - geometry[stride + 7 * k + 2] = static_cast(triangle[k].z()); - - 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/Screen.h b/engine/Screen.h index dbda99b..6bfd820 100644 --- a/engine/Screen.h +++ b/engine/Screen.h @@ -62,8 +62,6 @@ public: // OpenGL functions void glDrawMesh(GLfloat *geometry, GLfloat *view, GLfloat *model, size_t count); - static GLfloat *glMeshToGLfloatArray(std::shared_ptr mesh); - [[nodiscard]] std::shared_ptr renderWindow() { return _window; } void pushGLStates() { _window->pushGLStates(); };