3DNurbsSurface.cpp
上传用户:eehhbb
上传日期:2022-08-03
资源大小:2550k
文件大小:4k
源码类别:

OpenGL

开发平台:

Visual C++

  1. // 3DNurbsSurface.cpp: implementation of the C3DNurbsSurface class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "jhy3d.h"
  6. #include "3DNurbsSurface.h"
  7. #include "material.h" // Added by ClassView
  8. #include "Vector3.h" // Added by ClassView
  9. #include "glglut.h"
  10. #ifdef _DEBUG
  11. #undef THIS_FILE
  12. static char THIS_FILE[]=__FILE__;
  13. #define new DEBUG_NEW
  14. #endif
  15. //////////////////////////////////////////////////////////////////////
  16. // Construction/Destruction
  17. //////////////////////////////////////////////////////////////////////
  18. C3DNurbsSurface::C3DNurbsSurface()
  19. {
  20. m_position.m_x = m_position.m_y = m_position.m_z = 0.0f;
  21. m_uNumOfVKnot = m_uNumOfUKnot = m_uNumOfUCtrl = m_uNumOfVCtrl = 0;
  22. m_pCtrlPoints = NULL;
  23. m_pUKnot = NULL;
  24. m_pVKnot = NULL;
  25. }
  26. C3DNurbsSurface::~C3DNurbsSurface()
  27. {
  28. }
  29. void C3DNurbsSurface::DestroyNurbsSurface()
  30. {
  31. if(m_pCtrlPoints)
  32. {
  33. delete[] m_pCtrlPoints;
  34. m_pCtrlPoints = NULL;
  35. }
  36. if(m_pUKnot)
  37. {
  38. delete[] m_pUKnot;
  39. m_pUKnot = NULL;
  40. }
  41. if(m_pVKnot)
  42. {
  43. delete[] m_pVKnot;
  44. m_pVKnot = NULL;
  45. }
  46. }
  47. void C3DNurbsSurface::DefaultNurbsSuf()
  48. {
  49. if(m_pCtrlPoints || m_pUKnot || m_pVKnot)
  50. DestroyNurbsSurface();
  51. m_uNumOfVKnot = m_uNumOfUKnot = 8;
  52. m_uNumOfVCtrl = m_uNumOfUCtrl = 4;
  53. m_pCtrlPoints = new float[m_uNumOfVCtrl*m_uNumOfUCtrl*3];
  54. m_pCtrlPoints[0] = -0.25f; m_pCtrlPoints[1] = -0.5f; m_pCtrlPoints[2] = 0.5f;
  55. m_pCtrlPoints[3] = -0.1f; m_pCtrlPoints[4] = -0.5f; m_pCtrlPoints[5] = 0.25f;
  56. m_pCtrlPoints[6] = 0.1f;  m_pCtrlPoints[7] = -0.5f; m_pCtrlPoints[8] = -0.0f;
  57. m_pCtrlPoints[9] = 0.5f;  m_pCtrlPoints[10] = -0.5f;m_pCtrlPoints[11] = 0.25f;
  58. m_pCtrlPoints[12] = -0.25f;m_pCtrlPoints[13]=-0.25;  m_pCtrlPoints[14] = 0.5f;
  59. m_pCtrlPoints[15] = -0.1f;m_pCtrlPoints[16]=-0.25; m_pCtrlPoints[17]=0.25f;
  60. m_pCtrlPoints[18]=0.1f;  m_pCtrlPoints[19]=-0.25f;  m_pCtrlPoints[20]=0.0f;
  61. m_pCtrlPoints[21]=0.5f;  m_pCtrlPoints[22]=-0.25f;  m_pCtrlPoints[23]=-0.25f;
  62. m_pCtrlPoints[24]=-0.25f; m_pCtrlPoints[25]=0.25;  m_pCtrlPoints[26]= 0.5f;
  63. m_pCtrlPoints[27]=-0.1f; m_pCtrlPoints[28]=0.25f;  m_pCtrlPoints[29]=0.25f;
  64. m_pCtrlPoints[30]=0.1f;  m_pCtrlPoints[31]=0.25f;  m_pCtrlPoints[32]=0.0f;
  65. m_pCtrlPoints[33]=0.5f;  m_pCtrlPoints[34]=0.25f;  m_pCtrlPoints[35]=0.25f;
  66. m_pCtrlPoints[36]=-0.25f; m_pCtrlPoints[37]=0.5f;  m_pCtrlPoints[38]=0.5f;
  67. m_pCtrlPoints[39]=-0.25f; m_pCtrlPoints[40]=0.5f;  m_pCtrlPoints[41]=0.25f;
  68. m_pCtrlPoints[42]=0.1f;  m_pCtrlPoints[43]=0.5f;  m_pCtrlPoints[44]=0.0f;
  69. m_pCtrlPoints[45]=0.5f;  m_pCtrlPoints[46]=0.5f;  m_pCtrlPoints[47]=0.25f;
  70. m_pUKnot = new float(m_uNumOfUKnot);
  71. for(unsigned i = 0;i<m_uNumOfUKnot/2;i++)
  72. m_pUKnot[i] = 0.0f;
  73. for(i = m_uNumOfUKnot/2;i<m_uNumOfUKnot;i++)
  74. m_pUKnot[i] = 1.0f;
  75. m_pVKnot = new float(m_uNumOfVKnot);
  76. for(i = 0;i<m_uNumOfVKnot/2;i++)
  77. m_pVKnot[i] = 0.0f;
  78. for(i = m_uNumOfVKnot/2;i<m_uNumOfVKnot;i++)
  79. m_pVKnot[i] = 1.0f;
  80. }
  81. void C3DNurbsSurface::RenderWireNurbsSurface()
  82. {
  83. if(m_pCtrlPoints == NULL)
  84. return;
  85. glColor3f(m_matNurbs.m_vDiffuse[0],m_matNurbs.m_vDiffuse[1],m_matNurbs.m_vDiffuse[2]);
  86. GLUnurbs *theNurb;
  87. theNurb = gluNewNurbsRenderer();
  88. gluNurbsProperty(theNurb,GLU_SAMPLING_TOLERANCE,25.0);
  89. gluNurbsProperty(theNurb,GLU_DISPLAY_MODE,GLU_OUTLINE_POLYGON);
  90. glPushMatrix();
  91. glTranslatef(m_position.m_x,m_position.m_y,m_position.m_z);
  92. gluBeginSurface(theNurb);
  93. gluNurbsSurface(theNurb,
  94. m_uNumOfUKnot,m_pUKnot,
  95. m_uNumOfVKnot,m_pVKnot,
  96. 3,
  97. m_uNumOfUCtrl*3,
  98. m_pCtrlPoints,
  99. m_uNumOfUCtrl,m_uNumOfVCtrl,
  100. GL_MAP2_VERTEX_3);
  101. gluEndSurface(theNurb);
  102. glPopMatrix();
  103. gluDeleteNurbsRenderer(theNurb);
  104. }