3dObjectPlane.cpp
资源名称:gloop.zip [点击查看]
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:8k
源码类别:
OpenGL
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // 3dObjectPlane.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
- //////////////////////////////////////////////////////////////////
- // C3dObjectPlane
- IMPLEMENT_DYNAMIC(C3dObjectPlane, C3dObject)
- /////////////////////////////////////////////////////////////////////////////
- // C3dShape construction
- C3dObjectPlane::C3dObjectPlane()
- {
- // Set the attributes to default values..
- m_iType = SHAPE_OBJECT;
- m_szName.Format("Plane %u", nPlaneObjects++);
- m_fDepth = 2.0f;
- m_fWidth = 2.0f;
- // Create our C3dPointArray object
- m_pPointArray = new C3dPointArray;
- ASSERT(m_pPointArray);
- m_pPointArray->Create(4); // Create an array of points for our vertices
- // Initialize our cubes vertices (points)
- InitVertices();
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3DWorld Destructor
- C3dObjectPlane::~C3dObjectPlane()
- {
- // Delete our point array
- if(m_pPointArray)
- delete m_pPointArray;
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dObjectPlane Methods or virtual function implimentation
- void C3dObjectPlane::AddAttributePage(C3dWorld* pWorld, LPVOID pSht)
- {
- C3dObjectPropSheet* pSheet = (C3dObjectPropSheet*)pSht;
- ASSERT(pSheet);
- // Add the page to the property sheet
- pSheet->AddPage(&pSheet->m_PlanePage);
- // Save the address of this object in the page
- pSheet->m_PlanePage.m_pObject = this;
- }
- int C3dObjectPlane::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( iObjectPlaneBMImage > -1) {
- m_iBMImage = iObjectPlaneBMImage;
- 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_PLANE);
- m_iBMImage = pList->Add(&bitmap, (COLORREF)0xFFFFFF);
- bitmap.DeleteObject();
- // Load the bitmap for the non-selected light
- bitmap.LoadBitmap(IDB_OBJECT_PLANE_SELECTED);
- pList->Add(&bitmap, (COLORREF)0xFFFFFF);
- bitmap.DeleteObject();
- iObjectPlaneBMImage = m_iBMImage;
- return m_iBMImage;
- }
- void C3dObjectPlane::Serialize(CArchive& ar, int iVersion)
- {
- CString szBuffer;
- if (ar.IsStoring())
- {
- // Save the Object Class header...
- szBuffer.Format("n%sC3dObjectPlane {n", szIndent);
- ar.WriteString(szBuffer);
- // Save the this objects' specific data...
- szBuffer.Format("%stDepth < %f >n", szIndent, m_fDepth);
- 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, "Width < %f >n", &m_fWidth);
- if(iVersion > 130)
- {
- 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 C3dObjectPlane::Build(C3dWorld* pWorld, C3dCamera* pCamera)
- {
- // Our plane is defined by four (4) vertices, or points, as follows:
- /*
- p3 * ____________ * p0
- / /
- / /
- p2 * /------------/ * p1
- */
- // Compile and build the list
- glNewList(m_iDisplayLists, GL_COMPILE_AND_EXECUTE);
- VECTORF Normal;
- glBegin(GL_TRIANGLE_STRIP); // top
- CalNormalf(m_pPointArray->m_pPoints[1].m_fOrigin,
- m_pPointArray->m_pPoints[2].m_fOrigin,
- m_pPointArray->m_pPoints[0].m_fOrigin,
- Normal);
- glNormal3fv(Normal);
- if(m_pTexture)
- m_pTexture->SetTextureCoord3f(1.0f, 0.0f, 0.0f);
- // 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);
- CalNormalf(m_pPointArray->m_pPoints[0].m_fOrigin,
- m_pPointArray->m_pPoints[1].m_fOrigin,
- m_pPointArray->m_pPoints[3].m_fOrigin,
- Normal);
- glNormal3fv(Normal);
- if(m_pTexture)
- m_pTexture->SetTextureCoord3f(1.0f, 1.0f, 0.0f);
- // glTexCoord2d(1, 1);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[0].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[0].m_fOrigin);
- CalNormalf(m_pPointArray->m_pPoints[2].m_fOrigin,
- m_pPointArray->m_pPoints[3].m_fOrigin,
- m_pPointArray->m_pPoints[1].m_fOrigin,
- Normal);
- glNormal3fv(Normal);
- if(m_pTexture)
- m_pTexture->SetTextureCoord3f(0.0f, 0.0f, 0.0f);
- // 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[3].m_fOrigin,
- m_pPointArray->m_pPoints[0].m_fOrigin,
- m_pPointArray->m_pPoints[2].m_fOrigin,
- Normal);
- glNormal3fv(Normal);
- if(m_pTexture)
- m_pTexture->SetTextureCoord3f(0.0f, 1.0f, 0.0f);
- // glTexCoord2d(0.0, 1);
- if(!m_bSolidColor)
- glColor4fv(m_pPointArray->m_pPoints[3].m_Color.m_fColor);
- glVertex3fv(m_pPointArray->m_pPoints[3].m_fOrigin);
- glEnd();
- glEndList();
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dObjectPlane Methods or Implementation
- void C3dObjectPlane::InitVertices()
- {
- // Our plane is defined by four (4) vertices, or points, as follows:
- /*
- p3 * ____________ * p0
- / /
- / /
- p2 * /------------/ * p1
- */
- 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] = 0.0f;
- pPoint++;
- pPoint->m_fOrigin[X] = m_fWidth/2;
- pPoint->m_fOrigin[Y] = -m_fDepth/2;
- pPoint->m_fOrigin[Z] = 0.0f;
- pPoint++;
- pPoint->m_fOrigin[X] = -m_fWidth/2;
- pPoint->m_fOrigin[Y] = -m_fDepth/2;
- pPoint->m_fOrigin[Z] = 0.0f;
- pPoint++;
- pPoint->m_fOrigin[X] = -m_fWidth/2;
- pPoint->m_fOrigin[Y] = m_fDepth/2;
- pPoint->m_fOrigin[Z] = 0.0f;
- }