From f4120095ed53df57bda28fcce7238175f94e0e03 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Tue, 26 Nov 2024 18:44:52 -0600 Subject: [PATCH] Allowed scene items cleanup to happen earlier to prevent access null weak pointers. Also added Sphere Mesh and Sphere Collider(s) --- src/dawn/component/physics/BoxCollider.cpp | 14 ----- src/dawn/component/physics/CMakeLists.txt | 3 +- src/dawn/component/physics/Collider.cpp | 61 ++++++++++++++++++- src/dawn/component/physics/Collider.hpp | 38 +++++++++++- src/dawn/component/physics/CubeCollider.cpp | 23 +++++++ src/dawn/component/physics/CubeCollider.hpp | 32 ++++++++++ src/dawn/component/physics/SphereCollider.cpp | 12 ++++ .../{BoxCollider.hpp => SphereCollider.hpp} | 4 +- src/dawn/display/mesh/CMakeLists.txt | 1 + src/dawn/display/mesh/SphereMesh.cpp | 44 +++++++++++++ src/dawn/display/mesh/SphereMesh.hpp | 27 ++++++++ src/dawn/game/IGame.cpp | 28 +++++++-- src/dawn/game/IGame.hpp | 8 +++ src/dawn/physics/PhysicsManager.cpp | 31 ---------- src/dawn/scene/Scene.cpp | 22 ++++++- src/dawn/scene/Scene.hpp | 7 ++- src/dawn/scene/SceneComponent.cpp | 10 +-- src/dawn/scene/SceneItem.cpp | 25 +++++--- src/dawn/scene/SceneItem.hpp | 6 ++ src/dawnglfw/display/RenderHost.cpp | 2 +- src/dawnlinux/main.cpp | 1 + src/dawnrpg/scene/HelloWorldScene.cpp | 60 ++++++++++++++++-- 22 files changed, 380 insertions(+), 79 deletions(-) delete mode 100644 src/dawn/component/physics/BoxCollider.cpp create mode 100644 src/dawn/component/physics/CubeCollider.cpp create mode 100644 src/dawn/component/physics/CubeCollider.hpp create mode 100644 src/dawn/component/physics/SphereCollider.cpp rename src/dawn/component/physics/{BoxCollider.hpp => SphereCollider.hpp} (77%) create mode 100644 src/dawn/display/mesh/SphereMesh.cpp create mode 100644 src/dawn/display/mesh/SphereMesh.hpp diff --git a/src/dawn/component/physics/BoxCollider.cpp b/src/dawn/component/physics/BoxCollider.cpp deleted file mode 100644 index 6d19d94d..00000000 --- a/src/dawn/component/physics/BoxCollider.cpp +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright (c) 2024 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#include "BoxCollider.hpp" - -using namespace Dawn; - -std::shared_ptr BoxCollider::getShapeSettings() { - return std::make_shared( - JPH::Vec3(shape.x, shape.y, shape.z) - ); -} \ No newline at end of file diff --git a/src/dawn/component/physics/CMakeLists.txt b/src/dawn/component/physics/CMakeLists.txt index 79337608..386b4d44 100644 --- a/src/dawn/component/physics/CMakeLists.txt +++ b/src/dawn/component/physics/CMakeLists.txt @@ -6,5 +6,6 @@ target_sources(${DAWN_TARGET_NAME} PRIVATE Collider.cpp - BoxCollider.cpp + CubeCollider.cpp + SphereCollider.cpp ) \ No newline at end of file diff --git a/src/dawn/component/physics/Collider.cpp b/src/dawn/component/physics/Collider.cpp index 48da7a29..72ae11e3 100644 --- a/src/dawn/component/physics/Collider.cpp +++ b/src/dawn/component/physics/Collider.cpp @@ -11,6 +11,29 @@ using namespace Dawn; using namespace JPH; using namespace JPH::literals; +EMotionType Collider::getMotionType(const ColliderType colliderType) { + EMotionType motionType; + + switch(colliderType) { + case ColliderType::DYNAMIC: + return EMotionType::Dynamic; + break; + + case ColliderType::STATIC: + return EMotionType::Static; + break; + + case ColliderType::KINEMATIC: + return EMotionType::Kinematic; + break; + } + + assertUnreachable("Invalid ColliderType"); + return EMotionType::Kinematic; +} + +// + void Collider::onInit() { assertNull(this->body, "Body is not NULL?"); @@ -24,7 +47,7 @@ void Collider::onInit() { shape, RVec3(pos.x, pos.y, pos.z), Quat::sIdentity(), - EMotionType::Dynamic, + Collider::getMotionType(this->colliderType), layer ); @@ -40,10 +63,46 @@ void Collider::onDispose() { getBodyInterface().DestroyBody(this->bodyId); } +void Collider::notifyShapeChanged() { + if(!this->isColliderReady()) return; + + auto settings = this->getShapeSettings(); + auto shapeResult = settings->Create(); + auto shape = shapeResult.Get(); + + getBodyInterface().SetShape( + this->bodyId, + shape, + // TODO: I may not always need to re-activate the body here. + true, + EActivation::Activate + ); +} + +bool_t Collider::isColliderReady() { + return this->body != nullptr; +} + BodyInterface & Collider::getBodyInterface() { return getGame()->physicsManager->getBodyInterface(); } +ColliderType Collider::getColliderType() { + return colliderType; +} + BodyID Collider::getBodyId() { return bodyId; +} + +void Collider::setColliderType(const ColliderType type) { + this->colliderType = type; + + if(!this->isColliderReady()) return; + + getBodyInterface().SetMotionType( + this->bodyId, + Collider::getMotionType(type), + EActivation::Activate// TODO: Should be false on kinematics + ); } \ No newline at end of file diff --git a/src/dawn/component/physics/Collider.hpp b/src/dawn/component/physics/Collider.hpp index c7a07feb..ff59b6b4 100644 --- a/src/dawn/component/physics/Collider.hpp +++ b/src/dawn/component/physics/Collider.hpp @@ -16,10 +16,18 @@ namespace Dawn { class Collider : public SceneComponent { private: JPH::Body *body = nullptr; + ColliderType colliderType = ColliderType::DYNAMIC; + + /** + * Returns the JoltPhysics motion type for the collider type. + * + * @param colliderType The collider type. + * @return The JoltPhysics motion type. + */ + static JPH::EMotionType getMotionType(const ColliderType colliderType); protected: JPH::BodyID bodyId; - JPH::EMotionType emotionType = JPH::EMotionType::Dynamic; JPH::ObjectLayer layer = 1; /** @@ -36,17 +44,41 @@ namespace Dawn { */ JPH::BodyInterface & getBodyInterface(); - public: - ColliderType type = ColliderType::KINEMATIC; + /** + * Callable by subclasses to notify that the shape has changed. + */ + void notifyShapeChanged(); + public: void onInit() override; void onDispose() override; + /** + * Returns whether the collider is ready. + * + * @return Whether the collider is ready. + */ + bool_t isColliderReady(); + + /** + * Returns the collider type. + * + * @return The collider type. + */ + ColliderType getColliderType(); + /** * Returns the JoltPhysics body ID of the collider. * * @return The body ID of the collider. */ JPH::BodyID getBodyId(); + + /** + * Sets the collider type. + * + * @param colliderType The collider type. + */ + void setColliderType(ColliderType colliderType); }; } \ No newline at end of file diff --git a/src/dawn/component/physics/CubeCollider.cpp b/src/dawn/component/physics/CubeCollider.cpp new file mode 100644 index 00000000..234ed214 --- /dev/null +++ b/src/dawn/component/physics/CubeCollider.cpp @@ -0,0 +1,23 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "CubeCollider.hpp" + +using namespace Dawn; + +std::shared_ptr CubeCollider::getShapeSettings() { + return std::make_shared( + JPH::Vec3(shape.x, shape.y, shape.z) + ); +} + +glm::vec3 CubeCollider::getShape() { + return shape; +} + +void CubeCollider::setShape(const glm::vec3 &shape) { + this->shape = shape; + this->notifyShapeChanged(); +} \ No newline at end of file diff --git a/src/dawn/component/physics/CubeCollider.hpp b/src/dawn/component/physics/CubeCollider.hpp new file mode 100644 index 00000000..9ea3f1bd --- /dev/null +++ b/src/dawn/component/physics/CubeCollider.hpp @@ -0,0 +1,32 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "Collider.hpp" + +namespace Dawn { + class CubeCollider : public Collider { + private: + glm::vec3 shape = glm::vec3(1, 1, 1); + + protected: + std::shared_ptr getShapeSettings() override; + + public: + /** + * Returns the shape of the cube collider. + * + * @return The shape of the cube collider. + */ + glm::vec3 getShape(); + + /** + * Sets the shape of the cube collider. + * + * @param shape The shape of the cube collider. + */ + void setShape(const glm::vec3 &shape); + }; +} \ No newline at end of file diff --git a/src/dawn/component/physics/SphereCollider.cpp b/src/dawn/component/physics/SphereCollider.cpp new file mode 100644 index 00000000..74d9192f --- /dev/null +++ b/src/dawn/component/physics/SphereCollider.cpp @@ -0,0 +1,12 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "SphereCollider.hpp" + +using namespace Dawn; + +std::shared_ptr SphereCollider::getShapeSettings() { + return std::make_shared(radius); +} \ No newline at end of file diff --git a/src/dawn/component/physics/BoxCollider.hpp b/src/dawn/component/physics/SphereCollider.hpp similarity index 77% rename from src/dawn/component/physics/BoxCollider.hpp rename to src/dawn/component/physics/SphereCollider.hpp index 10691b72..bf37b518 100644 --- a/src/dawn/component/physics/BoxCollider.hpp +++ b/src/dawn/component/physics/SphereCollider.hpp @@ -7,11 +7,11 @@ #include "Collider.hpp" namespace Dawn { - class BoxCollider : public Collider { + class SphereCollider : public Collider { protected: std::shared_ptr getShapeSettings() override; public: - glm::vec3 shape = glm::vec3(1, 1, 1); + float radius = 1.0f; }; } \ No newline at end of file diff --git a/src/dawn/display/mesh/CMakeLists.txt b/src/dawn/display/mesh/CMakeLists.txt index 741a6ec2..9be93259 100644 --- a/src/dawn/display/mesh/CMakeLists.txt +++ b/src/dawn/display/mesh/CMakeLists.txt @@ -8,4 +8,5 @@ target_sources(${DAWN_TARGET_NAME} PRIVATE CubeMesh.cpp QuadMesh.cpp + SphereMesh.cpp ) \ No newline at end of file diff --git a/src/dawn/display/mesh/SphereMesh.cpp b/src/dawn/display/mesh/SphereMesh.cpp new file mode 100644 index 00000000..878ece0b --- /dev/null +++ b/src/dawn/display/mesh/SphereMesh.cpp @@ -0,0 +1,44 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "SphereMesh.hpp" + +using namespace Dawn; + +void SphereMesh::create( + std::shared_ptr mesh, + const float radius, + const uint32_t segments, + const uint32_t rings +) { + // Create the vertices + std::vector vertices; + for(uint32_t r = 0; r < rings; ++r) { + for(uint32_t s = 0; s < segments; ++s) { + float const y = sin(-M_PI_2 + M_PI * r / rings); + float const x = cos(2 * M_PI * s / segments) * sin(M_PI * r / rings); + float const z = sin(2 * M_PI * s / segments) * sin(M_PI * r / rings); + vertices.push_back(glm::vec3(x, y, z) * radius); + } + } + + // Create the indices + std::vector indices; + for(uint32_t r = 0; r < rings - 1; ++r) { + for(uint32_t s = 0; s < segments - 1; ++s) { + indices.push_back(r * segments + s); + indices.push_back(r * segments + (s + 1)); + indices.push_back((r + 1) * segments + (s + 1)); + + indices.push_back(r * segments + s); + indices.push_back((r + 1) * segments + (s + 1)); + indices.push_back((r + 1) * segments + s); + } + } + + mesh->createBuffers(vertices.size(), indices.size()); + mesh->bufferPositions(0, vertices.data(), vertices.size()); + mesh->bufferIndices(0, indices.data(), indices.size()); +} \ No newline at end of file diff --git a/src/dawn/display/mesh/SphereMesh.hpp b/src/dawn/display/mesh/SphereMesh.hpp new file mode 100644 index 00000000..04e9ba75 --- /dev/null +++ b/src/dawn/display/mesh/SphereMesh.hpp @@ -0,0 +1,27 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "display/mesh/Mesh.hpp" + +namespace Dawn { + class SphereMesh { + public: + /** + * Creates a sphere mesh. + * + * @param mesh The mesh to buffer into. + * @param radius The radius of the sphere. + * @param segments The number of segments. + * @param rings The number of rings. + */ + static void create( + std::shared_ptr mesh, + const float radius = 1.0f, + const uint32_t segments = 16, + const uint32_t rings = 16 + ); + }; +} \ No newline at end of file diff --git a/src/dawn/game/IGame.cpp b/src/dawn/game/IGame.cpp index 9f8c9de6..69fcb253 100644 --- a/src/dawn/game/IGame.cpp +++ b/src/dawn/game/IGame.cpp @@ -5,6 +5,7 @@ #include "game/Game.hpp" #include "scene/Scene.hpp" +#include "util/Flag.hpp" using namespace Dawn; @@ -13,6 +14,9 @@ IGame::IGame() { } void IGame::init() { + assertFlagOff(state, GAME_STATE_INITIALIZED, "Game already initialized?"); + Flag::turnOn(state, GAME_STATE_INITIALIZED); + auto selfAsGame = this->getSelfAsGame(); renderHost = std::make_shared(); @@ -36,12 +40,29 @@ void IGame::init() { nextFrameScene = std::make_shared(selfAsGame, initialScene); } +void IGame::deinit() { + assertFlagOn(state, GAME_STATE_INITIALIZED, "Game not initialized?"); + + if(currentScene) currentScene->deinit(); + currentScene = nullptr; + + if(nextFrameScene) nextFrameScene->deinit(); + nextFrameScene = nullptr; + + physicsManager = nullptr; + assetManager = nullptr; + renderHost = nullptr; + + Flag::turnOff(state, GAME_STATE_INITIALIZED); +} + void IGame::update() { this->assetManager->update(); this->inputManager.update(); if(nextFrameScene) { - nextFrameScene->stage(); + if(currentScene) currentScene->deinit(); + nextFrameScene->init(); currentScene = nextFrameScene; nextFrameScene = nullptr; } @@ -67,8 +88,5 @@ std::shared_ptr IGame::getSelfAsGame() { } IGame::~IGame() { - currentScene = nullptr; - nextFrameScene = nullptr; - assetManager = nullptr; - renderHost = nullptr; + assertFlagOff(state, GAME_STATE_INITIALIZED, "Game not deinited properly?"); } \ No newline at end of file diff --git a/src/dawn/game/IGame.hpp b/src/dawn/game/IGame.hpp index 8dec37e6..0f87ce54 100644 --- a/src/dawn/game/IGame.hpp +++ b/src/dawn/game/IGame.hpp @@ -13,6 +13,8 @@ #include "save/SaveManager.hpp" #include "physics/PhysicsManager.hpp" +#define GAME_STATE_INITIALIZED 0x01 + namespace Dawn { class Scene; class Game; @@ -21,6 +23,7 @@ namespace Dawn { private: std::shared_ptr currentScene = nullptr; std::shared_ptr nextFrameScene = nullptr; + uint8_t state = 0; protected: /** @@ -62,6 +65,11 @@ namespace Dawn { */ void init(); + /** + * Deinitialize the game and all of its components. + */ + void deinit(); + /** * Performs a single update tick on the game engine, and in turn all of * the game's sub systems. diff --git a/src/dawn/physics/PhysicsManager.cpp b/src/dawn/physics/PhysicsManager.cpp index cae32e0d..57a01bca 100644 --- a/src/dawn/physics/PhysicsManager.cpp +++ b/src/dawn/physics/PhysicsManager.cpp @@ -232,38 +232,7 @@ void PhysicsManager::init(const std::shared_ptr &game) { ); physicsSystem.SetBodyActivationListener(&bodyActivationListener); - physicsSystem.SetContactListener(&contactListener); - - BodyInterface &bodyInterface = physicsSystem.GetBodyInterface(); - - BoxShapeSettings floorShapeSettings(Vec3(100.0f, 1.0f, 100.0f)); - floorShapeSettings.SetEmbedded(); - ShapeSettings::ShapeResult floorShapeResult = floorShapeSettings.Create(); - ShapeRefC floorShape = floorShapeResult.Get(); - BodyCreationSettings floorSettings( - floorShape, - RVec3(0.0_r, -1.0_r, 0.0_r), - Quat::sIdentity(), - EMotionType::Static, - Layers::NON_MOVING - ); - Body *floor = bodyInterface.CreateBody(floorSettings); - bodyInterface.AddBody(floor->GetID(), EActivation::DontActivate); - - // BodyCreationSettings sphereSettings( - // new SphereShape(0.5f), - // RVec3(0.0_r, 2.0_r, 0.0_r), - // Quat::sIdentity(), - // EMotionType::Dynamic, - // Layers::MOVING - // ); - // sphereId = bodyInterface.CreateAndAddBody( - // sphereSettings, - // EActivation::Activate - // ); - // bodyInterface.SetLinearVelocity(sphereId, Vec3(0.0f, -5.0f, 0.0f)); - // physicsSystem.OptimizeBroadPhase(); } void PhysicsManager::update() { diff --git a/src/dawn/scene/Scene.cpp b/src/dawn/scene/Scene.cpp index 735baf95..b4fc5d58 100644 --- a/src/dawn/scene/Scene.cpp +++ b/src/dawn/scene/Scene.cpp @@ -16,11 +16,22 @@ Scene::Scene( { } -void Scene::stage() { +void Scene::init() { Scene &selfReference = *this; sceneInitializer(selfReference); } +void Scene::deinit() { + if(!this->hasInitialized) return; + this->hasInitialized = false; + + auto items = this->sceneItems; + for(auto &item : items) { + item->deinit(); + } + sceneItems.clear(); +} + void Scene::update() { // Initialize new scene items if(!hasInitialized) { @@ -34,8 +45,11 @@ void Scene::update() { auto itRemove = sceneItemsToRemove.begin(); while(itRemove != sceneItemsToRemove.end()) { auto item = *itRemove; + item->deinit(); auto it = std::find(sceneItems.begin(), sceneItems.end(), item); - if(it != sceneItems.end()) sceneItems.erase(it); + if(it != sceneItems.end()) { + sceneItems.erase(it); + } itRemove++; } sceneItemsToRemove.clear(); @@ -63,8 +77,12 @@ std::shared_ptr Scene::createSceneItem() { } void Scene::removeItem(const std::shared_ptr item) { + auto index = std::find(sceneItems.begin(), sceneItems.end(), item); + if(index == sceneItems.end()) return; + sceneItemsToRemove.push_back(item); } Scene::~Scene() { + this->deinit(); } \ No newline at end of file diff --git a/src/dawn/scene/Scene.hpp b/src/dawn/scene/Scene.hpp index 095a866f..13035067 100644 --- a/src/dawn/scene/Scene.hpp +++ b/src/dawn/scene/Scene.hpp @@ -44,7 +44,12 @@ namespace Dawn { /** * Stages all of the scene items on the scene. */ - void stage(); + void init(); + + /** + * Called when the scene is supposed to be deinitialized. + */ + void deinit(); /** * Called by the game every frame that the scene is set as the currently diff --git a/src/dawn/scene/SceneComponent.cpp b/src/dawn/scene/SceneComponent.cpp index cc5a0a9b..041fa0dd 100644 --- a/src/dawn/scene/SceneComponent.cpp +++ b/src/dawn/scene/SceneComponent.cpp @@ -57,17 +57,17 @@ bool_t SceneComponent::isInitialized() { } std::shared_ptr SceneComponent::getItem() { - return this->item.lock(); + auto item = this->item.lock(); + assertNotNull(item, "SceneItem has unloaded?"); + return item; } std::shared_ptr SceneComponent::getScene() { - auto item = this->getItem(); - return item->getScene(); + return this->getItem()->getScene(); } std::shared_ptr SceneComponent::getGame() { - auto scene = this->getScene(); - return scene->getGame(); + return this->getScene()->getGame(); } SceneComponent::~SceneComponent() { diff --git a/src/dawn/scene/SceneItem.cpp b/src/dawn/scene/SceneItem.cpp index 12e3ec84..61a44952 100644 --- a/src/dawn/scene/SceneItem.cpp +++ b/src/dawn/scene/SceneItem.cpp @@ -20,7 +20,9 @@ std::shared_ptr SceneItem::sceneItemComponentsSelf() { } std::shared_ptr SceneItem::getScene() { - return scene.lock(); + auto s = scene.lock(); + assertNotNull(s, "Scene has unloaded?"); + return s; } void SceneItem::init() { @@ -50,6 +52,19 @@ void SceneItem::init() { } } +void SceneItem::deinit() { + // Create copy of the components, components may chose to add more components + // but those sub components will not be disposed at this time. + auto components = this->components; + + for(auto &component : components) { + if(!component->isInitialized()) continue; + component->dispose(); + } + + this->components.clear(); +} + void SceneItem::remove() { auto scene = getScene(); if(!scene) return; @@ -57,11 +72,5 @@ void SceneItem::remove() { } SceneItem::~SceneItem() { - std::for_each( - components.begin(), - components.end(), - [](auto &component) { - component->dispose(); - } - ); + this->deinit(); } \ No newline at end of file diff --git a/src/dawn/scene/SceneItem.hpp b/src/dawn/scene/SceneItem.hpp index 32106923..1ca13e09 100644 --- a/src/dawn/scene/SceneItem.hpp +++ b/src/dawn/scene/SceneItem.hpp @@ -35,6 +35,12 @@ namespace Dawn { */ void init(); + /** + * Called when the scene item is supposed to deinitialize. Should happen + * when the scene item is removed from the scene. + */ + void deinit(); + /** * Returns the scene that this scene item belongs to. * diff --git a/src/dawnglfw/display/RenderHost.cpp b/src/dawnglfw/display/RenderHost.cpp index fcc872d2..ff0187e6 100644 --- a/src/dawnglfw/display/RenderHost.cpp +++ b/src/dawnglfw/display/RenderHost.cpp @@ -103,7 +103,7 @@ void RenderHost::update(const std::shared_ptr game) { glDepthFunc(GL_LESS); assertNoGLError(); - // glEnable(GL_DEPTH_TEST); + glEnable(GL_DEPTH_TEST); assertNoGLError(); glEnable(GL_BLEND); diff --git a/src/dawnlinux/main.cpp b/src/dawnlinux/main.cpp index f2cdc23a..c1d8ca27 100644 --- a/src/dawnlinux/main.cpp +++ b/src/dawnlinux/main.cpp @@ -73,6 +73,7 @@ int32_t main(int32_t argc, const char **argv) { game->update(); } + game->deinit(); game = nullptr; return 0; diff --git a/src/dawnrpg/scene/HelloWorldScene.cpp b/src/dawnrpg/scene/HelloWorldScene.cpp index edac668e..3713ce72 100644 --- a/src/dawnrpg/scene/HelloWorldScene.cpp +++ b/src/dawnrpg/scene/HelloWorldScene.cpp @@ -11,17 +11,44 @@ #include "component/display/material/SimpleTexturedMaterial.hpp" #include "component/display/MeshRenderer.hpp" #include "display/mesh/CubeMesh.hpp" -#include "component/physics/BoxCollider.hpp" +#include "display/mesh/SphereMesh.hpp" +#include "component/physics/CubeCollider.hpp" +#include "component/physics/SphereCollider.hpp" using namespace Dawn; void Dawn::helloWorldScene(Scene &s) { auto cameraItem = s.createSceneItem(); auto camera = cameraItem->addComponent(); - cameraItem->lookAt({ 5, 5, 5 }, { 0, 0, 0 }, { 0, 1, 0 }); + cameraItem->lookAt({ 20, 20, 20 }, { 0, 0, 0 }, { 0, 1, 0 }); camera->clipFar = 99999.99f; // Ground + { + // Create the scene item. + auto groundItem = s.createSceneItem(); + groundItem->setLocalPosition(glm::vec3(0, 0, 0)); + + // Create a simple cube mesh. + auto groundMesh = std::make_shared(); + groundMesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT); + CubeMesh::buffer(groundMesh, glm::vec3(-15, -1, -15), glm::vec3(30, 2, 30), 0, 0); + + // Add a renderer to the scene item. + auto groundMeshRenderer = groundItem->addComponent(); + groundMeshRenderer->mesh = groundMesh; + + // Add a material to the scene item. + auto groundMaterial = groundItem->addComponent(); + groundMaterial->setColor(COLOR_LIGHT_GREY); + + // Add collider + auto groundCollider = groundItem->addComponent(); + groundCollider->setColliderType(ColliderType::STATIC); + groundCollider->setShape(glm::vec3(15, 1, 15)); + } + + // Box { // Create the scene item. auto cubeItem = s.createSceneItem(); @@ -41,13 +68,14 @@ void Dawn::helloWorldScene(Scene &s) { cubeMaterial->setColor(COLOR_MAGENTA); // Add collider - auto boxCollider = cubeItem->addComponent(); + auto cubeCollider = cubeItem->addComponent(); } + // Other Box { // Create the scene item. auto cubeItem = s.createSceneItem(); - cubeItem->setLocalPosition(glm::vec3(0.88f, 13, 0.1f)); + cubeItem->setLocalPosition(glm::vec3(0.75f, 15, 0.1f)); // Create a simple cube mesh. auto cubeMesh = std::make_shared(); @@ -63,6 +91,28 @@ void Dawn::helloWorldScene(Scene &s) { cubeMaterial->setColor(COLOR_MAGENTA); // Add collider - auto boxCollider = cubeItem->addComponent(); + auto cubeCollider = cubeItem->addComponent(); + } + + // Ball + { + // Create the scene item. + auto sphereItem = s.createSceneItem(); + sphereItem->setLocalPosition(glm::vec3(-1.0f, 13, -0.6f)); + + // Create a simple cube mesh. + auto sphereMesh = std::make_shared(); + SphereMesh::create(sphereMesh, 1.0f); + + // Add a renderer to the scene item. + auto sphereMeshRenderer = sphereItem->addComponent(); + sphereMeshRenderer->mesh = sphereMesh; + + // Add a material to the scene item. + auto sphereMaterial = sphereItem->addComponent(); + sphereMaterial->setColor(COLOR_CYAN); + + // Add collider + auto sphereCollider = sphereItem->addComponent(); } } \ No newline at end of file