Camera (XSISDK)

Mapping a Position to Screen Space

This script demonstrates the transformations necessary to map from an point to the associated screen position.

It shows how to get the necessary Camera transformation information.

'
' ObjToScreenSpace.vbs -- by Glen Fraser, November 2002
'
' This script computes the "screen space" coordinates
' of an object.  It assumes there is a camera named "Camera"
' and finds the screen-space normalized coordinates
' of the first selected object.  If you want pixel coords
' rather than -1 to 1 normalized coords, just add 1, divide
' by 2, and multiply by the X/Y pixel resolution.
'
' NOTE: Currently this script does not take into account
' "shift-Z" zooming of the camera (sub-frustum zooming).
' It does correctly handle projection plane mode on the
' camera, though.

option explicit

dim oOrigPoint, oFinalPoint
set oOrigPoint = XSIMath.CreateVector3
set oFinalPoint = XSIMath.CreateVector3
selection(0).kinematics.global.transform.GetTranslation oOrigPoint

dim oCamObj
set oCamObj = GetValue( "Camera" )	' Change this if you like!
ObjToScreenSpace oOrigPoint, oCamObj, oFinalPoint
logmessage "Screen Position: ( " & oFinalPoint.x & ", " & oFinalPoint.y & " )"


' =============================================
' Finds the normalized screen space coordinates
' of a point for a given camera.
' These range from -1 to 1 in X and Y.
' - Glen Fraser (Nov 2002)
' =============================================
sub ObjToScreenSpace( in_pt, in_oCam, out_pt )
	dim oCamProj
	set oCamProj = XSIMath.CreateMatrix4
	BuildPerspMatrix oCamProj, in_oCam

	dim oCamTrans
	set oCamTrans = XSIMath.CreateMatrix4
	in_oCam.Kinematics.Global.Transform.GetMatrix4 oCamTrans
	oCamTrans.InvertInPlace
	out_pt.MulByMatrix4 in_pt, oCamTrans
	out_pt.MulByMatrix4InPlace oCamProj
end sub

' =============================================
' Builds the perspective transformation matrix
' from a camera.
' - Glen Fraser (Nov 2002)
' =============================================
sub BuildPerspMatrix( io_mat, in_oCam )
	'
	' Build the projection matrix
	'
	if in_oCam.projplane.value then
		MatrixFromPPlane io_mat, in_oCam
	else
		MatrixFromSimpleFOV io_mat, in_oCam
	end if
end sub


' =============================================
' Computes the camera projection matrix when
' only the FOV parameter is used (the "normal" case).
' - Glen Fraser (Nov 2002)
' =============================================
sub MatrixFromSimpleFOV( io_mat, in_oCam )
	dim fovTan, aspect, near, far, width, height
	
	fovTan = tan( XSIMath.DegreesToRadians( in_oCam.fov.value ) * 0.5 )
	aspect = in_oCam.aspect.value
	near = in_oCam.near.value
	far = in_oCam.far.value
	if in_oCam.fovtype.value = 1 then
		width = fovTan
		height = fovTan / aspect
	else
		width = aspect * fovTan
		height = fovTan
	end if
	io_mat.Set 1 / width, 0, 0, 0, _
				0, 1 / height, 0, 0, _
				0, 0, -(far+near)/(far-near), -1, _
				0, 0, -2*near*far/(far-near), 0
end sub

' =============================================
' Computes the camera projection matrix when
' the "projection plane" parameters are used.
' - Glen Fraser (Nov 2002)
' =============================================
sub MatrixFromPPlane( io_mat, in_oCam )
	dim cx, cy, focal, near, far, width, height

	near = in_oCam.near.value
	far = in_oCam.far.value
	focal = in_oCam.projplanedist.value / 25.4 'convert to inches
	width = in_oCam.projplanewidth.value * near / focal
	height = in_oCam.projplaneheight.value * near / focal
	cx = in_oCam.projplaneoffx.value * near / focal
	cy = in_oCam.projplaneoffy.value * near / focal
	
	io_mat.Set 2 * near / width, 0, 0, 0, _
				0, 2 * near / height, 0, 0, _
				2 * cx / width, 2 * cy / height, -(far+near)/(far-near), -1, _
				0, 0, -2*near*far/(far-near), 0
end sub


This page was last modified 10:51, 1 Sep 2005.
This page has been accessed 4621 times.

© Copyright 2009 Autodesk Inc. All Rights Reserved. Privacy Policy | Legal Notices and Trademarks | Report Piracy