⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 atifly.cpp

📁 游戏编程精粹2第三章源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/* Copyright (C) Alex Vlachos and John Isidoro, 2001. 
 * All rights reserved worldwide.
 *
 * This software is provided "as is" without express or implied
 * warranties. You may freely copy and compile this source into
 * applications you distribute provided that the copyright text
 * below is included in the resulting source code, for example:
 * "Portions Copyright (C) Alex Vlachos and John Isidoro, 2001"
 */
/******************************************************************************
 *  AtiFly.cpp -- Automated fly through code
 ******************************************************************************
 *  Written by Alex Vlachos (Alex@Vlachos.com)
 ******************************************************************************/

// INCLUDES ===================================================================
#include "AtiFly.h"

// DEFINES ====================================================================
#define ATI_FLY_CUBE_RADIUS 2.5f
#define ATI_FLY_SPLINE_TENSION (-0.5f)
#define ATI_FLY_S ((1.0f-ATI_FLY_SPLINE_TENSION)*0.5f)
#define ATI_STRING_LENGTH 64

// MACROS =====================================================================
#define AtiFreeArray(array) \
   if (array != NULL)       \
      delete [] array;      \
   array = NULL;

#define AtiMallocArray(array,num,type)                                   \
   array = NULL;                                                         \
   if (num > 0)                                                          \
   {                                                                     \
      array = new type [num];                                            \
      if (array == NULL)                                                 \
      {                                                                  \
         return FALSE;                                                   \
      }                                                                  \
      memset (array, 0, sizeof(type) * num);                             \
   }

// GLOBALS ====================================================================
AtiMatrix sgCardinalMatrix = { -ATI_FLY_S,     2.0f*ATI_FLY_S,      -ATI_FLY_S, 0.0f,
                               2.0f-ATI_FLY_S, ATI_FLY_S-3.0f,      0.0f,       1.0f,
                               ATI_FLY_S-2.0f, 3.0f-2.0f*ATI_FLY_S, ATI_FLY_S,  0.0f,
                               ATI_FLY_S,      -ATI_FLY_S,          0.0f,       0.0f };

static char8 sgFileBase[ATI_STRING_LENGTH] = "";

//=============================================================================
AtiFlyPath::AtiFlyPath()
{
   mFileName[0] = '\0';

   mInFlight = FALSE;
   mStartTime = 0.0;
   mPausedTime = 0.0;
   mPaused = FALSE;

   mNumFlyPoints = 0;
   mCurrentEditPoint = -1;
   mCurrentEditPointTime = 0.0;
   mSampleInterval = 1.0;
   mPathTime = 0.0;
   mNumSplineIntervals = 0;

   mLocation = NULL;
   mLocationIntervals = NULL;

   mOrientation = NULL;
   mOrientationIntervals = NULL;

   mPreviousPath = this;
   mNextPath = this;
}

//=============================================================================
AtiFlyPath::~AtiFlyPath()
{
   InitPath();
}

//=============================================================================
void AtiFlyPath::SetBasePath (char8 *aPath)
{
   strcpy(sgFileBase, aPath);
}

//=============================================================================
void AtiFlyPath::InitPath (void)
{
   mInFlight = FALSE;
   mStartTime = 0.0;
   mPausedTime = 0.0;
   mPaused = FALSE;

   mNumFlyPoints = 0;
   mCurrentEditPoint = -1;
   mCurrentEditPointTime = 0.0;
   mSampleInterval = 1.0;
   mPathTime = 0.0;
   mNumSplineIntervals = 0;

   AtiFreeArray(mLocation);
   AtiFreeArray(mLocationIntervals);

   AtiFreeArray(mOrientation);
   AtiFreeArray(mOrientationIntervals);
}

//=============================================================================
bool8 AtiFlyPath::ReadFile (char8 *aFileName)
{
   int32 i;
   FILE *fp;
   float32 tmpf;
   char8 buff[ATI_STRING_LENGTH*2];

   /*************************/
   /* Init Member Variables */
   /*************************/
   InitPath();

   /*****************************/
   /* Copy File Name To Current */
   /*****************************/
   strncpy(mFileName, aFileName, 31);
   mFileName[31] = '\0';

   /*************/
   /* Open File */
   /*************/
   sprintf(buff, "%s%s", sgFileBase, aFileName);
   fp = fopen(buff, "r");
   if (fp == NULL)
      return FALSE;

   /***********************/
   /* Get Sample Interval */
   /***********************/
   fscanf(fp, "%f", &tmpf);
   mSampleInterval = tmpf;

   /************************/
   /* Get number of points */
   /************************/
   fscanf(fp, "%d", &mNumFlyPoints);
   if (mNumFlyPoints <= 0)
   {
      mNumFlyPoints = 0;
      mCurrentEditPoint = -1;
      return TRUE;
   }
   mCurrentEditPoint = 0;

   /*******************/
   /* Allocate Memory */
   /*******************/
   AtiMallocArray(mLocation, mNumFlyPoints+1, AtiVertex4);
   AtiMallocArray(mOrientation, mNumFlyPoints+1, AtiQuaternion);

   /************/
   /* Get data */
   /************/
   for (i=0; i<mNumFlyPoints; i++)
   {
      fscanf(fp, "%f %f %f %f", &(mLocation[i].x), &(mLocation[i].y), &(mLocation[i].z), &(mLocation[i].w));
      fscanf(fp, "%f %f %f %f", &(mOrientation[i].x), &(mOrientation[i].y), &(mOrientation[i].z), &(mOrientation[i].w));

      //Read in the hex versions of the floats
      //fscanf(fp, "%x %x %x %x", &(mLocation[i].x), &(mLocation[i].y), &(mLocation[i].z), &(mLocation[i].w));
      //fscanf(fp, "%x %x %x %x", &(mOrientation[i].x), &(mOrientation[i].y), &(mOrientation[i].z), &(mOrientation[i].w));
   }

   /*********************************************/
   /* Don't allow first point to be a cut point */
   /*********************************************/
   mLocation[0].w = 0.0f;

   /**************/
   /* Close File */
   /**************/
   fclose(fp);

   /********************/
   /* Compile Fly Path */
   /********************/
   if (CompilePositionsAndOrientations() == FALSE)
      return FALSE;

   /***************************/
   /* Finished With No Errors */
   /***************************/
   return TRUE;
}

