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

OpenGL

开发平台:

Visual C++

  1. /////////////////////////////////////////////////////////////////////////////
  2. // 3dGrid.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 "math.h"
  22. #ifdef _DEBUG
  23. #define new DEBUG_NEW
  24. #undef THIS_FILE
  25. static char THIS_FILE[] = __FILE__;
  26. #endif
  27. /////////////////////////////////////////////////////////////////////////////
  28. // C3dGrid
  29. IMPLEMENT_DYNAMIC(C3dGrid, CObject)
  30. /////////////////////////////////////////////////////////////////////////////
  31. // C3dGrid construction
  32. C3dGrid::C3dGrid()
  33. {
  34. // Assign Default values to member attributes
  35. m_fGridWidth = 20.0f; 
  36. m_fGridDepth = 20.0f; 
  37. m_fSpacing  = 1.0f;
  38. m_fLineWidth = 0.05f;
  39. m_fGridColor[0] = 0.50f; // light gray
  40. m_fGridColor[1] = 0.50f;
  41. m_fGridColor[2] = 0.50f;
  42. m_fGridColor[3] = 1.00f;
  43. }
  44. /////////////////////////////////////////////////////////////////////////////
  45. // C3dGrid Destructor
  46. C3dGrid::~C3dGrid()
  47. {
  48. }
  49. C3dGrid* C3dGrid::Create(float fSpacing)
  50. {
  51. C3dGrid* pGrid = new C3dGrid;
  52. ASSERT_VALID(pGrid);
  53. if(fSpacing)
  54. pGrid->m_fSpacing = (GLfloat)fSpacing;
  55. return pGrid;
  56. }
  57. void C3dGrid::SetSize(float fSpacing)
  58. {
  59. m_fSpacing = (GLfloat)fSpacing;
  60. }
  61. void C3dGrid::PointToGrid(float *x, float *y, float *z)
  62. {
  63.    *x=Roundf(*x/m_fSpacing)*m_fSpacing;
  64.    *y=Roundf(*y/m_fSpacing)*m_fSpacing;
  65.    *z=Roundf(*z/m_fSpacing)*m_fSpacing;
  66. }
  67. void C3dGrid::Display(C3dWorld* pWorld, C3dCamera* pCamera)
  68. {
  69. if(!pCamera)
  70. return;
  71. // Save our current state
  72. glPushAttrib(GL_ALL_ATTRIB_BITS);
  73. // Save our modelview matrix
  74. glPushMatrix();
  75. // Disable lighting calculations..
  76. glDisable(GL_LIGHTING);
  77. // Reset color parameters to default values
  78. glDisable(GL_TEXTURE_2D);
  79. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  80. glColor4fv(m_fGridColor);
  81. glLineWidth(m_fLineWidth);
  82. if(pCamera->m_iViewType == VIEW_ISO)
  83. // Grid3D(pCamera);
  84. GridXY(pCamera);
  85. else
  86. {
  87. if(pCamera->m_iViewType == VIEW_TOP)
  88. {
  89. GridXY(pCamera);
  90. }
  91. else if(pCamera->m_iViewType == VIEW_FRONT)
  92. {
  93. glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
  94. GridXY(pCamera);
  95. }
  96. else if(pCamera->m_iViewType == VIEW_SIDE)
  97. {
  98. glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
  99. glRotatef(90.0f, 0.0f, 0.0f, 1.0f);
  100. GridXY(pCamera);
  101. }
  102. }
  103. // Restore our modelview matrix
  104. glPopMatrix();
  105. // Restore our state
  106. glPopAttrib();
  107. }
  108. void C3dGrid::Grid3D(C3dCamera* pCamera)
  109. {
  110. // Draw the bottom XY grid
  111. GridXY(pCamera);
  112. // Draw the back XZ grid
  113. glPushMatrix(); // Save our modelview matrix
  114. glTranslatef(0.0f, m_fGridDepth/2, m_fGridDepth/2);
  115. glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
  116. GridXY(pCamera);
  117. glPopMatrix(); // Restore our modelview matrix
  118. // Draw the side YZ grid
  119. glPushMatrix(); // Save our modelview matrix
  120. glTranslatef(-m_fGridWidth/2, 0.0f, m_fGridWidth/2);
  121. glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
  122. GridXY(pCamera);
  123. glPopMatrix(); // Restore our modelview matrix
  124. }
  125. void C3dGrid::GridXY(C3dCamera* pCamera)
  126. {
  127. GLfloat x, y;
  128. // Draw the Vertical lines
  129. for(x=-(m_fGridWidth/2); x<=(m_fGridWidth/2); x+=m_fSpacing)
  130. {
  131. glBegin(GL_LINES);
  132. glVertex3f(x*m_fSpacing,  m_fGridDepth/2, 0.0f); // top
  133. glVertex3f(x*m_fSpacing, -m_fGridDepth/2, 0.0f); // bottom
  134. glEnd();
  135. }
  136. // Draw the Horizontal lines
  137. for(y=-(m_fGridDepth/2); y<=(m_fGridDepth/2); y+=m_fSpacing)
  138. {
  139. glBegin(GL_LINES);
  140. glVertex3f(-m_fGridWidth/2, y*m_fSpacing, 0.0f); // left
  141. glVertex3f( m_fGridWidth/2, y*m_fSpacing, 0.0f); // right
  142. glEnd();
  143. }
  144. }