Sprites - Screen Co-ordinates to 3D World-Space

Started by
3 comments, last by Erik Rufelt 14 years, 8 months ago
Example: Say I want to draw a 128 by 128 pixel image to the screen co-ordinates 0, 0, 128, 128. If my camera is located at 0, 0, 0 in world-space co-ordinates, how do I calculate the position and scale of a 1 x 1 quad, in world-space, so that it appears at the rectangle defined as 0, 0, 128, 128 in screen co-ordinates? I've tried looking for source code on how ID3DX works internally, but I can't surmise anything more than that it uses textured 3D quads. I'm trying to understand how it takes screen co-ordinates as input and then generates world-space co-ordinates for use in rendering textured 3D quads. I'm going to take a gamble and say the answer has something to do with "unprojecting" screen co-ordinates to world-space, but I'm uncertain as to what projection matrix results in orthogonal vision or how to "unproject" as it were. Any suggestions or recommendations?
Advertisement
Does it need to be in 3D?
You can simply draw the quad with 2D-coordinates 0,0,128,128 if you want. For D3D9, use D3DFVF_XYZRHW. Tutorial here: Tutorial 2: Rendering Vertices.
Or how to do it in D3D10 here: Tutorial 2: Rendering a Triangle.

Also, in D3D9 pixel centers are not at 0.0f, but offset with -0.5f. So you would need to draw with -0.5f,-0.5f,127.5f,127.5f, to get perfect pixel mapping. In D3D10 this has changed to exact coordinates.
I'm working with pixel shaders/programmable pipeline. I'm avoiding the use of ID3DXSprite in favor of a system I can customize to my liking. I'm well aware of how to render 3D geometry using vertex buffers, index buffers, vertex shaders, and pixel shaders.

My inquiry is specific as to how one creates an orthogonal view and defines the field of vision as width and height in pixels, in addition to how one would "unproject" screen co-ordinates to world space co-ordinates, if that's even the right concept to apply.
World, View and Proj matrices can be identity; Then x and y are pixel sizes - on PC, Xbox may vary.
--------Roderick
Quote:Original post by Sion Sheevok
My inquiry is specific as to how one creates an orthogonal view and defines the field of vision as width and height in pixels, in addition to how one would "unproject" screen co-ordinates to world space co-ordinates, if that's even the right concept to apply.


If the purpose is to draw in screen-space, then then the answer is specific as to achieve that. =) Those tutorials do not specify coordinates in 3D, but in 2D screen-space pixel-coordinates. I don't think the sprite interface uses 3D quads internally, but 2D quads, exactly as in those tutorials, and does not generate world-space coordinates.
I assume you want to implement this, since you say you tried to see how that works internally. I apologize if I have misunderstood.
Just use an empty vertex shader, output.position = input.position, and specify screen-space coordinates. You can still specify a z-coordinate if you want depth culling.

Unprojecting can be done with D3DXVec3Unproject.

This topic is closed to new replies.

Advertisement