# Problem with orthographic projection

This topic is 2127 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So I'm looking into doing some 2D graphics with Direct3D 11. I am having a problem getting a simple orthographic projection to work right, and I am hoping that somebody can tell me what I may be doing wrong, or ideas of how to figure it out.

My vertex layout is a simple position/color layout, and the vertices are these:

{0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f},
{1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
{-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f}

cbuffer cbChangesPerFrame : register( b0 )
{
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
};

struct VS_Input
{
float4 pos   : POSITION;
float4 color : COLOR;
};

struct VS_Output
{
float4 pos   : SV_POSITION;
float4 color : COLOR;
};

VS_Output main( VS_Input vertex )
{
VS_Output vsOut = (VS_Output)0;
vsOut.pos = mul(vertex.pos, worldMatrix);
vsOut.pos = mul(vsOut.pos, viewMatrix);
vsOut.pos = mul(vsOut.pos, projectionMatrix);
vsOut.color = vertex.color;

return vsOut;
}

And this is my code to setup the constant buffer:

    XMMATRIX all[3];
all[0] = XMMatrixIdentity();
all[1] = XMMatrixIdentity();
all[2] = XMMatrixOrthographicOffCenterLH( 0.0f, 600.0f, 0.0f, 400.0f, 0.1f, 100.0f );

ctx->UpdateSubresource( _constants, 0, 0, all, sizeof(XMMATRIX) * 3, 0 );
ctx->VSSetConstantBuffers( 0, 1, &_constants );



This results in a red single-pixel line being drawn from the top center of the screen, to about the center of the screen.

If I change my matrices to the following:

    XMMATRIX all[3];
all[0] = XMMatrixIdentity();
all[1] = XMMatrixIdentity();
all[2] = XMMatrixIdentity();


Then the triangle renders as I expect; with one point at the top center, and the other 2 points at the bottom corners, with a red-green-blue gradient. So it would appear that the vertices are being correctly pushed to the shaders.

I have tried explicitly setting the .w position value to 1 in the vertex shader, I have tried using 4-element position values in the code, I have tried using a single matrix in the shader and multiplying them in code, and I have tried using a single matrix in the shader and just setting it to the projection matrix, all of which I figure should be the same since I'm using identity for the other 2 matrices anyway, and of course the result is the same in all cases.

What could I be missing here?

Thanks!

##### Share on other sites

Well I found that if I use D3DXMATRIX and D3DXMatrixOrthoLH instead, it works. I also have to change the vertices to use pixel values, which is what I expect, but which didn't work the other way either.

Perhaps some kind of alignment issue?

##### Share on other sites

Okay, so XMMatrixOrthographicOffCenterLH seems to work in a way that I don't quite understand. If I use XMMatrixOrthographicLH it works fine, or if I set the values of XMMatrixOrthographicOffCenterLH  to -300, 300, -200, 200, then it works exactly the same as XMMatrixOrthographicLH. If I set the values to 0, 600, 0, 400, I get some really funky results. I understand why the first one works the way it does, but it doesn't seem like the second one should give me the results that I am getting.

##### Share on other sites
HLSL defaults to column-major matrix layout, so you need to transpose your matrices before sending them to the constant buffer, e.g.

all[2] = XMMatrixTranspose(XMMatrixOrthographicOffCenterLH( 0.0f, 600.0f, 0.0f, 400.0f, 0.1f, 100.0f ))


Alternatively tag your matrices with row_major in your HLSL source file (there's also a compiler flag to change this globally).

##### Share on other sites

Also check your zNear and zFar input params - in this case you're using zNear 0.1 and zFar 100, but yet you're drawing your geometry with a Z of 0, which is outside of this range.

##### Share on other sites

Grrr. I had already tried transposing the matrix and it didn't work. Somehow the Z of 0 didn't even occur to me. Transposing and using a Z of 1 works.

Thanks.

• 13
• 18
• 29
• 11
• 27