Skip to content

Commit

Permalink
Added TransformGroups
Browse files Browse the repository at this point in the history
  • Loading branch information
matt77hias committed Apr 23, 2017
1 parent 7323f4f commit 2ebe1c0
Show file tree
Hide file tree
Showing 21 changed files with 231 additions and 152 deletions.
56 changes: 41 additions & 15 deletions MAGE/FPS/src/core/FPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,57 +49,83 @@ class TestScene final : public Scene {

virtual void Load() override {

//---------------------------------------------------------------------
// Camera
//---------------------------------------------------------------------
SharedPtr< Camera > camera = CreatePerspectiveCamera("camera");
camera->GetTransform()->SetTranslation(0.0f, 2.0f, 0.0f);
SetCamera(camera);

// ModelDescriptor

//---------------------------------------------------------------------
// ModelDescriptors
//---------------------------------------------------------------------
MeshDescriptor< VertexPositionNormalTexture > mesh_desc(true, true);
SharedPtr< ModelDescriptor > model_desc_sponza = CreateModelDescriptor(L"assets/models/sponza/sponza.mdl", mesh_desc);
SharedPtr< ModelDescriptor > model_desc_sphere = CreateModelDescriptor(L"assets/models/sphere/sphere.mdl", mesh_desc);
// Model
//---------------------------------------------------------------------
// Models
//---------------------------------------------------------------------
SharedPtr< MeshModel > model_sponza(new MeshModel("sponza", *model_desc_sponza));
model_sponza->GetTransform()->SetScale(10.0f);
GetWorld()->AddModel(model_sponza);
SharedPtr< MeshModel > model_sphere(new MeshModel("sphere", *model_desc_sphere));
model_sphere->GetTransform()->AddTranslationY(0.5f);
GetWorld()->AddModel(model_sphere);

// Light

//---------------------------------------------------------------------
// Lights
//---------------------------------------------------------------------
SharedPtr< OmniLight > omni_light(new OmniLight("omnilight", RGBSpectrum(1.0f, 1.0f, 1.0f)));
omni_light->GetTransform()->SetTranslationY(2.0f);
omni_light->SetDistanceFalloff(0.0f, 2.0f);
GetWorld()->AddLight(omni_light);
SharedPtr< SpotLight > spot_light(new SpotLight("spotlight", RGBSpectrum(1.0f, 1.0f, 1.0f)));
spot_light->GetTransform()->SetTranslation(0.0f, 2.0f, 0.0f);
spot_light->SetDistanceFalloff(0.0f, 3.0f);
camera->AddChild(spot_light);
GetWorld()->AddLight(spot_light);


//---------------------------------------------------------------------
// Texture
SharedPtr< Texture > texture = CreateTexture(L"assets/sprites/mage.dds");
//---------------------------------------------------------------------
SharedPtr< Texture > texture_logo = CreateTexture(L"assets/sprites/mage.dds");
//---------------------------------------------------------------------
// Image
SharedPtr< SpriteImage > image(new SpriteImage("image", texture));
image->GetTransform()->SetScale(0.25f, 0.25f);
image->GetTransform()->SetNormalizedTranslation(0.90f, 0.88f);
GetWorld()->AddSprite(image);
//---------------------------------------------------------------------
SharedPtr< SpriteImage > logo(new SpriteImage("logo", texture_logo));
logo->GetTransform()->SetScale(0.25f, 0.25f);
logo->GetTransform()->SetNormalizedTranslation(0.90f, 0.88f);
GetWorld()->AddSprite(logo);


//---------------------------------------------------------------------
// Font
//---------------------------------------------------------------------
SharedPtr< SpriteFont > font = CreateFont(L"assets/fonts/consolas.spritefont", SpriteFontDescriptor());
//---------------------------------------------------------------------
// Text
//---------------------------------------------------------------------
SharedPtr< SpriteText > text(new NormalSpriteText("text", font));
GetWorld()->AddSprite(text);


//---------------------------------------------------------------------
// Scripts
//---------------------------------------------------------------------
//SharedPtr< BehaviorScript > script(new TestScript(model_teapot));
//AddScript(script);
SharedPtr< BehaviorScript > controller(new FPSInputControllerScript(camera->GetTransform()));
AddScript(controller);

SharedPtr< BehaviorScript > stats(new StatsScript(text));
AddScript(stats);
SharedPtr< BehaviorScript > wf(new WireframeScript());
AddScript(wf);
TransformGroup transform(camera->GetTransform());
transform.AddTransform(spot_light->GetTransform());

SharedPtr< BehaviorScript > controller_script(new FPSInputControllerScript(transform));
AddScript(controller_script);
SharedPtr< BehaviorScript > stats_script(new StatsScript(text));
AddScript(stats_script);
SharedPtr< BehaviorScript > wireframe_script(new WireframeScript());
AddScript(wireframe_script);
}
};

