1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - GX -
3   File:     g3_util.h
4 
5   Copyright 2003-2008 Nintendo.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-09-18#$
14   $Rev: 8573 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef NITRO_G3_UTIL_H_
19 #define NITRO_G3_UTIL_H_
20 
21 #include <nitro/gx/gxcommon.h>
22 #include <nitro/gx/g3.h>
23 #include <nitro/fx/fx_const.h>
24 #include <nitro/fx/fx_mtx43.h>
25 #include <nitro/fx/fx_mtx44.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 //----------------------------------------------------------------------------
32 // Type definition
33 //----------------------------------------------------------------------------
34 
35 //----------------------------------------------------------------------------
36 // Declaration of function
37 //----------------------------------------------------------------------------
38 void    G3i_FrustumW_(fx32 t, fx32 b, fx32 l, fx32 r, fx32 n, fx32 f, fx32 scaleW, BOOL draw,
39                       MtxFx44 *mtx);
40 void    G3i_PerspectiveW_(fx32 fovySin, fx32 fovyCos, fx32 aspect, fx32 n, fx32 f, fx32 scaleW,
41                           BOOL draw, MtxFx44 *mtx);
42 void    G3i_OrthoW_(fx32 t, fx32 b, fx32 l, fx32 r, fx32 n, fx32 f, fx32 scaleW, BOOL draw,
43                     MtxFx44 *mtx);
44 void    G3i_LookAt_(const VecFx32 *camPos, const VecFx32 *camUp, const VecFx32 *target, BOOL draw,
45                     MtxFx43 *mtx);
46 void    G3_RotX(fx32 s, fx32 c);
47 void    G3_RotY(fx32 s, fx32 c);
48 void    G3_RotZ(fx32 s, fx32 c);
49 void    G3_LoadTexMtxTexCoord(const MtxFx44 *mtx);
50 void    G3_LoadTexMtxEnv(const MtxFx44 *mtx);
51 
52 void    G3BS_LoadTexMtxTexCoord(GXDLInfo *info, const MtxFx44 *mtx);
53 void    G3CS_LoadTexMtxTexCoord(GXDLInfo *info, const MtxFx44 *mtx);
54 void    G3B_LoadTexMtxTexCoord(GXDLInfo *info, const MtxFx44 *mtx);
55 void    G3C_LoadTexMtxTexCoord(GXDLInfo *info, const MtxFx44 *mtx);
56 void    G3BS_LoadTexMtxEnv(GXDLInfo *info, const MtxFx44 *mtx);
57 void    G3B_LoadTexMtxEnv(GXDLInfo *info, const MtxFx44 *mtx);
58 void    G3CS_LoadTexMtxEnv(GXDLInfo *info, const MtxFx44 *mtx);
59 void    G3C_LoadTexMtxEnv(GXDLInfo *info, const MtxFx44 *mtx);
60 
61 //----------------------------------------------------------------------------
62 // Implementation of inline function
63 //----------------------------------------------------------------------------
64 
65 /*---------------------------------------------------------------------------*
66   Name:         G3_Frustum
67 
68   Description:  Computes a 4x4 perspective projection matrix from a specified
69                 view volume, and Load it to the current projection matrix.
70 
71   Arguments:    t            top coordinate of view volume at the near clipping plane
72                 b            bottom coordinate of view volume at the near clipping plane
73                 l            left coordinate of view volume at near clipping plane
74                 r            right coordinate of view volume at near clipping plane
75                 n            positive distance from camera to near clipping plane
76                 f            positive distance from camera to far clipping plane
77                 mtx          4x4 matrix to be set if not NULL
78 
79   Returns:      none
80  *---------------------------------------------------------------------------*/
G3_Frustum(fx32 t,fx32 b,fx32 l,fx32 r,fx32 n,fx32 f,MtxFx44 * mtx)81 static inline void G3_Frustum(fx32 t, fx32 b, fx32 l, fx32 r, fx32 n, fx32 f, MtxFx44 *mtx)
82 {
83     G3i_FrustumW_(t, b, l, r, n, f, FX32_ONE, TRUE, mtx);
84     // GX_MTXMODE_PROJECTION now
85 }
86 
87 
88 /*---------------------------------------------------------------------------*
89   Name:         G3_Perspective
90 
91   Description:  Computes a 4x4 perspective projection matrix from field of
92                 view and aspect ratio, and Load it to the current projection
93                 matrix.
94 
95   Arguments:    fovySin      sine value of fovy
96                 fovyCos      cosine value of fovy
97                 aspect       ratio of view window width:height (X / Y)
98                 n            positive distance from camera to near clipping plane
99                 f            positive distance from camera to far clipping plane
100                 mtx          4x4 matrix to be set if not NULL
101 
102   Returns:      none
103  *---------------------------------------------------------------------------*/
104 static  inline
G3_Perspective(fx32 fovySin,fx32 fovyCos,fx32 aspect,fx32 n,fx32 f,MtxFx44 * mtx)105     void G3_Perspective(fx32 fovySin, fx32 fovyCos, fx32 aspect, fx32 n, fx32 f, MtxFx44 *mtx)
106 {
107     G3i_PerspectiveW_(fovySin, fovyCos, aspect, n, f, FX32_ONE, TRUE, mtx);
108     // GX_MTXMODE_PROJECTION now
109 }
110 
111 
112 /*---------------------------------------------------------------------------*
113   Name:         G3_Ortho
114 
115   Description:  Computes a 4x4 orthographic projection matrix, and Load it
116                 to the current projection matrix.
117 
118   Arguments:    t            top coordinate of parallel view volume
119                 b            bottom coordinate of parallel view volume
120                 l            left coordinate of parallel view volume
121                 r            right coordinate of parallel view volume
122                 n            positive distance from camera to near clipping plane
123                 f            positive distance from camera to far clipping plane
124                 mtx          4x4 matrix to be set if not NULL
125 
126   Returns:      none
127  *---------------------------------------------------------------------------*/
G3_Ortho(fx32 t,fx32 b,fx32 l,fx32 r,fx32 n,fx32 f,MtxFx44 * mtx)128 static inline void G3_Ortho(fx32 t, fx32 b, fx32 l, fx32 r, fx32 n, fx32 f, MtxFx44 *mtx)
129 {
130     G3i_OrthoW_(t, b, l, r, n, f, FX32_ONE, TRUE, mtx);
131     // GX_MTXMODE_PROJECTION now
132 }
133 
134 
135 /*---------------------------------------------------------------------------*
136   Name:         G3_FrustumW
137 
138   Description:  Computes a 4x4 perspective projection matrix from a specified
139                 view volume, and Load it to the current projection matrix.
140 
141   Arguments:    t            top coordinate of view volume at the near clipping plane
142                 b            bottom coordinate of view volume at the near clipping plane
143                 l            left coordinate of view volume at near clipping plane
144                 r            right coordinate of view volume at near clipping plane
145                 n            positive distance from camera to near clipping plane
146                 f            positive distance from camera to far clipping plane
147                 mtx          4x4 matrix to be set if not NULL
148 
149   Returns:      none
150  *---------------------------------------------------------------------------*/
151 static inline void
G3_FrustumW(fx32 t,fx32 b,fx32 l,fx32 r,fx32 n,fx32 f,fx32 scaleW,MtxFx44 * mtx)152 G3_FrustumW(fx32 t, fx32 b, fx32 l, fx32 r, fx32 n, fx32 f, fx32 scaleW, MtxFx44 *mtx)
153 {
154     G3i_FrustumW_(t, b, l, r, n, f, scaleW, TRUE, mtx);
155     // GX_MTXMODE_PROJECTION now
156 }
157 
158 
159 /*---------------------------------------------------------------------------*
160   Name:         G3_PerspectiveW
161 
162   Description:  Computes a 4x4 perspective projection matrix from field of
163                 view and aspect ratio, and Load it to the current projection
164                 matrix.
165 
166   Arguments:    fovySin      sine value of fovy
167                 fovyCos      cosine value of fovy
168                 aspect       ratio of view window width:height (X / Y)
169                 n            positive distance from camera to near clipping plane
170                 f            positive distance from camera to far clipping plane
171                 scaleW       W scale parameter that adjust precision of view volume.
172                 mtx          4x4 matrix to be set if not NULL
173 
174   Returns:      none
175  *---------------------------------------------------------------------------*/
176 static inline void
G3_PerspectiveW(fx32 fovySin,fx32 fovyCos,fx32 aspect,fx32 n,fx32 f,fx32 scaleW,MtxFx44 * mtx)177 G3_PerspectiveW(fx32 fovySin, fx32 fovyCos, fx32 aspect, fx32 n, fx32 f, fx32 scaleW, MtxFx44 *mtx)
178 {
179     G3i_PerspectiveW_(fovySin, fovyCos, aspect, n, f, scaleW, TRUE, mtx);
180     // GX_MTXMODE_PROJECTION now
181 }
182 
183 
184 /*---------------------------------------------------------------------------*
185   Name:         G3_OrthoW
186 
187   Description:  Computes a 4x4 orthographic projection matrix, and Load it
188                 to the current projection matrix.
189 
190   Arguments:    t            top coordinate of parallel view volume
191                 b            bottom coordinate of parallel view volume
192                 l            left coordinate of parallel view volume
193                 r            right coordinate of parallel view volume
194                 n            positive distance from camera to near clipping plane
195                 f            positive distance from camera to far clipping plane
196                 scaleW       W scale parameter that adjust precision of view volume.
197                 mtx          4x4 matrix to be set if not NULL
198 
199   Returns:      none
200  *---------------------------------------------------------------------------*/
201 static inline void
G3_OrthoW(fx32 t,fx32 b,fx32 l,fx32 r,fx32 n,fx32 f,fx32 scaleW,MtxFx44 * mtx)202 G3_OrthoW(fx32 t, fx32 b, fx32 l, fx32 r, fx32 n, fx32 f, fx32 scaleW, MtxFx44 *mtx)
203 {
204     G3i_OrthoW_(t, b, l, r, n, f, scaleW, TRUE, mtx);
205     // GX_MTXMODE_PROJECTION now
206 }
207 
208 
209 /*---------------------------------------------------------------------------*
210   Name:         G3_LookAt
211 
212   Description:  Computes a matrix to transform points to camera coordinates,
213                 and load it to the current position/vector matrix.
214 
215   Arguments:    camPos       camera position
216                 camUp        camera 'up' direction
217                 target       camera aim point
218                 mtx          a 4x3 matrix to be set if not NULL
219 
220   Returns:      none
221  *---------------------------------------------------------------------------*/
222 static inline void
G3_LookAt(const VecFx32 * camPos,const VecFx32 * camUp,const VecFx32 * target,MtxFx43 * mtx)223 G3_LookAt(const VecFx32 *camPos, const VecFx32 *camUp, const VecFx32 *target, MtxFx43 *mtx)
224 {
225     G3i_LookAt_(camPos, camUp, target, TRUE, mtx);
226     // GX_MTXMODE_POSITION_VECTOR now
227 }
228 
229 
230 #ifdef __cplusplus
231 }/* extern "C" */
232 #endif
233 
234 #endif
235