Google

Main Page   Class Hierarchy   Compound List   File List   Compound Members  

curve.h

00001 /*
00002     Copyright (C) 1998-2001 by Jorrit Tyberghein
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public
00015     License along with this library; if not, write to the Free
00016     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 */
00018 
00019 #ifndef __CS_CURVE_H__
00020 #define __CS_CURVE_H__
00021 
00022 #include "csutil/scf.h"
00023 #include "csutil/csvector.h"
00024 #include "csgeom/math3d.h"
00025 #include "csgeom/math2d.h"
00026 #include "csgeom/box.h"
00027 #include "csengine/bezier.h"
00028 #include "csengine/texture.h"
00029 #include "csengine/material.h"
00030 #include "csengine/lghtmap.h"
00031 #include "csengine/rview.h"
00032 #include "csutil/csobject.h"
00033 #include "ivideo/graph3d.h"
00034 #include "ivideo/vbufmgr.h"
00035 #include "imesh/thing/curve.h"
00036 
00037 class csThing;
00038 class csCurveTemplate;
00039 class csLightPatch;
00040 class csSector;
00041 class csRadCurve;
00042 class csBspContainer;
00043 class csFrustumView;
00044 struct csCoverageMatrix;
00045 struct iMaterialHandle;
00046 struct iCacheManager;
00047 
00051 class csCurveTesselated
00052 {
00053 private:
00054   // Object space coordinates.
00055   csVector3* ObjectCoords;
00056   // Texture coordinates.
00057   csVector2* TextureCoords;
00058   // Original control points.
00059   csVector2* ControlPoints;
00060   // Colors for the vertices.
00061   csColor* Colors;
00062   // Triangles.
00063   csTriangle* Triangles;
00064 
00065   // Number of vertices
00066   int NumVertices;
00067   // Number of triangles
00068   int NumTriangles;
00069   // A flag which indicates if the color table is filled in.
00070   bool ColorsValid;
00071 
00072 public:
00077   csCurveTesselated (int NumVertices, int NumTriangles);
00079   ~csCurveTesselated ();
00080 
00082   inline int GetVertexCount () const;
00084   inline int GetTriangleCount () const;
00086   inline csVector3* GetVertices ();
00088   inline csVector2* GetTxtCoords ();
00090   inline csVector2* GetControlPoints ();
00092   inline csColor* GetColors ();
00094   inline csTriangle* GetTriangles ();
00096   inline csTriangle& GetTriangle (int i);
00098   inline bool AreColorsValid () const;
00099 
00105   void UpdateColors (csLightMap* lightmap);
00106 };
00107 
00111 class csCurve : public csObject
00112 {
00114   friend class csRadCurve;
00115 
00116 private:
00118   unsigned long CurveID;
00120   static unsigned long LastCurveID;
00121 
00123   iMaterialWrapper* Material;
00124 
00126   csCurveTemplate* CurveTemplate;
00127 
00129   csLightPatch* LightPatches;
00130 
00135   csReversibleTransform* O2W;
00136 
00137   /*
00138    * Position Buffer: this is an array which coordinates u,v lightmap
00139    * pixel coordinates to the position on the curve in world space
00140    * coordinates.
00141    * i.e. in a 10x10 lightmap uv2World[5][5] is the world space coordinate
00142    *   of the lightmap texel 5,5 on the lightmap
00143    */
00144   csVector3* uv2World;
00145 
00146   /*
00147    * Normal Buffer: this is an array which coordinates u,v lightmap
00148    * pixel coordinates to the normal of the curve
00149    * i.e. in a 10x10 lightmap uv2Normal[5][5] is the normal vector which
00150    * corresponds to the lightmap texel 5,5 on the lightmap
00151    */
00152   csVector3* uv2Normal;
00153 
00155   iVertexBuffer* vbuf;
00156   iVertexBufferManager* vbufmgr;
00157 
00159   void SetupVertexBuffer ();
00160 
00162   struct eiVertexBufferManagerClient : public iVertexBufferManagerClient
00163   {
00164     SCF_DECLARE_EMBEDDED_IBASE (csCurve);
00165     virtual void ManagerClosing ();
00166   }scfiVertexBufferManagerClient;
00167   friend struct eiVertexBufferManagerClient;
00168 
00169 
00170 public:
00172   csThing* ParentThing;
00173 
00175   csLightMap* LightMap;
00176 
00178   bool LightmapUpToDate;
00179 
00180 public:
00181 
00183   csCurve (csCurveTemplate* parent_tmpl);
00185   virtual ~csCurve ();
00186 
00188   inline unsigned long GetCurveID () const;
00189 
00191   iVertexBuffer* GetVertexBuffer () const { return vbuf; }
00192 
00194   inline iMaterialHandle* GetMaterialHandle () const;
00196   inline iMaterialWrapper* GetMaterial () const;
00198   void SetMaterial (iMaterialWrapper* h);
00199 
00201   inline csCurveTemplate* GetParentTemplate () const;
00202 
00204   void MakeDirtyDynamicLights ();
00206   void AddLightPatch (csLightPatch* lp);
00208   void UnlinkLightPatch (csLightPatch* lp);
00210   bool RecalculateDynamicLights ();
00212   void ShineDynLight (csLightPatch* lp);
00213 
00215   void SetObject2World (const csReversibleTransform *o2w);
00217   inline const csReversibleTransform *GetObject2World () const;
00218 
00220   inline void SetParentThing (csThing* p);
00222   inline csThing* GetParentThing () const;
00223 
00225   inline csLightMap* GetLightMap () const;
00227   void CalculateLightingStatic (csFrustumView* lview, bool vis);
00229   void CalculateLightingDynamic (csFrustumView* lview);
00231   void InitializeDefaultLighting ();
00233   bool ReadFromCache (iCacheManager* cache_mgr, int id);
00235   bool WriteToCache (iCacheManager* cache_mgr, int id);
00237   void PrepareLighting ();
00238 
00243   void GetCoverageMatrix (csFrustumView& lview, csCoverageMatrix &cm) const;
00244 
00246   float GetArea();
00247 
00249   void CalcUVBuffers();
00250 
00256   virtual csCurveTesselated* Tesselate (int res) = 0;
00257 
00262   virtual void SetControlPoint (int index, int control_id) = 0;
00263 
00265   virtual void GetObjectBoundingBox (csBox3& bbox) = 0;
00266 
00272   void GetCameraBoundingBox (const csTransform& obj2cam, csBox3& cbox);
00273 
00283   float GetScreenBoundingBox (const csTransform& obj2cam,
00284         iCamera* camera, csBox3& cameraBox, csBox2& boundingBox);
00285 
00293   virtual bool IsLightable ();
00295   virtual void PosInSpace (csVector3& vec, double u, double v);
00297   virtual void Normal (csVector3& vec, double u, double v);
00298 
00300   virtual void HardTransform (const csReversibleTransform& trans);
00301 
00302   SCF_DECLARE_IBASE_EXT (csObject);
00303 
00304   //----------------------- iCurve interface implementation -----------------
00305   struct Curve : public iCurve
00306   {
00307     SCF_DECLARE_EMBEDDED_IBASE (csCurve);
00308 
00309     virtual csCurve* GetOriginalObject ()
00310     { return scfParent; }
00311     virtual iObject *QueryObject()
00312     { return scfParent; }
00313     virtual iCurveTemplate* GetParentTemplate ();
00314     virtual void SetMaterial (iMaterialWrapper* mat)
00315     { scfParent->SetMaterial (mat); }
00316     virtual iMaterialWrapper* GetMaterial ()
00317     { return scfParent->GetMaterial (); }
00318     virtual void SetName (const char* name)
00319     { scfParent->SetName (name); }
00320     virtual const char* GetName () const
00321     { return scfParent->GetName (); }
00322     virtual void SetControlPoint (int idx, int control_id)
00323     { scfParent->SetControlPoint (idx, control_id); }
00324   } scfiCurve;
00325   friend struct Curve;
00326 };
00327 
00328 
00329 SCF_VERSION (csCurveTemplate, 0, 0, 1);
00330 
00334 class csCurveTemplate : public csObject
00335 {
00336 protected:
00337   iMaterialWrapper* Material;
00338 
00339 protected:
00341   virtual ~csCurveTemplate ();
00342 
00343 public:
00345   csCurveTemplate();
00346 
00348   virtual csCurve* MakeCurve () = 0;
00349 
00351   virtual void SetVertex (int index, int ver_ind) = 0;
00353   virtual int GetVertex (int index)  = 0;
00355   virtual int GetVertexCount () = 0;
00356 
00358   iMaterialWrapper* GetMaterial () { return Material; }
00360   void SetMaterial (iMaterialWrapper* h);
00361 
00362   SCF_DECLARE_IBASE_EXT (csObject);
00363 
00364   //------------------ iCurveTemplate interface implementation --------------
00365   struct CurveTemplate : public iCurveTemplate
00366   {
00367     SCF_DECLARE_EMBEDDED_IBASE (csCurveTemplate);
00368     virtual iObject *QueryObject()
00369     { return scfParent; }
00370     virtual void SetMaterial (iMaterialWrapper* mat)
00371     { scfParent->SetMaterial (mat); }
00372     virtual iMaterialWrapper* GetMaterial ()
00373     { return scfParent->GetMaterial (); }
00374     virtual iCurve* MakeCurve ()
00375     { return &(scfParent->MakeCurve ()->scfiCurve); }
00376     virtual int GetVertexCount () const
00377     { return scfParent->GetVertexCount (); }
00378     virtual int GetVertex (int idx) const
00379     { return scfParent->GetVertex (idx); }
00380     virtual void SetVertex (int idx, int vt)
00381     { scfParent->SetVertex (idx, vt); }
00382   } scfiCurveTemplate;
00383   friend struct CurveTemplate;
00384 };
00385 
00389 class csBezierTemplate : public csCurveTemplate
00390 {
00391 private:
00392   int ver_id[9];
00393 
00394 public:
00395   csBezierTemplate();
00396 
00397   virtual csCurve* MakeCurve();
00398 
00400   virtual void SetVertex (int index, int ver_ind);
00402   virtual int GetVertex (int index);
00403   virtual int GetVertexCount ();
00404 
00405   // Should not be necessary, but without this buggy NextStep compiler
00406   // incorrectly calls csObject::QueryInterface() rather than correctly
00407   // calling csCurveTemplate::QueryInterface().
00408   SCF_DECLARE_IBASE_EXT(csCurveTemplate);
00409 };
00410 
00414 class csBezierCurve : public csCurve
00415 {
00416 private:
00418   csVector3 points[3][3];
00420   csVector2 texture_coords[3][3];
00421 
00422   double cpt[9][5];
00423 
00424   csCurveTesselated* previous_tesselation;
00425   int previous_resolution;
00426 
00428   csBox3 object_bbox;
00430   bool valid_bbox;
00431 
00432 public:
00434   csBezierCurve (csBezierTemplate* parent_tmpl);
00436   ~csBezierCurve ();
00437 
00439   virtual csCurveTesselated* Tesselate (int res);
00440 
00442   virtual void GetObjectBoundingBox (csBox3& bbox);
00443 
00445   void Load (char* buf);
00446 
00447   virtual void SetControlPoint (int index, int control_id);
00448 
00450   inline csVector3& GetControlPoint (int i) { return points[i/3][i-(i/3)*3]; }
00451 
00453   inline csVector2& GetTextureCoord (int i) { return texture_coords[i/3][i-(i/3)*3]; }
00454 
00455   virtual bool IsLightable ();
00456   virtual void PosInSpace (csVector3& vec, double u, double v);
00457   virtual void Normal (csVector3& vec, double u, double v);
00458 
00460   virtual void HardTransform (const csReversibleTransform& trans);
00461 };
00462 
00463 /*
00464  * Implementation of inline functions
00465  */
00466 
00467 inline int csCurveTesselated::GetVertexCount () const
00468 { return NumVertices; }
00469 inline int csCurveTesselated::GetTriangleCount () const
00470 { return NumTriangles; }
00471 inline csVector3* csCurveTesselated::GetVertices ()
00472 { return ObjectCoords; }
00473 inline csVector2* csCurveTesselated::GetTxtCoords ()
00474 { return TextureCoords; }
00475 inline csVector2* csCurveTesselated::GetControlPoints ()
00476 { return ControlPoints; }
00477 inline csColor* csCurveTesselated::GetColors ()
00478 { return Colors; }
00479 inline csTriangle* csCurveTesselated::GetTriangles ()
00480 { return Triangles; }
00481 inline csTriangle& csCurveTesselated::GetTriangle (int i)
00482 { return Triangles[i]; }
00483 inline bool csCurveTesselated::AreColorsValid () const
00484 { return ColorsValid; }
00485 
00486 inline unsigned long csCurve::GetCurveID () const
00487 { return CurveID; }
00488 inline iMaterialHandle* csCurve::GetMaterialHandle () const
00489 { return Material ? Material->GetMaterialHandle() : NULL; }
00490 inline iMaterialWrapper* csCurve::GetMaterial () const
00491 { return Material; }
00492 inline csCurveTemplate* csCurve::GetParentTemplate () const
00493 { return CurveTemplate; }
00494 inline csLightMap* csCurve::GetLightMap () const
00495 { return LightMap; }
00496 inline void csCurve::SetParentThing (csThing* p)
00497 { ParentThing = p; }
00498 inline csThing* csCurve::GetParentThing () const
00499 { return ParentThing; }
00500 inline const csReversibleTransform *csCurve::GetObject2World () const
00501 { return O2W; }
00502 
00503 #endif // __CS_CURVE_H__

Generated for Crystal Space by doxygen 1.2.5 written by Dimitri van Heesch, ©1997-2000