Expand Down
3 changes: 2 additions & 1 deletion MAGE/MAGE.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<ClInclude Include="MAGE\src\math\math_utils.hpp" />
<ClInclude Include="MAGE\src\math\sprite_transform.hpp" />
<ClInclude Include="MAGE\src\math\transform.hpp" />
<ClInclude Include="MAGE\src\math\transform_group.hpp" />
<ClInclude Include="MAGE\src\math\view_frustum.hpp" />
<ClInclude Include="MAGE\src\memory\allocation.hpp" />
<ClInclude Include="MAGE\src\memory\memory.hpp" />
Expand Down Expand Up @@ -399,6 +400,7 @@
<ItemGroup>
<None Include="MAGE\src\binary\binary_reader.tpp" />
<None Include="MAGE\src\binary\binary_writer.tpp" />
<None Include="MAGE\src\math\transform_group.tpp" />
<None Include="MAGE\src\memory\memory_arena.tpp" />
<None Include="MAGE\src\mesh\msh\msh_loader.tpp" />
<None Include="MAGE\src\mesh\msh\msh_reader.tpp" />
Expand All @@ -420,7 +422,6 @@
<None Include="MAGE\src\scripting\variable_script.tpp" />
<None Include="MAGE\src\text\sprite_text.tpp" />
<None Include="MAGE\src\world\world.tpp" />
<None Include="MAGE\src\world\world_object.tpp" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{28DC5FAC-C856-43E1-828E-BEAA8A0E2CE4}</ProjectGuid>
Expand Down
7 changes: 5 additions & 2 deletions MAGE/MAGE.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,9 @@
<ClInclude Include="MAGE\src\light\light_buffer.hpp">
<Filter>Header Files\light</Filter>
</ClInclude>
<ClInclude Include="MAGE\src\math\transform_group.hpp">
<Filter>Header Files\math</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="MAGE\src\core\engine.cpp">
Expand Down Expand Up @@ -1011,8 +1014,8 @@
<None Include="MAGE\src\rendering\structured_buffer.tpp">
<Filter>Header Files\rendering</Filter>
</None>
<None Include="MAGE\src\world\world_object.tpp">
<Filter>Header Files\world</Filter>
<None Include="MAGE\src\math\transform_group.tpp">
<Filter>Header Files\math</Filter>
</None>
</ItemGroup>
</Project>
10 changes: 1 addition & 9 deletions MAGE/MAGE/src/light/light_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@

#include "light\omni_light.hpp"
#include "light\spot_light.hpp"

#pragma endregion

//-----------------------------------------------------------------------------
// System Includes
//-----------------------------------------------------------------------------
#pragma region

#include <stdint.h>
#include "collection\collection.hpp"

#pragma endregion

Expand Down
1 change: 0 additions & 1 deletion MAGE/MAGE/src/light/omni_light.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#pragma region

#include "light\light.hpp"
#include "memory\allocation.hpp"

#pragma endregion

Expand Down
1 change: 0 additions & 1 deletion MAGE/MAGE/src/light/spot_light.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#pragma region

#include "light\light.hpp"
#include "memory\allocation.hpp"

