// FILE: transfm.c #include #include #include "3d\compatib.h" #include "3d\gdi3d.h" #include "3d\intern.h" #include "3d\globals.h" #include "3d\helpers.h" /* PROJECT: GDI3D DLL PROGRAMMER(S): Pasi Paavola COMPILER: Microsoft C/C++ 7.0 ENVIRONMENT: Windows 3.1 running on 386/33/SuperVGA/no 80x87; 386 enhanced mode ROUTINES: Routines related to 3D transformations */ //////////////////////////////////////////////////////////////////////////// /* G3RemoveTransformations() restores the transformation system to its original state (identity matrix, I). Parameter Description Comments */ //////////////////////////////////////////////////////////////////////////// VOID WINAPI_LIB G3RemoveTransformations() { GpParams->M11 = 1.0; GpParams->M22 = 1.0; GpParams->M33 = 1.0; GpParams->M12 = 0.0; GpParams->M13 = 0.0; GpParams->M14 = 0.0; GpParams->M21 = 0.0; GpParams->M23 = 0.0; GpParams->M24 = 0.0; GpParams->M31 = 0.0; GpParams->M32 = 0.0; GpParams->M34 = 0.0; GpParams->iTrFlag = 0; } //////////////////////////////////////////////////////////////////////////// /* G3SetTranslation() multiplies the current transformation matrix from the left by the translation matrix defined by the parameters. (Of course full matrix multiplication is not performed, only needed modifications to the transformation matrix.) Parameter Description x, y, z Translation shifts in the directions of x, y and z axes respectively Comments */ //////////////////////////////////////////////////////////////////////////// VOID WINAPI_LIB G3SetTranslation(double x, double y, double z) { GpParams->M14 += x; GpParams->M24 += y; GpParams->M34 += z; GpParams->iTrFlag |= TM_TRANS; } //////////////////////////////////////////////////////////////////////////// /* G3SetScale() multiplies the current transformation matrix from the left by the scaling matrix defined by the parameters. (Of course full matrix multiplication is not performed, only needed modifications to the transformation matrix.) Parameter Description Scx, Scy, Scz Scaling factors for x, y and z directions. If all values are the same, the transformation is normal overall scaling. Comments */ //////////////////////////////////////////////////////////////////////////// VOID WINAPI_LIB G3SetScale(double Scx, double Scy, double Scz) { GpParams->M11 *= Scx; GpParams->M12 *= Scx; GpParams->M13 *= Scx; GpParams->M14 *= Scx; GpParams->M21 *= Scy; GpParams->M22 *= Scy; GpParams->M23 *= Scy; GpParams->M24 *= Scy; GpParams->M31 *= Scz; GpParams->M32 *= Scz; GpParams->M33 *= Scz; GpParams->M34 *= Scz; GpParams->iTrFlag |= TM_SCALE; } //////////////////////////////////////////////////////////////////////////// /* G3SetRotate() multiplies the current transformation matrix from left by the rotation matrix defined by the parameters. (Of course full matrix multiplication is not performed, only needed modifications to the transformation matrix.) Parameter Description lpRotAxis Rotation axis. The rotation is performed about the axis defined by vector having coordinates lpRotAxis->x lpRotAxis->y and lpRotAxis->z. theta Rotation angle in radians Returns Comments */ //////////////////////////////////////////////////////////////////////////// VOID WINAPI_LIB G3SetRotate(LPG3VECTOR lpRotAxis, double theta) { double txy, txz, tyz, s, c, sz, sy, sx, t; // Temporaries double R11, R12, R13, R21, R22, R23, R31, R32, R33; // Rotation matrix elements double m11, m12, m13, m14, m21, m22, m23, m24, // Old tr. matrix values m31, m32, m33, m34; G3VECTOR Normalized; Normalized.x = lpRotAxis->x; Normalized.y = lpRotAxis->y; Normalized.z = lpRotAxis->z; G3Normalize(&Normalized); // Formulae taken from Graphics Gems, p. 466 s = sin(theta); c = cos(theta); t = 1 - c; txy = t * Normalized.x * Normalized.y; txz = t * Normalized.x * Normalized.z; tyz = t * Normalized.y * Normalized.z; sz = s*Normalized.z; sy = s*Normalized.y; sx = s*Normalized.x; // Calculate coefficient matrix elements R11 = t * Normalized.x * Normalized.x + c; R22 = t * Normalized.y * Normalized.y + c; R33 = t * Normalized.z * Normalized.z + c; R12 = txy - sz; R13 = txz + sy; R21 = txy + sz; R23 = tyz - sx; R31 = txz - sy; R32 = tyz + sx; // Save the old transformation matrix values m11 = GpParams->M11; m12 = GpParams->M12; m13 = GpParams->M13; m14 = GpParams->M14; m21 = GpParams->M21; m22 = GpParams->M22; m23 = GpParams->M23; m24 = GpParams->M24; m31 = GpParams->M31; m32 = GpParams->M32; m33 = GpParams->M33; m34 = GpParams->M34; // Modify current transformation matrix GpParams->M11 = R11*m11 + R12*m21 + R13*m31; GpParams->M12 = R11*m12 + R12*m22 + R13*m32; GpParams->M13 = R11*m13 + R12*m23 + R13*m33; GpParams->M14 = R11*m14 + R12*m24 + R13*m34; GpParams->M21 = R21*m11 + R22*m21 + R23*m31; GpParams->M22 = R21*m12 + R22*m22 + R23*m32; GpParams->M23 = R21*m13 + R22*m23 + R23*m33; GpParams->M24 = R21*m14 + R22*m24 + R23*m34; GpParams->M31 = R31*m11 + R32*m21 + R33*m31; GpParams->M32 = R31*m12 + R32*m22 + R33*m32; GpParams->M33 = R31*m13 + R32*m23 + R33*m33; GpParams->M34 = R31*m14 + R32*m24 + R33*m34; GpParams->iTrFlag |= TM_SCALE | TM_ROTATE; }