3dObjectTorus.cpp
资源名称:gloop.zip [点击查看]
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:6k
源码类别:
OpenGL
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // 3dObjectTorus.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 "3dObjectDialog.h"
- #include <math.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- //////////////////////////////////////////////////////////////////
- // C3dObjectTorus
- IMPLEMENT_DYNAMIC(C3dObjectTorus, C3dObject)
- /////////////////////////////////////////////////////////////////////////////
- // C3dObjectTorus construction
- C3dObjectTorus::C3dObjectTorus()
- {
- // Set the attributes to default values..
- m_iType = SHAPE_OBJECT;
- m_szName.Format("Torus %u", nTorusObjects++);
- m_iNumMajor = 40;
- m_iNumMinor = 40;
- m_fMajorRadius = 2.0f;
- m_fMinorRadius = 0.5f;
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dObjectTorus Destructor
- C3dObjectTorus::~C3dObjectTorus()
- {
- // TODO: add destruction code here,
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dObjectTorus Methods or virtual function implimentation
- void C3dObjectTorus::AddAttributePage(C3dWorld* pWorld, LPVOID pSht)
- {
- C3dObjectPropSheet* pSheet = (C3dObjectPropSheet*)pSht;
- ASSERT(pSheet);
- // Add the page to the property sheet
- pSheet->AddPage(&pSheet->m_TorusPage);
- // Save the address of this object in the page
- pSheet->m_TorusPage.m_pObject = this;
- }
- void C3dObjectTorus::GetShapeBounds(C3dBoundingBox* pBox)
- {
- // Calculate the bounds of the shape
- pBox->m_fMax[X] = m_fMajorRadius + m_fMinorRadius;
- pBox->m_fMax[Y] = m_fMajorRadius + m_fMinorRadius;
- pBox->m_fMax[Z] = m_fMinorRadius;
- pBox->m_fMin[X] = -(m_fMajorRadius + m_fMinorRadius);
- pBox->m_fMin[Y] = -(m_fMajorRadius + m_fMinorRadius);
- pBox->m_fMin[Z] = -m_fMinorRadius;
- }
- int C3dObjectTorus::LoadBitMapImage(CImageList* pList)
- {
- CBitmap bitmap;
- // If the image index has been stored in this object,
- // return the index.
- if(m_iBMImage > -1)
- return m_iBMImage;
- // If the image index for this object type has been
- // created, store the index for this object and
- // return the index.
- if( iObjectTorusBMImage > -1) {
- m_iBMImage = iObjectTorusBMImage;
- return m_iBMImage;
- }
- // The image index for this object type has not been
- // loaded and the object image index has not been
- // stored.
- //
- // Load the bitmap for the non-selected light
- bitmap.LoadBitmap(IDB_OBJECT_TORUS);
- m_iBMImage = pList->Add(&bitmap, (COLORREF)0xFFFFFF);
- bitmap.DeleteObject();
- // Load the bitmap for the non-selected light
- bitmap.LoadBitmap(IDB_OBJECT_TORUS_SELECTED);
- pList->Add(&bitmap, (COLORREF)0xFFFFFF);
- bitmap.DeleteObject();
- iObjectTorusBMImage = m_iBMImage;
- return m_iBMImage;
- }
- void C3dObjectTorus::Serialize(CArchive& ar, int iVersion)
- {
- CString szBuffer;
- if (ar.IsStoring())
- {
- // Save the Object Class header...
- szBuffer.Format("n%sC3dObjectTorus {n", szIndent);
- ar.WriteString(szBuffer);
- // Save the this objects' specific data...
- szBuffer.Format("%stMajorRadius < %f >n", szIndent, m_fMajorRadius);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stMinorRadius < %f >n", szIndent, m_fMinorRadius);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stMajorSlices < %d >n", szIndent, m_iNumMajor);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stMinorSlices < %d >n", szIndent, m_iNumMinor);
- ar.WriteString(szBuffer);
- // Save the base class object data...
- C3dObject::Serialize(ar, iVersion);
- szBuffer.Format("%s}n", szIndent); // end of object def
- ar.WriteString(szBuffer);
- }
- else
- {
- if(iVersion < 102)
- // Read the base class object data...
- C3dObject::Serialize(ar, iVersion);
- // Read the derived class data..
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft(); // Remove leading white spaces
- sscanf(szBuffer, "MajorRadius < %f >n", &m_fMajorRadius);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "MinorRadius < %f >n", &m_fMinorRadius);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "MajorSlices < %d >n", &m_iNumMajor);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "MinorSlices < %d >n", &m_iNumMinor);
- if(iVersion < 102)
- // Read all child objects...
- LoadChildObjects(ar, iVersion);
- else
- // Read the base class object data...
- C3dObject::Serialize(ar, iVersion);
- }
- }
- void C3dObjectTorus::Build(C3dWorld* pWorld, C3dCamera* pCamera)
- {
- double majorStep = 2.0F*Pi / m_iNumMajor;
- double minorStep = 2.0F*Pi / m_iNumMinor;
- int i, j;
- glNewList(m_iDisplayLists, GL_COMPILE_AND_EXECUTE);
- for (i=0; i<m_iNumMajor; ++i)
- {
- double a0 = i * majorStep;
- double a1 = a0 + majorStep;
- GLfloat x0 = (GLfloat)cos(a0);
- GLfloat y0 = (GLfloat)sin(a0);
- GLfloat x1 = (GLfloat)cos(a1);
- GLfloat y1 = (GLfloat)sin(a1);
- glBegin(GL_TRIANGLE_STRIP);
- for (j=0; j<=m_iNumMinor; ++j)
- {
- double b = j * minorStep;
- GLfloat c = (GLfloat)cos(b);
- GLfloat r = m_fMinorRadius * c + m_fMajorRadius;
- GLfloat z = m_fMinorRadius * (GLfloat)sin(b);
- glNormal3f(x0*c, y0*c, z/m_fMinorRadius);
- glTexCoord2f(i/(GLfloat) m_iNumMajor, j/(GLfloat) m_iNumMinor);
- glVertex3f(x0*r, y0*r, z);
- glNormal3f(x1*c, y1*c, z/m_fMinorRadius);
- glTexCoord2f((i+1)/(GLfloat) m_iNumMajor, j/(GLfloat) m_iNumMinor);
- glVertex3f(x1*r, y1*r, z);
- }
- glEnd();
- }
- glEndList();
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dObjectTorus function implimentation