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

📄 atifly.cpp

📁 游戏编程精粹2第三章源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
{
   AtiVertex4 *tmpLocation = NULL;
   AtiQuaternion *tmpOrientation = NULL;

   /******************************/
   /* Make sure we're not flying */
   /******************************/
   if (mInFlight == TRUE)
      return FALSE;

   /*********************************/
   /* Allocate memory for new point */
   /*********************************/
   tmpLocation = mLocation;
   AtiMallocArray(mLocation, mNumFlyPoints+1, AtiVertex4);

   tmpOrientation = mOrientation;
   AtiMallocArray(mOrientation, mNumFlyPoints+1, AtiQuaternion);

   /*********************************************/
   /* Coyp all points before current edit point */
   /*********************************************/
   if ((mNumFlyPoints > 0) && (mCurrentEditPoint >= 0))
   {
      memcpy(mLocation, tmpLocation, sizeof(AtiVertex4)*(mCurrentEditPoint+1));
      memcpy(mOrientation, tmpOrientation, sizeof(AtiQuaternion)*(mCurrentEditPoint+1));
   }

   /********************************************/
   /* Copy all points after current edit point */
   /********************************************/
   if ((mCurrentEditPoint+1) < mNumFlyPoints) //If last point is not current
   {
      memcpy(&(mLocation[mCurrentEditPoint+2]), &(tmpLocation[mCurrentEditPoint+1]), sizeof(AtiVertex4)*(mNumFlyPoints-mCurrentEditPoint-1));
      memcpy(&(mOrientation[mCurrentEditPoint+2]), &(tmpOrientation[mCurrentEditPoint+1]), sizeof(AtiQuaternion)*(mNumFlyPoints-mCurrentEditPoint-1));
   }

   /*********************/
   /* Now add new point */
   /*********************/
   mLocation[mCurrentEditPoint+1].x = aPosition.x;
   mLocation[mCurrentEditPoint+1].y = aPosition.y;
   mLocation[mCurrentEditPoint+1].z = aPosition.z;
   mLocation[mCurrentEditPoint+1].w = 0.0f;

   mOrientation[mCurrentEditPoint+1].x = aOrientation.x;
   mOrientation[mCurrentEditPoint+1].y = aOrientation.y;
   mOrientation[mCurrentEditPoint+1].z = aOrientation.z;
   mOrientation[mCurrentEditPoint+1].w = aOrientation.w;

   /*******************/
   /* Update counters */
   /*******************/
   mNumFlyPoints++;
   mCurrentEditPoint++;

   /*******************/
   /* Free tmp memory */
   /*******************/
   AtiFreeArray(tmpLocation);
   AtiFreeArray(tmpOrientation);

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

   return TRUE;
}

