Skip to main content
gamedev.net
🔒 Locked

normalized view -> screen coordinates

Started by StormArmageddon Jul 19, 2003 at 1:10 PM 5 replies 400+ views
Original Post
StormArmageddon
StormArmageddon
After you apply the view and projection transformation to your vertexs, how do you convert the normalized view coordinates to screen coordinates? Is their a special matrix or and equations you can use? Links would be just as useful.
JuNC
JuNC
Post w division, the NDC -> screen transform is a simple scale and translate. There are slightly different formulations but:

Sx = Px * (width/2) + (width/2)
Sy = Py * (height/2) + (height/2)

is the simplest one.
Trienco
Trienco
quote:
Original post by JuNC
Post w division, the NDC -> screen transform is a simple scale and translate. There are slightly different formulations but:

Sx = Px * (width/2) + (width/2)
Sy = Py * (height/2) + (height/2)

is the simplest one.


i think (Px+1) * width/2
would be simpler (even though by just one / )

or maybe (Px+1) * width>>1 (as width is often an even integer)

or even ++PX * width>>1 if you dont need the original values anyway. but i would expect that to be just confusing and not neccessary with any decent compiler anyway. i tend to keep half the width and height around.



[edited by - Trienco on July 20, 2003 5:52:30 AM]
f@dzhttp://festini.device-zero.de
Pipo DeClown
Pipo DeClown
quote:
Original post by Trienco
quote:
Original post by JuNC
Post w division, the NDC -> screen transform is a simple scale and translate. There are slightly different formulations but:

Sx = Px * (width/2) + (width/2)
Sy = Py * (height/2) + (height/2)

is the simplest one.


i think (Px+1) * width/2
would be simpler (even though by just one / )

or maybe (Px+1) * width>>1 (as width is often an even integer)

or even ++PX * width>>1 if you dont need the original values anyway. but i would expect that to be just confusing and not neccessary with any decent compiler anyway. i tend to keep half the width and height around.



[edited by - Trienco on July 20, 2003 5:52:30 AM]


lol.. what an optimist.


.lick
Trienco
Trienco
hrg.. for some reason i keep quoting myself and trying to edit others *lol*

while we're at it:

assuming you have a matrix camera or at least know it's three axes:

void CameraGL::Screen2World(float x, float y, float* vec) {
x= xFac * (x/HalfWidth -1);
y=-yFac * (y/HalfHeight -1);
vec[0]=Forward[0] + x*Right[0] + y*Up[0];
vec[1]=Forward[1] + x*Right[1] + y*Up[1];
vec[2]=Forward[2] + x*Right[2] + y*Up[2];
}

the easiest way i could come up with so far. returns a point in world coords one unit in front of the cam. to get a vector (for picking or whatever) just subtract the camera position.

yFac would be tan(FoV/2) and xFac = yFac * AspectRatio.

void CameraGL::World2Screen(vec* wpos, float* spos) {
wpos[0]-=Position[0]; wpos[1]-=Position[1]; wpos[2]-=Position[2];
float cz = wpos[0]*Forward[0] + wpos[1]*Forward[1] + wpos[2]*Forward[2];
float cx = (wpos[0]*Right[0] + wpos[1]*Right[1] + wpos[2]*Right[2]) / cz;
float cy = (wpos[0]*Up[0] + wpos[1]*Up[1] + wpos[2]*Up[2]) / cz;
spos[0] = ( cx/xFac +1) * HalfWidth;
spos[1] = (-cy/yFac +1) * HalfHeight;
}

and of course the less optimistic might do all this in assembler themselves ,-)
though somehow i hope there are even easier methods to convert between world and screen.

[edited by - Trienco on July 20, 2003 6:16:41 AM]
f@dzhttp://festini.device-zero.de
JuNC
JuNC
You and I obviously have different definitions of the word ''simpler''
Trienco
Trienco
hm.. with simple i usually mean "throw out as much math as possible and reduce the number of ops as far as you can", not necessarily "simple to understand".

but someday i really have to get a book about that stuff.. usually its done by the 3d api and you never care about it. thats why i hope that there are easier (read: faster) methods ,-)
f@dzhttp://festini.device-zero.de

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.