00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00057 MStringArray uvSetNames;
00058 MayaStatusCheck(fnMesh.getUVSetNames(uvSetNames));
00059 int uvSetCount = uvSetNames.length();
00060
00061
00062 MObjectArray shaders;
00063 MIntArray shaderIndices;
00064
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
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
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
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
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
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 }
00264