Reputation: 59
I'd like to render 2D geometry using DirectX (11) and have tried to do so by creating an effects file that doesn't transform vertices at all - that is where you'd normally apply your world/view/projection matrix I just return the (untransformed) input.
This worked as expected and my quad was rendered to the surface correctly. Then I tried transforming it with a translation (in 2D) along the X-axis and got some weird perspective changes.
Googling around I see people talking about using the Orthogonal transform matrices but don't understand why - I don't want to transform general 3d data into 2d, I'd like to do an identity transform of 2d data into 2d (and perhaps 2d translate/scale too)
I get the feeling I've missed something important about the pipeline?
Any help appreciated
Upvotes: 0
Views: 1320
Reputation: 182
I think you have probably forgotten to call m.Transpose() on your transformation matrix after finishing constructing but before assigning it to the device
Upvotes: 1
Reputation: 126
The projection matrix that you are using is a perspective matrix which gives perspective. This is fine when you are working in 3D, but when you want to render 2D (for example to render a GUI) you don't want to add perspective.
Orthogonal matrix is a matrix which basically ignores perspective, so that objects in the distance are just as big as things that are close.
The difference between them can be visualized as followed if you draw a box (a cube with the top missing):
Perspective vs Orthigonal
O---------O O---------O
|\ /| | |
| o-----o | | |
| | | | | |
| o-----o | | |
|/ \| | |
O---------O O---------O
So what happens in your case? Well, objects that are out of the center of your screen are naturally further away the objects that are near the center of your screen. Because of that they are drawn closer.
Example:
A B C Point A (-5, 0, 5)
\ | / Point B ( 0, 0, 5)
\ | / Point C ( 5, 0, 5)
\ | /
\ | /
\ | /
\|/
V
As you can see point A and B are further away. If we would use really basic perspective (it is more complex, but it suffices as example) to calculate where each point would come on your screen then we would use the following formula:
Vector2 Perspective(Vector3 pos)
{
return Vector2(pos.x, pos.y) / pos.length();
}
Now a orthogonal matrix would be more along the lines of the following:
Vector2 Orthogonal(Vector3 pos)
{
return Vector2(pos.x, pos.y);
}
So when you replace your old perspective matrix with a new orthogonal matrix then you should no longer have problems with perspective.
Upvotes: 2