//=============================================================================
bool8 AtiFlyPath::ReadCurrentFile (void)
{
   return ReadFile(mFileName);
}

//=============================================================================
bool8 AtiFlyPath::WriteFile (char8 *aFileName)
{
   int32 i;
   FILE *fp;
   char8 buff[ATI_STRING_LENGTH*2];

   /***********************************/
   /* Make sure we have enough points */
   /***********************************/
   if (mNumFlyPoints < 5)
      return FALSE;

   /*************/
   /* Open File */
   /*************/
   sprintf(buff, "%s%s", sgFileBase, aFileName);
   fp = fopen(buff, "w");
   if (fp == NULL)
      return FALSE;

   /*************************/
   /* Write Sample Interval */
   /*************************/
   fprintf(fp, "%f\n", mSampleInterval);

   /**************************/
   /* Write number of points */
   /**************************/
   fprintf(fp, "%d\n", mNumFlyPoints);

   /************************************/
   /* Write positions and orientations */
   /************************************/
   for (i=0; i<mNumFlyPoints; i++)
   {
      fprintf(fp, "%f %f %f %f\n", mLocation[i].x, mLocation[i].y, mLocation[i].z, mLocation[i].w);
      fprintf(fp, "%f %f %f %f\n", mOrientation[i].x, mOrientation[i].y, mOrientation[i].z, mOrientation[i].w);

      //Write out the hex versions of the floats
      //fprintf(fp, "%x %x %x %x\n", *(int*)&(mLocation[i].x), *(int*)&(mLocation[i].y), *(int*)&(mLocation[i].z), *(int*)&(mLocation[i].w));
      //fprintf(fp, "%x %x %x %x\n", *(int*)&(mOrientation[i].x), *(int*)&(mOrientation[i].y), *(int*)&(mOrientation[i].z), *(int*)&(mOrientation[i].w));
   }

   /**************/
   /* Close File */
   /**************/
   fclose(fp);

   /***************************/
   /* Finished With No Errors */
   /***************************/
   return TRUE;
}

//=============================================================================
bool8 AtiFlyPath::WriteCurrentFile (void)
{
   return WriteFile(mFileName);
}

//=============================================================================
bool8 AtiFlyPath::StartFlight (float64 aTime)
{
   /***********************************/
   /* Make sure we have enough points */
   /***********************************/
   if (mNumFlyPoints < 5)
      return FALSE;

   /*****************/
   /* Set variables */
   /*****************/
   mInFlight = TRUE;
   mPaused = FALSE;
   mStartTime = aTime;

   /***********************************************/
   /* Engine specific: This will start you at the */
   /* selected position when in editing mode      */
   /***********************************************/
   //if (gEditingPath == TRUE)
   //   mStartTime -= mCurrentEditPointTime;

   return TRUE;
}

//=============================================================================
void AtiFlyPath::PauseFlight (float64 aTime)
{
   mInFlight = FALSE;
   mPaused = TRUE;
   mPausedTime = aTime;
}

//=============================================================================
bool8 AtiFlyPath::ResumeFlight (float64 aTime)
{
   if ((mPaused == FALSE) && (mInFlight == FALSE))
   {
      return StartFlight(aTime);
   }

   if (mPaused == FALSE)
      return FALSE;

   mStartTime += aTime - mPausedTime;
   mPaused = FALSE;
   mInFlight = TRUE;

   return TRUE;
}

//=============================================================================
void AtiFlyPath::EndFlight (void)
{
   mInFlight = FALSE;
   mPaused = FALSE;
}

//=============================================================================
float64 AtiFlyPath::GetTotalPathTime (void)
{
   return (mPathTime);
}

//=============================================================================
float64 AtiFlyPath::GetTimeRelativeToStartFlight (float64 aTime)
{
   if (mPaused == TRUE)
      return (mPausedTime - mStartTime);
   else
      return (aTime - mStartTime);
}

//=============================================================================
bool8 AtiFlyPath::AddFlyPointAfterCurrent (AtiVertex3 aPosition, AtiQuaternion aOrientation)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -