Solved

Calculate Screen Coordinates (Determine Screen Coordinates from World Coordinates of an object)

Posted on 2004-07-31
3
1,676 Views
Last Modified: 2013-12-26
The Problem is: I can't fully understand this function wich calculates the screen coordinates of an given object in world coordinates:

inline void Calculate_Screen_Coordinates(float* pScreenX, float* pScreenY, D3DXVECTOR3* pVec)
{
    tempX = pVec->x;
    tempY = pVec->y;
    tempZ = pVec->z;

    tempX2 = ViewProjectionMatrix._11*tempX + ViewProjectionMatrix._21*tempY + ViewProjectionMatrix._31*tempZ + ViewProjectionMatrix._41;
    tempY2 = ViewProjectionMatrix._12*tempX + ViewProjectionMatrix._22*tempY + ViewProjectionMatrix._32*tempZ + ViewProjectionMatrix._42;
    tempW2 = ViewProjectionMatrix._14*tempX + ViewProjectionMatrix._24*tempY + ViewProjectionMatrix._34*tempZ + ViewProjectionMatrix._44;

// Here comes part wich i can't understand fully:
     tempInvW2 = 1.0f/tempW2;

    *pScreenX = (1.0f + (tempX2*tempInvW2))*0.5f*screenwidth;
    *pScreenY = (1.0f - (tempY2*tempInvW2))*0.5f*screenheight;  
}

In detail:
=============================================================
// Next line i can't understand ->
tempInvW2 = 1.0f/tempW2;
i only know that this is a faktor for the z coord because the screen has only x/y but i don't know
why 1.0f and why the variable name is tempInv like Invers ?

This line is clearly:
*pScreenX = (1.0f + (tempX2*tempInvW2))*0.5f*screenwidth;

This line seems me wrong:
*pScreenY = (1.0f - (tempY2*tempInvW2))*0.5f*screenheight;
==> i would say:
*pScreenY = (1.0f + (tempY2*tempInvW
because the viewport coordinates are from -1/-1 to 1/1 and the screen coords are beginning in the  left top corner at 0/0 therefore transform -1/-1 & 1/1 to 0/0 & 2/2 ?!

Can anybody explain this in detail ?
0
Comment
Question by:WolfgangKoenig
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 17

Accepted Solution

by:
davebytes earned 125 total points
ID: 11743364
A similar question/explanation at:
http://www.flipcode.com/cgi-bin/msg.cgi?showThread=18June2001-PlottingA3DPointOnA2DScreen&forum=askmid&id=-1

W is the scaling factor for the viewport.  Dividing by it will get you into screen-relative space.  1.0/W is pre-calculating the inverse so that a multiply can be used -- it is only used twice, but still faster to calculate the inverseW once and multiply against it, rather than dividing twice (yes, division is THAT BAD).

The - is because the 0,0 in 3D screen coords is usually the BOTTOM left, need to flip Y to get it to the TOP left.

As you say, generated view coordinates will be in the range of [-1,1] so for x:
1 + [-1,1] => [0,2] * 0.5 => [0,1]
and for y:
1 - [-1,1] => [2,0] * 0.5 => [1,0]
thus flipping the Y axis on the screen properly.

Multiplying by height/width then gets into screen pixels.

d
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question