diff --git a/3dzavr b/3dzavr index c93a712..f80ebaf 160000 --- a/3dzavr +++ b/3dzavr @@ -1 +1 @@ -Subproject commit c93a7123c1e76008ae7819041cdc7d51461e07c2 +Subproject commit f80ebaf4522310a7560c19e6da2a3a8f36f2eb16 diff --git a/CMakeLists.txt b/CMakeLists.txt index c0e34d1..3a018a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,87 +29,87 @@ add_executable(${CMAKE_PROJECT_NAME} network/ShooterMsgType.h network/ShooterMsgType.cpp # 3d engine: - engine/Consts.h - engine/math/Vec4D.h - engine/math/Vec4D.cpp - engine/math/Vec3D.cpp - engine/math/Vec3D.h - engine/math/Vec2D.cpp - engine/math/Vec2D.h - engine/math/Matrix4x4.h - engine/math/Matrix4x4.cpp - engine/Triangle.h - engine/Triangle.cpp - engine/math/Plane.h - engine/math/Plane.cpp - engine/Mesh.h - engine/Mesh.cpp - engine/utils/Log.h - engine/utils/Log.cpp - engine/utils/Time.h - engine/utils/Time.cpp - engine/utils/Timer.cpp - engine/utils/Timer.h - engine/utils/ResourceManager.h - engine/utils/ResourceManager.cpp - engine/World.h - engine/World.cpp - engine/Camera.h - engine/Camera.cpp - engine/io/Screen.h - engine/io/Screen.cpp - engine/Engine.h - engine/Engine.cpp - engine/io/Keyboard.cpp - engine/io/Keyboard.h - engine/io/Mouse.cpp - engine/io/Mouse.h - engine/io/SoundController.cpp - engine/io/SoundController.h - engine/utils/ObjectController.cpp - engine/utils/ObjectController.h - engine/animation/Animation.h - engine/animation/Timeline.cpp - engine/animation/Timeline.h - engine/animation/Interpolation.h - engine/animation/Animation.cpp - engine/animation/ATranslate.h - engine/animation/AScale.h - engine/animation/ARotate.h - engine/animation/AWait.h - engine/animation/AFunction.h - engine/animation/AAttractToPoint.h - engine/animation/ARotateRelativePoint.h - engine/animation/ARotateLeft.h - engine/animation/Interpolation.cpp - engine/animation/Animations.h - engine/animation/AShowCreation.h - engine/animation/AShowUncreation.h - engine/animation/ARotateLeftUpLookAt.h - engine/animation/ADecompose.h - engine/physics/RigidBody.cpp - engine/physics/RigidBody.h - engine/physics/Simplex.h - engine/physics/HitBox.cpp - engine/physics/HitBox.h - engine/Object.cpp - engine/Object.h - engine/gui/Button.cpp - engine/gui/Button.h - engine/gui/Window.cpp - engine/gui/Window.h - engine/network/ClientUDP.cpp - engine/network/ClientUDP.h - engine/network/MsgType.cpp - engine/network/MsgType.h - engine/network/ReliableMsg.cpp - engine/network/ReliableMsg.h - engine/network/ServerUDP.cpp - engine/network/ServerUDP.h - engine/network/UDPConnection.cpp - engine/network/UDPConnection.h - engine/network/UDPSocket.cpp - engine/network/UDPSocket.h + 3dzavr/engine/Consts.h + 3dzavr/engine/math/Vec4D.h + 3dzavr/engine/math/Vec4D.cpp + 3dzavr/engine/math/Vec3D.cpp + 3dzavr/engine/math/Vec3D.h + 3dzavr/engine/math/Vec2D.cpp + 3dzavr/engine/math/Vec2D.h + 3dzavr/engine/math/Matrix4x4.h + 3dzavr/engine/math/Matrix4x4.cpp + 3dzavr/engine/Triangle.h + 3dzavr/engine/Triangle.cpp + 3dzavr/engine/math/Plane.h + 3dzavr/engine/math/Plane.cpp + 3dzavr/engine/Mesh.h + 3dzavr/engine/Mesh.cpp + 3dzavr/engine/utils/Log.h + 3dzavr/engine/utils/Log.cpp + 3dzavr/engine/utils/Time.h + 3dzavr/engine/utils/Time.cpp + 3dzavr/engine/utils/Timer.cpp + 3dzavr/engine/utils/Timer.h + 3dzavr/engine/utils/ResourceManager.h + 3dzavr/engine/utils/ResourceManager.cpp + 3dzavr/engine/World.h + 3dzavr/engine/World.cpp + 3dzavr/engine/Camera.h + 3dzavr/engine/Camera.cpp + 3dzavr/engine/io/Screen.h + 3dzavr/engine/io/Screen.cpp + 3dzavr/engine/Engine.h + 3dzavr/engine/Engine.cpp + 3dzavr/engine/io/Keyboard.cpp + 3dzavr/engine/io/Keyboard.h + 3dzavr/engine/io/Mouse.cpp + 3dzavr/engine/io/Mouse.h + 3dzavr/engine/io/SoundController.cpp + 3dzavr/engine/io/SoundController.h + 3dzavr/engine/utils/ObjectController.cpp + 3dzavr/engine/utils/ObjectController.h + 3dzavr/engine/animation/Animation.h + 3dzavr/engine/animation/Timeline.cpp + 3dzavr/engine/animation/Timeline.h + 3dzavr/engine/animation/Interpolation.h + 3dzavr/engine/animation/Animation.cpp + 3dzavr/engine/animation/ATranslate.h + 3dzavr/engine/animation/AScale.h + 3dzavr/engine/animation/ARotate.h + 3dzavr/engine/animation/AWait.h + 3dzavr/engine/animation/AFunction.h + 3dzavr/engine/animation/AAttractToPoint.h + 3dzavr/engine/animation/ARotateRelativePoint.h + 3dzavr/engine/animation/ARotateLeft.h + 3dzavr/engine/animation/Interpolation.cpp + 3dzavr/engine/animation/Animations.h + 3dzavr/engine/animation/AShowCreation.h + 3dzavr/engine/animation/AShowUncreation.h + 3dzavr/engine/animation/ARotateLeftUpLookAt.h + 3dzavr/engine/animation/ADecompose.h + 3dzavr/engine/physics/RigidBody.cpp + 3dzavr/engine/physics/RigidBody.h + 3dzavr/engine/physics/Simplex.h + 3dzavr/engine/physics/HitBox.cpp + 3dzavr/engine/physics/HitBox.h + 3dzavr/engine/Object.cpp + 3dzavr/engine/Object.h + 3dzavr/engine/gui/Button.cpp + 3dzavr/engine/gui/Button.h + 3dzavr/engine/gui/Window.cpp + 3dzavr/engine/gui/Window.h + 3dzavr/engine/network/ClientUDP.cpp + 3dzavr/engine/network/ClientUDP.h + 3dzavr/engine/network/MsgType.cpp + 3dzavr/engine/network/MsgType.h + 3dzavr/engine/network/ReliableMsg.cpp + 3dzavr/engine/network/ReliableMsg.h + 3dzavr/engine/network/ServerUDP.cpp + 3dzavr/engine/network/ServerUDP.h + 3dzavr/engine/network/UDPConnection.cpp + 3dzavr/engine/network/UDPConnection.h + 3dzavr/engine/network/UDPSocket.cpp + 3dzavr/engine/network/UDPSocket.h ) if(APPLE OR UNIX) diff --git a/Shooter.cpp b/Shooter.cpp index a16b13e..a519e35 100644 --- a/Shooter.cpp +++ b/Shooter.cpp @@ -5,9 +5,9 @@ #include "Shooter.h" #include #include -#include "engine/animation/Animations.h" +#include "3dzavr/engine/animation/Animations.h" #include "ShooterConsts.h" -#include "engine/io/SoundController.h" +#include "3dzavr/engine/io/SoundController.h" using namespace std; diff --git a/Shooter.h b/Shooter.h index a145ee4..9956912 100644 --- a/Shooter.h +++ b/Shooter.h @@ -5,11 +5,11 @@ #ifndef SHOOTER_SHOOTER_H #define SHOOTER_SHOOTER_H -#include "engine/Engine.h" +#include "3dzavr/engine/Engine.h" #include "player/Player.h" #include "player/PlayerController.h" #include "player/PlayerController.h" -#include "engine/gui/Window.h" +#include "3dzavr/engine/gui/Window.h" #include "network/ShooterClient.h" #include "network/ShooterServer.h" diff --git a/engine/Camera.cpp b/engine/Camera.cpp deleted file mode 100644 index 7372b68..0000000 --- a/engine/Camera.cpp +++ /dev/null @@ -1,130 +0,0 @@ -// -// Created by Иван Ильин on 14.01.2021. -// - -#include - -#include "Camera.h" -#include "utils/Log.h" -#include "Consts.h" - -std::vector> Camera::project(std::shared_ptr mesh) { - - if (!_ready) { - Log::log("Camera::project(): cannot project _tris without camera initialization ( Camera::init() ) "); - return _triangles; - } - - if (!mesh->isVisible()) { - return this->_triangles; - } - - // Model transform matrix: translate _tris in the origin of body. - Matrix4x4 M = mesh->model(); - Matrix4x4 V = invModel(); - - // We don't want to waste time re-allocating memory every time - std::vector clippedTriangles, tempBuffer; - - for (auto &t : mesh->triangles()) { - - Triangle MTriangle = t * M; - - double dot = MTriangle.norm().dot((Vec3D(MTriangle[0]) - position()).normalized()); - - if (dot > 0) { - continue; - } - - Triangle VMTriangle = MTriangle * V; - - // It needs to be cleared because it's reused through iterations. Usually it doesn't free memory. - clippedTriangles.clear(); - tempBuffer.clear(); - - // In the beginning we need to to translate triangle from world coordinate to our camera system: - // After that we apply clipping for all planes from _clipPlanes - clippedTriangles.emplace_back(VMTriangle); - for (auto &plane : _clipPlanes) { - while (!clippedTriangles.empty()) { - std::vector clipResult = plane.clip(clippedTriangles.back()); - clippedTriangles.pop_back(); - for (auto &i : clipResult) { - tempBuffer.emplace_back(i); - } - } - clippedTriangles.swap(tempBuffer); - } - - for (auto &clipped : clippedTriangles) { - sf::Color color = clipped.color(); - sf::Color ambientColor = sf::Color(static_cast(color.r * (0.3 * std::abs(dot) + 0.7)), - static_cast(color.g * (0.3 * std::abs(dot) + 0.7)), - static_cast(color.b * (0.3 * std::abs(dot) + 0.7)), - static_cast(color.a)); - - // Finally its time to project our clipped colored drawTriangle from 3D -> 2D - // and transform it's coordinate to screen space (in pixels): - Triangle clippedProjected = clipped * _SP; - - Triangle clippedProjectedNormalized = Triangle(clippedProjected[0] / clippedProjected[0].w(), - clippedProjected[1] / clippedProjected[1].w(), - clippedProjected[2] / clippedProjected[2].w(), - ambientColor); - - _triangles.emplace_back(std::make_shared(clippedProjectedNormalized)); - } - } - - return this->_triangles; -} - -void Camera::init(int width, int height, double fov, double ZNear, double ZFar) { - // We need to init camera only after creation or changing width, height, fov, ZNear or ZFar. - // Because here we calculate matrix that does not change during the motion of _objects or camera - _aspect = (double) width / (double) height; - Matrix4x4 P = Matrix4x4::Projection(fov, _aspect, ZNear, ZFar); - Matrix4x4 S = Matrix4x4::ScreenSpace(width, height); - - _SP = S * P; // screen-space-projections matrix - - // This is planes for clipping _tris. - // Motivation: we are not interest in _tris that we cannot see. - _clipPlanes.emplace_back(Plane(Vec3D{0, 0, 1}, Vec3D{0, 0, ZNear})); // near plane - _clipPlanes.emplace_back(Plane(Vec3D{0, 0, -1}, Vec3D{0, 0, ZFar})); // far plane - - double thetta1 = Consts::PI * fov * 0.5 / 180.0; - double thetta2 = atan(_aspect * tan(thetta1)); - _clipPlanes.emplace_back(Plane(Vec3D{-cos(thetta2), 0, sin(thetta2)}, Vec3D{0, 0, 0})); // left plane - _clipPlanes.emplace_back(Plane(Vec3D{cos(thetta2), 0, sin(thetta2)}, Vec3D{0, 0, 0})); // right plane - _clipPlanes.emplace_back(Plane(Vec3D{0, cos(thetta1), sin(thetta1)}, Vec3D{0, 0, 0})); // down plane - _clipPlanes.emplace_back(Plane(Vec3D{0, -cos(thetta1), sin(thetta1)}, Vec3D{0, 0, 0})); // up plane - - _ready = true; - Log::log("Camera::init(): camera successfully initialized."); -} - -std::vector> Camera::sorted() { - - // Sort _tris from _back to front - // This is some replacement for Z-buffer - std::sort(_triangles.begin(), _triangles.end(), [](std::shared_ptr &t1, std::shared_ptr &t2) { - std::vector v_z1({(*t1)[0].z(), (*t1)[1].z(), (*t1)[2].z()}); - std::vector v_z2({(*t2)[0].z(), (*t2)[1].z(), (*t2)[2].z()}); - - std::sort(v_z1.begin(), v_z1.end()); - std::sort(v_z2.begin(), v_z2.end()); - - double z1 = v_z1[0] + v_z1[1] + v_z1[2]; - double z2 = v_z2[0] + v_z2[1] + v_z2[2]; - - return z1 > z2; - }); - - return _triangles; -} - -void Camera::clear() { - // Cleaning all _tris and recalculation of View matrix - _triangles.clear(); -} diff --git a/engine/Camera.h b/engine/Camera.h deleted file mode 100644 index 4b17394..0000000 --- a/engine/Camera.h +++ /dev/null @@ -1,41 +0,0 @@ -// -// Created by Иван Ильин on 14.01.2021. -// - -#ifndef ENGINE_CAMERA_H -#define ENGINE_CAMERA_H - -#include - -#include - -#include "math/Plane.h" -#include "Mesh.h" - -class Camera final : public Object { -private: - std::vector> _triangles{}; - std::vector _clipPlanes{}; - bool _ready = false; - double _aspect = 0; - - Matrix4x4 _SP; -public: - Camera() : Object(ObjectNameTag("Camera")) {}; - - Camera(const Camera &camera) = delete; - - void init(int width, int height, double fov = 90.0, double ZNear = 0.1, double ZFar = 5000.0); - - std::vector> project(std::shared_ptr mesh); - - void clear(); - - [[nodiscard]] int buffSize() const { return _triangles.size(); } - - std::vector> sorted(); - -}; - - -#endif //INC_3DZAVR_CAMERA_H diff --git a/engine/Consts.h b/engine/Consts.h deleted file mode 100644 index fac92a9..0000000 --- a/engine/Consts.h +++ /dev/null @@ -1,60 +0,0 @@ -// -// Created by Иван Ильин on 07.10.2021. -// - -#ifndef SHOOTER_CONSTS_H -#define SHOOTER_CONSTS_H - -#include - -#include "math/Vec2D.h" - -namespace Consts { - const int STANDARD_SCREEN_WIDTH = 1920; - const int STANDARD_SCREEN_HEIGHT = 1080; - const sf::Color BACKGROUND_COLOR = sf::Color(255, 255, 255); - const std::string PROJECT_NAME = "engine"; - const bool USE_LOG_FILE = true; - const bool USE_OPEN_GL = true; - const bool SHOW_DEBUG_INFO = false; - const bool SHOW_FPS_COUNTER = true; - - const double PI = 3.14159265358979323846264338327950288; - const double EPS = 0.000001; - - const double EPA_EPS = 0.0001; - - const double RAY_CAST_MAX_DISTANCE = 10000; - - const std::string THIN_FONT = "engine/fonts/Roboto-Thin.ttf"; - const std::string MEDIUM_FONT = "engine/fonts/Roboto-Medium.ttf"; - - const double LARGEST_TIME_STEP = 1.0 / 15.0; - const double TAP_DELAY = 0.2; - - const Vec2D BEZIER[2] = {Vec2D{0.8, 0}, Vec2D{0.2, 1}}; - - const unsigned NETWORK_VERSION = 3U; - const int NETWORK_TIMEOUT = 5U; - const int NETWORK_WORLD_UPDATE_RATE = 30; - const double NETWORK_RELIABLE_RETRY_TIME = 1.0 / 20; - const uint16_t NETWORK_MAX_CLIENTS = 64; - - const sf::Color WHITE_COLORS[] = { - sf::Color(137, 135, 222), // blue - sf::Color(195, 155, 209), // pink - sf::Color(201, 137, 137), // red - sf::Color(116, 204, 135), // green - sf::Color(201, 171, 137), // orange - }; - - const sf::Color DARK_COLORS[] = { - sf::Color(16, 18, 69), // blue - sf::Color(77, 0, 62), // pink - sf::Color(99, 20, 20), // red - sf::Color(12, 46, 9), // green - sf::Color(97, 70, 51), // orange - }; -} - -#endif //SHOOTER_CONSTS_H diff --git a/engine/Engine.cpp b/engine/Engine.cpp deleted file mode 100644 index dff36f4..0000000 --- a/engine/Engine.cpp +++ /dev/null @@ -1,197 +0,0 @@ -// -// Created by Иван Ильин on 14.01.2021. -// - -#include - -#include "Engine.h" -#include "utils/Time.h" -#include "utils/ResourceManager.h" -#include "animation/Timeline.h" -#include "io/SoundController.h" - -Engine::Engine() { - Time::init(); - Timeline::init(); - ResourceManager::init(); - SoundController::init(); -} - -void Engine::create(int screenWidth, int screenHeight, const std::string &name, bool verticalSync, sf::Color background, - sf::Uint32 style) { - _name = name; - screen->open(screenWidth, screenHeight, name, verticalSync, background, style); - - Log::log("Engine::create(): started engine (" + std::to_string(screenWidth) + "x" + std::to_string(screenHeight) + - ") with title '" + name + "'."); - Time::update(); - - start(); - camera->init(screenWidth, screenHeight); - - while (screen->isOpen()) { - // 'd' in the beginning of the name means debug. - // While printing debug info we will take into account only timer names witch start with 'd ' - Time::startTimer("d all"); - - screen->clear(); - - Time::update(); - - Time::startTimer("d game update"); - update(); - Time::stopTimer("d game update"); - - // sometimes we dont need to update physics world - // (for example in menu or while pause) - // hence we can set '_updateWorld' equal to false in setUpdateWorld(bool): - if (_updateWorld) { - - Time::startTimer("d animations"); - Timeline::update(); - Time::stopTimer("d animations"); - - Time::startTimer("d collisions"); - world->update(); - Time::stopTimer("d collisions"); - - Time::startTimer("d projections"); - if (_useOpenGL) { - GLfloat *view = camera->glInvModel(); - screen->popGLStates(); - screen->prepareToGlDrawMesh(); - for (auto &it : *world) { - if (it.second->isVisible()) { - GLfloat *model = it.second->glModel(); - GLfloat *geometry = it.second->glFloatArray(); - screen->glDrawMesh(geometry, view, model, 3 * it.second->triangles().size()); - delete[] model; - } - } - screen->pushGLStates(); - delete[] view; - } else { - // clear triangles from previous frame - camera->clear(); - // project triangles to the camera plane - for (auto &it : *world) { - camera->project(it.second); - } - // draw triangles on the screen - for (auto &t : camera->sorted()) { - screen->drawTriangle(*t); - } - - _triPerSec = camera->buffSize() * Time::fps(); - } - Time::stopTimer("d projections"); - - if (Consts::SHOW_FPS_COUNTER) { - screen->drawText(std::to_string(Time::fps()) + " fps", - Vec2D(static_cast(screen->width()) - 100.0, 10.0), 25, - sf::Color(100, 100, 100)); - } - printDebugInfo(); - gui(); - } - - screen->display(); - - Time::stopTimer("d all"); - } -} - -void Engine::exit() { - if (screen->isOpen()) { - screen->close(); - } - SoundController::free(); - ResourceManager::free(); - Timeline::free(); - Time::free(); - - Log::log("Engine::exit(): exit engine (" + std::to_string(screen->width()) + "x" + - std::to_string(screen->height()) + ") with title '" + screen->title() + "'."); -} - -void Engine::printDebugInfo() const { - - if (_showDebugInfo) { - // coordinates & fps: - - std::string text = _name + "\n\n X: " + - std::to_string((camera->position().x())) + "\n Y: " + - std::to_string((camera->position().y())) + "\n Z: " + - std::to_string((camera->position().z())) + "\n RY:" + - std::to_string(camera->angle().y()/Consts::PI) + "PI\n RL: " + - std::to_string(camera->angleLeftUpLookAt().x()/Consts::PI) + "PI\n\n" + - std::to_string(screen->width()) + "x" + - std::to_string(screen->height()) + "\t" + - std::to_string(Time::fps()) + " fps"; - - if (_useOpenGL) { - text += "\n Using OpenGL acceleration"; - } else { - text += "\n" + std::to_string(_triPerSec) + " tris/s"; - } - - sf::Text t; - - t.setFont(*ResourceManager::loadFont(Consts::THIN_FONT)); - t.setString(text); - t.setCharacterSize(30); - t.setFillColor(sf::Color::Black); - t.setPosition(static_cast(screen->width()) - 400.0f, 10.0f); - - screen->drawText(t); - - // timers: - int timerWidth = screen->width() - 100; - float xPos = 50; - float yPos = 300; - int height = 50; - - double totalTime = Time::elapsedTimerSeconds("d all"); - double timeSum = 0; - int i = 0; - for (auto &[timerName, timer] : Time::timers()) { - int width = timerWidth * timer.elapsedSeconds() / totalTime; - - if (timerName == "d all" || timerName[0] != 'd') { - continue; - } - - screen->drawTetragon(Vec2D{xPos, yPos + height * i}, - Vec2D{xPos + width, yPos + height * i}, - Vec2D{xPos + width, yPos + height + height * i}, - Vec2D{xPos, yPos + height + height * i}, - {static_cast(255.0 * static_cast(width) / timerWidth), - static_cast(255.0 * (1.0 - static_cast(width) / timerWidth)), - 0, 100}); - - screen->drawText( - timerName.substr(2, timerName.size()) + ":\t" + std::to_string(timer.elapsedMilliseconds()) + " ms \t (" + - std::to_string((int) (100 * timer.elapsedSeconds() / totalTime)) + "%)", - Vec2D{xPos + 10, yPos + height * i + 5}, 30, - sf::Color(0, 0, 0, 150)); - - i++; - timeSum += timer.elapsedSeconds(); - } - - int width = timerWidth * (totalTime - timeSum) / totalTime; - screen->drawTetragon(Vec2D{xPos, yPos + height * i}, - Vec2D{xPos + width, yPos + height * i}, - Vec2D{xPos + width, yPos + height + height * i}, - Vec2D{xPos, yPos + height + height * i}, - {static_cast(255.0 * static_cast(width) / timerWidth), - static_cast(255.0 * (1.0 - static_cast(width) / timerWidth)), - 0, 100}); - - screen->drawText("other:\t" + std::to_string(1000*(totalTime - timeSum)) + " ms \t (" + - std::to_string((int) (100 * (totalTime - timeSum) / totalTime)) + "%)", - Vec2D{xPos + 10, yPos + height * i + 5}, 30, - sf::Color(0, 0, 0, 150)); - - } -} diff --git a/engine/Engine.h b/engine/Engine.h deleted file mode 100644 index aa75af6..0000000 --- a/engine/Engine.h +++ /dev/null @@ -1,61 +0,0 @@ -// -// Created by Иван Ильин on 14.01.2021. -// - -#ifndef ENGINE_ENGINE_H -#define ENGINE_ENGINE_H - -#include "io/Screen.h" -#include "io/Keyboard.h" -#include "io/Mouse.h" -#include "World.h" -#include "Camera.h" -#include "utils/Log.h" - -class Engine { -private: - std::string _name; - - int _triPerSec = 0; - bool _updateWorld = true; - bool _showDebugInfo = Consts::SHOW_DEBUG_INFO; - bool _useOpenGL = Consts::USE_OPEN_GL; - - void printDebugInfo() const; - -protected: - const std::shared_ptr screen = std::make_shared(); - const std::shared_ptr keyboard = std::make_shared(); - const std::shared_ptr mouse = std::make_shared(screen); - - const std::shared_ptr world = std::make_shared(); - const std::shared_ptr camera = std::make_shared(); - - virtual void start() {}; - - virtual void update() {}; - - [[nodiscard]] bool showDebugInfo() const { return _showDebugInfo; } - void setDebugInfo(bool value) { _showDebugInfo = value; } - - void setUpdateWorld(bool value) { _updateWorld = value; } - - void setGlEnable(bool value) { _useOpenGL = value; } - [[nodiscard]] bool glEnable() const { return _useOpenGL; } - - virtual void gui() {} - -public: - Engine(); - - virtual ~Engine() = default; - - void create(int screenWidth = Consts::STANDARD_SCREEN_WIDTH, int screenHeight = Consts::STANDARD_SCREEN_HEIGHT, - const std::string &name = Consts::PROJECT_NAME, bool verticalSync = true, - sf::Color background = Consts::BACKGROUND_COLOR, sf::Uint32 style = sf::Style::Default); - - void exit(); -}; - - -#endif //INC_3DZAVR_TDZAVR_H diff --git a/engine/Mesh.cpp b/engine/Mesh.cpp deleted file mode 100644 index 9506249..0000000 --- a/engine/Mesh.cpp +++ /dev/null @@ -1,223 +0,0 @@ -// -// Created by Иван Ильин on 13.01.2021. -// - -#include - -#include "Mesh.h" -#include "utils/ResourceManager.h" -#include "io/Screen.h" - -using namespace std; - -Mesh &Mesh::operator*=(const Matrix4x4 &matrix4X4) { - std::vector newTriangles; - newTriangles.reserve(_tris.size()); - for (auto &t : _tris) { - newTriangles.emplace_back(t * matrix4X4); - } - setTriangles(std::move(newTriangles)); - - return *this; -} - -void Mesh::loadObj(const std::string &filename, const Vec3D &scale) { - _tris.clear(); - auto objects = ResourceManager::loadObjects(filename); - for (auto &obj : objects) { - for (auto &tri : obj->triangles()) { - _tris.push_back(tri); - } - } - this->scale(scale); -} - -Mesh::Mesh(ObjectNameTag nameTag, const std::string &filename, const Vec3D &scale) : Object(std::move(nameTag)) { - loadObj(filename, scale); -} - -Mesh::Mesh(ObjectNameTag nameTag, const vector &tries) : Object(std::move(nameTag)), _tris(tries) {} - -void Mesh::setColor(const sf::Color &c) { - _color = c; - - for (auto &t : _tris) { - t.setColor(c); - } - - // because we change the color of mesh we should update geometry with a new color - glFreeFloatArray(); -} - -Mesh -Mesh::LineTo(ObjectNameTag nameTag, const Vec3D &from, const Vec3D &to, double line_width, const sf::Color &color) { - - Mesh line(std::move(nameTag)); - - Vec3D v1 = (to - from).normalized(); - Vec3D v2 = from.cross(from + Vec3D{1, 0, 0}).normalized(); - Vec3D v3 = v1.cross(v2).normalized(); - - // from plane - Vec4D p1 = (- v2 * line_width / 2.0 - v3 * line_width / 2.0).makePoint4D(); - Vec4D p2 = (- v2 * line_width / 2.0 + v3 * line_width / 2.0).makePoint4D(); - Vec4D p3 = ( v2 * line_width / 2.0 + v3 * line_width / 2.0).makePoint4D(); - Vec4D p4 = ( v2 * line_width / 2.0 - v3 * line_width / 2.0).makePoint4D(); - // to plane - Vec4D p5 = (to - from - v2 * line_width / 2.0 - v3 * line_width / 2.0).makePoint4D(); - Vec4D p6 = (to - from - v2 * line_width / 2.0 + v3 * line_width / 2.0).makePoint4D(); - Vec4D p7 = (to - from + v2 * line_width / 2.0 + v3 * line_width / 2.0).makePoint4D(); - Vec4D p8 = (to - from + v2 * line_width / 2.0 - v3 * line_width / 2.0).makePoint4D(); - - - line._tris = std::move(std::vector{ - {p2, p4, p1}, - {p2, p3, p4}, - {p1, p6, p2}, - {p1, p5, p6}, - {p2, p6, p7}, - {p2, p7, p3}, - {p6, p5, p8}, - {p6, p8, p7}, - {p4, p3, p7}, - {p4, p7, p8}, - {p1, p8, p5}, - {p1, p4, p8} - }); - line.setColor(color); - line.translateToPoint(from); - - return line; -} - - -Mesh Mesh::ArrowTo(ObjectNameTag nameTag, const Vec3D &from, const Vec3D &to, double line_width, sf::Color color) { - - Mesh arrow(std::move(nameTag)); - - Vec3D v1 = (to - from).normalized(); - Vec3D v2 = from.cross(from + Vec3D{1, 0, 0}).normalized(); - Vec3D v3 = v1.cross(v2).normalized(); - - Vec3D to_line = to - v1*0.4; - - // from plane - Vec4D p1 = (- v2 * line_width / 2.0 - v3 * line_width / 2.0).makePoint4D(); - Vec4D p2 = (- v2 * line_width / 2.0 + v3 * line_width / 2.0).makePoint4D(); - Vec4D p3 = ( v2 * line_width / 2.0 + v3 * line_width / 2.0).makePoint4D(); - Vec4D p4 = ( v2 * line_width / 2.0 - v3 * line_width / 2.0).makePoint4D(); - // to plane - Vec4D p5 = (to_line - from - v2 * line_width / 2.0 - v3 * line_width / 2.0).makePoint4D(); - Vec4D p6 = (to_line - from - v2 * line_width / 2.0 + v3 * line_width / 2.0).makePoint4D(); - Vec4D p7 = (to_line - from + v2 * line_width / 2.0 + v3 * line_width / 2.0).makePoint4D(); - Vec4D p8 = (to_line - from + v2 * line_width / 2.0 - v3 * line_width / 2.0).makePoint4D(); - - // arrow - Vec4D p9 = (to_line - from - v2 * line_width*2 - v3 * line_width*2).makePoint4D(); - Vec4D p10 = (to_line - from - v2 * line_width*2 + v3 * line_width*2).makePoint4D(); - Vec4D p11 = (to_line - from + v2 * line_width*2 + v3 * line_width*2).makePoint4D(); - Vec4D p12 = (to_line - from + v2 * line_width*2 - v3 * line_width*2).makePoint4D(); - - Vec4D p13 = (to - from).makePoint4D(); - - arrow._tris = std::move(std::vector{ - {p2, p4, p1}, - {p2, p3, p4}, - {p1, p6, p2}, - {p1, p5, p6}, - {p2, p6, p7}, - {p2, p7, p3}, - {p6, p5, p8}, - {p6, p8, p7}, - {p4, p3, p7}, - {p4, p7, p8}, - {p1, p8, p5}, - {p1, p4, p8}, - - { p9, p10, p13 }, - { p10, p11, p13 }, - { p11, p12, p13 }, - { p12, p9, p13 }, - }); - arrow.setColor(color); - arrow.translateToPoint(from); - - return arrow; -} - -void Mesh::setOpacity(double t) { - setColor(sf::Color(_color.r, _color.g, _color.b, t*255)); -} - -void Mesh::setTriangles(vector&& t) { - _tris = std::move(t); -} - -Mesh::~Mesh() { - delete[] _geometry; - _geometry = nullptr; -} - -void Mesh::glFreeFloatArray() { - delete[] _geometry; - _geometry = nullptr; -} - -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; -} - -Mesh Mesh::Cube(ObjectNameTag tag, double size, sf::Color color) { - Mesh cube(std::move(tag)); - - cube._tris = { - { Vec4D{0.0, 0.0, 0.0, 1.0}, Vec4D{0.0, 1.0, 0.0, 1.0}, Vec4D{1.0, 1.0, 0.0, 1.0} }, - { Vec4D{0.0, 0.0, 0.0, 1.0}, Vec4D{1.0, 1.0, 0.0, 1.0}, Vec4D{1.0, 0.0, 0.0, 1.0} }, - { Vec4D{1.0, 0.0, 0.0, 1.0}, Vec4D{1.0, 1.0, 0.0, 1.0}, Vec4D{1.0, 1.0, 1.0, 1.0} }, - { Vec4D{1.0, 0.0, 0.0, 1.0}, Vec4D{1.0, 1.0, 1.0, 1.0}, Vec4D{1.0, 0.0, 1.0, 1.0} }, - { Vec4D{1.0, 0.0, 1.0, 1.0}, Vec4D{1.0, 1.0, 1.0, 1.0}, Vec4D{0.0, 1.0, 1.0, 1.0} }, - { Vec4D{1.0, 0.0, 1.0, 1.0}, Vec4D{0.0, 1.0, 1.0, 1.0}, Vec4D{0.0, 0.0, 1.0, 1.0} }, - { Vec4D{0.0, 0.0, 1.0, 1.0}, Vec4D{0.0, 1.0, 1.0, 1.0}, Vec4D{0.0, 1.0, 0.0, 1.0} }, - { Vec4D{0.0, 0.0, 1.0, 1.0}, Vec4D{0.0, 1.0, 0.0, 1.0}, Vec4D{0.0, 0.0, 0.0, 1.0} }, - { Vec4D{0.0, 1.0, 0.0, 1.0}, Vec4D{0.0, 1.0, 1.0, 1.0}, Vec4D{1.0, 1.0, 1.0, 1.0} }, - { Vec4D{0.0, 1.0, 0.0, 1.0}, Vec4D{1.0, 1.0, 1.0, 1.0}, Vec4D{1.0, 1.0, 0.0, 1.0} }, - { Vec4D{1.0, 0.0, 1.0, 1.0}, Vec4D{0.0, 0.0, 1.0, 1.0}, Vec4D{0.0, 0.0, 0.0, 1.0} }, - { Vec4D{1.0, 0.0, 1.0, 1.0}, Vec4D{0.0, 0.0, 0.0, 1.0}, Vec4D{1.0, 0.0, 0.0, 1.0} }, - }; - cube.setColor(color); - - return cube *= Matrix4x4::Scale(Vec3D(size, size, size))*Matrix4x4::Translation(Vec3D(-0.5, -0.5, -0.5)); -} diff --git a/engine/Mesh.h b/engine/Mesh.h deleted file mode 100644 index 9a00b9a..0000000 --- a/engine/Mesh.h +++ /dev/null @@ -1,69 +0,0 @@ -// -// Created by Иван Ильин on 13.01.2021. -// - -#ifndef ENGINE_MESH_H -#define ENGINE_MESH_H - -#include -#include - -#include - -#include "Triangle.h" -#include "Object.h" - -class Mesh : public Object { -private: - std::vector _tris; - sf::Color _color = sf::Color(255, 245, 194); - bool _visible = true; - - Mesh &operator*=(const Matrix4x4 &matrix4X4); - - // OpenGL - mutable GLfloat* _geometry = nullptr; -public: - explicit Mesh(ObjectNameTag nameTag) : Object(std::move(nameTag)) {}; - - Mesh &operator=(const Mesh &mesh) = delete; - - Mesh(const Mesh &mesh) = default; - - explicit Mesh(ObjectNameTag nameTag, const std::vector &tries); - - explicit Mesh(ObjectNameTag nameTag, const std::string &filename, const Vec3D &scale = Vec3D{1, 1, 1}); - - void loadObj(const std::string &filename, const Vec3D &scale = Vec3D{1, 1, 1}); - - [[nodiscard]] std::vector const &triangles() const { return _tris; } - - void setTriangles(std::vector&& t); - - [[nodiscard]] size_t size() const { return _tris.size() * 3; } - - [[nodiscard]] sf::Color color() const { return _color; } - - void setColor(const sf::Color &c); - - void setOpacity(double t); - - void setVisible(bool visibility) { _visible = visibility; } - - [[nodiscard]] bool isVisible() const { return _visible; } - - ~Mesh() override; - - Mesh static Cube(ObjectNameTag tag, double size = 1.0, sf::Color color = sf::Color(0,0,0)); - - Mesh static LineTo(ObjectNameTag nameTag, const Vec3D &from, const Vec3D &to, double line_width = 0.1, - const sf::Color &color = {150, 150, 150, 100}); - - Mesh static ArrowTo(ObjectNameTag nameTag, const Vec3D& from, const Vec3D& to, double line_width = 0.1, sf::Color color = {150, 150, 150, 255}); - - // OpenGL functions - GLfloat *glFloatArray() const; - void glFreeFloatArray(); -}; - -#endif //INC_3DZAVR_MESH_H diff --git a/engine/Object.cpp b/engine/Object.cpp deleted file mode 100644 index 5e7a906..0000000 --- a/engine/Object.cpp +++ /dev/null @@ -1,211 +0,0 @@ -// -// Created by Иван Ильин on 15.03.2021. -// - -#include - -#include "Object.h" -#include "math/Matrix4x4.h" - -bool ObjectNameTag::contains(const ObjectNameTag &nameTag) const { - if(_name.find(nameTag.str()) != std::string::npos) { - return true; - } - return false; -} - -void Object::transform(const Matrix4x4 &t) { - _transformMatrix = t * _transformMatrix; - - for (auto &[attachedName, attachedObject] : _attachedObjects) { - if (!attachedObject.expired()) { - attachedObject.lock()->transformRelativePoint(position(), t); - } - } -} - -void Object::transformRelativePoint(const Vec3D &point, const Matrix4x4 &transform) { - - // translate object in new coordinate system (connected with point) - _transformMatrix = Matrix4x4::Translation(position() - point) * _transformMatrix; - // transform object in the new coordinate system - _transformMatrix = transform * _transformMatrix; - // translate object back in self connected coordinate system - _position = _transformMatrix.w() + point; - _transformMatrix = Matrix4x4::Translation(-_transformMatrix.w()) * _transformMatrix; - - for (auto &[attachedName, attachedObject] : _attachedObjects) { - if (!attachedObject.expired()) { - attachedObject.lock()->transformRelativePoint(point, transform); - } - } -} - -void Object::translate(const Vec3D &dv) { - - _position = _position + dv; - - for (auto &[attachedName, attachedObject] : _attachedObjects) { - if (!attachedObject.expired()) { - attachedObject.lock()->translate(dv); - } - } -} - -void Object::scale(const Vec3D &s) { - transform(Matrix4x4::Scale(s)); -} - -void Object::rotate(const Vec3D &r) { - _angle = _angle + r; - - Matrix4x4 rotationMatrix = Matrix4x4::RotationX(r.x()) * Matrix4x4::RotationY(r.y()) * Matrix4x4::RotationZ(r.z()); - transform(rotationMatrix); -} - -void Object::rotate(const Vec3D &v, double rv) { - transform(Matrix4x4::Rotation(v, rv)); -} - -void Object::rotateRelativePoint(const Vec3D &s, const Vec3D &r) { - _angle = _angle + r; - - transformRelativePoint(s, Matrix4x4::Rotation(r)); -} - -void Object::rotateRelativePoint(const Vec3D &s, const Vec3D &v, double r) { - transformRelativePoint(s, Matrix4x4::Rotation(v, r)); -} - -void Object::rotateLeft(double rl) { - _angleLeftUpLookAt = Vec3D{_angleLeftUpLookAt.x() + rl, - _angleLeftUpLookAt.y(), - _angleLeftUpLookAt.z()}; - - rotate(left(), rl); -} - -void Object::rotateUp(double ru) { - _angleLeftUpLookAt = Vec3D{_angleLeftUpLookAt.x(), - _angleLeftUpLookAt.y() + ru, - _angleLeftUpLookAt.z()}; - - rotate(up(), ru); -} - -void Object::rotateLookAt(double rlAt) { - _angleLeftUpLookAt = Vec3D{_angleLeftUpLookAt.x(), - _angleLeftUpLookAt.y(), - _angleLeftUpLookAt.z() + rlAt}; - rotate(lookAt(), rlAt); -} - -void Object::translateToPoint(const Vec3D &point) { - translate(point - position()); -} - -void Object::attractToPoint(const Vec3D &point, double value) { - Vec3D v = (point - position()).normalized(); - translate(v*value); -} - -void Object::rotateToAngle(const Vec3D &v) { - rotate(v - _angle); -} - -std::shared_ptr Object::attached(const ObjectNameTag &tag) { - if (_attachedObjects.count(tag) == 0 || _attachedObjects.find(tag)->second.expired()) { - return nullptr; - } - return _attachedObjects.find(tag)->second.lock(); -} - -bool Object::checkIfAttached(Object *obj) { - for (const auto&[nameTag, attachedObject] : _attachedObjects) { - if (obj == attachedObject.lock().get() || attachedObject.lock()->checkIfAttached(obj)) { - return true; - } - } - return false; -} - -void Object::attach(std::shared_ptr object) { - if (this != object.get()) { - if (!object->checkIfAttached(this)) { - _attachedObjects.emplace(object->name(), object); - } else { - throw std::invalid_argument{"Object::attach: You tried to create infinite recursive call chains"}; - } - } else { - throw std::invalid_argument{"Object::attach: You cannot attach object to itself"}; - } -} - -void Object::unattach(const ObjectNameTag &tag) { - _attachedObjects.erase(tag); -} - -// OpenGL function -GLfloat *Object::glInvModel() const { - auto *v = new GLfloat[4 * 4]; - - Vec3D _left = _transformMatrix.x(); - Vec3D _up = _transformMatrix.y(); - Vec3D _lookAt = _transformMatrix.z(); - - v[0] = -static_cast(_left.x()); - v[4] = -static_cast(_left.y()); - v[8] = -static_cast(_left.z()); - v[12] = static_cast(position().dot(_left)); - - v[1] = static_cast(_up.x()); - v[5] = static_cast(_up.y()); - v[9] = static_cast(_up.z()); - v[13] = -static_cast(position().dot(_up)); - - v[2] = -static_cast(_lookAt.x()); - v[6] = -static_cast(_lookAt.y()); - v[10] = -static_cast(_lookAt.z()); - v[14] = static_cast(position().dot(_lookAt)); - - v[3] = static_cast(0.0f); - v[7] = static_cast(0.0f); - v[11] = static_cast(0.0f); - v[15] = static_cast(1.0f); - - return v; -} - -GLfloat *Object::glModel() const { - auto *m = new GLfloat[4 * 4]; - - Vec3D _left = _transformMatrix.x(); - Vec3D _up = _transformMatrix.y(); - Vec3D _lookAt = _transformMatrix.z(); - - m[0] = static_cast(_left.x()); - m[4] = static_cast(_up.x()); - m[8] = static_cast(_lookAt.x()); - m[12] = static_cast(position().x()); - - m[1] = static_cast(_left.y()); - m[5] = static_cast(_up.y()); - m[9] = static_cast(_lookAt.y()); - m[13] = static_cast(position().y()); - - m[2] = static_cast(_left.z()); - m[6] = static_cast(_up.z()); - m[10] = static_cast(_lookAt.z()); - m[14] = static_cast(position().z()); - - m[3] = static_cast(0.0f); - m[7] = static_cast(0.0f); - m[11] = static_cast(0.0f); - m[15] = static_cast(1.0f); - - return m; -} - -Object::~Object() { - _attachedObjects.clear(); -} diff --git a/engine/Object.h b/engine/Object.h deleted file mode 100644 index 3cb5530..0000000 --- a/engine/Object.h +++ /dev/null @@ -1,99 +0,0 @@ -// -// Created by Иван Ильин on 15.03.2021. -// - -#ifndef ENGINE_OBJECT_H -#define ENGINE_OBJECT_H - -#include -#include -#include -#include - -#include "math/Vec3D.h" -#include "math/Matrix4x4.h" -#include - -class ObjectNameTag final { -private: - const std::string _name; -public: - explicit ObjectNameTag(std::string name = "") : _name(std::move(name)) {} - - [[nodiscard]] std::string str() const { return _name; } - - bool operator==(const ObjectNameTag &tag) const { return _name == tag._name; } - bool operator!=(const ObjectNameTag &tag) const { return _name != tag._name; } - bool operator<(const ObjectNameTag &tag) const { return _name < tag._name; } - - [[nodiscard]] bool contains(const ObjectNameTag& nameTag) const; -}; - -class Object { -private: - bool checkIfAttached(Object *obj); - - const ObjectNameTag _nameTag; - - Matrix4x4 _transformMatrix = Matrix4x4::Identity(); - Vec3D _position{0, 0, 0}; - /* - * Take into account when you rotate body, - * you change '_angle' & '_angleLeftUpLookAt' only for this particular body, - * but not for attached objects! This way during rotation - * '_angle' & '_angleLeftUpLookAt' stays constant all attached objects. - */ - Vec3D _angle{0, 0, 0}; - Vec3D _angleLeftUpLookAt{0, 0, 0}; - - std::map> _attachedObjects; - -public: - explicit Object(ObjectNameTag nameTag) : _nameTag(std::move(nameTag)) {}; - - Object(const Object &object) : _nameTag(object._nameTag), - _transformMatrix(object._transformMatrix), - _position(object._position), - _angle(object._angle), - _angleLeftUpLookAt(object._angleLeftUpLookAt) {}; - - // TODO: implement rotations using quaternions (?) - void transform(const Matrix4x4 &t); - void transformRelativePoint(const Vec3D &point, const Matrix4x4 &transform); - void translate(const Vec3D &dv); - void translateToPoint(const Vec3D &point); - void attractToPoint(const Vec3D &point, double value); - void scale(const Vec3D &s); - void rotate(const Vec3D &r); - void rotate(const Vec3D &v, double rv); - void rotateToAngle(const Vec3D &v); - void rotateRelativePoint(const Vec3D &s, const Vec3D &r); - void rotateRelativePoint(const Vec3D &s, const Vec3D &v, double r); - void rotateLeft(double rl); - void rotateUp(double ru); - void rotateLookAt(double rlAt); - - [[nodiscard]] Vec3D left() const { return _transformMatrix.x().normalized(); } - [[nodiscard]] Vec3D up() const { return _transformMatrix.y().normalized(); } - [[nodiscard]] Vec3D lookAt() const { return _transformMatrix.z().normalized(); } - [[nodiscard]] Vec3D position() const { return _position; } - [[nodiscard]] Vec3D angle() const { return _angle; } - [[nodiscard]] Vec3D angleLeftUpLookAt() const { return _angleLeftUpLookAt; } - - void attach(std::shared_ptr object); - void unattach(const ObjectNameTag &tag); - std::shared_ptr attached(const ObjectNameTag &tag); - - [[nodiscard]] ObjectNameTag name() const { return _nameTag; } - - [[nodiscard]] Matrix4x4 model() const { return Matrix4x4::Translation(_position) * _transformMatrix; } - [[nodiscard]] Matrix4x4 invModel() const { return Matrix4x4::View(model()); } - - // OpenGL function - [[nodiscard]] GLfloat *glModel() const; - [[nodiscard]] GLfloat *glInvModel() const; - - virtual ~Object(); -}; - -#endif //MINECRAFT_3DZAVR_OBJECT_H diff --git a/engine/Triangle.cpp b/engine/Triangle.cpp deleted file mode 100644 index d2e2eef..0000000 --- a/engine/Triangle.cpp +++ /dev/null @@ -1,52 +0,0 @@ -// -// Created by Иван Ильин on 13.01.2021. -// - -#include "Triangle.h" -#include "Consts.h" - -Triangle::Triangle(const Vec4D &p1, const Vec4D &p2, const Vec4D &p3, sf::Color color) : _color(color), - _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]}, - _color(triangle._color), _normal(triangle._normal) { -} - -Triangle Triangle::operator*(const Matrix4x4 &matrix4X4) const { - return Triangle(matrix4X4 * _points[0], matrix4X4 * _points[1], matrix4X4 * _points[2], _color); -} - -Vec3D Triangle::norm() const { - return _normal; -} - -const Vec4D& Triangle::operator[](int i) const { - return _points[i]; -} - -bool Triangle::isPointInside(const Vec3D &point) const { - Vec3D triangleNorm = norm(); - - double dot1 = (point - Vec3D(_points[0])).cross(Vec3D(_points[1] - _points[0])).dot(triangleNorm); - double dot2 = (point - Vec3D(_points[1])).cross(Vec3D(_points[2] - _points[1])).dot(triangleNorm); - double dot3 = (point - Vec3D(_points[2])).cross(Vec3D(_points[0] - _points[2])).dot(triangleNorm); - - if ((dot1 >= 0 && dot2 >= 0 && dot3 >= 0) || (dot1 <= 0 && dot2 <= 0 && dot3 <= 0)) { - return true; - } - return false; -} diff --git a/engine/Triangle.h b/engine/Triangle.h deleted file mode 100644 index f6f4edf..0000000 --- a/engine/Triangle.h +++ /dev/null @@ -1,49 +0,0 @@ -// -// Created by Иван Ильин on 13.01.2021. -// - -#ifndef ENGINE_TRIANGLE_H -#define ENGINE_TRIANGLE_H - -#include - -#include "math/Vec4D.h" -#include "math/Vec3D.h" -#include "math/Matrix4x4.h" - -class Triangle final { -private: - sf::Color _color; - Vec4D _points[3]; - Vec3D _normal; - - void calculateNormal(); -public: - Triangle() = default; - - Triangle(const Triangle &triangle); - - Triangle(const Vec4D &p1, const Vec4D &p2, const Vec4D &p3, sf::Color color = {0, 0, 0}); - - Triangle &operator=(const Triangle &) = default; - - [[nodiscard]] const Vec4D& operator[](int i) const; - - [[nodiscard]] Vec3D position() const { return Vec3D(_points[0] + _points[1] + _points[2])/3; } - - [[nodiscard]] Vec3D norm() const; - - // Operations with Matrix4x4 - [[nodiscard]] Triangle operator*(const Matrix4x4 &matrix4X4) const; - - [[nodiscard]] bool isPointInside(const Vec3D &point) const; - - [[nodiscard]] sf::Color color() const { return _color; } - - void setColor(sf::Color newColor) { _color = newColor; } - - [[nodiscard]] double distance(const Vec3D &vec) const { return norm().dot(Vec3D(_points[0]) - vec); } -}; - - -#endif //INC_3DZAVR_TRIANGLE_H diff --git a/engine/World.cpp b/engine/World.cpp deleted file mode 100644 index 0fb2043..0000000 --- a/engine/World.cpp +++ /dev/null @@ -1,164 +0,0 @@ -// -// Created by Иван Ильин on 13.01.2021. -// - -#include -#include - -#include "World.h" -#include "utils/Log.h" -#include "math/Plane.h" -#include "utils/ResourceManager.h" - -using namespace std; - -std::shared_ptr World::addBody(std::shared_ptr body) { - _objects.emplace(body->name(), body); - Log::log("World::addBody(): inserted body '" + body->name().str() + "' with " + - std::to_string(_objects[body->name()]->triangles().size()) + " tris."); - return _objects[body->name()]; -} - -std::shared_ptr World::loadBody(const ObjectNameTag &tag, const string &filename, const Vec3D &scale) { - _objects.emplace(tag, std::make_shared(tag, filename, scale)); - Log::log("World::loadBody(): inserted body from " + filename + " with title '" + tag.str() + "' with " + - std::to_string(_objects[tag]->triangles().size()) + " tris."); - return _objects[tag]; -} - -IntersectionInformation World::rayCast(const Vec3D &from, const Vec3D &to, const std::string &skipTags) { - - // make vector of tags, that we are going to escape - vector tagsToSkip; - stringstream s(skipTags); - std::string t; - while (s >> t) { - tagsToSkip.push_back(t); - } - - bool intersected = false; - Vec3D point{}; - Triangle triangle; - std::string bodyName; - double minDistance = Consts::RAY_CAST_MAX_DISTANCE; - std::shared_ptr intersectedBody = nullptr; - - for (auto&[name, body] : _objects) { - - bool escapeThisBody = false; - for (auto &escapeTag : tagsToSkip) { - if (name.contains(ObjectNameTag(escapeTag))) { - escapeThisBody = true; - break; - } - } - if (escapeThisBody) { - continue; - } - - Matrix4x4 model = body->model(); - // It is computationally more efficient not to transform all object's triangles from model to global - // coordinate system, but translate 'from' and 'to' vectors inside once and check triangles without performing - // many matrix multiplication. - Matrix4x4 invModel = body->invModel(); - - Vec3D v = (to - from).normalized(); - Vec3D v_model = invModel*v; - Vec3D from_model = invModel*(from - body->position()); - Vec3D to_model = invModel*(to - body->position()); - - - for (auto &tri : body->triangles()) { - - if(tri.norm().dot(v_model) > 0) { - continue; - } - - auto intersection = Plane(tri).intersection(from_model, to_model); - - if (intersection.second > 0 && tri.isPointInside(intersection.first)) { - - // When you change to model coordinate system you also will get distance scaled by invModel. - // Due-to this effect if you scale some object in x times you will get distance in x times smaller. - // That's why we need to perform distance calculation in the global coordinate system where metric - // is the same for all objects. - Triangle globalTriangle(model * tri[0], model * tri[1], model * tri[2], tri.color()); - auto globalIntersection = Plane(globalTriangle).intersection(from, to); - double globalDistance = (globalIntersection.first - from).abs(); - - if(globalDistance < minDistance) { - minDistance = globalDistance; - point = globalIntersection.first; - triangle = globalTriangle; - bodyName = name.str(); - intersected = true; - intersectedBody = body; - //Triangle triangleRED = Triangle(model * tri[0], model * tri[1], model * tri[2], sf::Color(255, 0, 0)); - //addBody(std::make_shared(Mesh(ObjectNameTag("Test" + std::to_string(rand())), std::vector({triangleRED})))); - } - } - } - } - - return IntersectionInformation{point, sqrt(minDistance), triangle, ObjectNameTag(bodyName), intersectedBody, intersected}; -} - -void World::loadMap(const std::string &filename, const Vec3D &scale) { - auto objs = ResourceManager::loadObjects(filename); - for (auto &i : objs) { - std::shared_ptr obj = std::make_shared(*i, false); - addBody(obj); - obj->scale(scale); - } -} - -void World::removeBody(const ObjectNameTag &tag) { - if (_objects.erase(tag) > 0) { - Log::log("World::removeBody(): removed body '" + tag.str() + "'"); - } else { - Log::log("World::removeBody(): cannot remove body '" + tag.str() + "': body does not exist."); - } -} - -void World::checkCollision(const ObjectNameTag &tag) { - if (_objects[tag]->hasCollision()) { - - _objects[tag]->setInCollision(false); - - for (auto it = _objects.begin(); it != _objects.end();) { - auto obj = it->second; - ObjectNameTag name = it->first; - it++; - - if ((name == tag) || !(obj->isCollider() || obj->isTrigger())) { - continue; - } - - std::pair gjk = _objects[tag]->checkGJKCollision(obj); - if (gjk.first) { - if (obj->isCollider()) { - CollisionPoint epa = _objects[tag]->EPA(gjk.second, obj); - _objects[tag]->solveCollision(epa); - } - if (_objects[tag]->collisionCallBack() != nullptr) { - _objects[tag]->collisionCallBack()(name, obj); - } - } - - } - } -} - -void World::update() { - for (auto &[nameTag, obj] : _objects) { - obj->updatePhysicsState(); - checkCollision(nameTag); - } -} - -std::shared_ptr World::body(const ObjectNameTag &tag) { - if (_objects.count(tag) == 0) { - return nullptr; - } - return _objects.find(tag)->second; -} diff --git a/engine/World.h b/engine/World.h deleted file mode 100644 index 68eb723..0000000 --- a/engine/World.h +++ /dev/null @@ -1,47 +0,0 @@ -// -// Created by Иван Ильин on 13.01.2021. -// - -#ifndef ENGINE_WORLD_H -#define ENGINE_WORLD_H - -#include - -#include "Camera.h" -#include "io/Screen.h" -#include "physics/RigidBody.h" - -struct IntersectionInformation final { - const Vec3D pointOfIntersection; - const double distanceToObject; - const Triangle intersectedTriangle; - const ObjectNameTag objectName; - const std::shared_ptr obj; - const bool intersected; -}; - -class World final { -private: - std::map> _objects; - - void checkCollision(const ObjectNameTag &tag); -public: - World() = default; - - void update(); - - std::shared_ptr addBody(std::shared_ptr mesh); - std::shared_ptr body(const ObjectNameTag &tag); - void removeBody(const ObjectNameTag &tag); - std::shared_ptr loadBody(const ObjectNameTag &tag, const std::string &filename, const Vec3D &scale = Vec3D{1, 1, 1}); - void loadMap(const std::string &filename, const Vec3D &scale = Vec3D{1, 1, 1}); - - // std::string skipTags is a string that consist of all objects we want to skip in ray casting - IntersectionInformation rayCast(const Vec3D &from, const Vec3D &to, const std::string &skipTags = ""); - - std::map>::iterator begin() { return _objects.begin(); } - std::map>::iterator end() { return _objects.end(); } -}; - - -#endif //INC_3DZAVR_WORLD_H diff --git a/engine/animation/AAttractToPoint.h b/engine/animation/AAttractToPoint.h deleted file mode 100644 index df9f2cb..0000000 --- a/engine/animation/AAttractToPoint.h +++ /dev/null @@ -1,36 +0,0 @@ -// -// Created by Иван Ильин on 01.11.2021. -// - -#ifndef SHOOTER_AATTRACTTOPOINT_H -#define SHOOTER_AATTRACTTOPOINT_H - -#include "Animation.h" -#include "../Object.h" - -class AAttractToPoint : public Animation { -private: - const std::weak_ptr _object; - const Vec3D _targetPoint; - const double _valueToAttract; - - void update() override { - auto obj = _object.lock(); - - if (obj == nullptr) { - stop(); - return; - } - - obj->attractToPoint(_targetPoint, _valueToAttract * dprogress()); - } - -public: - AAttractToPoint(std::weak_ptr object, const Vec3D &targetPoint, double valueToAttract, double duration = 1, - Animation::LoopOut looped = LoopOut::None, - Animation::InterpolationType interpolationType = InterpolationType::Bezier) - : Animation(duration, looped, interpolationType), _object(object), _targetPoint(targetPoint), - _valueToAttract(valueToAttract) {} -}; - -#endif //SHOOTER_AATTRACTTOPOINT_H diff --git a/engine/animation/AColor.h b/engine/animation/AColor.h deleted file mode 100644 index 5870b06..0000000 --- a/engine/animation/AColor.h +++ /dev/null @@ -1,48 +0,0 @@ -// -// Created by Иван Ильин on 02.06.2021. -// - -#ifndef ENGINE_ACOLOR_H -#define ENGINE_ACOLOR_H - -#include "Animation.h" -#include "../Mesh.h" - -class AColor final : public Animation { -private: - const std::weak_ptr _mesh; - - sf::Color _startColor; - const sf::Color _newColor; - bool _started = false; - - void update() override { - auto mesh = _mesh.lock(); - - if (mesh == nullptr) { - stop(); - return; - } - - if (!_started) { - _started = true; - _startColor = mesh->color(); - } - - Vec4D start(_startColor.r, _startColor.g, _startColor.b, _startColor.a); - Vec4D end(_newColor.r, _newColor.g, _newColor.b, _newColor.a); - Vec4D mid = start + (end - start) * progress(); - - mesh->setColor(sf::Color(static_cast(mid.x()), static_cast(mid.y()), - static_cast(mid.z()), static_cast(mid.w()))); - } - -public: - AColor(std::weak_ptr mesh, const sf::Color &color, double duration = 1, LoopOut looped = LoopOut::None, - InterpolationType interpolationType = InterpolationType::Linear) : Animation(duration, looped, - interpolationType), - _mesh(mesh), _newColor(color) { - } -}; - -#endif //SHOOTER_3DZAVR_ACOLOR_H diff --git a/engine/animation/ADecompose.h b/engine/animation/ADecompose.h deleted file mode 100644 index 3bd49a5..0000000 --- a/engine/animation/ADecompose.h +++ /dev/null @@ -1,50 +0,0 @@ -// -// Created by Иван Ильин on 23.02.2022. -// - -#ifndef INC_3DZAVR_ADECOMPOSE_H -#define INC_3DZAVR_ADECOMPOSE_H - -#include "Animation.h" -#include "../Mesh.h" -#include "../Consts.h" - -class ADecompose final : public Animation { -private: - const std::weak_ptr _mesh; - std::vector _triangles; - - double _value; - bool _started = false; - - void update() override { - auto mesh = _mesh.lock(); - - if (mesh == nullptr) { - stop(); - return; - } - - if (!_started) { - _started = true; - _triangles = _mesh.lock()->triangles(); - } - - std::vector newTriangles; - newTriangles.reserve(_triangles.size()); - - for(auto &t : _triangles) { - newTriangles.emplace_back((t * Matrix4x4::Translation(t.position().normalized()*progress()*_value))); - } - mesh->setTriangles(std::move(newTriangles)); - mesh->glFreeFloatArray(); - } - -public: - ADecompose(std::weak_ptr mesh, double value, double duration = 1, LoopOut looped = LoopOut::None, - InterpolationType interpolationType = InterpolationType::Bezier) : Animation(duration, looped, - interpolationType), - _value(value), _mesh(mesh) {} -}; - -#endif //INC_3DZAVR_ADECOMPOSE_H diff --git a/engine/animation/AFunction.h b/engine/animation/AFunction.h deleted file mode 100644 index 1abfbb6..0000000 --- a/engine/animation/AFunction.h +++ /dev/null @@ -1,30 +0,0 @@ -// -// Created by Иван Ильин on 06.04.2021. -// - -#ifndef ENGINE_AFUNCTION_H -#define ENGINE_AFUNCTION_H - -#include "Animation.h" - -class AFunction final : public Animation { -private: - int _callsCounter = 0; - const int _allCalls = 1; - const std::function _callBack; - - void update() override { - if (_allCalls != 0 && progress() >= (double) (_callsCounter + 1) / _allCalls) { - _callsCounter++; - _callBack(); - } - } - -public: - explicit AFunction(std::function function, int calls = 1, double duration = 1, - LoopOut looped = LoopOut::None, InterpolationType interpolationType = InterpolationType::Linear) - : Animation(duration, looped, interpolationType), _callBack(std::move(function)), _allCalls(calls) { - } -}; - -#endif //MINECRAFT_3DZAVR_AFUNCTION_H diff --git a/engine/animation/ARotate.h b/engine/animation/ARotate.h deleted file mode 100644 index 90a4c20..0000000 --- a/engine/animation/ARotate.h +++ /dev/null @@ -1,34 +0,0 @@ -// -// Created by Иван Ильин on 29.01.2021. -// - -#ifndef ENGINE_AROTATE_H -#define ENGINE_AROTATE_H - -#include "Animation.h" -#include "../Object.h" - -class ARotate final : public Animation { -private: - const std::weak_ptr _object; - const Vec3D _rotationValue; - - void update() override { - auto obj = _object.lock(); - - if (obj == nullptr) { - stop(); - return; - } - - obj->rotate(_rotationValue * dprogress()); - } - -public: - ARotate(std::weak_ptr object, const Vec3D &r, double duration = 1, LoopOut looped = LoopOut::None, - InterpolationType interpolationType = InterpolationType::Bezier) - : Animation(duration, looped, interpolationType), _object(object), _rotationValue(r) { - } -}; - -#endif //INC_3DZAVR_AROTATE_H diff --git a/engine/animation/ARotateLeft.h b/engine/animation/ARotateLeft.h deleted file mode 100644 index 34f0e7f..0000000 --- a/engine/animation/ARotateLeft.h +++ /dev/null @@ -1,33 +0,0 @@ -// -// Created by Иван Ильин on 01.11.2021. -// - -#ifndef SHOOTER_AROTATELEFT_H -#define SHOOTER_AROTATELEFT_H - -#include "Animation.h" -#include "../Object.h" - -class ARotateLeft final : public Animation { -private: - const std::weak_ptr _object; - const double _rotationValue; - - void update() override { - auto obj = _object.lock(); - - if (obj == nullptr) { - stop(); - return; - } - - obj->rotateLeft(_rotationValue*dprogress()); - } - -public: - ARotateLeft(std::weak_ptr object, double r, double duration = 1, LoopOut looped = LoopOut::None, - InterpolationType interpolationType = InterpolationType::Bezier) - : Animation(duration, looped, interpolationType), _object(object), _rotationValue(r) {} -}; - -#endif //SHOOTER_AROTATELEFT_H diff --git a/engine/animation/ARotateLeftUpLookAt.h b/engine/animation/ARotateLeftUpLookAt.h deleted file mode 100644 index f9ae78f..0000000 --- a/engine/animation/ARotateLeftUpLookAt.h +++ /dev/null @@ -1,36 +0,0 @@ -// -// Created by Иван Ильин on 01.11.2021. -// - -#ifndef SHOOTER_AROTATELEFTUPLOOKAT_H -#define SHOOTER_AROTATELEFTUPLOOKAT_H - -#include "Animation.h" -#include "../Object.h" - - -class ARotateLeftUpLookAt final : public Animation { -private: - const std::weak_ptr _object; - const Vec3D _rotationValue; - - void update() override { - auto obj = _object.lock(); - - if (obj == nullptr) { - stop(); - return; - } - - obj->rotateLeft(_rotationValue.x()*dprogress()); - obj->rotateUp(_rotationValue.y()*dprogress()); - obj->rotateLookAt(_rotationValue.z()*dprogress()); - } - -public: - ARotateLeftUpLookAt(std::weak_ptr object, const Vec3D &r, double duration = 1, LoopOut looped = LoopOut::None, - InterpolationType interpolationType = InterpolationType::Bezier) - : Animation(duration, looped, interpolationType), _object(object), _rotationValue(r) {} -}; - -#endif //SHOOTER_AROTATELEFT_H diff --git a/engine/animation/ARotateRelativePoint.h b/engine/animation/ARotateRelativePoint.h deleted file mode 100644 index ac573f9..0000000 --- a/engine/animation/ARotateRelativePoint.h +++ /dev/null @@ -1,36 +0,0 @@ -// -// Created by Иван Ильин on 01.11.2021. -// - -#ifndef SHOOTER_AROTATERELATIVEPOINT_H -#define SHOOTER_AROTATERELATIVEPOINT_H - -#include "Animation.h" -#include "../Object.h" - -class ARotateRelativePoint : public Animation { -private: - const std::weak_ptr _object; - const Vec3D _targetPoint; - const Vec3D _rotationValue; - - void update() override { - auto obj = _object.lock(); - - if (obj == nullptr) { - stop(); - return; - } - - obj->rotateRelativePoint(_targetPoint, _rotationValue * dprogress()); - } - -public: - ARotateRelativePoint(std::weak_ptr object, const Vec3D &targetPoint, const Vec3D &rotationValue, - double duration = 1, Animation::LoopOut looped = LoopOut::None, - Animation::InterpolationType interpolationType = InterpolationType::Bezier) - : Animation(duration, looped, interpolationType), _object(object), _targetPoint(targetPoint), - _rotationValue(rotationValue) {} -}; - -#endif //SHOOTER_AROTATERELATIVEPOINT_H diff --git a/engine/animation/AScale.h b/engine/animation/AScale.h deleted file mode 100644 index f9b3643..0000000 --- a/engine/animation/AScale.h +++ /dev/null @@ -1,39 +0,0 @@ -// -// Created by Иван Ильин on 29.01.2021. -// - -#ifndef ENGINE_ASCALE_H -#define ENGINE_ASCALE_H - -#include "Animation.h" -#include "../Object.h" - -class AScale final : public Animation { -private: - const std::weak_ptr _object; - const Vec3D _scalingValue; - Vec3D _prevScaleFactor{1, 1, 1}; - - void update() override { - auto obj = _object.lock(); - - if (obj == nullptr) { - stop(); - return; - } - // invert scale - obj->scale(Vec3D(1.0/_prevScaleFactor.x(), 1.0/_prevScaleFactor.y(), 1.0/_prevScaleFactor.z())); - Vec3D scaleFactor = Vec3D{1, 1, 1} + (_scalingValue - Vec3D{1, 1, 1}) * progress(); - obj->scale(scaleFactor); - _prevScaleFactor = scaleFactor; - } - -public: - AScale(std::weak_ptr object, const Vec3D &s, double duration = 1, LoopOut looped = LoopOut::None, - InterpolationType interpolationType = InterpolationType::Bezier) : Animation(duration, looped, - interpolationType), - _object(object), _scalingValue(s) { - } -}; - -#endif //INC_3DZAVR_ASCALE_H diff --git a/engine/animation/AShowCreation.h b/engine/animation/AShowCreation.h deleted file mode 100644 index 08312ee..0000000 --- a/engine/animation/AShowCreation.h +++ /dev/null @@ -1,59 +0,0 @@ -// -// Created by Иван Ильин on 10.11.2021. -// - -#ifndef SHOOTER_ASHOWCREATION_H -#define SHOOTER_ASHOWCREATION_H - -#include "Animation.h" -#include "../Mesh.h" - -class AShowCreation final : public Animation { -private: - const std::weak_ptr _mesh; - const std::vector _triangles; - - void update() override { - auto mesh = _mesh.lock(); - - if (mesh == nullptr) { - stop(); - return; - } - - std::vector newTriangles; - newTriangles.reserve(_triangles.size()); - - double shift = 0.95/_triangles.size(); - double oneTriangleTime = 1.0 - shift*_triangles.size(); - - double k = 0.0; - for(auto &t : _triangles) { - if(progress() >= shift*k) { - - if(progress() <= shift*k + oneTriangleTime) { - double triProgressLinear = (progress() - shift*k) / oneTriangleTime; - double triProgressBezier = Interpolation::Bezier(Consts::BEZIER[0], Consts::BEZIER[1], triProgressLinear); - newTriangles.emplace_back(t[0], t[1], t[1] + (t[2] - t[1]) * triProgressBezier, sf::Color(t.color().r, t.color().g, t.color().b, t.color().a*triProgressBezier)); - } else { - newTriangles.emplace_back(t[0], t[1], t[2], t.color()); - } - - } else { - newTriangles.emplace_back(t[0], t[0], t[0]); - } - - k = k + 1.0; - } - mesh->setTriangles(std::move(newTriangles)); - mesh->glFreeFloatArray(); - } - -public: - AShowCreation(std::weak_ptr mesh, double duration = 1, LoopOut looped = LoopOut::None, - InterpolationType interpolationType = InterpolationType::Bezier) : Animation(duration, looped, - interpolationType), - _mesh(mesh), _triangles(mesh.lock()->triangles()) {} -}; - -#endif //SHOOTER_ASHOWCREATION_H diff --git a/engine/animation/AShowUncreation.h b/engine/animation/AShowUncreation.h deleted file mode 100644 index 3773aef..0000000 --- a/engine/animation/AShowUncreation.h +++ /dev/null @@ -1,61 +0,0 @@ -// -// Created by Иван Ильин on 10.11.2021. -// - -#ifndef SHOOTER_ASHOWUNCREATION_H -#define SHOOTER_ASHOWUNCREATION_H - -#include "Animation.h" -#include "../Mesh.h" - -class AShowUncreation final : public Animation { -private: - const std::weak_ptr _mesh; - const std::vector _triangles; - - void update() override { - auto mesh = _mesh.lock(); - - if (mesh == nullptr) { - stop(); - return; - } - - std::vector newTriangles; - newTriangles.reserve(_triangles.size()); - - double shift = 0.95/_triangles.size(); - double oneTriangleTime = 1.0 - shift*_triangles.size(); - - double k = 0.0; - - double progress_inv = 1 - progress(); - - for(auto &t : _triangles) { - if(progress_inv >= shift*k) { - if(progress_inv <= shift*k + oneTriangleTime) { - double triProgressLinear = (progress_inv - shift*k) / oneTriangleTime; - double triProgressBezier = Interpolation::Bezier(Consts::BEZIER[0], Consts::BEZIER[1], triProgressLinear); - newTriangles.emplace_back(t[0], t[1], t[1] + (t[2] - t[1]) * triProgressBezier, sf::Color(t.color().r, t.color().g, t.color().b, t.color().a*triProgressBezier)); - } else { - newTriangles.emplace_back(t[0], t[1], t[2], t.color()); - } - - } else { - newTriangles.emplace_back(t[0], t[0], t[0]); - } - - k = k + 1.0; - } - mesh->setTriangles(std::move(newTriangles)); - mesh->glFreeFloatArray(); - } - -public: - AShowUncreation(std::weak_ptr mesh, double duration = 1, LoopOut looped = LoopOut::None, - InterpolationType interpolationType = InterpolationType::Bezier) : Animation(duration, looped, - interpolationType), - _mesh(mesh), _triangles(mesh.lock()->triangles()) {} -}; - -#endif //SHOOTER_ASHOWUNCREATION_H diff --git a/engine/animation/ATranslate.h b/engine/animation/ATranslate.h deleted file mode 100644 index 333e466..0000000 --- a/engine/animation/ATranslate.h +++ /dev/null @@ -1,36 +0,0 @@ -// -// Created by Иван Ильин on 29.01.2021. -// - -#ifndef ENGINE_ATRANSLATE_H -#define ENGINE_ATRANSLATE_H - -#include "Animation.h" -#include "../Object.h" - -class ATranslate final : public Animation { -private: - const std::weak_ptr _object; - const Vec3D _translationValue; - - void update() override { - auto obj = _object.lock(); - - if (obj == nullptr) { - stop(); - return; - } - - obj->translate(_translationValue * dprogress()); - } - -public: - ATranslate(std::weak_ptr object, const Vec3D &t, double duration = 1, LoopOut looped = LoopOut::None, - InterpolationType interpolationType = InterpolationType::Bezier) : Animation(duration, looped, - interpolationType), - _object(object), - _translationValue(t) { - } -}; - -#endif //INC_3DZAVR_ATRANSLATE_H diff --git a/engine/animation/ATranslateToPoint.h b/engine/animation/ATranslateToPoint.h deleted file mode 100644 index 1de78ef..0000000 --- a/engine/animation/ATranslateToPoint.h +++ /dev/null @@ -1,41 +0,0 @@ -// -// Created by Иван Ильин on 29.01.2021. -// - -#ifndef ENGINE_ATRANSLATETOPOINT_H -#define ENGINE_ATRANSLATETOPOINT_H - -#include "Animation.h" -#include "../Object.h" - -class ATranslateToPoint final : public Animation { -private: - const std::weak_ptr _object; - const Vec3D _targetPoint; - Vec3D _translationValue; - - bool _started = false; - - void update() override { - auto obj = _object.lock(); - - if (obj == nullptr) { - stop(); - return; - } - - if (!_started) { - _started = true; - _translationValue = _targetPoint - _object.lock()->position(); - } - obj->translate(_translationValue * dprogress()); - } - -public: - ATranslateToPoint(std::weak_ptr object, const Vec3D &p, double duration = 1, LoopOut looped = LoopOut::None, - InterpolationType interpolationType = InterpolationType::Bezier) - : Animation(duration, looped, interpolationType), _targetPoint(p), _object(object) { - } -}; - -#endif //INC_3DZAVR_ATRANSLATETOPOINT_H diff --git a/engine/animation/AWait.h b/engine/animation/AWait.h deleted file mode 100644 index 59a3a40..0000000 --- a/engine/animation/AWait.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by Иван Ильин on 29.01.2021. -// - -#ifndef ENGINE_AWAIT_H -#define ENGINE_AWAIT_H - -#include "Animation.h" - -class AWait final : public Animation { -private: - void update() override {} - -public: - explicit AWait(double duration = 1) : Animation(duration, LoopOut::None, InterpolationType::Linear, true) { - } -}; - -#endif //INC_3DZAVR_AWAIT_H diff --git a/engine/animation/Animation.cpp b/engine/animation/Animation.cpp deleted file mode 100644 index 3a47eda..0000000 --- a/engine/animation/Animation.cpp +++ /dev/null @@ -1,60 +0,0 @@ -// -// Created by Иван Ильин on 27.01.2021. -// - -#include "Animation.h" -#include "../Consts.h" -#include "../utils/Time.h" - -Animation::Animation(double duration, Animation::LoopOut looped, Animation::InterpolationType intType, bool waitForFinish) - : _duration(duration), _looped(looped), _intType(intType), _waitForFinish(waitForFinish) { -} - -bool Animation::updateState() { - if (_finished || std::abs(_duration) < Consts::EPS) { - _finished = true; - return false; - } - - // linear normalized time: - _dtime = Time::deltaTime() / _duration; - - switch (_intType) { - case InterpolationType::Bezier: - _dprogress = Interpolation::dBezier(Consts::BEZIER[0], Consts::BEZIER[1], _time, _dtime); - break; - case InterpolationType::Bouncing: - _dprogress = Interpolation::dBouncing(_time, _dtime); - break; - case InterpolationType::Linear: - _dprogress = Interpolation::dLinear(_time, _dtime); - break; - case InterpolationType::Cos: - _dprogress = Interpolation::dCos(_time, _dtime); - break; - default: - throw std::logic_error{ - "Animation::updateState: unknown interpolation type " + std::to_string(static_cast(_intType)) - }; - } - - if (_time + _dtime > 1.0) { - _dtime = 1.0 - _time; - _time = 1.0; - _dprogress = 1.0 - _progress; - _progress = 1.0; - _finished = true; - - } else { - _time += _dtime; - _progress += _dprogress; - } - - if (_looped == LoopOut::Continue && _time > 0.5) { - _time = 0.5; - } - - update(); - - return !_finished; -} diff --git a/engine/animation/Animation.h b/engine/animation/Animation.h deleted file mode 100644 index d2f6c5a..0000000 --- a/engine/animation/Animation.h +++ /dev/null @@ -1,58 +0,0 @@ -// -// Created by Иван Ильин on 26.01.2021. -// - -#ifndef ENGINE_ANIMATION_H -#define ENGINE_ANIMATION_H - -#include "Interpolation.h" - -class Animation { -public: - enum class InterpolationType { - Linear, - Cos, - Bezier, - Bouncing - }; - enum class LoopOut { - None, - Continue - }; -private: - // normalized time (from 0 to 1) - double _time = 0; - double _dtime = 0; - - bool _finished = false; - - double _progress = 0; - double _dprogress = 0; - - // If '_waitForFinish' == true then we need to finish all animation before starting this one. (for example AWait) - // In addition new animations in particular animation list will be started only after finishing this animation. - const bool _waitForFinish; - const double _duration = 0; - const LoopOut _looped = LoopOut::None; - const InterpolationType _intType = InterpolationType::Bezier; - - // You should override this method for your particular animation - virtual void update() = 0; - -protected: - [[nodiscard]] double progress() const { return _progress; } - [[nodiscard]] double dprogress() const { return _dprogress; } - - void stop() { _finished = true; } - -public: - Animation(double duration, LoopOut looped, InterpolationType intType, bool waitForFinish = false); - - virtual ~Animation() = default; - - bool updateState(); - - [[nodiscard]] bool isWaitingForFinish() const { return _waitForFinish; } -}; - -#endif //INC_3DZAVR_ANIMATION_H diff --git a/engine/animation/Animations.h b/engine/animation/Animations.h deleted file mode 100644 index 1106c3b..0000000 --- a/engine/animation/Animations.h +++ /dev/null @@ -1,26 +0,0 @@ -// -// Created by Иван Ильин on 09.11.2021. -// - -#ifndef SHOOTER_ANIMATIONS_H -#define SHOOTER_ANIMATIONS_H - -#include "Timeline.h" - -#include "AAttractToPoint.h" -#include "AColor.h" -#include "AFunction.h" -#include "ARotate.h" -#include "ARotateLeft.h" -#include "ARotateRelativePoint.h" -#include "AScale.h" -#include "ATranslate.h" -#include "ATranslateToPoint.h" -#include "AWait.h" -#include "AShowCreation.h" -#include "AShowUncreation.h" -#include "ARotateLeftUpLookAt.h" -#include "ADecompose.h" - - -#endif //SHOOTER_ANIMATIONS_H diff --git a/engine/animation/Interpolation.cpp b/engine/animation/Interpolation.cpp deleted file mode 100644 index 2c1dcfe..0000000 --- a/engine/animation/Interpolation.cpp +++ /dev/null @@ -1,71 +0,0 @@ -// -// Created by Иван Ильин on 09.11.2021. -// -#include - -#include "Interpolation.h" -#include "../Consts.h" - -double Interpolation::Linear(double t) { - if (t < 0) { - t = -t; - } - int integer = static_cast(t); - - return (integer % 2) ? 1.0 - (t - integer) : (t - integer); -} - -double Interpolation::Bezier(const Vec2D &p1, const Vec2D &p2, double t) { - // TODO: implement bezier curve without finding the root of equation - t = Interpolation::Linear(t); - - double h = Consts::EPS; - double eps = Consts::EPS; - - // We are trying to find 's' when px = t - auto f = [=](double s) { - return 3.0 * (1.0 - s) * (1.0 - s) * s * p1.x() + 3.0 * (1.0 - s) * s * s * p2.x() + s * s * s - t; - }; - // Using found 's' we will calculate resulting py - auto py = [=](double s) { - return 3.0 * (1.0 - s) * (1.0 - s) * s * p1.y() + 3.0 * (1.0 - s) * s * s * p2.y() + s * s * s; - }; - - auto df = [=](double s) { - return (f(s + h) - f(s - h)) / (2.0 * h); - }; - - // Newton method - double s1 = 0.0, s2 = 0.5; - int i = 0; - - while (std::abs(s1 - s2) > eps) { - s1 = s2; - s2 = s1 - f(s1) / df(s1); - i++; - } - - return py(s1); -} - -double Interpolation::Bouncing(double t) { - t = Interpolation::Linear(t); - return 0.5 * (1.0 / (1.0 + exp(10.0 * (-4.0 * t + 0.8))) + - (1.0 + 2.5 * sin(50.0 * (t - 1.0 / 3.0)) * exp(-7.0 * t)) / (1.0 + exp(10.0 * (-15.0 * t + 3.1)))); -} - -double Interpolation::dLinear(double t, double dt) { - return ((int) trunc(t) % 2) ? -dt : dt; -} - -double Interpolation::dCos(double t, double dt) { - return 0.5 * Consts::PI * sin(Consts::PI * t) * dt; -} - -double Interpolation::dBezier(const Vec2D &p1, const Vec2D &p2, double t, double dt) { - return Interpolation::Bezier(p1, p2, t + dt) - Interpolation::Bezier(p1, p2, t); -} - -double Interpolation::dBouncing(double t, double dt) { - return Bouncing(t + dt) - Bouncing(t); -} diff --git a/engine/animation/Interpolation.h b/engine/animation/Interpolation.h deleted file mode 100644 index 4621c0d..0000000 --- a/engine/animation/Interpolation.h +++ /dev/null @@ -1,26 +0,0 @@ -// -// Created by Иван Ильин on 26.01.2021. -// - -#ifndef ENGINE_INTERPOLATION_H -#define ENGINE_INTERPOLATION_H - -#include "../math/Vec2D.h" - -namespace Interpolation { - double Linear(double t); - - double Bezier(const Vec2D &p1, const Vec2D &p2, double t); - - double Bouncing(double t); - - double dLinear(double t, double dt); - - double dCos(double t, double dt); - - double dBezier(const Vec2D &p1, const Vec2D &p2, double t, double dt); - - double dBouncing(double t, double dt); -}; - -#endif //INC_3DZAVR_INTERPOLATION_H diff --git a/engine/animation/Timeline.cpp b/engine/animation/Timeline.cpp deleted file mode 100644 index f7f397d..0000000 --- a/engine/animation/Timeline.cpp +++ /dev/null @@ -1,96 +0,0 @@ -// -// Created by Иван Ильин on 03.10.2021. -// - -#include - -#include "Timeline.h" -#include "../utils/Log.h" - -Timeline *Timeline::_instance = nullptr; - -void Timeline::init() { - delete _instance; - _instance = new Timeline(); - - Log::log("Timeline::init(): animation timeline was initialized"); -} - -void Timeline::deleteAllAnimations() { - if (_instance == nullptr) { - return; - } - - Log::log("Timeline::deleteAllAnimations(): all " + std::to_string(_instance->_animations.size()) + " list was deleted"); - _instance->_animations.clear(); -} - -void Timeline::deleteAnimationList(const AnimationListTag &listName) { - if (_instance == nullptr) { - return; - } - - auto it = _instance->_animations.find(listName); - - if(it != _instance->_animations.end()) { - _instance->_animations.erase(it); - } else { - Log::log("Timeline::deleteAnimationList(): list '" + listName.str() + "' does not exist"); - } -} - -[[nodiscard]] bool Timeline::isInAnimList(const AnimationListTag &listName) { - if (_instance == nullptr) { - return false; - } - - auto it = _instance->_animations.find(listName); - if(it != _instance->_animations.end()) { - return !it->second.empty(); - } - - return false; -} - -void Timeline::update() { - if (_instance == nullptr) { - return; - } - - for (auto iter = _instance->_animations.begin(); iter != _instance->_animations.end(); ) { - if (iter->second.empty()) { - _instance->_animations.erase(iter++); - continue; - } - auto& animationList = iter->second; - auto it = animationList.begin(); - - // If it the front animation is 'AWait' we should wait until waiting time is over - if ((it != animationList.end()) && (*it)->isWaitingForFinish()) { - if (!(*it)->updateState()) { - animationList.erase(it); - } - ++iter; - continue; - } - - // Otherwise we iterate over all animation until we meet animations.end() or wait animation - while (!animationList.empty() && (it != animationList.end()) && (!(*it)->isWaitingForFinish())) { - if (!(*it)->updateState()) { - animationList.erase(it++); - } else { - ++it; - } - } - ++iter; - } -} - -void Timeline::free() { - Timeline::deleteAllAnimations(); - - delete _instance; - _instance = nullptr; - - Log::log("Timeline::free(): pointer to 'Timeline' was freed"); -} diff --git a/engine/animation/Timeline.h b/engine/animation/Timeline.h deleted file mode 100644 index 8ba1bdb..0000000 --- a/engine/animation/Timeline.h +++ /dev/null @@ -1,73 +0,0 @@ -// -// Created by Иван Ильин on 03.10.2021. -// - -#ifndef SHOOTER_TIMELINE_H -#define SHOOTER_TIMELINE_H - -#include -#include -#include - -#include "Animation.h" - -class AnimationListTag final { -private: - const std::string _name; -public: - explicit AnimationListTag(std::string name = "") : _name(std::move(name)) {} - - [[nodiscard]] std::string str() const { return _name; } - - bool operator==(const AnimationListTag &tag) const { return _name == tag._name; } - - bool operator!=(const AnimationListTag &tag) const { return _name != tag._name; } - - bool operator<(const AnimationListTag &tag) const { return _name < tag._name; } -}; - -class Timeline { -private: - std::map>> _animations; - - static Timeline *_instance; - - Timeline() = default; - -public: - Timeline(const Timeline &) = delete; - - Timeline &operator=(Timeline &) = delete; - - static void update(); - - static void deleteAllAnimations(); - - static void deleteAnimationList(const AnimationListTag &listName); - - [[nodiscard]] static bool isInAnimList(const AnimationListTag &listName); - - static void init(); - - static void free(); - - template - static void addAnimation(const AnimationListTag &listName, Arguments... args) { - if (_instance == nullptr) { - return; - } - - _instance->_animations[listName].emplace_back(std::make_shared(args...)); - } - - template - static void addAnimation(Arguments... args) { - if (_instance == nullptr) { - return; - } - - _instance->_animations[AnimationListTag("timeline_0")].emplace_back(std::make_shared(args...)); - } -}; - -#endif //SHOOTER_TIMELINE_H diff --git a/engine/fonts/Roboto-Light.ttf b/engine/fonts/Roboto-Light.ttf deleted file mode 100644 index 3526798..0000000 Binary files a/engine/fonts/Roboto-Light.ttf and /dev/null differ diff --git a/engine/fonts/Roboto-Medium.ttf b/engine/fonts/Roboto-Medium.ttf deleted file mode 100644 index f714a51..0000000 Binary files a/engine/fonts/Roboto-Medium.ttf and /dev/null differ diff --git a/engine/fonts/Roboto-Thin.ttf b/engine/fonts/Roboto-Thin.ttf deleted file mode 100644 index 4e797cf..0000000 Binary files a/engine/fonts/Roboto-Thin.ttf and /dev/null differ diff --git a/engine/fonts/fontRU.ttf b/engine/fonts/fontRU.ttf deleted file mode 100644 index 9466430..0000000 Binary files a/engine/fonts/fontRU.ttf and /dev/null differ diff --git a/engine/gui/Button.cpp b/engine/gui/Button.cpp deleted file mode 100644 index bc776fd..0000000 --- a/engine/gui/Button.cpp +++ /dev/null @@ -1,62 +0,0 @@ -// -// Created by Иван Ильин on 26.03.2021. -// - -#include - -#include "Button.h" -#include "../utils/ResourceManager.h" - -void Button::select() { - if (!_selected && !_pressed) { - _button.setTextureRect(sf::IntRect(_selectedState.tx, _selectedState.ty, _w, _h)); - _selected = true; - } -} - -void Button::unSelect() { - if (_selected && !_pressed) { - _button.setTextureRect(sf::IntRect(_usualState.tx, _usualState.ty, _w, _h)); - _selected = false; - } -} - -void Button::press() { - if (!_pressed) { - _button.setTextureRect(sf::IntRect(_pressedState.tx, _pressedState.ty, _w, _h)); - if (_checkBox) { - _pressed = true; - } - _click(); - } else { - _button.setTextureRect(sf::IntRect(_usualState.tx, _usualState.ty, _w, _h)); - if (_checkBox) { - _pressed = false; - } - } -} - -void Button::init() { - _button.setTexture(*ResourceManager::loadTexture(_texture)); - _button.setTextureRect(sf::IntRect(_usualState.tx, _usualState.ty, _w, _h)); - _button.scale(static_cast(_sx), static_cast(_sy)); - _button.setPosition(static_cast(_x) - static_cast(_w * _sx) / 2.0f, - static_cast(_y) - static_cast(_h * _sy) / 2.0f); - - _text.setFont(*ResourceManager::loadFont(_font)); - _text.setString(_textString); - _text.setCharacterSize(static_cast((_h * _sy) / 2)); - _text.setFillColor(_textColor); - _text.setPosition(static_cast(_x) - _text.getLocalBounds().width / 2.0f, - static_cast(_y) - static_cast(_h * _sy) / 2.0f + _text.getLocalBounds().height / 4.0f); -} - -Button::Button(int x, int y, int width, int height, std::function click, std::string text, double sx, - double sy, std::string texture, tPos usualState, tPos selectedState, tPos pressedState, - std::string font, sf::Color textColor) : _x(x), _y(y), _w(width), _h(height), _click(std::move(click)), - _textString(std::move(text)), _sx(sx), _sy(sy), - _texture(std::move(texture)), _usualState(usualState), - _selectedState(selectedState), _pressedState(pressedState), - _font(std::move(font)), _textColor(textColor) { - -} diff --git a/engine/gui/Button.h b/engine/gui/Button.h deleted file mode 100644 index f6be041..0000000 --- a/engine/gui/Button.h +++ /dev/null @@ -1,80 +0,0 @@ -// -// Created by Иван Ильин on 26.03.2021. -// - -#ifndef ENGINE_BUTTON_H -#define ENGINE_BUTTON_H - -#include - -#include - -struct tPos final { - const int tx; - const int ty; -}; - -class Button final { -private: - const int _x{}; - const int _y{}; - - const int _w{}; - const int _h{}; - - const std::function _click; - - const std::string _textString; - - const double _sx{}; - const double _sy{}; - - const std::string _texture; - const tPos _usualState{}; - const tPos _selectedState{}; - const tPos _pressedState{}; - - const std::string _font; - const sf::Color _textColor; - - sf::Sprite _button; - sf::Text _text; - - bool _selected = false; - bool _pressed = false; - bool _checkBox = false; - -public: - Button() = default; - - Button(int x, int y, int width, int height, std::function click, std::string text, double sx, double sy, - std::string texture, tPos usualState, tPos selectedState, tPos pressedState, std::string font, - sf::Color textColor); - - void select(); - - void unSelect(); - - void press(); - - void init(); - - [[nodiscard]] int x() const { return _x; } - - [[nodiscard]] int y() const { return _y; } - - [[nodiscard]] int w() const { return _w; } - - [[nodiscard]] int h() const { return _h; } - - [[nodiscard]] double sx() const { return _sx; } - - [[nodiscard]] double sy() const { return _sy; } - - [[nodiscard]] sf::Sprite const &sprite() const { return _button; } - - [[nodiscard]] sf::Text const &text() const { return _text; } -}; - - -#endif //MINECRAFT_3DZAVR_BUTTON_H diff --git a/engine/gui/Window.cpp b/engine/gui/Window.cpp deleted file mode 100644 index 024f145..0000000 --- a/engine/gui/Window.cpp +++ /dev/null @@ -1,57 +0,0 @@ -// -// Created by Иван Ильин on 26.03.2021. -// - -#include - -#include "Window.h" -#include "../utils/ResourceManager.h" - -void Window::addButton(int x, int y, int w, int h, std::function click, const std::string &text, double sx, - double sy, - const std::string &texture, tPos usualState, tPos selectedState, tPos pressedState, - const std::string &font, sf::Color textColor) { - _buttons.emplace_back(x, y, w, h, std::move(click), text, sx, sy, texture, usualState, selectedState, pressedState, - font, textColor); - _buttons.back().init(); -} - -void Window::update() { - - _screen->setTitle(_name); - _screen->drawSprite(_back); - - Vec2D mousePos = _mouse->getMousePosition(); - Vec2D dMousePos = mousePos - _prevMousePosition; - _prevMousePosition = mousePos; - _back.setPosition(_back.getPosition() - sf::Vector2f(static_cast(dMousePos.x()) / 30.0f, - static_cast(dMousePos.y()) / 30.0f)); - bool isPressed = _mouse->isButtonTapped(sf::Mouse::Left); - - for (auto &button : _buttons) { - if (mousePos.x() > button.x() - button.w() * button.sx() / 2.0f && - mousePos.y() > button.y() - button.h() * button.sy() / 2.0f && - mousePos.x() < button.x() + button.w() * button.sx() / 2.0f && - mousePos.y() < button.y() + button.h() * button.sy() / 2.0f) { - button.select(); - if (isPressed) - button.press(); - } else { - button.unSelect(); - } - - if (_screen->isOpen()) { - _screen->drawSprite(button.sprite()); - _screen->drawText(button.text()); - } - } -} - -void Window::setBackgroundTexture(const std::string &texture, double sx, double sy, int w, int h) { - _backTexture = texture; - std::shared_ptr t = ResourceManager::loadTexture(_backTexture); - t->setRepeated(true); - _back = sf::Sprite(*t, sf::IntRect(0, 0, static_cast(w + w / 30.0), static_cast(h + h / 30.0))); - _back.scale((float) sx, (float) sy); - _back.setPosition(sf::Vector2f(static_cast(-w) / 30.0f, static_cast(-h) / 30.0f)); -} diff --git a/engine/gui/Window.h b/engine/gui/Window.h deleted file mode 100644 index 63883cd..0000000 --- a/engine/gui/Window.h +++ /dev/null @@ -1,47 +0,0 @@ -// -// Created by Иван Ильин on 26.03.2021. -// - -#ifndef ENGINE_WINDOW_H -#define ENGINE_WINDOW_H - -#include -#include - -#include "Button.h" -#include "../io/Screen.h" -#include "../io/Mouse.h" - -class Window final { -private: - std::string _name; - std::string _backTexture; - std::vector