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

OpenGL program

Development Platform:

Visual C++

  1. /////////////////////////////////////////////////////////////////////////////
  2. // KeyFrame.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. #ifdef _DEBUG
  22. #define new DEBUG_NEW
  23. #undef THIS_FILE
  24. static char THIS_FILE[] = __FILE__;
  25. #endif
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CKeyFrame
  28. IMPLEMENT_DYNAMIC(CKeyFrame, CAnimKeyFrame)
  29. /////////////////////////////////////////////////////////////////////////////
  30. // CKeyFrame construction
  31. CKeyFrame::CKeyFrame(double dTime, C3dObject* pObject)
  32. {
  33. if(!pObject)
  34. return;
  35. // Set the attributes to default values..
  36. m_pKeyFramePrevious = NULL;
  37. m_dTime = dTime;
  38. m_Color.SetColor4fv(&pObject->m_Color);
  39. VecCopy4f(pObject->m_fOrigin,  m_fOrigin);
  40. VecCopy3f(pObject->m_fRotation,  m_fRotation);
  41. VecCopy3f(pObject->m_fScale,  m_fScale);
  42. VecCopy3f(pObject->m_fTranslate, m_fTranslate);
  43. if(pObject->m_iType == LIGHT_OBJECT)
  44. {
  45. C3dObjectLight* pLight = (C3dObjectLight*)pObject;
  46. m_ColorAmb.SetColor4fv(&pLight->m_ColorAmbient);
  47. m_ColorDif.SetColor4fv(&pLight->m_ColorDiffuse);
  48. m_ColorSpc.SetColor4fv(&pLight->m_ColorSpecular);
  49. m_fSpotAngle = pLight->m_fSpotAngle;
  50. }
  51. }
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CKeyFrame Destructor
  54. CKeyFrame::~CKeyFrame()
  55. {
  56. }
  57. /////////////////////////////////////////////////////////////////////////////
  58. // CKeyFrame Methods or virtual function implimentation
  59. void CKeyFrame::AnimateCamera(C3dCamera* pCamera, double dTime)
  60. {
  61. }
  62. void CKeyFrame::AnimateObject(C3dObject* pObject, double dTime)
  63. {
  64. // KeyFrame Animation procedure.  Calculate the incremental values for x,y,z
  65. // and color RGBA.  Note that we update ONLY the object attributes which
  66. // change from one KeyFrame to the next.  This allows other animation procedures
  67. // a chance to modify the objects attributes..
  68. GLfloat fKeyIncrement[4];
  69. // Ensure that we have a vaild pointer..
  70. if(!pObject)
  71. return;
  72. double dTimeBetweenFrames  = m_dTime-m_pKeyFramePrevious->m_dTime;
  73. double dTimeSinceLastFrame = dTime-m_pKeyFramePrevious->m_dTime;
  74. double dPercentThisFrame = dTimeSinceLastFrame/dTimeBetweenFrames;
  75. // Calculate the Origin incremental index values
  76. fKeyIncrement[X] = (GLfloat)((m_fOrigin[X] - m_pKeyFramePrevious->m_fOrigin[X]) * dPercentThisFrame);
  77. fKeyIncrement[Y] = (GLfloat)((m_fOrigin[Y] - m_pKeyFramePrevious->m_fOrigin[Y]) * dPercentThisFrame);
  78. fKeyIncrement[Z] = (GLfloat)((m_fOrigin[Z] - m_pKeyFramePrevious->m_fOrigin[Z]) * dPercentThisFrame);
  79. if(fKeyIncrement[X])
  80. pObject->m_fOrigin[X] = m_pKeyFramePrevious->m_fOrigin[X] + fKeyIncrement[X];
  81. if(fKeyIncrement[Y])
  82. pObject->m_fOrigin[Y] = m_pKeyFramePrevious->m_fOrigin[Y] + fKeyIncrement[Y];
  83. if(fKeyIncrement[Z])
  84. pObject->m_fOrigin[Z] = m_pKeyFramePrevious->m_fOrigin[Z] + fKeyIncrement[Z];
  85. // Calculate the Rotation incremental index values
  86. fKeyIncrement[X] = (GLfloat)((m_fRotation[X] - m_pKeyFramePrevious->m_fRotation[X]) * dPercentThisFrame);
  87. fKeyIncrement[Y] = (GLfloat)((m_fRotation[Y] - m_pKeyFramePrevious->m_fRotation[Y]) * dPercentThisFrame);
  88. fKeyIncrement[Z] = (GLfloat)((m_fRotation[Z] - m_pKeyFramePrevious->m_fRotation[Z]) * dPercentThisFrame);
  89. if(fKeyIncrement[X])
  90. pObject->m_fRotation[X] = m_pKeyFramePrevious->m_fRotation[X] + fKeyIncrement[X];
  91. if(fKeyIncrement[Y])
  92. pObject->m_fRotation[Y] = m_pKeyFramePrevious->m_fRotation[Y] + fKeyIncrement[Y];
  93. if(fKeyIncrement[Z])
  94. pObject->m_fRotation[Z] = m_pKeyFramePrevious->m_fRotation[Z] + fKeyIncrement[Z];
  95. // Calculate the Scale incremental index values
  96. fKeyIncrement[X] = (GLfloat)((m_fScale[X] - m_pKeyFramePrevious->m_fScale[X]) * dPercentThisFrame);
  97. fKeyIncrement[Y] = (GLfloat)((m_fScale[Y] - m_pKeyFramePrevious->m_fScale[Y]) * dPercentThisFrame);
  98. fKeyIncrement[Z] = (GLfloat)((m_fScale[Z] - m_pKeyFramePrevious->m_fScale[Z]) * dPercentThisFrame);
  99. if(fKeyIncrement[X])
  100. pObject->m_fScale[X] = m_pKeyFramePrevious->m_fScale[X] + fKeyIncrement[X];
  101. if(fKeyIncrement[Y])
  102. pObject->m_fScale[Y] = m_pKeyFramePrevious->m_fScale[Y] + fKeyIncrement[Y];
  103. if(fKeyIncrement[Z])
  104. pObject->m_fScale[Z] = m_pKeyFramePrevious->m_fScale[Z] + fKeyIncrement[Z];
  105. // Calculate the Translation incremental index values
  106. fKeyIncrement[X] = (GLfloat)((m_fTranslate[X] - m_pKeyFramePrevious->m_fTranslate[X]) * dPercentThisFrame);
  107. fKeyIncrement[Y] = (GLfloat)((m_fTranslate[Y] - m_pKeyFramePrevious->m_fTranslate[Y]) * dPercentThisFrame);
  108. fKeyIncrement[Z] = (GLfloat)((m_fTranslate[Z] - m_pKeyFramePrevious->m_fTranslate[Z]) * dPercentThisFrame);
  109. if(fKeyIncrement[X])
  110. pObject->m_fTranslate[X] = m_pKeyFramePrevious->m_fTranslate[X] + fKeyIncrement[X];
  111. if(fKeyIncrement[Y])
  112. pObject->m_fTranslate[Y] = m_pKeyFramePrevious->m_fTranslate[Y] + fKeyIncrement[Y];
  113. if(fKeyIncrement[Z])
  114. pObject->m_fTranslate[Z] = m_pKeyFramePrevious->m_fTranslate[Z] + fKeyIncrement[Z];
  115. // Calculate the Color RGBA incremental index values
  116. fKeyIncrement[0] = (GLfloat)((m_Color.m_fColor[0] - m_pKeyFramePrevious->m_Color.m_fColor[0]) * dPercentThisFrame);
  117. fKeyIncrement[1] = (GLfloat)((m_Color.m_fColor[1] - m_pKeyFramePrevious->m_Color.m_fColor[1]) * dPercentThisFrame);
  118. fKeyIncrement[2] = (GLfloat)((m_Color.m_fColor[2] - m_pKeyFramePrevious->m_Color.m_fColor[2]) * dPercentThisFrame);
  119. fKeyIncrement[3] = (GLfloat)((m_Color.m_fColor[3] - m_pKeyFramePrevious->m_Color.m_fColor[3]) * dPercentThisFrame);
  120. if(fKeyIncrement[0])
  121. pObject->m_Color.m_fColor[0] = m_pKeyFramePrevious->m_Color.m_fColor[0] + fKeyIncrement[0];
  122. if(fKeyIncrement[1])
  123. pObject->m_Color.m_fColor[1] = m_pKeyFramePrevious->m_Color.m_fColor[1] + fKeyIncrement[1];
  124. if(fKeyIncrement[2])
  125. pObject->m_Color.m_fColor[2] = m_pKeyFramePrevious->m_Color.m_fColor[2] + fKeyIncrement[2];
  126. if(fKeyIncrement[3])
  127. pObject->m_Color.m_fColor[3] = m_pKeyFramePrevious->m_Color.m_fColor[3] + fKeyIncrement[3];
  128. if(pObject->m_iType == LIGHT_OBJECT)
  129. {
  130. C3dObjectLight* pLight = (C3dObjectLight*)pObject;
  131. // Calculate the Ambient Color RGBA incremental index values
  132. fKeyIncrement[0] = (GLfloat)((m_ColorAmb.m_fColor[0] - m_pKeyFramePrevious->m_ColorAmb.m_fColor[0]) * dPercentThisFrame);
  133. fKeyIncrement[1] = (GLfloat)((m_ColorAmb.m_fColor[1] - m_pKeyFramePrevious->m_ColorAmb.m_fColor[1]) * dPercentThisFrame);
  134. fKeyIncrement[2] = (GLfloat)((m_ColorAmb.m_fColor[2] - m_pKeyFramePrevious->m_ColorAmb.m_fColor[2]) * dPercentThisFrame);
  135. fKeyIncrement[3] = (GLfloat)((m_ColorAmb.m_fColor[3] - m_pKeyFramePrevious->m_ColorAmb.m_fColor[3]) * dPercentThisFrame);
  136. if(fKeyIncrement[0])
  137. pLight->m_ColorAmbient.m_fColor[0] = m_pKeyFramePrevious->m_ColorAmb.m_fColor[0] + fKeyIncrement[0];
  138. if(fKeyIncrement[1])
  139. pLight->m_ColorAmbient.m_fColor[1] = m_pKeyFramePrevious->m_ColorAmb.m_fColor[1] + fKeyIncrement[1];
  140. if(fKeyIncrement[2])
  141. pLight->m_ColorAmbient.m_fColor[2] = m_pKeyFramePrevious->m_ColorAmb.m_fColor[2] + fKeyIncrement[2];
  142. if(fKeyIncrement[3])
  143. pLight->m_ColorAmbient.m_fColor[3] = m_pKeyFramePrevious->m_ColorAmb.m_fColor[3] + fKeyIncrement[3];
  144. // Calculate the Diffuse Color RGBA incremental index values
  145. fKeyIncrement[0] = (GLfloat)((m_ColorDif.m_fColor[0] - m_pKeyFramePrevious->m_ColorDif.m_fColor[0]) * dPercentThisFrame);
  146. fKeyIncrement[1] = (GLfloat)((m_ColorDif.m_fColor[1] - m_pKeyFramePrevious->m_ColorDif.m_fColor[1]) * dPercentThisFrame);
  147. fKeyIncrement[2] = (GLfloat)((m_ColorDif.m_fColor[2] - m_pKeyFramePrevious->m_ColorDif.m_fColor[2]) * dPercentThisFrame);
  148. fKeyIncrement[3] = (GLfloat)((m_ColorDif.m_fColor[3] - m_pKeyFramePrevious->m_ColorDif.m_fColor[3]) * dPercentThisFrame);
  149. if(fKeyIncrement[0])
  150. pLight->m_ColorDiffuse.m_fColor[0] = m_pKeyFramePrevious->m_ColorDif.m_fColor[0] + fKeyIncrement[0];
  151. if(fKeyIncrement[1])
  152. pLight->m_ColorDiffuse.m_fColor[1] = m_pKeyFramePrevious->m_ColorDif.m_fColor[1] + fKeyIncrement[1];
  153. if(fKeyIncrement[2])
  154. pLight->m_ColorDiffuse.m_fColor[2] = m_pKeyFramePrevious->m_ColorDif.m_fColor[2] + fKeyIncrement[2];
  155. if(fKeyIncrement[3])
  156. pLight->m_ColorDiffuse.m_fColor[3] = m_pKeyFramePrevious->m_ColorDif.m_fColor[3] + fKeyIncrement[3];
  157. // Calculate the Specular Color RGBA incremental index values
  158. fKeyIncrement[0] = (GLfloat)((m_ColorSpc.m_fColor[0] - m_pKeyFramePrevious->m_ColorSpc.m_fColor[0]) * dPercentThisFrame);
  159. fKeyIncrement[1] = (GLfloat)((m_ColorSpc.m_fColor[1] - m_pKeyFramePrevious->m_ColorSpc.m_fColor[1]) * dPercentThisFrame);
  160. fKeyIncrement[2] = (GLfloat)((m_ColorSpc.m_fColor[2] - m_pKeyFramePrevious->m_ColorSpc.m_fColor[2]) * dPercentThisFrame);
  161. fKeyIncrement[3] = (GLfloat)((m_ColorSpc.m_fColor[3] - m_pKeyFramePrevious->m_ColorSpc.m_fColor[3]) * dPercentThisFrame);
  162. if(fKeyIncrement[0])
  163. pLight->m_ColorSpecular.m_fColor[0] = m_pKeyFramePrevious->m_ColorSpc.m_fColor[0] + fKeyIncrement[0];
  164. if(fKeyIncrement[1])
  165. pLight->m_ColorSpecular.m_fColor[1] = m_pKeyFramePrevious->m_ColorSpc.m_fColor[1] + fKeyIncrement[1];
  166. if(fKeyIncrement[2])
  167. pLight->m_ColorSpecular.m_fColor[2] = m_pKeyFramePrevious->m_ColorSpc.m_fColor[2] + fKeyIncrement[2];
  168. if(fKeyIncrement[3])
  169. pLight->m_ColorSpecular.m_fColor[3] = m_pKeyFramePrevious->m_ColorSpc.m_fColor[3] + fKeyIncrement[3];
  170. // Calculate the Spot angle incremental value
  171. fKeyIncrement[0] = (GLfloat)((m_fSpotAngle - m_pKeyFramePrevious->m_fSpotAngle) * dPercentThisFrame);
  172. if(fKeyIncrement[0])
  173. pLight->m_fSpotAngle = m_pKeyFramePrevious->m_fSpotAngle + fKeyIncrement[0];
  174. }
  175. }
  176. void CKeyFrame::Serialize(CArchive& ar, int iVersion)
  177. {
  178. CString szBuffer;
  179. CString szName;
  180. szBuffer.GetBuffer(256);
  181. szName.GetBuffer(128);
  182. if (ar.IsStoring())
  183. {
  184. // Save the CAnimation derived class header...
  185. szBuffer.Format("n%sCKeyFrame {n", szIndent);
  186. ar.WriteString(szBuffer);
  187. // Save the this KeyFrames' specific data...
  188. szBuffer.Format("%stTime Ref      < %lf >n", szIndent, m_dTime);
  189. ar.WriteString(szBuffer);
  190. szBuffer.Format("%stColor         < %f %f %f %f > // RGBAn", szIndent, m_Color.m_fColor[0], m_Color.m_fColor[1], m_Color.m_fColor[2], m_Color.m_fColor[3]);
  191. ar.WriteString(szBuffer);
  192. szBuffer.Format("%stAmbient       < %f %f %f %f > // RGBAn", szIndent, m_ColorAmb.m_fColor[0], m_ColorAmb.m_fColor[1], m_ColorAmb.m_fColor[2], m_ColorAmb.m_fColor[3]);
  193. ar.WriteString(szBuffer);
  194. szBuffer.Format("%stDiffuse       < %f %f %f %f > // RGBAn", szIndent, m_ColorDif.m_fColor[0], m_ColorDif.m_fColor[1], m_ColorDif.m_fColor[2], m_ColorDif.m_fColor[3]);
  195. ar.WriteString(szBuffer);
  196. szBuffer.Format("%stSpecular      < %f %f %f %f > // RGBAn", szIndent, m_ColorSpc.m_fColor[0], m_ColorSpc.m_fColor[1], m_ColorSpc.m_fColor[2], m_ColorSpc.m_fColor[3]);
  197. ar.WriteString(szBuffer);
  198. szBuffer.Format("%stOrigin        < %f %f %f %f >n", szIndent, m_fOrigin[X], m_fOrigin[Y], m_fOrigin[Z], m_fOrigin[W]);
  199. ar.WriteString(szBuffer);
  200. szBuffer.Format("%stRotation      < %f %f %f >n", szIndent, m_fRotation[X], m_fRotation[Y], m_fRotation[Z]);
  201. ar.WriteString(szBuffer);
  202. szBuffer.Format("%stScale         < %f %f %f >n", szIndent, m_fScale[X], m_fScale[Y], m_fScale[Z]);
  203. ar.WriteString(szBuffer);
  204. szBuffer.Format("%stTranslate     < %f %f %f >n", szIndent, m_fTranslate[X], m_fTranslate[Y], m_fTranslate[Z]);
  205. ar.WriteString(szBuffer);
  206. szBuffer.Format("%stSpotAngle     < %f >n", szIndent, m_fSpotAngle);
  207. ar.WriteString(szBuffer);
  208. szBuffer.Format("%s}n", szIndent); // end of animation def
  209. ar.WriteString(szBuffer);
  210. }
  211. else
  212. {
  213. // Read the derived class data..
  214. ar.ReadString(szBuffer);
  215. szBuffer.TrimLeft(); // Remove leading white spaces
  216. sscanf(szBuffer, "Time Ref      < %lf >n", &m_dTime);
  217. ar.ReadString(szBuffer);
  218. szBuffer.TrimLeft();
  219. sscanf(szBuffer, "Color         < %f %f %f %f >n", &m_Color.m_fColor[0], &m_Color.m_fColor[1], &m_Color.m_fColor[2], &m_Color.m_fColor[3]);
  220. if(iVersion > 120)
  221. {
  222. ar.ReadString(szBuffer);
  223. szBuffer.TrimLeft();
  224. sscanf(szBuffer, "Ambient       < %f %f %f %f >n", &m_ColorAmb.m_fColor[0], &m_ColorAmb.m_fColor[1], &m_ColorAmb.m_fColor[2], &m_ColorAmb.m_fColor[3]);
  225. ar.ReadString(szBuffer);
  226. szBuffer.TrimLeft();
  227. sscanf(szBuffer, "Diffuse       < %f %f %f %f >n", &m_ColorDif.m_fColor[0], &m_ColorDif.m_fColor[1], &m_ColorDif.m_fColor[2], &m_ColorDif.m_fColor[3]);
  228. ar.ReadString(szBuffer);
  229. szBuffer.TrimLeft();
  230. sscanf(szBuffer, "Specular      < %f %f %f %f >n", &m_ColorSpc.m_fColor[0], &m_ColorSpc.m_fColor[1], &m_ColorSpc.m_fColor[2], &m_ColorSpc.m_fColor[3]);
  231. }
  232. ar.ReadString(szBuffer);
  233. szBuffer.TrimLeft();
  234. sscanf(szBuffer, "Origin        < %f %f %f %f >n", &m_fOrigin[X], &m_fOrigin[Y], &m_fOrigin[Z], &m_fOrigin[W]);
  235. ar.ReadString(szBuffer);
  236. szBuffer.TrimLeft();
  237. sscanf(szBuffer, "Rotation      < %f %f %f >n", &m_fRotation[X], &m_fRotation[Y], &m_fRotation[Z]);
  238. ar.ReadString(szBuffer);
  239. szBuffer.TrimLeft();
  240. sscanf(szBuffer, "Scale         < %f %f %f >n", &m_fScale[X], &m_fScale[Y], &m_fScale[Z]);
  241. ar.ReadString(szBuffer);
  242. szBuffer.TrimLeft();
  243. sscanf(szBuffer, "Translate     < %f %f %f >n", &m_fTranslate[X], &m_fTranslate[Y], &m_fTranslate[Z]);
  244. if(iVersion > 120)
  245. {
  246. ar.ReadString(szBuffer);
  247. szBuffer.TrimLeft();
  248. sscanf(szBuffer, "SpotAngle     < %f >n", &m_fSpotAngle);
  249. }
  250. // Must remove the CKeyFrame end marker!
  251. ar.ReadString(szBuffer);
  252. szBuffer.TrimLeft();
  253. sscanf(szBuffer, "}n");
  254. }
  255. }
  256. /////////////////////////////////////////////////////////////////////////////
  257. // CKeyFrame function implimentation
  258. void CKeyFrame::SetObjectAttributes(C3dObject* pObject)
  259. {
  260. if(!pObject)
  261. return;
  262. // Set the objects attributes to this this keyframe
  263. pObject->m_Color.SetColor4fv(&m_Color);
  264. VecCopy4f(m_fOrigin,    pObject->m_fOrigin);
  265. VecCopy3f(m_fRotation,  pObject->m_fRotation);
  266. VecCopy3f(m_fScale,     pObject->m_fScale);
  267. VecCopy3f(m_fTranslate, pObject->m_fTranslate);
  268. if(pObject->m_iType == LIGHT_OBJECT)
  269. {
  270. C3dObjectLight* pLight = (C3dObjectLight*)pObject;
  271. pLight->m_ColorAmbient.SetColor4fv(&m_ColorAmb);
  272. pLight->m_ColorDiffuse.SetColor4fv(&m_ColorDif);
  273. pLight->m_ColorSpecular.SetColor4fv(&m_ColorSpc);
  274. pLight->m_fSpotAngle = m_fSpotAngle;
  275. }
  276. }