small optimizations with Triangle.cpp (normal vector calculated once in constructor)
parent
6bf5e1c790
commit
b0559caac3
|
@ -10,7 +10,7 @@ using namespace std;
|
||||||
int main() {
|
int main() {
|
||||||
Shooter game;
|
Shooter game;
|
||||||
|
|
||||||
game.create(1280, 720, ShooterConsts::PROJECT_NAME, true);
|
game.create(1280, 720, ShooterConsts::PROJECT_NAME, false);
|
||||||
//game.create(1920, 1080, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::Fullscreen);
|
//game.create(1920, 1080, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::Fullscreen);
|
||||||
|
|
||||||
//game.create(2048, 1152, ShooterConsts::PROJECT_NAME, false);
|
//game.create(2048, 1152, ShooterConsts::PROJECT_NAME, false);
|
||||||
|
|
|
@ -57,7 +57,7 @@ void Engine::create(int screenWidth, int screenHeight, const std::string &name,
|
||||||
|
|
||||||
Time::startTimer("d projections");
|
Time::startTimer("d projections");
|
||||||
if (_useOpenGL) {
|
if (_useOpenGL) {
|
||||||
GLfloat *view = camera->glView();
|
GLfloat *view = camera->glInvModel();
|
||||||
for (auto &it : *world) {
|
for (auto &it : *world) {
|
||||||
if (it.second->isVisible()) {
|
if (it.second->isVisible()) {
|
||||||
GLfloat *model = it.second->glModel();
|
GLfloat *model = it.second->glModel();
|
||||||
|
|
|
@ -139,7 +139,7 @@ void Object::unattach(const ObjectNameTag &tag) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// OpenGL function
|
// OpenGL function
|
||||||
GLfloat *Object::glView() const {
|
GLfloat *Object::glInvModel() const {
|
||||||
auto *v = new GLfloat[4 * 4];
|
auto *v = new GLfloat[4 * 4];
|
||||||
|
|
||||||
v[0] = -static_cast<GLfloat>(left().x());
|
v[0] = -static_cast<GLfloat>(left().x());
|
||||||
|
|
|
@ -105,12 +105,12 @@ public:
|
||||||
|
|
||||||
[[nodiscard]] Matrix4x4 model() const { return Matrix4x4::Translation(_position) * _transformMatrix; }
|
[[nodiscard]] Matrix4x4 model() const { return Matrix4x4::Translation(_position) * _transformMatrix; }
|
||||||
|
|
||||||
[[nodiscard]] Matrix4x4 view() const { return Matrix4x4::View(left(), up(), lookAt(), position()); }
|
[[nodiscard]] Matrix4x4 invModel() const { return Matrix4x4::View(left(), up(), lookAt(), position()); }
|
||||||
|
|
||||||
// OpenGL function
|
// OpenGL function
|
||||||
[[nodiscard]] GLfloat *glModel() const;
|
[[nodiscard]] GLfloat *glModel() const;
|
||||||
|
|
||||||
[[nodiscard]] GLfloat *glView() const;
|
[[nodiscard]] GLfloat *glInvModel() const;
|
||||||
|
|
||||||
virtual ~Object();
|
virtual ~Object();
|
||||||
};
|
};
|
||||||
|
|
|
@ -169,6 +169,7 @@ std::vector<std::shared_ptr<Mesh>> ResourceManager::loadObjects(const std::strin
|
||||||
return objects;
|
return objects;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// If objects is already loaded - return pointer to it
|
// If objects is already loaded - return pointer to it
|
||||||
auto it = _instance->_objects.find(filename);
|
auto it = _instance->_objects.find(filename);
|
||||||
if (it != _instance->_objects.end()) {
|
if (it != _instance->_objects.end()) {
|
||||||
|
|
|
@ -177,10 +177,10 @@ GLfloat *Screen::glMeshToGLfloatArray(std::shared_ptr<Mesh> mesh) {
|
||||||
int stride = 21 * i;
|
int stride = 21 * i;
|
||||||
|
|
||||||
Triangle triangle = triangles[i];
|
Triangle triangle = triangles[i];
|
||||||
|
Vec3D norm = triangles[i].norm();
|
||||||
|
float dot = norm.dot(Vec3D(2, 1, 0).normalized());
|
||||||
|
|
||||||
for (int k = 0; k < 3; k++) {
|
for (int k = 0; k < 3; k++) {
|
||||||
float dot = 0.5;
|
|
||||||
|
|
||||||
sf::Color color = triangle.color();
|
sf::Color color = triangle.color();
|
||||||
GLfloat ambientColor[4] = {
|
GLfloat ambientColor[4] = {
|
||||||
color.r * (0.3f * std::fabs(dot) + 0.7f) / 255.0f,
|
color.r * (0.3f * std::fabs(dot) + 0.7f) / 255.0f,
|
||||||
|
|
|
@ -7,10 +7,23 @@
|
||||||
|
|
||||||
Triangle::Triangle(const Vec4D &p1, const Vec4D &p2, const Vec4D &p3, sf::Color color) : _color(color),
|
Triangle::Triangle(const Vec4D &p1, const Vec4D &p2, const Vec4D &p3, sf::Color color) : _color(color),
|
||||||
_points{p1, p2, p3} {
|
_points{p1, p2, p3} {
|
||||||
|
calculateNormal();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Triangle::calculateNormal() {
|
||||||
|
Vec3D v1 = Vec3D(_points[1] - _points[0]);
|
||||||
|
Vec3D v2 = Vec3D(_points[2] - _points[0]);
|
||||||
|
Vec3D crossProduct = v1.cross(v2);
|
||||||
|
|
||||||
|
if (crossProduct.sqrAbs() > Consts::EPS) {
|
||||||
|
_normal = crossProduct.normalized();
|
||||||
|
} else {
|
||||||
|
_normal = Vec3D(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Triangle::Triangle(const Triangle &triangle) : _points{triangle._points[0], triangle._points[1], triangle._points[2]},
|
Triangle::Triangle(const Triangle &triangle) : _points{triangle._points[0], triangle._points[1], triangle._points[2]},
|
||||||
_color(triangle._color) {
|
_color(triangle._color), _normal(triangle._normal) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Triangle Triangle::operator*(const Matrix4x4 &matrix4X4) const {
|
Triangle Triangle::operator*(const Matrix4x4 &matrix4X4) const {
|
||||||
|
@ -18,16 +31,7 @@ Triangle Triangle::operator*(const Matrix4x4 &matrix4X4) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
Vec3D Triangle::norm() const {
|
Vec3D Triangle::norm() const {
|
||||||
|
return _normal;
|
||||||
Vec3D v1 = Vec3D(_points[1] - _points[0]);
|
|
||||||
Vec3D v2 = Vec3D(_points[2] - _points[0]);
|
|
||||||
Vec3D crossProduct = v1.cross(v2);
|
|
||||||
|
|
||||||
if (crossProduct.sqrAbs() > Consts::EPS) {
|
|
||||||
return crossProduct.normalized();
|
|
||||||
} else {
|
|
||||||
return Vec3D(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const Vec4D& Triangle::operator[](int i) const {
|
const Vec4D& Triangle::operator[](int i) const {
|
||||||
|
|
|
@ -15,8 +15,11 @@ class Triangle final {
|
||||||
private:
|
private:
|
||||||
sf::Color _color;
|
sf::Color _color;
|
||||||
Vec4D _points[3];
|
Vec4D _points[3];
|
||||||
|
Vec3D _normal;
|
||||||
|
|
||||||
|
void calculateNormal();
|
||||||
public:
|
public:
|
||||||
Triangle() : _points{Vec4D{}, Vec4D{}, Vec4D{}} {};
|
Triangle() = default;
|
||||||
|
|
||||||
Triangle(const Triangle &triangle);
|
Triangle(const Triangle &triangle);
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ private:
|
||||||
std::shared_ptr<Mouse> _mouse;
|
std::shared_ptr<Mouse> _mouse;
|
||||||
public:
|
public:
|
||||||
explicit Window(std::shared_ptr<Screen> screen, std::shared_ptr<Mouse> mouse, std::string name = "Menu",
|
explicit Window(std::shared_ptr<Screen> screen, std::shared_ptr<Mouse> mouse, std::string name = "Menu",
|
||||||
std::string backTexture = "") : _screen(std::move(screen)), _mouse(std::move(mouse)),
|
std::string backTexture = "") : _screen(screen), _mouse(mouse),
|
||||||
_name(std::move(name)), _backTexture(std::move(backTexture)) {}
|
_name(std::move(name)), _backTexture(std::move(backTexture)) {}
|
||||||
|
|
||||||
void addButton(int x, int y, int w, int h,
|
void addButton(int x, int y, int w, int h,
|
||||||
|
|
|
@ -24,7 +24,7 @@ Vec3D RigidBody::_findFurthestPoint(const Vec3D &direction) {
|
||||||
|
|
||||||
double maxDistance = -std::numeric_limits<double>::max();
|
double maxDistance = -std::numeric_limits<double>::max();
|
||||||
|
|
||||||
Vec3D transformedDirection = (view() * direction).normalized();
|
Vec3D transformedDirection = (invModel() * direction).normalized();
|
||||||
|
|
||||||
for(auto & it : _hitBox) {
|
for(auto & it : _hitBox) {
|
||||||
double distance = it.dot(transformedDirection);
|
double distance = it.dot(transformedDirection);
|
||||||
|
|
Loading…
Reference in New Issue