src/video/windowsrt/CubeRenderer.cpp
author David Ludwig <dludwig@pobox.com>
Sun, 28 Oct 2012 20:47:33 -0400
changeset 8332 ea83d191cc17
parent 8322 839cd578a474
permissions -rw-r--r--
WinRT: added mouse button event support
dludwig@8322
     1
#include "SDLmain_WinRT_common.h"
dludwig@8322
     2
#include "CubeRenderer.h"
dludwig@8322
     3
dludwig@8322
     4
using namespace DirectX;
dludwig@8322
     5
using namespace Microsoft::WRL;
dludwig@8322
     6
using namespace Windows::Foundation;
dludwig@8322
     7
using namespace Windows::UI::Core;
dludwig@8322
     8
dludwig@8322
     9
CubeRenderer::CubeRenderer() :
dludwig@8322
    10
	m_loadingComplete(false),
dludwig@8322
    11
	m_indexCount(0)
dludwig@8322
    12
{
dludwig@8322
    13
}
dludwig@8322
    14
dludwig@8322
    15
void CubeRenderer::CreateDeviceResources()
dludwig@8322
    16
{
dludwig@8322
    17
	Direct3DBase::CreateDeviceResources();
dludwig@8322
    18
dludwig@8322
    19
	auto loadVSTask = DX::ReadDataAsync("SimpleVertexShader.cso");
dludwig@8322
    20
	auto loadPSTask = DX::ReadDataAsync("SimplePixelShader.cso");
dludwig@8322
    21
dludwig@8322
    22
	auto createVSTask = loadVSTask.then([this](Platform::Array<byte>^ fileData) {
dludwig@8322
    23
		DX::ThrowIfFailed(
dludwig@8322
    24
			m_d3dDevice->CreateVertexShader(
dludwig@8322
    25
 				fileData->Data,
dludwig@8322
    26
				fileData->Length,
dludwig@8322
    27
				nullptr,
dludwig@8322
    28
				&m_vertexShader
dludwig@8322
    29
				)
dludwig@8322
    30
			);
dludwig@8322
    31
dludwig@8322
    32
		const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = 
dludwig@8322
    33
		{
dludwig@8322
    34
			{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
dludwig@8322
    35
			{ "COLOR",    0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
dludwig@8322
    36
		};
dludwig@8322
    37
dludwig@8322
    38
		DX::ThrowIfFailed(
dludwig@8322
    39
			m_d3dDevice->CreateInputLayout(
dludwig@8322
    40
				vertexDesc,
dludwig@8322
    41
				ARRAYSIZE(vertexDesc),
dludwig@8322
    42
				fileData->Data,
dludwig@8322
    43
				fileData->Length,
dludwig@8322
    44
				&m_inputLayout
dludwig@8322
    45
				)
dludwig@8322
    46
			);
dludwig@8322
    47
	});
dludwig@8322
    48
dludwig@8322
    49
	auto createPSTask = loadPSTask.then([this](Platform::Array<byte>^ fileData) {
dludwig@8322
    50
		DX::ThrowIfFailed(
dludwig@8322
    51
			m_d3dDevice->CreatePixelShader(
dludwig@8322
    52
				fileData->Data,
dludwig@8322
    53
				fileData->Length,
dludwig@8322
    54
				nullptr,
dludwig@8322
    55
				&m_pixelShader
dludwig@8322
    56
				)
dludwig@8322
    57
			);
dludwig@8322
    58
dludwig@8322
    59
		CD3D11_BUFFER_DESC constantBufferDesc(sizeof(ModelViewProjectionConstantBuffer), D3D11_BIND_CONSTANT_BUFFER);
dludwig@8322
    60
		DX::ThrowIfFailed(
dludwig@8322
    61
			m_d3dDevice->CreateBuffer(
dludwig@8322
    62
				&constantBufferDesc,
dludwig@8322
    63
				nullptr,
dludwig@8322
    64
				&m_constantBuffer
dludwig@8322
    65
				)
dludwig@8322
    66
			);
dludwig@8322
    67
	});
dludwig@8322
    68
dludwig@8322
    69
	auto createCubeTask = (createPSTask && createVSTask).then([this] () {
dludwig@8322
    70
		VertexPositionColor cubeVertices[] = 
dludwig@8322
    71
		{
dludwig@8322
    72
			{XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, 0.0f)},
dludwig@8322
    73
			{XMFLOAT3(-0.5f, -0.5f,  0.5f), XMFLOAT3(0.0f, 0.0f, 1.0f)},
dludwig@8322
    74
			{XMFLOAT3(-0.5f,  0.5f, -0.5f), XMFLOAT3(0.0f, 1.0f, 0.0f)},
dludwig@8322
    75
			{XMFLOAT3(-0.5f,  0.5f,  0.5f), XMFLOAT3(0.0f, 1.0f, 1.0f)},
dludwig@8322
    76
			{XMFLOAT3( 0.5f, -0.5f, -0.5f), XMFLOAT3(1.0f, 0.0f, 0.0f)},
dludwig@8322
    77
			{XMFLOAT3( 0.5f, -0.5f,  0.5f), XMFLOAT3(1.0f, 0.0f, 1.0f)},
dludwig@8322
    78
			{XMFLOAT3( 0.5f,  0.5f, -0.5f), XMFLOAT3(1.0f, 1.0f, 0.0f)},
dludwig@8322
    79
			{XMFLOAT3( 0.5f,  0.5f,  0.5f), XMFLOAT3(1.0f, 1.0f, 1.0f)},
dludwig@8322
    80
		};
dludwig@8322
    81
dludwig@8322
    82
		D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
dludwig@8322
    83
		vertexBufferData.pSysMem = cubeVertices;
dludwig@8322
    84
		vertexBufferData.SysMemPitch = 0;
dludwig@8322
    85
		vertexBufferData.SysMemSlicePitch = 0;
dludwig@8322
    86
		CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(cubeVertices), D3D11_BIND_VERTEX_BUFFER);
dludwig@8322
    87
		DX::ThrowIfFailed(
dludwig@8322
    88
			m_d3dDevice->CreateBuffer(
dludwig@8322
    89
				&vertexBufferDesc,
dludwig@8322
    90
				&vertexBufferData,
dludwig@8322
    91
				&m_vertexBuffer
dludwig@8322
    92
				)
dludwig@8322
    93
			);
dludwig@8322
    94
dludwig@8322
    95
		unsigned short cubeIndices[] = 
dludwig@8322
    96
		{
dludwig@8322
    97
			0,2,1, // -x
dludwig@8322
    98
			1,2,3,
dludwig@8322
    99
dludwig@8322
   100
			4,5,6, // +x
dludwig@8322
   101
			5,7,6,
dludwig@8322
   102
dludwig@8322
   103
			0,1,5, // -y
dludwig@8322
   104
			0,5,4,
dludwig@8322
   105
dludwig@8322
   106
			2,6,7, // +y
dludwig@8322
   107
			2,7,3,
dludwig@8322
   108
dludwig@8322
   109
			0,4,6, // -z
dludwig@8322
   110
			0,6,2,
dludwig@8322
   111
dludwig@8322
   112
			1,3,7, // +z
dludwig@8322
   113
			1,7,5,
dludwig@8322
   114
		};
dludwig@8322
   115
dludwig@8322
   116
		m_indexCount = ARRAYSIZE(cubeIndices);
dludwig@8322
   117
dludwig@8322
   118
		D3D11_SUBRESOURCE_DATA indexBufferData = {0};
dludwig@8322
   119
		indexBufferData.pSysMem = cubeIndices;
dludwig@8322
   120
		indexBufferData.SysMemPitch = 0;
dludwig@8322
   121
		indexBufferData.SysMemSlicePitch = 0;
dludwig@8322
   122
		CD3D11_BUFFER_DESC indexBufferDesc(sizeof(cubeIndices), D3D11_BIND_INDEX_BUFFER);
dludwig@8322
   123
		DX::ThrowIfFailed(
dludwig@8322
   124
			m_d3dDevice->CreateBuffer(
dludwig@8322
   125
				&indexBufferDesc,
dludwig@8322
   126
				&indexBufferData,
dludwig@8322
   127
				&m_indexBuffer
dludwig@8322
   128
				)
dludwig@8322
   129
			);
dludwig@8322
   130
	});
dludwig@8322
   131
dludwig@8322
   132
	createCubeTask.then([this] () {
dludwig@8322
   133
		m_loadingComplete = true;
dludwig@8322
   134
	});
dludwig@8322
   135
}
dludwig@8322
   136