#pragma endregion

Expand Down
77 changes: 77 additions & 0 deletions MAGE/MAGE/src/math/transform_group.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#pragma once

//-----------------------------------------------------------------------------
// Engine Includes
//-----------------------------------------------------------------------------
#pragma region

#include "math\transform.hpp"
#include "collection\collection.hpp"

#pragma endregion

//-----------------------------------------------------------------------------
// Engine Declarations and Definitions
//-----------------------------------------------------------------------------
namespace mage {

struct TransformGroup final {

public:

//---------------------------------------------------------------------
// Constructors and Destructors
//---------------------------------------------------------------------

TransformGroup() = default;
TransformGroup(Transform *transform)
: TransformGroup() {
AddTransform(transform);
}
TransformGroup(const TransformGroup &transform_group) = default;
TransformGroup(TransformGroup &&transform_group) = default;
~TransformGroup() {
m_transforms.clear();
m_transform_groups.clear();
}

//---------------------------------------------------------------------
// Assignment Operators
//---------------------------------------------------------------------

TransformGroup &operator=(const TransformGroup &transform_group) = delete;
TransformGroup &operator=(TransformGroup &&transform_group) = delete;

//---------------------------------------------------------------------
// Member Methods
//---------------------------------------------------------------------

void AddTransform(Transform *transform) {
m_transforms.push_back(transform);
}
void AddTransform(const TransformGroup &transform) {
m_transform_groups.push_back(transform);
}

template< typename ActionT >
void ForEachTransform(ActionT action) const;

private:

//---------------------------------------------------------------------
// Member Variables
//---------------------------------------------------------------------

vector< Transform * > m_transforms;
vector< TransformGroup > m_transform_groups;
};
}

//-----------------------------------------------------------------------------
// Engine Includes
//-----------------------------------------------------------------------------
#pragma region

#include "math\transform_group.tpp"

#pragma endregion
19 changes: 19 additions & 0 deletions MAGE/MAGE/src/math/transform_group.tpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

//-----------------------------------------------------------------------------
// Engine Definitions
//-----------------------------------------------------------------------------
namespace mage {

template< typename ActionT >
inline void TransformGroup::ForEachTransform(ActionT action) const {

for (vector< Transform * >::const_iterator it = m_transforms.cbegin(); it != m_transforms.cend(); ++it) {
action(**it);
}

for (vector< TransformGroup >::const_iterator it = m_transform_groups.cbegin(); it != m_transform_groups.cend(); ++it) {
it->ForEachTransform(action);
}
}
}
2 changes: 0 additions & 2 deletions MAGE/MAGE/src/model/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ namespace mage {
}

