/*---------------------------------------------------------------------------* Project: NintendoWare File: demo_Particle.cpp Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Revision: 26050 $ *---------------------------------------------------------------------------*/ #include #include #include namespace nw { namespace demo { NW_UT_RUNTIME_TYPEINFO_DEFINITION(ParticleNode, gfx::TransformNode); //---------------------------------------- ParticleNode::ParticleNode( os::IAllocator* allocator, gfx::ResTransformNode resObj, const gfx::TransformNode::Description& description) : gfx::TransformNode(allocator, resObj, description), m_ModelInstances(allocator), m_EmitterInstances(allocator) { } //---------------------------------------- ParticleNode* ParticleNode::DynamicBuilder::Create( os::IAllocator* allocator ) { NW_NULL_ASSERT(allocator); void* memory = allocator->Alloc(sizeof(ParticleNode)); if (memory == NULL) { return NULL; } ParticleNode* node = new(memory) ParticleNode( allocator, nw::gfx::ResTransformNode(), m_Description); { gfx::Result result = node->Initialize(allocator); if (!result.IsSuccess()) { SafeDestroy(node); return NULL; } } return node; } //-------------------------------------------------------------------------- ParticleEffect* ParticleEffect::Create(os::IAllocator* mainAllocator, os::IAllocator* deviceAllocator, bool autoAlocate, gfx::ParticleContext* particleContext) { NW_NULL_ASSERT( mainAllocator ); void* memory = mainAllocator->Alloc( sizeof(ParticleEffect) ); NW_ASSERT( memory != NULL ); ParticleEffect* particleEffect = new(memory) ParticleEffect(mainAllocator, deviceAllocator, autoAlocate, particleContext); return particleEffect; } //-------------------------------------------------------------------------- void ParticleEffect::Destroy() { os::IAllocator* allocator = this->m_MainAllocator; NW_ASSERT( allocator != NULL ); this->~ParticleEffect(); allocator->Free(this); } //---------------------------------------- void ParticleEffect::Setup(gfx::ResGraphicsFile resource, bool useParticleMaterial) { resource.ForeachTexture(nw::gfx::TextureLocationFlagSetter(NN_GX_MEM_VRAMA | GL_NO_COPY_FCRAM_DMP)); // TODO:パーティクルマテリアルをコミットしたら復帰する。 NW_UNUSED_VARIABLE(useParticleMaterial); // パーティクルマテリアルを使用します。 if (useParticleMaterial) { resource.ForeachModelMaterial(nw::gfx::ParticleMaterialFlagSetter()); } nw::gfx::Result result = resource.Setup(m_DeviceAllocator); if (result.IsFailure()) { NW_FATAL_ERROR("Fail to set up model. A result code is 0x%x", result.GetCode()); } } //---------------------------------------- void ParticleEffect::Register(gfx::ResGraphicsFile resource, const char** nodeNames) { nw::gfx::ResModelArray models = resource.GetModels(); nw::gfx::ResModelArray::iterator modelsEnd = models.end(); for (nw::gfx::ResModelArray::iterator modelResource = models.begin(); modelResource != modelsEnd; ++modelResource) { const char *resourceName = (*modelResource).GetName(); const char** name = nodeNames; bool find = false; while (!find && *name != NULL) { if (strcmp(resourceName, *name) == 0) { find = true; } ++name; } if (find) { m_ResModels.PushBack(nw::gfx::ResDynamicCast(*modelResource)); } } nw::gfx::ResEmitterArray emitters = resource.GetEmitters(); for (nw::gfx::ResEmitterArray::iterator emitterResource = emitters.begin(); emitterResource != emitters.end(); ++emitterResource) { const char *resourceName = (*emitterResource).GetName(); const char** name = nodeNames; bool find = false; while (!find && *name != NULL) { if (strcmp(resourceName, *name) == 0) { find = true; } ++name; } if (find) { m_ResEmitters.PushBack(*emitterResource); } } } //---------------------------------------- void ParticleEffect::Register(gfx::ResGraphicsFile resource) { nw::gfx::ResModelArray models = resource.GetModels(); nw::gfx::ResModelArray::iterator modelsEnd = models.end(); for (nw::gfx::ResModelArray::iterator modelResource = models.begin(); modelResource != modelsEnd; ++modelResource) { nw::ut::ResTypeInfo resTypeInfo = (*modelResource).GetTypeInfo(); if ( resTypeInfo == nw::gfx::ResParticleModel::TYPE_INFO) { m_ResModels.PushBack(nw::gfx::ResDynamicCast(*modelResource)); } } nw::gfx::ResEmitterArray emitters = resource.GetEmitters(); for (nw::gfx::ResEmitterArray::iterator emitterResource = emitters.begin(); emitterResource != emitters.end(); ++emitterResource) { m_ResEmitters.PushBack(*emitterResource); } } ParticleNode* ParticleEffect::Allocate() { // トップノード生成 ParticleNode* top = ParticleNode::DynamicBuilder() .MaxCallbacks(0) .MaxChildren(this->m_ResModels.Size() + this->m_ResEmitters.Size()) .Create(this->m_MainAllocator); top->SetID(this->m_NextId++); nw::ut::MoveArray sceneNodeArray(this->m_MainAllocator); // モデル生成 NW_FOREACH(const nw::gfx::ResParticleModel resModel, this->m_ResModels) { nw::gfx::SceneObject* sceneObject = nw::gfx::SceneBuilder() .Resource(resModel) .CreateObject(this->m_MainAllocator, this->m_DeviceAllocator); nw::gfx::SceneNode* node = nw::ut::DynamicCast(sceneObject); sceneNodeArray.PushBack(node); nw::gfx::ParticleModel* model = nw::ut::DynamicCast(node); top->RegisterParticleModel(model); } // エミッタ生成 NW_FOREACH(const nw::gfx::ResParticleEmitter resEmitter, this->m_ResEmitters) { nw::gfx::SceneObject* sceneObject = nw::gfx::SceneBuilder() .Resource(resEmitter) .CreateObject(this->m_MainAllocator, this->m_DeviceAllocator); nw::gfx::SceneNode* node = nw::ut::DynamicCast(sceneObject); sceneNodeArray.PushBack(node); nw::gfx::ParticleEmitter* emitter = nw::ut::DynamicCast(node); top->RegisterParticleEmitter(emitter); } // 階層再構築 nw::gfx::SceneHelper::ResolveReference(sceneNodeArray); // パーティクルの参照解決・初期化 nw::gfx::ParticleUtil::SetupParticleObject(&sceneNodeArray, this->m_ParticleContext); // トップノードにアタッチ NW_FOREACH(nw::gfx::SceneNode* node, sceneNodeArray) { if (node->GetParent() == NULL) { top->AttachChild(node); } } return top; } void ParticleEffect::DestroyParticleNode(ParticleNode* node) { NW_NULL_ASSERT(node); nw::gfx::SafeDestroyBranch(node); } } // namespace demo } // namespace nw