dludwig@8322
   137
void CubeRenderer::CreateWindowSizeDependentResources()
dludwig@8322
   138
{
dludwig@8322
   139
	Direct3DBase::CreateWindowSizeDependentResources();
dludwig@8322
   140
dludwig@8322
   141
	float aspectRatio = m_windowBounds.Width / m_windowBounds.Height;
dludwig@8322
   142
	float fovAngleY = 70.0f * XM_PI / 180.0f;
dludwig@8322
   143
dludwig@8322
   144
	// Note that the m_orientationTransform3D matrix is post-multiplied here
dludwig@8322
   145
	// in order to correctly orient the scene to match the display orientation.
dludwig@8322
   146
	// This post-multiplication step is required for any draw calls that are
dludwig@8322
   147
	// made to the swap chain render target. For draw calls to other targets,
dludwig@8322
   148
	// this transform should not be applied.
dludwig@8322
   149
	XMStoreFloat4x4(
dludwig@8322
   150
		&m_constantBufferData.projection,
dludwig@8322
   151
		XMMatrixTranspose(
dludwig@8322
   152
			XMMatrixMultiply(
dludwig@8322
   153
				XMMatrixPerspectiveFovRH(
dludwig@8322
   154
					fovAngleY,
dludwig@8322
   155
					aspectRatio,
dludwig@8322
   156
					0.01f,
dludwig@8322
   157
					100.0f
dludwig@8322
   158
					),
dludwig@8322
   159
				XMLoadFloat4x4(&m_orientationTransform3D)
dludwig@8322
   160
				)
dludwig@8322
   161
			)
dludwig@8322
   162
		);
dludwig@8322
   163
}
dludwig@8322
   164
