Drawing 2D primitives

Feb 17, 2010 at 1:47 AM

Hi,

I am sorry If this is a silly question but how do I draw a 2d object (such as a mouse pointer, cross-hair, or UI elements) while having 3d objects in the background?

I have tried doing something like the following inside my Draw() call:

state.PushCamera(new Camera2D());
state.PushWorldMatrixMultiply(ref _world);

state.RenderState.AlphaBlend.SetToAlphaBlending();
state.DrawDynamicVertices(_geometry, PrimitiveType.LineList);

state.PopWorldMatrix();
state.PopCamera();

 

Feb 17, 2010 at 2:19 AM

It looks like that worked all along... but the origin was not where I thought it would be (top left corner) it is instead in the bottom left corner.

Also, using:

state.ProjectFromScreen(ref _screenPosition, 1, out _position);
gives expected results except that the y axis is inverted as well. How do I fix this? (-_screenPosition.Y does not work, the position is then below the screen. However it is not inverted)

 

Feb 17, 2010 at 8:36 AM

To get the correct screen position, you'll have to subtract the screen height from the Y component of the position you got from the ProjectFromScreen().

You'll probably get the height somewhere in the state variable, maybe in a property called "Viewport" or something.

Sorry, I just formatted my computer, I can't look it up properly.

Feb 18, 2010 at 7:43 AM

In Xen, Texture Element's position (0, 0) is Bottom - Left of screen. and Text Element's postion (0, 0) is top - left of screen.

Coordinator
Feb 18, 2010 at 9:36 AM
Edited Feb 18, 2010 at 9:37 AM

Hi sTASH,

When you are using a Camera2D, the coordinate system is [0,0] in th bottom left to [width,height] in the top right. If you specify 'Normalised' as true when constructing the Camera2D, it will be [0,0]->[1,1].

If you have an identity world matrix, then drawing should work fine. (ie, draw a line from [0,0] to [100,100] should do exactly that).

What has probably happened, is you have something strange in your world matrix. Setting the camera does not reset the world matrix stack.
I would try using an identity world matrix first, and see if that helps you out.

The *Element classes in Xen.Ex.Graphics2D are probably not what you are after in this case. They are quite heavy duty, as they make sure everything is setup exactly right (eg, all make sure the world matrix is identity, the camera is correct, etc).

Cheers