AnimKeyFrame.cpp
资源名称:gloop.zip [点击查看]
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:6k
源码类别:
OpenGL
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // AnimKeyFrame.cpp : implementation file
- //
- // glOOP (OpenGL Object Oriented Programming library)
- // Copyright (c) Craig Fahrnbach 1997, 1998
- //
- // OpenGL is a registered trademark of Silicon Graphics
- //
- //
- // This program is provided for educational and personal use only and
- // is provided without guarantee or warrantee expressed or implied.
- //
- // Commercial use is strickly prohibited without written permission
- // from ImageWare Development.
- //
- // This program is -not- in the public domain.
- //
- /////////////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "glOOP.h"
- #include "AnimationDialog.h"
- #include <math.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CAnimKeyFrame
- IMPLEMENT_DYNAMIC(CAnimKeyFrame, CAnimation)
- /////////////////////////////////////////////////////////////////////////////
- // CAnimKeyFrame construction
- CAnimKeyFrame::CAnimKeyFrame()
- {
- // Set the attributes to default values..
- m_szName = SZ_ANIMATE_KEYFRAME;
- m_bCycleKeyFrames = TRUE;
- // Create a KeyFrame List
- m_pKeyFrameList = new CKeyFrameList;
- ASSERT(m_pKeyFrameList);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CAnimKeyFrame Destructor
- CAnimKeyFrame::~CAnimKeyFrame()
- {
- // Delete the keyframe list
- if(m_pKeyFrameList)
- delete m_pKeyFrameList;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CAnimKeyFrame Methods or virtual function implimentation
- void CAnimKeyFrame::AddAnimationPage(LPVOID pSht, C3dObject* pObject, C3dCamera* pCamera, C3dWorld* pWorld)
- {
- CAnimPropSheet* pSheet = (CAnimPropSheet*)pSht;
- ASSERT(pSheet);
- // Add the page to the property sheet
- pSheet->AddPage(&pSheet->m_KeyFramePage);
- // Save the address of this animation procedure in the page
- pSheet->m_KeyFramePage.m_pAnimation = this;
- }
- void CAnimKeyFrame::AnimateCamera(C3dCamera* pCamera, double dTime)
- {
- // First pass through, just save the objects original
- // parameters
- if(m_bFirst)
- {
- m_bFirst = FALSE;
- SaveCameraAttributes(pCamera);
- return;
- }
- }
- void CAnimKeyFrame::AnimateObject(C3dObject* pObject, double dTime)
- {
- // First pass through, just save the objects original
- // parameters
- if(m_bFirst)
- {
- m_bFirst = FALSE;
- SaveObjectAttributes(pObject);
- return;
- }
- // Ensure the list is not empty then calculate a new time index
- // if necessary.
- if(!m_pKeyFrameList->IsEmpty())
- {
- // Get the last keyframe
- CKeyFrame* pLastKey = (CKeyFrame*) m_pKeyFrameList->GetTail();
- if(m_bCycleKeyFrames) // Continuously cycle keyframes
- {
- if(pLastKey->m_dTime < dTime)
- // Last key is less than 'dTime', so divide the target 'dTime'
- // by the last keyframes' time and extract the remainder
- dTime = fmod( dTime, pLastKey->m_dTime);
- }
- else // Cycle through KeyFrame list once only..
- {
- if(pLastKey->m_dTime < dTime)
- // The animation time index is greater than last keyframe value,
- // so set the time index equal to the last valid keyframe so that
- // the object ends EXACTLY on the last time interpolated value
- dTime = pLastKey->m_dTime;
- }
- }
- // Step through list backward until we find a keyframe
- // which is less or equal to 'dTime'
- POSITION pos = m_pKeyFrameList->GetTailPosition();
- ASSERT(pos);
- do {
- POSITION thispos = pos;
- CKeyFrame* pKeyPrevious = (CKeyFrame*) m_pKeyFrameList->GetPrev(pos);
- if (pKeyPrevious->m_dTime <= dTime)
- {
- // Found the keyframe who's time is smaller than the target, now
- // see if we have next key frame.
- CKeyFrame* pKey = (CKeyFrame*) m_pKeyFrameList->GetNext(thispos);
- if(thispos)
- {
- pKey = (CKeyFrame*) m_pKeyFrameList->GetNext(thispos);
- if(pKey)
- {
- pKey->m_pKeyFramePrevious = pKeyPrevious;
- // Animate the keyframe
- pKey->AnimateObject(pObject, dTime);
- }
- }
- return;
- }
- } while (pos);
- }
- void CAnimKeyFrame::Serialize(CArchive& ar, int iVersion)
- {
- CString szIndentSave;
- CString szBuffer;
- CString szName;
- szBuffer.GetBuffer(256);
- szName.GetBuffer(128);
- if (ar.IsStoring())
- {
- // Save the CAnimation derived class header...
- szBuffer.Format("%sCAnimKeyFrame {n", szIndent);
- ar.WriteString(szBuffer);
- // Save the CAnimKeyFrame specific data...
- szBuffer.Format("%stCycle Frames < %d >n", szIndent, m_bCycleKeyFrames);
- ar.WriteString(szBuffer);
- // Save all keyframes in the list
- m_pKeyFrameList->Serialize(ar, iVersion);
- szBuffer.Format("%s}n", szIndent); // end of animation def
- ar.WriteString(szBuffer);
- }
- else
- {
- // Read the derived class data..
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft(); // Remove leading white spaces
- sscanf(szBuffer, "Cycle Frames < %d >n", &m_bCycleKeyFrames);
- // Load all CAnimation procedures
- while(ar.ReadString(szBuffer))
- {
- szBuffer.TrimLeft(); // Remove leading white spaces
- if(szBuffer.Compare("}") == 0) // end of CAnimKeyFrame procedure
- break;
- if(szBuffer.Compare("CKeyFrame List {") == 0)
- m_pKeyFrameList->Serialize(ar, iVersion);
- }
- }
- }
- void CAnimKeyFrame::Reset()
- {
- // TODO: Add any CAnimKeyFrame reset code here..
- }
- /////////////////////////////////////////////////////////////////////////////
- // CAnimKeyFrame function implimentation