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

OpenGL

开发平台:

Visual C++

  1. /////////////////////////////////////////////////////////////////////////////
  2. // 3dObjectCube.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. // C3dObjectCube
  29. IMPLEMENT_DYNAMIC(C3dObjectCube, C3dObject)
  30. /////////////////////////////////////////////////////////////////////////////
  31. // C3dObjectCube construction
  32. C3dObjectCube::C3dObjectCube()
  33. {
  34. // Set the attributes to default values..
  35. m_iType = SHAPE_OBJECT;
  36.     m_szName.Format("Cube %u", nCubeObjects++);
  37. m_fHeight = 2.0f;
  38. m_fWidth  = 2.0f;
  39. m_fDepth  = 2.0f;
  40. // Create our C3dPointArray object
  41. m_pPointArray = new C3dPointArray;
  42. ASSERT(m_pPointArray);
  43. m_pPointArray->Create(8); // Create an array of points for our vertices
  44. // Initialize our cubes vertices (points)
  45. InitVertices();
  46. }
  47. /////////////////////////////////////////////////////////////////////////////
  48. // C3dObjectCube Destructor
  49. C3dObjectCube::~C3dObjectCube()
  50. {
  51. // Delete our point array
  52. if(m_pPointArray)
  53. delete m_pPointArray;
  54. }
  55. /////////////////////////////////////////////////////////////////////////////
  56. // C3dObjectCube Methods or virtual function implimentation
  57. void C3dObjectCube::AddAttributePage(C3dWorld* pWorld, LPVOID pSht)
  58. {
  59. C3dObjectPropSheet* pSheet = (C3dObjectPropSheet*)pSht;
  60. ASSERT(pSheet);
  61. // Add the page to the property sheet
  62. pSheet->AddPage(&pSheet->m_CubePage);
  63. // Save the address of this object in the page
  64. pSheet->m_CubePage.m_pObject = this;
  65. }
  66. int C3dObjectCube::LoadBitMapImage(CImageList* pList)
  67. {
  68. CBitmap bitmap;
  69. // If the image index has been stored in this object,
  70. // return the index.
  71. if(m_iBMImage > -1)
  72. return m_iBMImage;
  73. // If the image index for this object type has been
  74. // created, store the index for this object and
  75. // return the index.
  76. if( iObjectCubeBMImage > -1) {
  77. m_iBMImage = iObjectCubeBMImage;
  78. return m_iBMImage;
  79. }
  80. // The image index for this object type has not been
  81. // loaded and the object image index has not been
  82. // stored.
  83. //
  84. // Load the bitmap for the non-selected light
  85. bitmap.LoadBitmap(IDB_OBJECT_CUBE);
  86. m_iBMImage = pList->Add(&bitmap, (COLORREF)0xFFFFFF);
  87. bitmap.DeleteObject();
  88. // Load the bitmap for the non-selected light
  89. bitmap.LoadBitmap(IDB_OBJECT_CUBE_SELECTED);
  90. pList->Add(&bitmap, (COLORREF)0xFFFFFF);
  91. bitmap.DeleteObject();
  92. iObjectCubeBMImage = m_iBMImage;
  93. return m_iBMImage;
  94. }
  95. void C3dObjectCube::Serialize(CArchive& ar, int iVersion)
  96. {
  97. CString szBuffer;
  98. if (ar.IsStoring())
  99. {
  100. // Save the Object Class header...
  101. szBuffer.Format("n%sC3dObjectCube {n", szIndent);
  102. ar.WriteString(szBuffer);
  103. // Save the this objects' specific data...
  104. szBuffer.Format("%stDepth         < %f >n", szIndent, m_fDepth);
  105. ar.WriteString(szBuffer);
  106. szBuffer.Format("%stHeight        < %f >n", szIndent, m_fHeight);
  107. ar.WriteString(szBuffer);
  108. szBuffer.Format("%stWidth         < %f >n", szIndent, m_fWidth);
  109. ar.WriteString(szBuffer);
  110. szBuffer.Format("%stSolid Color   < %d >n", szIndent, m_bSolidColor);
  111. ar.WriteString(szBuffer);
  112. szBuffer.Format("%stNum Points    < %d >n", szIndent, m_pPointArray->m_iNumPoints);
  113. ar.WriteString(szBuffer);
  114. m_pPointArray->Serialize(ar, iVersion, !m_bSolidColor);
  115. // Save the base class object data...
  116. C3dObject::Serialize(ar, iVersion);
  117. szBuffer.Format("%s}n", szIndent); // end of object def
  118. ar.WriteString(szBuffer);
  119. }
  120. else
  121. {
  122. if(iVersion < 102)
  123. // Read the base class object data...
  124. C3dObject::Serialize(ar, iVersion);
  125. // Read the derived class data..
  126. ar.ReadString(szBuffer);
  127. szBuffer.TrimLeft(); // Remove leading white spaces
  128. sscanf(szBuffer, "Depth         < %f >n", &m_fDepth);
  129. ar.ReadString(szBuffer);
  130. szBuffer.TrimLeft();
  131. sscanf(szBuffer, "Height        < %f >n", &m_fHeight);
  132. ar.ReadString(szBuffer);
  133. szBuffer.TrimLeft();
  134. sscanf(szBuffer, "Width         < %f >n", &m_fWidth);
  135. if(iVersion > 110)
  136. {
  137. ar.ReadString(szBuffer);
  138. szBuffer.TrimLeft();
  139. sscanf(szBuffer, "Solid Color   < %d >n", &m_bSolidColor);
  140. }
  141. if(iVersion < 110)
  142. // Initialize our point array vertices
  143. InitVertices();
  144. else
  145. {
  146. ar.ReadString(szBuffer);
  147. szBuffer.TrimLeft();
  148. sscanf(szBuffer, "Num Points    < %d >n", &m_pPointArray->m_iNumPoints);
  149. // Read the shape vertice, or point data
  150. m_pPointArray->Serialize(ar, iVersion, !m_bSolidColor);
  151. }
  152. if(iVersion < 102)
  153. // Read all child objects...
  154. LoadChildObjects(ar, iVersion);
  155. else
  156. // Read the base class object data...
  157. C3dObject::Serialize(ar, iVersion);
  158. }
  159. }
  160. void C3dObjectCube::Build(C3dWorld* pWorld, C3dCamera* pCamera)
  161. {
  162. // Our cube is defined by eight (8) vertices, or points, as follows:
  163. /*
  164.          p5 *  ____________  * p0
  165.              /            /
  166.            /            /  |           
  167.     p4 * /------------/  * p1
  168.          |            |    |
  169.          |            |    |
  170.          |            |    |
  171.        p6 *  |_            / * p3
  172.          |  /         |  / 
  173.     p7 * |____________|/ * p2
  174. */
  175. // Compile and build the list
  176. glNewList(m_iDisplayLists, GL_COMPILE_AND_EXECUTE);
  177. VECTORF Normal;
  178. // Front (CCW order)
  179. glBegin(GL_TRIANGLE_STRIP);
  180. CalNormalf(m_pPointArray->m_pPoints[2].m_fOrigin,
  181.    m_pPointArray->m_pPoints[7].m_fOrigin,
  182.    m_pPointArray->m_pPoints[1].m_fOrigin,
  183.    Normal);
  184. glNormal3fv(Normal);
  185.         glTexCoord2d(1, 0.0);
  186. if(!m_bSolidColor)
  187. glColor4fv(m_pPointArray->m_pPoints[2].m_Color.m_fColor);
  188. glVertex3fv(m_pPointArray->m_pPoints[2].m_fOrigin);
  189.         glTexCoord2d(1, 1);
  190. if(!m_bSolidColor)
  191. glColor4fv(m_pPointArray->m_pPoints[1].m_Color.m_fColor);
  192. glVertex3fv(m_pPointArray->m_pPoints[1].m_fOrigin);
  193. glTexCoord2d(0.0, 0.0);
  194. if(!m_bSolidColor)
  195. glColor4fv(m_pPointArray->m_pPoints[7].m_Color.m_fColor);
  196. glVertex3fv(m_pPointArray->m_pPoints[7].m_fOrigin);
  197. CalNormalf(m_pPointArray->m_pPoints[4].m_fOrigin,
  198.    m_pPointArray->m_pPoints[1].m_fOrigin,
  199.    m_pPointArray->m_pPoints[7].m_fOrigin,
  200.    Normal);
  201. glNormal3fv(Normal);
  202. glTexCoord2d(0.0, 1);
  203. if(!m_bSolidColor)
  204. glColor4fv(m_pPointArray->m_pPoints[4].m_Color.m_fColor);
  205. glVertex3fv(m_pPointArray->m_pPoints[4].m_fOrigin);
  206. glEnd();
  207. // Back
  208. glBegin(GL_TRIANGLE_STRIP);
  209. CalNormalf(m_pPointArray->m_pPoints[6].m_fOrigin,
  210.    m_pPointArray->m_pPoints[3].m_fOrigin,
  211.    m_pPointArray->m_pPoints[5].m_fOrigin,
  212.    Normal);
  213. glNormal3fv(Normal);
  214. glTexCoord2d(1, 0.0);
  215. if(!m_bSolidColor)
  216. glColor4fv(m_pPointArray->m_pPoints[6].m_Color.m_fColor);
  217. glVertex3fv(m_pPointArray->m_pPoints[6].m_fOrigin);
  218.         glTexCoord2d(1, 1);
  219. if(!m_bSolidColor)
  220. glColor4fv(m_pPointArray->m_pPoints[5].m_Color.m_fColor);
  221. glVertex3fv(m_pPointArray->m_pPoints[5].m_fOrigin);
  222.         glTexCoord2d(0.0, 0.0);
  223. if(!m_bSolidColor)
  224. glColor4fv(m_pPointArray->m_pPoints[3].m_Color.m_fColor);
  225. glVertex3fv(m_pPointArray->m_pPoints[3].m_fOrigin);
  226. CalNormalf(m_pPointArray->m_pPoints[0].m_fOrigin,
  227.    m_pPointArray->m_pPoints[5].m_fOrigin,
  228.    m_pPointArray->m_pPoints[3].m_fOrigin,
  229.    Normal);
  230. glNormal3fv(Normal);
  231.         glTexCoord2d(0.0, 1);
  232. if(!m_bSolidColor)
  233. glColor4fv(m_pPointArray->m_pPoints[0].m_Color.m_fColor);
  234. glVertex3fv(m_pPointArray->m_pPoints[0].m_fOrigin);
  235. glEnd();
  236. // Left
  237. glBegin(GL_TRIANGLE_STRIP);
  238. CalNormalf(m_pPointArray->m_pPoints[7].m_fOrigin,
  239.    m_pPointArray->m_pPoints[6].m_fOrigin,
  240.    m_pPointArray->m_pPoints[4].m_fOrigin,
  241.    Normal);
  242. glNormal3fv(Normal);
  243. glTexCoord2d(1, 0.0);
  244. if(!m_bSolidColor)
  245. glColor4fv(m_pPointArray->m_pPoints[7].m_Color.m_fColor);
  246. glVertex3fv(m_pPointArray->m_pPoints[7].m_fOrigin);
  247.         glTexCoord2d(1, 1);
  248. if(!m_bSolidColor)
  249. glColor4fv(m_pPointArray->m_pPoints[4].m_Color.m_fColor);
  250. glVertex3fv(m_pPointArray->m_pPoints[4].m_fOrigin);
  251.         glTexCoord2d(0.0, 0.0);
  252. if(!m_bSolidColor)
  253. glColor4fv(m_pPointArray->m_pPoints[6].m_Color.m_fColor);
  254. glVertex3fv(m_pPointArray->m_pPoints[6].m_fOrigin);
  255. CalNormalf(m_pPointArray->m_pPoints[5].m_fOrigin,
  256.    m_pPointArray->m_pPoints[4].m_fOrigin,
  257.    m_pPointArray->m_pPoints[6].m_fOrigin,
  258.    Normal);
  259. glNormal3fv(Normal);
  260.         glTexCoord2d(0.0, 1);
  261. if(!m_bSolidColor)
  262. glColor4fv(m_pPointArray->m_pPoints[5].m_Color.m_fColor);
  263. glVertex3fv(m_pPointArray->m_pPoints[5].m_fOrigin);
  264. glEnd();
  265. // Right
  266. glBegin(GL_TRIANGLE_STRIP);
  267. CalNormalf(m_pPointArray->m_pPoints[3].m_fOrigin,
  268.    m_pPointArray->m_pPoints[2].m_fOrigin,
  269.    m_pPointArray->m_pPoints[0].m_fOrigin,
  270.    Normal);
  271. glNormal3fv(Normal);
  272. glTexCoord2d(1, 0.0);
  273. if(!m_bSolidColor)
  274. glColor4fv(m_pPointArray->m_pPoints[3].m_Color.m_fColor);
  275. glVertex3fv(m_pPointArray->m_pPoints[3].m_fOrigin);
  276.         glTexCoord2d(1, 1);
  277. if(!m_bSolidColor)
  278. glColor4fv(m_pPointArray->m_pPoints[0].m_Color.m_fColor);
  279. glVertex3fv(m_pPointArray->m_pPoints[0].m_fOrigin);
  280.         glTexCoord2d(0.0, 0.0);
  281. if(!m_bSolidColor)
  282. glColor4fv(m_pPointArray->m_pPoints[2].m_Color.m_fColor);
  283. glVertex3fv(m_pPointArray->m_pPoints[2].m_fOrigin);
  284. CalNormalf(m_pPointArray->m_pPoints[1].m_fOrigin,
  285.    m_pPointArray->m_pPoints[0].m_fOrigin,
  286.    m_pPointArray->m_pPoints[2].m_fOrigin,
  287.    Normal);
  288. glNormal3fv(Normal);
  289. glTexCoord2d(0.0, 1);
  290. if(!m_bSolidColor)
  291. glColor4fv(m_pPointArray->m_pPoints[1].m_Color.m_fColor);
  292. glVertex3fv(m_pPointArray->m_pPoints[1].m_fOrigin);
  293. glEnd();
  294. // Top
  295. glBegin(GL_TRIANGLE_STRIP);
  296. CalNormalf(m_pPointArray->m_pPoints[1].m_fOrigin,
  297.    m_pPointArray->m_pPoints[4].m_fOrigin,
  298.    m_pPointArray->m_pPoints[0].m_fOrigin,
  299.    Normal);
  300. glNormal3fv(Normal);
  301. glTexCoord2d(1, 0.0);
  302. if(!m_bSolidColor)
  303. glColor4fv(m_pPointArray->m_pPoints[1].m_Color.m_fColor);
  304. glVertex3fv(m_pPointArray->m_pPoints[1].m_fOrigin);
  305.         glTexCoord2d(1, 1);
  306. if(!m_bSolidColor)
  307. glColor4fv(m_pPointArray->m_pPoints[0].m_Color.m_fColor);
  308. glVertex3fv(m_pPointArray->m_pPoints[0].m_fOrigin);
  309.         glTexCoord2d(0.0, 0.0);
  310. if(!m_bSolidColor)
  311. glColor4fv(m_pPointArray->m_pPoints[4].m_Color.m_fColor);
  312. glVertex3fv(m_pPointArray->m_pPoints[4].m_fOrigin);
  313. CalNormalf(m_pPointArray->m_pPoints[5].m_fOrigin,
  314.    m_pPointArray->m_pPoints[0].m_fOrigin,
  315.    m_pPointArray->m_pPoints[4].m_fOrigin,
  316.    Normal);
  317. glNormal3fv(Normal);
  318. glTexCoord2d(0.0, 1);
  319. if(!m_bSolidColor)
  320. glColor4fv(m_pPointArray->m_pPoints[5].m_Color.m_fColor);
  321. glVertex3fv(m_pPointArray->m_pPoints[5].m_fOrigin);
  322. glEnd();
  323. // Bottom
  324. glBegin(GL_TRIANGLE_STRIP);
  325. CalNormalf(m_pPointArray->m_pPoints[3].m_fOrigin,
  326.    m_pPointArray->m_pPoints[6].m_fOrigin,
  327.    m_pPointArray->m_pPoints[2].m_fOrigin,
  328.    Normal);
  329. glNormal3fv(Normal);
  330. glTexCoord2d(1, 0.0);
  331. if(!m_bSolidColor)
  332. glColor4fv(m_pPointArray->m_pPoints[3].m_Color.m_fColor);
  333. glVertex3fv(m_pPointArray->m_pPoints[3].m_fOrigin);
  334.         glTexCoord2d(1, 1);
  335. if(!m_bSolidColor)
  336. glColor4fv(m_pPointArray->m_pPoints[2].m_Color.m_fColor);
  337. glVertex3fv(m_pPointArray->m_pPoints[2].m_fOrigin);
  338.         glTexCoord2d(0.0, 0.0);
  339. if(!m_bSolidColor)
  340. glColor4fv(m_pPointArray->m_pPoints[6].m_Color.m_fColor);
  341. glVertex3fv(m_pPointArray->m_pPoints[6].m_fOrigin);
  342. CalNormalf(m_pPointArray->m_pPoints[7].m_fOrigin,
  343.    m_pPointArray->m_pPoints[2].m_fOrigin,
  344.    m_pPointArray->m_pPoints[6].m_fOrigin,
  345.    Normal);
  346. glNormal3fv(Normal);
  347.         glTexCoord2d(0.0, 1);
  348. if(!m_bSolidColor)
  349. glColor4fv(m_pPointArray->m_pPoints[7].m_Color.m_fColor);
  350. glVertex3fv(m_pPointArray->m_pPoints[7].m_fOrigin);
  351. glEnd();
  352. glEndList();
  353. }
  354. /////////////////////////////////////////////////////////////////////////////
  355. // C3dObjectCube Methods or Implementation
  356. void C3dObjectCube::InitVertices()
  357. {
  358. // Our cube is defined by eight (8) vertices, or points, as follows:
  359. /*
  360.          p5 *  ____________  * p0
  361.              /            /
  362.            /            /  |           
  363.     p4 * /------------/  * p1
  364.          |            |    |
  365.          |            |    |
  366.          |            |    |
  367.        p6 *  |_            / * p3
  368.          |  /         |  / 
  369.     p7 * |____________|/ * p2
  370. */
  371. C3dPoint* pPoint = m_pPointArray->m_pPoints;
  372. // Initialize our cube's vertice points p0-p3
  373. pPoint->m_fOrigin[X] = m_fWidth/2; 
  374. pPoint->m_fOrigin[Y] = m_fDepth/2;
  375. pPoint->m_fOrigin[Z] = m_fHeight/2;
  376. pPoint++;
  377. pPoint->m_fOrigin[X] = m_fWidth/2;
  378. pPoint->m_fOrigin[Y] = -m_fDepth/2;
  379. pPoint->m_fOrigin[Z] = m_fHeight/2;
  380. pPoint++;
  381. pPoint->m_fOrigin[X] = m_fWidth/2;
  382. pPoint->m_fOrigin[Y] = -m_fDepth/2;
  383. pPoint->m_fOrigin[Z] = -m_fHeight/2;
  384. pPoint++;
  385. pPoint->m_fOrigin[X] = m_fWidth/2;
  386. pPoint->m_fOrigin[Y] = m_fDepth/2;
  387. pPoint->m_fOrigin[Z] = -m_fHeight/2;
  388. // Initialize our cube's vertice points p4-p7
  389. pPoint++;
  390. pPoint->m_fOrigin[X] = -m_fWidth/2; 
  391. pPoint->m_fOrigin[Y] = -m_fDepth/2;
  392. pPoint->m_fOrigin[Z] = m_fHeight/2;
  393. pPoint++;
  394. pPoint->m_fOrigin[X] = -m_fWidth/2;
  395. pPoint->m_fOrigin[Y] = m_fDepth/2;
  396. pPoint->m_fOrigin[Z] = m_fHeight/2;
  397. pPoint++;
  398. pPoint->m_fOrigin[X] = -m_fWidth/2;
  399. pPoint->m_fOrigin[Y] = m_fDepth/2;
  400. pPoint->m_fOrigin[Z] = -m_fHeight/2;
  401. pPoint++;
  402. pPoint->m_fOrigin[X] = -m_fWidth/2;
  403. pPoint->m_fOrigin[Y] = -m_fDepth/2;
  404. pPoint->m_fOrigin[Z] = -m_fHeight/2;
  405. }