dludwig@8322
   165
void CubeRenderer::Update(float timeTotal, float timeDelta)
dludwig@8322
   166
{
dludwig@8322
   167
	(void) timeDelta; // Unused parameter.
dludwig@8322
   168
dludwig@8322
   169
	XMVECTOR eye = XMVectorSet(0.0f, 0.7f, 1.5f, 0.0f);
dludwig@8322
   170
	XMVECTOR at = XMVectorSet(0.0f, -0.1f, 0.0f, 0.0f);
dludwig@8322
   171
	XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
dludwig@8322
   172
dludwig@8322
   173
	XMStoreFloat4x4(&m_constantBufferData.view, XMMatrixTranspose(XMMatrixLookAtRH(eye, at, up)));
dludwig@8322
   174
	XMStoreFloat4x4(&m_constantBufferData.model, XMMatrixTranspose(XMMatrixRotationY(timeTotal * XM_PIDIV4)));
dludwig@8322
   175
}
dludwig@8322
   176
dludwig@8322
   177
void CubeRenderer::Render()
dludwig@8322
   178
{
dludwig@8322
   179
	const float midnightBlue[] = { 0.098f, 0.098f, 0.439f, 1.000f };
dludwig@8322
   180
	m_d3dContext->ClearRenderTargetView(
dludwig@8322
   181
		m_renderTargetView.Get(),
dludwig@8322
   182
		midnightBlue
dludwig@8322
   183
		);
dludwig@8322
   184
dludwig@8322
   185
	m_d3dContext->ClearDepthStencilView(
dludwig@8322
   186
		m_depthStencilView.Get(),
dludwig@8322
   187
		D3D11_CLEAR_DEPTH,
dludwig@8322
   188
		1.0f,
dludwig@8322
   189
		0
dludwig@8322
   190
		);
dludwig@8322
   191
dludwig@8322
   192
	// Only draw the cube once it is loaded (loading is asynchronous).
dludwig@8322
   193
	if (!m_loadingComplete)
dludwig@8322
   194
	{
dludwig@8322
   195
		return;
dludwig@8322
   196
	}
dludwig@8322
   197
dludwig@8322
   198
	m_d3dContext->OMSetRenderTargets(
dludwig@8322
   199
		1,
dludwig@8322
   200
		m_renderTargetView.GetAddressOf(),
dludwig@8322
   201
		m_depthStencilView.Get()
dludwig@8322
   202
		);
dludwig@8322
   203
dludwig@8322
   204
	m_d3dContext->UpdateSubresource(
dludwig@8322
   205
		m_constantBuffer.Get(),
dludwig@8322
   206
		0,
dludwig@8322
   207
		NULL,
dludwig@8322
   208
		&m_constantBufferData,
dludwig@8322
   209
		0,
dludwig@8322
   210
		0
dludwig@8322
   211
		);
dludwig@8322
   212
dludwig@8322
   213
	UINT stride = sizeof(VertexPositionColor);
dludwig@8322
   214
	UINT offset = 0;
dludwig@8322
   215
	m_d3dContext->IASetVertexBuffers(
dludwig@8322
   216
		0,
dludwig@8322
   217
		1,
dludwig@8322
   218
		m_vertexBuffer.GetAddressOf(),
dludwig@8322
   219
		&stride,
dludwig@8322
   220
		&offset
dludwig@8322
   221
		);
dludwig@8322
   222
dludwig@8322
   223
	m_d3dContext->IASetIndexBuffer(
dludwig@8322
   224
		m_indexBuffer.Get(),
dludwig@8322
   225
		DXGI_FORMAT_R16_UINT,
dludwig@8322
   226
		0
dludwig@8322
   227
		);
dludwig@8322
   228
dludwig@8322
   229
	m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
dludwig@8322
   230
dludwig@8322
   231
	m_d3dContext->IASetInputLayout(m_inputLayout.Get());
dludwig@8322
   232
dludwig@8322
   233
	m_d3dContext->VSSetShader(
dludwig@8322
   234
		m_vertexShader.Get(),
dludwig@8322
   235
		nullptr,
dludwig@8322
   236
		0
dludwig@8322
   237
		);
dludwig@8322
   238
dludwig@8322
   239
	m_d3dContext->VSSetConstantBuffers(
dludwig@8322
   240
		0,
dludwig@8322
   241
		1,
dludwig@8322
   242
		m_constantBuffer.GetAddressOf()
dludwig@8322
   243
		);
dludwig@8322
   244
dludwig@8322
   245
	m_d3dContext->PSSetShader(
dludwig@8322
   246
		m_pixelShader.Get(),
dludwig@8322
   247
		nullptr,
dludwig@8322
   248
		0
dludwig@8322
   249
		);
dludwig@8322
   250
dludwig@8322
   251
	m_d3dContext->DrawIndexed(
dludwig@8322
   252
		m_indexCount,
dludwig@8322
   253
		0,
dludwig@8322
   254
		0
dludwig@8322
   255
		);
dludwig@8322
   256
}