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