📄 maths.c
字号:
/**************************************************************************
* Name : maths.c
* Author : BCB
* Created : 06/05/2003
*
* Copyright : 2003 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.
*
* Platform : ANSI
*
* $Date: 2003/11/10 17:22:01 $ $Revision: 1.1 $
* $Log: maths.c $
**************************************************************************/
#define MODULE_ID MODID_MATHS
#include "context.h"
/***********************************************************************************
Function Name : Normalize
Inputs : afVin
Outputs : afVout
Returns : Square of length of input vector.
Description : Normalises a vector and returns dot product of input vector
with itself.
************************************************************************************/
IMG_FLOAT Normalize(IMG_FLOAT afVout[3], const IMG_FLOAT afVin[3])
{
IMG_FLOAT fLen;
fLen = afVin[0]*afVin[0] + afVin[1]*afVin[1] + afVin[2]*afVin[2];
if (fLen <= GLES_Zero)
{
afVout[0] = GLES_Zero;
afVout[1] = GLES_Zero;
afVout[2] = GLES_Zero;
return fLen;
}
if (fLen == 1.0F)
{
afVout[0] = afVin[0];
afVout[1] = afVin[1];
afVout[2] = afVin[2];
return fLen;
}
else
{
/*
* This code could calculates a reciprocal square root accurate to well over
* 16 bits using Newton-Raphson approximation.
*/
fLen = ((IMG_FLOAT) 1.0) / GLES_SQRTF(fLen);
afVout[0] = afVin[0] * fLen;
afVout[1] = afVin[1] * fLen;
afVout[2] = afVin[2] * fLen;
return fLen;
}
}
/***********************************************************************************
Function Name : FloorLog2
Inputs : ui32Val
Outputs :
Returns : Floor(Log2(ui32Val))
Description : Computes the floor of the log base 2 of a unsigned integer -
used mostly for computing log2(2^ui32Val).
************************************************************************************/
IMG_UINT32 FloorLog2(IMG_UINT32 ui32Val)
{
IMG_UINT32 ui32Ret = 1;
while ((ui32Val >> ui32Ret) > 0)
ui32Ret++;
return ui32Ret-1;
}
/***********************************************************************************
Function Name : Floor
Inputs : fVal
Outputs :
Returns : Floor(fVal)
Description : Computes the floor of a float value
************************************************************************************/
IMG_FLOAT Floor(IMG_FLOAT fVal)
{
IMG_FLOAT fRet;
fRet = (IMG_FLOAT)((IMG_INT32)fVal);
if (fVal < 0.0f)
{
fRet -= 1.0f;
}
return fRet;
}
/***********************************************************************************
Function Name : Vvec
Inputs : psSrc1, psSrc2
Outputs : psResult
Returns : -
Description : Computes a light to vertex vector
************************************************************************************/
IMG_VOID Vvec(GLEScoord *psResult, const GLEScoord *psSrc1, const GLEScoord *psSrc2)
{
const IMG_INT32 i = *(const IMG_INT32 *)&psSrc1->fW;
const IMG_INT32 j = *(const IMG_INT32 *)&psSrc2->fW;
/*
* Test for +/-0.0
*/
if (i << 1)
{
if (j << 1)
{
/*
* Cross multiply instead of dividing, since
* result always gets normalized.
*/
IMG_FLOAT fW1, fW2;
fW1 = psSrc1->fW;
fW2 = psSrc2->fW;
psResult->fX = psSrc2->fX * fW1 - psSrc1->fX * fW2;
psResult->fY = psSrc2->fY * fW1 - psSrc1->fY * fW2;
psResult->fZ = psSrc2->fZ * fW1 - psSrc1->fZ * fW2;
}
else
{
/*
* w2 == 0
*/
psResult->fX = psSrc2->fX;
psResult->fY = psSrc2->fY;
psResult->fZ = psSrc2->fZ;
}
}
else if (j << 1)
{
/*
* w1 == 0
*/
psResult->fX = -psSrc1->fX;
psResult->fY = -psSrc1->fY;
psResult->fZ = -psSrc1->fZ;
}
else
{
/*
* w1 == 0 & w2 == 0
*/
psResult->fX = psSrc2->fX - psSrc1->fX;
psResult->fY = psSrc2->fY - psSrc1->fY;
psResult->fZ = psSrc2->fZ - psSrc1->fZ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -