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.
--
Tobias Schachte / SSE
scha...@csi.com
Hed Bar-Nissan schrieb in Nachricht <36E4C7C2...@tiltan.com>...
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?
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
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.
But I still wonder what the w-value could be...
--
Tobias Schachte / SSE
scha...@csi.com
Robert Dunlop [MS MVP] schrieb in Nachricht <7c5b2j$18a$2...@remarQ.com>...
>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 **
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...
>