3dObjectCube.cpp
资源名称:gloop.zip [点击查看]
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:14k
源码类别:
OpenGL
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // 3dObjectCube.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"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- //////////////////////////////////////////////////////////////////
- // C3dObjectCube
- IMPLEMENT_DYNAMIC(C3dObjectCube, C3dObject)
- /////////////////////////////////////////////////////////////////////////////
- // C3dObjectCube construction
- C3dObjectCube::C3dObjectCube()
- {
- // Set the attributes to default values..
- m_iType = SHAPE_OBJECT;
- m_szName.Format("Cube %u", nCubeObjects++);
- m_fHeight = 2.0f;
- m_fWidth = 2.0f;
- m_fDepth = 2.0f;
- // Create our C3dPointArray object
- m_pPointArray = new C3dPointArray;
- ASSERT(m_pPointArray);
- m_pPointArray->Create(8); // Create an array of points for our vertices
- // Initialize our cubes vertices (points)
- InitVertices();
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dObjectCube Destructor
- C3dObjectCube::~C3dObjectCube()
- {
- // Delete our point array
- if(m_pPointArray)
- delete m_pPointArray;
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dObjectCube Methods or virtual function implimentation
- void C3dObjectCube::AddAttributePage(C3dWorld* pWorld, LPVOID pSht)
- {
- C3dObjectPropSheet* pSheet = (C3dObjectPropSheet*)pSht;
- ASSERT(pSheet);
- // Add the page to the property sheet
- pSheet->AddPage(&pSheet->m_CubePage);
- // Save the address of this object in the page
- pSheet->m_CubePage.m_pObject = this;
- }
- int C3dObjectCube::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( iObjectCubeBMImage > -1) {
- m_iBMImage = iObjectCubeBMImage;
- 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_CUBE);
- m_iBMImage = pList->Add(&bitmap, (COLORREF)0xFFFFFF);
- bitmap.DeleteObject();
- // Load the bitmap for the non-selected light
- bitmap.LoadBitmap(IDB_OBJECT_CUBE_SELECTED);
- pList->Add(&bitmap, (COLORREF)0xFFFFFF);
- bitmap.DeleteObject();
- iObjectCubeBMImage = m_iBMImage;
- return m_iBMImage;
- }
- void C3dObjectCube::Serialize(CArchive& ar, int iVersion)
- {
- CString szBuffer;
- if (ar.IsStoring())
- {
- // Save the Object Class header...
- szBuffer.Format("n%sC3dObjectCube {n", szIndent);
- ar.WriteString(szBuffer);
- // Save the this objects' specific data...
- szBuffer.Format("%stDepth < %f >n", szIndent, m_fDepth);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stHeight < %f >n", szIndent, m_fHeight);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stWidth < %f >n", szIndent, m_fWidth);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stSolid Color < %d >n", szIndent, m_bSolidColor);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stNum Points < %d >n", szIndent, m_pPointArray->m_iNumPoints);
- ar.WriteString(szBuffer);
- m_pPointArray->Serialize(ar, iVersion, !m_bSolidColor);
- // 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, "Depth < %f >n", &m_fDepth);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "Height < %f >n", &m_fHeight);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "Width < %f >n", &m_fWidth);
- if(iVersion > 110)
- {
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "Solid Color < %d >n", &m_bSolidColor);
- }
- if(iVersion < 110)
- // Initialize our point array vertices
- InitVertices();
- else
- {
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "Num Points < %d >n", &m_pPointArray->m_iNumPoints);
- // Read the shape vertice, or point data
- m_pPointArray->Serialize(ar, iVersion, !m_bSolidColor);
- }
- if(iVersion < 102)
- // Read all child objects...
- LoadChildObjects(ar, iVersion);
- else
- // Read the base class object data...
- C3dObject::Serialize(ar, iVersion);
- }
- }
- void C3dObjectCube::Build(C3dWorld* pWorld, C3dCamera* pCamera)
- {
- // Our cube is defined by eight (8) vertices, or points, as follows:
- /*
- p5 * ____________ * p0
- / /
- / / |
- p4 * /------------/ * p1
- | | |
- | | |
- | | |
- p6 * |_ / * p3
- | / | /
- p7 * |____________|/ * p2
- */
- // Compile and build the list
- glNewList(m_iDisplayLists, GL_COMPILE_AND_EXECUTE);
- VECTORF Normal;
- // Front (CCW order)
- glBegin(GL_TRIANGLE_STRIP);
- CalNormalf(m_pPointArray->m_pPoints[2].m_fOrigin,
- m_pPointArray->m_pPoints[7].m_fOrigin,
- m_pPointArray->m_pPoints[1].m_fOrigin,
- Normal);
- glNormal3fv(Normal);
- glTexCoord2d(1, 0.0);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[2].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[2].m_fOrigin);
- glTexCoord2d(1, 1);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[1].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[1].m_fOrigin);
- glTexCoord2d(0.0, 0.0);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[7].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[7].m_fOrigin);
- CalNormalf(m_pPointArray->m_pPoints[4].m_fOrigin,
- m_pPointArray->m_pPoints[1].m_fOrigin,
- m_pPointArray->m_pPoints[7].m_fOrigin,
- Normal);
- glNormal3fv(Normal);
- glTexCoord2d(0.0, 1);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[4].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[4].m_fOrigin);
- glEnd();
- // Back
- glBegin(GL_TRIANGLE_STRIP);
- CalNormalf(m_pPointArray->m_pPoints[6].m_fOrigin,
- m_pPointArray->m_pPoints[3].m_fOrigin,
- m_pPointArray->m_pPoints[5].m_fOrigin,
- Normal);
- glNormal3fv(Normal);
- glTexCoord2d(1, 0.0);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[6].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[6].m_fOrigin);
- glTexCoord2d(1, 1);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[5].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[5].m_fOrigin);
- glTexCoord2d(0.0, 0.0);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[3].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[3].m_fOrigin);
- CalNormalf(m_pPointArray->m_pPoints[0].m_fOrigin,
- m_pPointArray->m_pPoints[5].m_fOrigin,
- m_pPointArray->m_pPoints[3].m_fOrigin,
- Normal);
- glNormal3fv(Normal);
- glTexCoord2d(0.0, 1);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[0].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[0].m_fOrigin);
- glEnd();
- // Left
- glBegin(GL_TRIANGLE_STRIP);
- CalNormalf(m_pPointArray->m_pPoints[7].m_fOrigin,
- m_pPointArray->m_pPoints[6].m_fOrigin,
- m_pPointArray->m_pPoints[4].m_fOrigin,
- Normal);
- glNormal3fv(Normal);
- glTexCoord2d(1, 0.0);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[7].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[7].m_fOrigin);
- glTexCoord2d(1, 1);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[4].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[4].m_fOrigin);
- glTexCoord2d(0.0, 0.0);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[6].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[6].m_fOrigin);
- CalNormalf(m_pPointArray->m_pPoints[5].m_fOrigin,
- m_pPointArray->m_pPoints[4].m_fOrigin,
- m_pPointArray->m_pPoints[6].m_fOrigin,
- Normal);
- glNormal3fv(Normal);
- glTexCoord2d(0.0, 1);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[5].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[5].m_fOrigin);
- glEnd();
- // Right
- glBegin(GL_TRIANGLE_STRIP);
- CalNormalf(m_pPointArray->m_pPoints[3].m_fOrigin,
- m_pPointArray->m_pPoints[2].m_fOrigin,
- m_pPointArray->m_pPoints[0].m_fOrigin,
- Normal);
- glNormal3fv(Normal);
- glTexCoord2d(1, 0.0);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[3].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[3].m_fOrigin);
- glTexCoord2d(1, 1);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[0].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[0].m_fOrigin);
- glTexCoord2d(0.0, 0.0);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[2].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[2].m_fOrigin);
- CalNormalf(m_pPointArray->m_pPoints[1].m_fOrigin,
- m_pPointArray->m_pPoints[0].m_fOrigin,
- m_pPointArray->m_pPoints[2].m_fOrigin,
- Normal);
- glNormal3fv(Normal);
- glTexCoord2d(0.0, 1);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[1].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[1].m_fOrigin);
- glEnd();
- // Top
- glBegin(GL_TRIANGLE_STRIP);
- CalNormalf(m_pPointArray->m_pPoints[1].m_fOrigin,
- m_pPointArray->m_pPoints[4].m_fOrigin,
- m_pPointArray->m_pPoints[0].m_fOrigin,
- Normal);
- glNormal3fv(Normal);
- glTexCoord2d(1, 0.0);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[1].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[1].m_fOrigin);
- glTexCoord2d(1, 1);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[0].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[0].m_fOrigin);
- glTexCoord2d(0.0, 0.0);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[4].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[4].m_fOrigin);
- CalNormalf(m_pPointArray->m_pPoints[5].m_fOrigin,
- m_pPointArray->m_pPoints[0].m_fOrigin,
- m_pPointArray->m_pPoints[4].m_fOrigin,
- Normal);
- glNormal3fv(Normal);
- glTexCoord2d(0.0, 1);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[5].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[5].m_fOrigin);
- glEnd();
- // Bottom
- glBegin(GL_TRIANGLE_STRIP);
- CalNormalf(m_pPointArray->m_pPoints[3].m_fOrigin,
- m_pPointArray->m_pPoints[6].m_fOrigin,
- m_pPointArray->m_pPoints[2].m_fOrigin,
- Normal);
- glNormal3fv(Normal);
- glTexCoord2d(1, 0.0);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[3].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[3].m_fOrigin);
- glTexCoord2d(1, 1);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[2].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[2].m_fOrigin);
- glTexCoord2d(0.0, 0.0);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[6].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[6].m_fOrigin);
- CalNormalf(m_pPointArray->m_pPoints[7].m_fOrigin,
- m_pPointArray->m_pPoints[2].m_fOrigin,
- m_pPointArray->m_pPoints[6].m_fOrigin,
- Normal);
- glNormal3fv(Normal);
- glTexCoord2d(0.0, 1);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[7].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[7].m_fOrigin);
- glEnd();
- glEndList();
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dObjectCube Methods or Implementation
- void C3dObjectCube::InitVertices()
- {
- // Our cube is defined by eight (8) vertices, or points, as follows:
- /*
- p5 * ____________ * p0
- / /
- / / |
- p4 * /------------/ * p1
- | | |
- | | |
- | | |
- p6 * |_ / * p3
- | / | /
- p7 * |____________|/ * p2
- */
- C3dPoint* pPoint = m_pPointArray->m_pPoints;
- // Initialize our cube's vertice points p0-p3
- pPoint->m_fOrigin[X] = m_fWidth/2;
- pPoint->m_fOrigin[Y] = m_fDepth/2;
- pPoint->m_fOrigin[Z] = m_fHeight/2;
- pPoint++;
- pPoint->m_fOrigin[X] = m_fWidth/2;
- pPoint->m_fOrigin[Y] = -m_fDepth/2;
- pPoint->m_fOrigin[Z] = m_fHeight/2;
- pPoint++;
- pPoint->m_fOrigin[X] = m_fWidth/2;
- pPoint->m_fOrigin[Y] = -m_fDepth/2;
- pPoint->m_fOrigin[Z] = -m_fHeight/2;
- pPoint++;
- pPoint->m_fOrigin[X] = m_fWidth/2;
- pPoint->m_fOrigin[Y] = m_fDepth/2;
- pPoint->m_fOrigin[Z] = -m_fHeight/2;
- // Initialize our cube's vertice points p4-p7
- pPoint++;
- pPoint->m_fOrigin[X] = -m_fWidth/2;
- pPoint->m_fOrigin[Y] = -m_fDepth/2;
- pPoint->m_fOrigin[Z] = m_fHeight/2;
- pPoint++;
- pPoint->m_fOrigin[X] = -m_fWidth/2;
- pPoint->m_fOrigin[Y] = m_fDepth/2;
- pPoint->m_fOrigin[Z] = m_fHeight/2;
- pPoint++;
- pPoint->m_fOrigin[X] = -m_fWidth/2;
- pPoint->m_fOrigin[Y] = m_fDepth/2;
- pPoint->m_fOrigin[Z] = -m_fHeight/2;
- pPoint++;
- pPoint->m_fOrigin[X] = -m_fWidth/2;
- pPoint->m_fOrigin[Y] = -m_fDepth/2;
- pPoint->m_fOrigin[Z] = -m_fHeight/2;
- }