Skip to content

Commit

Permalink
Add EnvironmentComponent (background only)
Browse files Browse the repository at this point in the history
  • Loading branch information
Benualdo committed Oct 30, 2024
1 parent 14d98e7 commit a204909
Show file tree
Hide file tree
Showing 16 changed files with 254 additions and 212 deletions.
8 changes: 4 additions & 4 deletions Renderer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
<Property type="Bool" name="m_toolMode" value="false"/>
<Property type="EnumU32" name="m_debugDisplayMode" value="None"/>
<Property type="EnumU8" name="m_lightingMode" value="Forward"/>
<Property type="EnumU8" name="m_HDRmode" value="HDR16"/>
<Property type="EnumU8" name="m_msaa" value="None"/>
<Property type="EnumU8" name="m_HDRmode" value="None"/>
<Property type="EnumU8" name="m_msaa" value="MSAA4X"/>
<Property type="EnumU8" name="m_VSync" value="VSync_1"/>
<Property type="Bool" name="m_rayTracing" value="false"/>
<Property type="Bool" name="m_postProcess" value="true"/>
<Property type="EnumU8" name="m_aaPostProcess" value="FXAA"/>
<Property type="EnumU8" name="m_aaPostProcess" value="None"/>
<Property type="EnumFlagsU32" name="m_renderPassFlags" flags="Bitfield" value="ZPrepass|Opaque|Transparency"/>
<Property type="EnumFlagsU32" name="m_displayFlags" flags="Bitfield" value="AlbedoMap|NormalMap"/>
<Property type="Bool" name="m_wireframe" flags="SingleLine" value="false"/>
<Property type="Bool" name="m_aabb" flags="SingleLine" value="false"/>
<Property type="Bool" name="m_debugUI" flags="SingleLine" value="false"/>
<Property type="Float4" name="m_backgroundColor" flags="Color" x="0.48101264" y="0.7895999" z="1" w="0"/>
<Property type="Float4" name="m_defaultClearColor" flags="Color" x="0.50196081" y="0.50196081" z="0.50196081" w="1"/>
</Object>
</Root>
241 changes: 65 additions & 176 deletions data/Scenes/Aiguelongue.scene

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/core/IWorld.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "core/Object/Object.h"
#include "core/Object/Update.h"

namespace vg::core
{
Expand Down Expand Up @@ -41,6 +42,10 @@ namespace vg::core
virtual void OnPause () = 0;
virtual void OnResume () = 0;

using Context = WorldUpdateContext;
virtual void BeforeUpdate (const Context & _context) = 0;
virtual void AfterUpdate (const Context & _context) = 0;

virtual bool SetActiveScene (IBaseScene * _scene, BaseSceneType _sceneType) = 0;
virtual core::IBaseScene * GetActiveScene (BaseSceneType _sceneType) const = 0;

Expand All @@ -58,5 +63,8 @@ namespace vg::core
virtual IPhysicsWorld * GetPhysicsWorld () const = 0;

virtual bool IsPrefabWorld () const = 0;

virtual void SetEnvironmentColor (const core::float4 & _environmentColor) = 0;
virtual core::float4 GetEnvironmentColor () const = 0;
};
}
18 changes: 14 additions & 4 deletions src/core/Object/Update.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,30 @@ namespace vg::core
class IWorld;
class IGameObject;

struct GameObjectUpdateContext
struct WorldUpdateContext
{
GameObjectUpdateContext(bool _playing, bool _paused, float _dt, IWorld * _world) :
WorldUpdateContext(bool _playing, bool _paused, float _dt) :
m_playing(_playing),
m_paused(_paused),
m_dt(_dt),
m_world(_world)
m_dt(_dt)
{

}

bool m_playing : 1;
bool m_paused : 1;
float m_dt;
};

struct GameObjectUpdateContext : public WorldUpdateContext
{
GameObjectUpdateContext(const WorldUpdateContext & _context, IWorld * _world) :
WorldUpdateContext(_context),
m_world(_world)
{

}

IWorld * m_world;
};

