Beispiel #1
0
Vector3 Renderer::Unproject(const Vector3& screenPoint) const
{
	// Convert screenPoint to device coordinates (between -1 and +1)
	Vector3 deviceCoord = screenPoint;
	deviceCoord.x /= (mWidth) * 0.5f;
	deviceCoord.y /= (mHeight) * 0.5f;

	// First, undo the projection
	Matrix4 unprojection = mProj;
	unprojection.Invert();
	Vector3 unprojVec = TransformWithPerspDiv(deviceCoord, unprojection);

	// Now undo the view matrix
	Matrix4 uncamera = mView;
	uncamera.Invert();
	return Transform(unprojVec, uncamera);
}
Beispiel #2
0
void Camera::GetUpAxisTransform(Matrix4& m) const
{
    m = Matrix4::Identity;

    m.x = SideVector;
    m.y = UpVector;
    m.z = OutVector;

    m.Invert();
}
/// <summary>
/// Sets up a quaternion & position from vector camera components
/// and oriented the camera up
/// </summary>
/// <param name="eye">The camera position</param>
/// <param name="lookAt">The camera's look-at point</param>
/// <param name="up"></param>
void ArcBallCameraController::SetCamera(Vector3F position, Vector3F target, Vector3F up)
{
	m_bRecomputeViewMatrix = true;

	//Create a look at matrix, to simplify matters a bit
	Matrix4 temp;
	temp.LookAt(position, target, up);

	//invert the matrix, since we're determining the
	//orientation from the rotation matrix in RH coords
	temp.Invert();

	//set the position
	m_Target = target;

	//set distance
	m_fDistance = (target - position).Length();

	//create the new aspect from the look-at matrix
	m_ArcBallOrientation.FromMatrix(temp);

	//When setting a new eye-view direction 
	//in one of the gamble-locked modes, the yaw and
	//pitch gimble must be calculated.

	//first, get the direction projected on the x/z plne
	Vector3F dir = Direction();
	dir.y = 0.0f;
	if (dir.Length() == 0.0f)
	{
		dir = Vector3F::Forward();
	}
	dir.Normalize();

	//find the yaw of the direction on the x/z plane
	//and use the sign of the x-component since we have 360 degrees
	//of freedom
	m_fArcBallYaw = (acosf(-dir.z) * Sign(dir.x));

	//Get the pitch from the angle formed by the Up vector and the 
	//the forward direction, then subtracting Pi / 2, since 
	//we pitch is zero at Forward, not Up.
	m_fArcBallPitch = -(acosf(Vector3F::Dot(Vector3F::Up(), Direction())) - MATH_PI_DIV_2);
}
bool Screenport::ViewToWorld( const grinliz::Vector2F& v, const grinliz::Matrix4* _mvpi, grinliz::Ray* ray ) const
{
	Matrix4 mvpi;
	if ( _mvpi ) {
		mvpi = *_mvpi;
	}
	else {
		Matrix4 mvp;
		ViewProjection3D( &mvp );
		mvp.Invert( &mvpi );
	}

	// View normalized:
	Vector4F in = { 2.0f * v.x / screenWidth - 1.0f,
					2.0f * v.y / screenHeight - 1.0f,
					0.f, //v.z*2.0f-1.f,
					1.0f };

	Vector4F out0, out1;
	MultMatrix4( mvpi, in, &out0 );
	in.z = 1.0f;
	MultMatrix4( mvpi, in, &out1 );

	if ( out0.w == 0.0f ) {
		return false;
	}
	ray->origin.x = out0.x / out0.w;
	ray->origin.y = out0.y / out0.w;
	ray->origin.z = out0.z / out0.w;

	ray->direction.x = out1.x / out1.w - ray->origin.x;
	ray->direction.y = out1.y / out1.w - ray->origin.y;
	ray->direction.z = out1.z / out1.w - ray->origin.z;

	return true;
}