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

TranslationStandardModel.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 "System/stdafx.h"
00026 #include "Translator/Model/TranslationStandardModel.h"
00027 #include "Translator/Mesh/TranslationMeshManager.h"
00028 #include "Graphics/Scene/Scene.h"
00029 #include "Graphics/Model/ModelManager.h"
00030 #include "Graphics/Mesh/MeshManager.h"
00031 
00032 namespace LampForMaya{
00033 
00034 //------------------------------------------------------------------------------
00035 // コンストラクタ
00036 TranslationStandardModel::TranslationStandardModel(
00037     const MObject& initializeObject, const String& initializeName) :
00038     TranslationModel(initializeObject, initializeName), transMeshes_(NULL){
00039 }
00040 //------------------------------------------------------------------------------
00041 // デストラクタ
00042 TranslationStandardModel::~TranslationStandardModel(){
00043     SafeArrayDelete(transMeshes_);
00044 }
00045 //------------------------------------------------------------------------------
00046 // 分析
00047 bool TranslationStandardModel::analyze(TranslationMeshManager* meshManager){
00048     // モデルの分析
00049     if(!analyzeModel()){ return false; }
00050 
00051     MStatus result;
00052     String errorString;
00053     MFnMesh fnMesh(object_, &result);
00054     MayaStatusCheck(result);
00055 
00056     // UVセット数の取得
00057     MStringArray uvSetNames;
00058     MayaStatusCheck(fnMesh.getUVSetNames(uvSetNames));
00059     int uvSetCount = uvSetNames.length();
00060 
00061     // シェーダ情報の取得
00062     MObjectArray shaders;
00063     MIntArray shaderIndices;
00064     // 常にインスタンス番号0番のモデルデータを使用する
00065     result = fnMesh.getConnectedShaders(0, shaders, shaderIndices);
00066     MayaStatusCheck(result);
00067 
00068     // シェーダが割り当てられていないポリゴンがあればエラー
00069     for(u_int i = 0; i < shaderIndices.length(); i++){
00070         if(shaderIndices[i] == -1){
00071             MIntArray vertexIndex;
00072             MayaStatusCheck(fnMesh.getPolygonVertices(i, vertexIndex));
00073             int vertexCount = vertexIndex.length();
00074             errorString.format("TranslationStandardModel::analyze() "
00075                 "%sにシェーダの割り当てられいないポリゴンがあります ",
00076                 name_.getBytes());
00077             for(int i = 0; i < vertexCount; i++){
00078                 MPoint position;
00079                 MayaStatusCheck(fnMesh.getPoint(vertexIndex[i], position));
00080                 String temp;
00081                 temp.format(" ( %.3f , %.3f , %.3f)",
00082                     position.x, position.y, position.z);
00083                 errorString += temp;
00084             }
00085             MayaErrorOut(errorString);
00086             return false;
00087         }
00088     }
00089 
00090     // シェーダの数だけメッシュを用意する
00091     int meshCount = shaders.length();
00092     int nameCount = 0;
00093     transMeshes_ = new TranslationRigidMesh*[meshCount];
00094     for(int i = 0; i < meshCount; i++){
00095         // 重複の無い名前を作成
00096         String meshName;
00097         while(true){
00098             meshName.format("%sM%d", name_.getBytes(), nameCount);
00099             nameCount++;
00100             TranslationMesh* exist = meshManager->search(meshName);
00101             if(exist == NULL){ break; }
00102         }
00103         transMeshes_[i] = meshManager->createRigidMesh(meshName);
00104         // モデルにメッシュを追加する
00105         meshes_.add(transMeshes_[i]);
00106         // UVセット数を設定する
00107         transMeshes_[i]->setUVSetCount(uvSetCount);
00108         // シェーダ名を設定する
00109         transMeshes_[i]->setMaterialName(getShaderName(shaders[i]));
00110     }
00111 
00112     // ポリゴンをメッシュに割り振る
00113     MItMeshPolygon polygonIterator(object_, &result);
00114     MayaStatusCheck(result);
00115     int polygonCount = 0;
00116     for( ; !polygonIterator.isDone(); polygonIterator.next()){
00117         int shaderIndex = shaderIndices[polygonCount];
00118         polygonCount++;
00119         TranslationRigidMesh* mesh = transMeshes_[shaderIndex];
00120 
00121         // 位置
00122         MPointArray positions;
00123         polygonIterator.getPoints(positions, MSpace::kObject, &result);
00124         MayaStatusCheck(result);
00125         bool isSquare = (positions.length() == 4);
00126         if((positions.length() != 3) && (!isSquare)){
00127             errorString.format("TranslationStandardModel::analyze() "
00128                 "%sが三角、四角以外のポリゴン(%d角形)を持っています\n",
00129                 name_.getBytes(), positions.length());
00130             for(u_int i = 0; i < positions.length(); i++){
00131                 String temp;
00132                 temp.format(" %d ( %.3f , %.3f , %.3f)",
00133                     i, positions[i].x, positions[i].y, positions[i].z);
00134                 errorString += temp;
00135             }
00136             MayaErrorOut(errorString);
00137             return false;
00138         }
00139         mesh->addPosition(Vector3((float)positions[0].x,
00140             (float)positions[0].y, (float)positions[0].z));
00141         mesh->addPosition(Vector3((float)positions[1].x,
00142             (float)positions[1].y, (float)positions[1].z));
00143         mesh->addPosition(Vector3((float)positions[2].x,
00144             (float)positions[2].y, (float)positions[2].z));
00145         if(isSquare){
00146             mesh->addPosition(Vector3((float)positions[0].x,
00147                 (float)positions[0].y, (float)positions[0].z));
00148             mesh->addPosition(Vector3((float)positions[2].x,
00149                 (float)positions[2].y, (float)positions[2].z));
00150             mesh->addPosition(Vector3((float)positions[3].x,
00151                 (float)positions[3].y, (float)positions[3].z));
00152         }
00153 
00154         // 法線
00155         MVectorArray normals;
00156         result = polygonIterator.getNormals(normals, MSpace::kObject);
00157         MayaStatusCheck(result);
00158         mesh->addNormal(Vector3(
00159             (float)normals[0].x, (float)normals[0].y, (float)normals[0].z));
00160         mesh->addNormal(Vector3(
00161             (float)normals[1].x, (float)normals[1].y, (float)normals[1].z));
00162         mesh->addNormal(Vector3(
00163             (float)normals[2].x, (float)normals[2].y, (float)normals[2].z));
00164         if(isSquare){
00165             mesh->addNormal(Vector3(
00166                 (float)normals[0].x, (float)normals[0].y, (float)normals[0].z));
00167             mesh->addNormal(Vector3(
00168                 (float)normals[2].x, (float)normals[2].y, (float)normals[2].z));
00169             mesh->addNormal(Vector3(
00170                 (float)normals[3].x, (float)normals[3].y, (float)normals[3].z));
00171         }
00172 
00173         // カラー
00174         bool hasColor = polygonIterator.hasColor(&result);
00175         MayaStatusCheck(result);
00176         if(hasColor){
00177             MColorArray colors;
00178             result = polygonIterator.getColors(colors);
00179             MayaStatusCheck(result);
00180             mesh->addColor(Color4f(
00181                 colors[0].r, colors[0].g, colors[0].b, colors[0].a));
00182             mesh->addColor(Color4f(
00183                 colors[1].r, colors[1].g, colors[1].b, colors[1].a));
00184             mesh->addColor(Color4f(
00185                 colors[2].r, colors[2].g, colors[2].b, colors[2].a));
00186             if(isSquare){
00187                 mesh->addColor(Color4f(
00188                     colors[0].r, colors[0].g, colors[0].b, colors[0].a));
00189                 mesh->addColor(Color4f(
00190                     colors[2].r, colors[2].g, colors[2].b, colors[2].a));
00191                 mesh->addColor(Color4f(
00192                     colors[3].r, colors[3].g, colors[3].b, colors[3].a));
00193             }
00194         }
00195 
00196         // UV
00197         MFloatArray uArray, vArray;
00198         for(int i = 0; i < uvSetCount; i++){
00199             result = polygonIterator.getUVs(uArray, vArray, &uvSetNames[i]);
00200             if(!result){
00201                 errorString.format("TranslationStandardModel::analyze "
00202                     "UVの取得に失敗しました %s %s (%.2f, %.2f, %.2f) %s",
00203                     name_.getBytes(), uvSetNames[i].asChar(),
00204                     positions[0].x, positions[0].y, positions[0].z,
00205                     result.errorString().asChar());
00206                 MayaErrorOut(errorString);
00207                 return false;
00208             }
00209             // V座標を反転する
00210             mesh->addUV(TexCoord2(uArray[0], (vArray[0] - 1.f) * -1.f));
00211             mesh->addUV(TexCoord2(uArray[1], (vArray[1] - 1.f) * -1.f));
00212             mesh->addUV(TexCoord2(uArray[2], (vArray[2] - 1.f) * -1.f));
00213         }
00214         if(isSquare){
00215             for(int i = 0; i < uvSetCount; i++){
00216                 result = polygonIterator.getUVs(uArray, vArray, &uvSetNames[i]);
00217                 if(!result){
00218                     errorString.format("TranslationStandardModel::analyze "
00219                         "UVの取得に失敗しました %s %s (%.2f, %.2f, %.2f) %s",
00220                         name_.getBytes(), uvSetNames[i].asChar(),
00221                         positions[0].x, positions[0].y, positions[0].z,
00222                         result.errorString().asChar());
00223                     MayaErrorOut(errorString);
00224                     return false;
00225                 }
00226                 // V座標を反転する
00227                 mesh->addUV(TexCoord2(uArray[0], (vArray[0] - 1.f) * -1.f));
00228                 mesh->addUV(TexCoord2(uArray[2], (vArray[2] - 1.f) * -1.f));
00229                 mesh->addUV(TexCoord2(uArray[3], (vArray[3] - 1.f) * -1.f));
00230             }
00231         }
00232     }
00233 
00234     // メッシュの論理チェック
00235     for(int i = 0; i < meshCount; i++){
00236         if(!transMeshes_[i]->logicalCheck()){ return false; }
00237     }
00238     return true;
00239 }
00240 //------------------------------------------------------------------------------
00241 // Lampへの変換
00242 bool TranslationStandardModel::convertToLamp(Scene* scene){
00243     ModelManager* modelManager = scene->getModelManager();
00244     StandardModel* model = modelManager->createStandardModel(name_);
00245     // 有効、無効フラグ
00246     model->setEnabled(visibility_);
00247     // メッシュとのリンク
00248     MeshManager* meshManager = scene->getMeshManager();
00249     int meshCount = meshes_.getCount();
00250     for(int i = 0; i < meshCount; i++){
00251         String meshName = meshes_.get(i)->getName();
00252         Mesh* mesh = meshManager->search(meshName);
00253         if(mesh == NULL){
00254             MayaErrorOut(String("TranslationStandardModel::convertToLamp() "
00255                 "メッシュが見つかりません ") + meshName);
00256             return false;
00257         }
00258         model->addMesh(mesh);
00259     }
00260     return true;
00261 }
00262 //------------------------------------------------------------------------------
00263 } // End of namespace LampForMaya
00264 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:56 2005 for LampForMaya by doxygen 1.3.2