Sign in to follow this  

How to position a textured quad in screen coordinates?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am experimenting with different matrices, studying their effect on a textured quad. So far I have implemented Scaling, Rotation, and Translation matrices fairly easily - by using the following method against my position vectors:

for(int a=0;a<noOfVertices;a++)
{
myVectorPositions[a] = SlimDX.Vector3.TransformCoordinate(myVectorPositions[a],myPerspectiveMatrix);
}

However, I what I want to do is be able to position my vectors using world-space coordinates, not object-space.
At the moment my position vectors are declared thusly:



[source lang="csharp"] myVectorPositions[0] = new Vector3(-0.1f, 0.1f, 0.5f);
myVectorPositions[1] = new Vector3(0.1f, 0.1f, 0.5f);
myVectorPositions[2] = new Vector3(-0.1f, -0.1f, 0.5f);
myVectorPositions[3] = new Vector3(0.1f, -0.1f, 0.5f);[/source]

On the other hand (and as part of learning about matrices) I have read that I need to apply a matrix to get to screen coordinates. I've been looking through the SlimDX API docs and can't seem to pin down the one I should be using.
In any case, hopefully the above makes sense and what I am trying to achieve is clear. I'm aiming for a simple 1024 x 768 window as my application area, and want to position a my textured quad at 10,10. How do I go about this? Most confused right now.

Share this post


Link to post
Share on other sites
Screen space is from (-1, -1) to (+1, +1), where (-1, 1) is top left corner, and (0, 0) is center, basically it's like cartesian plane.

If you want quad at (10, 10) from top left corner, it would look about
[CODE]
Vector2 pos(10, 10);
pos += Vector2(-width / 2, height / 2);
pos /= Vector2(width, height);
[/CODE]

However, I'm unsure about the approach with matrices.

Share this post


Link to post
Share on other sites
With SlimDX you can use Matrix.OrthoOffCenterLH()/Matrix.OrthoOffCenterRH() to create an orthographic projection matrix for a given set of screen dimensions (LH/RH specifies left-handed/right-handed coordinate systems). Whatever you specify as left becomes the x coordinate for the left side of the screen, right becomes the x coordinate for the right side of the screen, etc.

Share this post


Link to post
Share on other sites
I've had a crack at using the Ortho matrix you suggested. However my quad is invisible at the moment.

[source lang="csharp"] float oLeft = 0;
float oRight = screenwidth;
float oBottom = 0;
float oTop = screenheight;
float oZnear = 0.0f;
float oZfar = 1.0f;

myOrthoMatrix = SlimDX.Matrix.OrthoOffCenterLH(oLeft,oRight,oBottom,oTop,oZnear,oZfar);[/source]

[source lang="csharp"] for(int a=0;a<noOfVertices;a++)
{
myVectorPositions[a] = SlimDX.Vector3.TransformCoordinate(myVectorPositions[a],myOrthoMatrix);
}
[/source]

I'm not sure if my matrix definition is correct though, or my idea about how to use it is correct. I've interpreted the left and right parameters to mean the minimum and maximum X screen coordinates. Is that correct?

Share this post


Link to post
Share on other sites
Sign in to follow this