⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ntvmath.h

📁 Lido PXA270平台开发板的最新BSP,包括源代码
💻 H
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************
<module>
* Name         : NTVMath.h
* Title        : Native maths functions
* Author(s)    : Imagination Technologies
* Created      : 2 March 2004
*
* Copyright    : 2004 by Imagination Technologies Limited.
*                All rights reserved.  No part of this software, either
*                material or conceptual may be copied or distributed,
*                transmitted, transcribed, stored in a retrieval system
*                or translated into any human or computer language in any
*                form by any means, electronic, mechanical, manual or
*                other-wise, or disclosed to third parties without the
*                express written permission of Imagination Technologies
*                Limited, Unit 8, HomePark Industrial Estate,
*                King's Langley, Hertfordshire, WD4 8LZ, U.K.
*
* Description  : Math functions and definitions for Native type calculations
*
* Platform     : Windows CE
*
</module>

 $Log: ntvmath.h $
*********************************************************************************/
#if !defined(_NTVMATH_H_)
#define _NTVMATH_H_
#include "math.h"

/*****************************************************************************
 Format dependant definitions
*****************************************************************************/
#if defined (D3DM_NATIVE_FLOAT)

	/**************************************************
		Simple Math Functions
	***************************************************/
	#define Mul(x,y)		((x)*(y))
	#define Div(x,y)		((x)/(y))
	#define DivPR_Y(x,y)	((x)/(y))
	#define	MulPR_Y(x,y)	((x)*(y))
	#define	MulPR_X(x,y)	((x)*(y))
	#define	MulPR_XY(x,y)	((x)*(y))
	#define MulPR(x,y)		((x)*(y))
	#define Sub(x,y)		((x)-(y))
	#define Add(x,y)		((x)+(y))
	#define Add3(x,y,z)		((x)+(y)+(z))
	#define Add4(w,x,y,z)	((w)+(x)+(y)+(z))
	#define Sqrt			sqrt
	#define Pow				pow
	#define LPow			pow
	#define PowPR_A			pow
	#define CLAMP(x)		(x)
	#define	PR_SHIFT(x)		(x)
	#define	PR_UNSHIFT(x)	(x)

#else

	#pragma warning(disable : 4244)
	/**************************************************
		Simple Math Functions
	***************************************************/
	#define Mul(x,y)	((__int64) (((__int64)x * (__int64)y) >> SHIFT))
	#define Div(x,y)	((__int64) ((((__int64) x) << SHIFT) / ((__int64) y)))

	/* 
		Fixed point maths with 12:20 precision 

		*PR		= convert both components to high precision
		*PR_XY	= both components already high precision
		*PR_X	= convert second component to high precision
		*PR_Y	= convert first component to high precision
	*/
	#define DivPR_Y(x,y)	((__int64) (((((__int64) x) << SHIFT_PR2NORM) << SHIFT_PR) / ((__int64) y)))
	#define	MulPR_Y(x,y)	((__int64) (((((__int64) x) << SHIFT_PR2NORM)  * ((__int64) y)) >> SHIFT_PR))
	#define	MulPR_X(x,y)	((__int64) ((((__int64) x)  * (((__int64) y) << SHIFT_PR2NORM)) >> SHIFT_PR))
	#define	MulPR_XY(x,y)	((__int64) ((((__int64) x)  * ((__int64) y)) >> SHIFT_PR))
	#define MulPR(x,y)		((__int64) (((((__int64) x) << SHIFT_PR2NORM)  * (((__int64) y) << SHIFT_PR2NORM) ) >> SHIFT_PR))

	#define Sub(x,y)		((__int64)(((__int64)(x)) - ((__int64)(y))))
	#define Add(x,y)		((__int64)(((__int64)(x)) + ((__int64)(y))))

	#define Add3(x,y,z)		Add(x,Add(y,z))
	#define Add4(w,x,y,z)	Add(Add(w,x),Add(y,z))

	#define CLAMP(x)	(((__int64)(x)) > MAX_FIXED_AS_I64) ? MAX_FIXED : \
						(((__int64)(x)) < MIN_FIXED_AS_I64) ? MIN_FIXED	: \
						(x)

	/* Precision Shift 16.16 to 12.20 fixed point */
	#define	PR_SHIFT(x)		(x << SHIFT_PR2NORM)

	/* Precision Unshift 12.20 to 16.16 fixed point */
	#define	PR_UNSHIFT(x)	(x >> SHIFT_PR2NORM)

	//FIXME: TODO Find a proper solution for these
	#define Sqrt(a)		(FL2FX(sqrt(LFX2FL(a))))
	#define Pow(a,b)	(FL2FX(pow(FX2FL(a),FX2FL(b))))
	#define LPow(a,b)	(FL2LFX(pow(FX2FL(a),FX2FL(b))))

	/* Power function takes a high precision A value */
	#define PowPR_A(a,b) (FL2FX(pow(FX2FL_PR(a),FX2FL(b))))

