Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to retrieve XYZ-Coordinate from a XY-Screen-Coordinate (D3DRM)

30 views
Skip to first unread message

Tobias Schachte

unread,
Mar 8, 1999, 3:00:00 AM3/8/99
to
Hi,

my problem is to retreive the xyz-Coordinate of the background pixel where
the player hits the mouse-button. I can calculate the proper
(Camera-Space)-Z-Value from my own Z-Buffer, so I need only the x and y
coordinates.

I've used the following code to do this:

dv4d.x = (float)MouseX;
dv4d.y = (float)MouseY;
dv4d.z = CalculateFromZBuffer();
dv4d.w = 1.0f;
lpD3DRMViewport->InverseTransform( &dv, &dv4d );
lpD3DRMPlayerFrame->SetPosition( lpD3DRMWorldFrame, dv.x, dv.y, dv.z );

At first - what the hell is the w-value? I don't know how to calculate it -
or why I must provide it. But I'v type in everything - nothing helped. I've
received only useles values.

The next idea was to look what the viewport will transform with the initial
coordinates from PlayerFrame:

lpD3DRMPlayerFrame->GetPosition( lpD3DRMWorldFrame, &dv );
lpD3DRMViewport->Transform( &dv4d, &dv );
fprintf( stderr, "x = %f, y = %f, z = %f, w = %f\n", dv4d.x,
dv4d.y, dv4d.z, dv4d.w );

The PlayerFrame stands at -50, 1.62, -14, camera at -57.927, 5.345, -6.928
(World Coordinates), that is in the middle of the bottom-right corner of the
640x480 screen. The results are all but clear to me: x = 4639.304688, y =
3742.523438, z = 10.348289, w = 11.075963

Can please anyone tell me how to do it right? Thanks in advandce.

--
Tobias Schachte / SSE
scha...@csi.com

Hed Bar-Nissan

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to Tobias Schachte
What I didn't understand from your mail is whether there is an object at this
specific XY screen point. If there is then this action name is picking and youve
got a viewport method Pick.
If that what you need it's a few lines, i can send it to you.

hed.

Tobias Schachte

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
No, at the point the user clicks ist no object. But I have a static
background, perspective correct rendered, with an attached zbuffer-map. Now,
when the user clicks, I can retrieve the z-depth from my zbuffer-map (this
works, proven) and the screen-coordinates from the mouse-position. With this
three values I must calculate the proper X, Y and Z coordinates from the
background. I must cast a ray from the camera at the point the user clicks,
but it can never hit any object. It must stop at the moment it reaches my
z-depth-value from the zbuffer map.


--
Tobias Schachte / SSE
scha...@csi.com

Hed Bar-Nissan schrieb in Nachricht <36E4C7C2...@tiltan.com>...

Tobias Schachte

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
Ok, I guess I solve it. I can calculate the coordinates by my own:

lpD3DRMViewporx->GetPlane( &left, &right, &bottom, &top );
dv.x = ((right/320)*((float)MouseX-320))*ZValue;
dv.y = ((bottom/240)*((float)MouseY-240))*ZValue;
dv.z = ZValue;
lpD3DRMCameraFrame->Transform( &dvo, &dv );
lpD3DRMPlayerFrame->SetPosition( lpD3DRMWorldFrame, dvo.x, dvo.y, dvo.z );

Can anyone verify that?

pmel...@stormfront.com

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
i work with d3d immidiate mode, so i do my own transformation and
projection, if i have a screen point mouseX, mouseY then the viewport
point vx and vy are

vx = viewPortLeftX + mouseX * viewPortWidth / screenPixelWidth

and vy is

vy= viewPortTopY - mouseY * viewPortHeight / screenPixelHeight


your viewport is probably normalized from -1 to 1 with ( -1, 1 ) being
top left corner and your screen size is probably 640 x 480 so then you
have

vx = -1 + mouseX * 320
vy = 1 - mouseY * 240

so finally to get the world x, y you need to apply an inverse
perspecrive projection by multiplying vx and vy by the z that you
have, so

x = vx * z and y = vy * z

