more reformatting

This commit is contained in:
Bailey Harrison 2023-05-02 12:02:43 +01:00
parent bf6c8d8a02
commit 56b8daa0bf
13 changed files with 456 additions and 474 deletions

View File

@ -193,7 +193,7 @@ RemoveBracesLLVM: false
RequiresClausePosition: OwnLine RequiresClausePosition: OwnLine
SeparateDefinitionBlocks: Leave SeparateDefinitionBlocks: Leave
ShortNamespaceLines: 1 ShortNamespaceLines: 1
SortIncludes: CaseSensitive SortIncludes: false
SortJavaStaticImport: Before SortJavaStaticImport: Before
SortUsingDeclarations: true SortUsingDeclarations: true
SpaceAfterCStyleCast: false SpaceAfterCStyleCast: false

View File

@ -35,7 +35,7 @@ class Scene {
size_t GetComponentSignaturePosition(size_t hash); size_t GetComponentSignaturePosition(size_t hash);
template <typename T> template <typename T>
void registerComponent() { void RegisterComponent() {
size_t hash = typeid(T).hash_code(); size_t hash = typeid(T).hash_code();
assert(component_arrays_.contains(hash) == false && assert(component_arrays_.contains(hash) == false &&
"Registering component type more than once."); "Registering component type more than once.");

View File

@ -2,9 +2,10 @@
#define ENGINE_INCLUDE_SYSTEMS_COLLISIONS_H_ #define ENGINE_INCLUDE_SYSTEMS_COLLISIONS_H_
#include <cstdint> #include <cstdint>
#include <glm/mat4x4.hpp>
#include <vector> #include <vector>
#include <glm/mat4x4.hpp>
#include "components/collider.h" #include "components/collider.h"
#include "ecs_system.h" #include "ecs_system.h"
@ -28,7 +29,6 @@ class PhysicsSystem : public System {
private: private:
// dynamic arrays to avoid realloc on every frame // dynamic arrays to avoid realloc on every frame
// entity, aabb, is_trigger // entity, aabb, is_trigger
std::vector<std::tuple<uint32_t, AABB, bool>> static_aabbs_{}; std::vector<std::tuple<uint32_t, AABB, bool>> static_aabbs_{};
std::vector<std::tuple<uint32_t, AABB, bool>> dynamic_aabbs_{}; std::vector<std::tuple<uint32_t, AABB, bool>> dynamic_aabbs_{};

View File

@ -17,9 +17,9 @@ namespace engine {
// ecs configuration: // ecs configuration:
registerComponent<TransformComponent>(); RegisterComponent<TransformComponent>();
registerComponent<RenderableComponent>(); RegisterComponent<RenderableComponent>();
registerComponent<ColliderComponent>(); RegisterComponent<ColliderComponent>();
// Order here matters: // Order here matters:
RegisterSystem<TransformSystem>(); RegisterSystem<TransformSystem>();

View File

@ -1,28 +1,24 @@
#include "camera_controller.hpp" #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 <glm/trigonometric.hpp>
#include <glm/gtc/constants.hpp> #include <glm/gtc/constants.hpp>
#include <glm/gtc/quaternion.hpp> #include <glm/gtc/quaternion.hpp>
#include <glm/gtx/rotate_vector.hpp> #include <glm/gtx/rotate_vector.hpp>
#include <glm/trigonometric.hpp>
#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) 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) { if (t == nullptr || c == nullptr || col == nullptr) {
for (uint32_t entity : entities_) { for (uint32_t entity : entities_) {
t = scene_->GetComponent<engine::TransformComponent>(entity); t = scene_->GetComponent<engine::TransformComponent>(entity);
@ -34,90 +30,92 @@ void CameraControllerSystem::OnUpdate(float ts)
if (c == nullptr) return; if (c == nullptr) return;
if (col == nullptr) return; if (col == nullptr) return;
} }
// calculate new position
// use one unit per meter
const float dt = ts; const float dt = ts;
constexpr float G = 9.8f; constexpr float G = 9.8f;
const float MAX_SLOPE_ANGLE = glm::cos(glm::radians(20.0f)); const float kMaxSlopeAngle = 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 kFloorSinkLevel =
constexpr float FLOOR_SINK_LEVEL = 0.05f; // how far into the floor to ground the player 0.05f; // how far into the floor to ground the player
glm::vec3 norm = c->lastCollisionNormal; glm::vec3 norm = c->last_collision_normal;
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)); 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); const float slope = glm::dot(dir, norm);
bool isSliding = false; bool is_sliding = false;
if (c->justCollided) { if (c->just_collided) {
if (slope > MAX_SLOPE_ANGLE) { if (slope > kMaxSlopeAngle) {
// slide across wall // slide across wall
isSliding = true; is_sliding = true;
} else { } else {
if (c->dy < 0.0f && c->isGrounded == false) { if (c->dy < 0.0f && c->is_grounded == false) {
// in the ground, push up a bit // in the ground, push up a bit
float floorY = c->lastCollisionPoint.y; float floorY = c->last_collision_point.y;
t->position.y = floorY + 1.5f - FLOOR_SINK_LEVEL; t->position.y = floorY + 1.5f - kFloorSinkLevel;
c->dy = 0.0f; c->dy = 0.0f;
c->isGrounded = true; c->is_grounded = true;
} }
} }
} }
if (c->justCollided == false && slope <= MAX_SLOPE_ANGLE) { if (c->just_collided == false && slope <= kMaxSlopeAngle) {
// just stopped colliding with a floor collider // just stopped colliding with a floor collider
c->isGrounded = false; c->is_grounded = false;
} }
if (c->isGrounded == false) if (c->is_grounded == false) c->dy -= G * dt;
c->dy -= G * dt;
// jumping // jumping
constexpr float JUMPVEL = (float)2.82231110971133017648; //std::sqrt(2 * G * JUMPHEIGHT); constexpr float JUMPVEL =
if (scene_->app()->input_manager()->GetButton("jump") && c->isGrounded == true) { (float)2.82231110971133017648; // std::sqrt(2 * G * JUMPHEIGHT);
if (scene_->app()->input_manager()->GetButton("jump") &&
c->is_grounded == true) {
c->dy = JUMPVEL; c->dy = JUMPVEL;
} }
if (scene_->app()->window()->GetButton(engine::inputs::MouseButton::M_LEFT)) { if (scene_->app()->window()->GetButton(engine::inputs::MouseButton::M_LEFT)) {
c->dy += dt * c->thrust; c->dy += dt * c->kThrust;
} }
// in metres per second // in metres per second
float SPEED = c->walk_speed; float speed = c->kWalkSpeed;
if (scene_->app()->input_manager()->GetButton("sprint")) SPEED *= 10.0f; if (scene_->app()->input_manager()->GetButton("sprint")) speed *= 10.0f;
float dx = scene_->app()->input_manager()->GetAxis("movex"); float dx = scene_->app()->input_manager()->GetAxis("movex");
float dz = (-scene_->app()->input_manager()->GetAxis("movey")); float dz = (-scene_->app()->input_manager()->GetAxis("movey"));
// calculate new pitch and yaw // calculate new pitch and yaw
constexpr float MAX_PITCH = glm::half_pi<float>(); constexpr float kMaxPitch = glm::half_pi<float>();
constexpr float MIN_PITCH = -MAX_PITCH; constexpr float kMinPitch = -kMaxPitch;
float dPitch = scene_->app()->input_manager()->GetAxis("looky") * -1.0f * c->m_cameraSensitivity; float d_pitch = scene_->app()->input_manager()->GetAxis("looky") * -1.0f *
c->m_pitch += dPitch; c->kCameraSensitivity;
if (c->m_pitch <= MIN_PITCH || c->m_pitch >= MAX_PITCH) { c->pitch += d_pitch;
c->m_pitch -= dPitch; if (c->pitch <= kMinPitch || c->pitch >= kMaxPitch) {
c->pitch -= d_pitch;
} }
c->m_yaw += scene_->app()->input_manager()->GetAxis("lookx") * -1.0f * c->m_cameraSensitivity; c->yaw += scene_->app()->input_manager()->GetAxis("lookx") * -1.0f *
c->kCameraSensitivity;
// update position relative to camera direction in xz plane // 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 d2x_rotated = glm::rotateY(glm::vec3{dx, 0.0f, 0.0f}, c->yaw);
const glm::vec3 d2zRotated = glm::rotateY(glm::vec3{ 0.0f, 0.0f, dz }, c->m_yaw); const glm::vec3 d2z_rotated = glm::rotateY(glm::vec3{0.0f, 0.0f, dz}, c->yaw);
glm::vec3 hVel = (d2xRotated + d2zRotated); glm::vec3 h_vel = (d2x_rotated + d2z_rotated);
if (isSliding) { if (is_sliding) {
hVel = glm::vec3{norm.z, 0.0f, -norm.x}; h_vel = glm::vec3{norm.z, 0.0f, -norm.x};
} }
hVel *= SPEED; h_vel *= speed;
t->position += hVel * dt; t->position += h_vel * dt;
t->position.y += c->dy * dt; t->position.y += c->dy * dt;
constexpr float MAX_DISTANCE_FROM_ORIGIN = 10000.0f; constexpr float kMaxDistanceFromOrigin = 10000.0f;
if (glm::length(t->position) > MAX_DISTANCE_FROM_ORIGIN) { if (glm::length(t->position) > kMaxDistanceFromOrigin) {
t->position = {0.0f, 5.0f, 0.0f}; t->position = {0.0f, 5.0f, 0.0f};
c->dy = 0.0f; c->dy = 0.0f;
} }
@ -125,36 +123,31 @@ void CameraControllerSystem::OnUpdate(float ts)
/* ROTATION STUFF */ /* ROTATION STUFF */
// pitch quaternion // pitch quaternion
const float halfPitch = c->m_pitch / 2.0f; const float half_pitch = c->pitch / 2.0f;
glm::quat pitchQuat{}; glm::quat pitch_quat{};
pitchQuat.x = glm::sin(halfPitch); pitch_quat.x = glm::sin(half_pitch);
pitchQuat.y = 0.0f; pitch_quat.y = 0.0f;
pitchQuat.z = 0.0f; pitch_quat.z = 0.0f;
pitchQuat.w = glm::cos(halfPitch); pitch_quat.w = glm::cos(half_pitch);
// yaw quaternion // yaw quaternion
const float halfYaw = c->m_yaw / 2.0f; const float half_yaw = c->yaw / 2.0f;
glm::quat yawQuat{}; glm::quat yaw_quat{};
yawQuat.x = 0.0f; yaw_quat.x = 0.0f;
yawQuat.y = glm::sin(halfYaw); yaw_quat.y = glm::sin(half_yaw);
yawQuat.z = 0.0f; yaw_quat.z = 0.0f;
yawQuat.w = glm::cos(halfYaw); yaw_quat.w = glm::cos(half_yaw);
// update rotation // update rotation
t->rotation = yawQuat * pitchQuat; t->rotation = yaw_quat * pitch_quat;
/* user interface inputs */ /* user interface inputs */
if (scene_->app()->window()->GetKeyPress(engine::inputs::Key::K_P)) { if (scene_->app()->window()->GetKeyPress(engine::inputs::Key::K_P)) {
std::string pos_string{ std::string pos_string{"x: " + std::to_string(t->position.x) +
"x: " + std::to_string(t->position.x) +
" y: " + std::to_string(t->position.y) + " y: " + std::to_string(t->position.y) +
" z: " + std::to_string(t->position.z) " z: " + std::to_string(t->position.z)};
}; LOG_INFO("position {}", pos_string);
//scene_->app()->window()->InfoBox("POSITION", pos_string);
LOG_INFO("position: " + pos_string);
} }
if (scene_->app()->window()->GetKeyPress(engine::inputs::Key::K_R)) { if (scene_->app()->window()->GetKeyPress(engine::inputs::Key::K_R)) {
@ -170,14 +163,13 @@ void CameraControllerSystem::OnUpdate(float ts)
scene_->app()->window()->SetCloseFlag(); scene_->app()->window()->SetCloseFlag();
} }
c->justCollided = false; c->just_collided = false;
} }
// called once per frame // called once per frame
void CameraControllerSystem::OnEvent(engine::PhysicsSystem::CollisionEvent info) void CameraControllerSystem::OnEvent(
{ engine::PhysicsSystem::CollisionEvent info) {
c->justCollided = info.is_collision_enter; c->just_collided = info.is_collision_enter;
c->lastCollisionNormal = info.normal; c->last_collision_normal = info.normal;
c->lastCollisionPoint = info.point; c->last_collision_point = info.point;
} }

