-
Notifications
You must be signed in to change notification settings - Fork 5
/
OgreColladaWriter.h
169 lines (129 loc) · 7.72 KB
/
OgreColladaWriter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// OgreColladaWriter.h, a class definition for (restricted) Collada import into Ogre
// Child classes to build scene hierarchies or a single mesh from a scene inherit from this
// Author: Jeff Trull <jetrull@sbcglobal.net>
/*
Copyright (c) 2010, 2011 Aditazz, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef OGRE_COLLADA_WRITER_H
#define OGRE_COLLADA_WRITER_H
#include <OgreString.h>
#include <OgreQuaternion.h>
#include <OgreVector3.h>
#include <OgreMatrix4.h>
#include <OgreCommon.h>
#include <COLLADAFWIWriter.h>
#include <COLLADAFWMaterialBinding.h>
#include <COLLADAFWTransformation.h>
#include "OgreColladaWriterBase.h"
namespace COLLADAFW {
class Node;
class EffectCommon;
class ColorOrTexture;
}
#define LOG_DEBUG(msg) { Ogre::LogManager::getSingleton().logMessage( Ogre::String((msg)) ); }
namespace OgreCollada {
// This class contains implementations shared by Scene (online) and Mesh writers
class Writer : public WriterBase {
public:
// declare implementations for parent class's virtual functions
virtual void cancel(const COLLADAFW::String&);
virtual void start();
virtual bool writeGlobalAsset(const COLLADAFW::FileInfo*);
virtual bool writeScene(const COLLADAFW::Scene*);
virtual bool writeLibraryNodes(const COLLADAFW::LibraryNodes*);
virtual bool writeMaterial(const COLLADAFW::Material*);
virtual bool writeEffect(const COLLADAFW::Effect*);
virtual bool writeCamera(const COLLADAFW::Camera*);
virtual bool writeImage(const COLLADAFW::Image*);
virtual bool writeVisualScene(const COLLADAFW::VisualScene*);
// two IWriter methods we expect child classes to implement
virtual bool writeGeometry(const COLLADAFW::Geometry*) = 0;
virtual void finish() = 0;
void setGraphOutput(const char* filename) { m_dotfn = filename; }
std::vector<Ogre::MaterialPtr> const& getMaterials() const { return m_ogreMaterials; }
// a separate method to disable culling for materials marked "double sided"
// this is out-of-band information supplied by some converters and not an official
// part of the standard, so it takes this route
void disableCulling(COLLADAFW::UniqueId const&);
protected:
// parent class members aren't accessible to child constructors, so provide this xtor for children to use:
Writer(const Ogre::String&, const char*, bool, bool);
// geometry statistics and debug tools are useful to child classes
const Ogre::String& m_dir; // directory relative to which we can find texture images etc.
const char* m_dotfn; // where (and if) to dump DOT output describing the scene hierarchy
void dump_as_dot(std::ostream& os);
bool m_checkNormals; // whether to do checking of the surface normals against vertex winding order
// starting points for final processing
std::vector<const COLLADAFW::Node*> m_vsRootNodes; // top-level nodes
Ogre::Quaternion m_ColladaRotation; // how to rotate Collada input to match Ogre's Y-up coordinates
Ogre::Vector3 m_ColladaScale; // how to scale Collada input into meters
// utility functions used while building scene
bool addGeometry(const COLLADAFW::Geometry* g, // input geometry from Collada
Ogre::ManualObject* manobj, // object under construction
const Ogre::Matrix4& xform = Ogre::Matrix4::IDENTITY, // transform to this point
const COLLADAFW::MaterialBindingArray* mba = 0); // materials to attach
void createMaterials();
static Ogre::Matrix4 computeTransformation(const COLLADAFW::Transformation*);
// stats
bool m_calculateGeometryStats; // whether to calculate and log statistics on geometries (meshes) and their usages
std::map<COLLADAFW::UniqueId, Ogre::String> m_geometryNames; // geometries in input
std::map<COLLADAFW::UniqueId, int> m_geometryInstanceCounts; // how often it gets used
std::map<COLLADAFW::UniqueId, int> m_geometryTriangleCounts; // how many triangles it contains
std::map<COLLADAFW::UniqueId, int> m_geometryLineCounts; // how many lines it contains
// internal class to do sorting of triangle counts
class TriangleCountComparator {
public:
TriangleCountComparator(const std::map<COLLADAFW::UniqueId, int>& instCount,
const std::map<COLLADAFW::UniqueId, int>& triCount) : m_icount(instCount), m_tcount(triCount) {}
bool operator() (const COLLADAFW::UniqueId& a,
const COLLADAFW::UniqueId& b) {
// biggest first
return m_icount.find(a)->second * m_tcount.find(a)->second > m_icount.find(b)->second * m_tcount.find(b)->second;
}
private:
const std::map<COLLADAFW::UniqueId, int> &m_icount, &m_tcount;
};
// we have to store, for each created mesh, what "material ID" is used by each submesh
// then we'll be able to bind materials to the created entities properly
typedef std::map<Ogre::MeshPtr, std::vector<COLLADAFW::MaterialId> > MeshMaterialIdMap;
typedef MeshMaterialIdMap::const_iterator MeshMaterialIdMapIterator;
MeshMaterialIdMap m_meshmatids;
// library geometries
std::map<COLLADAFW::UniqueId, Ogre::MeshPtr> m_meshMap; // loaded or generated meshes here
protected:
// data storage - stuff collected during callbacks from Collada
typedef std::map<COLLADAFW::UniqueId, const COLLADAFW::Node*> LibNodesContainer;
typedef LibNodesContainer::const_iterator LibNodesIterator;
LibNodesContainer m_libNodes; // tree roots for library nodes
// names and effect IDs for each material, searchable by material ID (referenced by geometry instances)
typedef std::map<COLLADAFW::UniqueId, std::pair<Ogre::String, COLLADAFW::UniqueId> > MaterialMap;
typedef MaterialMap::const_iterator MaterialMapIterator;
MaterialMap m_materials;
typedef std::map<COLLADAFW::UniqueId, std::vector<COLLADAFW::EffectCommon> > EffectMap;
typedef EffectMap::const_iterator EffectMapIterator;
EffectMap m_effects;
typedef std::map<COLLADAFW::UniqueId, Ogre::String> ImageMap;
typedef ImageMap::const_iterator ImageMapIterator;
ImageMap m_images;
private:
// hide default xtor and compiler-generated copy and assignment operators
Writer();
Writer( const Writer& pre );
const Writer& operator= ( const Writer& pre );
typedef void (Ogre::Pass::*ColorSetter)(const Ogre::ColourValue&);
void handleColorOrTexture(const COLLADAFW::EffectCommon& ce,
const COLLADAFW::ColorOrTexture&,
Ogre::Pass*, ColorSetter, Ogre::TrackVertexColourType);
std::vector<Ogre::MaterialPtr> m_ogreMaterials;
bool m_transparencyWorkarounds;
std::vector<COLLADAFW::UniqueId> m_unculledEffects;
// private debug functions
void node_dfs_print(const COLLADAFW::Node*, int);
void node_dfs_dot(std::ostream& os, const COLLADAFW::Node*, int, const std::map<const COLLADAFW::Node*, int>&);
void node_dfs_geocheck(const COLLADAFW::Node*);
};
} // end namespace OgreCollada
#endif // OGRE_COLLADA_WRITER_H