3dObject.cpp
资源名称:gloop.zip [点击查看]
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:34k
源码类别:
OpenGL
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // 3dObject.cpp : Implementation file of the C3dObject class
- //
- // 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 "3dObjectDialog.h"
- #include "AnimationDialog.h"
- #include "MyFileIO.h"
- #include <stdio.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- //////////////////////////////////////////////////////////////////
- // C3dObject
- IMPLEMENT_DYNAMIC(C3dObject, CObject)
- /////////////////////////////////////////////////////////////////////////////
- // C3dObject construction
- C3dObject::C3dObject()
- {
- // Assign default values to member attributes..
- m_Color.SetColor4f(1.0f, 1.0f, 1.0f, 1.0f); // White
- SetOrigin(0.0f, 0.0f, 0.0f, 0.0f);
- SetRotation(0.0f, 0.0f, 0.0f);
- SetScale(1.0f, 1.0f, 1.0f);
- SetTranslation(0.0f, 0.0f, 0.0f);
- m_pParent = NULL;
- m_pMaterial = NULL;
- m_pTexture = NULL;
- m_pQuad = NULL;
- m_pPointArray = NULL;
- m_bInside = FALSE;
- m_bSolidColor = TRUE;
- m_iBMImage = -1;
- m_bBuildLists = TRUE;
- m_iDisplayLists = 0;
- // Assign an integer name to this object
- m_iName = iGlobalNamesList++;
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3DWorld Destructor
- C3dObject::~C3dObject()
- {
- if(m_pTexture)
- delete m_pTexture;
- // Delete the Objects attached to this Object
- m_ObjectList.DeleteAll();
- // Delete the DisplayLists if the Derived object
- // did not...
- if(m_iDisplayLists)
- {
- glDeleteLists(m_iDisplayLists, 1);
- m_iDisplayLists = 0;
- }
- // Delete all animation procedures attached to this object
- m_AnimList.DeleteAll();
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dObject virtual methods or functions
- void C3dObject::AddAttributePage(C3dWorld* pWorld, LPVOID pSht)
- {
- // Virtual prototype function only! Add specific
- // functionality to your derived object class!
- }
- void C3dObject::Animate(double time)
- {
- // Animate the shape based on its animation procedures
- CAnimation* pAnimation = NULL;
- // walk the list
- POSITION Pos = m_AnimList.GetHeadPosition();
- while (Pos) {
- pAnimation = m_AnimList.GetAt(Pos);
- pAnimation->AnimateObject(this, time);
- pAnimation = m_AnimList.GetNext(Pos);
- }
- // Animate all child objects of this object
- C3dObject* pObject = NULL;
- // walk the list
- Pos = m_ObjectList.GetHeadPosition();
- while (Pos) {
- pObject = m_ObjectList.GetAt(Pos);
- pObject->Animate(time);
- pObject = m_ObjectList.GetNext(Pos);
- }
- }
- void C3dObject::GetShapeBounds(C3dBoundingBox* pBox)
- {
- if(m_pPointArray)
- m_pPointArray->GetMinMax(pBox);
- }
- void C3dObject::DisplayObject(C3dWorld* pWorld, C3dCamera* pCamera)
- {
- // Save the current transformation matrix..
- glPushMatrix();
- // Transform our object
- TransformObject(pWorld);
- if(this == pWorld->m_pSelectedObj)
- {
- DisplaySelected(pWorld, FALSE);
- if(pWorld->m_bDisplayPoints)
- // Display all points in our object
- DisplayPoints(pWorld, pCamera);
- }
- // Save the name on the stack. This will be used to determine
- // hits from the selection buffer
- glLoadName(m_iName);
- // Setup this objects polygon color material
- // or texture attributes
- SetAttributes(pWorld, m_pQuad);
- if(m_bBuildLists) // Do we need to build the display lists?
- {
- if(!m_iDisplayLists)
- m_iDisplayLists = glGenLists(1);// Create the DisplayList(s)
- Build(pWorld, pCamera); // Build the shape
- m_bBuildLists = FALSE; // Set the flag
- }
- else
- glCallList(m_iDisplayLists); // Display the list
- // Display all child objects of this object
- DisplayChildObjects(pWorld, pCamera);
- // Restore the current transformation matrix..
- glPopMatrix();
- }
- int C3dObject::LoadBitMapImage(CImageList* pList)
- {
- // Virtual prototype function only! Add specific
- // functionality to your derived object class!
- return 0;
- }
- void C3dObject::Serialize(CArchive& ar, int iVersion)
- {
- CString szBuffer;
- CString szName;
- CString szTemp;
- CString szIndentSave;
- szBuffer.GetBuffer(256);
- szName.GetBuffer(128);
- if (ar.IsStoring())
- {
- // Save the Base Class Object Data...
- szBuffer.Format("%stName < %s >n", szIndent, m_szName);
- 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("%stOrigin < %f %f %f %f >n", szIndent, m_fOrigin[0], m_fOrigin[1], m_fOrigin[2], m_fOrigin[3]);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stRotation < %f %f %f >n", szIndent, m_fRotation[0], m_fRotation[1], m_fRotation[2]);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stScale < %f %f %f >n", szIndent, m_fScale[0], m_fScale[1], m_fScale[2]);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stTranslate < %f %f %f >n", szIndent, m_fTranslate[0], m_fTranslate[1], m_fTranslate[2]);
- ar.WriteString(szBuffer);
- // If there is a material attached to this object
- // save the material name
- if(m_pMaterial)
- szName = m_pMaterial->m_szName;
- else
- szName = "None";
- szBuffer.Format("%stMaterial Name < %s >n", szIndent, szName);
- ar.WriteString(szBuffer);
- // If there is a texture map attached to this object
- // save the file name and serialize the object
- if(m_pTexture)
- {
- szName = m_pTexture->m_szFileName;
- szBuffer.Format("%stTexture Map < %s >n", szIndent, szName);
- ar.WriteString(szBuffer);
- // Save the current indention and indent
- // all CAnimation class procedures
- szIndentSave = szIndent;
- szIndent += _T("tt"); // Add tabs
- // Serialize the texturemap
- m_pTexture->Serialize(ar, iVersion);
- // Restore indent position
- szIndent = szIndentSave; // restore indent
- }
- else
- {
- szName = "None";
- szBuffer.Format("%stTexture Map < %s >n", szIndent, szName);
- ar.WriteString(szBuffer);
- }
- // Save any CAnimation derived class procedure(s)
- m_AnimList.Serialize(ar, iVersion);
- // Finished saving object data //
- // Save all child objects of this object
- SaveChildObjects(ar, iVersion);
- }
- else
- {
- // Read the Base Class Object data...
- // Get the Name of this object. Note that the
- // name may contain spaces, so we extract the
- // string between the < > characters..
- ar.ReadString(szBuffer);
- // locate the position of the brackets
- int begin = szBuffer.Find("<");
- int end = szBuffer.Find(">");
- if(end > begin)
- {
- // extract the objectname from within the barckets
- m_szName = szBuffer.Mid(begin+1, (end-begin)-1);
- m_szName.TrimLeft(); // trim leading spaces
- m_szName.TrimRight(); // trim tailing spaces
- }
- 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]);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "Origin < %f %f %f %f >n", &m_fOrigin[0], &m_fOrigin[1], &m_fOrigin[2], &m_fOrigin[3]);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "Rotation < %f %f %f >n", &m_fRotation[0], &m_fRotation[1], &m_fRotation[2]);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "Scale < %f %f %f >n", &m_fScale[0], &m_fScale[1], &m_fScale[2]);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "Translate < %f %f %f >n", &m_fTranslate[0], &m_fTranslate[1], &m_fTranslate[2]);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "Material Name < %s >n", szName);
- if(szName.Compare("None") !=0) {
- // Object has a material name assigned...
- if(pMatlList) {
- // Get a pointer to the material name stored
- // in the file and save the address in our
- // member pointer.
- m_pMaterial = pMatlList->Find(szName);
- }
- }
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- szTemp = szBuffer.Left(11);
- if(szTemp.Compare("Texture Map") == 0)
- {
- // locate the position of the brackets
- int begin = szBuffer.Find("<");
- int end = szBuffer.Find(">");
- if(end > begin)
- {
- // extract the texture file name from within the barckets
- szName = szBuffer.Mid(begin+1, (end-begin)-1);
- szName.TrimLeft(); // trim leading spaces
- szName.TrimRight(); // trim tailing spaces
- if(szName.Compare("None") !=0)
- {
- AddTexture(szName.GetBuffer(128));
- if(iVersion > 102)
- {
- if(m_pTexture)
- m_pTexture->Serialize(ar, iVersion);
- else
- {
- // File could not be opened, so we must read ahead to
- // the next object attribute.
- // We must continue reading until we find the texture
- // derived class defined end marker!
- while(ar.ReadString(szBuffer))
- {
- szBuffer.TrimLeft();
- if(strcmp(szBuffer, "}n") == 0)
- break;
- if(strcmp(szBuffer, "}") == 0)
- break;
- }
- }
- }
- }
- }
- }
- if(iVersion > 101)
- {
- // Load any CAnimation derived class procedure(s)
- m_AnimList.Serialize(ar, iVersion);
- // Read all child objects...
- LoadChildObjects(ar, iVersion);
- }
- if(m_bSolidColor)
- // copy our current color to all point vertices
- SetColor4fv(&m_Color);
- }
- }
- void C3dObject::Build(C3dWorld* pWorld, C3dCamera* pCamera)
- {
- // Virtual prototype function only! Add specific
- // functionality to your derived object class!
- }
- void C3dObject::DisplayPoints(C3dWorld* pWorld, C3dCamera* pCamera)
- {
- // Display all points in our object
- if(m_pPointArray)
- {
- if(m_pPointArray->m_pPoints)
- m_pPointArray->Display(pWorld, pCamera, this, 2.0f, FALSE);
- }
- }
- C3dPoint* C3dObject::FindPoint(C3dCamera* pCamera, float x, float y, float z)
- {
- // Search the array for a C3dPoint which passes through the vector from the
- // camera to the xyz World coordinate.
- if(m_pPointArray)
- return m_pPointArray->Find(pCamera, this, x, y, z);
- else
- return NULL;
- }
- void C3dObject::SetObjPointOrigin(C3dPoint* pPoint, float x, float y, float z)
- {
- ASSERT(pPoint);
- pPoint->SetOrigin(x, y, z);
- }
- void C3dObject::GetObjPointOrigin(C3dPoint* pPoint, float *x, float *y, float *z)
- {
- ASSERT(pPoint);
- pPoint->GetOrigin(x, y, z);
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dObject static functions
- C3dObject* C3dObject::IsLightClass(CString* szString)
- {
- // Dynamically create an object given its run-time
- // class. Use the following code to dynamically
- // create an object by using the CreateObject
- // function of the CRuntimeClass. Note that on
- // failure, CreateObject returns NULL instead of
- // raising an exception:
- /*
- // this is not working properly.. replaced with manual
- // string comparisons..
- CString szClassName;
- szClassName.GetBuffer(128);
- szClassName.Format("%s", szString);
- szClassName.TrimRight();
- C3dObject object;
- CRuntimeClass* prt = object.GetRuntimeClass();
- if( strcmp( prt->m_lpszClassName, "C3dObject" ) == 0 );
- int temp = 0;
- // CRuntimeClass* pRuntimeClass = RUNTIME_CLASS( szClassName );
- // CObject* pObject = pRuntimeClass->CreateObject();
- // ASSERT( pObject->IsKindOf( RUNTIME_CLASS( szClassName ) ) );
- */
- if(szString->Compare("C3dObjectLight {") == 0) {
- C3dObjectLight* pLight = new C3dObjectLight;
- ASSERT(pLight);
- return pLight;
- }
- // The following class is left for compatability with versions
- // 1.03 and earlier.
- if(szString->Compare("C3dLightAmbient {") == 0) {
- C3dObjectLight* pLight = new C3dObjectLight;
- ASSERT(pLight);
- return pLight;
- }
- if(szString->Compare("C3dLightDirectional {") == 0) {
- C3dObjectLight* pLight = new C3dObjectLight;
- ASSERT(pLight);
- return pLight;
- }
- // light object does not match available types..
- return NULL;
- }
- C3dObject* C3dObject::IsShapeClass(CString* szString)
- {
- // If other C3dObject derived classes are created, either
- // add the appropriate string comparison/creation function or
- // figure out how to create based on Runtime class..
- //
- if(szString->Compare("C3dObjectCloud {") == 0) {
- C3dObjectCloud* pObject = new C3dObjectCloud;
- ASSERT(pObject);
- return pObject;
- }
- if(szString->Compare("C3dObjectCone {") == 0) {
- C3dObjectCone* pObject = new C3dObjectCone;
- ASSERT(pObject);
- return pObject;
- }
- if(szString->Compare("C3dObjectCSG {") == 0) {
- C3dObjectCSG* pObject = new C3dObjectCSG;
- ASSERT(pObject);
- return pObject;
- }
- if(szString->Compare("C3dObjectCube {") == 0) {
- C3dObjectCube* pObject = new C3dObjectCube;
- ASSERT(pObject);
- return pObject;
- }
- if(szString->Compare("C3dObjectCylinder {") == 0) {
- C3dObjectCylinder* pObject = new C3dObjectCylinder;
- ASSERT(pObject);
- return pObject;
- }
- if(szString->Compare("C3dObjectDisk {") == 0) {
- C3dObjectDisk* pObject = new C3dObjectDisk;
- ASSERT(pObject);
- return pObject;
- }
- if(szString->Compare("C3dObjectGrid {") == 0) {
- C3dObjectGrid* pObject = new C3dObjectGrid;
- ASSERT(pObject);
- return pObject;
- }
- if(szString->Compare("C3dObjectHSpline {") == 0) {
- C3dObjectHSpline* pObject = new C3dObjectHSpline;
- ASSERT(pObject);
- return pObject;
- }
- if(szString->Compare("C3dObjectLathe {") == 0) {
- C3dObjectLathe* pObject = new C3dObjectLathe;
- ASSERT(pObject);
- return pObject;
- }
- if(szString->Compare("C3dObjectNURB {") == 0) {
- C3dObjectNURB* pObject = new C3dObjectNURB;
- ASSERT(pObject);
- return pObject;
- }
- if(szString->Compare("C3dObjectPlane {") == 0) {
- C3dObjectPlane* pObject = new C3dObjectPlane;
- ASSERT(pObject);
- return pObject;
- }
- if(szString->Compare("C3dObjectSphere {") == 0) {
- C3dObjectSphere* pObject = new C3dObjectSphere;
- ASSERT(pObject);
- return pObject;
- }
- if(szString->Compare("C3dObjectTerrain {") == 0) {
- C3dObjectTerrain* pObject = new C3dObjectTerrain;
- ASSERT(pObject);
- return pObject;
- }
- if(szString->Compare("C3dObjectTorus {") == 0) {
- C3dObjectTorus* pObject = new C3dObjectTorus;
- ASSERT(pObject);
- return pObject;
- }
- if(szString->Compare("C3dObjectTriangle {") == 0) {
- C3dObjectTriangle* pObject = new C3dObjectTriangle;
- ASSERT(pObject);
- return pObject;
- }
- if(szString->Compare("C3dObjectTTF {") == 0) {
- C3dObjectTTF* pObject = new C3dObjectTTF;
- ASSERT(pObject);
- return pObject;
- }
- // Object shape does not match available types..
- return NULL;
- }
- C3dObject* C3dObject::LoadObject(CString szFileName, int* iType)
- {
- CFile f;
- CFileException fe;
- float fVersion;
- int iVersion;
- if( !f.Open( szFileName, CFile::modeRead, &fe ) ) {
- // Error! Display message to the user..
- CString buffer;
- buffer.Format("Could not open object file '%s'!n", szFileName);
- AfxMessageBox(buffer, MB_OK, 0);
- #ifdef _DEBUG
- afxDump << "Unable to open object file" << "n";
- #endif
- return NULL;
- }
- CArchive ar( &f, CArchive::load );
- // Load the object data...
- try {
- CString szBuffer;
- // Read the version number
- ar.ReadString(szBuffer);
- sscanf(szBuffer, "// Version: %fn", &fVersion);
- iVersion = Roundf(fVersion*100);
- if(iVersion < 101)
- iVersion = 101; // File had no version number, so assign default
- // Load all C3dObjects derived objects..
- while(ar.ReadString(szBuffer)) {
- C3dObject* pObject = NULL;
- szBuffer.TrimLeft(); // Remove leading white spaces
- pObject = C3dObject::IsShapeClass(&szBuffer);
- if(pObject)
- {
- *iType = SHAPE_OBJECT;
- pObject->Serialize(ar, iVersion);
- return pObject;
- }
- pObject = C3dObject::IsLightClass(&szBuffer);
- if(pObject)
- {
- *iType = LIGHT_OBJECT;
- pObject->Serialize(ar, iVersion);
- return pObject;
- }
- }
- }
- catch (CFileException exception) {
- // Catch any exceptions that may be thrown and
- // display for the user...
- DisplayFileIOException(&exception);
- }
- AfxMessageBox("Invalid File Format! 3D Object type not found.", MB_OK, NULL);
- return NULL;
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dObject implementation
- void C3dObject::AddAnimationPages(C3dWorld* pWorld, LPVOID pSht)
- {
- CAnimPropSheet* pSheet = (CAnimPropSheet*)pSht;
- ASSERT(pSheet);
- // Load all animation pages for this object
- CAnimation* pAnimation = NULL;
- // walk the list
- POSITION Pos = m_AnimList.GetHeadPosition();
- while (Pos) {
- pAnimation = m_AnimList.GetAt(Pos);
- pAnimation->AddAnimationPage(pSht, this, NULL, NULL);
- pAnimation = m_AnimList.GetNext(Pos);
- }
- }
- void C3dObject::ResetAnimation()
- {
- // Reset the objects animation variables
- CAnimation* pAnimation = NULL;
- // walk the list
- POSITION Pos = m_AnimList.GetHeadPosition();
- while (Pos) {
- pAnimation = m_AnimList.GetAt(Pos);
- pAnimation->RestoreObjectAttributes(this);
- pAnimation = m_AnimList.GetNext(Pos);
- }
- // Reset all child objects of this object
- C3dObject* pObject = NULL;
- // walk the list
- Pos = m_ObjectList.GetHeadPosition();
- while (Pos) {
- pObject = m_ObjectList.GetAt(Pos);
- pObject->ResetAnimation();
- pObject = m_ObjectList.GetNext(Pos);
- }
- }
- void C3dObject::SaveObject(CString szFileName)
- {
- CFile f;
- CFileException fe;
- if( !f.Open( szFileName, CFile::modeCreate | CFile::modeReadWrite, &fe))
- {
- // Error! Display message to the user..
- CString buffer;
- buffer.Format("Could not open object file '%s'!n", szFileName);
- AfxMessageBox(buffer, MB_OK, 0);
- #ifdef _DEBUG
- afxDump << "Unable to open file" << "n";
- #endif
- }
- // Construct a CArchive object and specify whether it will be used for
- // loading or storing objects.
- CArchive ar( &f, CArchive::store );
- // Store, or save the object data...
- try
- {
- // Save the file version information
- ar.WriteString(szFileVersion);
- // Save the file header information
- ar.WriteString(szObjectFileHeader);
- ar.WriteString(szIWDFileHeader);
- // Save the object data
- Serialize(ar, iFileVersion);
- }
- catch (CFileException exception)
- {
- // Catch any exceptions that may be thrown and
- // display for the user...
- DisplayFileIOException(&exception);
- }
- }
- void C3dObject::LoadChildObjects(CArchive& ar, int iVersion)
- {
- CString szBuffer;
- // Load all Child Objects...
- ar.ReadString(szBuffer);
- do {
- C3dObject* pChildObject = NULL;
- szBuffer.TrimLeft(); // Remove leading white spaces
- if(szBuffer.Compare("}") == 0) // end of object def
- break;
- pChildObject = C3dObject::IsShapeClass(&szBuffer);
- if(pChildObject) {
- pChildObject->Serialize(ar, iVersion);
- AddChildObject(pChildObject);
- }
- } while(ar.ReadString(szBuffer));
- }
- void C3dObject::SaveChildObjects(CArchive& ar, int iVersion)
- {
- CString szIndentSave;
- CString szBuffer;
- // Save the current indention and indent
- // child object
- szIndentSave = szIndent;
- szIndent += _T("t"); // Add tab
- // Store, or save all Child Object data...
- C3dObject* pChildObject = NULL;
- // walk the list
- POSITION objectPos = m_ObjectList.GetHeadPosition();
- while (objectPos) {
- pChildObject = m_ObjectList.GetAt(objectPos);
- pChildObject->Serialize(ar, iVersion);
- pChildObject = m_ObjectList.GetNext(objectPos);
- }
- // Restore indent position
- szIndent = szIndentSave; // restore indent
- }
- void C3dObject::TransformObject(C3dWorld* pWorld)
- {
- // Translate the objects origin to world coordinates
- glTranslated(m_fOrigin[0], m_fOrigin[1], m_fOrigin[2]);
- // Rotate the object
- glRotated(m_fRotation[0], 1.0, 0.0, 0.0);
- glRotated(m_fRotation[1], 0.0, 1.0, 0.0);
- glRotated(m_fRotation[2], 0.0, 0.0, 1.0);
- if(this == pWorld->m_pSelectedObj)
- {
- // Display the objects axis?
- if(pWorld->m_bDisplayAxis)
- // Display the axis before we scale the object
- // and translate the object from its origin.
- DisplayAxis(pWorld);
- }
- // Scale the object
- glScaled(m_fScale[0], m_fScale[1], m_fScale[2]);
- // Translate the object relative to its origin (origin offset)
- glTranslated(m_fTranslate[0], m_fTranslate[1], m_fTranslate[2]);
- }
- void C3dObject::GetTransformMatrix(Matx4x4 XformMatrix, BOOL bGetParent)
- {
- // Calculate the objects' transformation matrix
- PrepareMatrix(m_fOrigin[X], m_fOrigin[Y], m_fOrigin[Z],
- m_fScale[X], m_fScale[Y], m_fScale[Z],
- m_fRotation[X], m_fRotation[Y], m_fRotation[Z],
- m_fTranslate[X], m_fTranslate[Y], m_fTranslate[Z],
- XformMatrix);
- // If this object has a parent, get the parents matrix and multiply by
- // its own
- if(m_pParent && bGetParent)
- {
- Matx4x4 ParentMatrix, temp;
- m_pParent->GetTransformMatrix(ParentMatrix);
- MultiplyMatricies(ParentMatrix, XformMatrix, temp);
- MatrixCopy(temp, XformMatrix);
- }
- }
- void C3dObject::GetInvTransformMatrix(Matx4x4 XformMatrix, BOOL bGetParent)
- {
- // Calculate the objects' transformation matrix
- PrepareInvMatrix(m_fOrigin[X], m_fOrigin[Y], m_fOrigin[Z],
- m_fScale[X], m_fScale[Y], m_fScale[Z],
- m_fRotation[X], m_fRotation[Y], m_fRotation[Z],
- m_fTranslate[X], m_fTranslate[Y], m_fTranslate[Z],
- XformMatrix);
- // If this object has a parent, get the parents matrix and multiply by
- // its own
- if(m_pParent && bGetParent)
- {
- Matx4x4 ParentMatrix, temp;
- m_pParent->GetInvTransformMatrix(ParentMatrix);
- MultiplyMatricies(XformMatrix, ParentMatrix, temp);
- MatrixCopy(temp, XformMatrix);
- }
- }
- void C3dObject::GetRotationMatrix(Matx4x4 XformMatrix, BOOL bGetParent)
- {
- // Calculate the objects' rotation matrix
- PrepareMatrix(0.0f, 0.0f, 0.0f, // Origin
- 1.0f, 1.0f, 1.0f, // Scale
- m_fRotation[X], m_fRotation[Y], m_fRotation[Z],
- 0.0f, 0.0f, 0.0f, // Translation
- XformMatrix);
- // If this object has a parent, get the parents matrix and multiply by
- // its own
- if(m_pParent && bGetParent)
- {
- Matx4x4 ParentMatrix, temp;
- m_pParent->GetRotationMatrix(ParentMatrix);
- MultiplyMatricies(ParentMatrix, XformMatrix, temp);
- MatrixCopy(temp, XformMatrix);
- }
- }
- void C3dObject::GetInvRotationMatrix(Matx4x4 XformMatrix, BOOL bGetParent)
- {
- // Calculate the objects' transformation matrix
- PrepareInvMatrix(0.0f, 0.0f, 0.0f, // Origin
- 1.0f, 1.0f, 1.0f, // Scale
- m_fRotation[X], m_fRotation[Y], m_fRotation[Z],
- 0.0f, 0.0f, 0.0f, // Translation
- XformMatrix);
- // If this object has a parent, get the parents matrix and multiply by
- // its own
- if(m_pParent && bGetParent)
- {
- Matx4x4 ParentMatrix, temp;
- m_pParent->GetInvRotationMatrix(ParentMatrix);
- MultiplyMatricies(XformMatrix, ParentMatrix, temp);
- MatrixCopy(temp, XformMatrix);
- }
- }
- void C3dObject::GetBounds(C3dBoundingBox* pBox, BOOL bWorldCoordinates)
- {
- Matx4x4 ObjXformMatrix;
- // Get the non-transformed shape bounding box coordinates
- GetShapeBounds(pBox);
- // Get the bounds of all child objects
- C3dObject* pChild = NULL;
- // walk the list
- POSITION childPos = m_ObjectList.GetHeadPosition();
- while (childPos) {
- Matx4x4 ChildXformMatrix;
- C3dBoundingBox childBoundingBox;
- // Get a pointer to the child object
- pChild = m_ObjectList.GetAt(childPos);
- // Get the non-transformed child object bounding rectangle
- pChild->GetBounds(&childBoundingBox, FALSE);
- // Get the transformation matrix of this child
- pChild->GetTransformMatrix(ChildXformMatrix);
- // Transform the child bounding box by its transformation
- // matrix to convert the bounding box to this childs
- // coordinate space.
- childBoundingBox.TransformBounds(ChildXformMatrix);
- // Get the transformation matrix of this child objects' parent
- GetInvTransformMatrix(ObjXformMatrix);
- // Transform the child bounding box by its parents transformation
- // matrix to convert the bounding box to Parent coordinate space.
- childBoundingBox.TransformBounds(ObjXformMatrix);
- // Set the Min/Max bounding coordinates between the
- // parent and child objects.
- pBox->SetMinMax(&childBoundingBox);
- // Get the next child object
- pChild = m_ObjectList.GetNext(childPos);
- }
- if(bWorldCoordinates)
- {
- // Get the object's transformation matrix
- GetTransformMatrix(ObjXformMatrix);
- // Transform the objects bounds by its transformation
- // matrix to convert the bounding box to world coordinates.
- pBox->TransformBounds(ObjXformMatrix);
- }
- }
- void C3dObject::AddChildObject(C3dObject* pObject)
- {
- // Add the object to 'this' objects' list
- ASSERT(pObject);
- pObject->m_pParent = (C3dObject*)this;
- this->m_ObjectList.Append(pObject);
- }
- void C3dObject::SetColor4fv(C3dColor* pColor)
- {
- // Set the objects color
- m_Color.SetColor4fv(pColor);
- if(m_pPointArray)
- {
- for(int i=0; i<m_pPointArray->m_iNumPoints; i++)
- m_pPointArray->m_pPoints[i].m_Color.SetColor4fv(pColor);
- }
- }
- void C3dObject::SetColor4f(GLfloat r, GLfloat g, GLfloat b, GLfloat a)
- {
- // Set the objects color
- m_Color.SetColor4f(r, g, b, a);
- if(m_pPointArray)
- {
- for(int i=0; i<m_pPointArray->m_iNumPoints; i++)
- m_pPointArray->m_pPoints[i].m_Color.SetColor4f(r, g, b, a);
- }
- }
- void C3dObject::SetOrigin(GLfloat x, GLfloat y, GLfloat z)
- {
- // Set the origin to the supplied values..
- m_fOrigin[0] = x;
- m_fOrigin[1] = y;
- m_fOrigin[2] = z;
- }
- void C3dObject::SetOrigin(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
- {
- // Set the origin to the supplied values..
- m_fOrigin[0] = x;
- m_fOrigin[1] = y;
- m_fOrigin[2] = z;
- m_fOrigin[3] = w;
- }
- void C3dObject::SetScale(GLfloat x, GLfloat y, GLfloat z)
- {
- // Prevent Scaling by Zero!
- if(x==0)
- x=(GLfloat)SMALL_NUMBER;
- if(y==0)
- y=(GLfloat)SMALL_NUMBER;
- if(z==0)
- z=(GLfloat)SMALL_NUMBER;
- m_fScale[0] = x;
- m_fScale[1] = y;
- m_fScale[2] = z;
- }
- void C3dObject::SetRotation(GLfloat x, GLfloat y, GLfloat z)
- {
- m_fRotation[0] = x;
- m_fRotation[1] = y;
- m_fRotation[2] = z;
- }
- void C3dObject::SetTranslation(GLfloat x, GLfloat y, GLfloat z)
- {
- m_fTranslate[0] = x;
- m_fTranslate[1] = y;
- m_fTranslate[2] = z;
- }
- void C3dObject::RemoveDibImage()
- {
- if(m_pTexture) {
- delete m_pTexture;
- m_pTexture = NULL;
- }
- else
- AfxMessageBox("No Bitmap to Remove..", MB_OK, 0);
- }
- void C3dObject::SetTexture(CTexture* pTexture)
- {
- // Set our objects texture map pointer
- if(m_pTexture)
- delete m_pTexture;
- m_pTexture = pTexture;
- }
- void C3dObject::SetMaterial(C3dMaterial* pMaterial)
- {
- // Set our objects material pointer
- m_pMaterial = pMaterial;
- }
- int C3dObject::EditAnimation(CWnd* pParentWnd, C3dWorld* pWorld)
- {
- char szName[80];
- sprintf(szName, "Edit Animation Attributes of object '%s'", m_szName);
- CAnimPropSheet* pAnimPropSheet = new CAnimPropSheet(szName, pParentWnd, this, NULL, pWorld);
- // Create and display the modal dialog box
- return( pAnimPropSheet->DoModal() );
- }
- int C3dObject::EditAttributes(CWnd* pParentWnd, C3dWorld* pWorld)
- {
- char szName[80];
- sprintf(szName, "Edit Attributes of object '%s'", m_szName);
- C3dObjectPropSheet* pObjectPropSheet = new C3dObjectPropSheet(szName, pParentWnd, this, pWorld);
- // Create and display the modal dialog box
- return( pObjectPropSheet->DoModal() );
- }
- void C3dObject::SetAttributes(C3dWorld* pWorld, void *pData)
- {
- GLUquadricObj* pQuad = (GLUquadricObj*) pData;
- if(pWorld)
- {
- if(pWorld->m_iRenderMode == RENDER_WIREFRAME)
- {
- // Reset color parameters to default values
- glDisable(GL_TEXTURE_2D);
- glDisable(GL_BLEND);
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- glColor4fv(m_Color.m_fColor);
- glLineWidth(0.05f);
- if(pQuad)
- {
- gluQuadricDrawStyle(pQuad, GLU_LINE);
- gluQuadricTexture(pQuad, FALSE);
- }
- return;
- }
- }
- if(m_bInside)
- glFrontFace(GL_CW);
- else
- glFrontFace(GL_CCW);
- // Set the material properties
- if(m_pMaterial)
- // Set our objects material properties
- m_pMaterial->SetMaterialAttrib();
- else
- // Reset the material properties to default
- // settings and track the current color
- C3dMaterial::ResetMaterialAttrib();
- if(m_pTexture)
- {
- if(m_pTexture->m_iEnvmode == GL_BLEND)
- {
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- }
- else
- // Disable blend function
- glDisable(GL_BLEND);
- if(pQuad)
- {
- gluQuadricDrawStyle(pQuad, GLU_FILL);
- gluQuadricTexture(pQuad, TRUE);
- }
- if(m_pTexture->m_pBits)
- {
- // Apply the Dib Image to our shape..
- m_pTexture->SetTexture();
- return;
- }
- }
- else
- {
- // Reset color parameters to default values
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- glDisable(GL_TEXTURE_2D);
- glColor4fv(m_Color.m_fColor);
- if(pQuad)
- {
- gluQuadricDrawStyle(pQuad, GLU_FILL);
- gluQuadricTexture(pQuad, FALSE);
- }
- if(m_Color.m_fColor[3] < 1.0f)
- {
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- }
- }
- }
- void C3dObject::GetOrigin(GLfloat *x, GLfloat *y, GLfloat *z)
- {
- // Get the origin
- *x = m_fOrigin[0];
- *y = m_fOrigin[1];
- *z = m_fOrigin[2];
- }
- void C3dObject::GetOrigin(GLfloat *x, GLfloat *y, GLfloat *z, GLfloat *w)
- {
- // Get the origin
- *x = m_fOrigin[0];
- *y = m_fOrigin[1];
- *z = m_fOrigin[2];
- *w = m_fOrigin[3];
- }
- void C3dObject::GetRotation(GLfloat *x, GLfloat *y, GLfloat *z)
- {
- // Get the lights rotation
- *x = m_fRotation[0];
- *y = m_fRotation[1];
- *z = m_fRotation[2];
- }
- void C3dObject::GetScale(GLfloat *x, GLfloat *y, GLfloat *z)
- {
- // Get the lights rotation
- *x = m_fScale[0];
- *y = m_fScale[1];
- *z = m_fScale[2];
- }
- void C3dObject::GetTranslation(GLfloat *x, GLfloat *y, GLfloat *z)
- {
- // Get the lights translation
- *x = m_fTranslate[0];
- *y = m_fTranslate[1];
- *z = m_fTranslate[2];
- }
- C3dObject* C3dObject::IsName(GLuint i, BOOL bSelParent)
- {
- if(i == m_iName)
- return this;
- // Name is not this object, is it one of its children?
- C3dObject* pSelectedObject; // C3dObject pointer
- // Display all objects attached to this world..
- C3dObject* pObject = NULL;
- // walk the list
- POSITION objectPos = m_ObjectList.GetHeadPosition();
- while (objectPos)
- {
- pObject = m_ObjectList.GetAt(objectPos);
- pSelectedObject = pObject->IsName(i, bSelParent);
- if(pSelectedObject)
- {
- if(bSelParent)
- return this;
- else
- return pSelectedObject;
- }
- pObject = m_ObjectList.GetNext(objectPos);
- }
- return NULL;
- }
- void C3dObject::DisplayChildObjects(C3dWorld* pWorld, C3dCamera* pCamera)
- {
- // Display all child objects attached to this object
- C3dObject* pObject = NULL;
- // walk the list
- POSITION objectPos = m_ObjectList.GetHeadPosition();
- while (objectPos) {
- pObject = m_ObjectList.GetAt(objectPos);
- pObject->DisplayObject(pWorld, pCamera);
- pObject = m_ObjectList.GetNext(objectPos);
- }
- }
- void C3dObject::DisplayAxis(C3dWorld* pWorld)
- {
- // Display the X, Y, Z Axis relative to this objects
- // origin
- pWorld->m_pAxis->Display( 3.0f, // Axis length
- 0.05f, // Axis diameter
- 0.5f, // Arrow length
- 0.15f,// Arrow diameter
- 6, // Number of slices around dia.
- 2, // Number of Stacks about length
- NULL, // X-Axis Color (Use default..)
- NULL, // Y-Axis Color
- NULL, // Z-Axis Color
- TRUE); // Display as Solid
- }
- void C3dObject::DisplaySelected(C3dWorld* pWorld, BOOL bWorldCoordinates)
- {
- // Display a Bounding Box around the object
- C3dBoundingBox BoundingBox;
- GetBounds(&BoundingBox, bWorldCoordinates);
- BoundingBox.Display(0.1f, // Rectangle offset
- 0.3f, // box line width
- 10, // line slices
- 2, // line segments
- NULL, // box color (use Default)
- TRUE); // display solid?
- }
- CAnimKeyFrame* C3dObject::GetKeyFrameList(int *iNumKeys)
- {
- // Do we have a KeyFrame Procedure attached to this object?
- CAnimKeyFrame* pAnimKey = (CAnimKeyFrame*) m_AnimList.Find("KeyFrame");
- if(pAnimKey)
- {
- // Get the number of Keyframes in the list
- *iNumKeys = pAnimKey->m_pKeyFrameList->GetNumKeys();
- return pAnimKey;
- }
- *iNumKeys = 0;
- return NULL;
- }
- BOOL C3dObject::AddTexture(LPSTR szFileName)
- {
- if(m_pTexture)
- // If we already have a texture, delete it
- DeleteTexture();
- // Create a TextureMap of the appropriate type
- CTexture::CreateTexture(szFileName, the3dEngine.m_hPalette, &m_pTexture);
- if(m_pTexture)
- {
- // Is the TextureMap an CTextureAviMovie?
- if( m_pTexture->IsKindOf(RUNTIME_CLASS(CTextureAviMovie)) )
- {
- // Create an AVI animation and add to the list
- CAnimAVI* pAnim = new CAnimAVI;
- ASSERT(pAnim);
- m_AnimList.Append(pAnim);
- // Cross link the two objects so that they are 'aware' of
- // each other..
- // Save the CTextureAviMovie pointer
- pAnim->m_pAviMovie = (CTextureAviMovie*)m_pTexture;
- // Save the CAnimAVI pointer
- CTextureAviMovie* pAviVideo = (CTextureAviMovie*)m_pTexture;
- pAviVideo->m_pAnimAVI = pAnim;
- }
- return TRUE;
- }
- else
- return FALSE;
- }
- void C3dObject::DeleteTexture()
- {
- if(m_pTexture)
- {
- if( m_pTexture->IsKindOf(RUNTIME_CLASS(CTextureAviMovie)) )
- {
- // TextureMap is of class type CTextureAviMovie, so find the animation
- // procedure in the list, remove from list and delete it..
- CAnimation* pAnimation = m_AnimList.Find(SZ_ANIMATE_AVI);
- if(pAnimation)
- {
- m_AnimList.Remove(pAnimation);
- delete pAnimation;
- }
- }
- // Now that we have removed any associated object members,
- // delete the texturemap
- delete m_pTexture;
- m_pTexture = NULL;
- }
- }
- void C3dObject::Rebuild()
- {
- // This function forces all objects and their children to rebuild
- // their display lists.
- // Force a rebuild of this objects display list
- m_bBuildLists = TRUE;
- C3dObject* pChild = NULL;
- // walk the list
- POSITION childPos = m_ObjectList.GetHeadPosition();
- while (childPos)
- {
- pChild = m_ObjectList.GetAt(childPos);
- pChild->Rebuild();
- pChild = m_ObjectList.GetNext(childPos);
- }
- }