[comp.sys.sgi] World Coordinates -> Screen Coordinates

gleicher@CS.CMU.EDU (Michael Gleicher) (11/06/90)

Does anyone have an easy way to get from worldspace co-ords to screen co-ords?
(basically do a mapw2 in reverse).
I need to do this to use the select routines.

I should be able to grab the matrix off the stack and run my point through it,
although, although I remember reading somewhere that there was an extra trick
to really making it work.

I appreciate the help.

	Mike

kurt@cashew.asd.sgi.com (Kurt Akeley) (11/08/90)

In article <GLEICHER.90Nov6001706@OREO.GRAPHICS.CS.CMU.EDU>,
gleicher@CS.CMU.EDU (Michael Gleicher) writes:
|> 
|> Does anyone have an easy way to get from worldspace co-ords to screen
co-ords?
|> (basically do a mapw2 in reverse).
|> I need to do this to use the select routines.
|> 
|> I should be able to grab the matrix off the stack and run my point
through it,
|> although, although I remember reading somewhere that there was an
extra trick
|> to really making it work.

Here's the complete algorithm:

    1.	Transform your point by the current matrix (if in single matrix mode)
	or by the ModelView matrix followed by the Projection matrix (if in
	multiple matrix mode).  Matrix mode is controlled by the mmode()
	command, which also controls which matrix is returned by getmatrix().
	Set the W component of your point to 1.0 prior to transformation.

    2.	Clip the resulting transformed point against all 6 clipping planes:

		if (x > w OR x < -w OR y > w OR ...)
		then the point does not have a screen coordinate

    3.	Project the transformed point by dividing each of x, y, and z by w.

    4.	Map the point to window coordinates using the equations:

		Xwin = Vsx * X + Vcx
		Ywin = Vsy * Y + Vcy
		Zwin = Vsz * Z + Vcz

		Vsx = (right-left+1)/2
		Vsy = (top-bottom+1)/2
		Vsz = (far-near+1)/2
		Vcx = (right+left)/2
		Vcy = (top+bottom)/2
		Vcz = (far+near)/2

    5.	Add the window offset to Xwin and Ywin to get screen coordinates.
	Z values are not affected by window position.

I've written the Z viewport to be identical to the X and Y viewport operations,
but some SGI machines are off by one on this mapping (no big deal usually).
All computations should be done in floating point.  If screen coordinates are
to be coerced to integers (generally not a good idea) be sure to round toward
nearest.  If truncation is easier, add 0.5 to Vcx, Vcy, and Vcz.

-- kurt