Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/tools/Mesh.cc @ 12196

Last change on this file since 12196 was 12189, checked in by merholzl, 6 years ago

Shader merge

  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
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
31#include <cassert>
32#include <sstream>
33#include <string>
34#include <OgreEntity.h>
35#include <OgreSubEntity.h>
36#include <OgreSceneManager.h>
37#include <OgreHardwareVertexBuffer.h>
38
39#include "util/Convert.h"
40#include "core/GameMode.h"
41
42namespace orxonox
43{
44    unsigned int Mesh::meshCounter_s = 0;
45
46    Mesh::Mesh()
47    {
48        this->entity_ = nullptr;
49        this->bCastShadows_ = true;
50    }
51
52    Mesh::~Mesh()
53    {
54        if (this->entity_ && this->scenemanager_)
55            this->scenemanager_->destroyEntity(this->entity_);
56    }
57
58    void Mesh::setMeshSource(Ogre::SceneManager* scenemanager, const std::string& meshsource)
59    {
60        assert(scenemanager);
61
62        this->scenemanager_ = scenemanager;
63
64        if (this->entity_)
65            this->scenemanager_->destroyEntity(this->entity_);
66
67
68        if (GameMode::showsGraphics())
69        {
70            try
71            {
72                this->entity_ = this->scenemanager_->createEntity("Mesh" + multi_cast<std::string>(Mesh::meshCounter_s++), meshsource);
73                this->entity_->setCastShadows(this->bCastShadows_);
74
75
76                unsigned short src, dest;
77                try
78                {
79                    if (!this->entity_->getMesh()->suggestTangentVectorBuildParams(Ogre::VertexElementSemantic::VES_TANGENT, src, dest))
80                    {
81                        orxout() << "Generate Tanget for \"" << meshsource << '"' << endl;
82                        this->entity_->getMesh()->buildTangentVectors(Ogre::VertexElementSemantic::VES_TANGENT, src, dest);
83                        // this version cleans mirrored and rotated UVs but requires quality models
84                        // mesh->buildTangentVectors(VES_TANGENT, src, dest, true, true);
85                    }
86                } catch (...)
87                {
88                    orxout(internal_warning) << "Couldn't generat tangets for \"" << meshsource << '"' << endl;
89                }
90
91            } catch (...)
92            {
93                orxout(internal_error) << "Couldn't load mesh \"" << meshsource << '"' << endl;
94                this->entity_ = nullptr;
95            }
96        }
97    }
98
99    void Mesh::setCastShadows(bool bCastShadows)
100    {
101        this->bCastShadows_ = bCastShadows;
102        if (this->entity_)
103            this->entity_->setCastShadows(this->bCastShadows_);
104    }
105
106    const std::string& Mesh::getName() const
107    {
108        if (this->entity_)
109            return this->entity_->getName();
110        else
111            return BLANKSTRING;
112    }
113
114    void Mesh::setMaterial(const std::string& name)
115    {
116        if (this->entity_)
117            this->entity_->setMaterialName(name);
118    }
119
120    // PRE: a valid  Ogre::Entity* entity with a valid subentity at index
121    // POST: changed material of subentity at index to name
122    void Mesh::setSubMaterial(const std::string& name, const int index){
123         if (this->entity_ && this->entity_->getSubEntity(index))
124        {
125                this->entity_->getSubEntity(index)->setMaterialName(name);
126        }
127        else
128            orxout() << "ERROR at Index " << index << endl;
129    }
130
131    const std::string& Mesh::getMaterial() const
132    {
133        if (this->entity_)
134        {
135            if(this->entity_->getSubEntity(0))
136                return this->entity_->getSubEntity(0)->getMaterialName(); //return the Material of the first SubEntity, should be enough for now
137            else
138                return BLANKSTRING;
139        }
140        else
141            return BLANKSTRING;
142    }
143
144    void Mesh::setVisible(bool bVisible)
145    {
146        if (this->entity_)
147            this->entity_->setVisible(bVisible);
148    }
149
150    bool Mesh::isVisible() const
151    {
152        if (this->entity_)
153            return this->entity_->getVisible();
154        else
155            return false;
156    }
157}
Note: See TracBrowser for help on using the repository browser.