Hitbox creation & render optimization

master
Neirokan 2021-11-04 04:30:58 +03:00
parent 3ce6345d21
commit a39fbaff47
6 changed files with 32 additions and 37 deletions

View File

@ -123,9 +123,6 @@ void Screen::drawText(const sf::Text &text) {
// OpenGL functions // OpenGL functions
void Screen::glDrawMesh(GLfloat *geometry, GLfloat *view, GLfloat *model, size_t count) { 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 glEnable(GL_CULL_FACE); // enable culling face
glCullFace(GL_BACK); // cull faces from back 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 // Draw the mesh
glDrawArrays(GL_TRIANGLES, 0, count); 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> mesh, const Vec3D &cameraPosition) { GLfloat *Screen::glMeshToGLfloatArray(std::shared_ptr<Mesh> mesh, const Vec3D &cameraPosition) {
@ -181,33 +175,36 @@ GLfloat *Screen::glMeshToGLfloatArray(std::shared_ptr<Mesh> mesh, const Vec3D &c
auto *geometry = (GLfloat *) malloc(7 * 3 * triangles.size() * sizeof(GLfloat)); auto *geometry = (GLfloat *) malloc(7 * 3 * triangles.size() * sizeof(GLfloat));
auto model = mesh->model();
for (size_t i = 0; i < triangles.size(); i++) { for (size_t i = 0; i < triangles.size(); i++) {
int stride = 21 * i; int stride = 21 * i;
double dot[3]; Triangle MTriangle = triangles[i] * model;
sf::Color ambientColor[3]; Vec3D norm = MTriangle.norm();
Triangle MTriangle = triangles[i] * mesh->model();
for (int k = 0; k < 3; k++) { 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(); sf::Color color = MTriangle.color();
ambientColor[k] = sf::Color(static_cast<sf::Uint8>(color.r * (0.3 * std::abs(dot[k]) + 0.7)), GLfloat ambientColor[4] = {
static_cast<sf::Uint8>(color.g * (0.3 * std::abs(dot[k]) + 0.7)), color.r * (0.3f * std::fabs(dot) + 0.7f) / 255.0f,
static_cast<sf::Uint8>(color.b * (0.3 * std::abs(dot[k]) + 0.7)), color.g * (0.3f * std::fabs(dot) + 0.7f) / 255.0f,
static_cast<sf::Uint8>(color.a)); color.b * (0.3f * std::fabs(dot) + 0.7f) / 255.0f,
color.a / 255.0f
};
geometry[stride + 7 * k + 0] = static_cast<GLfloat>(MTriangle[k].x()); geometry[stride + 7 * k + 0] = static_cast<GLfloat>(tris.x());
geometry[stride + 7 * k + 1] = static_cast<GLfloat>(MTriangle[k].y()); geometry[stride + 7 * k + 1] = static_cast<GLfloat>(tris.y());
geometry[stride + 7 * k + 2] = static_cast<GLfloat>(MTriangle[k].z()); geometry[stride + 7 * k + 2] = static_cast<GLfloat>(tris.z());
geometry[stride + 7 * k + 3] = static_cast<GLfloat>(ambientColor[k].r) / 255.0f; geometry[stride + 7 * k + 3] = ambientColor[0];
geometry[stride + 7 * k + 4] = static_cast<GLfloat>(ambientColor[k].g) / 255.0f; geometry[stride + 7 * k + 4] = ambientColor[1];
geometry[stride + 7 * k + 5] = static_cast<GLfloat>(ambientColor[k].b) / 255.0f; geometry[stride + 7 * k + 5] = ambientColor[2];
geometry[stride + 7 * k + 6] = static_cast<GLfloat>(ambientColor[k].a) / 255.0f; geometry[stride + 7 * k + 6] = ambientColor[3];
} }
} }
return geometry; return geometry;

View File

@ -30,7 +30,7 @@ Vec3D Triangle::norm() const {
} }
} }
Vec4D Triangle::operator[](int i) const { const Vec4D& Triangle::operator[](int i) const {
return _points[i]; return _points[i];
} }

View File

@ -24,7 +24,7 @@ public:
Triangle &operator=(const Triangle &) = default; Triangle &operator=(const Triangle &) = default;
[[nodiscard]] Vec4D operator[](int i) const; [[nodiscard]] const Vec4D& operator[](int i) const;
[[nodiscard]] Vec3D norm() const; [[nodiscard]] Vec3D norm() const;

View File

@ -2,6 +2,9 @@
// Created by Иван Ильин on 04.11.2021. // Created by Иван Ильин on 04.11.2021.
// //
#include <algorithm>
#include <execution>
#include "HitBox.h" #include "HitBox.h"
#include "../Consts.h" #include "../Consts.h"
@ -16,17 +19,13 @@ HitBox::HitBox(const Mesh &mesh) {
_hitBox.shrink_to_fit(); _hitBox.shrink_to_fit();
} }
void HitBox::_addIfUnique(const Vec3D &point) { void HitBox::_addIfUnique(Vec3D &&point) {
bool addPoint = true; bool addPoint = true;
for(const auto& p : _hitBox) { auto check = [&point](const auto& p) { return (p - point).sqrAbs() < Consts::EPS; };
if((p - point).sqrAbs() < Consts::EPS) {
addPoint = false; if (std::find_if(std::execution::par, _hitBox.rbegin(), _hitBox.rend(), check) == _hitBox.rend())
}
}
if(addPoint) {
_hitBox.push_back(point); _hitBox.push_back(point);
} }
}
HitBox HitBox::Box(const Mesh &mesh) { HitBox HitBox::Box(const Mesh &mesh) {
HitBox result; HitBox result;

View File

@ -11,7 +11,7 @@ class HitBox final {
private: private:
std::vector<Vec3D> _hitBox; std::vector<Vec3D> _hitBox;
void _addIfUnique(const Vec3D &point); void _addIfUnique(Vec3D &&point);
public: public:
HitBox() = default; HitBox() = default;

View File

@ -37,12 +37,11 @@ Vec3D RigidBody::_findFurthestPoint(const Vec3D &direction) {
*/ */
for(auto & it : _hitBox) { for(auto & it : _hitBox) {
auto point = Vec3D(it); double distance = it.dot(transformedDirection);
double distance = point.dot(transformedDirection);
if (distance > maxDistance) { if (distance > maxDistance) {
maxDistance = distance; maxDistance = distance;
maxPoint = point; maxPoint = it;
} }
} }