AnimSpin.cpp
Upload User: shxiangxiu
Upload Date: 2007-01-03
Package Size: 1101k
Code Size: 7k
Category:

OpenGL program

Development Platform:

Visual C++

  1. /////////////////////////////////////////////////////////////////////////////
  2. // AnimSpin.cpp : implementation file
  3. //
  4. // glOOP (OpenGL Object Oriented Programming library)
  5. // Copyright (c) Craig Fahrnbach 1997, 1998
  6. //
  7. // OpenGL is a registered trademark of Silicon Graphics
  8. //
  9. //
  10. // This program is provided for educational and personal use only and
  11. // is provided without guarantee or warrantee expressed or implied.
  12. //
  13. // Commercial use is strickly prohibited without written permission
  14. // from ImageWare Development.
  15. //
  16. // This program is -not- in the public domain.
  17. //
  18. /////////////////////////////////////////////////////////////////////////////
  19. #include "stdafx.h"
  20. #include "glOOP.h"
  21. #include "AnimationDialog.h"
  22. #ifdef _DEBUG
  23. #define new DEBUG_NEW
  24. #undef THIS_FILE
  25. static char THIS_FILE[] = __FILE__;
  26. #endif
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CAnimSpin
  29. IMPLEMENT_DYNAMIC(CAnimSpin, CAnimation)
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CAnimSpin construction
  32. CAnimSpin::CAnimSpin()
  33. {
  34. // Set the attributes to default values..
  35. m_szName = SZ_ANIMATE_SPIN;
  36. m_bFirst = TRUE;
  37. m_fSpinX = 2.0f;
  38. m_fSpinY = 2.0f;
  39. m_fSpinZ = 2.0f;
  40. m_dSpeedX = 0.0f;
  41. m_dSpeedY = 0.0f;
  42. m_dSpeedZ = 0.0f;
  43. }
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CAnimSpin Destructor
  46. CAnimSpin::~CAnimSpin()
  47. {
  48. }
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CAnimSpin Methods or virtual function implimentation
  51. void CAnimSpin::AddAnimationPage(LPVOID pSht, C3dObject* pObject, C3dCamera* pCamera, C3dWorld* pWorld)
  52. {
  53. CAnimPropSheet* pSheet = (CAnimPropSheet*)pSht;
  54. ASSERT(pSheet);
  55. // Add the page to the property sheet
  56. pSheet->AddPage(&pSheet->m_SpinPage);
  57. // Save the address of this animation procedure in the page
  58. pSheet->m_SpinPage.m_pAnimation = this;
  59. }
  60. void CAnimSpin::AnimateCamera(C3dCamera* pCamera, double dTime)
  61. {
  62. GLfloat fXIncrement; // Rotation incremental values
  63. GLfloat fYIncrement;
  64. GLfloat fZIncrement;
  65. // Ensure that we have a vaild pointer..
  66. if(!pCamera)
  67. return;
  68. // First pass through, just save the objects original
  69. // parameters
  70. if(m_bFirst)
  71. {
  72. m_bFirst = FALSE;
  73. SaveCameraAttributes(pCamera);
  74. return;
  75. }
  76. // Calculate the incremental index values
  77. if(m_dSpeedX)
  78. fXIncrement = (GLfloat)(m_fSpinX/(m_dSpeedX/(dTime-m_dTimePrevious)));
  79. else
  80. fXIncrement = m_fSpinX; // incrememt as fast as possible..
  81. if(m_dSpeedY)
  82. fYIncrement = (GLfloat)(m_fSpinY/(m_dSpeedY/(dTime-m_dTimePrevious)));
  83. else
  84. fYIncrement = m_fSpinY; // incrememt as fast as possible..
  85. if(m_dSpeedZ)
  86. fZIncrement = (GLfloat)(m_fSpinZ/(m_dSpeedZ/(dTime-m_dTimePrevious)));
  87. else
  88. fZIncrement = m_fSpinZ; // incrememt as fast as possible..
  89. // Rotate or spin the camera
  90. pCamera->m_fRotation[X] += fXIncrement;
  91. if(pCamera->m_fRotation[X] > 360)
  92. pCamera->m_fRotation[X] -= 360;
  93. pCamera->m_fRotation[Y] += fYIncrement;
  94. if(pCamera->m_fRotation[Y] > 360)
  95. pCamera->m_fRotation[Y] -= 360;
  96. pCamera->m_fRotation[Z] += fZIncrement;
  97. if(pCamera->m_fRotation[Z] > 360)
  98. pCamera->m_fRotation[Z] -= 360;
  99. // Save the time of the last iteration
  100. m_dTimePrevious = dTime;
  101. }
  102. void CAnimSpin::AnimateObject(C3dObject* pObject, double dTime)
  103. {
  104. GLfloat fXIncrement; // Rotation incremental values
  105. GLfloat fYIncrement;
  106. GLfloat fZIncrement;
  107. // Ensure that we have a vaild pointer..
  108. if(!pObject)
  109. return;
  110. // First pass through, just save the objects original
  111. // parameters
  112. if(m_bFirst)
  113. {
  114. m_bFirst = FALSE;
  115. SaveObjectAttributes(pObject);
  116. return;
  117. }
  118. // Calculate the incremental index values
  119. if(m_dSpeedX)
  120. fXIncrement = (GLfloat)(m_fSpinX/(m_dSpeedX/(dTime-m_dTimePrevious)));
  121. else
  122. fXIncrement = m_fSpinX; // incrememt as fast as possible..
  123. if(m_dSpeedY)
  124. fYIncrement = (GLfloat)(m_fSpinY/(m_dSpeedY/(dTime-m_dTimePrevious)));
  125. else
  126. fYIncrement = m_fSpinY; // incrememt as fast as possible..
  127. if(m_dSpeedZ)
  128. fZIncrement = (GLfloat)(m_fSpinZ/(m_dSpeedZ/(dTime-m_dTimePrevious)));
  129. else
  130. fZIncrement = m_fSpinZ; // incrememt as fast as possible..
  131. // Rotate or spin the shape
  132. if(m_fSpinX)
  133. {
  134. pObject->m_fRotation[X] += fXIncrement;
  135. if(pObject->m_fRotation[X] > 360)
  136. pObject->m_fRotation[X] -= 360;
  137. if(pObject->m_fRotation[X] < 0)
  138. pObject->m_fRotation[X] += 360;
  139. }
  140. if(m_fSpinY)
  141. {
  142. pObject->m_fRotation[Y] += fYIncrement;
  143. if(pObject->m_fRotation[Y] > 360)
  144. pObject->m_fRotation[Y] -= 360;
  145. if(pObject->m_fRotation[Y] < 0)
  146. pObject->m_fRotation[Y] += 360;
  147. }
  148. if(m_fSpinZ)
  149. {
  150. pObject->m_fRotation[Z] += fZIncrement;
  151. if(pObject->m_fRotation[Z] > 360)
  152. pObject->m_fRotation[Z] -= 360;
  153. if(pObject->m_fRotation[Z] < 0)
  154. pObject->m_fRotation[Z] += 360;
  155. }
  156. // Save the time of the last iteration
  157. m_dTimePrevious = dTime;
  158. }
  159. void CAnimSpin::Serialize(CArchive& ar, int iVersion)
  160. {
  161. CString szBuffer;
  162. CString szName;
  163. szBuffer.GetBuffer(256);
  164. szName.GetBuffer(128);
  165. if (ar.IsStoring())
  166. {
  167. // Save the CAnimation derived class header...
  168. szBuffer.Format("%sCAnimSpin {n", szIndent);
  169. ar.WriteString(szBuffer);
  170. // Save the this animation procedures' specific data...
  171. szBuffer.Format("%stSpinX         < %f >n", szIndent, m_fSpinX);
  172. ar.WriteString(szBuffer);
  173. szBuffer.Format("%stSpinY         < %f >n", szIndent, m_fSpinY);
  174. ar.WriteString(szBuffer);
  175. szBuffer.Format("%stSpinZ         < %f >n", szIndent, m_fSpinZ);
  176. ar.WriteString(szBuffer);
  177. szBuffer.Format("%stSpinSpeedX    < %f >n", szIndent, m_dSpeedX);
  178. ar.WriteString(szBuffer);
  179. szBuffer.Format("%stSpinSpeedY    < %f >n", szIndent, m_dSpeedY);
  180. ar.WriteString(szBuffer);
  181. szBuffer.Format("%stSpinSpeedZ    < %f >n", szIndent, m_dSpeedZ);
  182. ar.WriteString(szBuffer);
  183. // Save the base class data...
  184. CAnimation::Serialize(ar, iVersion);
  185. szBuffer.Format("%s}n", szIndent); // end of animation def
  186. ar.WriteString(szBuffer);
  187. }
  188. else
  189. {
  190. // Read the derived class data..
  191. ar.ReadString(szBuffer);
  192. szBuffer.TrimLeft(); // Remove leading white spaces
  193. sscanf(szBuffer, "SpinX         < %f >n", &m_fSpinX);
  194. ar.ReadString(szBuffer);
  195. szBuffer.TrimLeft();
  196. sscanf(szBuffer, "SpinY         < %f >n", &m_fSpinY);
  197. ar.ReadString(szBuffer);
  198. szBuffer.TrimLeft();
  199. sscanf(szBuffer, "SpinZ         < %f >n", &m_fSpinZ);
  200. ar.ReadString(szBuffer);
  201. szBuffer.TrimLeft();
  202. sscanf(szBuffer, "SpinSpeedX    < %lf >n", (float*)&m_dSpeedX);
  203. ar.ReadString(szBuffer);
  204. szBuffer.TrimLeft();
  205. sscanf(szBuffer, "SpinSpeedY    < %lf >n", (float*)&m_dSpeedY);
  206. ar.ReadString(szBuffer);
  207. szBuffer.TrimLeft();
  208. sscanf(szBuffer, "SpinSpeedZ    < %lf >n", (float*)&m_dSpeedZ);
  209. // Read the base class data...
  210. CAnimation::Serialize(ar, iVersion);
  211. }
  212. }
  213. void CAnimSpin::Reset()
  214. {
  215. // TODO:  Add any CAnimSpin reset code here..
  216. }
  217. /////////////////////////////////////////////////////////////////////////////
  218. // CAnimSpin function implimentation