[5] | 1 | /* |
---|
| 2 | ----------------------------------------------------------------------------- |
---|
| 3 | This source file is part of OGRE |
---|
| 4 | (Object-oriented Graphics Rendering Engine) |
---|
| 5 | For the latest info, see http://www.ogre3d.org/ |
---|
| 6 | |
---|
| 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
| 8 | Also see acknowledgements in Readme.html |
---|
| 9 | |
---|
| 10 | This program is free software; you can redistribute it and/or modify it under |
---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
| 13 | version. |
---|
| 14 | |
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
| 18 | |
---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
| 23 | |
---|
| 24 | You may alternatively use this source under the terms of a specific version of |
---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
| 26 | Torus Knot Software Ltd. |
---|
| 27 | ----------------------------------------------------------------------------- |
---|
| 28 | */ |
---|
| 29 | // The underlying algorithms in this class are based heavily on: |
---|
| 30 | /* |
---|
| 31 | * Progressive Mesh type Polygon Reduction Algorithm |
---|
| 32 | * by Stan Melax (c) 1998 |
---|
| 33 | */ |
---|
| 34 | |
---|
| 35 | #ifndef __ProgressiveMesh_H_ |
---|
| 36 | #define __ProgressiveMesh_H_ |
---|
| 37 | |
---|
| 38 | #include "OgrePrerequisites.h" |
---|
| 39 | #include "OgreVector3.h" |
---|
| 40 | #include "OgreHardwareVertexBuffer.h" |
---|
| 41 | #include "OgreHardwareIndexBuffer.h" |
---|
| 42 | #include "OgreRenderOperation.h" |
---|
| 43 | |
---|
| 44 | namespace Ogre { |
---|
| 45 | |
---|
| 46 | /** This class reduces the complexity of the geometry it is given. |
---|
| 47 | This class is dedicated to reducing the number of triangles in a given mesh |
---|
| 48 | taking into account seams in both geometry and texture co-ordinates and meshes |
---|
| 49 | which have multiple frames. |
---|
| 50 | @par |
---|
| 51 | The primary use for this is generating LOD versions of Mesh objects, but it can be |
---|
| 52 | used by any geometry provider. The only limitation at the moment is that the |
---|
| 53 | provider uses a common vertex buffer for all LODs and one index buffer per LOD. |
---|
| 54 | Therefore at the moment this class can only handle indexed geometry. |
---|
| 55 | @par |
---|
| 56 | NB the interface of this class will certainly change when compiled vertex buffers are |
---|
| 57 | supported. |
---|
| 58 | */ |
---|
| 59 | class _OgreExport ProgressiveMesh |
---|
| 60 | { |
---|
| 61 | public: |
---|
| 62 | |
---|
| 63 | /** The way to derive the quota of vertices which are reduced at each LOD. */ |
---|
| 64 | enum VertexReductionQuota |
---|
| 65 | { |
---|
| 66 | /// A set number of vertices are removed at each reduction |
---|
| 67 | VRQ_CONSTANT, |
---|
| 68 | /// A proportion of the remaining number of vertices are removed at each reduction |
---|
| 69 | VRQ_PROPORTIONAL |
---|
| 70 | }; |
---|
| 71 | |
---|
| 72 | typedef std::vector<IndexData*> LODFaceList; |
---|
| 73 | |
---|
| 74 | /** Constructor, takes the geometry data and index buffer. |
---|
| 75 | @remarks |
---|
| 76 | DO NOT pass write-only, unshadowed buffers to this method! They will not |
---|
| 77 | work. Pass only shadowed buffers, or better yet perform mesh reduction as |
---|
| 78 | an offline process using DefaultHardwareBufferManager to manage vertex |
---|
| 79 | buffers in system memory. |
---|
| 80 | */ |
---|
| 81 | ProgressiveMesh(const VertexData* vertexData, const IndexData* indexData); |
---|
| 82 | virtual ~ProgressiveMesh(); |
---|
| 83 | |
---|
| 84 | /** Adds an extra vertex position buffer. |
---|
| 85 | @remarks |
---|
| 86 | As well as the main vertex buffer, the client of this class may add extra versions |
---|
| 87 | of the vertex buffer which will also be taken into account when the cost of |
---|
| 88 | simplifying the mesh is taken into account. This is because the cost of |
---|
| 89 | simplifying an animated mesh cannot be calculated from just the reference position, |
---|
| 90 | multiple positions needs to be assessed in order to find the best simplification option. |
---|
| 91 | @par |
---|
| 92 | DO NOT pass write-only, unshadowed buffers to this method! They will not |
---|
| 93 | work. Pass only shadowed buffers, or better yet perform mesh reduction as |
---|
| 94 | an offline process using DefaultHardwareBufferManager to manage vertex |
---|
| 95 | buffers in system memory. |
---|
| 96 | @param buffer Pointer to x/y/z buffer with vertex positions. The number of vertices |
---|
| 97 | must be the same as in the original GeometryData passed to the constructor. |
---|
| 98 | */ |
---|
| 99 | virtual void addExtraVertexPositionBuffer(const VertexData* vertexData); |
---|
| 100 | |
---|
| 101 | /** Builds the progressive mesh with the specified number of levels. |
---|
| 102 | @param numLevels The number of levels to include in the output excluding the full detail version. |
---|
| 103 | @param outList Pointer to a list of LOD geometry data which will be completed by the application. |
---|
| 104 | Each entry is a reduced form of the mesh, in decreasing order of detail. |
---|
| 105 | @param quota The way to derive the number of vertices removed at each LOD |
---|
| 106 | @param reductionValue Either the proportion of vertices to remove at each level, or a fixed |
---|
| 107 | number of vertices to remove at each level, depending on the value of quota |
---|
| 108 | */ |
---|
| 109 | virtual void build(ushort numLevels, LODFaceList* outList, |
---|
| 110 | VertexReductionQuota quota = VRQ_PROPORTIONAL, Real reductionValue = 0.5f ); |
---|
| 111 | |
---|
| 112 | protected: |
---|
| 113 | const VertexData *mpVertexData; |
---|
| 114 | const IndexData *mpIndexData; |
---|
| 115 | |
---|
| 116 | size_t mCurrNumIndexes; |
---|
| 117 | size_t mNumCommonVertices; |
---|
| 118 | |
---|
| 119 | // Internal classes |
---|
| 120 | class PMTriangle; |
---|
| 121 | class PMVertex; |
---|
| 122 | |
---|
| 123 | public: // VC6 hack |
---|
| 124 | |
---|
| 125 | /** A vertex as used by a face. This records the index of the actual vertex which is used |
---|
| 126 | by the face, and a pointer to the common vertex used for surface evaluation. */ |
---|
| 127 | class _OgrePrivate PMFaceVertex { |
---|
| 128 | public: |
---|
| 129 | size_t realIndex; |
---|
| 130 | PMVertex* commonVertex; |
---|
| 131 | }; |
---|
| 132 | |
---|
| 133 | protected: |
---|
| 134 | |
---|
| 135 | /** A triangle in the progressive mesh, holds extra info like face normal. */ |
---|
| 136 | class _OgrePrivate PMTriangle { |
---|
| 137 | public: |
---|
| 138 | PMTriangle(); |
---|
| 139 | void setDetails(size_t index, PMFaceVertex *v0, PMFaceVertex *v1, PMFaceVertex *v2); |
---|
| 140 | void computeNormal(void); |
---|
| 141 | void replaceVertex(PMFaceVertex *vold, PMFaceVertex *vnew); |
---|
| 142 | bool hasCommonVertex(PMVertex *v) const; |
---|
| 143 | bool hasFaceVertex(PMFaceVertex *v) const; |
---|
| 144 | PMFaceVertex* getFaceVertexFromCommon(PMVertex* commonVert); |
---|
| 145 | void notifyRemoved(void); |
---|
| 146 | |
---|
| 147 | PMFaceVertex* vertex[3]; // the 3 points that make this tri |
---|
| 148 | Vector3 normal; // unit vector othogonal to this face |
---|
| 149 | bool removed; // true if this tri is now removed |
---|
| 150 | size_t index; |
---|
| 151 | }; |
---|
| 152 | |
---|
| 153 | /** A vertex in the progressive mesh, holds info like collapse cost etc. |
---|
| 154 | This vertex can actually represent several vertices in the final model, because |
---|
| 155 | vertices along texture seams etc will have been duplicated. In order to properly |
---|
| 156 | evaluate the surface properties, a single common vertex is used for these duplicates, |
---|
| 157 | and the faces hold the detail of the duplicated vertices. |
---|
| 158 | */ |
---|
| 159 | class _OgrePrivate PMVertex { |
---|
| 160 | public: |
---|
| 161 | PMVertex(); |
---|
| 162 | void setDetails(const Vector3& v, size_t index); |
---|
| 163 | void removeIfNonNeighbor(PMVertex *n); |
---|
| 164 | bool isBorder(void);/// true if this vertex is on the edge of an open geometry patch |
---|
| 165 | bool isManifoldEdgeWith(PMVertex* v); // is edge this->src a manifold edge? |
---|
| 166 | void notifyRemoved(void); |
---|
| 167 | |
---|
| 168 | Vector3 position; // location of point in euclidean space |
---|
| 169 | size_t index; // place of vertex in original list |
---|
| 170 | typedef std::set<PMVertex *> NeighborList; |
---|
| 171 | typedef std::set<PMVertex *> DuplicateList; |
---|
| 172 | NeighborList neighbor; // adjacent vertices |
---|
| 173 | typedef std::set<PMTriangle *> FaceList; |
---|
| 174 | FaceList face; // adjacent triangles |
---|
| 175 | |
---|
| 176 | Real collapseCost; // cached cost of collapsing edge |
---|
| 177 | PMVertex * collapseTo; // candidate vertex for collapse |
---|
| 178 | bool removed; // true if this vert is now removed |
---|
| 179 | bool toBeRemoved; // denug |
---|
| 180 | |
---|
| 181 | bool seam; /// true if this vertex is on a model seam where vertices are duplicated |
---|
| 182 | |
---|
| 183 | }; |
---|
| 184 | |
---|
| 185 | typedef std::vector<PMTriangle> TriangleList; |
---|
| 186 | typedef std::vector<PMFaceVertex> FaceVertexList; |
---|
| 187 | typedef std::vector<PMVertex> CommonVertexList; |
---|
| 188 | typedef std::vector<Real> WorstCostList; |
---|
| 189 | |
---|
| 190 | /// Data used to calculate the collapse costs |
---|
| 191 | struct PMWorkingData |
---|
| 192 | { |
---|
| 193 | TriangleList mTriList; /// List of faces |
---|
| 194 | FaceVertexList mFaceVertList; // The vertex details referenced by the triangles |
---|
| 195 | CommonVertexList mVertList; // The master list of common vertices |
---|
| 196 | }; |
---|
| 197 | |
---|
| 198 | typedef std::vector<PMWorkingData> WorkingDataList; |
---|
| 199 | /// Multiple copies, 1 per vertex buffer |
---|
| 200 | WorkingDataList mWorkingData; |
---|
| 201 | |
---|
| 202 | /// The worst collapse cost from all vertex buffers for each vertex |
---|
| 203 | WorstCostList mWorstCosts; |
---|
| 204 | |
---|
| 205 | /// Internal method for building PMWorkingData from geometry data |
---|
| 206 | void addWorkingData(const VertexData* vertexData, const IndexData* indexData); |
---|
| 207 | |
---|
| 208 | /// Internal method for initialising the edge collapse costs |
---|
| 209 | void initialiseEdgeCollapseCosts(void); |
---|
| 210 | /// Internal calculation method for deriving a collapse cost from u to v |
---|
| 211 | Real computeEdgeCollapseCost(PMVertex *src, PMVertex *dest); |
---|
| 212 | /// Internal method evaluates all collapse costs from this vertex and picks the lowest for a single buffer |
---|
| 213 | Real computeEdgeCostAtVertexForBuffer(WorkingDataList::iterator idata, size_t vertIndex); |
---|
| 214 | /// Internal method evaluates all collapse costs from this vertex for every buffer and returns the worst |
---|
| 215 | void computeEdgeCostAtVertex(size_t vertIndex); |
---|
| 216 | /// Internal method to compute edge collapse costs for all buffers / |
---|
| 217 | void computeAllCosts(void); |
---|
| 218 | /// Internal method for getting the index of next best vertex to collapse |
---|
| 219 | size_t getNextCollapser(void); |
---|
| 220 | /// Internal method builds an new LOD based on the current state |
---|
| 221 | void bakeNewLOD(IndexData* pData); |
---|
| 222 | |
---|
| 223 | /** Internal method, collapses vertex onto it's saved collapse target. |
---|
| 224 | @remarks |
---|
| 225 | This updates the working triangle list to drop a triangle and recalculates |
---|
| 226 | the edge collapse costs around the collapse target. |
---|
| 227 | This also updates all the working vertex lists for the relevant buffer. |
---|
| 228 | */ |
---|
| 229 | void collapse(PMVertex *collapser); |
---|
| 230 | |
---|
| 231 | /** Internal debugging method */ |
---|
| 232 | void dumpContents(const String& log); |
---|
| 233 | |
---|
| 234 | |
---|
| 235 | |
---|
| 236 | |
---|
| 237 | |
---|
| 238 | |
---|
| 239 | |
---|
| 240 | |
---|
| 241 | |
---|
| 242 | }; |
---|
| 243 | |
---|
| 244 | |
---|
| 245 | |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | #endif |
---|