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

# TranslationCharacterMesh.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/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 // Lampへの変換
    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         // 一番最後のWeightは捨てる
    00178         for(int j = 0; j < weightCount; j++){
    00179             mesh->setWeight(i, j, weights_[offset + j]);
    00180         }
    00181     }
    00182     return mesh;
    00183 }
    00184 //------------------------------------------------------------------------------
    00185 } // End of namespace LampForMaya
    00186 //------------------------------------------------------------------------------