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 "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
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 }
00516