src/video/windowsrt/CubeRenderer.cpp
changeset 8343 f593baeea84c
parent 8340 e470405f3ae9
parent 8342 05d7aa70928e
child 8344 11fd2e01a070
     1.1 --- a/src/video/windowsrt/CubeRenderer.cpp	Mon Nov 12 13:25:44 2012 -0500
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,256 +0,0 @@
     1.4 -#include "SDLmain_WinRT_common.h"
     1.5 -#include "CubeRenderer.h"
     1.6 -
     1.7 -using namespace DirectX;
     1.8 -using namespace Microsoft::WRL;
     1.9 -using namespace Windows::Foundation;
    1.10 -using namespace Windows::UI::Core;
    1.11 -
    1.12 -CubeRenderer::CubeRenderer() :
    1.13 -	m_loadingComplete(false),
    1.14 -	m_indexCount(0)
    1.15 -{
    1.16 -}
    1.17 -
    1.18 -void CubeRenderer::CreateDeviceResources()
    1.19 -{
    1.20 -	Direct3DBase::CreateDeviceResources();
    1.21 -
    1.22 -	auto loadVSTask = DX::ReadDataAsync("SimpleVertexShader.cso");
    1.23 -	auto loadPSTask = DX::ReadDataAsync("SimplePixelShader.cso");
    1.24 -
    1.25 -	auto createVSTask = loadVSTask.then([this](Platform::Array<byte>^ fileData) {
    1.26 -		DX::ThrowIfFailed(
    1.27 -			m_d3dDevice->CreateVertexShader(
    1.28 - 				fileData->Data,
    1.29 -				fileData->Length,
    1.30 -				nullptr,
    1.31 -				&m_vertexShader
    1.32 -				)
    1.33 -			);
    1.34 -
    1.35 -		const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = 
    1.36 -		{
    1.37 -			{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
    1.38 -			{ "COLOR",    0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    1.39 -		};
    1.40 -
    1.41 -		DX::ThrowIfFailed(
    1.42 -			m_d3dDevice->CreateInputLayout(
    1.43 -				vertexDesc,
    1.44 -				ARRAYSIZE(vertexDesc),
    1.45 -				fileData->Data,
    1.46 -				fileData->Length,
    1.47 -				&m_inputLayout
    1.48 -				)
    1.49 -			);
    1.50 -	});
    1.51 -
    1.52 -	auto createPSTask = loadPSTask.then([this](Platform::Array<byte>^ fileData) {
    1.53 -		DX::ThrowIfFailed(
    1.54 -			m_d3dDevice->CreatePixelShader(
    1.55 -				fileData->Data,
    1.56 -				fileData->Length,
    1.57 -				nullptr,
    1.58 -				&m_pixelShader
    1.59 -				)
    1.60 -			);
    1.61 -
    1.62 -		CD3D11_BUFFER_DESC constantBufferDesc(sizeof(ModelViewProjectionConstantBuffer), D3D11_BIND_CONSTANT_BUFFER);
    1.63 -		DX::ThrowIfFailed(
    1.64 -			m_d3dDevice->CreateBuffer(
    1.65 -				&constantBufferDesc,
    1.66 -				nullptr,
    1.67 -				&m_constantBuffer
    1.68 -				)
    1.69 -			);
    1.70 -	});
    1.71 -
    1.72 -	auto createCubeTask = (createPSTask && createVSTask).then([this] () {
    1.73 -		VertexPositionColor cubeVertices[] = 
    1.74 -		{
    1.75 -			{XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, 0.0f)},
    1.76 -			{XMFLOAT3(-0.5f, -0.5f,  0.5f), XMFLOAT3(0.0f, 0.0f, 1.0f)},
    1.77 -			{XMFLOAT3(-0.5f,  0.5f, -0.5f), XMFLOAT3(0.0f, 1.0f, 0.0f)},
    1.78 -			{XMFLOAT3(-0.5f,  0.5f,  0.5f), XMFLOAT3(0.0f, 1.0f, 1.0f)},
    1.79 -			{XMFLOAT3( 0.5f, -0.5f, -0.5f), XMFLOAT3(1.0f, 0.0f, 0.0f)},
    1.80 -			{XMFLOAT3( 0.5f, -0.5f,  0.5f), XMFLOAT3(1.0f, 0.0f, 1.0f)},
    1.81 -			{XMFLOAT3( 0.5f,  0.5f, -0.5f), XMFLOAT3(1.0f, 1.0f, 0.0f)},
    1.82 -			{XMFLOAT3( 0.5f,  0.5f,  0.5f), XMFLOAT3(1.0f, 1.0f, 1.0f)},
    1.83 -		};
    1.84 -
    1.85 -		D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
    1.86 -		vertexBufferData.pSysMem = cubeVertices;
    1.87 -		vertexBufferData.SysMemPitch = 0;
    1.88 -		vertexBufferData.SysMemSlicePitch = 0;
    1.89 -		CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(cubeVertices), D3D11_BIND_VERTEX_BUFFER);
    1.90 -		DX::ThrowIfFailed(
    1.91 -			m_d3dDevice->CreateBuffer(
    1.92 -				&vertexBufferDesc,
    1.93 -				&vertexBufferData,
    1.94 -				&m_vertexBuffer
    1.95 -				)
    1.96 -			);
    1.97 -
    1.98 -		unsigned short cubeIndices[] = 
    1.99 -		{
   1.100 -			0,2,1, // -x
   1.101 -			1,2,3,
   1.102 -
   1.103 -			4,5,6, // +x
   1.104 -			5,7,6,
   1.105 -
   1.106 -			0,1,5, // -y
   1.107 -			0,5,4,
   1.108 -
   1.109 -			2,6,7, // +y
   1.110 -			2,7,3,
   1.111 -
   1.112 -			0,4,6, // -z
   1.113 -			0,6,2,
   1.114 -
   1.115 -			1,3,7, // +z
   1.116 -			1,7,5,
   1.117 -		};
   1.118 -
   1.119 -		m_indexCount = ARRAYSIZE(cubeIndices);
   1.120 -
   1.121 -		D3D11_SUBRESOURCE_DATA indexBufferData = {0};
   1.122 -		indexBufferData.pSysMem = cubeIndices;
   1.123 -		indexBufferData.SysMemPitch = 0;
   1.124 -		indexBufferData.SysMemSlicePitch = 0;
   1.125 -		CD3D11_BUFFER_DESC indexBufferDesc(sizeof(cubeIndices), D3D11_BIND_INDEX_BUFFER);
   1.126 -		DX::ThrowIfFailed(
   1.127 -			m_d3dDevice->CreateBuffer(
   1.128 -				&indexBufferDesc,
   1.129 -				&indexBufferData,
   1.130 -				&m_indexBuffer
   1.131 -				)
   1.132 -			);
   1.133 -	});
   1.134 -
   1.135 -	createCubeTask.then([this] () {
   1.136 -		m_loadingComplete = true;
   1.137 -	});
   1.138 -}
   1.139 -
   1.140 -void CubeRenderer::CreateWindowSizeDependentResources()
   1.141 -{
   1.142 -	Direct3DBase::CreateWindowSizeDependentResources();
   1.143 -
   1.144 -	float aspectRatio = m_windowBounds.Width / m_windowBounds.Height;
   1.145 -	float fovAngleY = 70.0f * XM_PI / 180.0f;
   1.146 -
   1.147 -	// Note that the m_orientationTransform3D matrix is post-multiplied here
   1.148 -	// in order to correctly orient the scene to match the display orientation.
   1.149 -	// This post-multiplication step is required for any draw calls that are
   1.150 -	// made to the swap chain render target. For draw calls to other targets,
   1.151 -	// this transform should not be applied.
   1.152 -	XMStoreFloat4x4(
   1.153 -		&m_constantBufferData.projection,
   1.154 -		XMMatrixTranspose(
   1.155 -			XMMatrixMultiply(
   1.156 -				XMMatrixPerspectiveFovRH(
   1.157 -					fovAngleY,
   1.158 -					aspectRatio,
   1.159 -					0.01f,
   1.160 -					100.0f
   1.161 -					),
   1.162 -				XMLoadFloat4x4(&m_orientationTransform3D)
   1.163 -				)
   1.164 -			)
   1.165 -		);
   1.166 -}
   1.167 -
   1.168 -void CubeRenderer::Update(float timeTotal, float timeDelta)
   1.169 -{
   1.170 -	(void) timeDelta; // Unused parameter.
   1.171 -
   1.172 -	XMVECTOR eye = XMVectorSet(0.0f, 0.7f, 1.5f, 0.0f);
   1.173 -	XMVECTOR at = XMVectorSet(0.0f, -0.1f, 0.0f, 0.0f);
   1.174 -	XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
   1.175 -
   1.176 -	XMStoreFloat4x4(&m_constantBufferData.view, XMMatrixTranspose(XMMatrixLookAtRH(eye, at, up)));
   1.177 -	XMStoreFloat4x4(&m_constantBufferData.model, XMMatrixTranspose(XMMatrixRotationY(timeTotal * XM_PIDIV4)));
   1.178 -}
   1.179 -
   1.180 -void CubeRenderer::Render()
   1.181 -{
   1.182 -	const float midnightBlue[] = { 0.098f, 0.098f, 0.439f, 1.000f };
   1.183 -	m_d3dContext->ClearRenderTargetView(
   1.184 -		m_renderTargetView.Get(),
   1.185 -		midnightBlue
   1.186 -		);
   1.187 -
   1.188 -	m_d3dContext->ClearDepthStencilView(
   1.189 -		m_depthStencilView.Get(),
   1.190 -		D3D11_CLEAR_DEPTH,
   1.191 -		1.0f,
   1.192 -		0
   1.193 -		);
   1.194 -
   1.195 -	// Only draw the cube once it is loaded (loading is asynchronous).
   1.196 -	if (!m_loadingComplete)
   1.197 -	{
   1.198 -		return;
   1.199 -	}
   1.200 -
   1.201 -	m_d3dContext->OMSetRenderTargets(
   1.202 -		1,
   1.203 -		m_renderTargetView.GetAddressOf(),
   1.204 -		m_depthStencilView.Get()
   1.205 -		);
   1.206 -
   1.207 -	m_d3dContext->UpdateSubresource(
   1.208 -		m_constantBuffer.Get(),
   1.209 -		0,
   1.210 -		NULL,
   1.211 -		&m_constantBufferData,
   1.212 -		0,
   1.213 -		0
   1.214 -		);
   1.215 -
   1.216 -	UINT stride = sizeof(VertexPositionColor);
   1.217 -	UINT offset = 0;
   1.218 -	m_d3dContext->IASetVertexBuffers(
   1.219 -		0,
   1.220 -		1,
   1.221 -		m_vertexBuffer.GetAddressOf(),
   1.222 -		&stride,
   1.223 -		&offset
   1.224 -		);
   1.225 -
   1.226 -	m_d3dContext->IASetIndexBuffer(
   1.227 -		m_indexBuffer.Get(),
   1.228 -		DXGI_FORMAT_R16_UINT,
   1.229 -		0
   1.230 -		);
   1.231 -
   1.232 -	m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
   1.233 -
   1.234 -	m_d3dContext->IASetInputLayout(m_inputLayout.Get());
   1.235 -
   1.236 -	m_d3dContext->VSSetShader(
   1.237 -		m_vertexShader.Get(),
   1.238 -		nullptr,
   1.239 -		0
   1.240 -		);
   1.241 -
   1.242 -	m_d3dContext->VSSetConstantBuffers(
   1.243 -		0,
   1.244 -		1,
   1.245 -		m_constantBuffer.GetAddressOf()
   1.246 -		);
   1.247 -
   1.248 -	m_d3dContext->PSSetShader(
   1.249 -		m_pixelShader.Get(),
   1.250 -		nullptr,
   1.251 -		0
   1.252 -		);
   1.253 -
   1.254 -	m_d3dContext->DrawIndexed(
   1.255 -		m_indexCount,
   1.256 -		0,
   1.257 -		0
   1.258 -		);
   1.259 -}