Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.


This is really odd... I can see my particle when it's an identity matrix, so if I set the matrix X position to the aspect ratio (1.6), my rectangle becomes a square. This is still puzzling because every other example and 3D graphics book will use the FOV, aspect, near, and far plane values. What is going on?

XMMATRIX tmpMatrix = XMMatrixSet(1.6f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);

A matrix calculated as below code should work, but it never did.

float fieldOfView = XM_PI / 4.0f; // = 0.78f; try 1: 70.0f; try 2: 70 * PI / 180
    float screenAspect = m_windowBounds.Height / m_windowBounds.Width; // WVGA portrait: 768/480 = 1.60, or even Width/Height
    XMMATRIX tmpMatrix2 = XMMatrixPerspectiveFovRH(fieldOfView, screenAspect, 0.1f, 100.0f);

Gives a matrix like this:

{1.50888336, 0.000000000, 0.000000000, 0.000000000} {0.000000000, 2.41421342, 0.000000000, 0.000000000} {0.000000000, 0.000000000, -1.00100100, -1.00000000} {0.000000000, 0.000000000, -0.100100100, 0.000000000}

share|improve this question

closed as off-topic by Josh Petrie May 7 at 16:14

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about debugging a problem in your project must present a concise selection of code and context so as to allow a reader to diagnose the issue without needing to read all of your code or to engage in extensive back-and-forth dialog. For more information, see this meta thread." – Josh Petrie
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Can you provide source code for download? –  Toni Petrina Dec 4 '12 at 11:21
    
@ToniPetrina Sure, thanks for checking this out. Using the MS WP8 C# XAML with DirectX default template, then substitute the CubeRenderer.cpp/.h file with dl.dropbox.com/u/3787151/WP8/CubeRenderer.cpp and dl.dropbox.com/u/3787151/WP8/CubeRenderer.h and add DirectXTK toolkit to the project. Here's a package of the DirectX Component: dl.dropbox.com/u/3787151/WP8/ParticleIssues.zip –  Lisa Dec 4 '12 at 12:50
    
@ToniPetrina Here's a project, compiles, and ready to run. This sample doesn't have all my "tries" at view and projection matrices. Thanks. dl.dropbox.com/u/3787151/WP8/PhoneXamlDirect3DApp1.zip –  Lisa Dec 4 '12 at 16:09
    
Thanks, I'll check and report back soon :) –  Toni Petrina Dec 4 '12 at 16:15
2  
Hm. Apparently I -1ed this some time ago without leaving a comment. Sorry about that. -1 for "debug my code for me". This sort of only-one-person-will-ever-be-interested question doesn't improve the Internet by being archived forever; it's really more appropriate for a transient discussion medium such as our chat, or a discussion forum. –  Trevor Powell Dec 7 '12 at 23:14