void Model::UpdateChildTransforms(bool dirty_ancestor) {
WorldObject::UpdateChildTransforms(dirty_ancestor);

ForEachSubModel([&](SubModel &submodel) {
submodel.UpdateTransform(
GetTransform()->GetWorldToObjectMatrix(),
Expand Down
16 changes: 8 additions & 8 deletions MAGE/MAGE/src/scene/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ namespace mage {
UpdateTransforms();
}
void Scene::UpdateTransforms() {
// Traverse the scene graphs (root world objects) which can contain lights as childs.
m_world->ForEachModel([](WorldObject &world_object) {
world_object.UpdateTransform();
});
// Traverse the camera (no transformations will be re-calculated).
// Update the camera transform.
m_camera->UpdateTransform();
// Traverse the lights (no transformations will be re-calculated).
m_world->ForEachLight([](WorldObject &world_object) {
world_object.UpdateTransform();
// Update the model transforms.
m_world->ForEachModel([](Model &model) {
model.UpdateTransform();
});
// Update the light transforms.
m_world->ForEachLight([](Light &light) {
light.UpdateTransform();
});
}
void Scene::Update(double delta_time) {
Expand Down
34 changes: 24 additions & 10 deletions MAGE/MAGE/src/script/character_motor_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,38 @@ namespace mage {
const float delta = static_cast< float >(delta_time);

const float movement_magnitude = delta * m_velocity;
const float movement_cos = cos(m_transform->GetRotationY()) * movement_magnitude;
const float movement_sin = sin(m_transform->GetRotationY()) * movement_magnitude;

if (keyboard->GetKeyPress(DIK_UP, true) || keyboard->GetKeyPress(DIK_W, true)) {
m_transform->AddTranslationX(movement_sin);
m_transform->AddTranslationZ(movement_cos);
m_transform.ForEachTransform([=](Transform &transform) {
const float movement_cos = cos(transform.GetRotationY()) * movement_magnitude;
const float movement_sin = sin(transform.GetRotationY()) * movement_magnitude;
transform.AddTranslationX(movement_sin);
transform.AddTranslationZ(movement_cos);
});
}
else if (keyboard->GetKeyPress(DIK_DOWN, true) || keyboard->GetKeyPress(DIK_S, true)) {
m_transform->AddTranslationX(-movement_sin);
m_transform->AddTranslationZ(-movement_cos);
m_transform.ForEachTransform([=](Transform &transform) {
const float movement_cos = cos(transform.GetRotationY()) * movement_magnitude;
const float movement_sin = sin(transform.GetRotationY()) * movement_magnitude;
transform.AddTranslationX(-movement_sin);
transform.AddTranslationZ(-movement_cos);
});
}
else if (keyboard->GetKeyPress(DIK_RIGHT, true) || keyboard->GetKeyPress(DIK_D, true)) {
m_transform->AddTranslationX(movement_cos);
m_transform->AddTranslationZ(-movement_sin);
m_transform.ForEachTransform([=](Transform &transform) {
const float movement_cos = cos(transform.GetRotationY()) * movement_magnitude;
const float movement_sin = sin(transform.GetRotationY()) * movement_magnitude;
transform.AddTranslationX(movement_cos);
transform.AddTranslationZ(-movement_sin);
});
}
else if (keyboard->GetKeyPress(DIK_LEFT, true) || keyboard->GetKeyPress(DIK_A, true)) {
m_transform->AddTranslationX(-movement_cos);
m_transform->AddTranslationZ(movement_sin);
m_transform.ForEachTransform([=](Transform &transform) {
const float movement_cos = cos(transform.GetRotationY()) * movement_magnitude;
const float movement_sin = sin(transform.GetRotationY()) * movement_magnitude;
transform.AddTranslationX(-movement_cos);
transform.AddTranslationZ(movement_sin);
});
}
}
}
8 changes: 4 additions & 4 deletions MAGE/MAGE/src/script/character_motor_script.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#pragma region

#include "scripting\behavior_script.hpp"
#include "math\transform.hpp"
#include "math\transform_group.hpp"

#pragma endregion

Expand All @@ -22,8 +22,8 @@ namespace mage {
//---------------------------------------------------------------------
// Constructors and Destructors
//---------------------------------------------------------------------

explicit CharacterMotorScript(Transform *transform)
explicit CharacterMotorScript(const TransformGroup &transform)
: BehaviorScript(), m_transform(transform), m_velocity(2.0f) {}
CharacterMotorScript(const CharacterMotorScript &script) = delete;
CharacterMotorScript(CharacterMotorScript &&script) = default;
Expand Down Expand Up @@ -55,7 +55,7 @@ namespace mage {
// Member Variables
//---------------------------------------------------------------------

Transform * const m_transform;
const TransformGroup m_transform;
float m_velocity;
};
}
2 changes: 1 addition & 1 deletion MAGE/MAGE/src/script/fps_input_controller_script.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace mage {
// Constructors and Destructors
//---------------------------------------------------------------------

explicit FPSInputControllerScript(Transform *transform)
explicit FPSInputControllerScript(const TransformGroup &transform)
: BehaviorScript(),
m_orientation_script(new MouseLookScript(transform)),
m_movement_script(new CharacterMotorScript(transform)) {}
Expand Down
Loading

0 comments on commit 2ebe1c0

Please sign in to comment.