1 /*---------------------------------------------------------------------------*
2 Project: NintendoWare
3 File: gfx_ResModel.cpp
4
5 Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc. All rights reserved.
6
7 These coded instructions, statements, and computer programs contain proprietary
8 information of Nintendo and/or its licensed developers and are protected by
9 national and international copyright laws. They may not be disclosed to third
10 parties or copied or duplicated in any form, in whole or in part, without the
11 prior written consent of Nintendo.
12
13 The content herein is highly confidential and should be handled accordingly.
14
15 $Revision: $
16 *---------------------------------------------------------------------------*/
17
18 #include "../precompiled.h"
19
20 #include <nw/ut/ut_ResUtil.h>
21 #include <nw/ut/ut_ResDictionary.h>
22 #include <nw/gfx/gfx_SceneObject.h>
23 #include <nw/gfx/res/gfx_ResShape.h>
24 #include <nw/gfx/res/gfx_ResModel.h>
25 #include <nw/gfx/res/gfx_ResMaterial.h>
26 #include <nw/gfx/res/gfx_ResGraphicsFile.h>
27 #include <nw/gfx/gfx_Common.h>
28 #include <GLES2/gl2.h>
29 #include <GLES2/gl2ext.h>
30
31 namespace nw {
32 namespace gfx {
33 namespace res {
34
35 //----------------------------------------------------------------------------
36 Result
Setup(os::IAllocator * allocator,ResGraphicsFile graphicsFile)37 ResModel::Setup(os::IAllocator* allocator, ResGraphicsFile graphicsFile)
38 {
39 Result result = RESOURCE_RESULT_OK;
40 NW_ASSERT( this->IsValid() );
41 NW_ASSERT( internal::ResCheckRevision( *this ) );
42 NW_ASSERT( internal::ResCheckRevision( graphicsFile ) );
43
44 s32 shapeNum = this->GetShapesCount();
45 for ( int i = 0; i < shapeNum; ++i )
46 {
47 if (!ut::CheckFlag(this->GetShapes(i).GetFlags(), ResShape::FLAG_HAS_BEEN_SETUP))
48 {
49 result |= this->GetShapes( i ).Setup(allocator);
50 }
51 }
52
53 s32 materialNum = this->GetMaterialsCount();
54 for ( int i = 0; i < materialNum; ++i )
55 {
56 if (!ut::CheckFlag(this->GetMaterials( i ).GetFlags(), ResMaterialData::FLAG_HAS_BEEN_SETUP))
57 {
58 result |= this->GetMaterials( i ).Setup(allocator, graphicsFile);
59 }
60 }
61
62 #if defined(NW_CALL_DRAW_ELEMENTS_MANUALLY)
63 // シェーダーの参照解決が終わるまでは頂点用のコマンド生成ができない。
64 if ( (result.GetDescription() & RESOURCE_RESULT_NOT_FOUND_SHADER) == 0 )
65 {
66 // 各メッシュに対して、頂点アトリビュートの設定コマンドを生成する。
67 s32 meshNum = this->GetMeshesCount();
68 for ( int i = 0; i < meshNum; ++ i )
69 {
70 ResMesh mesh = this->GetMeshes( i );
71 if (!ut::CheckFlag(mesh.GetFlags(), ResMesh::FLAG_HAS_BEEN_SETUP))
72 {
73 mesh.Setup( *this, allocator, graphicsFile );
74 }
75 }
76 }
77 #endif
78
79 return result;
80 }
81
82 //----------------------------------------------------------------------------
83 void
Cleanup()84 ResModel::Cleanup()
85 {
86 s32 shapeNum = this->GetShapesCount();
87 for ( int i = 0; i < shapeNum; ++i )
88 {
89 if (ut::CheckFlag(this->GetShapes(i).GetFlags(), ResShape::FLAG_HAS_BEEN_SETUP))
90 {
91 this->GetShapes( i ).Cleanup();
92 }
93 }
94
95 s32 materialNum = this->GetMaterialsCount();
96 for ( int i = 0; i < materialNum; ++i )
97 {
98 this->GetMaterials( i ).Cleanup();
99 }
100
101 s32 meshNum = this->GetMeshesCount();
102 for ( int i = 0; i < meshNum; ++ i )
103 {
104 this->GetMeshes( i ).Cleanup();
105 }
106 }
107
108 //----------------------------------------------------------------------------
109 void
ForceSetupTexture(const char * targetName,ResTexture texture)110 ResModel::ForceSetupTexture(const char* targetName, ResTexture texture)
111 {
112 NW_ASSERT(texture.IsValid());
113 NW_NULL_ASSERT(targetName);
114
115 const int materialNum = GetMaterialsCount();
116 for (int idx = 0; idx < materialNum; ++idx)
117 {
118 ResMaterial material = GetMaterials(idx);
119 if (material.IsValid())
120 {
121 material.ForceSetupTexture(targetName, texture);
122 }
123 }
124 }
125
126 //----------------------------------------------------------------------------
127 void
ForceSetupShader(const char * targetName,ResShader shader)128 ResModel::ForceSetupShader(const char* targetName, ResShader shader)
129 {
130 NW_ASSERT(shader.IsValid());
131 NW_NULL_ASSERT(targetName);
132
133 const int materialNum = GetMaterialsCount();
134 for (int idx = 0; idx < materialNum; ++idx)
135 {
136 ResMaterial material = GetMaterials(idx);
137 if (material.IsValid())
138 {
139 material.ForceSetupShader(targetName, shader);
140 }
141 }
142 }
143
144 //----------------------------------------------------------------------------
145 void
ForceSetupLookupTable(const char * targetName,ResLookupTable lut)146 ResModel::ForceSetupLookupTable(const char* targetName, ResLookupTable lut)
147 {
148 NW_ASSERT(lut.IsValid());
149 NW_NULL_ASSERT(targetName);
150
151 const int materialNum = GetMaterialsCount();
152 for (int idx = 0; idx < materialNum; ++idx)
153 {
154 ResMaterial material = GetMaterials(idx);
155 if (material.IsValid())
156 {
157 material.ForceSetupLookupTable(targetName, lut);
158 }
159 }
160 }
161
162 } /* namespace res */
163 } /* namespace gfx */
164 } /* namespace nw */
165
166