[1505] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "Mesh.h" |
---|
| 30 | |
---|
[3196] | 31 | #include <cassert> |
---|
[1505] | 32 | #include <sstream> |
---|
[3196] | 33 | #include <string> |
---|
[2662] | 34 | #include <OgreEntity.h> |
---|
[11080] | 35 | #include <OgreSubEntity.h> |
---|
[1505] | 36 | #include <OgreSceneManager.h> |
---|
[12076] | 37 | #include <OgreHardwareVertexBuffer.h> |
---|
[1505] | 38 | |
---|
[2087] | 39 | #include "util/Convert.h" |
---|
[3196] | 40 | #include "core/GameMode.h" |
---|
[2087] | 41 | |
---|
[1505] | 42 | namespace orxonox |
---|
| 43 | { |
---|
| 44 | unsigned int Mesh::meshCounter_s = 0; |
---|
| 45 | |
---|
[2087] | 46 | Mesh::Mesh() |
---|
[1505] | 47 | { |
---|
[11071] | 48 | this->entity_ = nullptr; |
---|
[2087] | 49 | this->bCastShadows_ = true; |
---|
[1505] | 50 | } |
---|
| 51 | |
---|
[2087] | 52 | Mesh::~Mesh() |
---|
[1505] | 53 | { |
---|
[2087] | 54 | if (this->entity_ && this->scenemanager_) |
---|
| 55 | this->scenemanager_->destroyEntity(this->entity_); |
---|
[1505] | 56 | } |
---|
| 57 | |
---|
[2087] | 58 | void Mesh::setMeshSource(Ogre::SceneManager* scenemanager, const std::string& meshsource) |
---|
[1505] | 59 | { |
---|
[2087] | 60 | assert(scenemanager); |
---|
| 61 | |
---|
| 62 | this->scenemanager_ = scenemanager; |
---|
| 63 | |
---|
| 64 | if (this->entity_) |
---|
| 65 | this->scenemanager_->destroyEntity(this->entity_); |
---|
| 66 | |
---|
[12076] | 67 | |
---|
[2896] | 68 | if (GameMode::showsGraphics()) |
---|
[2087] | 69 | { |
---|
| 70 | try |
---|
| 71 | { |
---|
[3280] | 72 | this->entity_ = this->scenemanager_->createEntity("Mesh" + multi_cast<std::string>(Mesh::meshCounter_s++), meshsource); |
---|
[2087] | 73 | this->entity_->setCastShadows(this->bCastShadows_); |
---|
[12076] | 74 | |
---|
| 75 | |
---|
| 76 | unsigned short src, dest; |
---|
| 77 | if (!this->entity_->getMesh()->suggestTangentVectorBuildParams(Ogre::VertexElementSemantic::VES_TANGENT, src, dest)) |
---|
| 78 | { |
---|
| 79 | orxout() << "Generate Tanget for \"" << meshsource << '"' << endl; |
---|
| 80 | this->entity_->getMesh()->buildTangentVectors(Ogre::VertexElementSemantic::VES_TANGENT, src, dest); |
---|
| 81 | // this version cleans mirrored and rotated UVs but requires quality models |
---|
| 82 | // mesh->buildTangentVectors(VES_TANGENT, src, dest, true, true); |
---|
| 83 | } |
---|
| 84 | |
---|
[2087] | 85 | } |
---|
| 86 | catch (...) |
---|
| 87 | { |
---|
[8858] | 88 | orxout(internal_error) << "Couldn't load mesh \"" << meshsource << '"' << endl; |
---|
[11071] | 89 | this->entity_ = nullptr; |
---|
[2087] | 90 | } |
---|
| 91 | } |
---|
[1505] | 92 | } |
---|
[2087] | 93 | |
---|
| 94 | void Mesh::setCastShadows(bool bCastShadows) |
---|
| 95 | { |
---|
| 96 | this->bCastShadows_ = bCastShadows; |
---|
| 97 | if (this->entity_) |
---|
| 98 | this->entity_->setCastShadows(this->bCastShadows_); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | const std::string& Mesh::getName() const |
---|
| 102 | { |
---|
| 103 | if (this->entity_) |
---|
| 104 | return this->entity_->getName(); |
---|
| 105 | else |
---|
| 106 | return BLANKSTRING; |
---|
| 107 | } |
---|
| 108 | |
---|
[11080] | 109 | void Mesh::setMaterial(const std::string& name) |
---|
| 110 | { |
---|
| 111 | if (this->entity_) |
---|
| 112 | this->entity_->setMaterialName(name); |
---|
| 113 | } |
---|
| 114 | |
---|
[11783] | 115 | // PRE: a valid Ogre::Entity* entity with a valid subentity at index |
---|
| 116 | // POST: changed material of subentity at index to name |
---|
| 117 | void Mesh::setSubMaterial(const std::string& name, const int index){ |
---|
| 118 | if (this->entity_ && this->entity_->getSubEntity(index)) |
---|
| 119 | { |
---|
| 120 | this->entity_->getSubEntity(index)->setMaterialName(name); |
---|
| 121 | } |
---|
| 122 | else |
---|
| 123 | orxout() << "ERROR at Index " << index << endl; |
---|
| 124 | } |
---|
| 125 | |
---|
[11080] | 126 | const std::string& Mesh::getMaterial() const |
---|
| 127 | { |
---|
| 128 | if (this->entity_) |
---|
| 129 | { |
---|
| 130 | if(this->entity_->getSubEntity(0)) |
---|
| 131 | return this->entity_->getSubEntity(0)->getMaterialName(); //return the Material of the first SubEntity, should be enough for now |
---|
| 132 | else |
---|
| 133 | return BLANKSTRING; |
---|
| 134 | } |
---|
| 135 | else |
---|
| 136 | return BLANKSTRING; |
---|
| 137 | } |
---|
| 138 | |
---|
[2087] | 139 | void Mesh::setVisible(bool bVisible) |
---|
| 140 | { |
---|
| 141 | if (this->entity_) |
---|
| 142 | this->entity_->setVisible(bVisible); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | bool Mesh::isVisible() const |
---|
| 146 | { |
---|
| 147 | if (this->entity_) |
---|
| 148 | return this->entity_->getVisible(); |
---|
| 149 | else |
---|
| 150 | return false; |
---|
| 151 | } |
---|
[1505] | 152 | } |
---|