3dObjectPlane.cpp
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:8k
源码类别:

OpenGL

开发平台:

Visual C++

  1. /////////////////////////////////////////////////////////////////////////////
  2. // 3dObjectPlane.cpp : implementation file
  3. //
  4. // glOOP (OpenGL Object Oriented Programming library)
  5. // Copyright (c) Craig Fahrnbach 1997, 1998
  6. //
  7. // OpenGL is a registered trademark of Silicon Graphics
  8. //
  9. //
  10. // This program is provided for educational and personal use only and
  11. // is provided without guarantee or warrantee expressed or implied.
  12. //
  13. // Commercial use is strickly prohibited without written permission
  14. // from ImageWare Development.
  15. //
  16. // This program is -not- in the public domain.
  17. //
  18. /////////////////////////////////////////////////////////////////////////////
  19. #include "stdafx.h"
  20. #include "glOOP.h"
  21. #include "3dObjectDialog.h"
  22. #ifdef _DEBUG
  23. #define new DEBUG_NEW
  24. #undef THIS_FILE
  25. static char THIS_FILE[] = __FILE__;
  26. #endif
  27. //////////////////////////////////////////////////////////////////
  28. // C3dObjectPlane
  29. IMPLEMENT_DYNAMIC(C3dObjectPlane, C3dObject)
  30. /////////////////////////////////////////////////////////////////////////////
  31. // C3dShape construction
  32. C3dObjectPlane::C3dObjectPlane()
  33. {
  34. // Set the attributes to default values..
  35.   m_iType = SHAPE_OBJECT;
  36.    m_szName.Format("Plane %u", nPlaneObjects++);
  37. m_fDepth  = 2.0f;
  38. m_fWidth  = 2.0f;
  39. // Create our C3dPointArray object
  40. m_pPointArray = new C3dPointArray;
  41. ASSERT(m_pPointArray);
  42. m_pPointArray->Create(4); // Create an array of points for our vertices
  43. // Initialize our cubes vertices (points)
  44. InitVertices();
  45. }
  46. /////////////////////////////////////////////////////////////////////////////
  47. // C3DWorld Destructor
  48. C3dObjectPlane::~C3dObjectPlane()
  49. {
  50. // Delete our point array
  51. if(m_pPointArray)
  52. delete m_pPointArray;
  53. }
  54. /////////////////////////////////////////////////////////////////////////////
  55. // C3dObjectPlane Methods or virtual function implimentation
  56. void C3dObjectPlane::AddAttributePage(C3dWorld* pWorld, LPVOID pSht)
  57. {
  58. C3dObjectPropSheet* pSheet = (C3dObjectPropSheet*)pSht;
  59. ASSERT(pSheet);
  60. // Add the page to the property sheet
  61. pSheet->AddPage(&pSheet->m_PlanePage);
  62. // Save the address of this object in the page
  63. pSheet->m_PlanePage.m_pObject = this;
  64. }
  65. int C3dObjectPlane::LoadBitMapImage(CImageList* pList)
  66. {
  67. CBitmap bitmap;
  68. // If the image index has been stored in this object,
  69. // return the index.
  70. if(m_iBMImage > -1)
  71. return m_iBMImage;
  72. // If the image index for this object type has been
  73. // created, store the index for this object and
  74. // return the index.
  75. if( iObjectPlaneBMImage > -1) {
  76. m_iBMImage = iObjectPlaneBMImage;
  77. return m_iBMImage;
  78. }
  79. // The image index for this object type has not been
  80. // loaded and the object image index has not been
  81. // stored.
  82. //
  83. // Load the bitmap for the non-selected light
  84. bitmap.LoadBitmap(IDB_OBJECT_PLANE);
  85. m_iBMImage = pList->Add(&bitmap, (COLORREF)0xFFFFFF);
  86. bitmap.DeleteObject();
  87. // Load the bitmap for the non-selected light
  88. bitmap.LoadBitmap(IDB_OBJECT_PLANE_SELECTED);
  89. pList->Add(&bitmap, (COLORREF)0xFFFFFF);
  90. bitmap.DeleteObject();
  91. iObjectPlaneBMImage = m_iBMImage;
  92. return m_iBMImage;
  93. }
  94. void C3dObjectPlane::Serialize(CArchive& ar, int iVersion)
  95. {
  96. CString szBuffer;
  97. if (ar.IsStoring())
  98. {
  99. // Save the Object Class header...
  100. szBuffer.Format("n%sC3dObjectPlane {n", szIndent);
  101. ar.WriteString(szBuffer);
  102. // Save the this objects' specific data...
  103. szBuffer.Format("%stDepth         < %f >n", szIndent, m_fDepth);
  104. ar.WriteString(szBuffer);
  105. szBuffer.Format("%stWidth         < %f >n", szIndent, m_fWidth);
  106. ar.WriteString(szBuffer);
  107. szBuffer.Format("%stSolid Color   < %d >n", szIndent, m_bSolidColor);
  108. ar.WriteString(szBuffer);
  109. szBuffer.Format("%stNum Points    < %d >n", szIndent, m_pPointArray->m_iNumPoints);
  110. ar.WriteString(szBuffer);
  111. m_pPointArray->Serialize(ar, iVersion, !m_bSolidColor);
  112. // Save the base class object data...
  113. C3dObject::Serialize(ar, iVersion);
  114. szBuffer.Format("%s}n", szIndent); // end of object def
  115. ar.WriteString(szBuffer);
  116. }
  117. else
  118. {
  119. if(iVersion < 102)
  120. // Read the base class object data...
  121. C3dObject::Serialize(ar, iVersion);
  122. // Read the derived class data..
  123. ar.ReadString(szBuffer);
  124. szBuffer.TrimLeft(); // Remove leading white spaces
  125. sscanf(szBuffer, "Depth         < %f >n", &m_fDepth);
  126. ar.ReadString(szBuffer);
  127. szBuffer.TrimLeft();
  128. sscanf(szBuffer, "Width         < %f >n", &m_fWidth);
  129. if(iVersion > 130)
  130. {
  131. ar.ReadString(szBuffer);
  132. szBuffer.TrimLeft();
  133. sscanf(szBuffer, "Solid Color   < %d >n", &m_bSolidColor);
  134. }
  135. if(iVersion < 110)
  136. // Initialize our point array vertices
  137. InitVertices();
  138. else
  139. {
  140. ar.ReadString(szBuffer);
  141. szBuffer.TrimLeft();
  142. sscanf(szBuffer, "Num Points    < %d >n", &m_pPointArray->m_iNumPoints);
  143. // Read the shape vertice, or point data
  144. m_pPointArray->Serialize(ar, iVersion, !m_bSolidColor);
  145. }
  146. if(iVersion < 102)
  147. // Read all child objects...
  148. LoadChildObjects(ar, iVersion);
  149. else
  150. // Read the base class object data...
  151. C3dObject::Serialize(ar, iVersion);
  152. }
  153. }
  154. void C3dObjectPlane::Build(C3dWorld* pWorld, C3dCamera* pCamera)
  155. {
  156. // Our plane is defined by four (4) vertices, or points, as follows:
  157. /*
  158.          p3 *  ____________  * p0
  159.              /            /
  160.            /            /
  161.     p2 * /------------/  * p1
  162. */
  163. // Compile and build the list
  164. glNewList(m_iDisplayLists, GL_COMPILE_AND_EXECUTE);
  165. VECTORF Normal;
  166. glBegin(GL_TRIANGLE_STRIP); // top
  167. CalNormalf(m_pPointArray->m_pPoints[1].m_fOrigin,
  168.    m_pPointArray->m_pPoints[2].m_fOrigin,
  169.    m_pPointArray->m_pPoints[0].m_fOrigin,
  170.    Normal);
  171. glNormal3fv(Normal);
  172. if(m_pTexture)
  173. m_pTexture->SetTextureCoord3f(1.0f, 0.0f, 0.0f);
  174. // glTexCoord2d(1, 0.0);
  175. if(!m_bSolidColor)
  176. glColor4fv(m_pPointArray->m_pPoints[1].m_Color.m_fColor);
  177. glVertex3fv(m_pPointArray->m_pPoints[1].m_fOrigin);
  178. CalNormalf(m_pPointArray->m_pPoints[0].m_fOrigin,
  179.    m_pPointArray->m_pPoints[1].m_fOrigin,
  180.    m_pPointArray->m_pPoints[3].m_fOrigin,
  181.    Normal);
  182. glNormal3fv(Normal);
  183. if(m_pTexture)
  184. m_pTexture->SetTextureCoord3f(1.0f, 1.0f, 0.0f);
  185. // glTexCoord2d(1, 1);
  186. if(!m_bSolidColor)
  187. glColor4fv(m_pPointArray->m_pPoints[0].m_Color.m_fColor);
  188. glVertex3fv(m_pPointArray->m_pPoints[0].m_fOrigin);
  189. CalNormalf(m_pPointArray->m_pPoints[2].m_fOrigin,
  190.    m_pPointArray->m_pPoints[3].m_fOrigin,
  191.    m_pPointArray->m_pPoints[1].m_fOrigin,
  192.    Normal);
  193. glNormal3fv(Normal);
  194. if(m_pTexture)
  195. m_pTexture->SetTextureCoord3f(0.0f, 0.0f, 0.0f);
  196. // glTexCoord2d(0.0, 0.0);
  197. if(!m_bSolidColor)
  198. glColor4fv(m_pPointArray->m_pPoints[2].m_Color.m_fColor);
  199. glVertex3fv(m_pPointArray->m_pPoints[2].m_fOrigin);
  200. CalNormalf(m_pPointArray->m_pPoints[3].m_fOrigin,
  201.    m_pPointArray->m_pPoints[0].m_fOrigin,
  202.    m_pPointArray->m_pPoints[2].m_fOrigin,
  203.    Normal);
  204. glNormal3fv(Normal);
  205. if(m_pTexture)
  206. m_pTexture->SetTextureCoord3f(0.0f, 1.0f, 0.0f);
  207. // glTexCoord2d(0.0, 1);
  208. if(!m_bSolidColor)
  209. glColor4fv(m_pPointArray->m_pPoints[3].m_Color.m_fColor);
  210. glVertex3fv(m_pPointArray->m_pPoints[3].m_fOrigin);
  211. glEnd();
  212. glEndList();
  213. }
  214. /////////////////////////////////////////////////////////////////////////////
  215. // C3dObjectPlane Methods or Implementation
  216. void C3dObjectPlane::InitVertices()
  217. {
  218. // Our plane is defined by four (4) vertices, or points, as follows:
  219. /*
  220.          p3 *  ____________  * p0
  221.              /            /
  222.            /            /
  223.     p2 * /------------/  * p1
  224. */
  225. C3dPoint* pPoint = m_pPointArray->m_pPoints;
  226. // Initialize our cube's vertice points p0-p3
  227. pPoint->m_fOrigin[X] = m_fWidth/2; 
  228. pPoint->m_fOrigin[Y] = m_fDepth/2;
  229. pPoint->m_fOrigin[Z] = 0.0f;
  230. pPoint++;
  231. pPoint->m_fOrigin[X] = m_fWidth/2;
  232. pPoint->m_fOrigin[Y] = -m_fDepth/2;
  233. pPoint->m_fOrigin[Z] = 0.0f;
  234. pPoint++;
  235. pPoint->m_fOrigin[X] = -m_fWidth/2;
  236. pPoint->m_fOrigin[Y] = -m_fDepth/2;
  237. pPoint->m_fOrigin[Z] = 0.0f;
  238. pPoint++;
  239. pPoint->m_fOrigin[X] = -m_fWidth/2;
  240. pPoint->m_fOrigin[Y] = m_fDepth/2;
  241. pPoint->m_fOrigin[Z] = 0.0f;
  242. }