#endif
/********************************************************************************
<function>
	Function	: CLAMPTO
	Parameters	: ntvVal   - Value to clamp
				  ntvLower - Lower val to clamp to
				  ntvUpper - Upper val to clamp to
				
	Description : Clamps a value between given upper and lower values
	Returns		: Multiplied vector
<function/>
*********************************************************************************/
__inline NTV_TYPE CLAMPTO(NTV_TYPE ntvVal, NTV_TYPE ntvLower, NTV_TYPE ntvUpper)
{
	return ((ntvVal > D3DM_One) ? ntvUpper : ((ntvVal < ntvLower) ? ntvLower : ntvVal));
}
/********************************************************************************
<function>
	Function	: VectorAdd
	Parameters	:	In:  psA - Vector to be added to
					In:  psB - Vector to add
				
	Description : Multiplies input vector by input scalar value
	Returns		: Multiplied vector
<function/>
*********************************************************************************/
__inline void VectorAdd(PVR_VECTOR3 *psA, PVR_VECTOR3 *psB, PVR_VECTOR3 *psOut)
{
	psOut->x = Add(psA->x,psB->x);
	psOut->y = Add(psA->y,psB->y);
	psOut->z = Add(psA->z,psB->z);
}
/********************************************************************************
<function>
	Function	: VectorNegate
	Parameters	:	In:  psA - Vector negated
					In:  psOut
				
	Description : switches direction of vector
	Returns		: 
<function/>
*********************************************************************************/
__inline void VectorNegate(PVR_VECTOR3 *psA, PVR_VECTOR3 *psOut)
{
	psOut->x = -psA->x;
	psOut->y = -psA->y;
	psOut->z = -psA->z;
}


/********************************************************************************
<function>
	Function	: ColorAdd
	Parameters	:	In:  psA - First input vector
					In:  psB - Second input vector
	Description : multiples the first and second input color components
				  and copies result into the third color
	Returns		: none
<function/>
*********************************************************************************/
__inline void ColorAdd(PVR_COLORVALUE *psA, PVR_COLORVALUE *psB, PVR_COLORVALUE *psOut)
{
	psOut->r = Add(psA->r, psB->r);
	psOut->g = Add(psA->g, psB->g);
	psOut->b = Add(psA->b, psB->b);
}
/********************************************************************************
<function>
	Function	: ColorAddClamp
	Parameters	:	In:  psA - First input vector
					In:  psB - Second input vector
	Description : Clamps to zero then adds the first and second input color components
				  and copies result into the third color clamping to one
	Returns		: none
<function/>
*********************************************************************************/
__inline void ColorAddClamp(PVR_COLORVALUE *psA, PVR_COLORVALUE *psB, PVR_COLORVALUE *psOut)
{
	psOut->r = NTV_MIN(Add(NTV_MAX(D3DM_Zero,psA->r), psB->r), D3DM_One);
	psOut->g = NTV_MIN(Add(NTV_MAX(D3DM_Zero,psA->g), psB->g), D3DM_One);
	psOut->b = NTV_MIN(Add(NTV_MAX(D3DM_Zero,psA->b), psB->b), D3DM_One);
}
/********************************************************************************
<function>
	Function	: ScalarColorMultiply
	Parameters	:	In:  psA - Vector to multiply
					In:  ntvScalar - Scalar by which to multiply
	Description : Multiplies input vector by input scalar value
	Returns		: Multiplied vector
<function/>
*********************************************************************************/
__inline void ScalarColorMultiply(PVR_COLORVALUE *psA, NTV_TYPE ntvScalar, PVR_COLORVALUE *psOut)
{
	psOut->r = Mul(psA->r, ntvScalar);
	psOut->g = Mul(psA->g, ntvScalar);
	psOut->b = Mul(psA->b, ntvScalar);
}


