optimizations with converting mesh to GLFloatArray
parent
389936268a
commit
556ca037ec
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
#include "Mesh.h"
|
||||
#include "ResourceManager.h"
|
||||
#include "Screen.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
@ -107,4 +108,42 @@ void Mesh::setTriangles(vector<Triangle>&& 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<float>(norm.dot(Vec3D(0, 1, 2).normalized()));
|
||||
|
||||
for (int k = 0; k < 3; k++) {
|
||||
sf::Color color = triangle.color();
|
||||
GLfloat ambientColor[4] = {
|
||||
static_cast<float>(color.r) * (0.3f * std::fabs(dot) + 0.7f) / 255.0f,
|
||||
static_cast<float>(color.g) * (0.3f * std::fabs(dot) + 0.7f) / 255.0f,
|
||||
static_cast<float>(color.b) * (0.3f * std::fabs(dot) + 0.7f) / 255.0f,
|
||||
static_cast<float>(color.a) / 255.0f
|
||||
};
|
||||
|
||||
_geometry[stride + 7 * k + 0] = static_cast<GLfloat>(triangle[k].x());
|
||||
_geometry[stride + 7 * k + 1] = static_cast<GLfloat>(triangle[k].y());
|
||||
_geometry[stride + 7 * k + 2] = static_cast<GLfloat>(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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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> mesh) {
|
||||
std::vector<Triangle> 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<float>(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<GLfloat>(triangle[k].x());
|
||||
geometry[stride + 7 * k + 1] = static_cast<GLfloat>(triangle[k].y());
|
||||
geometry[stride + 7 * k + 2] = static_cast<GLfloat>(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;
|
||||
}
|
||||
|
|
|
@ -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> mesh);
|
||||
|
||||
[[nodiscard]] std::shared_ptr<sf::RenderWindow> renderWindow() { return _window; }
|
||||
|
||||
void pushGLStates() { _window->pushGLStates(); };
|
||||
|
|
Loading…
Reference in New Issue