Panda3D

cMotionTrail.h

00001 // Filename: cMotionTrail.h
00002 // Created by:  aignacio (29Jan07)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef CMOTIONTRAIL_H
00016 #define CMOTIONTRAIL_H
00017 
00018 #include "directbase.h"
00019 
00020 #include "geomNode.h"
00021 #include "geomVertexData.h"
00022 #include "geomVertexWriter.h"
00023 #include "geomTriangles.h"
00024 #include "luse.h"
00025 #include "nurbsCurveEvaluator.h"
00026 
00027 #include <list>
00028 
00029 class CMotionTrailVertex {
00030 
00031 public:
00032   LVector4f _vertex;
00033   LVector4f _start_color;
00034   LVector4f _end_color;
00035   float _v;
00036   
00037   PT(NurbsCurveEvaluator) _nurbs_curve_evaluator;
00038 };
00039 
00040 class CMotionTrailFrame {
00041 
00042 public:
00043   float _time;
00044   LMatrix4f _transform;
00045 };
00046 
00047 ////////////////////////////////////////////////////////////////////
00048 //       Class : CMotionTrail
00049 // Description : The method used in creating the motion trail is 
00050 //               based on taking samples of time and transformations 
00051 //               (the position and orientation matrix) in real-time.
00052 //               The method also requires a number of vertices 
00053 //               (positions) that determines "shape" of the motion 
00054 //               trail (i.e. the edge of a blade).  A start color 
00055 //               and end color is also required for each vertex. 
00056 //               The color is interpolated as function of time.
00057 //               The colors are typically used to fade the motion
00058 //               trail so the end color is typically black.
00059 //
00060 //               The vertices are submitted via the "add_vertex" 
00061 //               function.  For each frame, a sample is submited via 
00062 //               the "update_motion_trail" function.  During the 
00063 //               "update_motion_trail" function, the motion trail 
00064 //               geometry is created dynamically from the sample 
00065 //               history and the vertices.  
00066 //
00067 //               The user must specifiy a GeomNode via 
00068 //               "set_geom_node".
00069 //
00070 //               The duration of the sample history is specified by 
00071 //               a time window. A larger time window creates longer 
00072 //               motion trails (given constant speed).  Samples that 
00073 //               are no longer within the time window are 
00074 //               automatically discarded.
00075 //
00076 //               The nurbs option can be used to create smooth 
00077 //               interpolated curves from the samples.  The nurbs 
00078 //               option is useful for animations that lack sampling 
00079 //               to begin with, animations that move very quickly, 
00080 //               or low frame rates.
00081 //
00082 //               The texture option be used to create variation to 
00083 //               the motion trail.  The u coordinate of the texture 
00084 //               corresponds to time and the v coordinate 
00085 //               corresponds to the "shape" of the motion trail.
00086 ////////////////////////////////////////////////////////////////////
00087 
00088 class EXPCL_DIRECT CMotionTrail : public TypedReferenceCount {
00089 
00090 PUBLISHED:
00091 
00092   CMotionTrail ( );
00093   ~CMotionTrail ( );
00094 
00095   void reset ( );  
00096   void reset_vertex_list ( );
00097 
00098   void enable (bool enable);
00099 
00100   void set_geom_node (PT(GeomNode) geom_node);
00101   void add_vertex (LVector4f *vertex, LVector4f *start_color, LVector4f *end_color, float v);
00102 
00103   void set_parameters (float sampling_time, float time_window, bool use_texture, bool calculate_relative_matrix, bool use_nurbs, float resolution_distance);
00104 
00105   int check_for_update (float current_time);
00106   void update_motion_trail (float current_time, LMatrix4f *transform);
00107 
00108 public:
00109 
00110   void begin_geometry ( );
00111   void add_geometry_quad (LVector3f &v0, LVector3f &v1, LVector3f &v2, LVector3f &v3, LVector4f &c0, LVector4f &c1, LVector4f &c2, LVector4f &c3, LVector2f &t0, LVector2f &t1, LVector2f &t2, LVector2f &t3);
00112   void add_geometry_quad (LVector4f &v0, LVector4f &v1, LVector4f &v2, LVector4f &v3, LVector4f &c0, LVector4f &c1, LVector4f &c2, LVector4f &c3, LVector2f &t0, LVector2f &t1, LVector2f &t2, LVector2f &t3);
00113   void end_geometry ( );
00114 
00115   int _active;
00116   int _enable;
00117 
00118   int _pause;
00119   float _pause_time;
00120 
00121   int _fade;
00122   int _fade_end;
00123   float _fade_time;
00124   float _fade_start_time;
00125   float _fade_color_scale;
00126 
00127   float _last_update_time;
00128 
00129   list <CMotionTrailVertex> _vertex_list;
00130   list <CMotionTrailFrame> _frame_list;
00131 
00132   // parameters
00133   float _color_scale;
00134   float _sampling_time;
00135   float _time_window;
00136   bool _square_t;
00137   bool _use_texture;
00138   int _calculate_relative_matrix;
00139 
00140   // nurbs parameters
00141   bool _use_nurbs;
00142   float _resolution_distance;
00143 
00144   // geom
00145   PT(GeomNode) _geom_node;
00146       
00147   // real-time data
00148   int _vertex_index;
00149   PT(GeomVertexData) _vertex_data;
00150   GeomVertexWriter _vertex_writer;
00151   GeomVertexWriter _color_writer;
00152   GeomVertexWriter _texture_writer;
00153   PT(GeomTriangles) _triangles;  
00154 
00155   CMotionTrailVertex *_vertex_array;
00156 
00157 public:
00158   static TypeHandle get_class_type() {
00159     return _type_handle;
00160   }
00161   static void init_type() {
00162     TypedReferenceCount::init_type();
00163     register_type(_type_handle, "CMotionTrail",
00164                   TypedReferenceCount::get_class_type());
00165   }
00166   virtual TypeHandle get_type() const {
00167     return get_class_type();
00168   }
00169   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00170 
00171 private:
00172   static TypeHandle _type_handle;
00173 
00174 };
00175 
00176 #endif
 All Classes Functions Variables Enumerations