source: GTP/trunk/App/Demos/Geom/OgreStuff/include/OgrePatchSurface.h @ 1812

Revision 1812, 10.3 KB checked in by gumbau, 18 years ago (diff)
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25#ifndef __PatchSurface_H__
26#define __PatchSurface_H__
27
28#include "OgrePrerequisites.h"
29
30#include "OgreVector3.h"
31#include "OgreString.h"
32#include "OgreRenderOperation.h"
33#include "OgreAxisAlignedBox.h"
34
35namespace Ogre {
36
37    /** A surface which is defined by curves of some kind to form a patch, e.g. a Bezier patch.
38        @remarks
39            This object will take a list of control points with various assorted data, and will
40            subdivide it into a patch mesh. Currently only Bezier curves are supported for defining
41            the surface, but other techniques such as NURBS would follow the same basic approach.
42    */
43    class _OgreExport PatchSurface
44    {
45    public:
46        PatchSurface();
47        ~PatchSurface();
48
49        enum PatchSurfaceType
50        {
51            /// A patch defined by a set of bezier curves
52            PST_BEZIER
53        };
54
55        /// Constant for indicating automatic determination of subdivision level for patches
56        enum
57        {
58            AUTO_LEVEL = -1
59        };
60
61        enum VisibleSide {
62            /// The side from which u goes right and v goes up (as in texture coords)
63            VS_FRONT,
64            /// The side from which u goes right and v goes down (reverse of texture coords)
65            VS_BACK,
66            /// Both sides are visible - warning this creates 2x the number of triangles and adds extra overhead for calculating normals
67            VS_BOTH
68        };
69        /** Sets up the surface by defining it's control points, type and initial subdivision level.
70            @remarks
71                This method initialises the surface by passing it a set of control points. The type of curves to be used
72                are also defined here, although the only supported option currently is a bezier patch. You can also
73                specify a global subdivision level here if you like, although it is recommended that the parameter
74                is left as AUTO_LEVEL, which means the system decides how much subdivision is required (based on the
75                curvature of the surface)
76            @param
77                controlPointBuffer A pointer to a buffer containing the vertex data which defines control points
78                of the curves rather than actual vertices. Note that you are expected to provide not
79                just position information, but potentially normals and texture coordinates too. The
80                format of the buffer is defined in the VertexDeclaration parameter
81            @param
82                decaration VertexDeclaration describing the contents of the buffer.
83                Note this declaration must _only_ draw on buffer source 0!
84            @param
85                width Specifies the width of the patch in control points.
86            @param
87                height Specifies the height of the patch in control points.
88            @param
89                pType The type of surface - currently only PST_BEZIER is supported
90            @param
91                uMaxSubdivisionLevel,vMaxSubdivisionLevel If you want to manually set the top level of subdivision,
92                do it here, otherwise let the system decide.
93            @param
94                visibleSide Determines which side of the patch (or both) triangles are generated for.
95        */
96        void defineSurface(void* controlPointBuffer,
97            VertexDeclaration *declaration, size_t width, size_t height,
98            PatchSurfaceType pType = PST_BEZIER,
99            size_t uMaxSubdivisionLevel = AUTO_LEVEL, size_t vMaxSubdivisionLevel = AUTO_LEVEL,
100            VisibleSide visibleSide = VS_FRONT);
101
102        /** Based on a previous call to defineSurface, establishes the number of vertices required
103            to hold this patch at the maximum detail level.
104            @remarks This is useful when you wish to build the patch into external vertex / index buffers.
105
106        */
107        size_t getRequiredVertexCount(void) const;
108        /** Based on a previous call to defineSurface, establishes the number of indexes required
109            to hold this patch at the maximum detail level.
110            @remarks This is useful when you wish to build the patch into external vertex / index buffers.
111
112        */
113        size_t getRequiredIndexCount(void) const;
114
115        /** Gets the current index count based on the current subdivision level. */
116        size_t getCurrentIndexCount(void) const;
117        /// Returns the index offset used by this buffer to write data into the buffer
118        size_t getIndexOffset(void) const { return mIndexOffset; }
119        /// Returns the vertex offset used by this buffer to write data into the buffer
120        size_t getVertexOffset(void) const { return mVertexOffset; }
121
122
123        /** Gets the bounds of this patch, only valid after calling defineSurface. */
124        const AxisAlignedBox& getBounds(void) const;
125        /** Gets the radius of the bounding sphere for this patch, only valid after defineSurface
126        has been called. */
127        Real getBoundingSphereRadius(void) const;
128        /** Tells the system to build the mesh relating to the surface into externally created
129            buffers.
130            @remarks
131                The VertexDeclaration of the vertex buffer must be identical to the one passed into
132                defineSurface.  In addition, there must be enough space in the buffer to
133                accommodate the patch at full detail level; you should call getRequiredVertexCount
134                and getRequiredIndexCount to determine this. This method does not create an internal
135                mesh for this patch and so getMesh will return null if you call it after building the
136                patch this way.
137            @param destVertexBuffer The destination vertex buffer in which to build the patch.
138            @param vertexStart The offset at which to start writing vertices for this patch
139            @param destIndexBuffer The destination index buffer in which to build the patch.
140            @param vertexStart The offset at which to start writing indexes for this patch
141
142        */
143        void build(HardwareVertexBufferSharedPtr destVertexBuffer, size_t vertexStart,
144            HardwareIndexBufferSharedPtr destIndexBuffer, size_t indexStart);
145
146        /** Alters the level of subdivision for this surface.
147            @remarks
148                This method changes the proportionate detail level of the patch; since
149                the U and V directions can have different subdivision levels, this method
150                takes a single Real value where 0 is the minimum detail (the control points)
151                and 1 is the maximum detail level as supplied to the original call to
152                defineSurface.
153        */
154        void setSubdivisionFactor(Real factor);
155
156        /** Gets the current level of subdivision. */
157        Real getSubdivisionFactor(void) const;
158
159        void* getControlPointBuffer(void) const
160        {
161            return mControlPointBuffer;
162        }
163        /** Convenience method for telling the patch that the control points have been
164            deleted, since once the patch has been built they are not required. */
165        void notifyControlPointBufferDeallocated(void) {
166            mControlPointBuffer = 0;
167        }
168    protected:
169        /// Vertex declaration describing the control point buffer
170        VertexDeclaration* mDeclaration;
171        /// Buffer containing the system-memory control points
172        void* mControlPointBuffer;
173        /// Type of surface
174        PatchSurfaceType mType;
175        /// Width in control points
176        size_t mCtlWidth;
177        /// Height in control points
178        size_t mCtlHeight;
179        /// TotalNumber of control points
180        size_t mCtlCount;
181        /// U-direction subdivision level
182        size_t mULevel;
183        /// V-direction subdivision level
184        size_t mVLevel;
185        /// Max subdivision level
186        size_t mMaxULevel;
187        size_t mMaxVLevel;
188        /// Width of the subdivided mesh (big enough for max level)
189        size_t mMeshWidth;
190        /// Height of the subdivided mesh (big enough for max level)
191        size_t mMeshHeight;
192        /// Which side is visible
193        VisibleSide mVSide;
194
195        Real mSubdivisionFactor;
196
197        std::vector<Vector3> mVecCtlPoints;
198
199        /** Internal method for finding the subdivision level given 3 control points.
200        */
201        size_t findLevel( Vector3& a, Vector3& b, Vector3& c);
202
203        void distributeControlPoints(void* lockedBuffer);
204        void subdivideCurve(void* lockedBuffer, size_t startIdx, size_t stepSize, size_t numSteps, size_t iterations);
205        void interpolateVertexData(void* lockedBuffer, size_t leftIndex, size_t rightIndex, size_t destIndex);
206        void makeTriangles(void);
207
208        size_t getAutoULevel(bool forMax = false);
209        size_t getAutoVLevel(bool forMax = false);
210
211        HardwareVertexBufferSharedPtr mVertexBuffer;
212        HardwareIndexBufferSharedPtr mIndexBuffer;
213        size_t mVertexOffset;
214        size_t mIndexOffset;
215        size_t mRequiredVertexCount;
216        size_t mRequiredIndexCount;
217        size_t mCurrIndexCount;
218
219        AxisAlignedBox mAABB;
220        Real mBoundingSphere;
221
222
223
224    };
225
226
227} // namespace
228
229#endif
Note: See TracBrowser for help on using the repository browser.