• Create Account

## Problem with orthographic projection

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1krippy2k8  Members

646
Like
0Likes
Like

Posted 22 September 2013 - 08:39 PM

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!

### #2krippy2k8  Members

646
Like
0Likes
Like

Posted 22 September 2013 - 09:12 PM

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?

### #3krippy2k8  Members

646
Like
0Likes
Like

Posted 22 September 2013 - 09:44 PM

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.

### #4unbird  Members

8298
Like
1Likes
Like

Posted 24 September 2013 - 12:06 PM

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).

### #5mhagain  Members

12440
Like
1Likes
Like

Posted 24 September 2013 - 02:23 PM

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.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #6krippy2k8  Members

646
Like
0Likes
Like

Posted 24 September 2013 - 10:52 PM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.