Skip to content

Commit

Permalink
Fixed normal transformation and fixed window closing
Browse files Browse the repository at this point in the history
  • Loading branch information
matt77hias committed Apr 27, 2017
1 parent 0e1bc2c commit 129ebeb
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 67 deletions.
1 change: 0 additions & 1 deletion MAGE/FPS/src/core/FPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ class TestScene final : public Scene {
omni_light->SetTranslationY(2.0f);
omni_light->GetObject()->SetDistanceFalloff(0.0f, 2.0f);
SharedPtr< SpotLightNode > spot_light = GetWorld()->CreateSpotLightNode();
//spot_light->SetTranslation(0.0f, 2.0f, 0.0f);
spot_light->GetObject()->SetDistanceFalloff(0.0f, 3.0f);
camera->AddChildTransformNode(spot_light);

Expand Down
1 change: 0 additions & 1 deletion MAGE/MAGE.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="MAGE\src\binary\binary_writer.cpp" />
<ClCompile Include="MAGE\src\buffer\transform_buffer.cpp" />
<ClCompile Include="MAGE\src\camera\perspective_camera.cpp" />
<ClCompile Include="MAGE\src\core\engine.cpp" />
<ClCompile Include="MAGE\src\core\version.cpp" />
Expand Down
6 changes: 0 additions & 6 deletions MAGE/MAGE.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,6 @@
<Filter Include="Header Files\buffer">
<UniqueIdentifier>{6611e24b-5030-4508-99c0-733263f8d080}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\buffer">
<UniqueIdentifier>{d58afbdd-86c4-4381-a6d1-4e4284f2e246}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="MAGE\src\collection\collection.hpp">
Expand Down Expand Up @@ -926,9 +923,6 @@
<ClCompile Include="MAGE\src\math\transform_node.cpp">
<Filter>Source Files\math</Filter>
</ClCompile>
<ClCompile Include="MAGE\src\buffer\transform_buffer.cpp">
<Filter>Source Files\buffer</Filter>
</ClCompile>
<ClCompile Include="MAGE\src\logging\exception.cpp">
<Filter>Source Files\logging</Filter>
</ClCompile>
Expand Down
12 changes: 6 additions & 6 deletions MAGE/MAGE/shaders/lambertian.fx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// Transformations
//-----------------------------------------------------------------------------
cbuffer Transform : register(b0) {
matrix model_to_world; // The model-to-world transformation matrix.
matrix world_to_view; // The world-to-view transformation matrix.
matrix world_to_view_inverse_transpose; // The world-to-view inverse transpose transformation matrix.
matrix view_to_projection; // The view-to-projection transformation matrix.
matrix object_to_world; // The object-to-world transformation matrix.
matrix world_to_view; // The world-to-view transformation matrix.
matrix object_to_view_inverse_transpose; // The object-to-view inverse transpose transformation matrix.
matrix view_to_projection; // The view-to-projection transformation matrix.
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -301,11 +301,11 @@ struct PS_INPUT {

PS_INPUT VS(VS_INPUT input) {
PS_INPUT output = (PS_INPUT)0;
output.p_view = mul(input.p, model_to_world);
output.p_view = mul(input.p, object_to_world);
output.p_view = mul(output.p_view, world_to_view);
output.p = mul(output.p_view, view_to_projection);
output.tex = input.tex;
output.n_view = mul(input.n, (float3x3)world_to_view_inverse_transpose);
output.n_view = normalize(mul(input.n, (float3x3)object_to_view_inverse_transpose));
return output;
}

Expand Down
26 changes: 0 additions & 26 deletions MAGE/MAGE/src/buffer/transform_buffer.cpp

This file was deleted.

25 changes: 14 additions & 11 deletions MAGE/MAGE/src/buffer/transform_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ namespace mage {
// Constructors and Destructors
//---------------------------------------------------------------------

explicit TransformBuffer(const XMMATRIX &world_to_view,
const XMMATRIX &view_to_projection);
explicit TransformBuffer(const XMMATRIX &world_to_view,
const XMMATRIX &view_to_projection) :
m_world_to_view(XMMatrixTranspose(world_to_view)),
m_view_to_projection(XMMatrixTranspose(view_to_projection)) {}
TransformBuffer(const TransformBuffer &buffer) = default;
TransformBuffer(TransformBuffer &&buffer) = default;
~TransformBuffer() = default;
Expand All @@ -43,14 +45,15 @@ namespace mage {
const XMMATRIX GetWorldToViewMatrix() const {
return XMMatrixTranspose(m_world_to_view);
}
const XMMATRIX GetWorldToViewInverseTransposeMatrix() const {
return XMMatrixTranspose(m_world_to_view_inverse_transpose);
}
const XMMATRIX GetViewToProjectionMatrix() const {
return XMMatrixTranspose(m_view_to_projection);
}
void SetModelToWorld(const XMMATRIX &model_to_world) const {
m_model_to_world = XMMatrixTranspose(model_to_world);
void SetObjectMatrices(const XMMATRIX &object_to_world,
const XMMATRIX &view_to_object) const {
m_object_to_world
= XMMatrixTranspose(object_to_world);
m_object_to_view_inverse_transpose
= view_to_object;
}

private:
Expand All @@ -59,10 +62,10 @@ namespace mage {
// Member Variables
//---------------------------------------------------------------------

mutable XMMATRIX m_model_to_world;
XMMATRIX m_world_to_view;
XMMATRIX m_world_to_view_inverse_transpose;
XMMATRIX m_view_to_projection;
mutable XMMATRIX m_object_to_world;
XMMATRIX m_world_to_view;
mutable XMMATRIX m_object_to_view_inverse_transpose;
XMMATRIX m_view_to_projection;
};

static_assert(sizeof(TransformBuffer) == 256, "CPU/GPU struct mismatch");
Expand Down
24 changes: 18 additions & 6 deletions MAGE/MAGE/src/core/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "core\engine.hpp"
#include "core\version.hpp"
#include "logging\error.hpp"
#include "logging\exception.hpp"
#include "logging\logging.hpp"
#include "rendering\device_enumeration.hpp"
Expand All @@ -27,7 +28,7 @@ namespace mage {
//-------------------------------------------------------------------------

Engine::Engine(const EngineSetup &setup)
: m_main_window(), m_deactive(false),
: Loadable(), m_main_window(), m_deactive(false),
m_renderer(), m_mode_switch(false),
m_input_manager(), m_resource_factory(),
m_scene(), m_timer(new Timer()) {
Expand All @@ -40,12 +41,23 @@ namespace mage {
InitializeConsole();
PrintConsoleHeader();

// Enumerate the devices.
SAFE_DELETE(g_device_enumeration);
g_device_enumeration = new DeviceEnumeration();
const HRESULT result_enumerate = g_device_enumeration->Enumerate();
if (FAILED(result_enumerate)) {
Error("Device enumeration setup failed: %ld", result_enumerate);
return;
}

// Initialize the different engine systems.
InitializeSystems(setup);

// Initializes the COM library for use by the calling thread
// and sets the thread's concurrency model to multithreaded concurrency.
CoInitializeEx(nullptr, COINIT_MULTITHREADED);

SetLoaded();
}

Engine::~Engine() {
Expand All @@ -57,11 +69,6 @@ namespace mage {
}

void Engine::InitializeSystems(const EngineSetup &setup) {
// Enumerate the devices.
SAFE_DELETE(g_device_enumeration);
g_device_enumeration = new DeviceEnumeration();
g_device_enumeration->Enumerate();

const LONG width = static_cast< LONG >(g_device_enumeration->GetDisplayMode()->Width);
const LONG height = static_cast< LONG >(g_device_enumeration->GetDisplayMode()->Height);

Expand Down Expand Up @@ -106,6 +113,11 @@ namespace mage {
}

void Engine::Run(int nCmdShow) {
if (!IsLoaded()) {
Error("Game loop can not start because the engine is not loaded.");
return;
}

m_main_window->Show(nCmdShow);

// Handle startup in fullscreen mode.
Expand Down
5 changes: 3 additions & 2 deletions MAGE/MAGE/src/core/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "core\targetver.hpp"

#include "core\loadable.hpp"
#include "rendering\renderer.hpp"
#include "input\input_manager.hpp"
#include "ui\main_window.hpp"
Expand All @@ -25,7 +26,7 @@ namespace mage {
/**
A class of engines.
*/
class Engine final {
class Engine final : public Loadable {

public:

Expand Down Expand Up @@ -62,7 +63,7 @@ namespace mage {
/**
Destructs this engine.
*/
~Engine();
virtual ~Engine();

//---------------------------------------------------------------------
// Assignment Operators
Expand Down
2 changes: 1 addition & 1 deletion MAGE/MAGE/src/parallel/lock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ namespace mage {
}

void ReadWriteMutexLock::UpgradeToWrite() {
Assert(m_type == READ);
Assert(m_type == ReadWriteMutexLockType_Read);
m_mutex.ReleaseRead();
m_mutex.AcquireWrite();
m_type = ReadWriteMutexLockType_Write;
Expand Down
8 changes: 8 additions & 0 deletions MAGE/MAGE/src/string/string_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,12 @@ namespace mage {
const char *str_convert(const wchar_t *str) {
return CW2A(str);
}

const wstring str_convert(const string &str) {
return wstring(CA2W(str.c_str()));
}

const string str_convert(const wstring &str) {
return string(CW2A(str.c_str()));
}
}
8 changes: 2 additions & 6 deletions MAGE/MAGE/src/string/string_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,7 @@ namespace mage {
A reference to the byte string to copy.
@return The wide string copy of the given byte string.
*/
inline const wstring str_convert(const string &str) {
return str_convert(str.c_str());
}
const wstring str_convert(const string &str);

/**
Converts the given wide string to an byte string.
Expand All @@ -256,7 +254,5 @@ namespace mage {
A reference to the wide string to copy.
@return The byte string copy of the given wide string.
*/
inline const string str_convert(const wstring &str) {
return str_convert(str.c_str());
}
const string str_convert(const wstring &str);
}
8 changes: 7 additions & 1 deletion MAGE/MAGE/src/world/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace mage {
void World::Render3D() const {

const XMMATRIX world_to_view = m_camera->GetWorldToViewMatrix();
const XMMATRIX view_to_world = XMMatrixInverse(nullptr, world_to_view);

// Update omni light structured buffer.
vector< OmniLightBuffer > omni_lights_buffer;
Expand Down Expand Up @@ -100,7 +101,12 @@ namespace mage {

// Update transform constant buffer.
const XMMATRIX object_to_world = model_node.GetObjectToWorldMatrix();
transform_buffer.SetModelToWorld(object_to_world);
const XMMATRIX world_to_object = model_node.GetWorldToObjectMatrix();

XMFLOAT4X4 m;
XMStoreFloat4x4(&m, world_to_object);
const XMMATRIX view_to_object = view_to_world * world_to_object;
transform_buffer.SetObjectMatrices(object_to_world, view_to_object);
m_transform_buffer.UpdateData(transform_buffer);

// Draw model.
Expand Down

0 comments on commit 129ebeb

Please sign in to comment.