Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

Mesh.cpp

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Lamp : Open source game middleware
00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //------------------------------------------------------------------------------
00019 
00020 /** @file
00021  * メッシュ実装
00022  * @author Junpee
00023  */
00024 
00025 #include "LampBasic.h"
00026 #include "Graphics/Mesh/Mesh.h"
00027 #include "Graphics/Mesh/MeshManager.h"
00028 #include "Graphics/Model/Model.h"
00029 #include "Graphics/MeshData/MeshData.h"
00030 #include "Graphics/Material/Material.h"
00031 
00032 namespace Lamp{
00033 
00034 // プリミティブタイプ文字列テーブル
00035 const String Mesh::primitiveTypeStringTable[] = {
00036     "TriangleList",
00037     "IndexedTriangleList",
00038 };
00039 
00040 //------------------------------------------------------------------------------
00041 // コンストラクタ
00042 Mesh::Mesh(const String& name, Scene* scene) : SceneObject(name, scene),
00043     parent_(NULL), meshData_(NULL), material_(NULL),
00044     worldBoundingSphere_(Sphere::zero), worldBoundingBox_(AxisAlignedBox::zero),
00045     renderingTemporaryData_(0.f), enabled_(true), globalEnabled_(false){
00046 }
00047 //------------------------------------------------------------------------------
00048 // デストラクタ
00049 Mesh::~Mesh(){
00050 }
00051 //------------------------------------------------------------------------------
00052 // メッシュの値コピー
00053 void Mesh::copyMeshValue(Mesh* destination, u_int copyMask) const{
00054     // メンバのコピー
00055     destination->setEnabled(enabled_);
00056     // メッシュデータ
00057     if(meshData_ != NULL){
00058         if((copyMask & copyMeshData) == 0){
00059             // メッシュデータを共有する
00060             destination->setMeshData(meshData_);
00061         }else{
00062             // メッシュデータをコピーする
00063             destination->setMeshData(meshData_->copy());
00064         }
00065     }
00066     // マテリアル
00067     if(material_ != NULL){
00068         if((copyMask & copyMaterial) == 0){
00069             // マテリアルを共有する
00070             destination->setMaterial(material_);
00071         }else{
00072             // マテリアルをコピーする
00073             destination->setMaterial(material_->copy(copyMask));
00074         }
00075     }
00076 }
00077 //------------------------------------------------------------------------------
00078 // 再帰的破棄
00079 int Mesh::recursiveDestroy(Mesh* mesh){
00080     Assert(mesh != NULL);
00081     int result = 0;
00082     // メッシュデータの破棄
00083     MeshData* meshData = mesh->getMeshData();
00084     if(meshData != NULL){
00085         mesh->setMeshData(NULL);
00086         result += MeshData::destroy(meshData);
00087     }
00088     // マテリアルの破棄
00089     Material* material = mesh->getMaterial();
00090     if(material != NULL){
00091         mesh->setMaterial(NULL);
00092         result += Material::recursiveDestroy(material);
00093     }
00094     // 親から削除
00095     Model* parent = mesh->getParent();
00096     if(parent != NULL){ parent->removeMesh(mesh); }
00097     // 引数の破棄
00098     MeshManager* manager = mesh->getScene()->getMeshManager();
00099     if(manager->destroy(mesh) == 0){ result++; }
00100     return result;
00101 }
00102 //------------------------------------------------------------------------------
00103 // 走査
00104 void Mesh::traverse(const Matrix34& parentMatrix,
00105     bool parentEnabled, bool parentScaled, bool parentChanged){
00106     globalEnabled_ = parentEnabled && isEnabled();
00107     // parentEnabled(false) && isEnabled(true)の場合はバウンディングを計算する
00108     if(!isEnabled()){ return; }
00109     globalScaled_ = parentScaled;
00110     // 親に変更があったか、バウンディングに変更があれば再計算
00111     if(parentChanged || meshData_->isBoundingChanged()){
00112         // バウンディングボックス算出
00113         worldBoundingBox_ = getBoundingBox().transform(parentMatrix);
00114         if(globalScaled_){
00115             worldBoundingSphere_ =
00116                 getBoundingSphere().scaledTransform(parentMatrix);
00117         }else{
00118             worldBoundingSphere_ = getBoundingSphere().transform(parentMatrix);
00119         }
00120         meshData_->clearBoundingChanged();
00121     }
00122 }
00123 //------------------------------------------------------------------------------
00124 // バウンディングボックス
00125 //------------------------------------------------------------------------------
00126 // バウンディングボックスの設定
00127 void Mesh::setBoundingBox(const AxisAlignedBox& boundingBox){
00128     Assert(meshData_ != NULL);
00129     meshData_->setBoundingBox(boundingBox);
00130 }
00131 //------------------------------------------------------------------------------
00132 // バウンディングボックスの取得
00133 const AxisAlignedBox& Mesh::getBoundingBox() const{
00134     Assert(meshData_ != NULL);
00135     return meshData_->getBoundingBox();
00136 }
00137 //------------------------------------------------------------------------------
00138 // バウンディングスフィア
00139 //------------------------------------------------------------------------------
00140 // バウンディングスフィアの設定
00141 void Mesh::setBoundingSphere(const Sphere& boundingSphere){
00142     Assert(meshData_ != NULL);
00143     meshData_->setBoundingSphere(boundingSphere);
00144 }
00145 //------------------------------------------------------------------------------
00146 // バウンディングスフィアの取得
00147 const Sphere& Mesh::getBoundingSphere() const{
00148     Assert(meshData_ != NULL);
00149     return meshData_->getBoundingSphere();
00150 }
00151 //------------------------------------------------------------------------------
00152 // 中心の取得
00153 const Vector3& Mesh::getCenter() const{
00154     Assert(meshData_ != NULL);
00155     return meshData_->getBoundingSphere().getCenter();
00156 }
00157 //------------------------------------------------------------------------------
00158 // メッシュデータインターフェース
00159 //------------------------------------------------------------------------------
00160 // メッシュデータの設定
00161 void Mesh::setMeshData(MeshData* meshData){
00162     if(meshData_ != NULL){ meshData_->removeReference(this); }
00163     meshData_ = meshData;
00164     if(meshData_ != NULL){ meshData_->addReference(this); }
00165 }
00166 //------------------------------------------------------------------------------
00167 // マテリアルインターフェース
00168 //------------------------------------------------------------------------------
00169 // マテリアルの設定
00170 void Mesh::setMaterial(Material* material){
00171     if(material_ != NULL){ material_->removeReference(this); }
00172     material_ = material;
00173     if(material_ != NULL){ material_->addReference(this); }
00174 }
00175 //------------------------------------------------------------------------------
00176 // プリミティブタイプ
00177 //------------------------------------------------------------------------------
00178 // プリミティブタイプがインデックスを持つかどうか
00179 bool Mesh::primitiveTypeHasIndex(PrimitiveType primitiveType){
00180     if(primitiveType == Mesh::indexedTriangleList){
00181         return true;
00182     }
00183     return false;
00184 }
00185 //------------------------------------------------------------------------------
00186 // プリミティブタイプから文字列への変換
00187 String Mesh::primitiveTypeToString(PrimitiveType primitiveType){
00188     Assert(primitiveType >= 0);
00189     Assert(primitiveType < ptMax);
00190     return primitiveTypeStringTable[primitiveType];
00191 }
00192 //------------------------------------------------------------------------------
00193 // 文字列からプリミティブタイプへの変換
00194 Mesh::PrimitiveType Mesh::primitiveTypeFromString(
00195     const String& primitiveTypeString){
00196     for(int i = 0; i < ptMax; i++){
00197         if(primitiveTypeStringTable[i].equals(primitiveTypeString)){
00198             return PrimitiveType(i);
00199         }
00200     }
00201     ErrorOut("Mesh::primitiveTypeFromString() " + primitiveTypeString);
00202     return ptMax;
00203 }
00204 //-----------------------------------------------------------------------------
00205 // メッシュインターフェース
00206 //-----------------------------------------------------------------------------
00207 // プリミティブタイプの設定
00208 void Mesh::setPrimitiveType(Mesh::PrimitiveType primitiveType){
00209     getMeshData()->setPrimitiveType(primitiveType);
00210 }
00211 //-----------------------------------------------------------------------------
00212 // プリミティブタイプの取得
00213 Mesh::PrimitiveType Mesh::getPrimitiveType() const{
00214     return getMeshData()->getPrimitiveType();
00215 }
00216 //-----------------------------------------------------------------------------
00217 // プリミティブカウントの取得
00218 int Mesh::getPrimitiveCount() const{
00219     return getMeshData()->getPrimitiveCount();
00220 }
00221 //-----------------------------------------------------------------------------
00222 // 三角の取得
00223 Triangle Mesh::getTriangle(int index) const{
00224     return getMeshData()->getTriangle(index);
00225 }
00226 //-----------------------------------------------------------------------------
00227 // 頂点インデックスを持つかどうか
00228 bool Mesh::hasVertexIndices() const{
00229     return getMeshData()->hasVertexIndices();
00230 }
00231 //-----------------------------------------------------------------------------
00232 // 頂点インデックス数の設定
00233 void Mesh::setVertexIndexCount(int vertexIndexCount){
00234     getMeshData()->setVertexIndexCount(vertexIndexCount);
00235 }
00236 //-----------------------------------------------------------------------------
00237 // 頂点インデックス数の取得
00238 int Mesh::getVertexIndexCount() const{
00239     return getMeshData()->getVertexIndexCount();
00240 }
00241 //-----------------------------------------------------------------------------
00242 // 頂点インデックスの設定
00243 void Mesh::setVertexIndex(int index, int vertexIndex){
00244     getMeshData()->setVertexIndex(index, vertexIndex);
00245 }
00246 //-----------------------------------------------------------------------------
00247 // 頂点インデックスの取得
00248 int Mesh::getVertexIndex(int index) const{
00249     return getMeshData()->getVertexIndex(index);
00250 }
00251 //-----------------------------------------------------------------------------
00252 // 頂点インデックス配列の取得
00253 const u_short* Mesh::getVertexIndexArray(){
00254     return getMeshData()->getVertexIndexArray();
00255 }
00256 //-----------------------------------------------------------------------------
00257 // 頂点数の設定
00258 void Mesh::setVertexCount(int vertexCount){
00259     getMeshData()->setVertexCount(vertexCount);
00260 }
00261 //-----------------------------------------------------------------------------
00262 // 頂点数の取得
00263 int Mesh::getVertexCount() const{ return getMeshData()->getVertexCount(); }
00264 //-----------------------------------------------------------------------------
00265 // 位置の設定
00266 void Mesh::setPosition(int index, const Vector3& position){
00267     getMeshData()->setPosition(index, position);
00268 }
00269 //-----------------------------------------------------------------------------
00270 // 位置の取得
00271 const Vector3& Mesh::getPosition(int index) const{
00272     return getMeshData()->getPosition(index);
00273 }
00274 //-----------------------------------------------------------------------------
00275 // 位置配列の取得
00276 const Vector3* Mesh::getPositionArray() const{
00277     return getMeshData()->getPositionArray();
00278 }
00279 //-----------------------------------------------------------------------------
00280 // 法線を有効にするかどうか
00281 void Mesh::enableNormal(bool normalFlag){
00282     getMeshData()->enableNormal(normalFlag);
00283 }
00284 //-----------------------------------------------------------------------------
00285 // 法線が有効かどうか
00286 bool Mesh::hasNormal() const{ return getMeshData()->hasNormal(); }
00287 //-----------------------------------------------------------------------------
00288 // 法線の設定
00289 void Mesh::setNormal(int index, const Vector3& normal){
00290     getMeshData()->setNormal(index, normal);
00291 }
00292 //-----------------------------------------------------------------------------
00293 // 法線の取得
00294 const Vector3& Mesh::getNormal(int index) const{
00295     return getMeshData()->getNormal(index);
00296 }
00297 //-----------------------------------------------------------------------------
00298 // 法線配列の取得
00299 const Vector3* Mesh::getNormalArray() const{
00300     return getMeshData()->getNormalArray();
00301 }
00302 //-----------------------------------------------------------------------------
00303 // カラーを有効にするかどうか
00304 void Mesh::enableColor(bool colorFlag){
00305     getMeshData()->enableColor(colorFlag);
00306 }
00307 //-----------------------------------------------------------------------------
00308 // カラーが有効かどうか
00309 bool Mesh::hasColor() const{ return getMeshData()->hasColor(); }
00310 //-----------------------------------------------------------------------------
00311 // カラーの設定
00312 void Mesh::setColor(int index, const Color4c& color){
00313     getMeshData()->setColor(index, color);
00314 }
00315 //-----------------------------------------------------------------------------
00316 // カラーの取得
00317 const Color4c& Mesh::getColor(int index) const{
00318     return getMeshData()->getColor(index);
00319 }
00320 //-----------------------------------------------------------------------------
00321 // カラー配列の取得
00322 const Color4c* Mesh::getColorArray() const{
00323     return getMeshData()->getColorArray();
00324 }
00325 //-----------------------------------------------------------------------------
00326 // テクスチャ座標セット数の設定
00327 void Mesh::setTexCoordSetCount(int texCoordSetCount){
00328     getMeshData()->setTexCoordSetCount(texCoordSetCount);
00329 }
00330 //-----------------------------------------------------------------------------
00331 // テクスチャ座標セット数の設定
00332 int Mesh::getTexCoordSetCount() const{
00333     return getMeshData()->getTexCoordSetCount();
00334 }
00335 //-----------------------------------------------------------------------------
00336 // テクスチャ座標タイプの設定
00337 void Mesh::setTexCoordType(int texCoordSet, TexCoord::Type texCoordType){
00338     getMeshData()->setTexCoordType(texCoordSet, texCoordType);
00339 }
00340 //-----------------------------------------------------------------------------
00341 // テクスチャ座標タイプの取得
00342 TexCoord::Type Mesh::getTexCoordType(int texCoordSet) const{
00343     return getMeshData()->getTexCoordType(texCoordSet);
00344 }
00345 //-----------------------------------------------------------------------------
00346 // テクスチャ座標タイプ配列の取得
00347 const TexCoord::Type* Mesh::getTexCoordTypeArray() const{
00348     return getMeshData()->getTexCoordTypeArray();
00349 }
00350 //-----------------------------------------------------------------------------
00351 // テクスチャ座標の設定
00352 void Mesh::setTexCoord(
00353     int index, int texCoordSet, const float* texCoord, int numTexCoord){
00354     getMeshData()->setTexCoord(index, texCoordSet, texCoord, numTexCoord);
00355 }
00356 //-----------------------------------------------------------------------------
00357 // テクスチャ座標配列の取得
00358 const float* const* Mesh::getTexCoordArray() const{
00359     return getMeshData()->getTexCoordArray();
00360 }
00361 //-----------------------------------------------------------------------------
00362 // テクスチャ座標配列の取得
00363 const float* Mesh::getTexCoordArray(int texCoordSet) const{
00364     return getMeshData()->getTexCoordArray(texCoordSet);
00365 }
00366 //-----------------------------------------------------------------------------
00367 // テクスチャ座標配列サイズの取得
00368 int Mesh::getTexCoordArraySize(int texCoordSet) const{
00369     return getMeshData()->getTexCoordArraySize(texCoordSet);
00370 }
00371 //-----------------------------------------------------------------------------
00372 // 一次元テクスチャ座標の設定
00373 void Mesh::setTexCoord1(int index, int texCoordSet, const TexCoord1& texCoord){
00374     getMeshData()->setTexCoord1(index, texCoordSet, texCoord);
00375 }
00376 //-----------------------------------------------------------------------------
00377 // 一次元テクスチャ座標の取得
00378 const TexCoord1& Mesh::getTexCoord1(int index, int texCoordSet) const{
00379     return getMeshData()->getTexCoord1(index, texCoordSet);
00380 }
00381 //-----------------------------------------------------------------------------
00382 // 一次元テクスチャ座標配列の取得
00383 const TexCoord1* Mesh::getTexCoord1Array(int texCoordSet) const{
00384     return getMeshData()->getTexCoord1Array(texCoordSet);
00385 }
00386 //-----------------------------------------------------------------------------
00387 // 二次元テクスチャ座標の設定
00388 void Mesh::setTexCoord2(int index, int texCoordSet, const TexCoord2& texCoord){
00389     getMeshData()->setTexCoord2(index, texCoordSet, texCoord);
00390 }
00391 //-----------------------------------------------------------------------------
00392 // 二次元テクスチャ座標の取得
00393 const TexCoord2& Mesh::getTexCoord2(int index, int texCoordSet) const{
00394     return getMeshData()->getTexCoord2(index, texCoordSet);
00395 }
00396 //-----------------------------------------------------------------------------
00397 // 二次元テクスチャ座標配列の取得
00398 const TexCoord2* Mesh::getTexCoord2Array(int texCoordSet) const{
00399     return getMeshData()->getTexCoord2Array(texCoordSet);
00400 }
00401 //-----------------------------------------------------------------------------
00402 // 三次元テクスチャ座標の設定
00403 void Mesh::setTexCoord3(int index, int texCoordSet, const TexCoord3& texCoord){
00404     getMeshData()->setTexCoord3(index, texCoordSet, texCoord);
00405 }
00406 //-----------------------------------------------------------------------------
00407 // 三次元テクスチャ座標の取得
00408 const TexCoord3& Mesh::getTexCoord3(int index, int texCoordSet) const{
00409     return getMeshData()->getTexCoord3(index, texCoordSet);
00410 }
00411 //-----------------------------------------------------------------------------
00412 // 三次元テクスチャ座標配列の取得
00413 const TexCoord3* Mesh::getTexCoord3Array(int texCoordSet) const{
00414     return getMeshData()->getTexCoord3Array(texCoordSet);
00415 }
00416 //-----------------------------------------------------------------------------
00417 // 四次元テクスチャ座標の設定
00418 void Mesh::setTexCoord4(int index, int texCoordSet, const TexCoord4& texCoord){
00419     getMeshData()->setTexCoord4(index, texCoordSet, texCoord);
00420 }
00421 //-----------------------------------------------------------------------------
00422 // 四次元テクスチャ座標の取得
00423 const TexCoord4& Mesh::getTexCoord4(int index, int texCoordSet) const{
00424     return getMeshData()->getTexCoord4(index, texCoordSet);
00425 }
00426 //-----------------------------------------------------------------------------
00427 // 四次元テクスチャ座標配列の取得
00428 const TexCoord4* Mesh::getTexCoord4Array(int texCoordSet) const{
00429     return getMeshData()->getTexCoord4Array(texCoordSet);
00430 }
00431 //-----------------------------------------------------------------------------
00432 // 頂点あたりボーン数の設定
00433 void Mesh::setBonesPerVertex(int bonesPerVertex){
00434     getMeshData()->setBonesPerVertex(bonesPerVertex);
00435 }
00436 //-----------------------------------------------------------------------------
00437 // 頂点あたりボーン数の取得
00438 int Mesh::getBonesPerVertex() const{
00439     return getMeshData()->getBonesPerVertex();
00440 }
00441 //-----------------------------------------------------------------------------
00442 // ボーンインデックスが有効かどうか
00443 bool Mesh::hasBoneIndex() const{ return getMeshData()->hasBoneIndex(); }
00444 //-----------------------------------------------------------------------------
00445 // ボーンインデックスの設定
00446 void Mesh::setBoneIndex(
00447     int vertexIndex, int weightIndex, u_char boneIndex){
00448     getMeshData()->setBoneIndex(
00449         vertexIndex, weightIndex, boneIndex);
00450 }
00451 //-----------------------------------------------------------------------------
00452 // ボーンインデックスの設定
00453 void Mesh::setBoneIndex(int vertexIndex, u_char boneIndex){
00454     getMeshData()->setBoneIndex(vertexIndex, boneIndex);
00455 }
00456 //-----------------------------------------------------------------------------
00457 // ボーンインデックスの取得
00458 u_char Mesh::getBoneIndex(int vertexIndex, int weightIndex) const{
00459     return getMeshData()->getBoneIndex(vertexIndex, weightIndex);
00460 }
00461 //-----------------------------------------------------------------------------
00462 // ボーンインデックスの取得
00463 u_char Mesh::getBoneIndex(int vertexIndex) const{
00464     return getMeshData()->getBoneIndex(vertexIndex);
00465 }
00466 //-----------------------------------------------------------------------------
00467 // ボーンインデックス配列の取得
00468 const u_char* Mesh::getBoneIndexArray() const{
00469     return getMeshData()->getBoneIndexArray();
00470 }
00471 //-----------------------------------------------------------------------------
00472 // 頂点当たりウェイト数の取得
00473 int Mesh::getWeightsPerVertex() const{
00474     return getMeshData()->getWeightsPerVertex();
00475 }
00476 //-----------------------------------------------------------------------------
00477 // ウェイトが有効かどうか
00478 bool Mesh::hasWeight() const{ return getMeshData()->hasWeight(); }
00479 //-----------------------------------------------------------------------------
00480 // ウェイトの設定
00481 void Mesh::setWeight(int vertexIndex, int weightIndex, float weight){
00482     getMeshData()->setWeight(vertexIndex, weightIndex, weight);
00483 }
00484 //-----------------------------------------------------------------------------
00485 // ウェイトの取得
00486 float Mesh::getWeight(int vertexIndex, int weightIndex) const{
00487     return getMeshData()->getWeight(vertexIndex, weightIndex);
00488 }
00489 //-----------------------------------------------------------------------------
00490 // ウェイト配列の取得
00491 const float* Mesh::getWeightArray() const{
00492     return getMeshData()->getWeightArray();
00493 }
00494 //-----------------------------------------------------------------------------
00495 // バッファアクセス
00496 //-----------------------------------------------------------------------------
00497 // インデックスバッファの取得
00498 Direct3DIndexBuffer* Mesh::getIndexBuffer(){
00499     return getMeshData()->getIndexBuffer();
00500 }
00501 //-----------------------------------------------------------------------------
00502 // 頂点記述の取得
00503 Direct3DVertexDeclaration* Mesh::getVertexDeclaration(){
00504     return getMeshData()->getVertexDeclaration();
00505 }
00506 //-----------------------------------------------------------------------------
00507 // 頂点サイズの取得
00508 int Mesh::getVertexSize(){ return getMeshData()->getVertexSize(); }
00509 //-----------------------------------------------------------------------------
00510 // 頂点バッファの構築
00511 Direct3DVertexBuffer* Mesh::getVertexBuffer(){
00512     return getMeshData()->getVertexBuffer();
00513 }
00514 //------------------------------------------------------------------------------
00515 } // End of namespace Lamp
00516 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:33 2005 for Lamp by doxygen 1.3.2