Skip to content

Commit

Permalink
Full support for 3D models and 2D sprites
Browse files Browse the repository at this point in the history
  • Loading branch information
matt77hias committed Apr 10, 2017
1 parent fc01f84 commit f729fba
Show file tree
Hide file tree
Showing 21 changed files with 298 additions and 134 deletions.
32 changes: 20 additions & 12 deletions MAGE/FPS/src/core/FPS.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "stdafx.h"
#include "font\sprite_font.hpp"

using namespace mage;

Expand Down Expand Up @@ -56,28 +55,37 @@ class TestScene : public Scene {

virtual void Load() override {

// @TODO: cameras straight from renderer?
const float width = (float)g_engine->GetRenderer().GetWidth();
const float height = (float)g_engine->GetRenderer().GetHeight();
SharedPtr< Camera > camera(new PerspectiveCamera("camera", width, height));
SetCamera(camera);
// Camera
SharedPtr< Camera > camera = CreatePerspectiveCamera("camera");
camera->GetTransform().SetTranslation(0.0f, 1.0f, -4.0f);
SetCamera(camera);

CombinedShader shader = CreateLambertianShader();

// ModelDescriptor
MeshDescriptor< VertexPositionNormalTexture > mesh_desc(true, true);
SharedPtr< ModelDescriptor > model_desc = CreateModelDescriptor(L"assets/models/cube.obj", mesh_desc);
SharedPtr< Model > test_model(new MeshModel("model", *model_desc, shader));
// Model
SharedPtr< Model > test_model(new MeshModel("model", *model_desc));
GetWorld().AddModel(test_model);

// Light
SharedPtr< PointLight > light(new PointLight("light", 100.0f, RGBSpectrum(0.5f, 0.5f, 0.0f)));
GetWorld().AddLight(light);

// Font
SharedPtr< SpriteFont > font = CreateFont(L"assets/fonts/comicsansms.spritefont", SpriteFontDescriptor());
// Text
SharedPtr< SpriteText > text(new SpriteText("text", L"Hello World!", font));
GetWorld().AddText(text);

// Texture
SharedPtr< Texture > texture = CreateTexture(L"assets/models/seafloor.dds");
// Image
SharedPtr< SpriteImage > image(new SpriteImage("image", texture));
GetWorld().AddImage(image);

// Script
SharedPtr< BehaviorScript > test_script(new TestScript(test_model));
AddScript(test_script);

//TODO
GetWorld().m_font = SharedPtr< SpriteFont >(new SpriteFont(g_engine->GetRenderer().GetDevice(), L"assets/fonts/comicsansms.spritefont", SpriteFontDescriptor()));
}
};

Expand Down
6 changes: 5 additions & 1 deletion MAGE/FPS/src/core/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
#pragma region

#include "core\engine.hpp"

#include "camera\perspective_camera.hpp"
#include "model\meshmodel.hpp"
#include "scripting\behavior_script.hpp"
#include "model\meshmodel.hpp"
#include "light\point_light.hpp"
#include "text\sprite_text.hpp"
#include "sprite\sprite_image.hpp"
#include "shader\lambertian_shader.hpp"

#pragma endregion
2 changes: 2 additions & 0 deletions MAGE/MAGE.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@
<ClInclude Include="MAGE\src\world\world_object.hpp" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="MAGE\src\camera\orthographic_camera.cpp" />
<ClCompile Include="MAGE\src\camera\perspective_camera.cpp" />
<ClCompile Include="MAGE\src\core\engine.cpp" />
<ClCompile Include="MAGE\src\core\version.cpp" />
<ClCompile Include="MAGE\src\font\spritefont\spritefont_loader.cpp" />
Expand Down
10 changes: 8 additions & 2 deletions MAGE/MAGE.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@
<Filter Include="Header Files\text">
<UniqueIdentifier>{207846ed-8a16-4e94-a121-b9b981c5bb24}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\text">
<UniqueIdentifier>{8e8ea5e0-8c99-4c25-b6ad-76568f9c8a31}</UniqueIdentifier>
<Filter Include="Source Files\camera">
<UniqueIdentifier>{387a1d6e-f2b9-464f-91ca-5c2ced312bca}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -722,6 +722,12 @@
<ClCompile Include="MAGE\src\world\world_object.cpp">
<Filter>Source Files\world</Filter>
</ClCompile>
<ClCompile Include="MAGE\src\camera\perspective_camera.cpp">
<Filter>Source Files\camera</Filter>
</ClCompile>
<ClCompile Include="MAGE\src\camera\orthographic_camera.cpp">
<Filter>Source Files\camera</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<FxCompile Include="MAGE\shaders\lambertian.fx">
Expand Down
21 changes: 21 additions & 0 deletions MAGE/MAGE/src/camera/orthographic_camera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//-----------------------------------------------------------------------------
// Engine Includes
//-----------------------------------------------------------------------------
#pragma region

