3dObjectGrid.cpp
资源名称:gloop.zip [点击查看]
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:6k
源码类别:
OpenGL
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // 3dObjectGrid.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
- //////////////////////////////////////////////////////////////////
- // C3dObjectGrid
- IMPLEMENT_DYNAMIC(C3dObjectGrid, C3dObject)
- /////////////////////////////////////////////////////////////////////////////
- // C3dShape construction
- C3dObjectGrid::C3dObjectGrid()
- {
- // Set the attributes to default values..
- m_iType = SHAPE_OBJECT;
- m_szName.Format("Grid %u", nGridObjects++);
- // Set the grid alternate color to black
- m_AltColor.SetColor4f(0.0f, 0.0f, 0.0f, 1.0f); // Black
- m_fDepth = 11.0f;
- m_fWidth = 11.0f;
- m_iDivisionsX = 11; // odd number for checkerboard pattern
- m_iDivisionsY = 11;
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3DWorld Destructor
- C3dObjectGrid::~C3dObjectGrid()
- {
- // TODO: add destruction code here,
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dObjectGrid Methods or virtual function implimentation
- void C3dObjectGrid::AddAttributePage(C3dWorld* pWorld, LPVOID pSht)
- {
- C3dObjectPropSheet* pSheet = (C3dObjectPropSheet*)pSht;
- ASSERT(pSheet);
- // Add the page to the property sheet
- pSheet->AddPage(&pSheet->m_GridPage);
- // Save the address of this object in the page
- pSheet->m_GridPage.m_pObject = this;
- // Save the address of the global Color List in the page
- pSheet->m_GridPage.m_pColorList = &pWorld->m_ColorList;
- }
- void C3dObjectGrid::GetShapeBounds(C3dBoundingBox* pBox)
- {
- // Calculate the bounds of the shape
- pBox->m_fMax[X] = m_fWidth/2;
- pBox->m_fMax[Y] = m_fDepth/2;
- pBox->m_fMax[Z] = 0.0f;
- pBox->m_fMin[X] = -m_fWidth/2;
- pBox->m_fMin[Y] = -m_fDepth/2;
- pBox->m_fMin[Z] = 0.0f;
- }
- int C3dObjectGrid::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( iObjectGridBMImage > -1) {
- m_iBMImage = iObjectGridBMImage;
- 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_GRID);
- m_iBMImage = pList->Add(&bitmap, (COLORREF)0xFFFFFF);
- bitmap.DeleteObject();
- // Load the bitmap for the non-selected light
- bitmap.LoadBitmap(IDB_OBJECT_GRID_SELECTED);
- pList->Add(&bitmap, (COLORREF)0xFFFFFF);
- bitmap.DeleteObject();
- iObjectGridBMImage = m_iBMImage;
- return m_iBMImage;
- }
- void C3dObjectGrid::Serialize(CArchive& ar, int iVersion)
- {
- CString szBuffer;
- if (ar.IsStoring())
- {
- // Save the Object Class header...
- szBuffer.Format("n%sC3dObjectGrid {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_fDepth);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stDivisions 'X' < %d >n", szIndent, m_iDivisionsX);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stDivisions 'Y' < %d >n", szIndent, m_iDivisionsY);
- 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, "Depth < %f >n", &m_fDepth);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "Width < %f >n", &m_fWidth);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "Divisions 'X' < %d >n", &m_iDivisionsX);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "Divisions 'Y' < %d >n", &m_iDivisionsY);
- if(iVersion < 102)
- // Read all child objects...
- LoadChildObjects(ar, iVersion);
- else
- // Read the base class object data...
- C3dObject::Serialize(ar, iVersion);
- }
- }
- void C3dObjectGrid::Build(C3dWorld* pWorld, C3dCamera* pCamera)
- {
- GLfloat x, y, z = 0.0f;
- float fStepX, fStepY;
- BOOL bToggle = FALSE;
- // Compile and build the list
- glNewList(m_iDisplayLists, GL_COMPILE_AND_EXECUTE);
- // Calculate the number of steps in the x-axis
- if(m_iDivisionsX)
- fStepX = m_fWidth/m_iDivisionsX;
- else
- fStepX = 1.0f;
- // Calculate the number of steps in the x-axis
- if(m_iDivisionsY)
- fStepY = m_fDepth/m_iDivisionsY;
- else
- fStepY = 1.0f;
- for(x=-m_fWidth/2.0f; x<m_fWidth/2; x+=fStepX)
- {
- for(y=-m_fDepth/2.0f; y<m_fDepth/2; y+=fStepY)
- {
- if(bToggle)
- {
- // Reset color parameters to default values
- glDisable(GL_TEXTURE_2D);
- glColor4fv(m_AltColor.m_fColor);
- bToggle = FALSE;
- }
- else
- {
- // Setup this objects polygon color material
- // or texture attributes
- SetAttributes(pWorld, NULL);
- bToggle = TRUE;
- }
- glBegin(GL_POLYGON);
- glNormal3d(0.0, 0.0, 1.0);
- glTexCoord2d(0.0, 0.0);
- glVertex3d(x, y, z);
- glTexCoord2d(1, 0.0);
- glVertex3d(x+fStepX, y, z);
- glTexCoord2d(1, 1);
- glVertex3d(x+fStepX, y+fStepY, z);
- glTexCoord2d(0.0, 1);
- glVertex3d(x, y+fStepY, z);
- glEnd();
- }
- }
- glEndList();
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dObjectGrid function implimentation