Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
KeyFrame.cpp
Package: gloop.zip [view]
Upload User: shxiangxiu
Upload Date: 2007-01-03
Package Size: 1101k
Code Size: 15k
Category:
OpenGL program
Development Platform:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // KeyFrame.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"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CKeyFrame
- IMPLEMENT_DYNAMIC(CKeyFrame, CAnimKeyFrame)
- /////////////////////////////////////////////////////////////////////////////
- // CKeyFrame construction
- CKeyFrame::CKeyFrame(double dTime, C3dObject* pObject)
- {
- if(!pObject)
- return;
- // Set the attributes to default values..
- m_pKeyFramePrevious = NULL;
- m_dTime = dTime;
- m_Color.SetColor4fv(&pObject->m_Color);
- VecCopy4f(pObject->m_fOrigin, m_fOrigin);
- VecCopy3f(pObject->m_fRotation, m_fRotation);
- VecCopy3f(pObject->m_fScale, m_fScale);
- VecCopy3f(pObject->m_fTranslate, m_fTranslate);
- if(pObject->m_iType == LIGHT_OBJECT)
- {
- C3dObjectLight* pLight = (C3dObjectLight*)pObject;
- m_ColorAmb.SetColor4fv(&pLight->m_ColorAmbient);
- m_ColorDif.SetColor4fv(&pLight->m_ColorDiffuse);
- m_ColorSpc.SetColor4fv(&pLight->m_ColorSpecular);
- m_fSpotAngle = pLight->m_fSpotAngle;
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CKeyFrame Destructor
- CKeyFrame::~CKeyFrame()
- {
- }
- /////////////////////////////////////////////////////////////////////////////
- // CKeyFrame Methods or virtual function implimentation
- void CKeyFrame::AnimateCamera(C3dCamera* pCamera, double dTime)
- {
- }
- void CKeyFrame::AnimateObject(C3dObject* pObject, double dTime)
- {
- // KeyFrame Animation procedure. Calculate the incremental values for x,y,z
- // and color RGBA. Note that we update ONLY the object attributes which
- // change from one KeyFrame to the next. This allows other animation procedures
- // a chance to modify the objects attributes..
- GLfloat fKeyIncrement[4];
- // Ensure that we have a vaild pointer..
- if(!pObject)
- return;
- double dTimeBetweenFrames = m_dTime-m_pKeyFramePrevious->m_dTime;
- double dTimeSinceLastFrame = dTime-m_pKeyFramePrevious->m_dTime;
- double dPercentThisFrame = dTimeSinceLastFrame/dTimeBetweenFrames;
- // Calculate the Origin incremental index values
- fKeyIncrement[X] = (GLfloat)((m_fOrigin[X] - m_pKeyFramePrevious->m_fOrigin[X]) * dPercentThisFrame);
- fKeyIncrement[Y] = (GLfloat)((m_fOrigin[Y] - m_pKeyFramePrevious->m_fOrigin[Y]) * dPercentThisFrame);
- fKeyIncrement[Z] = (GLfloat)((m_fOrigin[Z] - m_pKeyFramePrevious->m_fOrigin[Z]) * dPercentThisFrame);
- if(fKeyIncrement[X])
- pObject->m_fOrigin[X] = m_pKeyFramePrevious->m_fOrigin[X] + fKeyIncrement[X];
- if(fKeyIncrement[Y])
- pObject->m_fOrigin[Y] = m_pKeyFramePrevious->m_fOrigin[Y] + fKeyIncrement[Y];
- if(fKeyIncrement[Z])
- pObject->m_fOrigin[Z] = m_pKeyFramePrevious->m_fOrigin[Z] + fKeyIncrement[Z];
- // Calculate the Rotation incremental index values
- fKeyIncrement[X] = (GLfloat)((m_fRotation[X] - m_pKeyFramePrevious->m_fRotation[X]) * dPercentThisFrame);
- fKeyIncrement[Y] = (GLfloat)((m_fRotation[Y] - m_pKeyFramePrevious->m_fRotation[Y]) * dPercentThisFrame);
- fKeyIncrement[Z] = (GLfloat)((m_fRotation[Z] - m_pKeyFramePrevious->m_fRotation[Z]) * dPercentThisFrame);
- if(fKeyIncrement[X])
- pObject->m_fRotation[X] = m_pKeyFramePrevious->m_fRotation[X] + fKeyIncrement[X];
- if(fKeyIncrement[Y])
- pObject->m_fRotation[Y] = m_pKeyFramePrevious->m_fRotation[Y] + fKeyIncrement[Y];
- if(fKeyIncrement[Z])
- pObject->m_fRotation[Z] = m_pKeyFramePrevious->m_fRotation[Z] + fKeyIncrement[Z];
- // Calculate the Scale incremental index values
- fKeyIncrement[X] = (GLfloat)((m_fScale[X] - m_pKeyFramePrevious->m_fScale[X]) * dPercentThisFrame);
- fKeyIncrement[Y] = (GLfloat)((m_fScale[Y] - m_pKeyFramePrevious->m_fScale[Y]) * dPercentThisFrame);
- fKeyIncrement[Z] = (GLfloat)((m_fScale[Z] - m_pKeyFramePrevious->m_fScale[Z]) * dPercentThisFrame);
- if(fKeyIncrement[X])
- pObject->m_fScale[X] = m_pKeyFramePrevious->m_fScale[X] + fKeyIncrement[X];
- if(fKeyIncrement[Y])
- pObject->m_fScale[Y] = m_pKeyFramePrevious->m_fScale[Y] + fKeyIncrement[Y];
- if(fKeyIncrement[Z])
- pObject->m_fScale[Z] = m_pKeyFramePrevious->m_fScale[Z] + fKeyIncrement[Z];
- // Calculate the Translation incremental index values
- fKeyIncrement[X] = (GLfloat)((m_fTranslate[X] - m_pKeyFramePrevious->m_fTranslate[X]) * dPercentThisFrame);
- fKeyIncrement[Y] = (GLfloat)((m_fTranslate[Y] - m_pKeyFramePrevious->m_fTranslate[Y]) * dPercentThisFrame);
- fKeyIncrement[Z] = (GLfloat)((m_fTranslate[Z] - m_pKeyFramePrevious->m_fTranslate[Z]) * dPercentThisFrame);
- if(fKeyIncrement[X])
- pObject->m_fTranslate[X] = m_pKeyFramePrevious->m_fTranslate[X] + fKeyIncrement[X];
- if(fKeyIncrement[Y])
- pObject->m_fTranslate[Y] = m_pKeyFramePrevious->m_fTranslate[Y] + fKeyIncrement[Y];
- if(fKeyIncrement[Z])
- pObject->m_fTranslate[Z] = m_pKeyFramePrevious->m_fTranslate[Z] + fKeyIncrement[Z];
- // Calculate the Color RGBA incremental index values
- fKeyIncrement[0] = (GLfloat)((m_Color.m_fColor[0] - m_pKeyFramePrevious->m_Color.m_fColor[0]) * dPercentThisFrame);
- fKeyIncrement[1] = (GLfloat)((m_Color.m_fColor[1] - m_pKeyFramePrevious->m_Color.m_fColor[1]) * dPercentThisFrame);
- fKeyIncrement[2] = (GLfloat)((m_Color.m_fColor[2] - m_pKeyFramePrevious->m_Color.m_fColor[2]) * dPercentThisFrame);
- fKeyIncrement[3] = (GLfloat)((m_Color.m_fColor[3] - m_pKeyFramePrevious->m_Color.m_fColor[3]) * dPercentThisFrame);
- if(fKeyIncrement[0])
- pObject->m_Color.m_fColor[0] = m_pKeyFramePrevious->m_Color.m_fColor[0] + fKeyIncrement[0];
- if(fKeyIncrement[1])
- pObject->m_Color.m_fColor[1] = m_pKeyFramePrevious->m_Color.m_fColor[1] + fKeyIncrement[1];
- if(fKeyIncrement[2])
- pObject->m_Color.m_fColor[2] = m_pKeyFramePrevious->m_Color.m_fColor[2] + fKeyIncrement[2];
- if(fKeyIncrement[3])
- pObject->m_Color.m_fColor[3] = m_pKeyFramePrevious->m_Color.m_fColor[3] + fKeyIncrement[3];
- if(pObject->m_iType == LIGHT_OBJECT)
- {
- C3dObjectLight* pLight = (C3dObjectLight*)pObject;
- // Calculate the Ambient Color RGBA incremental index values
- fKeyIncrement[0] = (GLfloat)((m_ColorAmb.m_fColor[0] - m_pKeyFramePrevious->m_ColorAmb.m_fColor[0]) * dPercentThisFrame);
- fKeyIncrement[1] = (GLfloat)((m_ColorAmb.m_fColor[1] - m_pKeyFramePrevious->m_ColorAmb.m_fColor[1]) * dPercentThisFrame);
- fKeyIncrement[2] = (GLfloat)((m_ColorAmb.m_fColor[2] - m_pKeyFramePrevious->m_ColorAmb.m_fColor[2]) * dPercentThisFrame);
- fKeyIncrement[3] = (GLfloat)((m_ColorAmb.m_fColor[3] - m_pKeyFramePrevious->m_ColorAmb.m_fColor[3]) * dPercentThisFrame);
- if(fKeyIncrement[0])
- pLight->m_ColorAmbient.m_fColor[0] = m_pKeyFramePrevious->m_ColorAmb.m_fColor[0] + fKeyIncrement[0];
- if(fKeyIncrement[1])
- pLight->m_ColorAmbient.m_fColor[1] = m_pKeyFramePrevious->m_ColorAmb.m_fColor[1] + fKeyIncrement[1];
- if(fKeyIncrement[2])
- pLight->m_ColorAmbient.m_fColor[2] = m_pKeyFramePrevious->m_ColorAmb.m_fColor[2] + fKeyIncrement[2];
- if(fKeyIncrement[3])
- pLight->m_ColorAmbient.m_fColor[3] = m_pKeyFramePrevious->m_ColorAmb.m_fColor[3] + fKeyIncrement[3];
- // Calculate the Diffuse Color RGBA incremental index values
- fKeyIncrement[0] = (GLfloat)((m_ColorDif.m_fColor[0] - m_pKeyFramePrevious->m_ColorDif.m_fColor[0]) * dPercentThisFrame);
- fKeyIncrement[1] = (GLfloat)((m_ColorDif.m_fColor[1] - m_pKeyFramePrevious->m_ColorDif.m_fColor[1]) * dPercentThisFrame);
- fKeyIncrement[2] = (GLfloat)((m_ColorDif.m_fColor[2] - m_pKeyFramePrevious->m_ColorDif.m_fColor[2]) * dPercentThisFrame);
- fKeyIncrement[3] = (GLfloat)((m_ColorDif.m_fColor[3] - m_pKeyFramePrevious->m_ColorDif.m_fColor[3]) * dPercentThisFrame);
- if(fKeyIncrement[0])
- pLight->m_ColorDiffuse.m_fColor[0] = m_pKeyFramePrevious->m_ColorDif.m_fColor[0] + fKeyIncrement[0];
- if(fKeyIncrement[1])
- pLight->m_ColorDiffuse.m_fColor[1] = m_pKeyFramePrevious->m_ColorDif.m_fColor[1] + fKeyIncrement[1];
- if(fKeyIncrement[2])
- pLight->m_ColorDiffuse.m_fColor[2] = m_pKeyFramePrevious->m_ColorDif.m_fColor[2] + fKeyIncrement[2];
- if(fKeyIncrement[3])
- pLight->m_ColorDiffuse.m_fColor[3] = m_pKeyFramePrevious->m_ColorDif.m_fColor[3] + fKeyIncrement[3];
- // Calculate the Specular Color RGBA incremental index values
- fKeyIncrement[0] = (GLfloat)((m_ColorSpc.m_fColor[0] - m_pKeyFramePrevious->m_ColorSpc.m_fColor[0]) * dPercentThisFrame);
- fKeyIncrement[1] = (GLfloat)((m_ColorSpc.m_fColor[1] - m_pKeyFramePrevious->m_ColorSpc.m_fColor[1]) * dPercentThisFrame);
- fKeyIncrement[2] = (GLfloat)((m_ColorSpc.m_fColor[2] - m_pKeyFramePrevious->m_ColorSpc.m_fColor[2]) * dPercentThisFrame);
- fKeyIncrement[3] = (GLfloat)((m_ColorSpc.m_fColor[3] - m_pKeyFramePrevious->m_ColorSpc.m_fColor[3]) * dPercentThisFrame);
- if(fKeyIncrement[0])
- pLight->m_ColorSpecular.m_fColor[0] = m_pKeyFramePrevious->m_ColorSpc.m_fColor[0] + fKeyIncrement[0];
- if(fKeyIncrement[1])
- pLight->m_ColorSpecular.m_fColor[1] = m_pKeyFramePrevious->m_ColorSpc.m_fColor[1] + fKeyIncrement[1];
- if(fKeyIncrement[2])
- pLight->m_ColorSpecular.m_fColor[2] = m_pKeyFramePrevious->m_ColorSpc.m_fColor[2] + fKeyIncrement[2];
- if(fKeyIncrement[3])
- pLight->m_ColorSpecular.m_fColor[3] = m_pKeyFramePrevious->m_ColorSpc.m_fColor[3] + fKeyIncrement[3];
- // Calculate the Spot angle incremental value
- fKeyIncrement[0] = (GLfloat)((m_fSpotAngle - m_pKeyFramePrevious->m_fSpotAngle) * dPercentThisFrame);
- if(fKeyIncrement[0])
- pLight->m_fSpotAngle = m_pKeyFramePrevious->m_fSpotAngle + fKeyIncrement[0];
- }
- }
- void CKeyFrame::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("n%sCKeyFrame {n", szIndent);
- ar.WriteString(szBuffer);
- // Save the this KeyFrames' specific data...
- szBuffer.Format("%stTime Ref < %lf >n", szIndent, m_dTime);
- ar.WriteString(szBuffer);
- 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]);
- ar.WriteString(szBuffer);
- 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]);
- ar.WriteString(szBuffer);
- 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]);
- ar.WriteString(szBuffer);
- 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]);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stOrigin < %f %f %f %f >n", szIndent, m_fOrigin[X], m_fOrigin[Y], m_fOrigin[Z], m_fOrigin[W]);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stRotation < %f %f %f >n", szIndent, m_fRotation[X], m_fRotation[Y], m_fRotation[Z]);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stScale < %f %f %f >n", szIndent, m_fScale[X], m_fScale[Y], m_fScale[Z]);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stTranslate < %f %f %f >n", szIndent, m_fTranslate[X], m_fTranslate[Y], m_fTranslate[Z]);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stSpotAngle < %f >n", szIndent, m_fSpotAngle);
- ar.WriteString(szBuffer);
- 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, "Time Ref < %lf >n", &m_dTime);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- 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]);
- if(iVersion > 120)
- {
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- 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]);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- 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]);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- 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]);
- }
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "Origin < %f %f %f %f >n", &m_fOrigin[X], &m_fOrigin[Y], &m_fOrigin[Z], &m_fOrigin[W]);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "Rotation < %f %f %f >n", &m_fRotation[X], &m_fRotation[Y], &m_fRotation[Z]);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "Scale < %f %f %f >n", &m_fScale[X], &m_fScale[Y], &m_fScale[Z]);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "Translate < %f %f %f >n", &m_fTranslate[X], &m_fTranslate[Y], &m_fTranslate[Z]);
- if(iVersion > 120)
- {
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "SpotAngle < %f >n", &m_fSpotAngle);
- }
- // Must remove the CKeyFrame end marker!
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "}n");
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CKeyFrame function implimentation
- void CKeyFrame::SetObjectAttributes(C3dObject* pObject)
- {
- if(!pObject)
- return;
- // Set the objects attributes to this this keyframe
- pObject->m_Color.SetColor4fv(&m_Color);
- VecCopy4f(m_fOrigin, pObject->m_fOrigin);
- VecCopy3f(m_fRotation, pObject->m_fRotation);
- VecCopy3f(m_fScale, pObject->m_fScale);
- VecCopy3f(m_fTranslate, pObject->m_fTranslate);
- if(pObject->m_iType == LIGHT_OBJECT)
- {
- C3dObjectLight* pLight = (C3dObjectLight*)pObject;
- pLight->m_ColorAmbient.SetColor4fv(&m_ColorAmb);
- pLight->m_ColorDiffuse.SetColor4fv(&m_ColorDif);
- pLight->m_ColorSpecular.SetColor4fv(&m_ColorSpc);
- pLight->m_fSpotAngle = m_fSpotAngle;
- }
- }