#include "engine.hpp"
#include "camera\orthographic_camera.hpp"

#pragma endregion

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

SharedPtr< Camera > CreateOrthographicCamera(const string &name, float near_z, float far_z) {
const float width = static_cast< float >(g_engine->GetRenderer().GetWidth());
const float height = static_cast< float >(g_engine->GetRenderer().GetHeight());
return SharedPtr< Camera >(new OrthographicCamera(name, width, height, near_z, far_z));
}
}
6 changes: 6 additions & 0 deletions MAGE/MAGE/src/camera/orthographic_camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace mage {
*/
class OrthographicCamera : public Camera {

public:

//---------------------------------------------------------------------
// Constructors and Destructors
//---------------------------------------------------------------------
Expand Down Expand Up @@ -123,4 +125,8 @@ namespace mage {
SetNearAndFarZ(near_z, far_z);
}
};

SharedPtr< Camera > CreateOrthographicCamera(const string &name,
float near_z = MAGE_DEFAULT_CAMERA_NEAR_Z,
float far_z = MAGE_DEFAULT_CAMERA_FAR_Z);
}
21 changes: 21 additions & 0 deletions MAGE/MAGE/src/camera/perspective_camera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//-----------------------------------------------------------------------------
// Engine Includes
//-----------------------------------------------------------------------------
#pragma region

#include "engine.hpp"
#include "camera\perspective_camera.hpp"

#pragma endregion

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

SharedPtr< Camera > CreatePerspectiveCamera(const string &name, float fov_y, float near_z, float far_z) {
const float width = static_cast< float >(g_engine->GetRenderer().GetWidth());
const float height = static_cast< float >(g_engine->GetRenderer().GetHeight());
return SharedPtr< Camera >(new PerspectiveCamera(name, width, height, fov_y, near_z, far_z));
}
}
5 changes: 5 additions & 0 deletions MAGE/MAGE/src/camera/perspective_camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,9 @@ namespace mage {
*/
float m_fov_y;
};

SharedPtr< Camera > CreatePerspectiveCamera(const string &name,
float fov_y = MAGE_DEFAULT_CAMERA_FOV_Y,
float near_z = MAGE_DEFAULT_CAMERA_NEAR_Z,
float far_z = MAGE_DEFAULT_CAMERA_FAR_Z);
}
8 changes: 7 additions & 1 deletion MAGE/MAGE/src/font/sprite_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//-----------------------------------------------------------------------------
#pragma region

#include "font\sprite_font.hpp"
#include "resource\resource_factory.hpp"
#include "font\sprite_font_loader.hpp"
#include "logging\error.hpp"

Expand Down Expand Up @@ -277,4 +277,10 @@ namespace mage {

return m_default_glyph;
}

