home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 5164 / miletos.7z / pmesh.h < prev    next >
Encoding:
C/C++ Source or Header  |  2011-04-02  |  5.9 KB  |  220 lines

  1. #ifndef __MILETOS_PMESH_H__
  2. #define __MILETOS_PMESH_H__
  3.  
  4. //
  5. // Libmiletos
  6. //
  7. // Copyright (C) Lauris Kaplinski 2008
  8. //
  9.  
  10. #include <string>
  11.  
  12. #include <elea/color.h>
  13. #include <elea/quaternion.h>
  14.  
  15. #include <miletos/types.h>
  16. #include <miletos/scene.h>
  17.  
  18. namespace Miletos {
  19. #if 0
  20. class Skeleton;
  21.  
  22. class PMesh : public Item {
  23. public:
  24.     static const int MESH_DEFINITION_CHANGED = 64;
  25.     // static const int SKELETON_MODIFIED = 256;
  26.     // static const int MAXBONESPERVERTEX = 10;
  27.     struct PBone {
  28.         std::string id;
  29.         // -1 for root bone
  30.         int parent;
  31.         // Bone neutral orientation in parent space
  32.         Orientation pos;
  33.         // Transformation from mesh to neutral bone space
  34.         Elea::Matrix4x4f mesh2bone;
  35.  
  36.         // Transformation from animated bone to object space
  37.         Elea::Matrix4x4f anim2object;
  38.         // Mesh animation transformation
  39.         Elea::Matrix4x4f am2o;
  40.  
  41.         // Mesh update flag
  42.         bool modified;
  43.  
  44.         // Animated bone orientation in parent space
  45.         // Orientation animpos;
  46.         // Default bounding box in object space
  47.         Elea::Cuboid3f bbox;
  48.         // Animated bounding box in scaled object space
  49.         Elea::Cuboid3f animbbox;
  50.     };
  51. #if 0
  52.     struct PBlend {
  53.         int matidx;
  54.         int nbones;
  55.         int bones[MAXBONESPERVERTEX];
  56.         int firstvertex;
  57.         int numvertices;
  58.         int firstindex;
  59.         int numindices;
  60.         std::vector<float> weights;
  61.     };
  62. #endif
  63.     struct Frag {
  64.         int matidx;
  65.         int firstindex;
  66.         int numindices;
  67.     };
  68.     struct Weight {
  69.         // u16 vertex;
  70.         u16 bone;
  71.         f32 weight;
  72.     };
  73.     struct PBoneAnimation {
  74.         int bone;
  75.         std::vector<float> times;
  76.         std::vector<Orientation> orientations;
  77.     };
  78.     struct PAnimation {
  79.         std::string name;
  80.         // Length in frames
  81.         float numframes;
  82.         // Frames per second
  83.         float fps;
  84.         // Has to be the same length and order as bones
  85.         std::vector<PBoneAnimation> boneanimations;
  86.     };
  87.     struct PMorphKey {
  88.         float time;
  89.         int mbufpos;
  90.     };
  91.     struct PMorph {
  92.         int firstvertex;
  93.         int numvertices;
  94.         std::vector<float> times;
  95.         std::vector<int> mbufpositions;
  96.     };
  97. private:
  98.     // Skeleton name
  99.     char *skelname;
  100.  
  101.     // Object implementation
  102.     virtual const Type *objectType (void);
  103.     // The following two update skeleton pointer
  104.     virtual Object *childAdded (Thera::Node *cnode, Thera::Node *rnode);
  105.     virtual void childRemoved (Thera::Node *cnode, Thera::Node *rnode);
  106.  
  107.     // Create Sehle material
  108.     virtual Sehle::Material *getMaterial (int matidx, Sehle::Engine *engine) = 0;
  109.  
  110.     // Helpers
  111.     bool findKeys (float time, const float *times, u32 ntimes, int& k0, float& w0, int& k1);
  112.     void rebuildRenderable (Sehle::StaticMesh *mesh);
  113.     void updateRenderable (Sehle::StaticMesh *mesh);
  114. protected:
  115.     // Number of renderable elements
  116.     int nvertices;
  117.     int nindices;
  118.     // Index buffer
  119.     int *indices;
  120.     // Textures
  121.     Elea::Vector2f *tbase;
  122.     // Morph data
  123.     int nmorphvertices;
  124.     Elea::Vector3f *vmorph;
  125.     Elea::Vector3f *nmorph;
  126.     // Base plus morphed state
  127.     Elea::Vector3f *vbase;
  128.     Elea::Vector3f *nbase;
  129.     // Animated state
  130.     Elea::Vector3f *vanim;
  131.     Elea::Vector3f *nanim;
  132.     Elea::Vector4f *tanim;
  133.  
  134.     std::vector<PBone> pbones;
  135.     std::vector<PAnimation> animations;
  136.     // std::vector<PBlend> blends;
  137.     std::vector<PMorph> morphs;
  138.  
  139.     std::vector<int> wcounts;
  140.     std::vector<Weight> weights;
  141.     std::vector<Frag> frags;
  142.  
  143.     // Number of Sehle materials
  144.     int nmaterials;
  145.  
  146.     // Skeleton tree
  147.     Skeleton *skeleton;
  148.  
  149.     // Constructor
  150.     PMesh (void);
  151.     // Destructor
  152.     ~PMesh (void);
  153.  
  154.     // Object implementation
  155.     virtual void build (Thera::Node *pnode, Document *doc, BuildCtx *ctx);
  156.     virtual void release (void);
  157.     virtual void set (const char *attrid, const char *val);
  158.     virtual void update (UpdateCtx *ctx, unsigned int flags);
  159.     virtual void identityAdded (Document *pdocument, const char *pidentity, Object *pobject);
  160.     virtual void identityRemoved (Document *pdocument, const char *pidentity);
  161.     // Item implementation
  162.     virtual Sehle::Renderable *show (Sehle::Graph *graph, Sehle::u32 contextmask);
  163.     virtual Item *trace (const Elea::Line3f *wray, unsigned int mask, float *distance);
  164.  
  165.     // Helpers
  166.     void clear ();
  167.     // Compute default animation transformations for bones
  168.     void initializeBoneMatrixes (void);
  169.     // Update mesh animation transforms based on anim2object
  170.     void updateBoneMatrixes (void);
  171.     // Builds default bounding box for bones
  172.     void initializeBoneBBoxes (void);
  173.     // Update bone bounding boxes based on animation
  174.     void updateBoneBBoxes (void);
  175. public:
  176.     // Type system
  177.     static const Type *type (void);
  178.  
  179.     // Signals
  180.     // The private definition data has changed
  181.     // Signals::Signal2<void, PMesh *, unsigned int> sig_definition_changed;
  182.  
  183.     // Access
  184.     const char *getName (void);
  185.     int getNumVertices (void);
  186.     int getNumTriangles (void);
  187.     int getNumBones (void);
  188.     const PBone *getBones (void) { return (!pbones.empty ()) ? &pbones[0] : NULL; }
  189.     int getNumMaterials (void);
  190.     Skeleton *getSkeleton (void) { return skeleton; }
  191.     const Elea::Vector3f *getVertices (void) { return vbase; }
  192.     const Elea::Vector3f *getNormals (void) { return nbase; }
  193.     const Elea::Vector2f *getTexCoords (void) { return tbase; }
  194.     const int *getWeightCounts (void) { return (!wcounts.empty ()) ? &wcounts[0] : NULL; }
  195.     int getNumWeights (void) { return (int) weights.size (); }
  196.     const Weight *getWeights (void) { return (!weights.empty ()) ? &weights[0] : NULL; }
  197.     const int *getIndices (void) { return indices; }
  198.     int getNumFrags (void) { return (int) frags.size (); }
  199.     const Frag *getFrags (void) { return (!frags.empty ()) ? &frags[0] : NULL; }
  200.  
  201.     // Helpers
  202.     PBone *lookupPBone (const char *sid);
  203.     // Normally to be called from inside skeleton to notify that bone animation has changed
  204.     void setPBoneAnimation (PBone *pbone, const Elea::Matrix4x4f& panim2object);
  205.     // To be called from skeleton to signal that skeleton has finished update cycle
  206.     void skeletonModified (unsigned int flags);
  207.  
  208.     // Create skeleton tree
  209.     // Only to be called during building
  210.     // fixme: move to more appropriate place
  211.     Thera::Node *buildSkeleton (Thera::Document *pthedoc);
  212.     Thera::Node *buildSkeleton (Thera::Document *pthedoc, const char *rootname);
  213.     Thera::Node *buildSkeletalBone (Thera::Document *pthedoc, int pboneidx);
  214. };
  215. #endif
  216. } // Namespace Miletos
  217.  
  218. #endif
  219.  
  220.