View File

@ -1,31 +1,33 @@
#pragma once #ifndef ENGINE_TEST_SRC_CAMERA_CONTROLLER_H_
#define ENGINE_TEST_SRC_CAMERA_CONTROLLER_H_
#include "ecs_system.h" #include <glm/vec3.hpp>
#include "event_system.h"
#include "components/transform.h" #include "components/transform.h"
#include "ecs_system.h"
#include "event_system.h"
#include "systems/collisions.h" #include "systems/collisions.h"
struct CameraControllerComponent { 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 yaw = 0.0f;
float m_pitch = 0.0f; float pitch = 0.0f;
const float walk_speed = 4.0f;
bool isGrounded = false;
float dy = 0.0f; float dy = 0.0f;
float standingHeight = 0.0f;
const float thrust = 25.0f;
glm::vec3 lastCollisionNormal{}; glm::vec3 last_collision_normal{};
glm::vec3 lastCollisionPoint{}; glm::vec3 last_collision_point{};
bool justCollided = false;
bool just_collided = false;
bool is_grounded = false;
}; };
class CameraControllerSystem : public engine::System, public engine::EventHandler<engine::PhysicsSystem::CollisionEvent> { class CameraControllerSystem
: public engine::System,
public engine::EventHandler<engine::PhysicsSystem::CollisionEvent> {
public: public:
CameraControllerSystem(engine::Scene* scene); CameraControllerSystem(engine::Scene* scene);
@ -39,3 +41,5 @@ public:
engine::ColliderComponent* col = nullptr; engine::ColliderComponent* col = nullptr;
CameraControllerComponent* c = nullptr; CameraControllerComponent* c = nullptr;
}; };
#endif