SharedPtr< SpriteFont > CreateFont(const wstring &fname, const SpriteFontDescriptor &desc) {
ID3D11Device2 *device = GetRenderingDevice();
ResourceFactory &factory = GetResourceFactory();
return factory.CreateFont(device, fname, desc);
}
}
5 changes: 4 additions & 1 deletion MAGE/MAGE/src/font/sprite_font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ namespace mage {
// Constructors and Destructors
//---------------------------------------------------------------------

explicit SpriteFont(ID3D11Device2 *device, const wstring &fname, const SpriteFontDescriptor &desc);
explicit SpriteFont(ID3D11Device2 *device, const wstring &fname,
const SpriteFontDescriptor &desc = SpriteFontDescriptor());
SpriteFont(const SpriteFont &font) = delete;
SpriteFont(SpriteFont &&font) = default;
virtual ~SpriteFont() = default;
Expand Down Expand Up @@ -79,4 +80,6 @@ namespace mage {
const Glyph *m_default_glyph;
float m_line_spacing;
};

SharedPtr< SpriteFont > CreateFont(const wstring &fname, const SpriteFontDescriptor &desc);
}
4 changes: 3 additions & 1 deletion MAGE/MAGE/src/model/meshmodel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "model\model.hpp"
#include "model\model_descriptor.hpp"
#include "shader\lambertian_shader.hpp"

#pragma endregion

