diff --git a/Graphics2.sln b/Graphics2.sln new file mode 100644 index 0000000..6d0a2cc --- /dev/null +++ b/Graphics2.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27004.2009 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Graphics2", "Graphics2\Graphics2.vcxproj", "{E4F72E0B-F014-43F1-BC2A-EA26230158F9}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E4F72E0B-F014-43F1-BC2A-EA26230158F9}.Debug|x64.ActiveCfg = Debug|x64 + {E4F72E0B-F014-43F1-BC2A-EA26230158F9}.Debug|x64.Build.0 = Debug|x64 + {E4F72E0B-F014-43F1-BC2A-EA26230158F9}.Debug|x86.ActiveCfg = Debug|Win32 + {E4F72E0B-F014-43F1-BC2A-EA26230158F9}.Debug|x86.Build.0 = Debug|Win32 + {E4F72E0B-F014-43F1-BC2A-EA26230158F9}.Release|x64.ActiveCfg = Release|x64 + {E4F72E0B-F014-43F1-BC2A-EA26230158F9}.Release|x64.Build.0 = Release|x64 + {E4F72E0B-F014-43F1-BC2A-EA26230158F9}.Release|x86.ActiveCfg = Release|Win32 + {E4F72E0B-F014-43F1-BC2A-EA26230158F9}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {EF90D084-277F-4434-9B4A-DA8B2C2E8FD6} + EndGlobalSection +EndGlobal diff --git a/Graphics2/Core.h b/Graphics2/Core.h new file mode 100644 index 0000000..96fd36d --- /dev/null +++ b/Graphics2/Core.h @@ -0,0 +1,7 @@ +#pragma once +#include +#include "Resource.h" +#include +#include +#include +#include "HelperFunctions.h" diff --git a/Graphics2/DirectXCore.h b/Graphics2/DirectXCore.h new file mode 100644 index 0000000..43cf3da --- /dev/null +++ b/Graphics2/DirectXCore.h @@ -0,0 +1,11 @@ +#pragma once +#include +#include +#include +#include +#include + +using namespace DirectX; + +using Microsoft::WRL::ComPtr; + diff --git a/Graphics2/DirectXFramework.cpp b/Graphics2/DirectXFramework.cpp new file mode 100644 index 0000000..c6a753e --- /dev/null +++ b/Graphics2/DirectXFramework.cpp @@ -0,0 +1,230 @@ +#include "DirectXFramework.h" + +// DirectX libraries that are needed +#pragma comment(lib, "d3d11.lib") +#pragma comment(lib, "d3dcompiler.lib") + +DirectXFramework * _dxFramework = nullptr; + +DirectXFramework::DirectXFramework() : DirectXFramework(800, 600) +{ +} + +DirectXFramework::DirectXFramework(unsigned int width, unsigned int height) : Framework(width, height) +{ + _dxFramework = this; + + // Set default background colour + _backgroundColour[0] = 0.0f; + _backgroundColour[1] = 0.0f; + _backgroundColour[2] = 0.0f; + _backgroundColour[3] = 0.0f; + + // Initialise vectors used to create camera. We will move these + // to a separate Camera class later + _eyePosition = XMFLOAT4(0.0f, 1.0f, -15.0f, 0.0f); + _focalPointPosition = XMFLOAT4(0.0f, 0.0f, 0.0f, 0.0f); + _upVector = XMFLOAT4(0.0f, 1.0f, 0.0f, 0.0f); +} + +DirectXFramework * DirectXFramework::GetDXFramework() +{ + return _dxFramework; +} + +XMMATRIX DirectXFramework::GetViewTransformation() +{ + return XMLoadFloat4x4(&_viewTransformation); +} + +XMMATRIX DirectXFramework::GetProjectionTransformation() +{ + return XMLoadFloat4x4(&_projectionTransformation); +} + +void DirectXFramework::SetBackgroundColour(XMFLOAT4 backgroundColour) +{ + _backgroundColour[0] = backgroundColour.x; + _backgroundColour[1] = backgroundColour.y; + _backgroundColour[2] = backgroundColour.z; + _backgroundColour[3] = backgroundColour.w; +} + +void DirectXFramework::CreateSceneGraph() +{ +} + +void DirectXFramework::UpdateSceneGraph() +{ +} + +bool DirectXFramework::Initialise() +{ + // The call to CoInitializeEx is needed if we are using + // textures since the WIC library used requires it, so we + // take care of initialising it here + if FAILED(CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED)) + { + return false; + } + if (!GetDeviceAndSwapChain()) + { + return false; + } + OnResize(SIZE_RESTORED); + + // Create camera and projection matrices (we will look at how the + // camera matrix is created from vectors later) + XMStoreFloat4x4(&_projectionTransformation, XMMatrixPerspectiveFovLH(XM_PIDIV4, (float)GetWindowWidth() / GetWindowHeight(), 1.0f, 10000.0f)); + _sceneGraph = make_shared(); + CreateSceneGraph(); + return _sceneGraph->Initialise(); +} + +void DirectXFramework::Shutdown() +{ + // Required because we called CoInitialize above + _sceneGraph->Shutdown(); + CoUninitialize(); +} + +void DirectXFramework::Update() +{ + // Do any updates to the scene graph nodes + UpdateSceneGraph(); + // Now apply any updates that have been made to world transformations + // to all the nodes + _sceneGraph->Update(XMMatrixIdentity()); +} + +void DirectXFramework::Render() +{ + // Clear the render target and the depth stencil view + _deviceContext->ClearRenderTargetView(_renderTargetView.Get(), _backgroundColour); + _deviceContext->ClearDepthStencilView(_depthStencilView.Get(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0); + // Now recurse through the scene graph, rendering each object + _sceneGraph->Render(); + // Now display the scene + ThrowIfFailed(_swapChain->Present(0, 0)); +} + +void DirectXFramework::OnResize(WPARAM wParam) +{ + // Update view and projection matrices to allow for the window size change + XMStoreFloat4x4(&_viewTransformation, XMMatrixLookAtLH(XMLoadFloat4(&_eyePosition), XMLoadFloat4(&_focalPointPosition), XMLoadFloat4(&_upVector))); + XMStoreFloat4x4(&_projectionTransformation, XMMatrixPerspectiveFovLH(XM_PIDIV4, (float)GetWindowWidth() / GetWindowHeight(), 1.0f, 10000.0f)); + + // This will free any existing render and depth views (which + // would be the case if the window was being resized) + _renderTargetView = nullptr; + _depthStencilView = nullptr; + _depthStencilBuffer = nullptr; + + ThrowIfFailed(_swapChain->ResizeBuffers(1, GetWindowWidth(), GetWindowHeight(), DXGI_FORMAT_R8G8B8A8_UNORM, 0)); + + // Create a drawing surface for DirectX to render to + ComPtr backBuffer; + ThrowIfFailed(_swapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer))); + ThrowIfFailed(_device->CreateRenderTargetView(backBuffer.Get(), NULL, _renderTargetView.GetAddressOf())); + + // The depth buffer is used by DirectX to ensure + // that pixels of closer objects are drawn over pixels of more + // distant objects. + + // First, we need to create a texture (bitmap) for the depth buffer + D3D11_TEXTURE2D_DESC depthBufferTexture = { 0 }; + depthBufferTexture.Width = GetWindowWidth(); + depthBufferTexture.Height = GetWindowHeight(); + depthBufferTexture.ArraySize = 1; + depthBufferTexture.MipLevels = 1; + depthBufferTexture.SampleDesc.Count = 4; + depthBufferTexture.Format = DXGI_FORMAT_D32_FLOAT; + depthBufferTexture.Usage = D3D11_USAGE_DEFAULT; + depthBufferTexture.BindFlags = D3D11_BIND_DEPTH_STENCIL; + + // Create the depth buffer. + ComPtr depthBuffer; + ThrowIfFailed(_device->CreateTexture2D(&depthBufferTexture, NULL, depthBuffer.GetAddressOf())); + ThrowIfFailed(_device->CreateDepthStencilView(depthBuffer.Get(), 0, _depthStencilView.GetAddressOf())); + + // Bind the render target view buffer and the depth stencil view buffer to the output-merger stage + // of the pipeline. + _deviceContext->OMSetRenderTargets(1, _renderTargetView.GetAddressOf(), _depthStencilView.Get()); + + // Specify a viewport of the required size + D3D11_VIEWPORT viewPort; + viewPort.Width = static_cast(GetWindowWidth()); + viewPort.Height = static_cast(GetWindowHeight()); + viewPort.MinDepth = 0.0f; + viewPort.MaxDepth = 1.0f; + viewPort.TopLeftX = 0; + viewPort.TopLeftY = 0; + _deviceContext->RSSetViewports(1, &viewPort); +} + +bool DirectXFramework::GetDeviceAndSwapChain() +{ + UINT createDeviceFlags = 0; + + // We are going to only accept a hardware driver or a WARP + // driver + D3D_DRIVER_TYPE driverTypes[] = + { + D3D_DRIVER_TYPE_HARDWARE, + D3D_DRIVER_TYPE_WARP + }; + unsigned int totalDriverTypes = ARRAYSIZE(driverTypes); + + D3D_FEATURE_LEVEL featureLevels[] = + { + D3D_FEATURE_LEVEL_11_0 + }; + unsigned int totalFeatureLevels = ARRAYSIZE(featureLevels); + + DXGI_SWAP_CHAIN_DESC swapChainDesc = { 0 }; + swapChainDesc.BufferCount = 1; + swapChainDesc.BufferDesc.Width = GetWindowWidth(); + swapChainDesc.BufferDesc.Height = GetWindowHeight(); + swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + // Set the refresh rate to 0 and let DXGI determine the best option (refer to DXGI best practices) + swapChainDesc.BufferDesc.RefreshRate.Numerator = 0; + swapChainDesc.BufferDesc.RefreshRate.Denominator = 0; + swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; + swapChainDesc.OutputWindow = GetHWnd(); + // Start out windowed + swapChainDesc.Windowed = true; + // Enable multi-sampling to give smoother lines (set to 1 if performance becomes an issue) + swapChainDesc.SampleDesc.Count = 4; + swapChainDesc.SampleDesc.Quality = 0; + + // Loop through the driver types to determine which one is available to us + D3D_DRIVER_TYPE driverType = D3D_DRIVER_TYPE_UNKNOWN; + + for (unsigned int driver = 0; driver < totalDriverTypes && driverType == D3D_DRIVER_TYPE_UNKNOWN; driver++) + { + if (SUCCEEDED(D3D11CreateDeviceAndSwapChain(0, + driverTypes[driver], + 0, + createDeviceFlags, + featureLevels, + totalFeatureLevels, + D3D11_SDK_VERSION, + &swapChainDesc, + _swapChain.GetAddressOf(), + _device.GetAddressOf(), + 0, + _deviceContext.GetAddressOf() + ))) + + { + driverType = driverTypes[driver]; + } + } + if (driverType == D3D_DRIVER_TYPE_UNKNOWN) + { + // Unable to find a suitable device driver + return false; + } + return true; +} + diff --git a/Graphics2/DirectXFramework.h b/Graphics2/DirectXFramework.h new file mode 100644 index 0000000..86512f4 --- /dev/null +++ b/Graphics2/DirectXFramework.h @@ -0,0 +1,63 @@ +#pragma once +#include +#include "Framework.h" +#include "DirectXCore.h" +#include "SceneGraph.h" + +class DirectXFramework : public Framework +{ +public: + DirectXFramework(); + DirectXFramework(unsigned int width, unsigned int height); + + virtual void CreateSceneGraph(); + virtual void UpdateSceneGraph(); + + bool Initialise(); + void Update(); + void Render(); + void OnResize(WPARAM wParam); + void Shutdown(); + + static DirectXFramework * GetDXFramework(); + + inline SceneGraphPointer GetSceneGraph() { return _sceneGraph; } + inline ComPtr GetDevice() { return _device; } + inline ComPtr GetDeviceContext() { return _deviceContext; } + + XMMATRIX GetViewTransformation(); + XMMATRIX GetProjectionTransformation(); + + void SetBackgroundColour(XMFLOAT4 backgroundColour); + +private: + ComPtr _device; + ComPtr _deviceContext; + ComPtr _swapChain; + ComPtr _depthStencilBuffer; + ComPtr _renderTargetView; + ComPtr _depthStencilView; + + D3D11_VIEWPORT _screenViewport; + + // Our vectors and matrices. Note that we cannot use + // XMVECTOR and XMMATRIX for class variables since they need + // to be aligned on 16-byte boundaries and the compiler cannot + // guarantee this for class variables + + // For now, we are storing our camera vectors and matrix here. + // We will move it to a separate Camera class later + XMFLOAT4 _eyePosition; + XMFLOAT4 _focalPointPosition; + XMFLOAT4 _upVector; + + XMFLOAT4X4 _viewTransformation; + XMFLOAT4X4 _projectionTransformation; + + SceneGraphPointer _sceneGraph; + + float _backgroundColour[4]; + + bool GetDeviceAndSwapChain(); +}; + diff --git a/Graphics2/Framework.cpp b/Graphics2/Framework.cpp new file mode 100644 index 0000000..64edcec --- /dev/null +++ b/Graphics2/Framework.cpp @@ -0,0 +1,233 @@ +#include "Framework.h" + +#define DEFAULT_FRAMERATE 60 +#define DEFAULT_WIDTH 800 +#define DEFAULT_HEIGHT 600 + +// Reference to ourselves - primarily used to access the message handler correctly +// This is initialised in the constructor +Framework * _thisFramework = NULL; + +// Forward declaration of our window procedure +LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); + +int APIENTRY wWinMain(_In_ HINSTANCE hInstance, + _In_opt_ HINSTANCE hPrevInstance, + _In_ LPWSTR lpCmdLine, + _In_ int nCmdShow) +{ + UNREFERENCED_PARAMETER(hPrevInstance); + UNREFERENCED_PARAMETER(lpCmdLine); + + // We can only run if an instance of a class that inherits from Framework + // has been created + if (_thisFramework) + { + return _thisFramework->Run(hInstance, nCmdShow); + } + return -1; +} + +Framework::Framework() : Framework(DEFAULT_WIDTH, DEFAULT_HEIGHT) +{ +} + +Framework::Framework(unsigned int width, unsigned int height) +{ + _thisFramework = this; + _width = width; + _height = height; +} + +Framework::~Framework() +{ +} + +int Framework::Run(HINSTANCE hInstance, int nCmdShow) +{ + int returnValue; + + _hInstance = hInstance; + if (!InitialiseMainWindow(nCmdShow)) + { + return -1; + } + returnValue = MainLoop(); + Shutdown(); + return returnValue; +} + +// Main program loop. + +int Framework::MainLoop() +{ + MSG msg; + HACCEL hAccelTable = LoadAccelerators(_hInstance, MAKEINTRESOURCE(IDC_GRAPHICS2)); + LARGE_INTEGER counterFrequency; + LARGE_INTEGER nextTime; + LARGE_INTEGER currentTime; + LARGE_INTEGER lastTime; + bool updateFlag = true; + + // Initialise timer + QueryPerformanceFrequency(&counterFrequency); + DWORD msPerFrame = (DWORD)(counterFrequency.QuadPart / DEFAULT_FRAMERATE); + double timeFactor = 1.0 / counterFrequency.QuadPart; + QueryPerformanceCounter(&nextTime); + lastTime = nextTime; + + // Main message loop: + msg.message = WM_NULL; + while (msg.message != WM_QUIT) + { + if (updateFlag) + { + QueryPerformanceCounter(¤tTime); + _timeSpan = (currentTime.QuadPart - lastTime.QuadPart) * timeFactor; + lastTime = currentTime; + Update(); + updateFlag = false; + } + QueryPerformanceCounter(¤tTime); + // Is it time to render the frame? + if (currentTime.QuadPart > nextTime.QuadPart) + { + Render(); + // Set time for next frame + nextTime.QuadPart += msPerFrame; + // If we get more than a frame ahead, allow one to be dropped + // Otherwise, we will never catch up if we let the error accumulate + // and message handling will suffer + if (nextTime.QuadPart < currentTime.QuadPart) + { + nextTime.QuadPart = currentTime.QuadPart + msPerFrame; + } + updateFlag = true; + } + else + { + if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) + { + if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + } + } + } + return (int)msg.wParam; +} + +// Register the window class, create the window and +// create the bitmap that we will use for rendering + +bool Framework::InitialiseMainWindow(int nCmdShow) +{ + #define MAX_LOADSTRING 100 + + WCHAR windowTitle[MAX_LOADSTRING]; + WCHAR windowClass[MAX_LOADSTRING]; + + LoadStringW(_hInstance, IDS_APP_TITLE, windowTitle, MAX_LOADSTRING); + LoadStringW(_hInstance, IDC_GRAPHICS2, windowClass, MAX_LOADSTRING); + + WNDCLASSEXW wcex; + wcex.cbSize = sizeof(WNDCLASSEX); + wcex.style = CS_HREDRAW | CS_VREDRAW; + wcex.lpfnWndProc = WndProc; + wcex.cbClsExtra = 0; + wcex.cbWndExtra = 0; + wcex.hInstance = _hInstance; + wcex.hIcon = LoadIcon(_hInstance, MAKEINTRESOURCE(IDI_GRAPHICS2)); + wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); + wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); + wcex.lpszMenuName = nullptr; + wcex.lpszClassName = windowClass; + wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); + if (!RegisterClassExW(&wcex)) + { + MessageBox(0, L"Unable to register window class", 0, 0); + return false; + } + + // Now work out how large the window needs to be for our required client window size + RECT windowRect = { 0, 0, static_cast(_width), static_cast(_height) }; + AdjustWindowRect(&windowRect, WS_OVERLAPPEDWINDOW, FALSE); + _width = windowRect.right - windowRect.left; + _height = windowRect.bottom - windowRect.top; + + _hWnd = CreateWindowW(windowClass, + windowTitle, + WS_OVERLAPPEDWINDOW, + CW_USEDEFAULT, CW_USEDEFAULT, _width, _height, + nullptr, nullptr, _hInstance, nullptr); + if (!_hWnd) + { + MessageBox(0, L"Unable to create window", 0, 0); + return false; + } + if (!Initialise()) + { + return false; + } + ShowWindow(_hWnd, nCmdShow); + UpdateWindow(_hWnd); + return true; +} + +// The WndProc for the current window. This cannot be a method, but we can +// redirect all messages to a method. + +LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +{ + if (_thisFramework != NULL) + { + // If framework is started, then we can call our own message proc + return _thisFramework->MsgProc(hWnd, message, wParam, lParam); + } + else + { + // otherwise, we just pass control to the default message proc + return DefWindowProc(hWnd, message, wParam, lParam); + } +} + +// Our main WndProc + +LRESULT Framework::MsgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +{ + switch (message) + { + case WM_PAINT: + break; + + case WM_KEYDOWN: + OnKeyDown(wParam); + return 0; + + case WM_KEYUP: + OnKeyUp(wParam); + return 0; + + case WM_DESTROY: + PostQuitMessage(0); + break; + + case WM_MOVE: + Render(); + break; + + case WM_SIZE: + _width = LOWORD(lParam); + _height = HIWORD(lParam); + OnResize(wParam); + Render(); + break; + + default: + return DefWindowProc(hWnd, message, wParam, lParam); + } + return 0; +} + diff --git a/Graphics2/Framework.h b/Graphics2/Framework.h new file mode 100644 index 0000000..a044850 --- /dev/null +++ b/Graphics2/Framework.h @@ -0,0 +1,55 @@ +#pragma once +#include "Core.h" + +using namespace std; + +class Framework +{ +public: + Framework(); + Framework(unsigned int width, unsigned int height); + virtual ~Framework(); + + int Run(HINSTANCE hInstance, int nCmdShow); + + LRESULT MsgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); + + inline unsigned int GetWindowWidth() { return _width; } + inline unsigned int GetWindowHeight() { return _height; } + inline HWND GetHWnd() { return _hWnd; } + + // Initialise the application. Called after the window and bitmap has been + // created, but before the main loop starts + // + // Return false if the application cannot be initialised. + virtual bool Initialise() { return true; } + + // Perform any updates to the structures that will be used + // to render the window (i.e. transformation matrices, etc). + virtual void Update() {} + + // Render the contents of the window. + virtual void Render() {}; + + // Perform any application shutdown or cleanup that is needed + virtual void Shutdown() {} + + // Handlers for Windows messages. If you need more, add them + // here and call them from MsgProc. + virtual void OnKeyDown(WPARAM wParam) {} + virtual void OnKeyUp(WPARAM wParam) {} + virtual void OnResize(WPARAM wParam) {} + +private: + HINSTANCE _hInstance; + HWND _hWnd; + unsigned int _width; + unsigned int _height; + + // Used in timing loop + double _timeSpan; + + bool InitialiseMainWindow(int nCmdShow); + int MainLoop(); +}; + diff --git a/Graphics2/Graphics2.cpp b/Graphics2/Graphics2.cpp new file mode 100644 index 0000000..18cc4c3 --- /dev/null +++ b/Graphics2/Graphics2.cpp @@ -0,0 +1,18 @@ +#include "Graphics2.h" + +Graphics2 app; + +void Graphics2::CreateSceneGraph() +{ + SceneGraphPointer sceneGraph = GetSceneGraph(); + + // This is where you add nodes to the scene graph +} + +void Graphics2::UpdateSceneGraph() +{ + SceneGraphPointer sceneGraph = GetSceneGraph(); + + // This is where you make any changes to the local world transformations to nodes + // in the scene graph +} diff --git a/Graphics2/Graphics2.h b/Graphics2/Graphics2.h new file mode 100644 index 0000000..cb2d36f --- /dev/null +++ b/Graphics2/Graphics2.h @@ -0,0 +1,11 @@ +#pragma once +#include "DirectXFramework.h" + +class Graphics2 : public DirectXFramework +{ +public: + void CreateSceneGraph(); + void UpdateSceneGraph(); + +}; + diff --git a/Graphics2/Graphics2.ico b/Graphics2/Graphics2.ico new file mode 100644 index 0000000..b3ec03b Binary files /dev/null and b/Graphics2/Graphics2.ico differ diff --git a/Graphics2/Graphics2.rc b/Graphics2/Graphics2.rc new file mode 100644 index 0000000..10a81c9 Binary files /dev/null and b/Graphics2/Graphics2.rc differ diff --git a/Graphics2/Graphics2.vcxproj b/Graphics2/Graphics2.vcxproj new file mode 100644 index 0000000..6ed90e9 --- /dev/null +++ b/Graphics2/Graphics2.vcxproj @@ -0,0 +1,172 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {E4F72E0B-F014-43F1-BC2A-EA26230158F9} + Win32Proj + Graphics2 + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + NotUsing + Level3 + Disabled + true + WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) + + + Windows + true + + + + + NotUsing + Level3 + Disabled + true + _DEBUG;_WINDOWS;%(PreprocessorDefinitions) + + + Windows + true + + + + + Use + Level3 + MaxSpeed + true + true + true + WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + + + Windows + true + true + true + + + + + Use + Level3 + MaxSpeed + true + true + true + NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + + + Windows + true + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Graphics2/Graphics2.vcxproj.filters b/Graphics2/Graphics2.vcxproj.filters new file mode 100644 index 0000000..fb173ac --- /dev/null +++ b/Graphics2/Graphics2.vcxproj.filters @@ -0,0 +1,73 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + + + Resource Files + + + + + Resource Files + + + Resource Files + + + + + Source Files + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/Graphics2/HelperFunctions.h b/Graphics2/HelperFunctions.h new file mode 100644 index 0000000..9fc04c6 --- /dev/null +++ b/Graphics2/HelperFunctions.h @@ -0,0 +1,13 @@ +#pragma once + +// Various helper functions + +using namespace std; + +inline void ThrowIfFailed(HRESULT hr) +{ + if (FAILED(hr)) + { + throw exception(); + } +} diff --git a/Graphics2/SceneGraph.h b/Graphics2/SceneGraph.h new file mode 100644 index 0000000..9359151 --- /dev/null +++ b/Graphics2/SceneGraph.h @@ -0,0 +1,26 @@ +#pragma once +#include "SceneNode.h" + +class SceneGraph : public SceneNode +{ +public: + SceneGraph() : SceneNode(L"Root") {}; + SceneGraph(wstring name) : SceneNode(name) {}; + ~SceneGraph(void) {}; + + virtual bool Initialise(void); + virtual void Update(FXMMATRIX& currentWorldTransformation); + virtual void Render(void); + virtual void Shutdown(void); + + void Add(SceneNodePointer node); + void Remove(SceneNodePointer node); + SceneNodePointer Find(wstring name); + +//private: + + // Here you need to add whatever collection you are going to + // use to store the children of this scene graph +}; + +typedef shared_ptr SceneGraphPointer; diff --git a/Graphics2/SceneNode.h b/Graphics2/SceneNode.h new file mode 100644 index 0000000..fc0e8c8 --- /dev/null +++ b/Graphics2/SceneNode.h @@ -0,0 +1,39 @@ +#pragma once +#include "core.h" +#include "DirectXCore.h" + +using namespace std; + +// Abstract base class for all nodes of the scene graph. +// This scene graph implements the Composite Design Pattern + +class SceneNode; + +typedef shared_ptr SceneNodePointer; + +class SceneNode : public enable_shared_from_this +{ +public: + SceneNode(wstring name) {_name = name; XMStoreFloat4x4(&_worldTransformation, XMMatrixIdentity()); }; + ~SceneNode(void) {}; + + // Core methods + virtual bool Initialise() = 0; + virtual void Update(FXMMATRIX& currentWorldTransformation) { XMStoreFloat4x4(&_combinedWorldTransformation, XMLoadFloat4x4(&_worldTransformation) * currentWorldTransformation); } + virtual void Render() = 0; + virtual void Shutdown() = 0; + + void SetWorldTransform(FXMMATRIX& worldTransformation) { XMStoreFloat4x4(&_worldTransformation, worldTransformation); } + + // Although only required in the composite class, these are provided + // in order to simplify the code base. + virtual void Add(SceneNodePointer node) {} + virtual void Remove(SceneNodePointer node) {}; + virtual SceneNodePointer Find(wstring name) { return (_name == name) ? shared_from_this() : nullptr; } + +protected: + XMFLOAT4X4 _worldTransformation; + XMFLOAT4X4 _combinedWorldTransformation; + wstring _name; +}; + diff --git a/Graphics2/resource.h b/Graphics2/resource.h new file mode 100644 index 0000000..95c1ddb --- /dev/null +++ b/Graphics2/resource.h @@ -0,0 +1,27 @@ +//{{NO_DEPENDENCIES}} +// Microsoft Visual C++ generated include file. +// Used by WindowsApp.rc +// + +#define IDS_APP_TITLE 103 + +#define IDR_MAINFRAME 128 +#define IDI_GRAPHICS2 107 +#define IDI_SMALL 108 +#define IDC_GRAPHICS2 109 +#define IDC_MYICON 2 +#ifndef IDC_STATIC +#define IDC_STATIC -1 +#endif +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS + +#define _APS_NO_MFC 130 +#define _APS_NEXT_RESOURCE_VALUE 129 +#define _APS_NEXT_COMMAND_VALUE 32771 +#define _APS_NEXT_CONTROL_VALUE 1000 +#define _APS_NEXT_SYMED_VALUE 110 +#endif +#endif diff --git a/Graphics2/small.ico b/Graphics2/small.ico new file mode 100644 index 0000000..b3ec03b Binary files /dev/null and b/Graphics2/small.ico differ diff --git a/Graphics2/targetver.h b/Graphics2/targetver.h new file mode 100644 index 0000000..87c0086 --- /dev/null +++ b/Graphics2/targetver.h @@ -0,0 +1,8 @@ +#pragma once + +// Including SDKDDKVer.h defines the highest available Windows platform. + +// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and +// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. + +#include