Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/OgreMain/include/OgrePatchSurface.h @ 1

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