View File

@ -1,132 +1,148 @@
#include "game.hpp" #include "game.hpp"
#include "config.h"
#include "camera_controller.hpp"
#include "meshgen.hpp"
#include "application.h" #include "application.h"
#include "window.h" #include "camera_controller.hpp"
#include "input_manager.h"
#include "scene_manager.h"
#include "scene.h"
#include "components/transform.h"
#include "components/collider.h" #include "components/collider.h"
#include "components/renderable.h" #include "components/renderable.h"
#include "systems/transform.h" #include "components/transform.h"
#include "systems/render.h" #include "input_manager.h"
#include "meshgen.hpp"
#include "resources/material.h" #include "resources/material.h"
#include "resources/texture.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 "util/model_loader.h"
#include "window.h"
static void configureInputs(engine::InputManager* inputManager) #include "config.h"
{
static void ConfigureInputs(engine::InputManager* input_manager) {
// user interface mappings // user interface mappings
inputManager->AddInputButton("fullscreen", engine::inputs::Key::K_F11); input_manager->AddInputButton("fullscreen", engine::inputs::Key::K_F11);
inputManager->AddInputButton("exit", engine::inputs::Key::K_ESCAPE); input_manager->AddInputButton("exit", engine::inputs::Key::K_ESCAPE);
// game buttons // game buttons
inputManager->AddInputButton("fire", engine::inputs::MouseButton::M_LEFT); input_manager->AddInputButton("fire", engine::inputs::MouseButton::M_LEFT);
inputManager->AddInputButton("aim", engine::inputs::MouseButton::M_RIGHT); input_manager->AddInputButton("aim", engine::inputs::MouseButton::M_RIGHT);
inputManager->AddInputButton("jump", engine::inputs::Key::K_SPACE); input_manager->AddInputButton("jump", engine::inputs::Key::K_SPACE);
inputManager->AddInputButton("sprint", engine::inputs::Key::K_LSHIFT); input_manager->AddInputButton("sprint", engine::inputs::Key::K_LSHIFT);
// game movement // game movement
inputManager->AddInputButtonAsAxis("movex", engine::inputs::Key::K_D, engine::inputs::Key::K_A); input_manager->AddInputButtonAsAxis("movex", engine::inputs::Key::K_D,
inputManager->AddInputButtonAsAxis("movey", engine::inputs::Key::K_W, engine::inputs::Key::K_S); engine::inputs::Key::K_A);
input_manager->AddInputButtonAsAxis("movey", engine::inputs::Key::K_W,
engine::inputs::Key::K_S);
// looking around // looking around
inputManager->AddInputAxis("lookx", engine::inputs::MouseAxis::X); input_manager->AddInputAxis("lookx", engine::inputs::MouseAxis::X);
inputManager->AddInputAxis("looky", engine::inputs::MouseAxis::Y); input_manager->AddInputAxis("looky", engine::inputs::MouseAxis::Y);
} }
void playGame(GameSettings settings) void PlayGame(GameSettings settings) {
{ LOG_INFO("FPS limiter: {}", settings.enable_frame_limiter ? "ON" : "OFF");
LOG_INFO("FPS limiter: {}", settings.enableFrameLimiter ? "ON" : "OFF"); LOG_INFO("Graphics Validation: {}",
LOG_INFO("Graphics Validation: {}", settings.enableValidation ? "ON" : "OFF"); settings.enable_validation ? "ON" : "OFF");
engine::gfx::GraphicsSettings graphicsSettings{}; engine::gfx::GraphicsSettings graphics_settings{};
graphicsSettings.enable_validation = settings.enableValidation; graphics_settings.enable_validation = settings.enable_validation;
graphicsSettings.vsync = true; graphics_settings.vsync = true;
graphicsSettings.wait_for_present = false; graphics_settings.wait_for_present = false;
graphicsSettings.msaa_level = engine::gfx::MSAALevel::kOff; graphics_settings.msaa_level = engine::gfx::MSAALevel::kOff;
engine::Application app(PROJECT_NAME, PROJECT_VERSION, graphicsSettings); engine::Application app(PROJECT_NAME, PROJECT_VERSION, graphics_settings);
app.SetFrameLimiter(settings.enableFrameLimiter); app.SetFrameLimiter(settings.enable_frame_limiter);
// configure window // configure window
app.window()->SetRelativeMouseMode(true); app.window()->SetRelativeMouseMode(true);
configureInputs(app.input_manager()); ConfigureInputs(app.input_manager());
auto myScene = app.scene_manager()->CreateEmptyScene(); auto my_scene = app.scene_manager()->CreateEmptyScene();
/* create camera */ /* create camera */
{ {
myScene->registerComponent<CameraControllerComponent>(); my_scene->RegisterComponent<CameraControllerComponent>();
myScene->RegisterSystem<CameraControllerSystem>(); my_scene->RegisterSystem<CameraControllerSystem>();
auto camera = myScene->CreateEntity("camera"); auto camera = my_scene->CreateEntity("camera");
myScene->GetComponent<engine::TransformComponent>(camera)->position = { 0.0f, 10.0f, 0.0f }; my_scene->GetComponent<engine::TransformComponent>(camera)->position = {
auto cameraCollider = myScene->AddComponent<engine::ColliderComponent>(camera); 0.0f, 10.0f, 0.0f};
cameraCollider->is_static = false; auto camer_collider =
cameraCollider->is_trigger = true; my_scene->AddComponent<engine::ColliderComponent>(camera);
cameraCollider->aabb = { { -0.2f, -1.5f, -0.2f }, { 0.2f, 0.2f, 0.2f} }; // Origin is at eye level camer_collider->is_static = false;
myScene->AddComponent<CameraControllerComponent>(camera); camer_collider->is_trigger = true;
myScene->event_system()->SubscribeToEventType<engine::PhysicsSystem::CollisionEvent>( camer_collider->aabb = {{-0.2f, -1.5f, -0.2f},
engine::EventSubscriberKind::kEntity, camera, myScene->GetSystem<CameraControllerSystem>() {0.2f, 0.2f, 0.2f}}; // Origin is at eye level
); my_scene->AddComponent<CameraControllerComponent>(camera);
my_scene->event_system()
->SubscribeToEventType<engine::PhysicsSystem::CollisionEvent>(
engine::EventSubscriberKind::kEntity, camera,
my_scene->GetSystem<CameraControllerSystem>());
auto renderSystem = myScene->GetSystem<engine::RenderSystem>(); auto render_system = my_scene->GetSystem<engine::RenderSystem>();
renderSystem->SetCameraEntity(camera); render_system->SetCameraEntity(camera);
} }
/* shared resources */ /* shared resources */
auto grassTexture = std::make_shared<engine::resources::Texture>( auto grass_texture = std::make_shared<engine::resources::Texture>(
&app.render_data_, &app.render_data_, app.GetResourcePath("textures/grass.jpg"),
app.GetResourcePath("textures/grass.jpg"), engine::resources::Texture::Filtering::kAnisotropic);
engine::resources::Texture::Filtering::kAnisotropic auto space_texture = std::make_shared<engine::resources::Texture>(
); &app.render_data_, app.GetResourcePath("textures/space2.png"),
auto spaceTexture = std::make_shared<engine::resources::Texture>( engine::resources::Texture::Filtering::kAnisotropic);
&app.render_data_,
app.GetResourcePath("textures/space2.png"),
engine::resources::Texture::Filtering::kAnisotropic
);
/* cube */ /* cube */
{ {
uint32_t cube = myScene->CreateEntity("cube"); uint32_t cube = my_scene->CreateEntity("cube");
myScene->GetComponent<engine::TransformComponent>(cube)->position = glm::vec3{ -0.5f + 5.0f, -0.5f + 5.0f, -0.5f + 5.0f }; my_scene->GetComponent<engine::TransformComponent>(cube)->position =
auto cubeRenderable = myScene->AddComponent<engine::RenderableComponent>(cube); glm::vec3{-0.5f + 5.0f, -0.5f + 5.0f, -0.5f + 5.0f};
cubeRenderable->material = std::make_shared<engine::resources::Material>(app.GetResource<engine::resources::Shader>("builtin.standard")); auto cube_renderable =
cubeRenderable->material->texture_ = app.GetResource<engine::resources::Texture>("builtin.white"); my_scene->AddComponent<engine::RenderableComponent>(cube);
cubeRenderable->mesh = genCuboidMesh(app.gfxdev(), 1.0f, 1.0f, 1.0f, 1); cube_renderable->material = std::make_shared<engine::resources::Material>(
auto cubeCollider = myScene->AddComponent<engine::ColliderComponent>(cube); app.GetResource<engine::resources::Shader>("builtin.standard"));
cubeCollider->is_static = true; cube_renderable->material->texture_ =
cubeCollider->aabb = { { 0.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, 1.0f } }; app.GetResource<engine::resources::Texture>("builtin.white");
cube_renderable->mesh = GenCuboidMesh(app.gfxdev(), 1.0f, 1.0f, 1.0f, 1);
auto cube_collider =
my_scene->AddComponent<engine::ColliderComponent>(cube);
cube_collider->is_static = true;
cube_collider->aabb = {{0.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f}};
} }
/* floor */ /* floor */
{ {
uint32_t floor = myScene->CreateEntity("floor"); uint32_t floor = my_scene->CreateEntity("floor");
myScene->GetComponent<engine::TransformComponent>(floor)->position = glm::vec3{-5000.0f, -1.0f, -5000.0f}; my_scene->GetComponent<engine::TransformComponent>(floor)->position =
auto floorRenderable = myScene->AddComponent<engine::RenderableComponent>(floor); glm::vec3{-5000.0f, -1.0f, -5000.0f};
floorRenderable->material = std::make_shared<engine::resources::Material>(app.GetResource<engine::resources::Shader>("builtin.standard")); auto floor_renderable =
floorRenderable->material->texture_ = grassTexture; my_scene->AddComponent<engine::RenderableComponent>(floor);
floorRenderable->mesh = genCuboidMesh(app.gfxdev(), 10000.0f, 1.0f, 10000.0f, 5000.0f); floor_renderable->material = std::make_shared<engine::resources::Material>(
floorRenderable->shown = true; app.GetResource<engine::resources::Shader>("builtin.standard"));
auto floorCollider = myScene->AddComponent<engine::ColliderComponent>(floor); floor_renderable->material->texture_ = grass_texture;
floorCollider->is_static = true; floor_renderable->mesh =
floorCollider->aabb = { { 0.0f, 0.0f, 0.0f }, { 10000.0f, 1.0f, 10000.0f } }; GenCuboidMesh(app.gfxdev(), 10000.0f, 1.0f, 10000.0f, 5000.0f);
floor_renderable->shown = true;
auto floor_collider =
my_scene->AddComponent<engine::ColliderComponent>(floor);
floor_collider->is_static = true;
floor_collider->aabb = {{0.0f, 0.0f, 0.0f}, {10000.0f, 1.0f, 10000.0f}};
} }
//engine::util::LoadMeshFromFile(myScene, app.GetResourcePath("models/astronaut/astronaut.dae")); engine::util::LoadMeshFromFile(
my_scene, app.GetResourcePath("models/astronaut/astronaut.dae"));
/* skybox */ /* skybox */
{ {
uint32_t skybox = myScene->CreateEntity("skybox"); uint32_t skybox = my_scene->CreateEntity("skybox");
auto skyboxRenderable = myScene->AddComponent<engine::RenderableComponent>(skybox); auto skybox_renderable =
skyboxRenderable->material = std::make_unique<engine::resources::Material>(app.GetResource<engine::resources::Shader>("builtin.skybox")); my_scene->AddComponent<engine::RenderableComponent>(skybox);
skyboxRenderable->material->texture_ = spaceTexture; skybox_renderable->material = std::make_unique<engine::resources::Material>(
skyboxRenderable->mesh = genCuboidMesh(app.gfxdev(), 10.0f, 10.0f, 10.0f, 1.0f, true); app.GetResource<engine::resources::Shader>("builtin.skybox"));
myScene->GetComponent<engine::TransformComponent>(skybox)->position = { -5.0f, -5.0f, -5.0f }; skybox_renderable->material->texture_ = space_texture;
skybox_renderable->mesh =
GenCuboidMesh(app.gfxdev(), 10.0f, 10.0f, 10.0f, 1.0f, true);
my_scene->GetComponent<engine::TransformComponent>(skybox)->position = {
-5.0f, -5.0f, -5.0f};
} }
app.GameLoop(); app.GameLoop();
} }

