/*---------------------------------------------------------------------------* Project: NintendoWare File: LytCtrl.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: 1 $ *---------------------------------------------------------------------------*/ #include #include //#include #include #include #include #include "../include/LytCtrl.h" namespace { /*---------------------------------------------------------------------------* @brief グラフィックスの初期設定を行います。 *---------------------------------------------------------------------------*/ void InitGX() { #ifdef NW_PLATFORM_CTR GLenum curCmdGenMode = 0; nngxGetCommandGenerationMode(&curCmdGenMode); if (curCmdGenMode != NN_GX_CMDGEN_MODE_UNCONDITIONAL) { // NN_GX_STATE_OTHER ステートのコマンドをただちに発行させます。 nngxSetCommandGenerationMode(NN_GX_CMDGEN_MODE_UNCONDITIONAL); } #endif glClearColor(0.2f, 0.2f, 0.2f, 0.0f); glDisable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); glDisable(GL_SCISSOR_TEST); glDisable(GL_STENCIL_TEST); glDisable(GL_POLYGON_OFFSET_FILL); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glDepthMask(GL_TRUE); #ifdef NW_PLATFORM_CTR glDisable(GL_EARLY_DEPTH_TEST_DMP); #endif #ifdef NW_PLATFORM_CTR // コマンド発行モードを元に戻します。 if (curCmdGenMode != NN_GX_CMDGEN_MODE_UNCONDITIONAL) { nngxSetCommandGenerationMode(curCmdGenMode); } // NN_GX_STATE_OTHERS 以外のステートはバリデートによりコマンドを発行します。 const GLbitfield stateMask = NN_GX_STATE_TRIOFFSET | NN_GX_STATE_SCISSOR | NN_GX_STATE_FBACCESS; nngxUpdateState(stateMask); NW_LYT_DRAWER_VALIDATE(stateMask); #endif NW_GL_ASSERT(); } /*!--------------------------------------------------------------------------* @brief コンストラクタ。 *---------------------------------------------------------------------------*/ LytCtrl::LytCtrl() { m_ResourcesArray.clear(); // シェーダファイル等のロード { m_GraphicsResource.StartSetup(); const wchar_t* resourcePath = 0; for (int i = 0; (resourcePath = m_GraphicsResource.GetResourcePath(i)) != NULL; ++i) { // todo:ここでロードされたメモリの行方 SmFile file; if (!file.Read(resourcePath, m_Allocator)) { NW_FATAL_ERROR("can not read lyt resource file."); } m_GraphicsResource.SetResource(i, file.Buffer(), file.Size()); } m_GraphicsResource.FinishSetup(); m_DrawInfo.SetGraphicsResource( &m_GraphicsResource ); } // Drawerの初期化 m_Drawer.Initialize( m_GraphicsResource ); } /*!--------------------------------------------------------------------------* @brief デストラクタ。 *---------------------------------------------------------------------------*/ LytCtrl::~LytCtrl() { uint i = 0; // SmLayout インスタンスを破棄します。 for ( i=0; iAttach(resSet.lytFile.Buffer(), ".")) { NW_FATAL_ERROR("can not attach layout archive.\n"); } // レイアウトリソースクラスを生成します。 smLytResource = new SmLayoutResource(); // nw::lyt::Layoutを生成します。 const void* lytRes = resSet.resAccessor->GetResource( 0, lytResFile.clyt.c_str() ); NW_NULL_ASSERT( lytRes ); if ( !smLytResource->InitLayout( lytRes, resSet.resAccessor ) ) { NW_FATAL_ERROR("can not setup layout archive.\n"); } // アニメーションリソースの読み込みます。 for ( uint i = 0; i < lytResFile.clan.size(); i++ ) { const void* lpaRes = resSet.resAccessor->GetResource( 0, lytResFile.clan[i].c_str() ); if ( lpaRes ) { if ( !smLytResource->AddAnimation( lpaRes, resSet.resAccessor ) ) { NW_FATAL_ERROR("can not setup layout animation archive.\n"); } } } isPushed = m_SmLytResourceArray.push_back( smLytResource ); NW_ASSERT(isPushed); isPushed = m_ResourcesArray.push_back( resSet ); NW_ASSERT(isPushed); // カメラのセットアップ setupCamera( smLytResource->GetLayout()->GetLayoutRect() ); return smLytResource; } /*!--------------------------------------------------------------------------* @brief レイアウトリソースクラスのインスタンスを引数に、SmLayoutクラスのインスタンスを生成します。 *---------------------------------------------------------------------------*/ SmLayout* LytCtrl::CreateSmLayout( SmLayoutResource* layoutResource ) { SmLayout* smLyt = new SmLayout( layoutResource ); NW_NULL_ASSERT( smLyt ); // 生成したインスタンスを保持します。 bool isPushed = m_SmLytInstanceArray.push_back( smLyt ); NW_ASSERT(isPushed); return smLyt; } /*!--------------------------------------------------------------------------* @brief 保持するSmLayoutクラスの描画を行います。 *---------------------------------------------------------------------------*/ void LytCtrl::Render() { if ( m_SmLytInstanceArray.empty() ) return; InitGX(); NW_LYT_DRAWER_VALIDATE(NN_GX_STATE_ALL); // 保持しているSmLaytouの配列分だけエントリを行います。 for ( uint i = 0; i < m_SmLytInstanceArray.size(); i++ ) { SmLayout* pLayout = m_SmLytInstanceArray[i]; if ( pLayout->GetVisible() ) { m_Drawer.DrawBegin( m_DrawInfo ); // レイアウトへデータを反映する if ( pLayout->SetupLayout() ) { SmLayoutResource* smLayoutRes = pLayout->GetLayoutResource(); nw::lyt::Layout* layout = smLayoutRes->GetLayout(); layout->Animate(); layout->CalculateMtx( m_DrawInfo ); m_Drawer.Draw( layout, m_DrawInfo ); m_Drawer.DrawEnd( m_DrawInfo ); pLayout->ResetLayout(); } } } nngxUpdateState(NN_GX_STATE_ALL); } /*!--------------------------------------------------------------------------* @brief m_DrawInfoの初期化を行います。 *---------------------------------------------------------------------------*/ void LytCtrl::setupCamera( nw::ut::Rect rect ) { f32 znear = 0.f; f32 zfar = 500.f; // 射影行列を正射影に設定 // (Layoutデータは横向きなので向きを変換する) nw::math::MTX44 projMtx; nw::math::MTX44OrthoPivot( &projMtx, rect.left, // left rect.right, // right rect.bottom, // bottom rect.top, // top znear, zfar, nw::math::PIVOT_UPSIDE_TO_TOP); m_DrawInfo.SetProjectionMtx(projMtx); // モデルビュー行列を設定 // (Layoutデータは横向きなので画面の上方向はレイアウトの-X方向) nw::math::VEC3 pos(0, 0, 1); nw::math::VEC3 up(0, 1, 0); nw::math::VEC3 target(0, 0, 0); nw::math::MTX34 viewMtx; nw::math::MTX34LookAt(&viewMtx, &pos, &up, &target); m_DrawInfo.SetViewMtx(viewMtx); } } // namespace