one thing to keep in mind is that you might be getting a z value that
is normalized between near and far clipping plane so you might need to
convert it back to world z before doing the above multiplication

z = nearClip + z * ( farClip - nearClip )


hope this helps

Robert Dunlop [MS MVP]

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
Tobias Schachte <Scha...@t-online.de> wrote in message
news:7c0v40$2gv$1...@news04.btx.dtag.de...
>Hi,

>
>The PlayerFrame stands at -50, 1.62, -14, camera at -57.927, 5.345, -6.928
>(World Coordinates), that is in the middle of the bottom-right corner of
the
>640x480 screen. The results are all but clear to me: x = 4639.304688, y =
>3742.523438, z = 10.348289, w = 11.075963


You have one step left in your transformation - divide x and y by w, which
in the above case yields 419, 338.... right where you would expect it ;-)


Robert Dunlop
Monarch Interactive, Inc.
http://www.monarch-interactive.com/workshop.html

Microsoft DirectX MVP
---------------------------------------
The opinions expressed in this message are my own personal views and do not
reflect the official views of the Microsoft Corporation

The MVP program does not constitute employment or contractual obligation
with Microsoft.


Tobias Schachte

unread,
Mar 10, 1999, 3:00:00 AM3/10/99
to
Hey, thats the way I've done it. It seems to work well. Thank you for your
effort.

But I still wonder what the w-value could be...

Robert Dunlop [MS MVP]

unread,
Mar 10, 1999, 3:00:00 AM3/10/99
to

Tobias Schachte

unread,
Mar 11, 1999, 3:00:00 AM3/11/99
to
Hmm, ok, I see it. But can you tell what this w-coordinate is?
How can I calculate it - for feeding the InverseTransform method.

--
Tobias Schachte / SSE
scha...@csi.com

Robert Dunlop [MS MVP] schrieb in Nachricht <7c5b2j$18a$2...@remarQ.com>...

Brian Upton

unread,
Mar 11, 1999, 3:00:00 AM3/11/99
to
Scha...@t-online.de (Tobias Schachte) wrote:

>Hmm, ok, I see it. But can you tell what this w-coordinate is?
>How can I calculate it - for feeding the InverseTransform method.


The w-coordinate is a scaling factor. To translate a 4-vector to a
3-vector, divide x,y and z by w. To translate a 3-vector to a
4-vector, set w to 1.0.

Why have a w-coordinate at all, you may ask? It's there because you
can't specify certain useful transformations (like translation and
perspective projection) with a 3x3 matrix. You can scale and rotate
3-vectors using 3x3 matrices but you can't perform these other
operations. If you move the problem into homogenous coordinates space
(4-vectors) expressing these operations a matrix multiplications
becomes possible.

You can see this by examining the contents of various transform
matrices. Rotation and scaling matrices have their important bits
(the elements that aren't 0 or 1 ) in the 3x3 upper left submatrix.
Translation and perspective matrices will have important bits in the
last row or column (depending whether you've got row or column
vectors.)

Interesting optimizations can be performed if you know what sort of
matrices you're working with. For instance if you limit yourself to
affine transformations (rotation, scale, translation) it's guaranteed
that the last row or column of your concatenated transform matrix will
always be 0,0,0,1. This means you can store it as a 4x3 matrix and
ignore 25% of your matrix multiply. And if you know you're only doing
rotation you can work with 3x3 matrices.


--
"No live organism can continue for long to exist
sanely under conditions of absolute reality; even
larks and katydids are supposed, by some, to dream."

Brian Upton brian...@redstorm.com

** Opinions expressed are my own and not those of Red Storm **


pmel...@stormfront.com

unread,
Mar 12, 1999, 3:00:00 AM3/12/99
to
On Wed, 10 Mar 1999 09:00:00 +0100, Scha...@t-online.de (Tobias
Schachte) wrote:

isn't w just 1/z ?

>Hey, thats the way I've done it. It seems to work well. Thank you for your
>effort.
>
>But I still wonder what the w-value could be...
>

Tobias Schachte

unread,
Mar 12, 1999, 3:00:00 AM3/12/99
to
Thanks for your declaration.
0 new messages