View File

@ -1,8 +1,11 @@
#pragma once #ifndef ENGINE_TEST_SRC_GAME_H_
#define ENGINE_TEST_SRC_GAME_H_
struct GameSettings { struct GameSettings {
bool enableFrameLimiter; bool enable_frame_limiter;
bool enableValidation; bool enable_validation;
}; };
void playGame(GameSettings settings); void PlayGame(GameSettings settings);
#endif

View File

@ -1,28 +1,25 @@
#include "config.h" #include <string.h>
#include "game.hpp"
#include <exception>
#include <unordered_set>
// engine
#include "logger.h" #include "logger.h"
#include "window.h" #include "window.h"
// standard library #include "config.h"
#include <exception> #include "game.hpp"
#include <unordered_set>
#include <string.h>
int main(int argc, char* argv[])
{
int main(int argc, char* argv[]) {
GameSettings settings{}; GameSettings settings{};
settings.enableFrameLimiter = true; settings.enable_frame_limiter = true;
settings.enableValidation = false; settings.enable_validation = false;
if (argc >= 2) { if (argc >= 2) {
std::unordered_set<std::string> args{}; std::unordered_set<std::string> args{};
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
args.insert(std::string(argv[i])); args.insert(std::string(argv[i]));
} }
if (args.contains("nofpslimit")) settings.enableFrameLimiter = false; if (args.contains("nofpslimit")) settings.enable_frame_limiter = false;
if (args.contains("gpuvalidation")) settings.enableValidation = true; if (args.contains("gpuvalidation")) settings.enable_validation = true;
} }
engine::SetupLog(PROJECT_NAME); engine::SetupLog(PROJECT_NAME);
@ -30,9 +27,8 @@ int main(int argc, char* argv[])
LOG_INFO("{} v{}", PROJECT_NAME, PROJECT_VERSION); LOG_INFO("{} v{}", PROJECT_NAME, PROJECT_VERSION);
try { try {
playGame(settings); PlayGame(settings);
} } catch (const std::exception& e) {
catch (const std::exception& e) {
LOG_CRITICAL("{}", e.what()); LOG_CRITICAL("{}", e.what());
engine::Window::ErrorBox(e.what()); engine::Window::ErrorBox(e.what());
return EXIT_FAILURE; return EXIT_FAILURE;

View File

@ -1,44 +1,42 @@
#include "meshgen.hpp" #include "meshgen.hpp"
#include "resources/mesh.h" #include <stdexcept>
#include <glm/gtc/constants.hpp> #include <glm/gtc/constants.hpp>
#include <glm/ext.hpp> #include <glm/ext.hpp>
#include <glm/trigonometric.hpp> #include <glm/trigonometric.hpp>
#include <stdexcept> #include "resources/mesh.h"
std::unique_ptr<engine::resources::Mesh> genSphereMesh(engine::GFXDevice* gfx, float r, int detail, bool windInside, bool flipNormals) std::unique_ptr<engine::resources::Mesh> GenSphereMesh(engine::GFXDevice* gfx,
{ float r, int detail,
bool wind_inside,
bool flip_normals) {
using namespace glm; using namespace glm;
std::vector<engine::Vertex> vertices{}; std::vector<engine::Vertex> vertices{};
float angleStep = two_pi<float>() / (float)detail; const float angle_step = two_pi<float>() / (float)detail;
for (int i = 0; i < detail; i++) { for (int i = 0; i < detail; i++) {
// theta goes north-to-south // theta goes north-to-south
float theta = i * angleStep; float theta = i * angle_step;
float theta2 = theta + angleStep; float theta2 = theta + angle_step;
for (int j = 0; j < detail / 2; j++) { for (int j = 0; j < detail / 2; j++) {
// phi goes west-to-east // phi goes west-to-east
float phi = j * angleStep; float phi = j * angle_step;
float phi2 = phi + angleStep; float phi2 = phi + angle_step;
vec3 top_left{ r * sin(phi) * cos(theta), vec3 top_left{r * sin(phi) * cos(theta), r * cos(phi),
r * cos(phi),
r * sin(phi) * sin(theta)}; r * sin(phi) * sin(theta)};
vec3 bottom_left{ r * sin(phi) * cos(theta2), vec3 bottom_left{r * sin(phi) * cos(theta2), r * cos(phi),
r * cos(phi),
r * sin(phi) * sin(theta2)}; r * sin(phi) * sin(theta2)};
vec3 top_right{ r * sin(phi2) * cos(theta), vec3 top_right{r * sin(phi2) * cos(theta), r * cos(phi2),
r * cos(phi2),
r * sin(phi2) * sin(theta)}; r * sin(phi2) * sin(theta)};
vec3 bottom_right{ r * sin(phi2) * cos(theta2), vec3 bottom_right{r * sin(phi2) * cos(theta2), r * cos(phi2),
r * cos(phi2),
r * sin(phi2) * sin(theta2)}; r * sin(phi2) * sin(theta2)};
if (windInside == false) { if (wind_inside == false) {
// tris are visible from outside the sphere // tris are visible from outside the sphere
// triangle 1 // triangle 1
@ -50,8 +48,7 @@ std::unique_ptr<engine::resources::Mesh> genSphereMesh(engine::GFXDevice* gfx, f
vertices.push_back({top_left, {}, {0.0f, 0.0f}}); vertices.push_back({top_left, {}, {0.0f, 0.0f}});
vertices.push_back({bottom_right, {}, {1.0f, 1.0f}}); vertices.push_back({bottom_right, {}, {1.0f, 1.0f}});
} } else {
else {
// tris are visible from inside the sphere // tris are visible from inside the sphere
// triangle 1 // triangle 1
@ -63,7 +60,6 @@ std::unique_ptr<engine::resources::Mesh> genSphereMesh(engine::GFXDevice* gfx, f
vertices.push_back({bottom_right, {}, {1.0f, 1.0f}}); vertices.push_back({bottom_right, {}, {1.0f, 1.0f}});
vertices.push_back({top_left, {}, {0.0f, 0.0f}}); vertices.push_back({top_left, {}, {0.0f, 0.0f}});
vertices.push_back({top_right, {}, {1.0f, 0.0f}}); vertices.push_back({top_right, {}, {1.0f, 0.0f}});
} }
vec3 vector1 = (vertices.end() - 1)->pos - (vertices.end() - 2)->pos; vec3 vector1 = (vertices.end() - 1)->pos - (vertices.end() - 2)->pos;
@ -72,25 +68,23 @@ std::unique_ptr<engine::resources::Mesh> genSphereMesh(engine::GFXDevice* gfx, f
// NORMALS HAVE BEEN FIXED // NORMALS HAVE BEEN FIXED
if (flipNormals) if (flip_normals) norm = -norm;
norm = -norm;
if (j == (detail / 2) - 1) if (j == (detail / 2) - 1) norm = -norm;
norm = -norm;
for (auto it = vertices.end() - 6; it != vertices.end(); it++) { for (auto it = vertices.end() - 6; it != vertices.end(); it++) {
it->norm = norm; it->norm = norm;
} }
} }
} }
return std::make_unique<engine::resources::Mesh>(gfx, vertices); return std::make_unique<engine::resources::Mesh>(gfx, vertices);
} }
std::unique_ptr<engine::resources::Mesh> genCuboidMesh(engine::GFXDevice* gfx, float x, float y, float z, float tiling, bool windInside) std::unique_ptr<engine::resources::Mesh> GenCuboidMesh(engine::GFXDevice* gfx,
{ float x, float y,
float z, float tiling,
bool wind_inside) {
// x goes -> // x goes ->
// y goes ^ // y goes ^
// z goes into the screen // z goes into the screen
@ -147,12 +141,11 @@ std::unique_ptr<engine::resources::Mesh> genCuboidMesh(engine::GFXDevice* gfx, f
v.push_back({{0.0f, y, z}, {0.0f, 1.0f, 0.0f}, {0.0f, tiling}}); 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}}); v.push_back({{x, y, z}, {0.0f, 1.0f, 0.0f}, {tiling, tiling}});
if (windInside) { if (wind_inside) {
for (size_t i = 0; i < v.size(); i += 3) { for (size_t i = 0; i < v.size(); i += 3) {
std::swap(v[i], v[i + 2]); std::swap(v[i], v[i + 2]);
} }
} }
return std::make_unique<engine::resources::Mesh>(gfx, v); return std::make_unique<engine::resources::Mesh>(gfx, v);
} }

View File

@ -1,8 +1,16 @@
#pragma once #ifndef ENGINE_TEST_SRC_MESHGEN_H_
#define ENGINE_TEST_SRC_MESHGEN_H_
#include <memory> #include <memory>
#include "resources/mesh.h" #include "resources/mesh.h"
// generates a UV sphere std::unique_ptr<engine::resources::Mesh> GenSphereMesh(
std::unique_ptr<engine::resources::Mesh> genSphereMesh(engine::GFXDevice* gfx, float r, int detail, bool windInside = false, bool flipNormals = false); engine::GFXDevice* gfx, float r, int detail, bool wind_inside = false,
std::unique_ptr<engine::resources::Mesh> genCuboidMesh(engine::GFXDevice* gfx, float x, float y, float z, float tiling = 1.0f, bool windInside = false); bool flip_normals = false);
std::unique_ptr<engine::resources::Mesh> GenCuboidMesh(
engine::GFXDevice* gfx, float x, float y, float z, float tiling = 1.0f,
bool wind_inside = false);
#endif

View File

@ -1,21 +0,0 @@
#if 0
#include "terrain.hpp"
#include "resources/mesh.hpp"
std::unique_ptr<engine::resources::Mesh> getChunkMesh(int x, int y)
{
(void)x;
(void)y;
std::vector<Vertex> 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<engine::resources::Mesh>(vertices);
}
#endif

View File

@ -1,9 +0,0 @@
#pragma once
#include <memory>
namespace engine::resources {
class Mesh;
}
std::unique_ptr<engine::resources::Mesh> getChunkMesh(int x, int y);