Expand All @@ -26,7 +27,8 @@ namespace mage {
// Constructors and Destructors
//---------------------------------------------------------------------

explicit MeshModel(const string &name, const ModelDescriptor &desc, const CombinedShader &shader);
explicit MeshModel(const string &name, const ModelDescriptor &desc,
const CombinedShader &shader = CreateLambertianShader());
MeshModel(const MeshModel &model) = default;
MeshModel(MeshModel &&model) = default;
virtual ~MeshModel() = default;
Expand Down
7 changes: 7 additions & 0 deletions MAGE/MAGE/src/resource/resource_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ namespace mage {
: m_model_descriptor_resource_pool(new ResourcePool< wstring, ModelDescriptor >()),
m_vertex_shader_resource_pool(new ResourcePool< wstring, VertexShader >()),
m_pixel_shader_resource_pool(new ResourcePool< wstring, PixelShader >()),
m_font_resource_pool(new ResourcePool< wstring, SpriteFont >()),
m_texture_resource_pool(new ResourcePool< wstring, Texture >()),
m_variable_script_resource_pool(new ResourcePool< wstring, VariableScript >()) {}

ResourceFactory::~ResourceFactory() {
m_model_descriptor_resource_pool->RemoveAllResources();
m_vertex_shader_resource_pool->RemoveAllResources();
m_pixel_shader_resource_pool->RemoveAllResources();
m_font_resource_pool->RemoveAllResources();
m_texture_resource_pool->RemoveAllResources();
m_variable_script_resource_pool->RemoveAllResources();
}
Expand All @@ -53,6 +55,11 @@ namespace mage {
return m_pixel_shader_resource_pool->template
GetDerivedResource< SpritePixelShader, ID3D11Device2 *&, ID3D11DeviceContext2 *& >(MAGE_GUID_SPRITE_PS, device, device_context);
}
SharedPtr< SpriteFont > ResourceFactory::CreateFont(
ID3D11Device2 *device, const wstring &fname, const SpriteFontDescriptor &desc) {
return m_font_resource_pool->template
GetResource< ID3D11Device2 *&, const wstring &, const SpriteFontDescriptor & >(fname, device, fname, desc);
}
SharedPtr< Texture > ResourceFactory::CreateTexture(
ID3D11Device2 *device, const wstring &fname) {
return m_texture_resource_pool->template
Expand Down
4 changes: 4 additions & 0 deletions MAGE/MAGE/src/resource/resource_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "resource\resource_pool.hpp"
#include "model\model_descriptor.hpp"
#include "shader\shader.hpp"
#include "font\sprite_font.hpp"
#include "scripting\variable_script.hpp"

#pragma endregion
Expand Down Expand Up @@ -58,6 +59,8 @@ namespace mage {
ID3D11Device2 *device, ID3D11DeviceContext2 *device_context);
SharedPtr< PixelShader > CreateSpritePixelShader(
ID3D11Device2 *device, ID3D11DeviceContext2 *device_context);
SharedPtr< SpriteFont > CreateFont(ID3D11Device2 *device, const wstring &fname,
const SpriteFontDescriptor &desc);
SharedPtr< Texture > CreateTexture(ID3D11Device2 *device, const wstring &fname);
SharedPtr< VariableScript > CreateVariableScript(const wstring &fname, bool import);

Expand All @@ -70,6 +73,7 @@ namespace mage {
UniquePtr< ResourcePool< wstring, ModelDescriptor > > m_model_descriptor_resource_pool;
UniquePtr< ResourcePool< wstring, VertexShader > > m_vertex_shader_resource_pool;
UniquePtr< ResourcePool< wstring, PixelShader > > m_pixel_shader_resource_pool;
UniquePtr< ResourcePool< wstring, SpriteFont > > m_font_resource_pool;
UniquePtr< ResourcePool< wstring, Texture > > m_texture_resource_pool;
UniquePtr< ResourcePool< wstring, VariableScript > > m_variable_script_resource_pool;
};
Expand Down
34 changes: 5 additions & 29 deletions MAGE/MAGE/src/scene/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
#pragma region

#include "scene\scene.hpp"
#include "model\model.hpp"
#include "model\meshmodel.hpp"
#include "light\point_light.hpp"
#include "text\sprite_text.hpp"
#include "sprite\sprite_image.hpp"
#include "scripting\behavior_script.hpp"
#include "logging\error.hpp"

Expand All @@ -15,18 +18,6 @@
//-----------------------------------------------------------------------------
namespace mage {

SharedPtr< BehaviorScript > Scene::GetScript(const string &name) const {
vector< SharedPtr< BehaviorScript > >::const_iterator it = m_scripts.cbegin();
while (it != m_scripts.cend()) {
if ((*it)->GetName() == name) {
return (*it);
}
else {
++it;
}
}
return nullptr;
}
bool Scene::HasScript(const SharedPtr< BehaviorScript > script) const {
vector< SharedPtr< BehaviorScript > >::const_iterator it = m_scripts.cbegin();
while (it != m_scripts.cend()) {
Expand All @@ -50,21 +41,6 @@ namespace mage {
script->Load();
}
}
void Scene::RemoveScript(const string &name, bool close) {
vector< SharedPtr< BehaviorScript > >::iterator it = m_scripts.begin();
while (it != m_scripts.end()) {
if ((*it)->GetName() == name) {
if (close) {
(*it)->Close();
}
it = m_scripts.erase(it);
break;
}
else {
++it;
}
}
}
void Scene::RemoveScript(SharedPtr< BehaviorScript > script, bool close) {
vector< SharedPtr< BehaviorScript > >::iterator it = m_scripts.begin();
while (it != m_scripts.end()) {
Expand Down Expand Up @@ -108,7 +84,7 @@ namespace mage {

// Update and propagate transforms.
m_camera->UpdateTransform();
m_world->ForEachWorldObject([](WorldObject &world_object) {
m_world->ForEachObject3D([](WorldObject &world_object) {
world_object.UpdateTransform();
});
}
Expand Down
5 changes: 0 additions & 5 deletions MAGE/MAGE/src/scene/scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,8 @@ namespace mage {
size_t GetNumberOfScripts() const {
return m_scripts.size();
}
SharedPtr< BehaviorScript > GetScript(const string &name) const;
bool HasScript(const string &name) const {
return GetScript(name) != nullptr;
}
bool HasScript(const SharedPtr< BehaviorScript > script) const;
void AddScript(SharedPtr< BehaviorScript > script, bool load = false);
void RemoveScript(const string &name, bool close = false);
void RemoveScript(SharedPtr< BehaviorScript > script, bool close = false);
void RemoveAllScripts(bool close = false);
template< typename ActionT >
Expand Down
1 change: 0 additions & 1 deletion MAGE/MAGE/src/sprite/sprite_batch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "math\sprite_transform.hpp"
#include "mesh\sprite_batch_mesh.hpp"
#include "mesh\vertex.hpp"
#include "shader\shader.hpp"
#include "shader\sprite_shader.hpp"

#include "sprite\sprite_utils.hpp"
Expand Down
Loading

0 comments on commit f729fba

Please sign in to comment.