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

OpenGL

开发平台:

Visual C++

  1. /////////////////////////////////////////////////////////////////////////////
  2. // 3dPointArray.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. #ifdef _DEBUG
  22. #define new DEBUG_NEW
  23. #undef THIS_FILE
  24. static char THIS_FILE[] = __FILE__;
  25. #endif
  26. //////////////////////////////////////////////////////////////////
  27. // C3dPointArray
  28. IMPLEMENT_DYNAMIC(C3dPointArray, CObject)
  29. /////////////////////////////////////////////////////////////////////////////
  30. // C3dPointArray construction
  31. C3dPointArray::C3dPointArray()
  32. {
  33. // Initialize our data members
  34. m_hPoints = NULL;
  35. m_pPoints = NULL;
  36. m_iNumPoints = 0;
  37. }
  38. /////////////////////////////////////////////////////////////////////////////
  39. // C3dPointArray Destructor
  40. C3dPointArray::~C3dPointArray()
  41. {
  42. // Free our points array memory
  43. if(m_hPoints)
  44. {
  45. GlobalUnlock(m_hPoints);
  46. GlobalFree(m_hPoints);
  47. m_hPoints = NULL;
  48. m_pPoints = NULL;
  49. }
  50. }
  51. /////////////////////////////////////////////////////////////////////////////
  52. // C3dPointArray function implimentation
  53. BOOL C3dPointArray::Create(int iNumPoints)
  54. {
  55. if(!iNumPoints)
  56. return -1;
  57. m_iNumPoints = iNumPoints;
  58. m_hPoints = GlobalAlloc(GPTR, iNumPoints*sizeof(C3dPoint));
  59. if(m_hPoints == NULL) {
  60. AfxMessageBox("Not enought memory to create Point Array!", MB_OK, NULL);
  61. return(-1);
  62. }
  63. m_pPoints = (C3dPoint*)GlobalLock(m_hPoints);
  64. if(m_pPoints == NULL) {
  65. AfxMessageBox("Not enought memory to create Point Array!", MB_OK, NULL);
  66. GlobalFree(m_hPoints);
  67. m_hPoints = NULL;
  68. return(-1);
  69. }
  70. return 0;
  71. }
  72. BOOL C3dPointArray::SetArraySize(int iNumPoints)
  73. {
  74. if(!iNumPoints)
  75. return -1;
  76. // Unlock the memory handle
  77. GlobalUnlock(m_hPoints);
  78. // Reallocate the memory block
  79. m_hPoints = GlobalReAlloc(m_hPoints, iNumPoints*sizeof(C3dPoint), GMEM_MOVEABLE | GMEM_ZEROINIT);
  80. if(m_hPoints == NULL) {
  81. AfxMessageBox("Not enought memory to resize Point Array!", MB_OK, NULL);
  82. return(-1);
  83. }
  84. m_pPoints = (C3dPoint*)GlobalLock(m_hPoints);
  85. if(m_pPoints == NULL) {
  86. AfxMessageBox("Not enought memory to resize Point Array!", MB_OK, NULL);
  87. GlobalFree(m_hPoints);
  88. m_hPoints = NULL;
  89. return(-1);
  90. }
  91. // Save the number of points in our array
  92. m_iNumPoints = iNumPoints;
  93. return 0;
  94. }
  95. BOOL C3dPointArray::Add(float x, float y, float z)
  96. {
  97. // Unlock the memory handle
  98. GlobalUnlock(m_hPoints);
  99. // Reallocate the memory block
  100. m_hPoints = GlobalReAlloc(m_hPoints, (m_iNumPoints+1)*sizeof(C3dPoint), GMEM_MOVEABLE | GMEM_ZEROINIT);
  101. if(m_hPoints == NULL) {
  102. AfxMessageBox("Not enought memory to add Point to Point Array!", MB_OK, NULL);
  103. return(-1);
  104. }
  105. m_pPoints = (C3dPoint*)GlobalLock(m_hPoints);
  106. if(m_pPoints == NULL) {
  107. AfxMessageBox("Not enought memory to add Point to Point Array!", MB_OK, NULL);
  108. GlobalFree(m_hPoints);
  109. m_hPoints = NULL;
  110. return(-1);
  111. }
  112. // We sucessfully increased the size of the array, so add the point
  113. m_pPoints[m_iNumPoints].SetOrigin(x, y, z);
  114. // Increment the number of points in the array
  115. m_iNumPoints++;
  116. return 0;
  117. }
  118. BOOL C3dPointArray::CopyFromList(C3dPointList* pPointList)
  119. {
  120. ASSERT(pPointList);
  121. if(pPointList->GetCount() > m_iNumPoints)
  122. return FALSE;
  123. // Copy the points from the point list to our
  124. // members point/vertice array
  125. C3dPoint* pPoint = NULL;
  126. int iPoint = 0;
  127. // walk the list
  128. POSITION Pos = pPointList->GetHeadPosition();
  129. while (Pos) {
  130. pPoint = pPointList->GetAt(Pos);
  131. m_pPoints[iPoint].m_fOrigin[0] = pPoint->m_fOrigin[0];
  132. m_pPoints[iPoint].m_fOrigin[1] = pPoint->m_fOrigin[1];
  133. m_pPoints[iPoint].m_fOrigin[2] = pPoint->m_fOrigin[2];
  134. pPoint = pPointList->GetNext(Pos);
  135. ++iPoint;
  136. }
  137. return 0;
  138. }
  139. void C3dPointArray::Display(C3dWorld* pWorld, C3dCamera* pCamera, C3dObject* pObject, float fPointSize, BOOL bConnectPoints){
  140. GLfloat fPointColor[4]   = { 0.0f, 0.6f, 0.2f, 1.0f }; // Dark Green
  141. GLfloat fSelectedColor[4] = { 0.9f, 0.9f, 0.0f, 1.0f }; // Yellow
  142. GLfloat fLineColor[4]   = { 0.5f, 0.6f, 0.2f, 1.0f }; // Light Green
  143. GLfloat fLastOrigin[4];
  144. BOOL bMoreThanOnePoint = FALSE;
  145. VECTORF CameraPosn, PointPosn, CameraToPoint;
  146. float distance;
  147. GLfloat fRadius;
  148. Matx4x4 ObjXformMatrix;
  149. float fPointScale = fPointSize/.01f;
  150. // Transform the camera's origin to world coordinates
  151. Transformf(pCamera->m_fOrigin, CameraPosn, pCamera->m_dModelViewMatrix);
  152. // Get the objects transformation matrix.  We will use this to
  153. // transform each point in the array to world coordinates.
  154. pObject->GetTransformMatrix(ObjXformMatrix);
  155. // Disable lighting calculations
  156. glDisable(GL_LIGHTING);
  157. glLineWidth(2.0f);
  158. for(int i=0; i<m_iNumPoints; i++)
  159. {
  160. // Calculate the points world position
  161. VecTransformf(m_pPoints[i].m_fOrigin, PointPosn, ObjXformMatrix);
  162. // Subtract the cameras' world coordinate from the points's 
  163. // world coordinate, then calculate the distance between them.
  164. VecSubf(CameraPosn, PointPosn, CameraToPoint);
  165. distance = VecLenf(CameraToPoint);
  166. // Now that we have the distance between the camera and the point, 
  167. // calculate our points' radius.  (We don't want the point to
  168. // be too small or too large)
  169. fRadius = distance/fPointScale;
  170. if(pWorld->m_pSelectedPnt == &m_pPoints[i])
  171. m_pPoints[i].Display(fRadius, fSelectedColor, TRUE);
  172. else
  173. m_pPoints[i].Display(fRadius, fPointColor, TRUE);
  174. glColor4fv(fLineColor);
  175. if(bConnectPoints && bMoreThanOnePoint) {
  176. glBegin(GL_LINES);
  177. glVertex3fv(fLastOrigin);
  178. glVertex3fv(m_pPoints[i].m_fOrigin);
  179. glEnd();
  180. }
  181. fLastOrigin[0] = m_pPoints[i].m_fOrigin[0];
  182. fLastOrigin[1] = m_pPoints[i].m_fOrigin[1];
  183. fLastOrigin[2] = m_pPoints[i].m_fOrigin[2];
  184. fLastOrigin[3] = m_pPoints[i].m_fOrigin[3];
  185. bMoreThanOnePoint = TRUE;
  186. }
  187. // Enable lighting calculations
  188. glEnable(GL_LIGHTING);
  189. glLineWidth(1.0f);
  190. }
  191. C3dPoint* C3dPointArray::Find(C3dCamera* pCamera, C3dObject* pObject, 
  192.   float x, float y, float z)
  193. {
  194. static C3dPoint* pLastPoint = NULL;
  195. BOOL bFoundMatch = FALSE;
  196. Matx4x4 ObjXformMatrix;
  197. // Get the objects transformation matrix.  We will use this to
  198. // transform each point in the array to world coordinates.
  199. pObject->GetTransformMatrix(ObjXformMatrix);
  200. // Search the array of C3dPoints to find a match
  201. for(int i=0; i<m_iNumPoints; i++)
  202. {
  203. if(m_pPoints[i].IsPoint(pCamera, ObjXformMatrix, x, y, z))
  204. {
  205. bFoundMatch = TRUE;
  206. if(&m_pPoints[i] != pLastPoint)
  207. {
  208. pLastPoint = &m_pPoints[i];
  209. return &m_pPoints[i];
  210. }
  211. }
  212. }
  213. // We found only one point in the array matching the xyz value
  214. if(bFoundMatch)
  215. return pLastPoint;
  216. return NULL;
  217. }
  218. void C3dPointArray::GetMinMax(C3dBoundingBox* pBox)
  219. {
  220. C3dPoint* pPoint;
  221. // Set to large positive number
  222. pBox->m_fMin[X] = (GLfloat)LARGE_NUMBER;
  223. pBox->m_fMin[Y] = (GLfloat)LARGE_NUMBER;
  224. pBox->m_fMin[Z] = (GLfloat)LARGE_NUMBER;
  225. // Set to large negative number
  226. pBox->m_fMax[X] = -(GLfloat)LARGE_NUMBER;
  227. pBox->m_fMax[Y] = -(GLfloat)LARGE_NUMBER;
  228. pBox->m_fMax[Z] = -(GLfloat)LARGE_NUMBER;
  229. // Search through the array to find the min/max
  230. // object coordinates
  231. for(int i=0; i<m_iNumPoints; i++)
  232. {
  233. pPoint = &m_pPoints[i];
  234. if(pPoint->m_fOrigin[X] > pBox->m_fMax[X])
  235. pBox->m_fMax[X] = pPoint->m_fOrigin[X];
  236. if(pPoint->m_fOrigin[Y] > pBox->m_fMax[Y])
  237. pBox->m_fMax[Y] = pPoint->m_fOrigin[Y];
  238. if(pPoint->m_fOrigin[Z] > pBox->m_fMax[Z])
  239. pBox->m_fMax[Z] = pPoint->m_fOrigin[Z];
  240. if(pPoint->m_fOrigin[X] < pBox->m_fMin[X])
  241. pBox->m_fMin[X] = pPoint->m_fOrigin[X];
  242. if(pPoint->m_fOrigin[Y] < pBox->m_fMin[Y])
  243. pBox->m_fMin[Y] = pPoint->m_fOrigin[Y];
  244. if(pPoint->m_fOrigin[Z] < pBox->m_fMin[Z])
  245. pBox->m_fMin[Z] = pPoint->m_fOrigin[Z];
  246. }
  247. }
  248. void C3dPointArray::Serialize(CArchive& ar, int iVersion, BOOL bReadColorData)
  249. {
  250. CString szBuffer;
  251. int i;
  252. if (ar.IsStoring())
  253. {
  254. for(i=0; i<m_iNumPoints; i++)
  255. {
  256. szBuffer.Format("%stt        < %f, %f, %f >n", szIndent, m_pPoints[i].m_fOrigin[0],
  257. m_pPoints[i].m_fOrigin[1],
  258. m_pPoints[i].m_fOrigin[2]);
  259. ar.WriteString(szBuffer);
  260. }
  261. if(bReadColorData)
  262. {
  263. szBuffer.Format("%st[color]n", szIndent);
  264. ar.WriteString(szBuffer);
  265. for(i=0; i<m_iNumPoints; i++)
  266. {
  267. szBuffer.Format("%stt        < %f, %f, %f, %f >n", szIndent, m_pPoints[i].m_Color.m_fColor[0],
  268. m_pPoints[i].m_Color.m_fColor[1],
  269. m_pPoints[i].m_Color.m_fColor[2],
  270. m_pPoints[i].m_Color.m_fColor[3]);
  271. ar.WriteString(szBuffer);
  272. }
  273. }
  274. }
  275. else
  276. {
  277. // Read the shape vertice, or point data
  278. for(i=0; i<m_iNumPoints; i++)
  279. {
  280. ar.ReadString(szBuffer);
  281. szBuffer.TrimLeft();
  282. sscanf(szBuffer, "< %f, %f, %f >n", &m_pPoints[i].m_fOrigin[X],
  283.  &m_pPoints[i].m_fOrigin[Y],
  284.  &m_pPoints[i].m_fOrigin[Z]);
  285. }
  286. if(iVersion > 110 && bReadColorData)
  287. {
  288. ar.ReadString(szBuffer); // [color] header
  289. // Read the shape vertice, or point data
  290. for(int i=0; i<m_iNumPoints; i++)
  291. {
  292. ar.ReadString(szBuffer);
  293. szBuffer.TrimLeft();
  294. sscanf(szBuffer, "< %f, %f, %f, %f >n", &m_pPoints[i].m_Color.m_fColor[0],
  295.  &m_pPoints[i].m_Color.m_fColor[1],
  296.  &m_pPoints[i].m_Color.m_fColor[2],
  297.  &m_pPoints[i].m_Color.m_fColor[3]);
  298. }
  299. }
  300. }
  301. }