2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
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 );
35 float Q_fabs( float f ) {
36 int tmp = *( int * ) &f;
38 return *( float * ) &tmp;
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
46 const int MAX_CAMERAS = 64;
48 idCameraDef camera[MAX_CAMERAS];
51 qboolean loadCamera( int camNum, const char *name ) {
52 if ( camNum < 0 || camNum >= MAX_CAMERAS ) {
55 camera[camNum].clear();
56 return (qboolean)camera[camNum].load( name );
59 qboolean getCameraInfo( int camNum, int time, float *origin, float *angles, float *fov ) {
61 if ( camNum < 0 || camNum >= MAX_CAMERAS ) {
67 if ( camera[camNum].getCameraInfo( time, org, dir, fov ) ) {
71 angles[1] = atan2( dir[1], dir[0] ) * 180 / 3.14159;
72 angles[0] = asin( dir[2] ) * 180 / 3.14159;
78 void startCamera( int camNum, int time ) {
79 if ( camNum < 0 || camNum >= MAX_CAMERAS ) {
82 camera[camNum].startCamera( time );
88 //#include "../shared/windings.h"
89 //#include "../qcommon/qcommon.h"
90 //#include "../sys/sys_public.h"
91 //#include "../game/game_entity.h"
93 idCameraDef splineList;
94 idCameraDef *g_splineList = &splineList;
96 idVec3 idSplineList::zero( 0,0,0 );
98 void glLabeledPoint( idVec3 &color, idVec3 &point, float size, const char *label ) {
101 glBegin( GL_POINTS );
102 glVertex3fv( point );
109 glCallLists( strlen( label ), GL_UNSIGNED_BYTE, label );
113 void glBox( idVec3 &color, idVec3 &point, float size ) {
114 idVec3 mins( point );
115 idVec3 maxs( point );
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] );
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] );
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] );
150 //g_splineList->load("p:/doom/base/maps/test_base1.camera");
154 //g_splineList->addToRenderer();
158 //extern void D_DebugLine( const idVec3 &color, const idVec3 &start, const idVec3 &end );
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);
166 void idSplineList::addToRenderer() {
168 if ( controlPoints.Num() == 0 ) {
173 idVec3 yellow( 1.0, 1.0, 0 );
174 idVec3 white( 1.0, 1.0, 1.0 );
177 for ( i = 0; i < controlPoints.Num(); i++ ) {
178 VectorCopy( *controlPoints[i], mins );
179 VectorCopy( mins, maxs );
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] );
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] );
200 for ( i = 3; i < controlPoints.Num(); i++ ) {
201 for ( float tension = 0.0f; tension < 1.001f; tension += 0.1f ) {
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 );
217 debugLine( white, step1[0], step1[1], step1[2], x, y, z );
225 void idSplineList::buildSpline() {
226 //int start = Sys_Milliseconds();
228 for ( int i = 3; i < controlPoints.Num(); i++ ) {
229 for ( float tension = 0.0f; tension < 1.001f; tension += granularity ) {
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 );
238 splinePoints.Append( new idVec3( x, y, z ) );
242 //Com_Printf("Spline build took %f seconds\n", (float)(Sys_Milliseconds() - start) / 1000);
246 void idSplineList::draw( bool editMode ) {
248 idVec4 yellow( 1, 1, 0, 1 );
250 if ( controlPoints.Num() == 0 ) {
259 glColor3fv( controlColor );
262 glBegin( GL_POINTS );
263 for ( i = 0; i < controlPoints.Num(); i++ ) {
264 glVertex3fv( *controlPoints[i] );
269 for ( i = 0; i < controlPoints.Num(); i++ ) {
270 glBox( activeColor, *controlPoints[i], 4 );
275 glColor3fv( pathColor );
276 glBegin( GL_LINE_STRIP );
277 int count = splinePoints.Num();
278 for ( i = 0; i < count; i++ ) {
279 glVertex3fv( *splinePoints[i] );
284 glColor3fv( segmentColor );
286 glBegin( GL_POINTS );
287 for ( i = 0; i < count; i++ ) {
288 glVertex3fv( *splinePoints[i] );
293 //assert(activeSegment >=0 && activeSegment < count);
294 if ( activeSegment >= 0 && activeSegment < count ) {
295 glBox( activeColor, *splinePoints[activeSegment], 6 );
296 glBox( yellow, *splinePoints[activeSegment], 8 );
302 float idSplineList::totalDistance() {
304 // FIXME: save dist and return
306 if ( controlPoints.Num() == 0 ) {
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();
325 void idSplineList::initPosition( long bt, long totalTime ) {
331 if ( splinePoints.Num() == 0 ) {
338 // calc distance to travel ( this will soon be broken into time segments )
340 splineTime.Append( bt );
341 double dist = totalDistance();
342 double distSoFar = 0.0;
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 );
354 assert( splineTime.Num() == splinePoints.Num() );
360 float idSplineList::calcSpline( int step, float tension ) {
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;
372 void idSplineList::updateSelection( const idVec3 &move ) {
375 VectorAdd( *selected, move, *selected );
380 void idSplineList::setSelectedPoint( idVec3 *p ) {
383 for ( int i = 0; i < controlPoints.Num(); i++ ) {
384 if ( *p == *controlPoints[i] ) {
385 selected = controlPoints[i];
394 const idVec3 *idSplineList::getPosition( long t ) {
395 static idVec3 interpolatedPos;
397 int count = splineTime.Num();
402 // Com_Printf("Time: %d\n", t);
403 assert( splineTime.Num() == splinePoints.Num() );
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 );
417 interpolatedPos = v2;
418 return &interpolatedPos;
420 return splinePoints[activeSegment];
426 return splinePoints[count - 1];
429 void idSplineList::parse( const char *( *text ) ) {
431 //Com_MatchToken( text, "{" );
433 token = Com_Parse( text );
438 if ( !Q_stricmp( token, "}" ) ) {
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, "}" ) ) {
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 );
454 else if ( Q_stricmp( key.c_str(), "name" ) == 0 ) {
457 token = Com_Parse( text );
461 if ( !Q_stricmp( token, "}" ) ) {
466 // read the control point
468 Com_Parse1DMatrix( text, 3, point );
469 addPoint( point.x, point.y, point.z );
473 //Com_MatchToken( text, "}" );
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 );
490 FS_Write( s.c_str(), s.length(), file );
494 void idCameraDef::getActiveSegmentInfo( int segment, idVec3 &origin, idVec3 &direction, float *fov ) {
496 if ( !cameraSpline.validTime() ) {
499 double d = (double)segment / numSegments();
500 getCameraInfo( d * totalTime * 1000, origin, direction, fov );
503 if (!cameraSpline.validTime()) {
506 origin = *cameraSpline.getSegmentPoint(segment);
511 int numTargets = getTargetSpline()->controlPoints.Num();
512 int count = cameraSpline.splineTime.Num();
513 if (numTargets == 0) {
515 if (cameraSpline.getActiveSegment() < count - 1) {
516 temp = *cameraSpline.splinePoints[cameraSpline.getActiveSegment()+1];
518 } else if (numTargets == 1) {
519 temp = *getTargetSpline()->controlPoints[0];
521 temp = *getTargetSpline()->getSegmentPoint(segment);
530 bool idCameraDef::getCameraInfo( long time, idVec3 &origin, idVec3 &direction, float *fv ) {
534 if ( ( time - startTime ) / 1000 > totalTime ) {
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());
547 else if ( events[i]->getType() == idCameraEvent::EVENT_TRIGGER ) {
548 //idEntity *ent = NULL;
549 //ent = level.FindTarget( ent, events[i]->getParam());
551 // ent->signal( SIG_TRIGGER );
552 // ent->ProcessEvent( &EV_Activate, world );
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());
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 ) );
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 ) );
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" );
582 loadCamera( atoi( param1 ), va( "cameras/%s.camera", param2 ) );
586 loadCamera( 0, va( "cameras/%s.camera", events[i]->getParam() ) );
591 else if ( events[i]->getType() == idCameraEvent::EVENT_STOP ) {
597 origin = *cameraPosition->getPosition( time );
599 *fv = fov.getFOV( time );
601 idVec3 temp = origin;
603 int numTargets = targetPositions.Num();
604 if ( numTargets == 0 ) {
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++];
619 if ( getActiveTarget()->numPoints() > 0 ) {
620 temp = *getActiveTarget()->getPosition( time );
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) {
641 const int NUM_CCELERATION_SEGS = 10;
642 const int CELL_AMT = 5;
644 void idCameraDef::buildCamera() {
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() ) {
657 case idCameraEvent::EVENT_TARGET: {
661 case idCameraEvent::EVENT_FEATHER: {
665 float stepGoal = cameraPosition->getBaseVelocity() / ( 1000 / loopTime );
666 while ( startTime <= 1000 ) {
667 cameraPosition->addVelocity( startTime, loopTime, speed );
669 if ( speed > cameraPosition->getBaseVelocity() ) {
670 speed = cameraPosition->getBaseVelocity();
672 startTime += loopTime;
675 startTime = (long)( totalTime * 1000 - 1000 );
676 long endTime = startTime + 1000;
677 speed = cameraPosition->getBaseVelocity();
678 while ( startTime < endTime ) {
683 cameraPosition->addVelocity( startTime, loopTime, speed );
684 startTime += loopTime;
689 case idCameraEvent::EVENT_WAIT: {
690 waits.Append( atof( events[i]->getParam() ) );
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 ) {
698 float speed = cameraPosition->getBaseVelocity();
700 float steps = speed / ( ( events[i]->getTime() - startTime ) / loopTime );
701 while ( startTime <= events[i]->getTime() - loopTime ) {
702 cameraPosition->addVelocity( startTime, loopTime, speed );
704 startTime += loopTime;
706 cameraPosition->addVelocity( events[i]->getTime(), (long)atof( events[i]->getParam() ) * 1000, 0 );
708 startTime = (long)( events[i]->getTime() + atof( events[i]->getParam() ) * 1000 );
709 long endTime = startTime + 1000;
711 while ( startTime <= endTime ) {
712 cameraPosition->addVelocity( startTime, loopTime, speed );
714 startTime += loopTime;
718 case idCameraEvent::EVENT_TARGETWAIT: {
719 //targetWaits.Append(i);
722 case idCameraEvent::EVENT_SPEED: {
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();
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);
736 // multiply that by the adjustment
737 double newTotal = total * adjust;
738 // what is the difference..
740 totalTime += newTotal / 1000;
742 // per segment difference
744 int additive = newTotal;
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;
758 for ( i = 0; i < waits.Num(); i++ ) {
759 totalTime += waits[i];
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
765 long total = (long)( totalTime * 1000 );
766 for ( i = 0; i < targets.Num(); i++ ) {
768 if ( i < targets.Num() - 1 ) {
769 t = events[targets[i + 1]]->getTime();
772 t = total - timeSoFar;
774 // t is how much time to use for this target
775 setActiveTargetByName( events[targets[i]]->getParam() );
776 getActiveTarget()->setTime( t );
783 void idCameraDef::startCamera( long t ) {
784 cameraPosition->clearVelocities();
785 cameraPosition->start( t );
787 fov.reset( 90, 90, t, 0 );
788 //for (int i = 0; i < targetPositions.Num(); i++) {
789 // targetPositions[i]->
792 cameraRunning = true;
796 void idCameraDef::parse( const char *( *text ) ) {
800 token = Com_Parse( text );
805 if ( !Q_stricmp( token, "}" ) ) {
809 if ( Q_stricmp( token, "time" ) == 0 ) {
810 baseTime = Com_ParseFloat( text );
812 else if ( Q_stricmp( token, "camera_fixed" ) == 0 ) {
813 cameraPosition = new idFixedPosition();
814 cameraPosition->parse( text );
816 else if ( Q_stricmp( token, "camera_interpolated" ) == 0 ) {
817 cameraPosition = new idInterpolatedPosition();
818 cameraPosition->parse( text );
820 else if ( Q_stricmp( token, "camera_spline" ) == 0 ) {
821 cameraPosition = new idSplinePosition();
822 cameraPosition->parse( text );
824 else if ( Q_stricmp( token, "target_fixed" ) == 0 ) {
825 idFixedPosition *pos = new idFixedPosition();
827 targetPositions.Append( pos );
829 else if ( Q_stricmp( token, "target_interpolated" ) == 0 ) {
830 idInterpolatedPosition *pos = new idInterpolatedPosition();
832 targetPositions.Append( pos );
834 else if ( Q_stricmp( token, "target_spline" ) == 0 ) {
835 idSplinePosition *pos = new idSplinePosition();
837 targetPositions.Append( pos );
839 else if ( Q_stricmp( token, "fov" ) == 0 ) {
842 else if ( Q_stricmp( token, "event" ) == 0 ) {
843 idCameraEvent *event = new idCameraEvent();
844 event->parse( text );
851 if ( !cameraPosition ) {
852 Com_Printf( "no camera position specified\n" );
853 // prevent a crash later on
854 cameraPosition = new idFixedPosition();
858 Com_MatchToken( text, "}" );
862 bool idCameraDef::load( const char *filename ) {
866 FS_ReadFile( filename, (void **)&buf );
872 Com_BeginParseSession( filename );
875 Com_EndParseSession();
881 void idCameraDef::save( const char *filename ) {
882 fileHandle_t file = FS_FOpenFileWrite( filename );
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 );
890 cameraPosition->write( file, va( "camera_%s",cameraPosition->typeStr() ) );
892 for ( i = 0; i < numTargets(); i++ ) {
893 targetPositions[i]->write( file, va( "target_%s", targetPositions[i]->typeStr() ) );
896 for ( i = 0; i < events.Num(); i++ ) {
897 events[i]->write( file, "event" );
900 fov.write( file, "fov" );
903 FS_Write( s.c_str(), s.length(), file );
905 FS_FCloseFile( file );
908 int idCameraDef::sortEvents( const void *p1, const void *p2 ) {
909 idCameraEvent *ev1 = (idCameraEvent*)( p1 );
910 idCameraEvent *ev2 = (idCameraEvent*)( p2 );
912 if ( ev1->getTime() > ev2->getTime() ) {
915 if ( ev1->getTime() < ev2->getTime() ) {
921 void idCameraDef::addEvent( idCameraEvent *event ) {
922 events.Append( event );
923 //events.Sort(&sortEvents);
926 void idCameraDef::addEvent( idCameraEvent::eventType t, const char *param, long time ) {
927 addEvent( new idCameraEvent( t, param, time ) );
931 void idCameraDef::removeEvent( int index ) {
932 events.RemoveIndex( index );
937 const char *idCameraEvent::eventStr[] = {
954 void idCameraEvent::parse( const char *( *text ) ) {
956 Com_MatchToken( text, "{" );
958 token = Com_Parse( text );
963 if ( !strcmp( token, "}" ) ) {
967 // here we may have to jump over brush epairs ( only used in editor )
969 // if token is not a brace, it is a key for a key/value pair
970 if ( !token[0] || !strcmp( token, "(" ) || !strcmp( token, "}" ) ) {
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 ) );
980 else if ( Q_stricmp( key.c_str(), "param" ) == 0 ) {
983 else if ( Q_stricmp( key.c_str(), "time" ) == 0 ) {
984 time = atoi( token );
986 token = Com_Parse( text );
990 if ( !strcmp( token, "}" ) ) {
997 Com_MatchToken( text, "}" );
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 );
1010 FS_Write( s.c_str(), s.length(), file );
1014 const char *idCameraPosition::positionStr[] = {
1022 const idVec3 *idInterpolatedPosition::getPosition( long t ) {
1023 static idVec3 interpolatedPos;
1025 float velocity = getVelocity( t );
1026 float timePassed = t - lastTime;
1029 // convert to seconds
1032 float distToTravel = timePassed * velocity;
1034 idVec3 temp = startPos;
1036 float distance = temp.Length();
1038 distSoFar += distToTravel;
1039 float percent = (float)( distSoFar ) / distance;
1041 if ( percent > 1.0 ) {
1044 else if ( percent < 0.0 ) {
1048 // the following line does a straigt calc on percentage of time
1049 // float percent = (float)(startTime + time - t) / time;
1051 idVec3 v1 = startPos;
1053 v1 *= ( 1.0 - percent );
1056 interpolatedPos = v1;
1057 return &interpolatedPos;
1061 void idCameraFOV::parse( const char *( *text ) ) {
1063 Com_MatchToken( text, "{" );
1065 token = Com_Parse( text );
1070 if ( !strcmp( token, "}" ) ) {
1074 // here we may have to jump over brush epairs ( only used in editor )
1076 // if token is not a brace, it is a key for a key/value pair
1077 if ( !token[0] || !strcmp( token, "(" ) || !strcmp( token, "}" ) ) {
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 );
1087 else if ( Q_stricmp( key.c_str(), "startFOV" ) == 0 ) {
1088 startFOV = atof( token );
1090 else if ( Q_stricmp( key.c_str(), "endFOV" ) == 0 ) {
1091 endFOV = atof( token );
1093 else if ( Q_stricmp( key.c_str(), "time" ) == 0 ) {
1094 time = atoi( token );
1096 token = Com_Parse( text );
1100 if ( !strcmp( token, "}" ) ) {
1107 Com_MatchToken( text, "}" );
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 );
1116 else if ( Q_stricmp( key, "type" ) == 0 ) {
1117 type = static_cast<idCameraPosition::positionType>( atoi( token ) );
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 );
1129 else if ( Q_stricmp( key, "baseVelocity" ) == 0 ) {
1130 baseVelocity = atof( token );
1133 else if ( Q_stricmp( key, "name" ) == 0 ) {
1137 else if ( Q_stricmp( key, "time" ) == 0 ) {
1138 time = atoi( token );
1147 void idFixedPosition::parse( const char *( *text ) ) {
1149 Com_MatchToken( text, "{" );
1151 token = Com_Parse( text );
1156 if ( !strcmp( token, "}" ) ) {
1160 // here we may have to jump over brush epairs ( only used in editor )
1162 // if token is not a brace, it is a key for a key/value pair
1163 if ( !token[0] || !strcmp( token, "(" ) || !strcmp( token, "}" ) ) {
1168 idStr key = Com_ParseOnLine( text );
1171 if ( Q_stricmp( key.c_str(), "pos" ) == 0 ) {
1173 Com_Parse1DMatrix( text, 3, pos );
1177 idCameraPosition::parseToken( key.c_str(), text );
1183 if ( !strcmp( token, "}" ) ) {
1190 Com_MatchToken( text, "}" );
1193 void idInterpolatedPosition::parse( const char *( *text ) ) {
1195 Com_MatchToken( text, "{" );
1197 token = Com_Parse( text );
1202 if ( !strcmp( token, "}" ) ) {
1206 // here we may have to jump over brush epairs ( only used in editor )
1208 // if token is not a brace, it is a key for a key/value pair
1209 if ( !token[0] || !strcmp( token, "(" ) || !strcmp( token, "}" ) ) {
1214 idStr key = Com_ParseOnLine( text );
1217 if ( Q_stricmp( key.c_str(), "startPos" ) == 0 ) {
1219 Com_Parse1DMatrix( text, 3, startPos );
1221 else if ( Q_stricmp( key.c_str(), "endPos" ) == 0 ) {
1223 Com_Parse1DMatrix( text, 3, endPos );
1227 idCameraPosition::parseToken( key.c_str(), text );
1233 if ( !strcmp( token, "}" ) ) {
1240 Com_MatchToken( text, "}" );
1244 void idSplinePosition::parse( const char *( *text ) ) {
1246 Com_MatchToken( text, "{" );
1248 token = Com_Parse( text );
1253 if ( !strcmp( token, "}" ) ) {
1257 // here we may have to jump over brush epairs ( only used in editor )
1259 // if token is not a brace, it is a key for a key/value pair
1260 if ( !token[0] || !strcmp( token, "(" ) || !strcmp( token, "}" ) ) {
1265 idStr key = Com_ParseOnLine( text );
1268 if ( Q_stricmp( key.c_str(), "target" ) == 0 ) {
1269 target.parse( text );
1273 idCameraPosition::parseToken( key.c_str(), text );
1279 if ( !strcmp( token, "}" ) ) {
1286 Com_MatchToken( text, "}" );
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 );
1295 s = va( "\t\tfov %f\n", fov );
1296 FS_Write( s.c_str(), s.length(), file );
1298 s = va( "\t\tstartFOV %f\n", startFOV );
1299 FS_Write( s.c_str(), s.length(), file );
1301 s = va( "\t\tendFOV %f\n", endFOV );
1302 FS_Write( s.c_str(), s.length(), file );
1304 s = va( "\t\ttime %i\n", time );
1305 FS_Write( s.c_str(), s.length(), file );
1308 FS_Write( s.c_str(), s.length(), file );
1312 void idCameraPosition::write( fileHandle_t file, const char *p ) {
1314 idStr s = va( "\t\ttime %i\n", time );
1315 FS_Write( s.c_str(), s.length(), file );
1317 s = va( "\t\ttype %i\n", static_cast<int>( type ) );
1318 FS_Write( s.c_str(), s.length(), file );
1320 s = va( "\t\tname %s\n", name.c_str() );
1321 FS_Write( s.c_str(), s.length(), file );
1323 s = va( "\t\tbaseVelocity %f\n", baseVelocity );
1324 FS_Write( s.c_str(), s.length(), file );
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 );
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 );
1340 FS_Write( s.c_str(), s.length(), file );
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 );
1352 FS_Write( s.c_str(), s.length(), file );
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" );
1361 FS_Write( s.c_str(), s.length(), file );
1364 void idCameraDef::addTarget( const char *name, idCameraPosition::positionType type ) {
1365 idCameraPosition *pos = newFromType( type );
1367 pos->setName( name );
1368 targetPositions.Append( pos );
1369 activeTarget = numTargets() - 1;
1370 if ( activeTarget == 0 ) {
1372 addEvent( idCameraEvent::EVENT_TARGET, name, 0 );
1377 const idVec3 *idSplinePosition::getPosition( long t ) {
1378 static idVec3 interpolatedPos;
1380 float velocity = getVelocity( t );
1381 float timePassed = t - lastTime;
1384 // convert to seconds
1387 float distToTravel = timePassed * velocity;
1389 distSoFar += distToTravel;
1390 double tempDistance = target.totalDistance();
1392 double percent = (double)( distSoFar ) / tempDistance;
1394 double targetDistance = percent * tempDistance;
1397 double lastDistance1,lastDistance2;
1398 lastDistance1 = lastDistance2 = 0;
1400 int count = target.numSegments();
1402 for ( i = 1; i < count; i++ ) {
1403 temp = *target.getSegmentPoint( i - 1 );
1404 temp -= *target.getSegmentPoint( i );
1405 tempDistance += temp.Length();
1407 lastDistance1 = tempDistance;
1410 lastDistance2 = tempDistance;
1412 if ( tempDistance >= targetDistance ) {
1417 if ( i >= count - 1 ) {
1418 interpolatedPos = *target.getSegmentPoint( i - 1 );
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 );
1430 interpolatedPos = v2;
1432 if ( lastDistance1 > lastDistance2 ) {
1433 double d = lastDistance2;
1434 lastDistance2 = lastDistance1;
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 );
1444 interpolatedPos = v2;
1447 return &interpolatedPos;