# How can I render primitives to pixel coordinates?

I'll emphasize that I am very new to graphics programming. I've been using XNA up until now and I've been looking for a way to build simple 2D functionality with textured quads. I'm sort of starting to get how the whole process works, but the idea of positioning things from 0 to 1 doesn't seem quite right for what I'm doing. Is this the only way to do it? I've learned that I'm supposed to be rendering quads to an orthogonal projection, but I guess my problem is exactly how to implement that the correct way.

I totally acknowledge that my problems are a result of not understanding the technology, but all of the tutorials I can find tend to gloss over a lot of the details. Is there a simple way to set up an orthogonal projection in d3d11, and is there a simple way to convert the coordinates so I can specify it in pixels (like the XNA spritebatch)? I'm hoping that seeing the answer to this will help me get a more solid grasp on a lot of the areas where I'm confused in general with graphics programming.

I don't know exactly how its done in DirectX as I've only ever worked with OpenGL, but presumably you just transform all your objects' location vectors by a scaling matrix made from the reciprocal of your viewport's current resolution.

For example, in fullscreen 1080p you would scale all x coordinates by 1/1920, and y's by 1/1080. That way you can specify all locations in pixels.

(UPDATE: Actually, having just tried this out, for pixel perfect it should be HALF the viewport size, so 1080p would mean scaling by x=1/960, y=1/540, I forgot that device coordinates go from -1.0 to 1.0, resulting in a width of 2.0 not 1.0. Yeah I know, obvious in hindsight huh?)

You just have to remember to keep those scale factors up-to-date with your viewport if it get's resized, and if I recall correctly, XNA's 2D origin was in the top left, while in this case it would always be the middle of the screen, though that's fix-able too, just translate down and right by half the resolution in each direction after scaling. (Update: this translation should still be correct)

Edit: Also, when working in 2D don't bother with complex orthogonal projection matrices, scaling with your resolution does the same thing and at least you know what's going on.

• Would this work perfectly for things that need perfect positioning, like UI elements or a tiled map?
– ssb
Jan 8 '13 at 13:25
• As long as you keep the scales and translates the same as the viewport's size all coordinates should be pixel perfect for every vector the matrix is applied to. Just to gauge your knowledge though, do you know what I mean when I spit out words like vector and matrix? Jan 8 '13 at 13:50
• I'm fine with that. A lot of the problems I've been having have been in figuring out exactly how I should be building my matrices, especially working out how I should be scaling sprites and how exactly to work the coordinates, and how that positioning relates to the shaders.
– ssb
Jan 8 '13 at 14:07
• In that case I'd say it's better for you to do a little more study on DirectX and it's rendering pipeline before diving in too far, it's a little too much to explain in one simple comment. Just look up a few basic DirectX tutorials, the more the better as the more practice you get the more this will start to make sense. For this case in particular, provided all of your objects are positioned using Vector objects, just position everything in pixels from the center of the screen and then transform the vectors by the matrix I mentioned and it's almost the same as XNA's SpriteBatch.Draw Jan 8 '13 at 14:27