1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_ResCamera.h
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  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   $Revision: 18106 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NW_GFX_RESCAMERA_H_
17 #define NW_GFX_RESCAMERA_H_
18 
19 #include <nw/ut/ut_ResUtil.h>
20 #include <nw/ut/ut_ResDictionary.h>
21 #include <nw/gfx/res/gfx_ResLookupTable.h>
22 #include <nw/gfx/res/gfx_ResFragmentShader.h>
23 #include <nw/ut/ut_Rect.h>
24 #include <nw/gfx/res/gfx_ResRevision.h>
25 
26 namespace nw {
27 namespace gfx {
28 namespace res {
29 
30 // TODO: Updaterクラス構造の最適化をおこなう。
31 
32 //! @details :private
33 struct ResCameraData : public ResTransformNodeData
34 {
35     nw::ut::ResS32 m_ViewType;
36     nw::ut::ResS32 m_ProjectionType;
37     nw::ut::Offset toViewUpdater;
38     nw::ut::Offset toProjectionUpdater;
39     nw::ut::ResF32 m_WScale;
40 };
41 
42 //! @details :private
43 struct ResCameraViewUpdaterData
44 {
45     nw::ut::ResTypeInfo typeInfo;
46 };
47 
48 //! @details :private
49 struct ResCameraProjectionUpdaterData
50 {
51     nw::ut::ResTypeInfo typeInfo;
52     nw::ut::ResF32 m_Near;
53     nw::ut::ResF32 m_Far;
54 };
55 
56 
57 //! @details :private
58 struct ResAimTargetViewUpdaterData : public ResCameraViewUpdaterData
59 {
60     enum Flag
61     {
62         FLAG_INHERITING_TARGET_ROTATE    = 0x1 << 0,
63         FLAG_INHERITING_TARGET_TRANSLATE = 0x1 << 1
64     };
65 
66     nw::ut::ResU32 m_Flags;
67     nw::ut::ResVec3 m_TargetPosition;
68     nw::ut::ResF32 m_Twist;
69 };
70 
71 //! @details :private
72 struct ResLookAtTargetViewUpdaterData : public ResCameraViewUpdaterData
73 {
74     enum Flag
75     {
76         FLAG_INHERITING_UP_ROTATE        = 0x1 << 0,
77         FLAG_INHERITING_TARGET_ROTATE    = 0x1 << 1,
78         FLAG_INHERITING_TARGET_TRANSLATE = 0x1 << 2
79     };
80 
81     nw::ut::ResU32 m_Flags;
82     nw::ut::ResVec3 m_TargetPosition;
83     nw::ut::ResVec3 m_UpwardVector;
84 };
85 
86 //! @details :private
87 struct ResRotateViewUpdaterData : public ResCameraViewUpdaterData
88 {
89     enum Flag
90     {
91         FLAG_INHERITING_ROTATE = 0x1 << 0
92     };
93 
94     nw::ut::ResU32 m_Flags;
95     nw::ut::ResVec3 m_ViewRotate;
96 };
97 
98 struct ResProjectionRect
99 {
100     nw::ut::ResF32 m_AspectRatio;
101     nw::ut::ResF32 m_Height;
102     nw::ut::ResVec2 m_Center;
103 
104     //----------------------------------------
105     //! @name 取得/設定
106     //@{
107 
108     //! @brief        nw::ut::Rect 形式の矩形情報に変換するキャスト演算子です。
RectResProjectionRect109     operator nw::ut::Rect() const
110     {
111         f32 width = m_Height * m_AspectRatio;
112 
113         return nw::ut::Rect( m_Center.x - width / 2, m_Center.y - m_Height / 2, m_Center.x + width / 2, m_Center.y + m_Height / 2 );
114     }
115 
116     //! @brief        高さを取得します。
117     //!
118     //! @return       矩形の高さを返します。
GetHeightResProjectionRect119     f32 GetHeight() const { return m_Height; }
120 
121     //! @brief        幅を取得します。
122     //!
123     //! @return       矩形の幅を返します。
GetWidthResProjectionRect124     f32 GetWidth() const { return m_Height * m_AspectRatio; }
125 
126     //! @brief        アスペクト比を取得します。
127     //!
128     //! @return       矩形のアスペクト比を返します。
GetAspectRatioResProjectionRect129     f32 GetAspectRatio() const { return m_AspectRatio; }
130 
131     //! @brief        中心座標を取得します。
132     //!
133     //! @return       矩形の中心座標を返します。
GetCenterResProjectionRect134     nw::math::VEC2 GetCenter() const { return m_Center; }
135 
136     //! @brief        左上、右下の座標で矩形を設定します。
137     //!
138     //! @param[in]    l       矩形の左座標
139     //! @param[in]    r       矩形の右座標
140     //! @param[in]    b       矩形の下座標
141     //! @param[in]    t       矩形の上座標
SetResProjectionRect142     void Set( f32 l, f32 r, f32 b, f32 t )
143     {
144         m_Height      = b - t;
145         m_AspectRatio = (r - l) / m_Height;
146         m_Center = nw::math::VEC2( (r + l) / 2, (b + t) / 2 );
147     }
148 
149     //! @brief        高さとアスペクト比で矩形を設定します。
150     //!
151     //! @param[in]    aspect  アスペクト比です。
152     //! @param[in]    height  高さです。
153     //! @param[in]    center  中心座標です。
SetResProjectionRect154     void Set( f32 aspect, f32 height, const nw::math::VEC2& center )
155     {
156         m_Height = height;
157         m_AspectRatio = aspect;
158         m_Center = center;
159     }
160 
161     //@}
162 };
163 
164 //! @details :private
165 struct ResFrustumProjectionUpdaterData : public ResCameraProjectionUpdaterData
166 {
167     ResProjectionRect m_Rect;
168 };
169 
170 //! @details :private
171 struct ResOrthoProjectionUpdaterData : public ResCameraProjectionUpdaterData
172 {
173     ResProjectionRect m_Rect;
174 };
175 
176 //! @details :private
177 struct ResPerspectiveProjectionUpdaterData : public ResCameraProjectionUpdaterData
178 {
179     nw::ut::ResF32 m_AspectRatio;
180     nw::ut::ResF32 m_Fovy;
181 };
182 
183 
184 //--------------------------------------------------------------------------
185 //! @brief      カメラビューアップデータリソースの基底クラスです。
186 //---------------------------------------------------------------------------
187 class ResCameraViewUpdater : public nw::ut::ResCommon< ResCameraViewUpdaterData >
188 {
189 public:
190     enum { TYPE_INFO = NW_GFX_RES_TYPE_INFO(ResCameraViewUpdater) };
191 
NW_RES_CTOR(ResCameraViewUpdater)192     NW_RES_CTOR( ResCameraViewUpdater )
193 
194     //---------------------------------------------------------------------------
195     //! @brief        インスタンスの型情報を取得します。
196     //!
197     //! @return       型情報です。
198     //---------------------------------------------------------------------------
199     nw::ut::ResTypeInfo     GetTypeInfo() const { return ref().typeInfo; }
200 };
201 
202 //--------------------------------------------------------------------------
203 //! @brief      カメラプロジェクションアップデータリソースの基底クラスです。
204 //---------------------------------------------------------------------------
205 class ResCameraProjectionUpdater : public nw::ut::ResCommon< ResCameraProjectionUpdaterData >
206 {
207 public:
208 
209     enum { TYPE_INFO = NW_GFX_RES_TYPE_INFO(ResCameraProjectionUpdater) };
210 
211     NW_RES_CTOR( ResCameraProjectionUpdater )
212 
213     //---------------------------------------------------------------------------
214     //! @fn           void SetNear(f32 value)
215     //! @brief        ニアクリップの値を設定します。
216     //---------------------------------------------------------------------------
217     //---------------------------------------------------------------------------
218     //! @fn           f32 GetNear() const
219     //! @brief        ニアクリップの値を取得します。
220     //---------------------------------------------------------------------------
NW_RES_FIELD_PRIMITIVE_DECL(f32,Near)221     NW_RES_FIELD_PRIMITIVE_DECL( f32, Near )                    // GetNear(), SetNear()
222 
223     //---------------------------------------------------------------------------
224     //! @fn           void SetFar(f32 value)
225     //! @brief        ファークリップの値を設定します。
226     //---------------------------------------------------------------------------
227     //---------------------------------------------------------------------------
228     //! @fn           f32 GetFar() const
229     //! @brief        ファークリップの値を取得します。
230     //---------------------------------------------------------------------------
231     NW_RES_FIELD_PRIMITIVE_DECL( f32, Far )                     // GetFar(), SetFar()
232 
233     //---------------------------------------------------------------------------
234     //! @brief        インスタンスの型情報を取得します。
235     //!
236     //! @return       型情報です。
237     //---------------------------------------------------------------------------
238     nw::ut::ResTypeInfo     GetTypeInfo() const { return ref().typeInfo; }
239 };
240 
241 //--------------------------------------------------------------------------
242 //! @brief  Aim カメラ用ビューアップデータのバイナリリソースクラスです。
243 //---------------------------------------------------------------------------
244 class ResAimTargetViewUpdater : public ResCameraViewUpdater
245 {
246 public:
247     enum { TYPE_INFO = NW_GFX_RES_TYPE_INFO(ResAimTargetViewUpdater) };
248 
249     NW_RES_CTOR_INHERIT( ResAimTargetViewUpdater, ResCameraViewUpdater )
250 
251     //---------------------------------------------------------------------------
252     //! @fn           void SetTwist(f32 value)
253     //! @brief        カメラの視線方向に対する回転角度を設定します。
254     //---------------------------------------------------------------------------
255     //---------------------------------------------------------------------------
256     //! @fn           void SetTargetPosition(f32 x, f32 y, f32 z)
257     //! @brief        注視点の位置を設定します。
258     //---------------------------------------------------------------------------
259     //---------------------------------------------------------------------------
260     //! @fn           void SetFlags(u32 value)
261     //! @brief        フラグの値を設定します。
262     //---------------------------------------------------------------------------
263     //---------------------------------------------------------------------------
264     //! @fn           f32 GetTwist() const
265     //! @brief        カメラの視線方向に対する回転角度を取得します。
266     //---------------------------------------------------------------------------
267     //---------------------------------------------------------------------------
268     //! @fn           const nw::math::VEC3 & GetTargetPosition() const
269     //! @brief        注視点の位置を取得します。
270     //---------------------------------------------------------------------------
271     //---------------------------------------------------------------------------
272     //! @fn           u32 GetFlags() const
273     //! @brief        フラグの値を取得します。
274     //---------------------------------------------------------------------------
275 
276     NW_RES_FIELD_VECTOR3_DECL( nw::math::VEC3, TargetPosition )  // VEC3& GetTargetPosition()
277     NW_RES_FIELD_PRIMITIVE_DECL( f32, Twist )                   // GetTwist(), SetTwist()
278     NW_RES_FIELD_PRIMITIVE_DECL( u32, Flags )                   // GetFlags(), SetFlags()
279 };
280 
281 //--------------------------------------------------------------------------
282 //! @brief  LookAt カメラ用ビューアップデータのバイナリリソースクラスです。
283 //---------------------------------------------------------------------------
284 class ResLookAtTargetViewUpdater : public ResCameraViewUpdater
285 {
286 public:
287     enum { TYPE_INFO = NW_GFX_RES_TYPE_INFO(ResLookAtTargetViewUpdater) };
288 
289     NW_RES_CTOR_INHERIT( ResLookAtTargetViewUpdater, ResCameraViewUpdater )
290 
291     //---------------------------------------------------------------------------
292     //! @fn           void SetUpwardVector(f32 x, f32 y, f32 z)
293     //! @brief        カメラの上方向ベクトルを設定します。
294     //---------------------------------------------------------------------------
295     //---------------------------------------------------------------------------
296     //! @fn           void SetTargetPosition(f32 x, f32 y, f32 z)
297     //! @brief        カメラの注視点の座標を設定します。
298     //---------------------------------------------------------------------------
299     //---------------------------------------------------------------------------
300     //! @fn           void SetFlags(u32 value)
301     //! @brief        フラグの値を設定します。
302     //---------------------------------------------------------------------------
303     //---------------------------------------------------------------------------
304     //! @fn           const nw::math::VEC3 & GetUpwardVector() const
305     //! @brief        カメラの上方向ベクトルを取得します。
306     //---------------------------------------------------------------------------
307     //---------------------------------------------------------------------------
308     //! @fn           const nw::math::VEC3 & GetTargetPosition() const
309     //! @brief        カメラの注視点の座標を取得します。
310     //---------------------------------------------------------------------------
311     //---------------------------------------------------------------------------
312     //! @fn           u32 GetFlags() const
313     //! @brief        フラグの値を取得します。
314     //---------------------------------------------------------------------------
315     NW_RES_FIELD_VECTOR3_DECL( nw::math::VEC3, TargetPosition )  // VEC3& GetTargetPosition()
316     NW_RES_FIELD_VECTOR3_DECL( nw::math::VEC3, UpwardVector )    // VEC3& GetUpwardVector()
317     NW_RES_FIELD_PRIMITIVE_DECL( u32, Flags )                   // GetFlags(), SetFlags()
318 };
319 
320 //--------------------------------------------------------------------------
321 //! @brief  Rotate カメラ用ビューアップデータのバイナリリソースクラスです。
322 //---------------------------------------------------------------------------
323 class ResRotateViewUpdater : public ResCameraViewUpdater
324 {
325 public:
326     enum { TYPE_INFO = NW_GFX_RES_TYPE_INFO(ResRotateViewUpdater) };
327 
328     NW_RES_CTOR_INHERIT( ResRotateViewUpdater, ResCameraViewUpdater )
329 
330     //---------------------------------------------------------------------------
331     //! @fn           void SetViewRotate(f32 x, f32 y, f32 z)
332     //! @brief        カメラの回転量を設定します。
333     //---------------------------------------------------------------------------
334     //---------------------------------------------------------------------------
335     //! @fn           void SetFlags(u32 value)
336     //! @brief        フラグの値を設定します。
337     //---------------------------------------------------------------------------
338     //---------------------------------------------------------------------------
339     //! @fn           const nw::math::VEC3 & GetViewRotate() const
340     //! @brief        カメラの回転量を取得します。
341     //---------------------------------------------------------------------------
342     //---------------------------------------------------------------------------
343     //! @fn           u32 GetFlags() const
344     //! @brief        フラグの値を取得します。
345     //---------------------------------------------------------------------------
346     NW_RES_FIELD_VECTOR3_DECL( nw::math::VEC3, ViewRotate )      // VEC3& GetViewRotate()
347     NW_RES_FIELD_PRIMITIVE_DECL( u32, Flags )                   // GetFlags(), SetFlags()
348 };
349 
350 //--------------------------------------------------------------------------
351 //! @brief  Frustum 用プロジェクションアップデータのバイナリリソースクラスです。
352 //---------------------------------------------------------------------------
353 class ResFrustumProjectionUpdater : public ResCameraProjectionUpdater
354 {
355 public:
356     enum { TYPE_INFO = NW_GFX_RES_TYPE_INFO(ResFrustumProjectionUpdater) };
357 
358     NW_RES_CTOR_INHERIT( ResFrustumProjectionUpdater, ResCameraProjectionUpdater )
359 
360     //---------------------------------------------------------------------------
361     //! @fn           void SetRect(f32 l, f32 r, f32 b, f32 t)
362     //! @brief        透視射影の近平面の矩形情報を設定します。
363     //---------------------------------------------------------------------------
364     //---------------------------------------------------------------------------
365     //! @fn           const ResProjectionRect & GetRect() const
366     //! @brief        透視射影の近平面の矩形情報を取得します。
367     //---------------------------------------------------------------------------
368     NW_RES_FIELD_RECT_DECL( ResProjectionRect, Rect )         // ResProjectionRect& GetRect()
369 };
370 
371 //--------------------------------------------------------------------------
372 //! @brief  Ortho 用プロジェクションアップデータのバイナリリソースクラスです。
373 //---------------------------------------------------------------------------
374 class ResOrthoProjectionUpdater : public ResCameraProjectionUpdater
375 {
376 public:
377     enum { TYPE_INFO = NW_GFX_RES_TYPE_INFO(ResOrthoProjectionUpdater) };
378 
379     NW_RES_CTOR_INHERIT( ResOrthoProjectionUpdater, ResCameraProjectionUpdater )
380 
381     //---------------------------------------------------------------------------
382     //! @fn           void SetRect(f32 l, f32 r, f32 b, f32 t)
383     //! @brief        正射影の矩形情報を設定します。
384     //---------------------------------------------------------------------------
385     //---------------------------------------------------------------------------
386     //! @fn           const ResProjectionRect & GetRect() const
387     //! @brief        正射影の矩形情報を取得します。
388     //---------------------------------------------------------------------------
389     NW_RES_FIELD_RECT_DECL( ResProjectionRect, Rect )         // ResProjectionRect& GetRect()
390 };
391 
392 //--------------------------------------------------------------------------
393 //! @brief  Perspective 用プロジェクションアップデータのバイナリリソースクラスです。
394 //---------------------------------------------------------------------------
395 class ResPerspectiveProjectionUpdater : public ResCameraProjectionUpdater
396 {
397 public:
398     enum { TYPE_INFO = NW_GFX_RES_TYPE_INFO(ResPerspectiveProjectionUpdater) };
399 
400     NW_RES_CTOR_INHERIT( ResPerspectiveProjectionUpdater, ResCameraProjectionUpdater )
401 
402     //---------------------------------------------------------------------------
403     //! @fn           void SetFovy(f32 value)
404     //! @brief        カメラの画角を設定します。
405     //---------------------------------------------------------------------------
406     //---------------------------------------------------------------------------
407     //! @fn           void SetAspectRatio(f32 value)
408     //! @brief        アスペクト比を設定します。
409     //---------------------------------------------------------------------------
410     //---------------------------------------------------------------------------
411     //! @fn           f32 GetFovy() const
412     //! @brief        カメラの画角を取得します。
413     //---------------------------------------------------------------------------
414     //---------------------------------------------------------------------------
415     //! @fn           f32 GetAspectRatio() const
416     //! @brief        アスペクト比を取得します。
417     //---------------------------------------------------------------------------
418     NW_RES_FIELD_PRIMITIVE_DECL( f32, AspectRatio )             // GetAspectRatio(), SetAspectRatio()
419     NW_RES_FIELD_PRIMITIVE_DECL( f32, Fovy )                    // GetFovy(), SetFovy()
420 };
421 
422 //--------------------------------------------------------------------------
423 //! @brief  カメラのバイナリリソースクラスです。
424 //---------------------------------------------------------------------------
425 class ResCamera : public ResTransformNode
426 {
427 public:
428     enum { BINARY_REVISION = REVISION_RES_CAMERA };
429     enum { TYPE_INFO = NW_GFX_RES_TYPE_INFO(ResCamera) };
430 
431     //! @brief ビューアップデータの種類です。
432     enum ViewType
433     {
434         VIEWTYPE_AIM,       //!< Aim ビューアップデータです。
435         VIEWTYPE_LOOKAT,    //!< LookAt ビューアップデータです。
436         VIEWTYPE_ROTATE,    //!< Rotate ビューアップデータです。
437         VIEWTYPE_COUNT      //!< enum の最大値です。
438     };
439 
440     //! @brief プロジェクションアップデータの種類です。
441     enum ProjType
442     {
443         PROJTYPE_PERSPECTIVE,   //!< Perspective プロジェクションアップデータです。
444         PROJTYPE_FRUSTUM,       //!< Frustum プロジェクションアップデータです。
445         PROJTYPE_ORTHO,         //!< Ortho プロジェクションアップデータです。
446         PROJTYPE_COUNT          //!< enum の最大値です。
447     };
448 
NW_RES_CTOR_INHERIT(ResCamera,ResTransformNode)449     NW_RES_CTOR_INHERIT( ResCamera, ResTransformNode )
450 
451     //---------------------------------------------------------------------------
452     //! @fn           void SetViewType(ViewType value)
453     //! @brief        ビューカメラアップデータのタイプを設定します。
454     //---------------------------------------------------------------------------
455     //---------------------------------------------------------------------------
456     //! @fn           ViewType GetViewType() const
457     //! @brief        ビューカメラアップデータのタイプを取得します。
458     //---------------------------------------------------------------------------
459     NW_RES_FIELD_PRIMITIVE_DECL( ViewType, ViewType )                        // GetViewType()
460 
461     //---------------------------------------------------------------------------
462     //! @fn           void SetProjectionType(ProjType value)
463     //! @brief        プロジェクションカメラアップデータのタイプを設定します。
464     //---------------------------------------------------------------------------
465     //---------------------------------------------------------------------------
466     //! @fn           ProjType GetProjectionType() const
467     //! @brief        プロジェクションカメラアップデータのタイプを取得します。
468     //---------------------------------------------------------------------------
469     NW_RES_FIELD_PRIMITIVE_DECL( ProjType, ProjectionType )                  // GetProjectionType()
470 
471     //---------------------------------------------------------------------------
472     //! @fn           ResCameraViewUpdater GetViewUpdater()
473     //! @brief        ビュー行列のアップデータを取得します。
474     //---------------------------------------------------------------------------
475     NW_RES_FIELD_CLASS_DECL( ResCameraViewUpdater, ViewUpdater )             // GetViewUpdater()
476 
477     //---------------------------------------------------------------------------
478     //! @fn           ResCameraProjectionUpdater GetProjectionUpdater()
479     //! @brief        射影行列のアップデータを取得します。
480     //---------------------------------------------------------------------------
481     NW_RES_FIELD_CLASS_DECL( ResCameraProjectionUpdater, ProjectionUpdater ) // GetProjectionUpdater()
482 
483     //---------------------------------------------------------------------------
484     //! @fn           void SetWScale(f32 value)
485     //! @brief        Wバッファのスケール因子を設定します。
486     //---------------------------------------------------------------------------
487     //---------------------------------------------------------------------------
488     //! @fn           f32 GetWScale() const
489     //! @brief        Wバッファのスケール因子を取得します。
490     //---------------------------------------------------------------------------
491     NW_RES_FIELD_PRIMITIVE_DECL( f32, WScale )                               // GetWScale()
492 
493     //---------------------------------------------------------------------------
494     //! @brief        リビジョンを取得します。
495     //!
496     //! @return       リソースのリビジョン情報です。
497     //---------------------------------------------------------------------------
498     u32 GetRevision() const { return this->GetHeader().revision; }
499 };
500 
501 typedef nw::ut::ResArrayPatricia<ResCamera>::type  ResCameraArray;
502 
503 } // namespace res
504 } // namespace gfx
505 } // namespace nw
506 
507 #endif // NW_GFX_RESCAMERA_H_
508