00001 #ifndef ROB_PRIMITIVES_H
00002 #define ROB_PRIMITIVES_H
00003
00004 #include <OgreSimpleRenderable.h>
00005 #include <OgreCamera.h>
00006 #include <vector>
00007
00033
00034
00035
00036
00037
00038
00039
00040 namespace GhoulPrimitive {
00041
00042 class Primitive;
00043 class Drawer;
00044
00045
00046 typedef Ogre::Real Real;
00047 typedef Ogre::Vector3 Vector3;
00048 typedef Ogre::Quaternion Quaternion;
00049 typedef Ogre::Camera Camera;
00050 typedef unsigned short IndexInt;
00051
00052 typedef const Real kReal;
00053 typedef const Vector3 kVector3;
00054 typedef const Quaternion kQuaternion;
00055
00056
00057 const Real pi = 3.14159265358979323846;
00058 const Real pi2 = 2.0* 3.14159265358979323846;
00059
00060 typedef enum {
00061 kInterpolate_Constant,
00062 kInterpolate_Linear,
00063 kInterpolate_SmoothQuadric,
00064
00065 } eInterpolationMode;
00066
00067 typedef enum {
00068 kOpType_POINT_LIST,
00069 kOpType_LINE_LIST,
00070 kOpType_LINE_STRIP,
00071 kOpType_TRIANGLE_LIST,
00072 kOpType_TRIANGLE_STRIP,
00073 kOpType_TRIANGLE_FAN,
00074 } eOpType;
00075
00076 typedef enum {
00077
00078 kGeometryChange_Seldom,
00079 kGeometryChange_Often,
00080 kGeometryChange_AlmostEveryFrame,
00081 } eGeometryChange;
00082
00085 class Drawer {
00086 public:
00087 Drawer();
00088 virtual ~Drawer();
00089
00090 virtual void Prepare (eOpType opType,eGeometryChange gcHint,size_t vertexCount,size_t indexCount=0,bool hasNormals=true,bool hasTexCoords=true);
00091 void AddVertex (kVector3 p,kVector3 n=Vector3::ZERO,kReal u=0.0,kReal v=0.0);
00092 virtual void AddVertex (kReal x,kReal y,kReal z=0.0,kReal nx=0.0,kReal ny=0.0,kReal nz=0.0,kReal u=0.0,kReal v=0.0);
00093 virtual void AddIndex (IndexInt i);
00094 virtual void Finish ();
00095 };
00096
00097
00098 class OgreRenderableDrawer : public Drawer,public Ogre::SimpleRenderable {
00099 public:
00101 OgreRenderableDrawer ();
00102 virtual ~OgreRenderableDrawer ();
00103
00104
00105
00107 virtual void Prepare (eOpType opType,eGeometryChange gcHint,size_t vertexCount,size_t indexCount=0,bool hasNormals=true,bool hasTexCoords=true);
00108 virtual void AddVertex (kReal x,kReal y,kReal z=0.0,kReal nx=0.0,kReal ny=0.0,kReal nz=0.0,kReal u=0.0,kReal v=0.0);
00109 virtual void AddIndex (IndexInt i);
00110 virtual void Finish ();
00111
00112 static Ogre::RenderOperation::OperationType GetOgreOpType (const eOpType opType);
00113 static Ogre::HardwareBuffer::Usage GetOgreHWBUsage (const eGeometryChange geometryChangeHint);
00114
00115
00117 virtual Ogre::Real getBoundingRadius(void) const;
00119 virtual Ogre::Real getSquaredViewDepth(const Ogre::Camera* cam) const;
00120
00121 protected:
00122 bool mReady;
00123 bool mVertexDecInitialized;
00124 bool mIndexBufferInitialized;
00125 bool mHasNormals;
00126 bool mHasTexCoords;
00127 bool mBoundingBoxEmpty;
00128 Vector3 mvAABMin;
00129 Vector3 mvAABMax;
00130 Real* mWritePtr;
00131 IndexInt* mIndexWritePtr;
00132 Ogre::HardwareVertexBufferSharedPtr mHWVBuf;
00133 Ogre::HardwareIndexBufferSharedPtr mHWIBuf;
00134 size_t miVerticesLeft;
00135 size_t miIndicesLeft;
00136 size_t mVertexBufferCapacity;
00137 size_t mIndexBufferCapacity;
00138 };
00139
00140
00141 class Primitive {
00143 public:
00144
00146 virtual void Update (Drawer& drawer,bool bHasNormals=true,bool bHasTexCoords=true);
00147
00148
00149
00150 Primitive();
00151 virtual ~Primitive();
00152
00154 static void Circle (Real* a,size_t blocks,size_t components=2,kReal radius=1.0,kReal startang=0.0,kReal endang=pi2,bool setOtherComponentsToZero=true);
00155 static void Ellipse (Real* a,size_t blocks,size_t components=2,kReal radiusx=1.0,kReal radiusy=1.0,kReal startang=0.0,kReal endang=pi2,bool setOtherComponentsToZero=true);
00156
00157
00158 static void InterpolatateFloatV (kReal* source,Real* dest,kReal t,size_t num=1,size_t startoff=0,size_t bufsize=2,eInterpolationMode mode=kInterpolate_Linear,size_t stride=0);
00159 static Real InterpolatateFloat (kReal* source,kReal t,size_t startoff=0,size_t bufsize=2,eInterpolationMode mode=kInterpolate_Linear,size_t stride=0);
00161 static Real InterpolatateFloat_Linear (kReal p1,kReal p2,kReal t);
00163 static Real InterpolatateFloat_Smooth (kReal q1,kReal p1,kReal p2,kReal q2,kReal t);
00164
00165
00166 };
00167
00168
00172 class Loft : public Primitive {
00173 public:
00174
00175
00177 Loft();
00178 virtual ~Loft();
00179
00180
00181 virtual void Update (Drawer& drawer,bool bHasNormals=true,bool bHasTexCoords=true);
00182
00183
00184
00185
00186
00187 void ClearBasePoints ();
00188 void SetBasePoint (size_t i, kVector3 p,kVector3 n=Vector3::ZERO,kReal u=0);
00189 void AddBasePoint ( kVector3 p,kVector3 n=Vector3::ZERO,kReal u=0);
00190
00191 void ClearPathPoints ();
00192 void SetPathPoint (size_t i, kVector3 p,kReal uBias=0,kReal v=0,kVector3 scale=Vector3::UNIT_SCALE,kQuaternion rot=Quaternion::IDENTITY);
00193 void AddPathPoint ( kVector3 p,kReal uBias=0,kReal v=0,kVector3 scale=Vector3::UNIT_SCALE,kQuaternion rot=Quaternion::IDENTITY);
00194
00195 bool mBase_bAutoNormal;
00196 bool mUseScale;
00197 bool mUseRot;
00198 bool mAutoBackSide;
00199 bool mSphericalAutoNormals;
00200 Vector3 mSphericalAutoNormals_Center;
00201
00202 private:
00203
00204 class BasePoint {
00205 public:
00206 Vector3 p;
00207 Vector3 n;
00208 Real u;
00209 inline BasePoint () { }
00210 inline BasePoint (kVector3 p,kVector3 n,kReal u) : p(p), n(n), u(u) {}
00211 inline BasePoint (const BasePoint& a) : p(a.p), n(a.n), u(a.u) {}
00212 };
00213 class PathPoint {
00214 public:
00215 Vector3 p;
00216 Real v;
00217 Real uBias;
00218 Vector3 scale;
00219 Quaternion rot;
00220 inline PathPoint () { }
00221 inline PathPoint (kVector3 p,kReal v,kReal uBias,kVector3 scale,kQuaternion rot) : p(p), v(v), uBias(uBias), scale(scale), rot(rot) {}
00222 inline PathPoint (const PathPoint& a) : p(a.p), v(a.v), uBias(a.uBias), scale(a.scale), rot(a.rot) {}
00223 };
00224 std::vector<BasePoint> mlBasePoint;
00225 std::vector<PathPoint> mlPathPoint;
00226 };
00227
00228
00230 class Ellipsoid : public Loft { public:
00231 virtual ~Ellipsoid();
00232 Ellipsoid (kVector3 pos=Vector3::ZERO,kVector3 rad=Vector3::UNIT_SCALE,size_t rings=11,size_t segments=23);
00233 Ellipsoid& SetParams (kVector3 pos=Vector3::ZERO,kVector3 rad=Vector3::UNIT_SCALE,size_t rings=11,size_t segments=23);
00234 };
00235 class OgreEllipsoid : public Ellipsoid,public OgreRenderableDrawer { public:
00236 virtual ~OgreEllipsoid();
00237 OgreEllipsoid (kVector3 pos=Vector3::ZERO,kVector3 rad=Vector3::UNIT_SCALE,size_t rings=11,size_t segments=23);
00238 };
00239
00241 class Cone : public Loft { public:
00242 virtual ~Cone();
00243 Cone (kVector3 pos1=Vector3::ZERO,kVector3 pos2=Vector3::UNIT_Y,kReal rad1X=1.0,kReal rad1Y=1.0,kReal endscale=1.0,size_t segments=23,size_t height_segments=1);
00244 Cone& SetParams (kVector3 pos1=Vector3::ZERO,kVector3 pos2=Vector3::UNIT_Y,kReal rad1X=1.0,kReal rad1Y=1.0,kReal endscale=1.0,size_t segments=23,size_t height_segments=1);
00245 };
00246 class OgreCone : public Cone, public OgreRenderableDrawer { public:
00247 virtual ~OgreCone();
00248 OgreCone (kVector3 pos1=Vector3::ZERO,kVector3 pos2=Vector3::UNIT_Y,kReal rad1X=1.0,kReal rad1Y=1.0,kReal endscale=1.0,size_t segments=23,size_t height_segments=1);
00249 };
00250
00251 };
00252
00253
00254 #if 0 // NOTES
00255
00256
00257
00258
00259
00260
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354 #endif
00355
00356 #endif // ROB_PRIMITIVES_H