//=============================================================================
void AtiFlyPath::ChangeCurrentFlyPoint (AtiVertex3 aPosition, AtiQuaternion aOrientation)
{
   /******************************/
   /* Make sure we're not flying */
   /******************************/
   if (mInFlight == TRUE)
      return;

   /*************************************/
   /* Make sure we have a point to move */
   /*************************************/
   if (mCurrentEditPoint < 0)
      return;

   /**********/
   /* Update */
   /**********/
   mLocation[mCurrentEditPoint].x = aPosition.x;
   mLocation[mCurrentEditPoint].y = aPosition.y;
   mLocation[mCurrentEditPoint].z = aPosition.z;

   mOrientation[mCurrentEditPoint].x = aOrientation.x;
   mOrientation[mCurrentEditPoint].y = aOrientation.y;
   mOrientation[mCurrentEditPoint].z = aOrientation.z;
   mOrientation[mCurrentEditPoint].w = aOrientation.w;

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

//=============================================================================
void AtiFlyPath::DeleteCurrentFlyPoint (bool8 aSimulateBackspaceKey)
{
   /******************************/
   /* Make sure we're not flying */
   /******************************/
   if (mInFlight == TRUE)
      return;

   /***************************************/
   /* Make sure we have a point to delete */
   /***************************************/
   if ((mCurrentEditPoint < 0) || (mNumFlyPoints == 0))
      return;

   /*****************************************************/
   /* Copy all points after current edit point back one */
   /*****************************************************/
   if ((mCurrentEditPoint+1) < mNumFlyPoints) //If last point is not current
   {
      memcpy(&(mLocation[mCurrentEditPoint]), &(mLocation[mCurrentEditPoint+1]), sizeof(AtiVertex4)*(mNumFlyPoints-mCurrentEditPoint-1));
      memcpy(&(mOrientation[mCurrentEditPoint]), &(mOrientation[mCurrentEditPoint+1]), sizeof(AtiQuaternion)*(mNumFlyPoints-mCurrentEditPoint-1));
   }

   /*******************/
   /* Update counters */
   /*******************/
   mNumFlyPoints--;
   if (aSimulateBackspaceKey == TRUE)
   {
      if ((mCurrentEditPoint != 0) || (mNumFlyPoints == 0))
         mCurrentEditPoint--;
   }
   else //Like delete key
   {
      if (mCurrentEditPoint == mNumFlyPoints)
         mCurrentEditPoint--;
   }

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

//=============================================================================
void AtiFlyPath::ToggleCutForCurrentFlyPoint (void)
{
   /******************************/
   /* Make sure we're not flying */
   /******************************/
   if (mInFlight == TRUE)
      return;

   /**********/
   /* Toggle */
   /**********/
   if (mCurrentEditPoint > 0) //Any point except first point
   {
      if (mLocation[mCurrentEditPoint].w == 0.0)
         mLocation[mCurrentEditPoint].w = 1.0;
      else
         mLocation[mCurrentEditPoint].w = 0.0;
   }

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

//=============================================================================
// NOTE: This isn't meant to be very optimal since end-users won't ever see this
//=============================================================================
bool8 AtiFlyPath::DrawFlyPath (void)
{
   int32 i;
   int32 j;
   int32 numVertices = 0;
   AtiVertexColor1 *vertexArray;
   int32 numIndices = 0;
   uint16 *indexArray;

   bool8 inFlight = mInFlight;
   float64 startTime = mStartTime;
   float64 pausedTime = mPausedTime;
   bool8 paused = mPaused;

   if (mNumFlyPoints <= 0)
      return TRUE;

   /*******************/
   /* Allocate memory */
   /*******************/
   AtiMallocArray(vertexArray, mNumFlyPoints*46, AtiVertexColor1);
   AtiMallocArray(indexArray, mNumFlyPoints*46, uint16);

   /******************/
   /* Reset counters */
   /******************/
   numVertices = 0;
   numIndices = 0;

   if (mNumFlyPoints >= 5)
   {
      /**********************************/
      /* Build draw array for cut lines */
      /**********************************/
      for (i=0; i<mNumFlyPoints-1; i++)
      {
         if (mLocation[i].w != 0.0f)
         {
            //This point
            vertexArray[numVertices].vertex.x = mLocation[i].x;
            vertexArray[numVertices].vertex.y = mLocation[i].y;
            vertexArray[numVertices].vertex.z = mLocation[i].z;
            vertexArray[numVertices].color1.r = 0;
            vertexArray[numVertices].color1.g = 0;
            vertexArray[numVertices].color1.b = 255;
            numVertices++;

            //Next point
            vertexArray[numVertices].vertex.x = mLocation[i+1].x;
            vertexArray[numVertices].vertex.y = mLocation[i+1].y;
            vertexArray[numVertices].vertex.z = mLocation[i+1].z;
            vertexArray[numVertices].color1.r = 0;
            vertexArray[numVertices].color1.g = 0;
            vertexArray[numVertices].color1.b = 255;
            numVertices++;
         }
      }

      if (mNumFlyPoints > 0)
      {
         if ((mLocation[i].w != 0.0f) && (mNextPath->mNumFlyPoints > 0))
         {
            //This point
            vertexArray[numVertices].vertex.x = mLocation[i].x;
            vertexArray[numVertices].vertex.y = mLocation[i].y;
            vertexArray[numVertices].vertex.z = mLocation[i].z;
            vertexArray[numVertices].color1.r = 0;
            vertexArray[numVertices].color1.g = 0;
            vertexArray[numVertices].color1.b = 255;
            numVertices++;

            //Next point
            vertexArray[numVertices].vertex.x = mNextPath->mLocation[0].x;
            vertexArray[numVertices].vertex.y = mNextPath->mLocation[0].y;
            vertexArray[numVertices].vertex.z = mNextPath->mLocation[0].z;
            vertexArray[numVertices].color1.r = 0;
            vertexArray[numVertices].color1.g = 0;
            vertexArray[numVertices].color1.b = 255;
            numVertices++;
         }
      }

      /*******************************/
      /* Build draw array for spline */
      /*******************************/
      StartFlight(0.0);
      for (i=0; i<mNumSplineIntervals; i++)
      {
         for (j=0; j<7; j++)
         {
            float64 x1 = 0.0;
            float64 x2 = 0.0;

            AtiMatrix matrix;
            AtiQuaternion quat;
            AtiVertex3 pos;

            x1 = (i*7 + (j+0)) * (mSampleInterval/7.0);
            x2 = (i*7 + (j+1)) * (mSampleInterval/7.0);
            if (j == 6)
               x2 -= 0.001f;

            if (GetPathPosition(&pos, x1) == TRUE)
            {
               vertexArray[numVertices].vertex.x = pos.x;
               vertexArray[numVertices].vertex.y = pos.y;
               vertexArray[numVertices].vertex.z = pos.z;
               vertexArray[numVertices].color1.r = 255;
               vertexArray[numVertices].color1.g = 0;
               vertexArray[numVertices].color1.b = 255;
               numVertices++;
            }

            if (GetPathPosition(&pos, x2) == TRUE)
            {
               vertexArray[numVertices].vertex.x = pos.x;
               vertexArray[numVertices].vertex.y = pos.y;
               vertexArray[numVertices].vertex.z = pos.z;
               vertexArray[numVertices].color1.r = 255;
               vertexArray[numVertices].color1.g = 0;
               vertexArray[numVertices].color1.b = 255;
               numVertices++;
            }

            if (GetPathOrientation(&quat, x2) == TRUE)
            {
               AtiQuatToMatrix(&quat, matrix);

               //This point
               vertexArray[numVertices].vertex.x = pos.x;
               vertexArray[numVertices].vertex.y = pos.y;
               vertexArray[numVertices].vertex.z = pos.z;
               vertexArray[numVertices].color1.r = 255;
               vertexArray[numVertices].color1.g = 255;
               vertexArray[numVertices].color1.b = 255;
               numVertices++;

               //Up Vector
               vertexArray[numVertices].vertex.x = pos.x + matrix[1]*ATI_FLY_CUBE_RADIUS*2.0f;
               vertexArray[numVertices].vertex.y = pos.y + matrix[5]*ATI_FLY_CUBE_RADIUS*2.0f;
               vertexArray[numVertices].vertex.z = pos.z + matrix[9]*ATI_FLY_CUBE_RADIUS*2.0f;
               vertexArray[numVertices].color1.r = 255;
               vertexArray[numVertices].color1.g = 255;
               vertexArray[numVertices].color1.b = 255;
               numVertices++;

               //This point
               vertexArray[numVertices].vertex.x = pos.x;
               vertexArray[numVertices].vertex.y = pos.y;
               vertexArray[numVertices].vertex.z = pos.z;
               vertexArray[numVertices].color1.r = 255;
               vertexArray[numVertices].color1.g = 255;
               vertexArray[numVertices].color1.b = 0;
               numVertices++;

               //View Vector
               vertexArray[numVertices].vertex.x = pos.x - matrix[2]*ATI_FLY_CUBE_RADIUS*4.0f;
               vertexArray[numVertices].vertex.y = pos.y - matrix[6]*ATI_FLY_CUBE_RADIUS*4.0f;
               vertexArray[numVertices].vertex.z = pos.z - matrix[10]*ATI_FLY_CUBE_RADIUS*4.0f;

⌨️ 快捷键说明

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