/********************************************************************************
<function>
	Function	: ScalarMultiply
	Parameters	:	In:  psA - Vector to multiply
					In:  ntvScalar - Scalar by which to multiply
	Description : Multiplies input vector by input scalar value
<function/>
*********************************************************************************/
__inline void ScalarMultiply(PVR_VECTOR3 *psA, NTV_TYPE ntvScalar, PVR_VECTOR3 *psOut)
{
	psOut->x = Mul(psA->x, ntvScalar);
	psOut->y = Mul(psA->y, ntvScalar);
	psOut->z = Mul(psA->z, ntvScalar);
}
/********************************************************************************
<function>
	Function	: MultiplyVector
	Parameters	:	In:  psA - 1st Vector to multiply
					In:  psB - 2nd Vector to multiply
					In:  psOut - Output Vector
	Description : Component-wise mutiplication of vector
<function/>
*********************************************************************************/
__inline void MultiplyVector(PVR_VECTOR3* psA, PVR_VECTOR3* psB, PVR_VECTOR3* psOut)
{
	psOut->x = Mul(psA->x, psB->x);
	psOut->y = Mul(psA->y, psB->y);
	psOut->z = Mul(psA->z, psB->z);
}
/********************************************************************************
<function>
	Function	: MagSquared
	Parameters	:	In:  psA - Vector whose squared magnitude is required
	Description : Finds the Squared Magnitude of the input vector
	Returns		: NTV_TYPE - Squared Magnitude of input vector
<function/>
*********************************************************************************/
#if defined (D3DM_NATIVE_FLOAT)
__inline NTV_TYPE MagSquared(PVR_VECTOR3 *psA)
#else
__inline __int64 MagSquared(PVR_VECTOR3 *psA)
#endif
{
	return Add3( Mul(psA->x, psA->x),
				 Mul(psA->y, psA->y),
				 Mul(psA->z, psA->z));
}
/********************************************************************************
<function>
	Function	: Magnitude
	Parameters	:	In:  psA - Vector whose magnitude is required
	Description : Finds the Magnitude of the input vector
	Returns		: NTV_TYPE - Magnitude of input vector
<function/>
*********************************************************************************/
__inline NTV_TYPE Magnitude(PVR_VECTOR3 *psA)
{
	if(psA->x == D3DM_Zero &&
	   psA->y == D3DM_Zero &&
	   psA->z == D3DM_Zero)
	{
		return D3DM_Zero;
	}
	else return Sqrt(MagSquared(psA));
}
/********************************************************************************
<function>
	Function	: DistanceVector
	Parameters	:	In:  psA - First input vector
					In:  psB - Second input vector
	Description : Subtracts the Second input vector from the First
	Returns		: PVR_VECTOR3 - resultant vector
<function/>
*********************************************************************************/
__inline void DistanceVector(PVR_VECTOR3 *psA, PVR_VECTOR3 *psB, PVR_VECTOR3 *psOut)
{
	psOut->x = Sub(psA->x,psB->x);
	psOut->y = Sub(psA->y,psB->y);
	psOut->z = Sub(psA->z,psB->z);
}

/********************************************************************************
<function>
	Function	: ColorMultiply
	Parameters	:	In:  psA - First input vector
					In:  psB - Second input vector
	Description : multiples the first and second input color components
				  and copies result into the third color
	Returns		: none
<function/>
*********************************************************************************/
__inline void ColorMultiply(PVR_COLORVALUE *psA, PVR_COLORVALUE *psB, PVR_COLORVALUE *psOut)
{
	psOut->r = Mul(psA->r, psB->r);
	psOut->g = Mul(psA->g, psB->g);
	psOut->b = Mul(psA->b, psB->b);
}

/********************************************************************************
<function>
	Function	: Distance
	Parameters	:	In:  psA - First input vector
					In:  psB - Second input vector
	Description : Returns the magnitude of the distance vector of the 2 input vectors
	Returns		:
<function/>
*********************************************************************************/
__inline NTV_TYPE Distance(PVR_VECTOR3 *psA, PVR_VECTOR3 *psB)
{
	PVR_VECTOR3 sVector;
	DistanceVector(psB, psA, &sVector);
	return Magnitude(&sVector);
}
/********************************************************************************
<function>
	Function	: Normalize
	Parameters	: In:  psA - Input vector
	Description : Normalises the input vector
	Returns		:
<function/>
*********************************************************************************/
__inline void Normalize(PVR_VECTOR3 *psA)
{
	NTV_TYPE ntvInvMag;
	NTV_TYPE ntvMag		= Magnitude(psA);

	/* Div zero protection */
	if(ntvMag == D3DM_Zero) return;

	ntvInvMag  = Div(D3DM_One, ntvMag);
	ScalarMultiply(psA, ntvInvMag, psA);
}
/********************************************************************************
<function>
	Function	: Normalized
	Parameters	: In:  psA - Input vector
				  In:  psOut - Output vector
	Description : Creates a normalized version of the input vector
	Returns		:
<function/>
*********************************************************************************/
__inline void Normalized(PVR_VECTOR3 *psA, PVR_VECTOR3 *psOut)
{
	*psOut = *psA;
	Normalize(psOut);
}
/********************************************************************************
<function>
	Function	: DotProduct
	Parameters	:	In:  psA - First input vector
					In:  psB - Second input vector
	Description : Finds the dot product of the input vectors

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -