From 56b8daa0bf7e7c1117054ef89564d36715fd52b2 Mon Sep 17 00:00:00 2001 From: bailwillharr Date: Tue, 2 May 2023 12:02:43 +0100 Subject: [PATCH] more reformatting --- .clang-format | 2 +- include/scene.h | 2 +- include/systems/collisions.h | 4 +- src/scene.cpp | 6 +- test/src/camera_controller.cpp | 278 ++++++++++++++++----------------- test/src/camera_controller.hpp | 56 ++++--- test/src/game.cpp | 228 ++++++++++++++------------- test/src/game.hpp | 11 +- test/src/main.cpp | 60 ++++--- test/src/meshgen.cpp | 237 ++++++++++++++-------------- test/src/meshgen.hpp | 16 +- test/src/terrain.cpp | 21 --- test/src/terrain.hpp | 9 -- 13 files changed, 456 insertions(+), 474 deletions(-) delete mode 100644 test/src/terrain.cpp delete mode 100644 test/src/terrain.hpp diff --git a/.clang-format b/.clang-format index e1a4576..5347876 100644 --- a/.clang-format +++ b/.clang-format @@ -193,7 +193,7 @@ RemoveBracesLLVM: false RequiresClausePosition: OwnLine SeparateDefinitionBlocks: Leave ShortNamespaceLines: 1 -SortIncludes: CaseSensitive +SortIncludes: false SortJavaStaticImport: Before SortUsingDeclarations: true SpaceAfterCStyleCast: false diff --git a/include/scene.h b/include/scene.h index 53a368b..262e1c4 100644 --- a/include/scene.h +++ b/include/scene.h @@ -35,7 +35,7 @@ class Scene { size_t GetComponentSignaturePosition(size_t hash); template - void registerComponent() { + void RegisterComponent() { size_t hash = typeid(T).hash_code(); assert(component_arrays_.contains(hash) == false && "Registering component type more than once."); diff --git a/include/systems/collisions.h b/include/systems/collisions.h index 657dca0..74c0c5d 100644 --- a/include/systems/collisions.h +++ b/include/systems/collisions.h @@ -2,9 +2,10 @@ #define ENGINE_INCLUDE_SYSTEMS_COLLISIONS_H_ #include -#include #include +#include + #include "components/collider.h" #include "ecs_system.h" @@ -28,7 +29,6 @@ class PhysicsSystem : public System { private: // dynamic arrays to avoid realloc on every frame - // entity, aabb, is_trigger std::vector> static_aabbs_{}; std::vector> dynamic_aabbs_{}; diff --git a/src/scene.cpp b/src/scene.cpp index 3c84263..ecdc5c3 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -17,9 +17,9 @@ namespace engine { // ecs configuration: - registerComponent(); - registerComponent(); - registerComponent(); + RegisterComponent(); + RegisterComponent(); + RegisterComponent(); // Order here matters: RegisterSystem(); diff --git a/test/src/camera_controller.cpp b/test/src/camera_controller.cpp index 7da39c1..959ac85 100644 --- a/test/src/camera_controller.cpp +++ b/test/src/camera_controller.cpp @@ -1,183 +1,175 @@ #include "camera_controller.hpp" -#include "application.h" -#include "window.h" -#include "input_manager.h" -#include "scene_manager.h" -#include "scene.h" - -#include "components/transform.h" -#include "components/collider.h" -#include "log.h" - -#include #include #include #include +#include + +#include "application.h" +#include "components/collider.h" +#include "components/transform.h" +#include "input_manager.h" +#include "log.h" +#include "scene.h" +#include "scene_manager.h" +#include "window.h" CameraControllerSystem::CameraControllerSystem(engine::Scene* scene) - : System(scene, { typeid(engine::TransformComponent).hash_code(), typeid(CameraControllerComponent).hash_code() }) -{ -} + : System(scene, {typeid(engine::TransformComponent).hash_code(), + typeid(CameraControllerComponent).hash_code()}) {} -void CameraControllerSystem::OnUpdate(float ts) -{ +void CameraControllerSystem::OnUpdate(float ts) { + if (t == nullptr || c == nullptr || col == nullptr) { + for (uint32_t entity : entities_) { + t = scene_->GetComponent(entity); + col = scene_->GetComponent(entity); + c = scene_->GetComponent(entity); + break; + } + if (t == nullptr) return; + if (c == nullptr) return; + if (col == nullptr) return; + } - if (t == nullptr || c == nullptr || col == nullptr) { - for (uint32_t entity : entities_) { - t = scene_->GetComponent(entity); - col = scene_->GetComponent(entity); - c = scene_->GetComponent(entity); - break; - } - if (t == nullptr) return; - if (c == nullptr) return; - if (col == nullptr) return; - } - // calculate new position + const float dt = ts; - // use one unit per meter + constexpr float G = 9.8f; + const float kMaxSlopeAngle = glm::cos(glm::radians(20.0f)); + constexpr float kFloorSinkLevel = + 0.05f; // how far into the floor to ground the player - const float dt = ts; + glm::vec3 norm = c->last_collision_normal; - constexpr float G = 9.8f; - const float MAX_SLOPE_ANGLE = glm::cos(glm::radians(20.0f)); -// constexpr float MAX_SLOPE_ANGLE = glm::radians(1000.0f); // treat every collider as a floor, (TODO: get wall collisions working so this can be removed) - constexpr float FLOOR_SINK_LEVEL = 0.05f; // how far into the floor to ground the player + glm::vec3 dir = + glm::normalize(glm::rotateY(glm::vec3{1.0f, 0.0f, 0.0f}, c->yaw) + + glm::rotateY(glm::vec3{0.0f, 0.0f, 1.0f}, c->yaw)); + const float slope = glm::dot(dir, norm); - glm::vec3 norm = c->lastCollisionNormal; + bool is_sliding = false; - glm::vec3 dir = glm::normalize(glm::rotateY(glm::vec3{ 1.0f, 0.0f, 0.0f }, c->m_yaw) + glm::rotateY(glm::vec3{ 0.0f, 0.0f, 1.0f }, c->m_yaw)); - const float slope = glm::dot(dir, norm); + if (c->just_collided) { + if (slope > kMaxSlopeAngle) { + // slide across wall + is_sliding = true; + } else { + if (c->dy < 0.0f && c->is_grounded == false) { + // in the ground, push up a bit + float floorY = c->last_collision_point.y; + t->position.y = floorY + 1.5f - kFloorSinkLevel; + c->dy = 0.0f; + c->is_grounded = true; + } + } + } - bool isSliding = false; + if (c->just_collided == false && slope <= kMaxSlopeAngle) { + // just stopped colliding with a floor collider + c->is_grounded = false; + } - if (c->justCollided) { - if (slope > MAX_SLOPE_ANGLE) { - // slide across wall - isSliding = true; - } else { - if (c->dy < 0.0f && c->isGrounded == false) { - // in the ground, push up a bit - float floorY = c->lastCollisionPoint.y; - t->position.y = floorY + 1.5f - FLOOR_SINK_LEVEL; - c->dy = 0.0f; - c->isGrounded = true; - } - } - } + if (c->is_grounded == false) c->dy -= G * dt; - if (c->justCollided == false && slope <= MAX_SLOPE_ANGLE) { - // just stopped colliding with a floor collider - c->isGrounded = false; - } + // jumping + constexpr float JUMPVEL = + (float)2.82231110971133017648; // std::sqrt(2 * G * JUMPHEIGHT); + if (scene_->app()->input_manager()->GetButton("jump") && + c->is_grounded == true) { + c->dy = JUMPVEL; + } - if (c->isGrounded == false) - c->dy -= G * dt; + if (scene_->app()->window()->GetButton(engine::inputs::MouseButton::M_LEFT)) { + c->dy += dt * c->kThrust; + } - // jumping - constexpr float JUMPVEL = (float)2.82231110971133017648; //std::sqrt(2 * G * JUMPHEIGHT); - if (scene_->app()->input_manager()->GetButton("jump") && c->isGrounded == true) { - c->dy = JUMPVEL; - } + // in metres per second + float speed = c->kWalkSpeed; + if (scene_->app()->input_manager()->GetButton("sprint")) speed *= 10.0f; - if (scene_->app()->window()->GetButton(engine::inputs::MouseButton::M_LEFT)) { - c->dy += dt * c->thrust; - } + float dx = scene_->app()->input_manager()->GetAxis("movex"); + float dz = (-scene_->app()->input_manager()->GetAxis("movey")); - // in metres per second - float SPEED = c->walk_speed; - if (scene_->app()->input_manager()->GetButton("sprint")) SPEED *= 10.0f; + // calculate new pitch and yaw - float dx = scene_->app()->input_manager()->GetAxis("movex"); - float dz = (-scene_->app()->input_manager()->GetAxis("movey")); + constexpr float kMaxPitch = glm::half_pi(); + constexpr float kMinPitch = -kMaxPitch; - // calculate new pitch and yaw + float d_pitch = scene_->app()->input_manager()->GetAxis("looky") * -1.0f * + c->kCameraSensitivity; + c->pitch += d_pitch; + if (c->pitch <= kMinPitch || c->pitch >= kMaxPitch) { + c->pitch -= d_pitch; + } + c->yaw += scene_->app()->input_manager()->GetAxis("lookx") * -1.0f * + c->kCameraSensitivity; - constexpr float MAX_PITCH = glm::half_pi(); - constexpr float MIN_PITCH = -MAX_PITCH; + // update position relative to camera direction in xz plane + const glm::vec3 d2x_rotated = glm::rotateY(glm::vec3{dx, 0.0f, 0.0f}, c->yaw); + const glm::vec3 d2z_rotated = glm::rotateY(glm::vec3{0.0f, 0.0f, dz}, c->yaw); + glm::vec3 h_vel = (d2x_rotated + d2z_rotated); + if (is_sliding) { + h_vel = glm::vec3{norm.z, 0.0f, -norm.x}; + } + h_vel *= speed; + t->position += h_vel * dt; + t->position.y += c->dy * dt; - float dPitch = scene_->app()->input_manager()->GetAxis("looky") * -1.0f * c->m_cameraSensitivity; - c->m_pitch += dPitch; - if (c->m_pitch <= MIN_PITCH || c->m_pitch >= MAX_PITCH) { - c->m_pitch -= dPitch; - } - c->m_yaw += scene_->app()->input_manager()->GetAxis("lookx") * -1.0f * c->m_cameraSensitivity; + constexpr float kMaxDistanceFromOrigin = 10000.0f; - // update position relative to camera direction in xz plane - const glm::vec3 d2xRotated = glm::rotateY(glm::vec3{ dx, 0.0f, 0.0f }, c->m_yaw); - const glm::vec3 d2zRotated = glm::rotateY(glm::vec3{ 0.0f, 0.0f, dz }, c->m_yaw); - glm::vec3 hVel = (d2xRotated + d2zRotated); - if (isSliding) { - hVel = glm::vec3{norm.z, 0.0f, -norm.x}; - } - hVel *= SPEED; - t->position += hVel * dt; - t->position.y += c->dy * dt; + if (glm::length(t->position) > kMaxDistanceFromOrigin) { + t->position = {0.0f, 5.0f, 0.0f}; + c->dy = 0.0f; + } - constexpr float MAX_DISTANCE_FROM_ORIGIN = 10000.0f; + /* ROTATION STUFF */ - if (glm::length(t->position) > MAX_DISTANCE_FROM_ORIGIN) { - t->position = { 0.0f, 5.0f, 0.0f }; - c->dy = 0.0f; - } + // pitch quaternion + const float half_pitch = c->pitch / 2.0f; + glm::quat pitch_quat{}; + pitch_quat.x = glm::sin(half_pitch); + pitch_quat.y = 0.0f; + pitch_quat.z = 0.0f; + pitch_quat.w = glm::cos(half_pitch); - /* ROTATION STUFF */ + // yaw quaternion + const float half_yaw = c->yaw / 2.0f; + glm::quat yaw_quat{}; + yaw_quat.x = 0.0f; + yaw_quat.y = glm::sin(half_yaw); + yaw_quat.z = 0.0f; + yaw_quat.w = glm::cos(half_yaw); - // pitch quaternion - const float halfPitch = c->m_pitch / 2.0f; - glm::quat pitchQuat{}; - pitchQuat.x = glm::sin(halfPitch); - pitchQuat.y = 0.0f; - pitchQuat.z = 0.0f; - pitchQuat.w = glm::cos(halfPitch); + // update rotation + t->rotation = yaw_quat * pitch_quat; - // yaw quaternion - const float halfYaw = c->m_yaw / 2.0f; - glm::quat yawQuat{}; - yawQuat.x = 0.0f; - yawQuat.y = glm::sin(halfYaw); - yawQuat.z = 0.0f; - yawQuat.w = glm::cos(halfYaw); + /* user interface inputs */ - // update rotation - t->rotation = yawQuat * pitchQuat; + if (scene_->app()->window()->GetKeyPress(engine::inputs::Key::K_P)) { + std::string pos_string{"x: " + std::to_string(t->position.x) + + " y: " + std::to_string(t->position.y) + + " z: " + std::to_string(t->position.z)}; + LOG_INFO("position {}", pos_string); + } - + if (scene_->app()->window()->GetKeyPress(engine::inputs::Key::K_R)) { + t->position = {0.0f, 5.0f, 0.0f}; + c->dy = 0.0f; + } - /* user interface inputs */ + if (scene_->app()->input_manager()->GetButtonPress("fullscreen")) { + scene_->app()->window()->ToggleFullscreen(); + } - if (scene_->app()->window()->GetKeyPress(engine::inputs::Key::K_P)) { - std::string pos_string{ - "x: " + std::to_string(t->position.x) + - " y: " + std::to_string(t->position.y) + - " z: " + std::to_string(t->position.z) - }; - //scene_->app()->window()->InfoBox("POSITION", pos_string); - LOG_INFO("position: " + pos_string); - } - - if (scene_->app()->window()->GetKeyPress(engine::inputs::Key::K_R)) { - t->position = { 0.0f, 5.0f, 0.0f }; - c->dy = 0.0f; - } - - if (scene_->app()->input_manager()->GetButtonPress("fullscreen")) { - scene_->app()->window()->ToggleFullscreen(); - } - - if (scene_->app()->input_manager()->GetButtonPress("exit")) { - scene_->app()->window()->SetCloseFlag(); - } - - c->justCollided = false; + if (scene_->app()->input_manager()->GetButtonPress("exit")) { + scene_->app()->window()->SetCloseFlag(); + } + c->just_collided = false; } // called once per frame -void CameraControllerSystem::OnEvent(engine::PhysicsSystem::CollisionEvent info) -{ - c->justCollided = info.is_collision_enter; - c->lastCollisionNormal = info.normal; - c->lastCollisionPoint = info.point; +void CameraControllerSystem::OnEvent( + engine::PhysicsSystem::CollisionEvent info) { + c->just_collided = info.is_collision_enter; + c->last_collision_normal = info.normal; + c->last_collision_point = info.point; } diff --git a/test/src/camera_controller.hpp b/test/src/camera_controller.hpp index 5a00c72..5ddfe3a 100644 --- a/test/src/camera_controller.hpp +++ b/test/src/camera_controller.hpp @@ -1,41 +1,45 @@ -#pragma once +#ifndef ENGINE_TEST_SRC_CAMERA_CONTROLLER_H_ +#define ENGINE_TEST_SRC_CAMERA_CONTROLLER_H_ -#include "ecs_system.h" -#include "event_system.h" +#include #include "components/transform.h" +#include "ecs_system.h" +#include "event_system.h" #include "systems/collisions.h" struct CameraControllerComponent { - float m_cameraSensitivity = 0.007f; + static constexpr float kWalkSpeed = 4.0f; + static constexpr float kThrust = 25.0f; + static constexpr float kCameraSensitivity = 0.007f; - float m_yaw = 0.0f; - float m_pitch = 0.0f; + float yaw = 0.0f; + float pitch = 0.0f; + float dy = 0.0f; - const float walk_speed = 4.0f; - - bool isGrounded = false; - float dy = 0.0f; - float standingHeight = 0.0f; - const float thrust = 25.0f; + glm::vec3 last_collision_normal{}; + glm::vec3 last_collision_point{}; + + bool just_collided = false; + bool is_grounded = false; - glm::vec3 lastCollisionNormal{}; - glm::vec3 lastCollisionPoint{}; - bool justCollided = false; }; -class CameraControllerSystem : public engine::System, public engine::EventHandler { +class CameraControllerSystem + : public engine::System, + public engine::EventHandler { + public: + CameraControllerSystem(engine::Scene* scene); -public: - CameraControllerSystem(engine::Scene* scene); + // engine::System overrides + void OnUpdate(float ts) override; - // engine::System overrides - void OnUpdate(float ts) override; + // engine::EventHandler overrides + void OnEvent(engine::PhysicsSystem::CollisionEvent info) override; - // engine::EventHandler overrides - void OnEvent(engine::PhysicsSystem::CollisionEvent info) override; - - engine::TransformComponent* t = nullptr; - engine::ColliderComponent* col = nullptr; - CameraControllerComponent* c = nullptr; + engine::TransformComponent* t = nullptr; + engine::ColliderComponent* col = nullptr; + CameraControllerComponent* c = nullptr; }; + +#endif \ No newline at end of file diff --git a/test/src/game.cpp b/test/src/game.cpp index 5ea8140..325d7db 100644 --- a/test/src/game.cpp +++ b/test/src/game.cpp @@ -1,132 +1,148 @@ #include "game.hpp" -#include "config.h" -#include "camera_controller.hpp" -#include "meshgen.hpp" #include "application.h" -#include "window.h" -#include "input_manager.h" -#include "scene_manager.h" -#include "scene.h" -#include "components/transform.h" +#include "camera_controller.hpp" #include "components/collider.h" #include "components/renderable.h" -#include "systems/transform.h" -#include "systems/render.h" +#include "components/transform.h" +#include "input_manager.h" +#include "meshgen.hpp" #include "resources/material.h" #include "resources/texture.h" +#include "scene.h" +#include "scene_manager.h" +#include "systems/render.h" +#include "systems/transform.h" #include "util/model_loader.h" +#include "window.h" -static void configureInputs(engine::InputManager* inputManager) -{ - // user interface mappings - inputManager->AddInputButton("fullscreen", engine::inputs::Key::K_F11); - inputManager->AddInputButton("exit", engine::inputs::Key::K_ESCAPE); - // game buttons - inputManager->AddInputButton("fire", engine::inputs::MouseButton::M_LEFT); - inputManager->AddInputButton("aim", engine::inputs::MouseButton::M_RIGHT); - inputManager->AddInputButton("jump", engine::inputs::Key::K_SPACE); - inputManager->AddInputButton("sprint", engine::inputs::Key::K_LSHIFT); - // game movement - inputManager->AddInputButtonAsAxis("movex", engine::inputs::Key::K_D, engine::inputs::Key::K_A); - inputManager->AddInputButtonAsAxis("movey", engine::inputs::Key::K_W, engine::inputs::Key::K_S); - // looking around - inputManager->AddInputAxis("lookx", engine::inputs::MouseAxis::X); - inputManager->AddInputAxis("looky", engine::inputs::MouseAxis::Y); +#include "config.h" + +static void ConfigureInputs(engine::InputManager* input_manager) { + // user interface mappings + input_manager->AddInputButton("fullscreen", engine::inputs::Key::K_F11); + input_manager->AddInputButton("exit", engine::inputs::Key::K_ESCAPE); + // game buttons + input_manager->AddInputButton("fire", engine::inputs::MouseButton::M_LEFT); + input_manager->AddInputButton("aim", engine::inputs::MouseButton::M_RIGHT); + input_manager->AddInputButton("jump", engine::inputs::Key::K_SPACE); + input_manager->AddInputButton("sprint", engine::inputs::Key::K_LSHIFT); + // game movement + input_manager->AddInputButtonAsAxis("movex", engine::inputs::Key::K_D, + engine::inputs::Key::K_A); + input_manager->AddInputButtonAsAxis("movey", engine::inputs::Key::K_W, + engine::inputs::Key::K_S); + // looking around + input_manager->AddInputAxis("lookx", engine::inputs::MouseAxis::X); + input_manager->AddInputAxis("looky", engine::inputs::MouseAxis::Y); } -void playGame(GameSettings settings) -{ - LOG_INFO("FPS limiter: {}", settings.enableFrameLimiter ? "ON" : "OFF"); - LOG_INFO("Graphics Validation: {}", settings.enableValidation ? "ON" : "OFF"); +void PlayGame(GameSettings settings) { + LOG_INFO("FPS limiter: {}", settings.enable_frame_limiter ? "ON" : "OFF"); + LOG_INFO("Graphics Validation: {}", + settings.enable_validation ? "ON" : "OFF"); - engine::gfx::GraphicsSettings graphicsSettings{}; - graphicsSettings.enable_validation = settings.enableValidation; - graphicsSettings.vsync = true; - graphicsSettings.wait_for_present = false; - graphicsSettings.msaa_level = engine::gfx::MSAALevel::kOff; - engine::Application app(PROJECT_NAME, PROJECT_VERSION, graphicsSettings); + engine::gfx::GraphicsSettings graphics_settings{}; + graphics_settings.enable_validation = settings.enable_validation; + graphics_settings.vsync = true; + graphics_settings.wait_for_present = false; + graphics_settings.msaa_level = engine::gfx::MSAALevel::kOff; + engine::Application app(PROJECT_NAME, PROJECT_VERSION, graphics_settings); - app.SetFrameLimiter(settings.enableFrameLimiter); + app.SetFrameLimiter(settings.enable_frame_limiter); - // configure window - app.window()->SetRelativeMouseMode(true); - - configureInputs(app.input_manager()); + // configure window + app.window()->SetRelativeMouseMode(true); - auto myScene = app.scene_manager()->CreateEmptyScene(); + ConfigureInputs(app.input_manager()); - /* create camera */ - { - myScene->registerComponent(); - myScene->RegisterSystem(); + auto my_scene = app.scene_manager()->CreateEmptyScene(); - auto camera = myScene->CreateEntity("camera"); - myScene->GetComponent(camera)->position = { 0.0f, 10.0f, 0.0f }; - auto cameraCollider = myScene->AddComponent(camera); - cameraCollider->is_static = false; - cameraCollider->is_trigger = true; - cameraCollider->aabb = { { -0.2f, -1.5f, -0.2f }, { 0.2f, 0.2f, 0.2f} }; // Origin is at eye level - myScene->AddComponent(camera); - myScene->event_system()->SubscribeToEventType( - engine::EventSubscriberKind::kEntity, camera, myScene->GetSystem() - ); + /* create camera */ + { + my_scene->RegisterComponent(); + my_scene->RegisterSystem(); - auto renderSystem = myScene->GetSystem(); - renderSystem->SetCameraEntity(camera); - } + auto camera = my_scene->CreateEntity("camera"); + my_scene->GetComponent(camera)->position = { + 0.0f, 10.0f, 0.0f}; + auto camer_collider = + my_scene->AddComponent(camera); + camer_collider->is_static = false; + camer_collider->is_trigger = true; + camer_collider->aabb = {{-0.2f, -1.5f, -0.2f}, + {0.2f, 0.2f, 0.2f}}; // Origin is at eye level + my_scene->AddComponent(camera); + my_scene->event_system() + ->SubscribeToEventType( + engine::EventSubscriberKind::kEntity, camera, + my_scene->GetSystem()); - /* shared resources */ - auto grassTexture = std::make_shared( - &app.render_data_, - app.GetResourcePath("textures/grass.jpg"), - engine::resources::Texture::Filtering::kAnisotropic - ); - auto spaceTexture = std::make_shared( - &app.render_data_, - app.GetResourcePath("textures/space2.png"), - engine::resources::Texture::Filtering::kAnisotropic - ); + auto render_system = my_scene->GetSystem(); + render_system->SetCameraEntity(camera); + } - /* cube */ - { - uint32_t cube = myScene->CreateEntity("cube"); - myScene->GetComponent(cube)->position = glm::vec3{ -0.5f + 5.0f, -0.5f + 5.0f, -0.5f + 5.0f }; - auto cubeRenderable = myScene->AddComponent(cube); - cubeRenderable->material = std::make_shared(app.GetResource("builtin.standard")); - cubeRenderable->material->texture_ = app.GetResource("builtin.white"); - cubeRenderable->mesh = genCuboidMesh(app.gfxdev(), 1.0f, 1.0f, 1.0f, 1); - auto cubeCollider = myScene->AddComponent(cube); - cubeCollider->is_static = true; - cubeCollider->aabb = { { 0.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, 1.0f } }; - } + /* shared resources */ + auto grass_texture = std::make_shared( + &app.render_data_, app.GetResourcePath("textures/grass.jpg"), + engine::resources::Texture::Filtering::kAnisotropic); + auto space_texture = std::make_shared( + &app.render_data_, app.GetResourcePath("textures/space2.png"), + engine::resources::Texture::Filtering::kAnisotropic); - /* floor */ - { - uint32_t floor = myScene->CreateEntity("floor"); - myScene->GetComponent(floor)->position = glm::vec3{-5000.0f, -1.0f, -5000.0f}; - auto floorRenderable = myScene->AddComponent(floor); - floorRenderable->material = std::make_shared(app.GetResource("builtin.standard")); - floorRenderable->material->texture_ = grassTexture; - floorRenderable->mesh = genCuboidMesh(app.gfxdev(), 10000.0f, 1.0f, 10000.0f, 5000.0f); - floorRenderable->shown = true; - auto floorCollider = myScene->AddComponent(floor); - floorCollider->is_static = true; - floorCollider->aabb = { { 0.0f, 0.0f, 0.0f }, { 10000.0f, 1.0f, 10000.0f } }; - } + /* cube */ + { + uint32_t cube = my_scene->CreateEntity("cube"); + my_scene->GetComponent(cube)->position = + glm::vec3{-0.5f + 5.0f, -0.5f + 5.0f, -0.5f + 5.0f}; + auto cube_renderable = + my_scene->AddComponent(cube); + cube_renderable->material = std::make_shared( + app.GetResource("builtin.standard")); + cube_renderable->material->texture_ = + app.GetResource("builtin.white"); + cube_renderable->mesh = GenCuboidMesh(app.gfxdev(), 1.0f, 1.0f, 1.0f, 1); + auto cube_collider = + my_scene->AddComponent(cube); + cube_collider->is_static = true; + cube_collider->aabb = {{0.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f}}; + } - //engine::util::LoadMeshFromFile(myScene, app.GetResourcePath("models/astronaut/astronaut.dae")); + /* floor */ + { + uint32_t floor = my_scene->CreateEntity("floor"); + my_scene->GetComponent(floor)->position = + glm::vec3{-5000.0f, -1.0f, -5000.0f}; + auto floor_renderable = + my_scene->AddComponent(floor); + floor_renderable->material = std::make_shared( + app.GetResource("builtin.standard")); + floor_renderable->material->texture_ = grass_texture; + floor_renderable->mesh = + GenCuboidMesh(app.gfxdev(), 10000.0f, 1.0f, 10000.0f, 5000.0f); + floor_renderable->shown = true; + auto floor_collider = + my_scene->AddComponent(floor); + floor_collider->is_static = true; + floor_collider->aabb = {{0.0f, 0.0f, 0.0f}, {10000.0f, 1.0f, 10000.0f}}; + } - /* skybox */ - { - uint32_t skybox = myScene->CreateEntity("skybox"); - auto skyboxRenderable = myScene->AddComponent(skybox); - skyboxRenderable->material = std::make_unique(app.GetResource("builtin.skybox")); - skyboxRenderable->material->texture_ = spaceTexture; - skyboxRenderable->mesh = genCuboidMesh(app.gfxdev(), 10.0f, 10.0f, 10.0f, 1.0f, true); - myScene->GetComponent(skybox)->position = { -5.0f, -5.0f, -5.0f }; - } + engine::util::LoadMeshFromFile( + my_scene, app.GetResourcePath("models/astronaut/astronaut.dae")); - app.GameLoop(); + /* skybox */ + { + uint32_t skybox = my_scene->CreateEntity("skybox"); + auto skybox_renderable = + my_scene->AddComponent(skybox); + skybox_renderable->material = std::make_unique( + app.GetResource("builtin.skybox")); + skybox_renderable->material->texture_ = space_texture; + skybox_renderable->mesh = + GenCuboidMesh(app.gfxdev(), 10.0f, 10.0f, 10.0f, 1.0f, true); + my_scene->GetComponent(skybox)->position = { + -5.0f, -5.0f, -5.0f}; + } + app.GameLoop(); } diff --git a/test/src/game.hpp b/test/src/game.hpp index b6a47d5..0b5864d 100644 --- a/test/src/game.hpp +++ b/test/src/game.hpp @@ -1,8 +1,11 @@ -#pragma once +#ifndef ENGINE_TEST_SRC_GAME_H_ +#define ENGINE_TEST_SRC_GAME_H_ struct GameSettings { - bool enableFrameLimiter; - bool enableValidation; + bool enable_frame_limiter; + bool enable_validation; }; -void playGame(GameSettings settings); +void PlayGame(GameSettings settings); + +#endif \ No newline at end of file diff --git a/test/src/main.cpp b/test/src/main.cpp index 4e7fee1..420af2d 100644 --- a/test/src/main.cpp +++ b/test/src/main.cpp @@ -1,42 +1,38 @@ -#include "config.h" -#include "game.hpp" +#include + +#include +#include -// engine #include "logger.h" #include "window.h" -// standard library -#include -#include -#include +#include "config.h" +#include "game.hpp" -int main(int argc, char* argv[]) -{ +int main(int argc, char* argv[]) { + GameSettings settings{}; + settings.enable_frame_limiter = true; + settings.enable_validation = false; + if (argc >= 2) { + std::unordered_set args{}; + for (int i = 1; i < argc; i++) { + args.insert(std::string(argv[i])); + } + if (args.contains("nofpslimit")) settings.enable_frame_limiter = false; + if (args.contains("gpuvalidation")) settings.enable_validation = true; + } - GameSettings settings{}; - settings.enableFrameLimiter = true; - settings.enableValidation = false; - if (argc >= 2) { - std::unordered_set args{}; - for (int i = 1; i < argc; i++) { - args.insert(std::string(argv[i])); - } - if (args.contains("nofpslimit")) settings.enableFrameLimiter = false; - if (args.contains("gpuvalidation")) settings.enableValidation = true; - } + engine::SetupLog(PROJECT_NAME); - engine::SetupLog(PROJECT_NAME); + LOG_INFO("{} v{}", PROJECT_NAME, PROJECT_VERSION); - LOG_INFO("{} v{}", PROJECT_NAME, PROJECT_VERSION); + try { + PlayGame(settings); + } catch (const std::exception& e) { + LOG_CRITICAL("{}", e.what()); + engine::Window::ErrorBox(e.what()); + return EXIT_FAILURE; + } - try { - playGame(settings); - } - catch (const std::exception& e) { - LOG_CRITICAL("{}", e.what()); - engine::Window::ErrorBox(e.what()); - return EXIT_FAILURE; - } - - return EXIT_SUCCESS; + return EXIT_SUCCESS; } diff --git a/test/src/meshgen.cpp b/test/src/meshgen.cpp index f7899e6..9a8dd06 100644 --- a/test/src/meshgen.cpp +++ b/test/src/meshgen.cpp @@ -1,158 +1,151 @@ #include "meshgen.hpp" -#include "resources/mesh.h" +#include #include #include #include -#include +#include "resources/mesh.h" -std::unique_ptr genSphereMesh(engine::GFXDevice* gfx, float r, int detail, bool windInside, bool flipNormals) -{ - using namespace glm; +std::unique_ptr GenSphereMesh(engine::GFXDevice* gfx, + float r, int detail, + bool wind_inside, + bool flip_normals) { + using namespace glm; - std::vector vertices{}; + std::vector vertices{}; - float angleStep = two_pi() / (float)detail; + const float angle_step = two_pi() / (float)detail; - for (int i = 0; i < detail; i++) { - // theta goes north-to-south - float theta = i * angleStep; - float theta2 = theta + angleStep; - for (int j = 0; j < detail/2; j++) { - // phi goes west-to-east - float phi = j * angleStep; - float phi2 = phi + angleStep; + for (int i = 0; i < detail; i++) { + // theta goes north-to-south + float theta = i * angle_step; + float theta2 = theta + angle_step; + for (int j = 0; j < detail / 2; j++) { + // phi goes west-to-east + float phi = j * angle_step; + float phi2 = phi + angle_step; - vec3 top_left{ r * sin(phi) * cos(theta), - r * cos(phi), - r * sin(phi) * sin(theta) }; - vec3 bottom_left{ r * sin(phi) * cos(theta2), - r * cos(phi), - r * sin(phi) * sin(theta2) }; - vec3 top_right{ r * sin(phi2) * cos(theta), - r * cos(phi2), - r * sin(phi2) * sin(theta) }; - vec3 bottom_right{ r * sin(phi2) * cos(theta2), - r * cos(phi2), - r * sin(phi2) * sin(theta2) }; + vec3 top_left{r * sin(phi) * cos(theta), r * cos(phi), + r * sin(phi) * sin(theta)}; + vec3 bottom_left{r * sin(phi) * cos(theta2), r * cos(phi), + r * sin(phi) * sin(theta2)}; + vec3 top_right{r * sin(phi2) * cos(theta), r * cos(phi2), + r * sin(phi2) * sin(theta)}; + vec3 bottom_right{r * sin(phi2) * cos(theta2), r * cos(phi2), + r * sin(phi2) * sin(theta2)}; - if (windInside == false) { - // tris are visible from outside the sphere + if (wind_inside == false) { + // tris are visible from outside the sphere - // triangle 1 - vertices.push_back({ top_left, {}, {0.0f, 0.0f} }); - vertices.push_back({ bottom_left, {}, {0.0f, 1.0f} }); - vertices.push_back({ bottom_right, {}, {1.0f, 1.0f} }); - // triangle 2 - vertices.push_back({ top_right, {}, {1.0f, 0.0f} }); - vertices.push_back({ top_left, {}, {0.0f, 0.0f} }); - vertices.push_back({ bottom_right, {}, {1.0f, 1.0f} }); + // triangle 1 + vertices.push_back({top_left, {}, {0.0f, 0.0f}}); + vertices.push_back({bottom_left, {}, {0.0f, 1.0f}}); + vertices.push_back({bottom_right, {}, {1.0f, 1.0f}}); + // triangle 2 + vertices.push_back({top_right, {}, {1.0f, 0.0f}}); + vertices.push_back({top_left, {}, {0.0f, 0.0f}}); + vertices.push_back({bottom_right, {}, {1.0f, 1.0f}}); - } - else { - // tris are visible from inside the sphere + } else { + // tris are visible from inside the sphere - // triangle 1 - vertices.push_back({ bottom_right, {}, {1.0f, 1.0f} }); - vertices.push_back({ bottom_left, {}, {0.0f, 1.0f} }); - vertices.push_back({ top_left, {}, {0.0f, 0.0f} }); - - // triangle 2 - vertices.push_back({ bottom_right, {}, {1.0f, 1.0f} }); - vertices.push_back({ top_left, {}, {0.0f, 0.0f} }); - vertices.push_back({ top_right, {}, {1.0f, 0.0f} }); + // triangle 1 + vertices.push_back({bottom_right, {}, {1.0f, 1.0f}}); + vertices.push_back({bottom_left, {}, {0.0f, 1.0f}}); + vertices.push_back({top_left, {}, {0.0f, 0.0f}}); - } - - vec3 vector1 = (vertices.end() - 1)->pos - (vertices.end() - 2)->pos; - vec3 vector2 = (vertices.end() - 2)->pos - (vertices.end() - 3)->pos; - vec3 norm = normalize(cross(vector2, vector1)); + // triangle 2 + vertices.push_back({bottom_right, {}, {1.0f, 1.0f}}); + vertices.push_back({top_left, {}, {0.0f, 0.0f}}); + vertices.push_back({top_right, {}, {1.0f, 0.0f}}); + } - // NORMALS HAVE BEEN FIXED + vec3 vector1 = (vertices.end() - 1)->pos - (vertices.end() - 2)->pos; + vec3 vector2 = (vertices.end() - 2)->pos - (vertices.end() - 3)->pos; + vec3 norm = normalize(cross(vector2, vector1)); - if (flipNormals) - norm = -norm; + // NORMALS HAVE BEEN FIXED - if (j == (detail / 2) - 1) - norm = -norm; + if (flip_normals) norm = -norm; - for (auto it = vertices.end() - 6; it != vertices.end(); it++) { - it->norm = norm; - } + if (j == (detail / 2) - 1) norm = -norm; - } - } + for (auto it = vertices.end() - 6; it != vertices.end(); it++) { + it->norm = norm; + } + } + } - return std::make_unique(gfx, vertices); + return std::make_unique(gfx, vertices); } -std::unique_ptr genCuboidMesh(engine::GFXDevice* gfx, float x, float y, float z, float tiling, bool windInside) -{ +std::unique_ptr GenCuboidMesh(engine::GFXDevice* gfx, + float x, float y, + float z, float tiling, + bool wind_inside) { + // x goes -> + // y goes ^ + // z goes into the screen - // x goes -> - // y goes ^ - // z goes into the screen + using namespace glm; - using namespace glm; + std::vector v{}; - std::vector v{}; + // front + v.push_back({{x, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {tiling, 0.0f}}); + v.push_back({{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f}}); + v.push_back({{0.0f, y, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, tiling}}); + v.push_back({{x, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {tiling, 0.0f}}); + v.push_back({{0.0f, y, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, tiling}}); + v.push_back({{x, y, 0.0f}, {0.0f, 0.0f, -1.0f}, {tiling, tiling}}); - // front - v.push_back({{x, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {tiling, 0.0f}}); - v.push_back({{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f}}); - v.push_back({{0.0f, y, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, tiling}}); - v.push_back({{x, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {tiling, 0.0f}}); - v.push_back({{0.0f, y, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, tiling}}); - v.push_back({{x, y, 0.0f}, {0.0f, 0.0f, -1.0f}, {tiling, tiling}}); + // back + v.push_back({{0.0f, 0.0f, z}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f}}); + v.push_back({{x, 0.0f, z}, {0.0f, 0.0f, 1.0f}, {tiling, 0.0f}}); + v.push_back({{0.0f, y, z}, {0.0f, 0.0f, 1.0f}, {0.0f, tiling}}); + v.push_back({{x, 0.0f, z}, {0.0f, 0.0f, 1.0f}, {tiling, 0.0f}}); + v.push_back({{x, y, z}, {0.0f, 0.0f, 1.0f}, {tiling, tiling}}); + v.push_back({{0.0f, y, z}, {0.0f, 0.0f, 1.0f}, {0.0f, tiling}}); - // back - v.push_back({{0.0f, 0.0f, z}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f}}); - v.push_back({{x, 0.0f, z}, {0.0f, 0.0f, 1.0f}, {tiling, 0.0f}}); - v.push_back({{0.0f, y, z}, {0.0f, 0.0f, 1.0f}, {0.0f, tiling}}); - v.push_back({{x, 0.0f, z}, {0.0f, 0.0f, 1.0f}, {tiling, 0.0f}}); - v.push_back({{x, y, z}, {0.0f, 0.0f, 1.0f}, {tiling, tiling}}); - v.push_back({{0.0f, y, z}, {0.0f, 0.0f, 1.0f}, {0.0f, tiling}}); + // left + v.push_back({{0.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{0.0f, 0.0f, x}, {-1.0f, 0.0f, 0.0f}, {0.0f, tiling}}); + v.push_back({{0.0f, y, 0.0f}, {-1.0f, 0.0f, 0.0f}, {tiling, 0.0f}}); + v.push_back({{0.0f, 0.0f, x}, {-1.0f, 0.0f, 0.0f}, {0.0f, tiling}}); + v.push_back({{0.0f, y, x}, {-1.0f, 0.0f, 0.0f}, {tiling, tiling}}); + v.push_back({{0.0f, y, 0.0f}, {-1.0f, 0.0f, 0.0f}, {tiling, 0.0f}}); - // left - v.push_back({{0.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}); - v.push_back({{0.0f, 0.0f, x}, {-1.0f, 0.0f, 0.0f}, {0.0f, tiling}}); - v.push_back({{0.0f, y, 0.0f}, {-1.0f, 0.0f, 0.0f}, {tiling, 0.0f}}); - v.push_back({{0.0f, 0.0f, x}, {-1.0f, 0.0f, 0.0f}, {0.0f, tiling}}); - v.push_back({{0.0f, y, x}, {-1.0f, 0.0f, 0.0f}, {tiling, tiling}}); - v.push_back({{0.0f, y, 0.0f}, {-1.0f, 0.0f, 0.0f}, {tiling, 0.0f}}); + // right + v.push_back({{x, y, 0.0f}, {1.0f, 0.0f, 0.0f}, {tiling, 0.0f}}); + v.push_back({{x, 0.0f, x}, {1.0f, 0.0f, 0.0f}, {0.0f, tiling}}); + v.push_back({{x, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{x, y, 0.0f}, {1.0f, 0.0f, 0.0f}, {tiling, 0.0f}}); + v.push_back({{x, y, x}, {1.0f, 0.0f, 0.0f}, {tiling, tiling}}); + v.push_back({{x, 0.0f, x}, {1.0f, 0.0f, 0.0f}, {0.0f, tiling}}); - // right - v.push_back({{x, y, 0.0f}, {1.0f, 0.0f, 0.0f}, {tiling, 0.0f}}); - v.push_back({{x, 0.0f, x}, {1.0f, 0.0f, 0.0f}, {0.0f, tiling}}); - v.push_back({{x, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}); - v.push_back({{x, y, 0.0f}, {1.0f, 0.0f, 0.0f}, {tiling, 0.0f}}); - v.push_back({{x, y, x}, {1.0f, 0.0f, 0.0f}, {tiling, tiling}}); - v.push_back({{x, 0.0f, x}, {1.0f, 0.0f, 0.0f}, {0.0f, tiling}}); + // bottom + v.push_back({{0.0f, 0.0f, z}, {0.0f, -1.0f, 0.0f}, {0.0f, tiling}}); + v.push_back({{0.0f, 0.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{x, 0.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {tiling, 0.0f}}); + v.push_back({{x, 0.0f, z}, {0.0f, -1.0f, 0.0f}, {tiling, tiling}}); + v.push_back({{0.0f, 0.0f, z}, {0.0f, -1.0f, 0.0f}, {0.0f, tiling}}); + v.push_back({{x, 0.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {tiling, 0.0f}}); - // bottom - v.push_back({{0.0f, 0.0f, z}, {0.0f, -1.0f, 0.0f}, {0.0f, tiling}}); - v.push_back({{0.0f, 0.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {0.0f, 0.0f}}); - v.push_back({{x, 0.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {tiling, 0.0f}}); - v.push_back({{x, 0.0f, z}, {0.0f, -1.0f, 0.0f}, {tiling, tiling}}); - v.push_back({{0.0f, 0.0f, z}, {0.0f, -1.0f, 0.0f}, {0.0f, tiling}}); - v.push_back({{x, 0.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {tiling, 0.0f}}); + // top + v.push_back({{x, y, 0.0f}, {0.0f, 1.0f, 0.0f}, {tiling, 0.0f}}); + v.push_back({{0.0f, y, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{0.0f, y, z}, {0.0f, 1.0f, 0.0f}, {0.0f, tiling}}); + v.push_back({{x, y, 0.0f}, {0.0f, 1.0f, 0.0f}, {tiling, 0.0f}}); + v.push_back({{0.0f, y, z}, {0.0f, 1.0f, 0.0f}, {0.0f, tiling}}); + v.push_back({{x, y, z}, {0.0f, 1.0f, 0.0f}, {tiling, tiling}}); - // top - v.push_back({{x, y, 0.0f}, {0.0f, 1.0f, 0.0f}, {tiling, 0.0f}}); - v.push_back({{0.0f, y, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}}); - v.push_back({{0.0f, y, z}, {0.0f, 1.0f, 0.0f}, {0.0f, tiling}}); - v.push_back({{x, y, 0.0f}, {0.0f, 1.0f, 0.0f}, {tiling, 0.0f}}); - v.push_back({{0.0f, y, z}, {0.0f, 1.0f, 0.0f}, {0.0f, tiling}}); - v.push_back({{x, y, z}, {0.0f, 1.0f, 0.0f}, {tiling, tiling}}); - - if (windInside) { - for (size_t i = 0; i < v.size(); i += 3) { - std::swap(v[i], v[i + 2]); - } - } - - return std::make_unique(gfx, v); + if (wind_inside) { + for (size_t i = 0; i < v.size(); i += 3) { + std::swap(v[i], v[i + 2]); + } + } + return std::make_unique(gfx, v); } diff --git a/test/src/meshgen.hpp b/test/src/meshgen.hpp index b43a7e1..888d3a9 100644 --- a/test/src/meshgen.hpp +++ b/test/src/meshgen.hpp @@ -1,8 +1,16 @@ -#pragma once +#ifndef ENGINE_TEST_SRC_MESHGEN_H_ +#define ENGINE_TEST_SRC_MESHGEN_H_ #include + #include "resources/mesh.h" -// generates a UV sphere -std::unique_ptr genSphereMesh(engine::GFXDevice* gfx, float r, int detail, bool windInside = false, bool flipNormals = false); -std::unique_ptr genCuboidMesh(engine::GFXDevice* gfx, float x, float y, float z, float tiling = 1.0f, bool windInside = false); +std::unique_ptr GenSphereMesh( + engine::GFXDevice* gfx, float r, int detail, bool wind_inside = false, + bool flip_normals = false); + +std::unique_ptr GenCuboidMesh( + engine::GFXDevice* gfx, float x, float y, float z, float tiling = 1.0f, + bool wind_inside = false); + +#endif \ No newline at end of file diff --git a/test/src/terrain.cpp b/test/src/terrain.cpp deleted file mode 100644 index b33312b..0000000 --- a/test/src/terrain.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#if 0 - -#include "terrain.hpp" - -#include "resources/mesh.hpp" - -std::unique_ptr getChunkMesh(int x, int y) -{ - (void)x; - (void)y; - - std::vector vertices{ - {{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}, - {{1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}, - {{0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f}} - }; - - return std::make_unique(vertices); -} - -#endif diff --git a/test/src/terrain.hpp b/test/src/terrain.hpp deleted file mode 100644 index 5ed5c19..0000000 --- a/test/src/terrain.hpp +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include - -namespace engine::resources { - class Mesh; -} - -std::unique_ptr getChunkMesh(int x, int y);