splines.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __SPLINES_H__
  21. #define __SPLINES_H__
  22. extern void glBox(idVec4 &color, idVec3 &point, float size);
  23. extern void glLabeledPoint(idVec4 &color, idVec3 &point, float size, const char *label);
  24. class idPointListInterface {
  25. public:
  26. idPointListInterface() { selectedPoints.Clear(); };
  27. ~idPointListInterface() {};
  28. virtual int numPoints() { return 0; }
  29. virtual void addPoint( const float x, const float y, const float z ) {}
  30. virtual void addPoint( const idVec3 &v ) {}
  31. virtual void removePoint( int index ) {}
  32. virtual idVec3 * getPoint( int index ) { return NULL; }
  33. int numSelectedPoints() { return selectedPoints.Num(); }
  34. idVec3 * getSelectedPoint( int index );
  35. int selectPointByRay( const idVec3 &origin, const idVec3 &direction, bool single );
  36. int isPointSelected( int index );
  37. int selectPoint( int index, bool single );
  38. void selectAll();
  39. void deselectAll();
  40. virtual void updateSelection( const idVec3 &move );
  41. void drawSelection();
  42. protected:
  43. idList<int> selectedPoints;
  44. };
  45. class idSplineList {
  46. friend class idCamera;
  47. public:
  48. idSplineList() { clear(); }
  49. idSplineList( const char *p ) { clear(); name = p; }
  50. ~idSplineList() { clear(); }
  51. void clearControl();
  52. void clearSpline();
  53. void parse( idParser *src );
  54. void write( idFile *f, const char *name );
  55. void clear();
  56. void initPosition( long startTime, long totalTime );
  57. const idVec3 * getPosition( long time );
  58. void draw( bool editMode );
  59. void addToRenderer();
  60. void setSelectedPoint( idVec3 *p );
  61. idVec3 * getSelectedPoint() { return selected; }
  62. void addPoint( const idVec3 &v ) { controlPoints.Append(new idVec3(v) ); dirty = true; }
  63. void addPoint( float x, float y, float z ) { controlPoints.Append(new idVec3(x, y, z)); dirty = true; }
  64. void updateSelection(const idVec3 &move);
  65. void startEdit() { editMode = true; }
  66. void stopEdit() { editMode = false; }
  67. void buildSpline();
  68. void setGranularity( float f ) { granularity = f; }
  69. float getGranularity() { return granularity; }
  70. int numPoints() { return controlPoints.Num(); }
  71. idVec3 * getPoint(int index) { assert(index >= 0 && index < controlPoints.Num()); return controlPoints[index]; }
  72. idVec3 * getSegmentPoint(int index) { assert(index >= 0 && index < splinePoints.Num()); return splinePoints[index]; }
  73. void setSegmentTime(int index, int time) { assert(index >= 0 && index < splinePoints.Num()); splineTime[index] = time; }
  74. int getSegmentTime(int index) { assert(index >= 0 && index < splinePoints.Num()); return splineTime[index]; }
  75. void addSegmentTime(int index, int time) { assert(index >= 0 && index < splinePoints.Num()); splineTime[index] += time; }
  76. float totalDistance();
  77. int getActiveSegment() { return activeSegment; }
  78. void setActiveSegment( int i ) { /* assert(i >= 0 && (splinePoints.Num() > 0 && i < splinePoints.Num())); */ activeSegment = i; }
  79. int numSegments() { return splinePoints.Num(); }
  80. void setColors(idVec4 &path, idVec4 &segment, idVec4 &control, idVec4 &active);
  81. const char * getName() { return name.c_str(); }
  82. void setName( const char *p ) { name = p; }
  83. bool validTime();
  84. void setTime( long t ) { time = t; }
  85. void setBaseTime( long t ) { baseTime = t; }
  86. protected:
  87. idStr name;
  88. float calcSpline(int step, float tension);
  89. idList<idVec3*> controlPoints;
  90. idList<idVec3*> splinePoints;
  91. idList<double> splineTime;
  92. idVec3 * selected;
  93. idVec4 pathColor, segmentColor, controlColor, activeColor;
  94. float granularity;
  95. bool editMode;
  96. bool dirty;
  97. int activeSegment;
  98. long baseTime;
  99. long time;
  100. };
  101. // time in milliseconds
  102. // velocity where 1.0 equal rough walking speed
  103. struct idVelocity {
  104. idVelocity( long start, long duration, float s ) { startTime = start; time = duration; speed = s; }
  105. long startTime;
  106. long time;
  107. float speed;
  108. };
  109. // can either be a look at or origin position for a camera
  110. class idCameraPosition : public idPointListInterface {
  111. public:
  112. idCameraPosition() { time = 0; name = "position"; }
  113. idCameraPosition( const char *p ) { name = p; }
  114. idCameraPosition( long t ) { time = t; }
  115. virtual ~idCameraPosition() { clear(); }
  116. // this can be done with RTTI syntax but i like the derived classes setting a type
  117. // makes serialization a bit easier to see
  118. //
  119. enum positionType {
  120. FIXED = 0x00,
  121. INTERPOLATED,
  122. SPLINE,
  123. POSITION_COUNT
  124. };
  125. virtual void clearVelocities();
  126. virtual void clear() { editMode = false; time = 5000; clearVelocities(); }
  127. virtual void start( long t ) { startTime = t; }
  128. long getTime() { return time; }
  129. virtual void setTime(long t) { time = t; }
  130. float getVelocity( long t );
  131. float getBaseVelocity() { return baseVelocity; }
  132. void addVelocity( long start, long duration, float speed ) { velocities.Append(new idVelocity(start, duration, speed)); }
  133. virtual const idVec3 *getPosition( long t ) { return NULL; }
  134. virtual void draw( bool editMode ) {};
  135. virtual void parse( idParser *src ) {};
  136. virtual void write( idFile *f, const char *name);
  137. virtual bool parseToken( const idStr &key, idParser *src );
  138. const char * getName() { return name.c_str(); }
  139. void setName( const char *p ) { name = p; }
  140. virtual void startEdit() { editMode = true; }
  141. virtual void stopEdit() { editMode = false; }
  142. virtual void draw() {};
  143. const char * typeStr() { return positionStr[static_cast<int>(type)]; }
  144. void calcVelocity( float distance ) { float secs = (float)time / 1000; baseVelocity = distance / secs; }
  145. protected:
  146. static const char * positionStr[POSITION_COUNT];
  147. long startTime;
  148. long time;
  149. positionType type;
  150. idStr name;
  151. bool editMode;
  152. idList<idVelocity*> velocities;
  153. float baseVelocity;
  154. };
  155. class idFixedPosition : public idCameraPosition {
  156. public:
  157. idFixedPosition() : idCameraPosition() { init(); }
  158. idFixedPosition(idVec3 p) : idCameraPosition() { init(); pos = p; }
  159. ~idFixedPosition() { }
  160. void init() { pos.Zero(); type = idCameraPosition::FIXED; }
  161. virtual void addPoint( const idVec3 &v ) { pos = v; }
  162. virtual void addPoint( const float x, const float y, const float z ) { pos.Set(x, y, z); }
  163. virtual const idVec3 *getPosition( long t ) { return &pos; }
  164. void parse( idParser *src );
  165. void write( idFile *f, const char *name );
  166. virtual int numPoints() { return 1; }
  167. virtual idVec3 * getPoint( int index ) { assert( index == 0 ); return &pos; }
  168. virtual void draw( bool editMode ) { glLabeledPoint(colorBlue, pos, (editMode) ? 5 : 3, "Fixed point"); }
  169. protected:
  170. idVec3 pos;
  171. };
  172. class idInterpolatedPosition : public idCameraPosition {
  173. public:
  174. idInterpolatedPosition() : idCameraPosition() { init(); }
  175. idInterpolatedPosition( idVec3 start, idVec3 end, long time ) : idCameraPosition(time) { init(); startPos = start; endPos = end; }
  176. ~idInterpolatedPosition() { }
  177. void init() { type = idCameraPosition::INTERPOLATED; first = true; startPos.Zero(); endPos.Zero(); }
  178. virtual const idVec3 *getPosition(long t);
  179. void parse( idParser *src );
  180. void write( idFile *f, const char *name );
  181. virtual int numPoints() { return 2; }
  182. virtual idVec3 * getPoint( int index );
  183. virtual void addPoint( const float x, const float y, const float z );
  184. virtual void addPoint( const idVec3 &v );
  185. virtual void draw( bool editMode );
  186. virtual void start( long t );
  187. protected:
  188. bool first;
  189. idVec3 startPos;
  190. idVec3 endPos;
  191. long lastTime;
  192. float distSoFar;
  193. };
  194. class idSplinePosition : public idCameraPosition {
  195. public:
  196. idSplinePosition() : idCameraPosition() { init(); }
  197. idSplinePosition( long time ) : idCameraPosition( time ) { init(); }
  198. ~idSplinePosition() { }
  199. void init() { type = idCameraPosition::SPLINE; }
  200. virtual void start( long t );
  201. virtual const idVec3 *getPosition( long t );
  202. void addControlPoint( idVec3 &v ) { target.addPoint(v); }
  203. void parse( idParser *src );
  204. void write( idFile *f, const char *name );
  205. virtual int numPoints() { return target.numPoints(); }
  206. virtual idVec3 * getPoint( int index ) { return target.getPoint(index); }
  207. virtual void addPoint( const idVec3 &v ) { target.addPoint( v ); }
  208. virtual void draw( bool editMode ) { target.draw( editMode ); }
  209. virtual void updateSelection( const idVec3 &move ) { idCameraPosition::updateSelection(move); target.buildSpline(); }
  210. protected:
  211. idSplineList target;
  212. long lastTime;
  213. float distSoFar;
  214. };
  215. class idCameraFOV {
  216. public:
  217. idCameraFOV() { time = 0; fov = 90; }
  218. idCameraFOV( int v ) { time = 0; fov = v; }
  219. idCameraFOV( int s, int e, long t ) { startFOV = s; endFOV = e; time = t; }
  220. ~idCameraFOV() { }
  221. void SetFOV( float f ) { fov = f; }
  222. float GetFOV( long t );
  223. void start( long t ) { startTime = t; }
  224. void reset( float startfov, float endfov, int start, int len );
  225. void parse( idParser *src );
  226. void write( idFile *f, const char *name );
  227. protected:
  228. float fov;
  229. float startFOV;
  230. float endFOV;
  231. int startTime;
  232. int time;
  233. int length;
  234. };
  235. class idCameraEvent {
  236. public:
  237. enum eventType {
  238. EVENT_NA = 0x00,
  239. EVENT_WAIT,
  240. EVENT_TARGETWAIT,
  241. EVENT_SPEED,
  242. EVENT_TARGET,
  243. EVENT_SNAPTARGET,
  244. EVENT_FOV,
  245. EVENT_CMD,
  246. EVENT_TRIGGER,
  247. EVENT_STOP,
  248. EVENT_CAMERA,
  249. EVENT_FADEOUT,
  250. EVENT_FADEIN,
  251. EVENT_FEATHER,
  252. EVENT_COUNT
  253. };
  254. idCameraEvent() { paramStr = ""; type = EVENT_NA; time = 0; }
  255. idCameraEvent( eventType t, const char *param, long n ) { type = t; paramStr = param; time = n; }
  256. ~idCameraEvent() { }
  257. eventType getType() { return type; }
  258. const char * typeStr() { return eventStr[static_cast<int>(type)]; }
  259. const char * getParam() { return paramStr.c_str(); }
  260. long getTime() { return time; }
  261. void setTime(long n) { time = n; }
  262. void parse( idParser *src );
  263. void write( idFile *f, const char *name );
  264. void setTriggered( bool b ) { triggered = b; }
  265. bool getTriggered() { return triggered; }
  266. static const char * eventStr[EVENT_COUNT];
  267. protected:
  268. eventType type;
  269. idStr paramStr;
  270. long time;
  271. bool triggered;
  272. };
  273. class idCameraDef {
  274. public:
  275. idCameraDef() { cameraPosition = NULL; clear(); }
  276. ~idCameraDef() { clear(); }
  277. void clear();
  278. idCameraPosition * startNewCamera(idCameraPosition::positionType type);
  279. void addEvent( idCameraEvent::eventType t, const char *param, long time );
  280. void addEvent( idCameraEvent *event );
  281. static int sortEvents( const void *p1, const void *p2 );
  282. int numEvents() { return events.Num(); }
  283. idCameraEvent * getEvent(int index) { assert(index >= 0 && index < events.Num()); return events[index]; }
  284. void parse( idParser *src );
  285. bool load( const char *filename );
  286. void save( const char *filename );
  287. void buildCamera();
  288. void addTarget( const char *name, idCameraPosition::positionType type );
  289. idCameraPosition * getActiveTarget();
  290. idCameraPosition * getActiveTarget( int index );
  291. int numTargets() { return targetPositions.Num(); }
  292. void setActiveTargetByName(const char *name);
  293. void setActiveTarget( int index );
  294. void setRunning( bool b ) { cameraRunning = b; }
  295. void setBaseTime( float f ) { baseTime = f; }
  296. float getBaseTime() { return baseTime; }
  297. float getTotalTime() { return totalTime; }
  298. void startCamera( long t );
  299. void stopCamera() { cameraRunning = true; }
  300. void getActiveSegmentInfo(int segment, idVec3 &origin, idVec3 &direction, float *fv);
  301. bool getCameraInfo(long time, idVec3 &origin, idVec3 &direction, float *fv);
  302. void draw( bool editMode );
  303. int numPoints();
  304. const idVec3 * getPoint( int index );
  305. void stopEdit();
  306. void startEdit( bool camera );
  307. bool waitEvent( int index );
  308. const char * getName() { return name.c_str(); }
  309. void setName( const char *p ) { name = p; }
  310. idCameraPosition * getPositionObj();
  311. static idCameraPosition *newFromType( idCameraPosition::positionType t );
  312. protected:
  313. idStr name;
  314. int currentCameraPosition;
  315. idVec3 lastDirection;
  316. bool cameraRunning;
  317. idCameraPosition * cameraPosition;
  318. idList<idCameraPosition*> targetPositions;
  319. idList<idCameraEvent*> events;
  320. idCameraFOV fov;
  321. int activeTarget;
  322. float totalTime;
  323. float baseTime;
  324. long startTime;
  325. bool cameraEdit;
  326. bool editMode;
  327. };
  328. extern bool g_splineMode;
  329. extern idCameraDef *g_splineList;
  330. #endif /* !__SPLINES_H__ */