1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     anim_ResAnimGroup.cpp
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:  $
14  *---------------------------------------------------------------------------*/
15 
16 #include "../precompiled.h"
17 
18 #include <nw/anim/res/anim_ResAnimGroup.h>
19 #include <nw/gfx/gfx_SkeletalModel.h>
20 #include <nw/gfx/gfx_Model.h>
21 #include <nw/gfx/res/gfx_ResTexture.h>
22 #include <nw/gfx/gfx_Camera.h>
23 #include <nw/gfx/gfx_Light.h>
24 #include <nw/gfx/gfx_FragmentLight.h>
25 #include <nw/gfx/gfx_AmbientLight.h>
26 #include <nw/gfx/gfx_VertexLight.h>
27 #include <nw/gfx/gfx_HemiSphereLight.h>
28 
29 using namespace nw::gfx;
30 
31 namespace nw {
32 namespace anim {
33 namespace res {
34 
35 //----------------------------------------
36 void
SetValueForType(void * object,const void * value) const37 ResAnimGroupMember::SetValueForType(void* object, const void* value) const
38 {
39     NW_NULL_ASSERT(object);
40     NW_NULL_ASSERT(value);
41 
42     // ObjectTypeに対応する派生クラスに処理を委譲
43     switch(GetObjectType())
44     {
45     case OBJECT_TYPE_BONE:
46         ResStaticCast<ResBoneMember>(*this).SetValue(object, value);
47         break;
48 
49     case OBJECT_TYPE_MATERIAL_COLOR:
50         ResStaticCast<ResMaterialColorMember>(*this).SetValue(object, value);
51         break;
52 
53     case OBJECT_TYPE_TEXTURE_SAMPLER:
54         ResStaticCast<ResTextureSamplerMember>(*this).SetValue(object, value);
55         break;
56 
57     case OBJECT_TYPE_TEXTURE_MAPPER:
58         ResStaticCast<ResTextureMapperMember>(*this).SetValue(object, value);
59         break;
60 
61     case OBJECT_TYPE_BLEND_OPERATION:
62         ResStaticCast<ResBlendOperationMember>(*this).SetValue(object, value);
63         break;
64 
65     case OBJECT_TYPE_TEXTURE_COORDINATOR:
66         ResStaticCast<ResTextureCoordinatorMember>(*this).SetValue(object, value);
67         break;
68 
69     case OBJECT_TYPE_MODEL:
70         ResStaticCast<ResModelMember>(*this).SetValue(object, value);
71         break;
72 
73     case OBJECT_TYPE_MESH:
74         ResStaticCast<ResMeshMember>(*this).SetValue(object, value);
75         break;
76 
77     case OBJECT_TYPE_MESH_NODE_VISIBILITY:
78         ResStaticCast<ResMeshNodeVisibilityMember>(*this).SetValue(object, value);
79         break;
80 
81     case OBJECT_TYPE_TRANSFORM:
82         ResStaticCast<ResTransformMember>(*this).SetValue(object, value);
83         break;
84 
85     case OBJECT_TYPE_VIEW_UPDATER:
86         ResStaticCast<ResViewUpdaterMember>(*this).SetValue(object, value);
87         break;
88 
89     case OBJECT_TYPE_PROJECTION_UPDATER:
90         ResStaticCast<ResProjectionUpdaterMember>(*this).SetValue(object, value);
91         break;
92 
93     case OBJECT_TYPE_LIGHT:
94         ResStaticCast<ResLightMember>(*this).SetValue(object, value);
95         break;
96 
97     case OBJECT_TYPE_FRAGMENT_LIGHT:
98         ResStaticCast<ResFragmentLightMember>(*this).SetValue(object, value);
99         break;
100 
101     case OBJECT_TYPE_AMBIENT_LIGHT:
102         ResStaticCast<ResAmbientLightMember>(*this).SetValue(object, value);
103         break;
104 
105     case OBJECT_TYPE_VERTEX_LIGHT:
106         ResStaticCast<ResVertexLightMember>(*this).SetValue(object, value);
107         break;
108 
109     case OBJECT_TYPE_HEMISPHERE_LIGHT:
110         ResStaticCast<ResHemiSphereLightMember>(*this).SetValue(object, value);
111         break;
112 
113     default:
114         NW_ASSERT(false);
115         break;
116     }
117 }
118 
119 //----------------------------------------
120 void
SetValue(void * object,const void * value) const121 ResBoneMember::SetValue(void* object, const void* value) const
122 {
123     CalculatedTransform* bone =
124         reinterpret_cast<CalculatedTransform*>(object);
125     const math::Transform3* transform =
126         reinterpret_cast<const math::Transform3*>(value);
127 
128     bone->SetTransform(*transform);
129     bone->UpdateFlags();
130 }
131 
132 
133 //----------------------------------------
134 void
SetValue(void * object,const void * value) const135 ResMaterialColorMember::SetValue(void* object, const void* value) const
136 {
137     ResMaterialColor materialColor(object);
138     const ut::FloatColor& color = *reinterpret_cast<const ut::FloatColor*>(value);
139 
140     // MemberTypeに対応するsetterを呼び出す
141     switch(GetMemberType())
142     {
143     case MEMBER_TYPE_EMISSION:
144         materialColor.SetEmission(color);
145         break;
146 
147     case MEMBER_TYPE_AMBIENT:
148         materialColor.SetAmbient(color.r, color.g, color.b);
149         break;
150 
151     case MEMBER_TYPE_DIFFUSE:
152         materialColor.SetDiffuse(color);
153         break;
154 
155     case MEMBER_TYPE_SPECULAR0:
156         materialColor.SetSpecular0(color);
157         break;
158 
159     case MEMBER_TYPE_SPECULAR1:
160         materialColor.SetSpecular1(color);
161         break;
162 
163     case MEMBER_TYPE_CONSTANT0:
164         materialColor.SetConstant0(color);
165         break;
166 
167     case MEMBER_TYPE_CONSTANT1:
168         materialColor.SetConstant1(color);
169         break;
170 
171     case MEMBER_TYPE_CONSTANT2:
172         materialColor.SetConstant2(color);
173         break;
174 
175     case MEMBER_TYPE_CONSTANT3:
176         materialColor.SetConstant3(color);
177         break;
178 
179     case MEMBER_TYPE_CONSTANT4:
180         materialColor.SetConstant4(color);
181         break;
182 
183     case MEMBER_TYPE_CONSTANT5:
184         materialColor.SetConstant5(color);
185         break;
186 
187     default:
188         NW_ASSERT(false);
189     }
190 }
191 
192 //----------------------------------------
193 void
SetValue(void * object,const void * value) const194 ResTextureSamplerMember::SetValue(void* object, const void* value) const
195 {
196     ResPixelBasedTextureMapper mapper(object);
197     const ut::FloatColor& color = *reinterpret_cast<const ut::FloatColor*>(value);
198 
199     NW_NULL_ASSERT(ResDynamicCast<ResStandardTextureSampler>(mapper.GetSampler()).ptr());
200 
201     // mapper.GetSampler()はconstなので、内部に直接アクセスする
202     ResStandardTextureSampler sampler(mapper.ref().toSampler.to_ptr());
203 
204     switch(GetMemberType())
205     {
206     case MEMBER_TYPE_BORDER_COLOR:
207         sampler.SetBorderColor(color);
208         break;
209 
210     default:
211         NW_ASSERT(false);
212     }
213 }
214 
215 //----------------------------------------
216 void
SetValue(void * object,const void * value) const217 ResTextureMapperMember::SetValue(void* object, const void* value) const
218 {
219     ResPixelBasedTextureMapper mapper(object);
220     const ut::Offset& offset = *reinterpret_cast<const ut::Offset*>(value);
221 
222     ResReferenceTexture texture(offset.to_ptr());
223 
224     switch(GetMemberType())
225     {
226     case MEMBER_TYPE_TEXTURE:
227         mapper.SetTexture(texture.GetTargetTexture());
228         break;
229 
230     default:
231         NW_ASSERT(false);
232     }
233 }
234 
235 //----------------------------------------
236 void
SetValue(void * object,const void * value) const237 ResBlendOperationMember::SetValue(void* object, const void* value) const
238 {
239     ResBlendOperation operation(object);
240     const ut::FloatColor& color = *reinterpret_cast<const ut::FloatColor*>(value);
241 
242     switch(GetMemberType())
243     {
244     case MEMBER_TYPE_BLEND_COLOR:
245         operation.SetBlendColor(color);
246         break;
247 
248     default:
249         NW_ASSERT(false);
250     }
251 }
252 
253 //----------------------------------------
254 void
SetValue(void * object,const void * value) const255 ResTextureCoordinatorMember::SetValue(void* object, const void* value) const
256 {
257     ResTextureCoordinator coordinator(object);
258 
259     switch(GetMemberType())
260     {
261     case MEMBER_TYPE_SCALE:
262         {
263             const math::VEC2& scale = *reinterpret_cast<const math::VEC2*>(value);
264             coordinator.SetScale(scale);
265         }
266         break;
267 
268     case MEMBER_TYPE_ROTATE:
269         {
270             float rotate = *reinterpret_cast<const float*>(value);
271             coordinator.SetRotate(rotate);
272         }
273         break;
274 
275     case MEMBER_TYPE_TRANSLATE:
276         {
277             const math::VEC2& translate = *reinterpret_cast<const math::VEC2*>(value);
278             coordinator.SetTranslate(translate);
279         }
280         break;
281 
282     default:
283         NW_ASSERT(false);
284     }
285 
286     // Coordinator内で、値が変化した場合のみDirtyフラグを立てるようになっている。
287     // しかし、ここ以前に直接メモリに書き込んでいるので、
288     // 値が変化していないと判定されてしまう。そのため、明示的にフラグを立てる。
289     coordinator.SetDirty(true);
290 }
291 
292 //----------------------------------------
293 void
SetValue(void * object,const void * value) const294 ResModelMember::SetValue(void* object, const void* value) const
295 {
296     Model* model = reinterpret_cast<Model*>(object);
297 
298     switch(GetMemberType())
299     {
300     case MEMBER_TYPE_BRANCH_VISIBLE:
301         // TODO: 未実装
302         NW_ASSERT(false);
303         break;
304     case MEMBER_TYPE_VISIBLE:
305         {
306             const bool visibility = *static_cast<const bool*>(value);
307             model->SetVisible(visibility);
308         }
309         break;
310     default:
311         NW_ASSERT(false);
312     }
313 }
314 
315 //----------------------------------------
316 void
SetValue(void * object,const void * value) const317 ResMeshMember::SetValue(void* object, const void* value) const
318 {
319     ResMesh mesh(object);
320 
321     const bool visibility = *static_cast<const bool*>(value);
322     mesh.SetVisible(visibility);
323 }
324 
325 //----------------------------------------
326 void
SetValue(void * object,const void * value) const327 ResMeshNodeVisibilityMember::SetValue(void* object, const void* value) const
328 {
329     ResMeshNodeVisibility nodeVisibility(object);
330 
331     const bool visibility = *static_cast<const bool*>(value);
332     nodeVisibility.SetVisible(visibility);
333 }
334 
335 //----------------------------------------
336 void
SetValue(void * object,const void * value) const337 ResTransformMember::SetValue(void* object, const void* value) const
338 {
339     TransformNode* transformNode = reinterpret_cast<TransformNode*>(object);
340 
341     switch(GetMemberType())
342     {
343     case MEMBER_TYPE_TRANSFORM:
344         {
345             transformNode->Transform().SetTransform(*reinterpret_cast<const math::Transform3*>(value));
346             transformNode->Transform().EnableFlags(CalculatedTransform::FLAG_IS_DIRTY);
347         }
348         break;
349 
350     default:
351         NW_ASSERT(false);
352     }
353 }
354 
355 //----------------------------------------
356 void
SetValue(void * object,const void * value) const357 ResViewUpdaterMember::SetValue(void* object, const void* value) const
358 {
359     ResCameraViewUpdater viewUpdater(object);
360 
361     switch(viewUpdater.GetTypeInfo())
362     {
363     case ResAimTargetViewUpdater::TYPE_INFO:
364         {
365             ResAimTargetViewUpdater aimUpdater(object);
366 
367             switch(GetMemberType())
368             {
369             case MEMBER_TYPE_TARGET_POSITION:
370                 {
371                     const math::VEC3& position = *reinterpret_cast<const math::VEC3*>(value);
372                     aimUpdater.SetTargetPosition(position);
373                 }
374                 break;
375 
376             case MEMBER_TYPE_TWIST:
377                 {
378                     float twist = *reinterpret_cast<const float*>(value);
379                     aimUpdater.SetTwist(twist);
380                 }
381                 break;
382 
383             default:
384                 NW_ASSERT(false);
385             }
386         }
387         break;
388 
389     case ResLookAtTargetViewUpdater::TYPE_INFO:
390         {
391             ResLookAtTargetViewUpdater lookAtUpdater(object);
392 
393             switch(GetMemberType())
394             {
395             case MEMBER_TYPE_TARGET_POSITION:
396                 {
397                     const math::VEC3& position = *reinterpret_cast<const math::VEC3*>(value);
398                     lookAtUpdater.SetTargetPosition(position);
399                 }
400                 break;
401 
402             case MEMBER_TYPE_UPWARD_VECTOR:
403                 {
404                     const math::VEC3& upVector = *reinterpret_cast<const math::VEC3*>(value);
405                     lookAtUpdater.SetUpwardVector(upVector);
406                 }
407                 break;
408 
409             default:
410                 NW_ASSERT(false);
411             }
412         }
413         break;
414 
415     case ResRotateViewUpdater::TYPE_INFO:
416         {
417             ResRotateViewUpdater rotateUpdater(object);
418 
419             switch(GetMemberType())
420             {
421             case MEMBER_TYPE_VIEW_ROTATE:
422                 {
423                     const math::VEC3& rotate = *reinterpret_cast<const math::VEC3*>(value);
424                     rotateUpdater.SetViewRotate(rotate);
425                 }
426                 break;
427 
428             default:
429                 NW_ASSERT(false);
430             }
431         }
432         break;
433 
434     default:
435         NW_ASSERT(false);
436     }
437 }
438 
439 //----------------------------------------
440 void
SetValue(void * object,const void * value) const441 ResProjectionUpdaterMember::SetValue(void* object, const void* value) const
442 {
443     ResCameraProjectionUpdater projectionUpdater(object);
444 
445     switch(projectionUpdater.GetTypeInfo())
446     {
447     case ResPerspectiveProjectionUpdater::TYPE_INFO:
448         {
449             ResPerspectiveProjectionUpdater perspectiveUpdater(object);
450             float floatValue = *reinterpret_cast<const float*>(value);
451 
452             switch(GetMemberType())
453             {
454             case MEMBER_TYPE_NEAR:
455                 perspectiveUpdater.SetNear(floatValue);
456                 break;
457 
458             case MEMBER_TYPE_FAR:
459                 perspectiveUpdater.SetFar(floatValue);
460                 break;
461 
462             case MEMBER_TYPE_FOVY:
463                 perspectiveUpdater.SetFovy(floatValue);
464                 break;
465 
466             case MEMBER_TYPE_ASPECT_RATIO:
467                 perspectiveUpdater.SetAspectRatio(floatValue);
468                 break;
469 
470             default:
471                 NW_ASSERT(false);
472             }
473         }
474         break;
475 
476     case ResOrthoProjectionUpdater::TYPE_INFO:
477         {
478             ResOrthoProjectionUpdater orthoUpdater(object);
479             float floatValue = *reinterpret_cast<const float*>(value);
480 
481             switch(GetMemberType())
482             {
483             case MEMBER_TYPE_NEAR:
484                 orthoUpdater.SetNear(floatValue);
485                 break;
486 
487             case MEMBER_TYPE_FAR:
488                 orthoUpdater.SetFar(floatValue);
489                 break;
490 
491             case MEMBER_TYPE_ASPECT_RATIO:
492                 {
493                     ResProjectionRect rect = orthoUpdater.GetRect();
494                     rect.Set(floatValue, rect.GetHeight(), rect.GetCenter());
495                     orthoUpdater.SetRect(rect);
496                 }
497                 break;
498 
499             case MEMBER_TYPE_HEIGHT:
500                 {
501                     ResProjectionRect rect = orthoUpdater.GetRect();
502                     rect.Set(rect.GetAspectRatio(), floatValue, rect.GetCenter());
503                     orthoUpdater.SetRect(rect);
504                 }
505                 break;
506 
507             case MEMBER_TYPE_CENTER:
508                 {
509                     const math::VEC2& center = *reinterpret_cast<const math::VEC2*>(value);
510 
511                     ResProjectionRect rect = orthoUpdater.GetRect();
512                     rect.Set(rect.GetAspectRatio(), rect.GetHeight(), center);
513                     orthoUpdater.SetRect(rect);
514                 }
515                 break;
516 
517             default:
518                 NW_ASSERT(false);
519             }
520         }
521         break;
522 
523     // ResFrustumProjectionUpdaterは、アニメーションを設定できない
524 
525     default:
526         NW_ASSERT(false);
527     }
528 }
529 
530 //----------------------------------------
531 void
SetValue(void * object,const void * value) const532 ResLightMember::SetValue(void* object, const void* value) const
533 {
534     ResLight light(object);
535     bool boolValue = *static_cast<const bool*>(value);
536 
537     switch (GetMemberType())
538     {
539     case MEMBER_TYPE_IS_LIGHT_ENABLED:
540         light.SetLightEnabled(boolValue);
541         break;
542 
543     default:
544         NW_ASSERT(false);
545     }
546 }
547 
548 //----------------------------------------
549 void
SetValue(void * object,const void * value) const550 ResFragmentLightMember::SetValue(void* object, const void* value) const
551 {
552     ResFragmentLight light(object);
553     const ut::FloatColor& color = *reinterpret_cast<const ut::FloatColor*>(value);
554 
555     switch (GetMemberType())
556     {
557     case MEMBER_TYPE_AMBIENT:
558         light.SetAmbient(color);
559         break;
560 
561     case MEMBER_TYPE_DIFFUSE:
562         light.SetDiffuse(color);
563         break;
564 
565     case MEMBER_TYPE_SPECULAR0:
566         light.SetSpecular0(color);
567         break;
568 
569     case MEMBER_TYPE_SPECULAR1:
570         light.SetSpecular1(color);
571         break;
572 
573     case MEMBER_TYPE_DIRECTION:
574         {
575             math::VEC3 direction = *reinterpret_cast<const math::VEC3*>(value);
576             direction.Normalize();
577             light.SetDirection(direction);
578         }
579         break;
580 
581     case MEMBER_TYPE_DISTANCE_ATTENUATION_START:
582         {
583             float start = *reinterpret_cast<const float*>(value);
584             light.SetDistanceAttenuationStart(start);
585         }
586         break;
587 
588     case MEMBER_TYPE_DISTANCE_ATTENUATION_END:
589         {
590             float end = *reinterpret_cast<const float*>(value);
591             light.SetDistanceAttenuationEnd(end);
592         }
593         break;
594 
595     default:
596         NW_ASSERT(false);
597     }
598 }
599 
600 //----------------------------------------
601 void
SetValue(void * object,const void * value) const602 ResAmbientLightMember::SetValue(void* object, const void* value) const
603 {
604     ResAmbientLight light(object);
605     const ut::FloatColor& color = *reinterpret_cast<const ut::FloatColor*>(value);
606 
607     switch (GetMemberType())
608     {
609     case MEMBER_TYPE_AMBIENT:
610         light.SetAmbient(color);
611         break;
612 
613     default:
614         NW_ASSERT(false);
615     }
616 }
617 
618 //----------------------------------------
619 void
SetValue(void * object,const void * value) const620 ResVertexLightMember::SetValue(void* object, const void* value) const
621 {
622     ResVertexLight light(object);
623     const ut::FloatColor& color = *reinterpret_cast<const ut::FloatColor*>(value);
624     float floatValue = *static_cast<const float*>(value);
625 
626     switch (GetMemberType())
627     {
628     case MEMBER_TYPE_AMBIENT:
629         light.SetAmbient(color);
630         break;
631 
632     case MEMBER_TYPE_DIFFUSE:
633         light.SetDiffuse(color);
634         break;
635 
636     case MEMBER_TYPE_DIRECTION:
637         {
638             math::VEC3 direction = *reinterpret_cast<const math::VEC3*>(value);
639             direction.Normalize();
640             light.SetDirection(direction);
641         }
642         break;
643 
644     case MEMBER_TYPE_DISTANCE_ATTENUATION_CONSTANT:
645         {
646             math::VEC3 attenuation = light.GetDistanceAttenuation();
647             attenuation.x = floatValue;
648             light.SetDistanceAttenuation(attenuation);
649         }
650         break;
651 
652     case MEMBER_TYPE_DISTANCE_ATTENUATION_LINEAR:
653         {
654             math::VEC3 attenuation = light.GetDistanceAttenuation();
655             attenuation.y = floatValue;
656             light.SetDistanceAttenuation(attenuation);
657         }
658         break;
659 
660     case MEMBER_TYPE_DISTANCE_ATTENUATION_QUADRATIC:
661         {
662             math::VEC3 attenuation = light.GetDistanceAttenuation();
663             attenuation.z = floatValue;
664             light.SetDistanceAttenuation(attenuation);
665         }
666         break;
667 
668     case MEMBER_TYPE_SPOT_EXPONENT:
669         {
670             math::VEC2 spotFactor = light.GetSpotFactor();
671             spotFactor.x = floatValue;
672             light.SetSpotFactor(spotFactor);
673         }
674         break;
675 
676     case MEMBER_TYPE_SPOT_CUTOFF_ANGLE:
677         {
678             math::VEC2 spotFactor = light.GetSpotFactor();
679             spotFactor.y = floatValue;
680             light.SetSpotFactor(spotFactor);
681         }
682         break;
683 
684     default:
685         NW_ASSERT(false);
686     }
687 }
688 
689 //----------------------------------------
690 void
SetValue(void * object,const void * value) const691 ResHemiSphereLightMember::SetValue(void* object, const void* value) const
692 {
693     ResHemiSphereLight light(object);
694     const ut::FloatColor& color = *reinterpret_cast<const ut::FloatColor*>(value);
695 
696     switch (GetMemberType())
697     {
698     case MEMBER_TYPE_GROUND_COLOR:
699         light.SetGroundColor(color);
700         break;
701 
702     case MEMBER_TYPE_SKY_COLOR:
703         light.SetSkyColor(color);
704         break;
705 
706     case MEMBER_TYPE_DIRECTION:
707         {
708             math::VEC3 direction = *reinterpret_cast<const math::VEC3*>(value);
709             direction.Normalize();
710             light.SetDirection(direction);
711         }
712         break;
713 
714     case MEMBER_TYPE_LERP_FACTOR:
715         {
716             float floatValue = *static_cast<const float*>(value);
717             light.SetLerpFactor(floatValue);
718         }
719         break;
720 
721     default:
722         NW_ASSERT(false);
723     }
724 }
725 
726 } // namespace res
727 } // namespace anim
728 } // namespace nw
729