Expand Down
1 change: 1 addition & 0 deletions src/editor/Editor_Consts.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ namespace vg::editor
inline static const char * EditorView = ICON_FA_DESKTOP;
inline static const char * Engine = ICON_FA_GEAR;
inline static const char * Error = ICON_FA_BUG;
inline static const char * Environment = ICON_FA_CLOUD_MOON;
inline static const char * File = ICON_FA_FILE;
inline static const char * Font = ICON_FA_FONT;
inline static const char * FPS = ICON_FA_STOPWATCH;
Expand Down
47 changes: 47 additions & 0 deletions src/engine/Component/Environment/EnvironmentComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "engine/Precomp.h"
#include "EnvironmentComponent.h"
#include "core/IBaseScene.h"
#include "core/IGameObject.h"
#include "engine/Engine.h"
#include "renderer/IRenderer.h"
#include "editor/Editor_Consts.h"

using namespace vg::core;

namespace vg::engine
{
VG_REGISTER_COMPONENT_CLASS(EnvironmentComponent, "Environment", "Renderer", "Environment settings for scene", editor::style::icon::Environment, 0);

//--------------------------------------------------------------------------------------
bool EnvironmentComponent::registerProperties(IClassDesc & _desc)
{
super::registerProperties(_desc);

registerPropertyEx(EnvironmentComponent, m_environmentColor, "Color", PropertyFlags::Color);

return true;
}

//--------------------------------------------------------------------------------------
EnvironmentComponent::EnvironmentComponent(const core::string & _name, core::IObject * _parent) :
super(_name, _parent)
{

}

//--------------------------------------------------------------------------------------
EnvironmentComponent::~EnvironmentComponent()
{

}

//--------------------------------------------------------------------------------------
void EnvironmentComponent::Update(const Context & _context)
{
// Get the world we're rendering to
if (auto * world = _context.m_gameObject->GetWorld())
{
world->SetEnvironmentColor(m_environmentColor);
}
}
}
19 changes: 19 additions & 0 deletions src/engine/Component/Environment/EnvironmentComponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include "core/Component/Component.h"

namespace vg::engine
{
class EnvironmentComponent : public core::Component
{
public:
VG_CLASS_DECL(EnvironmentComponent, core::Component);
EnvironmentComponent(const core::string & _name, core::IObject * _parent);
~EnvironmentComponent();

void Update(const Context & _context) final override;

private:
core::float4 m_environmentColor = core::float4(0.1f, 0.1f, 0.1f, 1.0f);
};
}
13 changes: 10 additions & 3 deletions src/engine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,8 @@ namespace vg::engine
VG_PROFILE_CPU("FixedUpdate");
for (IWorld * world : GetWorlds())
{
GameObject::Context gameObjectUpdateContext(playing, paused, getWorldDeltaTime(world), world);
const World::Context worldUpdateContext(playing, paused, getWorldDeltaTime(world));
const GameObject::Context gameObjectUpdateContext(worldUpdateContext, world);

for (uint i = 0; i < world->GetSceneCount(BaseSceneType::Scene); ++i)
{
Expand All @@ -837,7 +838,10 @@ namespace vg::engine
VG_PROFILE_CPU("Update");
for (IWorld * world : GetWorlds())
{
GameObject::Context gameObjectUpdateContext(playing, paused, getWorldDeltaTime(world), world);
const World::Context worldUpdateContext(playing, paused, getWorldDeltaTime(world));
world->BeforeUpdate(worldUpdateContext);

const GameObject::Context gameObjectUpdateContext(worldUpdateContext, world);

for (uint i = 0; i < world->GetSceneCount(BaseSceneType::Scene); ++i)
{
Expand All @@ -846,6 +850,8 @@ namespace vg::engine
if (root && asBool(UpdateFlags::Update & root->getUpdateFlags()))
root->Update(gameObjectUpdateContext);
}

world->AfterUpdate(worldUpdateContext);
}
}

Expand All @@ -861,7 +867,8 @@ namespace vg::engine
VG_PROFILE_CPU("LateUpdate");
for (IWorld * world : GetWorlds())
{
GameObject::Context gameObjectUpdateContext(playing, paused, getWorldDeltaTime(world), world);
const World::Context worldUpdateContext(playing, paused, getWorldDeltaTime(world));
const GameObject::Context gameObjectUpdateContext(worldUpdateContext, world);

for (uint i = 0; i < world->GetSceneCount(BaseSceneType::Scene); ++i)
{
Expand Down
9 changes: 9 additions & 0 deletions src/engine/Engine.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
<ClCompile Include="Component\Audio\AudioComponent.cpp">
<Filter>Component\Audio</Filter>
</ClCompile>
<ClCompile Include="Component\Environment\EnvironmentComponent.cpp">
<Filter>Component\Environment</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="Engine.def" />
Expand Down Expand Up @@ -371,6 +374,9 @@
<ClInclude Include="ISoundResource.h" />
<ClInclude Include="IUIImageComponent.h" />
<ClInclude Include="IUIComponent.h" />
<ClInclude Include="Component\Environment\EnvironmentComponent.h">
<Filter>Component\Environment</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="Input">
Expand Down Expand Up @@ -496,5 +502,8 @@
<Filter Include="Component\Physics\Object\MergeStaticCollider">
<UniqueIdentifier>{d9f8099c-01ad-4620-a7c6-40d04d5b256b}</UniqueIdentifier>
</Filter>
<Filter Include="Component\Environment">
<UniqueIdentifier>{c10d46fe-9dd2-4ad6-af98-be48d90ee89d}</UniqueIdentifier>
</Filter>
</ItemGroup>
</Project>
31 changes: 31 additions & 0 deletions src/engine/World/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "core/Math/Math.h"
#include "gfx/IUIRenderer.h"
#include "renderer/IRenderer.h"
#include "renderer/IRendererOptions.h"
#include "renderer/IDebugDraw.h"

#if !VG_ENABLE_INLINE
Expand Down Expand Up @@ -400,4 +401,34 @@ namespace vg::engine
}
}
}

