Debian Codegraph @

libs/splines/splines.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "q_shared.h"
23 #include "splines.h"
24
25 extern "C" {
26 int FS_Write( const void *buffer, int len, fileHandle_t h );
27 int FS_ReadFile( const char *qpath, void **buffer );
28 void FS_FreeFile( void *buffer );
29 fileHandle_t FS_FOpenFileWrite( const char *filename );
30 void FS_FCloseFile( fileHandle_t f );
31 void Cbuf_AddText( const char *text );
32 void Cbuf_Execute( void );
33 }
34
35 float Q_fabs( float f ) {
36 int tmp = *( int * ) &f;
37 tmp &= 0x7FFFFFFF;
38 return *( float * ) &tmp;
39 }
40
41 // (SA) making a list of cameras so I can use
42 // the splines as targets for other things.
43 // Certainly better ways to do this, but this lets
44 // me get underway quickly with ents that need spline
45 // targets.
46 const int MAX_CAMERAS = 64;
47
48 idCameraDef camera[MAX_CAMERAS];
49
50 extern "C" {
51 qboolean loadCamera( int camNum, const char *name ) {
52 if ( camNum < 0 || camNum >= MAX_CAMERAS ) {
53 return qfalse;
54 }
55 camera[camNum].clear();
56 return (qboolean)camera[camNum].load( name );
57 }
58
59 qboolean getCameraInfo( int camNum, int time, float *origin, float *angles, float *fov ) {
60 idVec3 dir, org;
61 if ( camNum < 0 || camNum >= MAX_CAMERAS ) {
62 return qfalse;
63 }
64 org[0] = origin[0];
65 org[1] = origin[1];
66 org[2] = origin[2];
67 if ( camera[camNum].getCameraInfo( time, org, dir, fov ) ) {
68 origin[0] = org[0];
69 origin[1] = org[1];
70 origin[2] = org[2];
71 angles[1] = atan2( dir[1], dir[0] ) * 180 / 3.14159;
72 angles[0] = asin( dir[2] ) * 180 / 3.14159;
73 return qtrue;
74 }
75 return qfalse;
76 }
77
78 void startCamera( int camNum, int time ) {
79 if ( camNum < 0 || camNum >= MAX_CAMERAS ) {
80 return;
81 }
82 camera[camNum].startCamera( time );
83 }
84
85 }
86
87
88 //#include "../shared/windings.h"
89 //#include "../qcommon/qcommon.h"
90 //#include "../sys/sys_public.h"
91 //#include "../game/game_entity.h"
92
93 idCameraDef splineList;
94 idCameraDef *g_splineList = &splineList;
95
96 idVec3 idSplineList::zero( 0,0,0 );
97
98 void glLabeledPoint( idVec3 &color, idVec3 &point, float size, const char *label ) {
99 glColor3fv( color );
100 glPointSize( size );
101 glBegin( GL_POINTS );
102 glVertex3fv( point );
103 glEnd();
104 idVec3 v = point;
105 v.x += 1;
106 v.y += 1;
107 v.z += 1;
108 glRasterPos3fv( v );
109 glCallLists( strlen( label ), GL_UNSIGNED_BYTE, label );
110 }
111
112
113 void glBox( idVec3 &color, idVec3 &point, float size ) {
114 idVec3 mins( point );
115 idVec3 maxs( point );
116 mins[0] -= size;
117 mins[1] += size;
118 mins[2] -= size;
119 maxs[0] += size;
120 maxs[1] -= size;
121 maxs[2] += size;
122 glColor3fv( color );
123 glBegin( GL_LINE_LOOP );
124 glVertex3f( mins[0],mins[1],mins[2] );
125 glVertex3f( maxs[0],mins[1],mins[2] );
126 glVertex3f( maxs[0],maxs[1],mins[2] );
127 glVertex3f( mins[0],maxs[1],mins[2] );
128 glEnd();
129 glBegin( GL_LINE_LOOP );
130 glVertex3f( mins[0],mins[1],maxs[2] );
131 glVertex3f( maxs[0],mins[1],maxs[2] );
132 glVertex3f( maxs[0],maxs[1],maxs[2] );
133 glVertex3f( mins[0],maxs[1],maxs[2] );
134 glEnd();
135
136 glBegin( GL_LINES );
137 glVertex3f( mins[0],mins[1],mins[2] );
138 glVertex3f( mins[0],mins[1],maxs[2] );
139 glVertex3f( mins[0],maxs[1],maxs[2] );
140 glVertex3f( mins[0],maxs[1],mins[2] );
141 glVertex3f( maxs[0],mins[1],mins[2] );
142 glVertex3f( maxs[0],mins[1],maxs[2] );
143 glVertex3f( maxs[0],maxs[1],maxs[2] );
144 glVertex3f( maxs[0],maxs[1],mins[2] );
145 glEnd();
146
147 }
148
149 void splineTest() {
150 //g_splineList->load("p:/doom/base/maps/test_base1.camera");
151 }
152
153 void splineDraw() {
154 //g_splineList->addToRenderer();
155 }
156
157
158 //extern void D_DebugLine( const idVec3 &color, const idVec3 &start, const idVec3 &end );
159
160 void debugLine( idVec3 &color, float x, float y, float z, float x2, float y2, float z2 ) {
161 idVec3 from( x, y, z );
162 idVec3 to( x2, y2, z2 );
163 //D_DebugLine(color, from, to);
164 }
165
166 void idSplineList::addToRenderer() {
167
168 if ( controlPoints.Num() == 0 ) {
169 return;
170 }
171
172 idVec3 mins, maxs;
173 idVec3 yellow( 1.0, 1.0, 0 );
174 idVec3 white( 1.0, 1.0, 1.0 );
175 int i;
176
177 for ( i = 0; i < controlPoints.Num(); i++ ) {
178 VectorCopy( *controlPoints[i], mins );
179 VectorCopy( mins, maxs );
180 mins[0] -= 8;
181 mins[1] += 8;
182 mins[2] -= 8;
183 maxs[0] += 8;
184 maxs[1] -= 8;
185 maxs[2] += 8;
186 debugLine( yellow, mins[0], mins[1], mins[2], maxs[0], mins[1], mins[2] );
187 debugLine( yellow, maxs[0], mins[1], mins[2], maxs[0], maxs[1], mins[2] );
188 debugLine( yellow, maxs[0], maxs[1], mins[2], mins[0], maxs[1], mins[2] );
189 debugLine( yellow, mins[0], maxs[1], mins[2], mins[0], mins[1], mins[2] );
190
191 debugLine( yellow, mins[0], mins[1], maxs[2], maxs[0], mins[1], maxs[2] );
192 debugLine( yellow, maxs[0], mins[1], maxs[2], maxs[0], maxs[1], maxs[2] );
193 debugLine( yellow, maxs[0], maxs[1], maxs[2], mins[0], maxs[1], maxs[2] );
194 debugLine( yellow, mins[0], maxs[1], maxs[2], mins[0], mins[1], maxs[2] );
195
196 }
197
198 int step = 0;
199 idVec3 step1;
200 for ( i = 3; i < controlPoints.Num(); i++ ) {
201 for ( float tension = 0.0f; tension < 1.001f; tension += 0.1f ) {
202 float x = 0;
203 float y = 0;
204 float z = 0;
205 for ( int j = 0; j < 4; j++ ) {
206 x += controlPoints[i - ( 3 - j )]->x * calcSpline( j, tension );
207 y += controlPoints[i - ( 3 - j )]->y * calcSpline( j, tension );
208 z += controlPoints[i - ( 3 - j )]->z * calcSpline( j, tension );
209 }
210 if ( step == 0 ) {
211 step1[0] = x;
212 step1[1] = y;
213 step1[2] = z;
214 step = 1;
215 }
216 else {
217 debugLine( white, step1[0], step1[1], step1[2], x, y, z );
218 step = 0;
219 }
220
221 }
222 }
223 }
224
225 void idSplineList::buildSpline() {
226 //int start = Sys_Milliseconds();
227 clearSpline();
228 for ( int i = 3; i < controlPoints.Num(); i++ ) {
229 for ( float tension = 0.0f; tension < 1.001f; tension += granularity ) {
230 float x = 0;
231 float y = 0;
232 float z = 0;
233 for ( int j = 0; j < 4; j++ ) {
234 x += controlPoints[i - ( 3 - j )]->x * calcSpline( j, tension );
235 y += controlPoints[i - ( 3 - j )]->y * calcSpline( j, tension );
236 z += controlPoints[i - ( 3 - j )]->z * calcSpline( j, tension );
237 }
238 splinePoints.Append( new idVec3( x, y, z ) );
239 }
240 }
241 dirty = false;
242 //Com_Printf("Spline build took %f seconds\n", (float)(Sys_Milliseconds() - start) / 1000);
243 }
244
245
246 void idSplineList::draw( bool editMode ) {
247 int i;
248 idVec4 yellow( 1, 1, 0, 1 );
249
250 if ( controlPoints.Num() == 0 ) {
251 return;
252 }
253
254 if ( dirty ) {
255 buildSpline();
256 }
257
258
259 glColor3fv( controlColor );
260 glPointSize( 5 );
261
262 glBegin( GL_POINTS );
263 for ( i = 0; i < controlPoints.Num(); i++ ) {
264 glVertex3fv( *controlPoints[i] );
265 }
266 glEnd();
267
268 if ( editMode ) {
269 for ( i = 0; i < controlPoints.Num(); i++ ) {
270 glBox( activeColor, *controlPoints[i], 4 );
271 }
272 }
273
274 //Draw the curve
275 glColor3fv( pathColor );
276 glBegin( GL_LINE_STRIP );
277 int count = splinePoints.Num();
278 for ( i = 0; i < count; i++ ) {
279 glVertex3fv( *splinePoints[i] );
280 }
281 glEnd();
282
283 if ( editMode ) {
284 glColor3fv( segmentColor );
285 glPointSize( 3 );
286 glBegin( GL_POINTS );
287 for ( i = 0; i < count; i++ ) {
288 glVertex3fv( *splinePoints[i] );
289 }
290 glEnd();
291 }
292 if ( count > 0 ) {
293 //assert(activeSegment >=0 && activeSegment < count);
294 if ( activeSegment >= 0 && activeSegment < count ) {
295 glBox( activeColor, *splinePoints[activeSegment], 6 );
296 glBox( yellow, *splinePoints[activeSegment], 8 );
297 }
298 }
299
300 }
301
302 float idSplineList::totalDistance() {
303
304 // FIXME: save dist and return
305 //
306 if ( controlPoints.Num() == 0 ) {
307 return 0.0;
308 }
309
310 if ( dirty ) {
311 buildSpline();
312 }
313
314 float dist = 0.0;
315 idVec3 temp;
316 int count = splinePoints.Num();
317 for ( int i = 1; i < count; i++ ) {
318 temp = *splinePoints[i - 1];
319 temp -= *splinePoints[i];
320 dist += temp.Length();
321 }
322 return dist;
323 }
324
325 void idSplineList::initPosition( long bt, long totalTime ) {
326
327 if ( dirty ) {
328 buildSpline();
329 }
330
331 if ( splinePoints.Num() == 0 ) {
332 return;
333 }
334
335 baseTime = bt;
336 time = totalTime;
337
338 // calc distance to travel ( this will soon be broken into time segments )
339 splineTime.Clear();
340 splineTime.Append( bt );
341 double dist = totalDistance();
342 double distSoFar = 0.0;
343 idVec3 temp;
344 int count = splinePoints.Num();
345 //for(int i = 2; i < count - 1; i++) {
346 for ( int i = 1; i < count; i++ ) {
347 temp = *splinePoints[i - 1];
348 temp -= *splinePoints[i];
349 distSoFar += temp.Length();
350 double percent = distSoFar / dist;
351 percent *= totalTime;
352 splineTime.Append( percent + bt );
353 }
354 assert( splineTime.Num() == splinePoints.Num() );
355 activeSegment = 0;
356 }
357
358
359
360 float idSplineList::calcSpline( int step, float tension ) {
361 switch ( step ) {
362 case 0: return ( pow( 1 - tension, 3 ) ) / 6;
363 case 1: return ( 3 * pow( tension, 3 ) - 6 * pow( tension, 2 ) + 4 ) / 6;
364 case 2: return ( -3 * pow( tension, 3 ) + 3 * pow( tension, 2 ) + 3 * tension + 1 ) / 6;
365 case 3: return pow( tension, 3 ) / 6;
366 }
367 return 0.0;
368 }
369
370
371
372 void idSplineList::updateSelection( const idVec3 &move ) {
373 if ( selected ) {
374 dirty = true;
375 VectorAdd( *selected, move, *selected );
376 }
377 }
378
379
380 void idSplineList::setSelectedPoint( idVec3 *p ) {
381 if ( p ) {
382 p->Snap();
383 for ( int i = 0; i < controlPoints.Num(); i++ ) {
384 if ( *p == *controlPoints[i] ) {
385 selected = controlPoints[i];
386 }
387 }
388 }
389 else {
390 selected = NULL;
391 }
392 }
393
394 const idVec3 *idSplineList::getPosition( long t ) {
395 static idVec3 interpolatedPos;
396
397 int count = splineTime.Num();
398 if ( count == 0 ) {
399 return &zero;
400 }
401
402 // Com_Printf("Time: %d\n", t);
403 assert( splineTime.Num() == splinePoints.Num() );
404
405 while ( activeSegment < count ) {
406 if ( splineTime[activeSegment] >= t ) {
407 if ( activeSegment > 0 && activeSegment < count - 1 ) {
408 double timeHi = splineTime[activeSegment + 1];
409 double timeLo = splineTime[activeSegment - 1];
410 double percent = ( timeHi - t ) / ( timeHi - timeLo );
411 // pick two bounding points
412 idVec3 v1 = *splinePoints[activeSegment - 1];
413 idVec3 v2 = *splinePoints[activeSegment + 1];
414 v2 *= ( 1.0 - percent );
415 v1 *= percent;
416 v2 += v1;
417 interpolatedPos = v2;
418 return &interpolatedPos;
419 }
420 return splinePoints[activeSegment];
421 }
422 else {
423 activeSegment++;
424 }
425 }
426 return splinePoints[count - 1];
427 }
428
429 void idSplineList::parse( const char *( *text ) ) {
430 const char *token;
431 //Com_MatchToken( text, "{" );
432 do {
433 token = Com_Parse( text );
434
435 if ( !token[0] ) {
436 break;
437 }
438 if ( !Q_stricmp( token, "}" ) ) {
439 break;
440 }
441
442 do {
443 // if token is not a brace, it is a key for a key/value pair
444 if ( !token[0] || !Q_stricmp( token, "(" ) || !Q_stricmp( token, "}" ) ) {
445 break;
446 }
447
448 Com_UngetToken();
449 idStr key = Com_ParseOnLine( text );
450 const char *token = Com_Parse( text );
451 if ( Q_stricmp( key.c_str(), "granularity" ) == 0 ) {
452 granularity = atof( token );
453 }
454 else if ( Q_stricmp( key.c_str(), "name" ) == 0 ) {
455 name = token;
456 }
457 token = Com_Parse( text );
458
459 } while ( 1 );
460
461 if ( !Q_stricmp( token, "}" ) ) {
462 break;
463 }
464
465 Com_UngetToken();
466 // read the control point
467 idVec3 point;
468 Com_Parse1DMatrix( text, 3, point );
469 addPoint( point.x, point.y, point.z );
470 } while ( 1 );
471
472 //Com_UngetToken();
473 //Com_MatchToken( text, "}" );
474 dirty = true;
475 }
476
477 void idSplineList::write( fileHandle_t file, const char *p ) {
478 idStr s = va( "\t\t%s {\n", p );
479 FS_Write( s.c_str(), s.length(), file );
480 //s = va("\t\tname %s\n", name.c_str());
481 //FS_Write(s.c_str(), s.length(), file);
482 s = va( "\t\t\tgranularity %f\n", granularity );
483 FS_Write( s.c_str(), s.length(), file );
484 int count = controlPoints.Num();
485 for ( int i = 0; i < count; i++ ) {
486 s = va( "\t\t\t( %f %f %f )\n", controlPoints[i]->x, controlPoints[i]->y, controlPoints[i]->z );
487 FS_Write( s.c_str(), s.length(), file );
488 }
489 s = "\t\t}\n";
490 FS_Write( s.c_str(), s.length(), file );
491 }
492
493
494 void idCameraDef::getActiveSegmentInfo( int segment, idVec3 &origin, idVec3 &direction, float *fov ) {
495 #if 0
496 if ( !cameraSpline.validTime() ) {
497 buildCamera();
498 }
499 double d = (double)segment / numSegments();
500 getCameraInfo( d * totalTime * 1000, origin, direction, fov );
501 #endif
502 /*
503 if (!cameraSpline.validTime()) {
504 buildCamera();
505 }
506 origin = *cameraSpline.getSegmentPoint(segment);
507
508
509 idVec3 temp;
510
511 int numTargets = getTargetSpline()->controlPoints.Num();
512 int count = cameraSpline.splineTime.Num();
513 if (numTargets == 0) {
514 // follow the path
515 if (cameraSpline.getActiveSegment() < count - 1) {
516 temp = *cameraSpline.splinePoints[cameraSpline.getActiveSegment()+1];
517 }
518 } else if (numTargets == 1) {
519 temp = *getTargetSpline()->controlPoints[0];
520 } else {
521 temp = *getTargetSpline()->getSegmentPoint(segment);
522 }
523
524 temp -= origin;
525 temp.Normalize();
526 direction = temp;
527 */
528 }
529
530 bool idCameraDef::getCameraInfo( long time, idVec3 &origin, idVec3 &direction, float *fv ) {
531
532 char buff[1024];
533
534 if ( ( time - startTime ) / 1000 > totalTime ) {
535 return false;
536 }
537
538
539 for ( int i = 0; i < events.Num(); i++ ) {
540 if ( time >= startTime + events[i]->getTime() && !events[i]->getTriggered() ) {
541 events[i]->setTriggered( true );
542 if ( events[i]->getType() == idCameraEvent::EVENT_TARGET ) {
543 setActiveTargetByName( events[i]->getParam() );
544 getActiveTarget()->start( startTime + events[i]->getTime() );
545 //Com_Printf("Triggered event switch to target: %s\n",events[i]->getParam());
546 }
547 else if ( events[i]->getType() == idCameraEvent::EVENT_TRIGGER ) {
548 //idEntity *ent = NULL;
549 //ent = level.FindTarget( ent, events[i]->getParam());
550 //if (ent) {
551 // ent->signal( SIG_TRIGGER );
552 // ent->ProcessEvent( &EV_Activate, world );
553 //}
554 }
555 else if ( events[i]->getType() == idCameraEvent::EVENT_FOV ) {
556 memset( buff, 0, sizeof( buff ) );
557 strcpy( buff, events[i]->getParam() );
558 const char *param1 = strtok( buff, " \t,\0" );
559 const char *param2 = strtok( NULL, " \t,\0" );
560 float len = ( param2 ) ? atof( param2 ) : 0;
561 float newfov = ( param1 ) ? atof( param1 ) : 90;
562 fov.reset( fov.getFOV( time ), newfov, time, len );
563 //*fv = fov = atof(events[i]->getParam());
564 }
565 else if ( events[i]->getType() == idCameraEvent::EVENT_FADEIN ) {
566 float time = atof( events[i]->getParam() );
567 Cbuf_AddText( va( "fade 0 0 0 0 %f", time ) );
568 Cbuf_Execute();
569 }
570 else if ( events[i]->getType() == idCameraEvent::EVENT_FADEOUT ) {
571 float time = atof( events[i]->getParam() );
572 Cbuf_AddText( va( "fade 0 0 0 255 %f", time ) );
573 Cbuf_Execute();
574 }
575 else if ( events[i]->getType() == idCameraEvent::EVENT_CAMERA ) {
576 memset( buff, 0, sizeof( buff ) );
577 strcpy( buff, events[i]->getParam() );
578 const char *param1 = strtok( buff, " \t,\0" );
579 const char *param2 = strtok( NULL, " \t,\0" );
580
581 if ( param2 ) {
582 loadCamera( atoi( param1 ), va( "cameras/%s.camera", param2 ) );
583 startCamera( time );
584 }
585 else {
586 loadCamera( 0, va( "cameras/%s.camera", events[i]->getParam() ) );
587 startCamera( time );
588 }
589 return true;
590 }
591 else if ( events[i]->getType() == idCameraEvent::EVENT_STOP ) {
592 return false;
593 }
594 }
595 }
596
597 origin = *cameraPosition->getPosition( time );
598
599 *fv = fov.getFOV( time );
600
601 idVec3 temp = origin;
602
603 int numTargets = targetPositions.Num();
604 if ( numTargets == 0 ) {
605 /*
606 // follow the path
607 if (cameraSpline.getActiveSegment() < count - 1) {
608 temp = *cameraSpline.splinePoints[cameraSpline.getActiveSegment()+1];
609 if (temp == origin) {
610 int index = cameraSpline.getActiveSegment() + 2;
611 while (temp == origin && index < count - 1) {
612 temp = *cameraSpline.splinePoints[index++];
613 }
614 }
615 }
616 */
617 }
618 else {
619 if ( getActiveTarget()->numPoints() > 0 ) {
620 temp = *getActiveTarget()->getPosition( time );
621 }
622 }
623
624 temp -= origin;
625 temp.Normalize();
626 direction = temp;
627
628 return true;
629 }
630
631 bool idCameraDef::waitEvent( int index ) {
632 //for (int i = 0; i < events.Num(); i++) {
633 // if (events[i]->getSegment() == index && events[i]->getType() == idCameraEvent::EVENT_WAIT) {
634 // return true;
635 // }
636 //}
637 return false;
638 }
639
640
641 const int NUM_CCELERATION_SEGS = 10;
642 const int CELL_AMT = 5;
643
644 void idCameraDef::buildCamera() {
645 int i;
646 idList<float> waits;
647 idList<int> targets;
648
649 totalTime = baseTime;
650 cameraPosition->setTime( (long)totalTime * 1000 );
651 // we have a base time layout for the path and the target path
652 // now we need to layer on any wait or speed changes
653 for ( i = 0; i < events.Num(); i++ ) {
654 events[i]->setTriggered( false );
655 switch ( events[i]->getType() ) {
656 default: break;
657 case idCameraEvent::EVENT_TARGET: {
658 targets.Append( i );
659 break;
660 }
661 case idCameraEvent::EVENT_FEATHER: {
662 long startTime = 0;
663 float speed = 0;
664 long loopTime = 10;
665 float stepGoal = cameraPosition->getBaseVelocity() / ( 1000 / loopTime );
666 while ( startTime <= 1000 ) {
667 cameraPosition->addVelocity( startTime, loopTime, speed );
668 speed += stepGoal;
669 if ( speed > cameraPosition->getBaseVelocity() ) {
670 speed = cameraPosition->getBaseVelocity();
671 }
672 startTime += loopTime;
673 }
674
675 startTime = (long)( totalTime * 1000 - 1000 );
676 long endTime = startTime + 1000;
677 speed = cameraPosition->getBaseVelocity();
678 while ( startTime < endTime ) {
679 speed -= stepGoal;
680 if ( speed < 0 ) {
681 speed = 0;
682 }
683 cameraPosition->addVelocity( startTime, loopTime, speed );
684 startTime += loopTime;
685 }
686 break;
687
688 }
689 case idCameraEvent::EVENT_WAIT: {
690 waits.Append( atof( events[i]->getParam() ) );
691
692 //FIXME: this is quite hacky for Wolf E3, accel and decel needs
693 // do be parameter based etc..
694 long startTime = events[i]->getTime() - 1000;
695 if ( startTime < 0 ) {
696 startTime = 0;
697 }
698 float speed = cameraPosition->getBaseVelocity();
699 long loopTime = 10;
700 float steps = speed / ( ( events[i]->getTime() - startTime ) / loopTime );
701 while ( startTime <= events[i]->getTime() - loopTime ) {
702 cameraPosition->addVelocity( startTime, loopTime, speed );
703 speed -= steps;
704 startTime += loopTime;
705 }
706 cameraPosition->addVelocity( events[i]->getTime(), (long)atof( events[i]->getParam() ) * 1000, 0 );
707
708 startTime = (long)( events[i]->getTime() + atof( events[i]->getParam() ) * 1000 );
709 long endTime = startTime + 1000;
710 speed = 0;
711 while ( startTime <= endTime ) {
712 cameraPosition->addVelocity( startTime, loopTime, speed );
713 speed += steps;
714 startTime += loopTime;
715 }
716 break;
717 }
718 case idCameraEvent::EVENT_TARGETWAIT: {
719 //targetWaits.Append(i);
720 break;
721 }
722 case idCameraEvent::EVENT_SPEED: {
723 /*
724 // take the average delay between up to the next five segments
725 float adjust = atof(events[i]->getParam());
726 int index = events[i]->getSegment();
727 total = 0;
728 count = 0;
729
730 // get total amount of time over the remainder of the segment
731 for (j = index; j < cameraSpline.numSegments() - 1; j++) {
732 total += cameraSpline.getSegmentTime(j + 1) - cameraSpline.getSegmentTime(j);
733 count++;
734 }
735
736 // multiply that by the adjustment
737 double newTotal = total * adjust;
738 // what is the difference..
739 newTotal -= total;
740 totalTime += newTotal / 1000;
741
742 // per segment difference
743 newTotal /= count;
744 int additive = newTotal;
745
746 // now propogate that difference out to each segment
747 for (j = index; j < cameraSpline.numSegments(); j++) {
748 cameraSpline.addSegmentTime(j, additive);
749 additive += newTotal;
750 }
751 break;
752 */
753 }
754 }
755 }
756
757
758 for ( i = 0; i < waits.Num(); i++ ) {
759 totalTime += waits[i];
760 }
761
762 // on a new target switch, we need to take time to this point ( since last target switch )
763 // and allocate it across the active target, then reset time to this point
764 long timeSoFar = 0;
765 long total = (long)( totalTime * 1000 );
766 for ( i = 0; i < targets.Num(); i++ ) {
767 long t;
768 if ( i < targets.Num() - 1 ) {
769 t = events[targets[i + 1]]->getTime();
770 }
771 else {
772 t = total - timeSoFar;
773 }
774 // t is how much time to use for this target
775 setActiveTargetByName( events[targets[i]]->getParam() );
776 getActiveTarget()->setTime( t );
777 timeSoFar += t;
778 }
779
780
781 }
782
783 void idCameraDef::startCamera( long t ) {
784 cameraPosition->clearVelocities();
785 cameraPosition->start( t );
786 buildCamera();
787 fov.reset( 90, 90, t, 0 );
788 //for (int i = 0; i < targetPositions.Num(); i++) {
789 // targetPositions[i]->
790 //}
791 startTime = t;
792 cameraRunning = true;
793 }
794
795
796 void idCameraDef::parse( const char *( *text ) ) {
797
798 const char *token;
799 do {
800 token = Com_Parse( text );
801
802 if ( !token[0] ) {
803 break;
804 }
805 if ( !Q_stricmp( token, "}" ) ) {
806 break;
807 }
808
809 if ( Q_stricmp( token, "time" ) == 0 ) {
810 baseTime = Com_ParseFloat( text );
811 }
812 else if ( Q_stricmp( token, "camera_fixed" ) == 0 ) {
813 cameraPosition = new idFixedPosition();
814 cameraPosition->parse( text );
815 }
816 else if ( Q_stricmp( token, "camera_interpolated" ) == 0 ) {
817 cameraPosition = new idInterpolatedPosition();
818 cameraPosition->parse( text );
819 }
820 else if ( Q_stricmp( token, "camera_spline" ) == 0 ) {
821 cameraPosition = new idSplinePosition();
822 cameraPosition->parse( text );
823 }
824 else if ( Q_stricmp( token, "target_fixed" ) == 0 ) {
825 idFixedPosition *pos = new idFixedPosition();
826 pos->parse( text );
827 targetPositions.Append( pos );
828 }
829 else if ( Q_stricmp( token, "target_interpolated" ) == 0 ) {
830 idInterpolatedPosition *pos = new idInterpolatedPosition();
831 pos->parse( text );
832 targetPositions.Append( pos );
833 }
834 else if ( Q_stricmp( token, "target_spline" ) == 0 ) {
835 idSplinePosition *pos = new idSplinePosition();
836 pos->parse( text );
837 targetPositions.Append( pos );
838 }
839 else if ( Q_stricmp( token, "fov" ) == 0 ) {
840 fov.parse( text );
841 }
842 else if ( Q_stricmp( token, "event" ) == 0 ) {
843 idCameraEvent *event = new idCameraEvent();
844 event->parse( text );
845 addEvent( event );
846 }
847
848
849 } while ( 1 );
850
851 if ( !cameraPosition ) {
852 Com_Printf( "no camera position specified\n" );
853 // prevent a crash later on
854 cameraPosition = new idFixedPosition();
855 }
856
857 Com_UngetToken();
858 Com_MatchToken( text, "}" );
859
860 }
861
862 bool idCameraDef::load( const char *filename ) {
863 char *buf;
864 const char *buf_p;
865
866 FS_ReadFile( filename, (void **)&buf );
867 if ( !buf ) {
868 return false;
869 }
870
871 clear();
872 Com_BeginParseSession( filename );
873 buf_p = buf;
874 parse( &buf_p );
875 Com_EndParseSession();
876 FS_FreeFile( buf );
877
878 return true;
879 }
880
881 void idCameraDef::save( const char *filename ) {
882 fileHandle_t file = FS_FOpenFileWrite( filename );
883 if ( file ) {
884 int i;
885 idStr s = "cameraPathDef { \n";
886 FS_Write( s.c_str(), s.length(), file );
887 s = va( "\ttime %f\n", baseTime );
888 FS_Write( s.c_str(), s.length(), file );
889
890 cameraPosition->write( file, va( "camera_%s",cameraPosition->typeStr() ) );
891
892 for ( i = 0; i < numTargets(); i++ ) {
893 targetPositions[i]->write( file, va( "target_%s", targetPositions[i]->typeStr() ) );
894 }
895
896 for ( i = 0; i < events.Num(); i++ ) {
897 events[i]->write( file, "event" );
898 }
899
900 fov.write( file, "fov" );
901
902 s = "}\n";
903 FS_Write( s.c_str(), s.length(), file );
904 }
905 FS_FCloseFile( file );
906 }
907
908 int idCameraDef::sortEvents( const void *p1, const void *p2 ) {
909 idCameraEvent *ev1 = (idCameraEvent*)( p1 );
910 idCameraEvent *ev2 = (idCameraEvent*)( p2 );
911
912 if ( ev1->getTime() > ev2->getTime() ) {
913 return -1;
914 }
915 if ( ev1->getTime() < ev2->getTime() ) {
916 return 1;
917 }
918 return 0;
919 }
920
921 void idCameraDef::addEvent( idCameraEvent *event ) {
922 events.Append( event );
923 //events.Sort(&sortEvents);
924
925 }
926 void idCameraDef::addEvent( idCameraEvent::eventType t, const char *param, long time ) {
927 addEvent( new idCameraEvent( t, param, time ) );
928 buildCamera();
929 }
930
931 void idCameraDef::removeEvent( int index ) {
932 events.RemoveIndex( index );
933 buildCamera();
934 }
935
936
937 const char *idCameraEvent::eventStr[] = {
938 "NA",
939 "WAIT",
940 "TARGETWAIT",
941 "SPEED",
942 "TARGET",
943 "SNAPTARGET",
944 "FOV",
945 "CMD",
946 "TRIGGER",
947 "STOP",
948 "CAMERA",
949 "FADEOUT",
950 "FADEIN",
951 "FEATHER"
952 };
953
954 void idCameraEvent::parse( const char *( *text ) ) {
955 const char *token;
956 Com_MatchToken( text, "{" );
957 do {
958 token = Com_Parse( text );
959
960 if ( !token[0] ) {
961 break;
962 }
963 if ( !strcmp( token, "}" ) ) {
964 break;
965 }
966
967 // here we may have to jump over brush epairs ( only used in editor )
968 do {
969 // if token is not a brace, it is a key for a key/value pair
970 if ( !token[0] || !strcmp( token, "(" ) || !strcmp( token, "}" ) ) {
971 break;
972 }
973
974 Com_UngetToken();
975 idStr key = Com_ParseOnLine( text );
976 const char *token = Com_Parse( text );
977 if ( Q_stricmp( key.c_str(), "type" ) == 0 ) {
978 type = static_cast<idCameraEvent::eventType>( atoi( token ) );
979 }
980 else if ( Q_stricmp( key.c_str(), "param" ) == 0 ) {
981 paramStr = token;
982 }
983 else if ( Q_stricmp( key.c_str(), "time" ) == 0 ) {
984 time = atoi( token );
985 }
986 token = Com_Parse( text );
987
988 } while ( 1 );
989
990 if ( !strcmp( token, "}" ) ) {
991 break;
992 }
993
994 } while ( 1 );
995
996 Com_UngetToken();
997 Com_MatchToken( text, "}" );
998 }
999
1000 void idCameraEvent::write( fileHandle_t file, const char *name ) {
1001 idStr s = va( "\t%s {\n", name );
1002 FS_Write( s.c_str(), s.length(), file );
1003 s = va( "\t\ttype %d\n", static_cast<int>( type ) );
1004 FS_Write( s.c_str(), s.length(), file );
1005 s = va( "\t\tparam \"%s\"\n", paramStr.c_str() );
1006 FS_Write( s.c_str(), s.length(), file );
1007 s = va( "\t\ttime %d\n", time );
1008 FS_Write( s.c_str(), s.length(), file );
1009 s = "\t}\n";
1010 FS_Write( s.c_str(), s.length(), file );
1011 }
1012
1013
1014 const char *idCameraPosition::positionStr[] = {
1015 "Fixed",
1016 "Interpolated",
1017 "Spline",
1018 };
1019
1020
1021
1022 const idVec3 *idInterpolatedPosition::getPosition( long t ) {
1023 static idVec3 interpolatedPos;
1024
1025 float velocity = getVelocity( t );
1026 float timePassed = t - lastTime;
1027 lastTime = t;
1028
1029 // convert to seconds
1030 timePassed /= 1000;
1031
1032 float distToTravel = timePassed * velocity;
1033
1034 idVec3 temp = startPos;
1035 temp -= endPos;
1036 float distance = temp.Length();
1037
1038 distSoFar += distToTravel;
1039 float percent = (float)( distSoFar ) / distance;
1040
1041 if ( percent > 1.0 ) {
1042 percent = 1.0;
1043 }
1044 else if ( percent < 0.0 ) {
1045 percent = 0.0;
1046 }
1047
1048 // the following line does a straigt calc on percentage of time
1049 // float percent = (float)(startTime + time - t) / time;
1050
1051 idVec3 v1 = startPos;
1052 idVec3 v2 = endPos;
1053 v1 *= ( 1.0 - percent );
1054 v2 *= percent;
1055 v1 += v2;
1056 interpolatedPos = v1;
1057 return &interpolatedPos;
1058 }
1059
1060
1061 void idCameraFOV::parse( const char *( *text ) ) {
1062 const char *token;
1063 Com_MatchToken( text, "{" );
1064 do {
1065 token = Com_Parse( text );
1066
1067 if ( !token[0] ) {
1068 break;
1069 }
1070 if ( !strcmp( token, "}" ) ) {
1071 break;
1072 }
1073
1074 // here we may have to jump over brush epairs ( only used in editor )
1075 do {
1076 // if token is not a brace, it is a key for a key/value pair
1077 if ( !token[0] || !strcmp( token, "(" ) || !strcmp( token, "}" ) ) {
1078 break;
1079 }
1080
1081 Com_UngetToken();
1082 idStr key = Com_ParseOnLine( text );
1083 const char *token = Com_Parse( text );
1084 if ( Q_stricmp( key.c_str(), "fov" ) == 0 ) {
1085 fov = atof( token );
1086 }
1087 else if ( Q_stricmp( key.c_str(), "startFOV" ) == 0 ) {
1088 startFOV = atof( token );
1089 }
1090 else if ( Q_stricmp( key.c_str(), "endFOV" ) == 0 ) {
1091 endFOV = atof( token );
1092 }
1093 else if ( Q_stricmp( key.c_str(), "time" ) == 0 ) {
1094 time = atoi( token );
1095 }
1096 token = Com_Parse( text );
1097
1098 } while ( 1 );
1099
1100 if ( !strcmp( token, "}" ) ) {
1101 break;
1102 }
1103
1104 } while ( 1 );
1105
1106 Com_UngetToken();
1107 Com_MatchToken( text, "}" );
1108 }
1109
1110 bool idCameraPosition::parseToken( const char *key, const char *( *text ) ) {
1111 const char *token = Com_Parse( text );
1112 if ( Q_stricmp( key, "time" ) == 0 ) {
1113 time = atol( token );
1114 return true;
1115 }
1116 else if ( Q_stricmp( key, "type" ) == 0 ) {
1117 type = static_cast<idCameraPosition::positionType>( atoi( token ) );
1118 return true;
1119 }
1120 else if ( Q_stricmp( key, "velocity" ) == 0 ) {
1121 long t = atol( token );
1122 token = Com_Parse( text );
1123 long d = atol( token );
1124 token = Com_Parse( text );
1125 float s = atof( token );
1126 addVelocity( t, d, s );
1127 return true;
1128 }
1129 else if ( Q_stricmp( key, "baseVelocity" ) == 0 ) {
1130 baseVelocity = atof( token );
1131 return true;
1132 }
1133 else if ( Q_stricmp( key, "name" ) == 0 ) {
1134 name = token;
1135 return true;
1136 }
1137 else if ( Q_stricmp( key, "time" ) == 0 ) {
1138 time = atoi( token );
1139 return true;
1140 }
1141 Com_UngetToken();
1142 return false;
1143 }
1144
1145
1146
1147 void idFixedPosition::parse( const char *( *text ) ) {
1148 const char *token;
1149 Com_MatchToken( text, "{" );
1150 do {
1151 token = Com_Parse( text );
1152
1153 if ( !token[0] ) {
1154 break;
1155 }
1156 if ( !strcmp( token, "}" ) ) {
1157 break;
1158 }
1159
1160 // here we may have to jump over brush epairs ( only used in editor )
1161 do {
1162 // if token is not a brace, it is a key for a key/value pair
1163 if ( !token[0] || !strcmp( token, "(" ) || !strcmp( token, "}" ) ) {
1164 break;
1165 }
1166
1167 Com_UngetToken();
1168 idStr key = Com_ParseOnLine( text );
1169
1170 Com_Parse( text );
1171 if ( Q_stricmp( key.c_str(), "pos" ) == 0 ) {
1172 Com_UngetToken();
1173 Com_Parse1DMatrix( text, 3, pos );
1174 }
1175 else {
1176 Com_UngetToken();
1177 idCameraPosition::parseToken( key.c_str(), text );
1178 }
1179 Com_Parse( text );
1180
1181 } while ( 1 );
1182
1183 if ( !strcmp( token, "}" ) ) {
1184 break;
1185 }
1186
1187 } while ( 1 );
1188
1189 Com_UngetToken();
1190 Com_MatchToken( text, "}" );
1191 }
1192
1193 void idInterpolatedPosition::parse( const char *( *text ) ) {
1194 const char *token;
1195 Com_MatchToken( text, "{" );
1196 do {
1197 token = Com_Parse( text );
1198
1199 if ( !token[0] ) {
1200 break;
1201 }
1202 if ( !strcmp( token, "}" ) ) {
1203 break;
1204 }
1205
1206 // here we may have to jump over brush epairs ( only used in editor )
1207 do {
1208 // if token is not a brace, it is a key for a key/value pair
1209 if ( !token[0] || !strcmp( token, "(" ) || !strcmp( token, "}" ) ) {
1210 break;
1211 }
1212
1213 Com_UngetToken();
1214 idStr key = Com_ParseOnLine( text );
1215
1216 Com_Parse( text );
1217 if ( Q_stricmp( key.c_str(), "startPos" ) == 0 ) {
1218 Com_UngetToken();
1219 Com_Parse1DMatrix( text, 3, startPos );
1220 }
1221 else if ( Q_stricmp( key.c_str(), "endPos" ) == 0 ) {
1222 Com_UngetToken();
1223 Com_Parse1DMatrix( text, 3, endPos );
1224 }
1225 else {
1226 Com_UngetToken();
1227 idCameraPosition::parseToken( key.c_str(), text );
1228 }
1229 Com_Parse( text );
1230
1231 } while ( 1 );
1232
1233 if ( !strcmp( token, "}" ) ) {
1234 break;
1235 }
1236
1237 } while ( 1 );
1238
1239 Com_UngetToken();
1240 Com_MatchToken( text, "}" );
1241 }
1242
1243
1244 void idSplinePosition::parse( const char *( *text ) ) {
1245 const char *token;
1246 Com_MatchToken( text, "{" );
1247 do {
1248 token = Com_Parse( text );
1249
1250 if ( !token[0] ) {
1251 break;
1252 }
1253 if ( !strcmp( token, "}" ) ) {
1254 break;
1255 }
1256
1257 // here we may have to jump over brush epairs ( only used in editor )
1258 do {
1259 // if token is not a brace, it is a key for a key/value pair
1260 if ( !token[0] || !strcmp( token, "(" ) || !strcmp( token, "}" ) ) {
1261 break;
1262 }
1263
1264 Com_UngetToken();
1265 idStr key = Com_ParseOnLine( text );
1266
1267 Com_Parse( text );
1268 if ( Q_stricmp( key.c_str(), "target" ) == 0 ) {
1269 target.parse( text );
1270 }
1271 else {
1272 Com_UngetToken();
1273 idCameraPosition::parseToken( key.c_str(), text );
1274 }
1275 Com_Parse( text );
1276
1277 } while ( 1 );
1278
1279 if ( !strcmp( token, "}" ) ) {
1280 break;
1281 }
1282
1283 } while ( 1 );
1284
1285 Com_UngetToken();
1286 Com_MatchToken( text, "}" );
1287 }
1288
1289
1290
1291 void idCameraFOV::write( fileHandle_t file, const char *p ) {
1292 idStr s = va( "\t%s {\n", p );
1293 FS_Write( s.c_str(), s.length(), file );
1294
1295 s = va( "\t\tfov %f\n", fov );
1296 FS_Write( s.c_str(), s.length(), file );
1297
1298 s = va( "\t\tstartFOV %f\n", startFOV );
1299 FS_Write( s.c_str(), s.length(), file );
1300
1301 s = va( "\t\tendFOV %f\n", endFOV );
1302 FS_Write( s.c_str(), s.length(), file );
1303
1304 s = va( "\t\ttime %i\n", time );
1305 FS_Write( s.c_str(), s.length(), file );
1306
1307 s = "\t}\n";
1308 FS_Write( s.c_str(), s.length(), file );
1309 }
1310
1311
1312 void idCameraPosition::write( fileHandle_t file, const char *p ) {
1313
1314 idStr s = va( "\t\ttime %i\n", time );
1315 FS_Write( s.c_str(), s.length(), file );
1316
1317 s = va( "\t\ttype %i\n", static_cast<int>( type ) );
1318 FS_Write( s.c_str(), s.length(), file );
1319
1320 s = va( "\t\tname %s\n", name.c_str() );
1321 FS_Write( s.c_str(), s.length(), file );
1322
1323 s = va( "\t\tbaseVelocity %f\n", baseVelocity );
1324 FS_Write( s.c_str(), s.length(), file );
1325
1326 for ( int i = 0; i < velocities.Num(); i++ ) {
1327 s = va( "\t\tvelocity %i %i %f\n", velocities[i]->startTime, velocities[i]->time, velocities[i]->speed );
1328 FS_Write( s.c_str(), s.length(), file );
1329 }
1330
1331 }
1332
1333 void idFixedPosition::write( fileHandle_t file, const char *p ) {
1334 idStr s = va( "\t%s {\n", p );
1335 FS_Write( s.c_str(), s.length(), file );
1336 idCameraPosition::write( file, p );
1337 s = va( "\t\tpos ( %f %f %f )\n", pos.x, pos.y, pos.z );
1338 FS_Write( s.c_str(), s.length(), file );
1339 s = "\t}\n";
1340 FS_Write( s.c_str(), s.length(), file );
1341 }
1342
1343 void idInterpolatedPosition::write( fileHandle_t file, const char *p ) {
1344 idStr s = va( "\t%s {\n", p );
1345 FS_Write( s.c_str(), s.length(), file );
1346 idCameraPosition::write( file, p );
1347 s = va( "\t\tstartPos ( %f %f %f )\n", startPos.x, startPos.y, startPos.z );
1348 FS_Write( s.c_str(), s.length(), file );
1349 s = va( "\t\tendPos ( %f %f %f )\n", endPos.x, endPos.y, endPos.z );
1350 FS_Write( s.c_str(), s.length(), file );
1351 s = "\t}\n";
1352 FS_Write( s.c_str(), s.length(), file );
1353 }
1354
1355 void idSplinePosition::write( fileHandle_t file, const char *p ) {
1356 idStr s = va( "\t%s {\n", p );
1357 FS_Write( s.c_str(), s.length(), file );
1358 idCameraPosition::write( file, p );
1359 target.write( file, "target" );
1360 s = "\t}\n";
1361 FS_Write( s.c_str(), s.length(), file );
1362 }
1363
1364 void idCameraDef::addTarget( const char *name, idCameraPosition::positionType type ) {
1365 idCameraPosition *pos = newFromType( type );
1366 if ( pos ) {
1367 pos->setName( name );
1368 targetPositions.Append( pos );
1369 activeTarget = numTargets() - 1;
1370 if ( activeTarget == 0 ) {
1371 // first one
1372 addEvent( idCameraEvent::EVENT_TARGET, name, 0 );
1373 }
1374 }
1375 }
1376
1377 const idVec3 *idSplinePosition::getPosition( long t ) {
1378 static idVec3 interpolatedPos;
1379
1380 float velocity = getVelocity( t );
1381 float timePassed = t - lastTime;
1382 lastTime = t;
1383
1384 // convert to seconds
1385 timePassed /= 1000;
1386
1387 float distToTravel = timePassed * velocity;
1388
1389 distSoFar += distToTravel;
1390 double tempDistance = target.totalDistance();
1391
1392 double percent = (double)( distSoFar ) / tempDistance;
1393
1394 double targetDistance = percent * tempDistance;
1395 tempDistance = 0;
1396
1397 double lastDistance1,lastDistance2;
1398 lastDistance1 = lastDistance2 = 0;
1399 idVec3 temp;
1400 int count = target.numSegments();
1401 int i;
1402 for ( i = 1; i < count; i++ ) {
1403 temp = *target.getSegmentPoint( i - 1 );
1404 temp -= *target.getSegmentPoint( i );
1405 tempDistance += temp.Length();
1406 if ( i & 1 ) {
1407 lastDistance1 = tempDistance;
1408 }
1409 else {
1410 lastDistance2 = tempDistance;
1411 }
1412 if ( tempDistance >= targetDistance ) {
1413 break;
1414 }
1415 }
1416
1417 if ( i >= count - 1 ) {
1418 interpolatedPos = *target.getSegmentPoint( i - 1 );
1419 }
1420 else {
1421 #if 0
1422 double timeHi = target.getSegmentTime( i + 1 );
1423 double timeLo = target.getSegmentTime( i - 1 );
1424 double percent = ( timeHi - t ) / ( timeHi - timeLo );
1425 idVec3 v1 = *target.getSegmentPoint( i - 1 );
1426 idVec3 v2 = *target.getSegmentPoint( i + 1 );
1427 v2 *= ( 1.0 - percent );
1428 v1 *= percent;
1429 v2 += v1;
1430 interpolatedPos = v2;
1431 #else
1432 if ( lastDistance1 > lastDistance2 ) {
1433 double d = lastDistance2;
1434 lastDistance2 = lastDistance1;
1435 lastDistance1 = d;
1436 }
1437
1438 idVec3 v1 = *target.getSegmentPoint( i - 1 );
1439 idVec3 v2 = *target.getSegmentPoint( i );
1440 double percent = ( lastDistance2 - targetDistance ) / ( lastDistance2 - lastDistance1 );
1441 v2 *= ( 1.0 - percent );
1442 v1 *= percent;
1443 v2 += v1;
1444 interpolatedPos = v2;
1445 #endif
1446 }
1447 return &interpolatedPos;
1448
1449 }