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/Mesh/TranslationCharacterMesh.h"
00027 #include "Graphics/Scene/Scene.h"
00028 #include "Graphics/Mesh/MeshManager.h"
00029 #include "Graphics/MeshData/MeshDataManager.h"
00030 #include "Graphics/Material/MaterialManager.h"
00031
00032 namespace LampForMaya{
00033
00034
00035
00036 TranslationCharacterMesh::TranslationCharacterMesh(
00037 const String& initializeName) :
00038 TranslationMesh(initializeName), maxWeightCount_(0),
00039 weights_(NULL), boneIndices_(NULL){
00040 }
00041
00042
00043 TranslationCharacterMesh::~TranslationCharacterMesh(){
00044 SafeArrayDelete(weights_);
00045 SafeArrayDelete(boneIndices_);
00046 }
00047
00048
00049 bool TranslationCharacterMesh::setWeights(
00050 int boneCount, float* weights, int* weightCounts){
00051
00052 maxWeightCount_ = 0;
00053 for(int i = 0; i < indices_.getCount(); i++){
00054 int index = indices_[i];
00055 if(weightCounts[indices_[i]] > maxWeightCount_){
00056 maxWeightCount_ = weightCounts[indices_[i]];
00057 }
00058 }
00059 if(maxWeightCount_ == 0){
00060 MayaErrorOut("TranslationCharacterMesh::setWeights() "
00061 "Weightがありません。");
00062 }
00063
00064
00065 weights_ = new float[indices_.getCount() * maxWeightCount_];
00066 boneIndices_ = new int[indices_.getCount() * maxWeightCount_];
00067
00068 for(int i = 0; i < indices_.getCount(); i++){
00069 int destinationOffset = i * maxWeightCount_;
00070 int index = indices_.get(i);
00071 int sourceOffset = index * boneCount;
00072 int writeCount = 0;
00073
00074 for(int j = 0; j < boneCount; j++){
00075 if(weights[sourceOffset + j] != 0.f){
00076 weights_[destinationOffset + writeCount] =
00077 weights[sourceOffset + j];
00078 boneIndices_[destinationOffset + writeCount] = j;
00079 writeCount++;
00080 }
00081 }
00082
00083 for(int j = writeCount; j < maxWeightCount_; j++){
00084 weights_[destinationOffset + j] = 0.f;
00085 boneIndices_[destinationOffset + j] = 0;
00086 }
00087 }
00088 return true;
00089 }
00090
00091
00092 bool TranslationCharacterMesh::logicalCheck(){
00093 if(!vertexLogicalCheck()){ return false; }
00094 String errorString;
00095 int vertexCount = positions_.getCount();
00096
00097 if(indices_.getCount() != vertexCount){
00098 errorString.format("TranslationCharacterMesh::logicalCheck() "
00099 "%sの頂点数(%d)とインデックス数(%d)が違います",
00100 name_.getBytes(), vertexCount, indices_.getCount());
00101 MayaErrorOut(errorString);
00102 return false;
00103 }
00104 return true;
00105 }
00106
00107
00108 bool TranslationCharacterMesh::convertToLamp(Scene* scene){
00109
00110 Mesh* mesh = convertCharacterMesh(scene);
00111
00112 MaterialManager* materialManager = scene->getMaterialManager();
00113 Material* material = materialManager->search(materialName_);
00114 if(material == NULL){
00115 MayaErrorOut(String("TranslationCharacterMesh::convertToLamp() ") +
00116 name_ + "に接続されているマテリアル" + materialName_ +
00117 "が見つかりません ");
00118 return false;
00119 }
00120 mesh->setMaterial(material);
00121 return true;
00122 }
00123
00124
00125 Mesh* TranslationCharacterMesh::convertCharacterMesh(Scene* scene){
00126 MeshManager* meshManager = scene->getMeshManager();
00127 CharacterMesh* mesh = meshManager->createCharacterMesh(name_);
00128
00129 MeshDataManager* meshDataManager = scene->getMeshDataManager();
00130 String meshDataName(name_);
00131 meshDataName.append("d");
00132 MeshData* meshData = meshDataManager->createMeshData(meshDataName);
00133 mesh->setMeshData(meshData);
00134
00135 mesh->setPrimitiveType(Mesh::triangleList);
00136 int vertexCount = positions_.getCount();
00137 mesh->setVertexCount(vertexCount);
00138 mesh->enableNormal(true);
00139 for(int i = 0; i < vertexCount; i++){
00140 mesh->setPosition(i, positions_[i]);
00141 mesh->setNormal(i, normals_[i]);
00142 }
00143 if(colors_.getCount() != 0){
00144 mesh->enableColor(true);
00145 for(int i = 0; i < vertexCount; i++){
00146 Color4c color(colors_[i]);
00147 mesh->setColor(i, color);
00148 }
00149 }
00150 if(uvs_.getCount() != 0){
00151 mesh->setTexCoordSetCount(uvSetCount_);
00152 for(int i = 0; i < uvSetCount_; i++){
00153 mesh->setTexCoordType(i, TexCoord::type2);
00154 }
00155 int uvIndex = 0;
00156 int polygonCount = vertexCount / 3;
00157 for(int i = 0; i < polygonCount; i++){
00158 int vertexOffset = i * 3;
00159 for(int j = 0; j < uvSetCount_; j++){
00160 mesh->setTexCoord2(vertexOffset + 0, j, uvs_[uvIndex]);
00161 uvIndex++;
00162 mesh->setTexCoord2(vertexOffset + 1, j, uvs_[uvIndex]);
00163 uvIndex++;
00164 mesh->setTexCoord2(vertexOffset + 2, j, uvs_[uvIndex]);
00165 uvIndex++;
00166 }
00167 }
00168 }
00169 mesh->setBonesPerVertex(maxWeightCount_);
00170 int boneCount = maxWeightCount_;
00171 int weightCount = mesh->getWeightsPerVertex();
00172 for(int i = 0; i < vertexCount; i++){
00173 int offset = i * maxWeightCount_;
00174 for(int j = 0; j < boneCount; j++){
00175 mesh->setBoneIndex(i, j, boneIndices_[offset + j]);
00176 }
00177
00178 for(int j = 0; j < weightCount; j++){
00179 mesh->setWeight(i, j, weights_[offset + j]);
00180 }
00181 }
00182 return mesh;
00183 }
00184
00185 }
00186