/*------------------------------------------------------------------------ * FILE: matrix.c * $Author: $ * $Date: $ * $Locker: $ * $Revision: $ * $Source: $ * $State: $ * * Copyright (c) by SEGA Enterprises Ltd. 1992. All rights reserved. * * PURPOSE: * * * DESCRIPTION: * * * INTERFACE * * * CAVEATS * * * AUTHOR(S): * * * MOD HISTORY: * $Log: $ * * *------------------------------------------------------------------------ */ #include "shtypes.h" #include "shmath.h" #include "matrix.h" #include "logger.h" static XYZ matxLightVector; /* light vector for shading (unit vector) */ const Matrix unitMatrix = { { FIXED(1.0), FIXED(0.0), FIXED(0.0), FIXED(0.0) }, { FIXED(0.0), FIXED(1.0), FIXED(0.0), FIXED(0.0) }, { FIXED(0.0), FIXED(0.0), FIXED(1.0), FIXED(0.0) }}; void initMatrixStack ( Matrix *sp[], Matrix **ctm) { LOG( BEGIN, initMatrixStack, ctm ) *ctm = *sp; memcpy((Uint8 *)*ctm, (Uint8 *)&unitMatrix, sizeof(Matrix)); LOG( BEGIN, initMatrixStack, sp ) } /* * clear and make current matrix to unit matrix */ void clearMatrix(Matrix *mat) { LOG( BEGIN, clearMatrix, mat ) memcpy((Uint8 *)mat, (Uint8 *)&unitMatrix, sizeof(Matrix)); LOG( END, clearMatrix, 0 ) } /*------------------------------------------------------------------------ * * NAME : pushmatrix / popmatrix * * PARAMETERS * param1 - * param2 - * * DESCRIPTION * * PRECONDITIONS * * POSTCONDITIONS * * CAVEATS * *------------------------------------------------------------------------ */ void pushmatrix ( Matrix *sp[], Matrix **ctm ) { LOG( BEGIN, pushmatrix, ctm ) (Uint32) *sp += sizeof(Matrix); memcpy((Uint8 *)*sp, (Uint8 *)*ctm, sizeof(Matrix)); *ctm = *sp; LOG( END, pushmatrix, sp ) } void popmatrix ( Matrix *sp[], Matrix **ctm ) { LOG( BEGIN, popmatrix, ctm ) (Uint32) *sp -= sizeof(Matrix); *ctm = *sp; LOG( END, popmatrix, sp ) } /*------------------------------------------------------------------------ * * NAME : rotateX * * PARAMETERS * param1 - * param2 - * * DESCRIPTION * * | M00 M01 M02 M03 | | 1 0 0 0 | * | M10 M11 M12 M13 |.| 0 cos -sin 0 | * | M20 M21 M22 M23 | | 0 sin cos 0 | * | 0 0 0 1 | | 0 0 0 1 | * * | M00 M01*cos+M02*sin -M01*sin+M02*cos M03 | * = | M10 M11*cos+M12*sin -M11*sin+M12*cos M13 | * | M20 M21*cos+M22*sin -M21*sin+M22*cos M23 | * | 0 0 0 1 | * * PRECONDITIONS * * POSTCONDITIONS * * CAVEATS * *------------------------------------------------------------------------ */ void rotateX(Matrix *ctm, Fixed32 sinNum, Fixed32 cosNum) { Fixed32 M01, M02, M11, M12, M21, M22; LOG( BEGIN, rotateY, sinNum ) M01 = (*ctm)[0][1]; M02 = (*ctm)[0][2]; M11 = (*ctm)[1][1]; M12 = (*ctm)[1][2]; M21 = (*ctm)[2][1]; M22 = (*ctm)[2][2]; (*ctm)[0][1] = MUL_FIXED(M01, cosNum) + MUL_FIXED(M02, sinNum); (*ctm)[0][2] = -MUL_FIXED(M01, sinNum) + MUL_FIXED(M02, cosNum); (*ctm)[1][1] = MUL_FIXED(M11, cosNum) + MUL_FIXED(M12, sinNum); (*ctm)[1][2] = -MUL_FIXED(M11, sinNum) + MUL_FIXED(M12, cosNum); (*ctm)[2][1] = MUL_FIXED(M21, cosNum) + MUL_FIXED(M22, sinNum); (*ctm)[2][2] = -MUL_FIXED(M21, sinNum) + MUL_FIXED(M22, cosNum); LOG( END, rotateX, cosNum ) return; } /*------------------------------------------------------------------------ * * NAME : rotateY * * PARAMETERS * param1 - * param2 - * * DESCRIPTION * * | M00 M01 M02 M03 | | cos 0 sin 0 | * | M10 M11 M12 M13 |.| 0 1 0 0 | * | M20 M21 M22 M23 | | -sin 0 cos 0 | * | 0 0 0 1 | | 0 0 0 1 | * * | M00*cos-M02*sin M01 M00*sin+M02*cos M03 | * = | M10*cos-M12*sin M11 M10*sin+M12*cos M13 | * | M20*cos-M22*sin M21 M20*sin+M22*cos M23 | * | 0 0 0 1 | * * PRECONDITIONS * * POSTCONDITIONS * * CAVEATS * *------------------------------------------------------------------------ */ void rotateY(Matrix *ctm, Fixed32 sinNum, Fixed32 cosNum) { Fixed32 M00, M02, M10, M12, M20, M22; LOG( BEGIN, rotateY, sinNum ) M00 = (*ctm)[0][0]; M02 = (*ctm)[0][2]; M10 = (*ctm)[1][0]; M12 = (*ctm)[1][2]; M20 = (*ctm)[2][0]; M22 = (*ctm)[2][2]; (*ctm)[0][0] = MUL_FIXED(M00, cosNum) - MUL_FIXED(M02, sinNum); (*ctm)[0][2] = MUL_FIXED(M00, sinNum) + MUL_FIXED(M02, cosNum); (*ctm)[1][0] = MUL_FIXED(M10, cosNum) - MUL_FIXED(M12, sinNum); (*ctm)[1][2] = MUL_FIXED(M10, sinNum) + MUL_FIXED(M12, cosNum); (*ctm)[2][0] = MUL_FIXED(M20, cosNum) - MUL_FIXED(M22, sinNum); (*ctm)[2][2] = MUL_FIXED(M20, sinNum) + MUL_FIXED(M22, cosNum); LOG( END, rotateY, cosNum ) return; } /*------------------------------------------------------------------------ * * NAME : rotateZ * * PARAMETERS * param1 - * param2 - * * DESCRIPTION * * | M00 M01 M02 M03 | | cos -sin 0 0 | * | M10 M11 M12 M13 |.| sin cos 0 0 | * | M20 M21 M22 M23 | | 0 0 1 0 | * | 0 0 0 1 | | 0 0 0 1 | * * | M00*cos+M01*sin -M00*sin+M01*cos M02 M03 | * = | M10*cos+M11*sin -M10*sin+M11*cos M12 M13 | * | M20*cos+M21*sin -M20*sin+M21*cos M22 M23 | * | 0 0 0 1 | * * PRECONDITIONS * * POSTCONDITIONS * * CAVEATS * *------------------------------------------------------------------------ */ void rotateZ(Matrix *ctm, Fixed32 sinNum, Fixed32 cosNum) { Fixed32 M00, M01, M10, M11, M20, M21; LOG( BEGIN, rotateZ, sinNum ) M00 = (*ctm)[0][0]; M01 = (*ctm)[0][1]; M10 = (*ctm)[1][0]; M11 = (*ctm)[1][1]; M20 = (*ctm)[2][0]; M21 = (*ctm)[2][1]; (*ctm)[0][0] = MUL_FIXED(M00, cosNum) + MUL_FIXED(M01, sinNum); (*ctm)[0][1] = -MUL_FIXED(M00, sinNum) + MUL_FIXED(M01, cosNum); (*ctm)[1][0] = MUL_FIXED(M10, cosNum) + MUL_FIXED(M11, sinNum); (*ctm)[1][1] = -MUL_FIXED(M10, sinNum) + MUL_FIXED(M11, cosNum); (*ctm)[2][0] = MUL_FIXED(M20, cosNum) + MUL_FIXED(M21, sinNum); (*ctm)[2][1] = -MUL_FIXED(M20, sinNum) + MUL_FIXED(M21, cosNum); LOG( END, rotateZ, cosNum ) return; } /*------------------------------------------------------------------------ * * NAME : scale * * PARAMETERS * param1 - * param2 - * * DESCRIPTION * * | M00 M01 M02 M03 | | sx 0 0 0 | * | M10 M11 M12 M13 |.| 0 sy 0 0 | * | M20 M21 M22 M23 | | 0 0 1 0 | * | 0 0 0 1 | | 0 0 0 1 | * * | M00*sx M01 M02 M03 | * = | M10 M11*sy M12 M13 | * | M20 M21 M22 M23 | * | 0 0 0 1 | * * * PRECONDITIONS * * POSTCONDITIONS * * CAVEATS * *------------------------------------------------------------------------ */ void scale(Matrix *ctm, Fixed32 dx, Fixed32 dy) { Fixed32 M00, M11; LOG( BEGIN, scale, dx ) M00 = (*ctm)[0][0]; M11 = (*ctm)[1][1]; (*ctm)[0][0] = MUL_FIXED(M00, dx); (*ctm)[1][1] = MUL_FIXED(M11, dy); LOG( END, scale, dy ) } /*------------------------------------------------------------------------ * * NAME : translate * * PARAMETERS * param1 - * param2 - * * DESCRIPTION * * | M00 M01 M02 M03 | | 1 0 0 x | | M00 M01 M02 M00*x+M01*y+M02*z+M03 | * | M10 M11 M12 M13 |.| 0 1 0 y | = | M10 M11 M12 M10*x+M11*y+M12*z+M13 | * | M20 M21 M22 M23 | | 0 0 1 z | | M20 M21 M22 M20*x+M21*y+M22*z+M23 | * | 0 0 0 1 | | 0 0 0 1 | | 0 0 0 1 | * * PRECONDITIONS * * POSTCONDITIONS * * CAVEATS * *------------------------------------------------------------------------ */ void translate(Matrix *ctm, Fixed32 x, Fixed32 y, Fixed32 z) { Fixed32 *pt; LOG( BEGIN, translate, x ) pt = (Fixed32 *)ctm; (*ctm)[0][3] += MUL_FIXED(*pt++, x); (*ctm)[0][3] += MUL_FIXED(*pt++, y); (*ctm)[0][3] += MUL_FIXED(*pt++, z); pt++; (*ctm)[1][3] += MUL_FIXED(*pt++, x); (*ctm)[1][3] += MUL_FIXED(*pt++, y); (*ctm)[1][3] += MUL_FIXED(*pt++, z); pt++; (*ctm)[2][3] += MUL_FIXED(*pt++, x); (*ctm)[2][3] += MUL_FIXED(*pt++, y); (*ctm)[2][3] += MUL_FIXED(*pt++, z); LOG( END, translate, y ) return; } #if 0 /*------------------------------------------------------------------------ * * NAME : coordTrans * * PARAMETERS * param1 - The source coordinate to be transformed * param2 - The result of coordinate transformation * * DESCRIPTION * * | Xn | | M00, M01, M02, M03 | | Xa | | M00Xa+M01Ya+M02Za+M03 | * | Yn | = | M10, M11, M12, M13 |.| Ya | = | M10Xa+M11Ya+M12Za+M13 | * | Zn | | M20, M21, M22, M23 | | Za | | M20Xa+M21Ya+M22Za+M23 | * | 1 | | 0, 0, 0, 1 | | 1 | | 1 | * * PRECONDITIONS * * POSTCONDITIONS * * CAVEATS * *------------------------------------------------------------------------ */ void coordTrans(Matrix *ctm, XYZ *src, XYZ *ans) { Fixed32 *pts; Fixed32 *ptMatrix; Fixed32 *ptn; LOG( BEGIN, coordTrans, src ) ptn = (Fixed32 *)ans; pts = (Fixed32 *)src; ptMatrix = (Fixed32 *)(*ctm)[0]; *ptn = MUL_FIXED(*pts++, *ptMatrix++); *ptn += MUL_FIXED(*pts++, *ptMatrix++); *ptn += MUL_FIXED(*pts++, *ptMatrix++); *ptn++ += *ptMatrix; pts = (Fixed32 *)src; ptMatrix = (Fixed32 *)(*ctm)[1]; *ptn = MUL_FIXED(*pts++, *ptMatrix++); *ptn += MUL_FIXED(*pts++, *ptMatrix++); *ptn += MUL_FIXED(*pts++, *ptMatrix++); *ptn++ += *ptMatrix; pts = (Fixed32 *)src; ptMatrix = (Fixed32 *)(*ctm)[2]; *ptn = MUL_FIXED(*pts++, *ptMatrix++); *ptn += MUL_FIXED(*pts++, *ptMatrix++); *ptn += MUL_FIXED(*pts++, *ptMatrix++); *ptn += *ptMatrix; LOG( END, coordTrans, ans ) return; } /*------------------------------------------------------------------------ * * NAME : normalTrans * * PARAMETERS * param1 - The source normal vector to be transformed * param2 - The result of normal vector transformation * * DESCRIPTION * * | Xn | | M00, M01, M02 | | Xa | | M00Xa+M01Ya+M02Za | * | Yn | = | M10, M11, M12 |.| Ya | = | M10Xa+M11Ya+M12Za | * | Zn | | M20, M21, M22 | | Za | | M20Xa+M21Ya+M22Za | * * PRECONDITIONS * * POSTCONDITIONS * * CAVEATS * *------------------------------------------------------------------------ */ void normalTrans(Matrix *ctm, XYZ *src, XYZ *ans) { Fixed32 *pts; Fixed32 *ptMatrix; Fixed32 *ptn; ptn = (Fixed32 *)ans; pts = (Fixed32 *)src; ptMatrix = (Fixed32 *)(*ctm)[0]; *ptn = MUL_FIXED(*pts++, *ptMatrix++); *ptn += MUL_FIXED(*pts++, *ptMatrix++); *ptn++ += MUL_FIXED(*pts++, *ptMatrix++); pts = (Fixed32 *)src; ptMatrix = (Fixed32 *)(*ctm)[1]; *ptn = MUL_FIXED(*pts++, *ptMatrix++); *ptn += MUL_FIXED(*pts++, *ptMatrix++); *ptn++ += MUL_FIXED(*pts++, *ptMatrix++); pts = (Fixed32 *)src; ptMatrix = (Fixed32 *)(*ctm)[2]; *ptn = MUL_FIXED(*pts++, *ptMatrix++); *ptn += MUL_FIXED(*pts++, *ptMatrix++); *ptn++ += MUL_FIXED(*pts++, *ptMatrix++); return; } #endif