//--------------------------------------------------------------------------------------
void World::BeforeUpdate(const Context & _context)
{
// Use default background clear color from options if not overriden by environment
m_nextEnvironmentColor = Engine::get()->GetRenderer()->GetOptions()->GetDefaultClearColor();
}

//--------------------------------------------------------------------------------------
void World::SetEnvironmentColor(const core::float4 & _environmentColor)
{
m_nextEnvironmentColor = _environmentColor;
}

//--------------------------------------------------------------------------------------
void World::AfterUpdate(const Context & _context)
{
if (any(m_nextEnvironmentColor != m_currentEnvironmentColor))
{
// We must reset the render target pool when clear color actually changed
Engine::get()->GetRenderer()->SetResized();
m_currentEnvironmentColor = m_nextEnvironmentColor;
}
}

//--------------------------------------------------------------------------------------
core::float4 World::GetEnvironmentColor() const
{
return m_currentEnvironmentColor;
}
}
11 changes: 10 additions & 1 deletion src/engine/World/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace vg::engine
//--------------------------------------------------------------------------------------
// A "World" is a container for a list of scenes
//--------------------------------------------------------------------------------------
class World : public core::IWorld
class World final : public core::IWorld
{
public:
VG_CLASS_DECL(World, core::IWorld);
Expand All @@ -31,6 +31,9 @@ namespace vg::engine
void OnPause () final override;
void OnResume () final override;

void BeforeUpdate (const Context & _context) final override;
void AfterUpdate (const Context & _context) final override;

bool SetActiveScene (core::IBaseScene * _scene, core::BaseSceneType _sceneType) final override;
core::IBaseScene * GetActiveScene (core::BaseSceneType _sceneType) const final override;

Expand All @@ -51,6 +54,9 @@ namespace vg::engine

bool IsPrefabWorld () const final override;

void SetEnvironmentColor (const core::float4 & _environmentColor) final override;
core::float4 GetEnvironmentColor () const final override;

protected:
void mergeStaticBodies ();
void clearUI ();
Expand All @@ -61,6 +67,9 @@ namespace vg::engine
core::IDebugDrawData * m_debugDrawData = nullptr;
core::IPhysicsWorld * m_physicsWorld = nullptr;
core::vector<physics::IBody *> m_staticColliders;

core::float4 m_currentEnvironmentColor = (core::float4)0.0f;
core::float4 m_nextEnvironmentColor = (core::float4)0.0f;
};
}

