Black Screen: How to set Projection/View Matrix
- by Lisa
I have a Windows Phone 8 C#/XAML with DirectX component project. I'm rendering some particles, but each particle is a rectangle versus a square (as I've set the vertices to be positions equally offset from each other). I used an Identity matrix in the view and projection matrix.
I decided to add the windows aspect ratio to prevent the rectangles. But now I get a black screen. None of the particles are rendered now. I don't know what's wrong with my matrices. Can anyone see the problem?
These are the default matrices in Microsoft's project example.
View Matrix:
XMVECTOR eye = XMVectorSet(0.0f, 0.7f, 1.5f, 0.0f);
XMVECTOR at = XMVectorSet(0.0f, -0.1f, 0.0f, 0.0f);
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
XMStoreFloat4x4(&m_constantBufferData.view, XMMatrixTranspose(XMMatrixLookAtRH(eye, at, up)));
Projection Matrix:
void CubeRenderer::CreateWindowSizeDependentResources()
{
Direct3DBase::CreateWindowSizeDependentResources();
float aspectRatio = m_windowBounds.Width / m_windowBounds.Height;
float fovAngleY = 70.0f * XM_PI / 180.0f;
if (aspectRatio < 1.0f)
{
fovAngleY /= aspectRatio;
}
XMStoreFloat4x4(&m_constantBufferData.projection, XMMatrixTranspose(XMMatrixPerspectiveFovRH(fovAngleY, aspectRatio, 0.01f, 100.0f)));
}
I've tried modifying them to use cocos2dx's WP8 example.
XMMATRIX identityMatrix = XMMatrixIdentity();
float fovy = 60.0f;
float aspect = m_windowBounds.Width / m_windowBounds.Height;
float zNear = 0.1f;
float zFar = 100.0f;
float xmin, xmax, ymin, ymax;
ymax = zNear * tanf(fovy * XM_PI / 360);
ymin = -ymax;
xmin = ymin * aspect;
xmax = ymax * aspect;
XMMATRIX tmpMatrix = XMMatrixPerspectiveOffCenterRH(xmin, xmax, ymin, ymax, zNear, zFar);
XMMATRIX projectionMatrix = XMMatrixMultiply(tmpMatrix, identityMatrix);
// View Matrix
float fEyeX = m_windowBounds.Width * 0.5f;
float fEyeY = m_windowBounds.Height * 0.5f;
float fEyeZ = m_windowBounds.Height / 1.1566f;
float fLookAtX = m_windowBounds.Width * 0.5f;
float fLookAtY = m_windowBounds.Height * 0.5f;
float fLookAtZ = 0.0f;
float fUpX = 0.0f;
float fUpY = 1.0f;
float fUpZ = 0.0f;
XMMATRIX tmpMatrix2 = XMMatrixLookAtRH(XMVectorSet(fEyeX,fEyeY,fEyeZ,0.f), XMVectorSet(fLookAtX,fLookAtY,fLookAtZ,0.f), XMVectorSet(fUpX,fUpY,fUpZ,0.f));
XMMATRIX viewMatrix = XMMatrixMultiply(tmpMatrix2, identityMatrix);
XMStoreFloat4x4(&m_constantBufferData.view, viewMatrix);
Vertex Shader
cbuffer ModelViewProjectionConstantBuffer : register(b0)
{
//matrix model;
matrix view;
matrix projection;
};
struct VertexInputType
{
float4 position : POSITION;
float2 tex : TEXCOORD0;
float4 color : COLOR;
};
struct PixelInputType
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
float4 color : COLOR;
};
PixelInputType main(VertexInputType input)
{
PixelInputType output;
// Change the position vector to be 4 units for proper matrix calculations.
input.position.w = 1.0f;
//=====================================
// TODO: ADDED for testing
input.position.z = 0.0f;
//=====================================
// Calculate the position of the vertex against the world, view, and projection matrices.
//output.position = mul(input.position, model);
output.position = mul(input.position, view);
output.position = mul(output.position, projection);
// Store the texture coordinates for the pixel shader.
output.tex = input.tex;
// Store the particle color for the pixel shader.
output.color = input.color;
return output;
}
Before I render the shader, I set the view/projection matrices into the constant buffer
void ParticleRenderer::SetShaderParameters()
{
ViewProjectionConstantBuffer* dataPtr;
D3D11_MAPPED_SUBRESOURCE mappedResource;
DX::ThrowIfFailed(m_d3dContext->Map(m_constantBuffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource));
dataPtr = (ViewProjectionConstantBuffer*)mappedResource.pData;
dataPtr->view = m_constantBufferData.view;
dataPtr->projection = m_constantBufferData.projection;
m_d3dContext->Unmap(m_constantBuffer.Get(), 0);
// Now set the constant buffer in the vertex shader with the updated values.
m_d3dContext->VSSetConstantBuffers(0, 1, m_constantBuffer.GetAddressOf() );
// Set shader texture resource in the pixel shader.
m_d3dContext->PSSetShaderResources(0, 1, &m_textureView);
}
Nothing, black screen... I tried so many different look at, eye, and up vectors. I tried transposing the matrices. I've set the particle center position to always be (0, 0, 0), I tried different positions too, just to make sure they're not being rendered offscreen.