AnimWobble.cpp
资源名称:gloop.zip [点击查看]
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:9k
源码类别:
OpenGL
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // AnimWobble.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"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CAnimWobble
- IMPLEMENT_DYNAMIC(CAnimWobble, CAnimation)
- /////////////////////////////////////////////////////////////////////////////
- // CAnimWobble construction
- CAnimWobble::CAnimWobble()
- {
- // Set the attributes to default values..
- m_szName = SZ_ANIMATE_WOBBLE;
- m_bFirst = TRUE;
- m_fWobbleX = 2.0f;
- m_fWobbleY = 2.0f;
- m_fWobbleZ = 2.0f;
- m_fLimitX = 45.0f;
- m_fLimitY = 45.0f;
- m_fLimitZ = 45.0f;
- m_dSpeedX = 0.0f;
- m_dSpeedY = 0.0f;
- m_dSpeedZ = 0.0f;
- m_bIncX = TRUE;
- m_bIncY = TRUE;
- m_bIncZ = TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CAnimWobble Destructor
- CAnimWobble::~CAnimWobble()
- {
- }
- /////////////////////////////////////////////////////////////////////////////
- // CAnimWobble Methods or virtual function implimentation
- void CAnimWobble::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_WobblePage);
- // Save the address of this animation procedure in the page
- pSheet->m_WobblePage.m_pAnimation = this;
- }
- void CAnimWobble::AnimateCamera(C3dCamera* pCamera, double dTime)
- {
- GLfloat fXIncrement; // Rotation incremental values
- GLfloat fYIncrement;
- GLfloat fZIncrement;
- // Ensure that we have a vaild pointer..
- if(!pCamera)
- return;
- // First pass through, just save the objects original
- // parameters
- if(m_bFirst)
- {
- m_bFirst = FALSE;
- SaveCameraAttributes(pCamera);
- return;
- }
- // Calculate the incremental index values
- if(m_dSpeedX)
- fXIncrement = (GLfloat)(m_fWobbleX/(m_dSpeedX/(dTime-m_dTimePrevious)));
- else
- fXIncrement = m_fWobbleX;
- if(m_dSpeedY)
- fYIncrement = (GLfloat)(m_fWobbleY/(m_dSpeedY/(dTime-m_dTimePrevious)));
- else
- fYIncrement = m_fWobbleY;
- if(m_dSpeedZ)
- fZIncrement = (GLfloat)(m_fWobbleZ/(m_dSpeedZ/(dTime-m_dTimePrevious)));
- else
- fZIncrement = m_fWobbleZ;
- // Wobble the shape
- if(m_fWobbleX)
- {
- if(m_bIncX)
- {
- pCamera->m_fRotation[X] += fXIncrement;
- if(pCamera->m_fRotation[X] > (m_fRotation[X]+m_fLimitX))
- {
- pCamera->m_fRotation[X] -= fXIncrement;
- m_bIncX = FALSE;
- }
- }
- else
- {
- pCamera->m_fRotation[X] -= fXIncrement;
- if(pCamera->m_fRotation[X] < (m_fRotation[X]-m_fLimitX))
- {
- pCamera->m_fRotation[X] += fXIncrement;
- m_bIncX = TRUE;
- }
- }
- }
- if(m_fWobbleY)
- {
- if(m_bIncY)
- {
- pCamera->m_fRotation[Y] += fYIncrement;
- if(pCamera->m_fRotation[Y] > (m_fRotation[Y]+m_fLimitY))
- {
- pCamera->m_fRotation[Y] -= fYIncrement;
- m_bIncY = FALSE;
- }
- }
- else
- {
- pCamera->m_fRotation[Y] -= fYIncrement;
- if(pCamera->m_fRotation[Y] < (m_fRotation[Y]-m_fLimitY))
- {
- pCamera->m_fRotation[Y] += fYIncrement;
- m_bIncY = TRUE;
- }
- }
- }
- if(m_fWobbleZ)
- {
- if(m_bIncZ)
- {
- pCamera->m_fRotation[Z] += fZIncrement;
- if(pCamera->m_fRotation[Z] > (m_fRotation[Z]+m_fLimitZ))
- {
- pCamera->m_fRotation[Z] -= fZIncrement;
- m_bIncZ = FALSE;
- }
- }
- else
- {
- pCamera->m_fRotation[Z] -= fZIncrement;
- if(pCamera->m_fRotation[Z] < (m_fRotation[Z]-m_fLimitZ))
- {
- pCamera->m_fRotation[Z] += fZIncrement;
- m_bIncZ = TRUE;
- }
- }
- }
- // Save the time of the last iteration
- m_dTimePrevious = dTime;
- }
- void CAnimWobble::AnimateObject(C3dObject* pObject, double dTime)
- {
- GLfloat fXIncrement; // Rotation incremental values
- GLfloat fYIncrement;
- GLfloat fZIncrement;
- // Ensure that we have a vaild pointer..
- if(!pObject)
- return;
- // First pass through, just save the objects original
- // parameters
- if(m_bFirst)
- {
- m_bFirst = FALSE;
- SaveObjectAttributes(pObject);
- return;
- }
- // Calculate the incremental index values
- if(m_dSpeedX)
- fXIncrement = (GLfloat)(m_fWobbleX/(m_dSpeedX/(dTime-m_dTimePrevious)));
- else
- fXIncrement = m_fWobbleX;
- if(m_dSpeedY)
- fYIncrement = (GLfloat)(m_fWobbleY/(m_dSpeedY/(dTime-m_dTimePrevious)));
- else
- fYIncrement = m_fWobbleY;
- if(m_dSpeedZ)
- fZIncrement = (GLfloat)(m_fWobbleZ/(m_dSpeedZ/(dTime-m_dTimePrevious)));
- else
- fZIncrement = m_fWobbleZ;
- // Wobble the shape
- if(m_fWobbleX)
- {
- if(m_bIncX)
- {
- pObject->m_fRotation[X] += fXIncrement;
- if(pObject->m_fRotation[X] > (m_fRotation[X]+m_fLimitX))
- {
- pObject->m_fRotation[X] -= fXIncrement;
- m_bIncX = FALSE;
- }
- }
- else
- {
- pObject->m_fRotation[X] -= fXIncrement;
- if(pObject->m_fRotation[X] < (m_fRotation[X]-m_fLimitX))
- {
- pObject->m_fRotation[X] += fXIncrement;
- m_bIncX = TRUE;
- }
- }
- }
- if(m_fWobbleY)
- {
- if(m_bIncY)
- {
- pObject->m_fRotation[Y] += fYIncrement;
- if(pObject->m_fRotation[Y] > (m_fRotation[Y]+m_fLimitY))
- {
- pObject->m_fRotation[Y] -= fYIncrement;
- m_bIncY = FALSE;
- }
- }
- else
- {
- pObject->m_fRotation[Y] -= fYIncrement;
- if(pObject->m_fRotation[Y] < (m_fRotation[Y]-m_fLimitY))
- {
- pObject->m_fRotation[Y] += fYIncrement;
- m_bIncY = TRUE;
- }
- }
- }
- if(m_fWobbleZ)
- {
- if(m_bIncZ)
- {
- pObject->m_fRotation[Z] += fZIncrement;
- if(pObject->m_fRotation[Z] > (m_fRotation[Z]+m_fLimitZ))
- {
- pObject->m_fRotation[Z] -= fZIncrement;
- m_bIncZ = FALSE;
- }
- }
- else
- {
- pObject->m_fRotation[Z] -= fZIncrement;
- if(pObject->m_fRotation[Z] < (m_fRotation[Z]-m_fLimitZ))
- {
- pObject->m_fRotation[Z] += fZIncrement;
- m_bIncZ = TRUE;
- }
- }
- }
- // Save the time of the last iteration
- m_dTimePrevious = dTime;
- }
- void CAnimWobble::Serialize(CArchive& ar, int iVersion)
- {
- CString szBuffer;
- CString szName;
- szBuffer.GetBuffer(256);
- szName.GetBuffer(128);
- if (ar.IsStoring())
- {
- // Save the CAnimation derived class header...
- szBuffer.Format("%sCAnimWobble {n", szIndent);
- ar.WriteString(szBuffer);
- // Save the this animation procedures' specific data...
- szBuffer.Format("%stWobbleX < %f >n", szIndent, m_fWobbleX);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stWobbleY < %f >n", szIndent, m_fWobbleY);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stWobbleZ < %f >n", szIndent, m_fWobbleZ);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stLimitX < %f >n", szIndent, m_fLimitX);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stLimitY < %f >n", szIndent, m_fLimitY);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stLimitZ < %f >n", szIndent, m_fLimitZ);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stSpinSpeedX < %f >n", szIndent, m_dSpeedX);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stSpinSpeedY < %f >n", szIndent, m_dSpeedY);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stSpinSpeedZ < %f >n", szIndent, m_dSpeedZ);
- ar.WriteString(szBuffer);
- // Save the base class data...
- CAnimation::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, "WobbleX < %f >n", &m_fWobbleX);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "WobbleY < %f >n", &m_fWobbleY);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "WobbleZ < %f >n", &m_fWobbleZ);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "LimitX < %f >n", &m_fLimitX);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "LimitY < %f >n", &m_fLimitY);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "LimitZ < %f >n", &m_fLimitZ);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "SpinSpeedX < %lf >n", (float*)&m_dSpeedX);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "SpinSpeedY < %lf >n", (float*)&m_dSpeedY);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "SpinSpeedZ < %lf >n", (float*)&m_dSpeedZ);
- // Read the base class data...
- CAnimation::Serialize(ar, iVersion);
- }
- }
- void CAnimWobble::Reset()
- {
- // TODO: Add any CAnimWobble reset code here..
- }
- /////////////////////////////////////////////////////////////////////////////
- // CAnimWobble function implimentation