PDA

View Full Version : CalcScreen



sp0rky
02-08-2005, 07:58 PM
int ScreenTransform( const Vector& point, Vector& screen )
{
// UNDONE: Clean this up some, handle off-screen vertices
float w;
const VMatrix &worldToScreen = WorldToScreenMatrix();

screen.x = worldToScreen[0][0] * point[0] + worldToScreen[0][1] * point[1] + worldToScreen[0][2] * point[2] + worldToScreen[0][3];
screen.y = worldToScreen[1][0] * point[0] + worldToScreen[1][1] * point[1] + worldToScreen[1][2] * point[2] + worldToScreen[1][3];
// z = worldToScreen[2][0] * point[0] + worldToScreen[2][1] * point[1] + worldToScreen[2][2] * point[2] + worldToScreen[2][3];
w = worldToScreen[3][0] * point[0] + worldToScreen[3][1] * point[1] + worldToScreen[3][2] * point[2] + worldToScreen[3][3];

// Just so we have something valid here
screen.z = 0.0f;

bool behind;
if( w < 0.001f )
{
behind = true;
screen.x *= 100000;
screen.y *= 100000;
}
else
{
behind = false;
float invw = 1.0f / w;
screen.x *= invw;
screen.y *= invw;
}

return behind;
}

bool VectorToScreenSpace( Vector pos, int& iX, int& iY )
{
Vector screen;

// Transform to screen space
int iFacing = ScreenTransform( pos, screen );
iX = 0.5 * screen[0] * ScreenWidth();
iY = -0.5 * screen[1] * ScreenHeight();
iX += 0.5 * ScreenWidth();
iY += 0.5 * ScreenHeight();

// Make sure the player's facing it
if ( iFacing )
{
// We're actually facing away from the Target. Stomp the screen position.
iX = -640;
iY = -640;
return false;
}

return true;
}

bool CalcScreen( Vector vOrigin, int *iScreen )
{
return VectorToScreenSpace( vOrigin, iScreen[0], iScreen[1] );
}


Credits: Source SDK

This may not appear elsewhere without permission, but may be linked to.