Expand Down
2 changes: 2 additions & 0 deletions src/engine/engine.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<ClCompile Include="Component\Debug\HUD\HUDComponent.cpp" />
<ClCompile Include="Component\Debug\TestComponent\TestComponent.cpp" />
<ClCompile Include="Component\Editor\Snap\SnapComponent.cpp" />
<ClCompile Include="Component\Environment\EnvironmentComponent.cpp" />
<ClCompile Include="Component\Mesh\MeshComponent.cpp" />
<ClCompile Include="Component\Physics\PhysicsComponent.cpp" />
<ClCompile Include="Component\UI\UIComponent.cpp" />
Expand All @@ -61,6 +62,7 @@
<ClInclude Include="Component\Debug\HUD\HUDComponent.h" />
<ClInclude Include="Component\Debug\TestComponent\TestComponent.h" />
<ClInclude Include="Component\Editor\Snap\SnapComponent.h" />
<ClInclude Include="Component\Environment\EnvironmentComponent.h" />
<ClInclude Include="Component\Physics\Object\Body\PhysicsBodyComponent.h" />
<ClInclude Include="Component\Physics\Object\Body\PhysicsBodyComponent.hpp" />
<ClInclude Include="Component\Physics\Object\CharacterController\CharacterControllerComponent.h" />
Expand Down
22 changes: 12 additions & 10 deletions src/renderer/IRendererOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,23 @@ namespace vg::renderer
public:
VG_CLASS_DECL_ABSTRACT(IRendererOptions, core::Options);

virtual bool IsToolModeEnabled () const = 0;
virtual bool IsRayTracingEnabled () const = 0;
virtual const core::float4 & GetDefaultClearColor () const = 0;

virtual gfx::MSAA GetMSAA () const = 0;
virtual bool SetMSAA (gfx::MSAA _msaa) = 0;
virtual bool IsToolModeEnabled () const = 0;
virtual bool IsRayTracingEnabled () const = 0;

virtual gfx::HDR GetHDR () const = 0;
virtual bool SetHDR (gfx::HDR _hdr) = 0;
virtual gfx::MSAA GetMSAA () const = 0;
virtual bool SetMSAA (gfx::MSAA _msaa) = 0;

virtual gfx::HDR GetHDR () const = 0;
virtual bool SetHDR (gfx::HDR _hdr) = 0;

virtual gfx::AAPostProcess GetAAPostProcess () const = 0;
virtual bool SetAAPostProcess (gfx::AAPostProcess _aa) = 0;

virtual gfx::VSync GetVSync () const = 0;
virtual bool SetVSync (gfx::VSync _vsync) = 0;
virtual gfx::AAPostProcess GetAAPostProcess () const = 0;
virtual bool SetAAPostProcess (gfx::AAPostProcess _aa) = 0;

virtual gfx::VSync GetVSync () const = 0;
virtual bool SetVSync (gfx::VSync _vsync) = 0;


};
Expand Down
8 changes: 5 additions & 3 deletions src/renderer/Options/RendererOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ namespace vg::renderer

RendererOptions (const core::string & _name, core::IObject * _parent = nullptr);

const core::float4 & GetDefaultClearColor () const { return m_defaultClearColor; }

bool IsToolModeEnabled () const final override { return isToolModeEnabled(); };
bool IsRayTracingEnabled () const final override { return isRayTracingEnabled(); };

Expand Down Expand Up @@ -79,8 +81,8 @@ namespace vg::renderer
bool isAlbedoMapsEnabled () const { return 0 != (DisplayFlags::AlbedoMap & m_displayFlags); }
bool isNormalMapsEnabled () const { return 0 != (DisplayFlags::NormalMap & m_displayFlags); }

void setBackgroundColor (const core::float4 & _backgroundColor);
core::float4 getBackgroundColor () const { return m_backgroundColor; }
void setDefaultClearColor (const core::float4 & _backgroundColor);
core::float4 getDefaultClearColor () const { return m_defaultClearColor; }

void update () const;

Expand All @@ -90,7 +92,7 @@ namespace vg::renderer
void applyMSAA (const core::IProperty * _prop);

private:
core::float4 m_backgroundColor = core::float4(0, 0, 0, 0);
core::float4 m_defaultClearColor = core::float4(0, 0, 0, 0);
bool m_toolMode = true;
bool m_aabb = false;
bool m_wireframe = false;
Expand Down
Loading

0 comments on commit a204909

Please sign in to comment.