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

OpenGL

开发平台:

Visual C++

  1. // 3DBezierSurface.cpp: implementation of the C3DBezierSurface class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "jhy3d.h"
  6. #include "3DBezierSurface.h"
  7. #include "Material.h"
  8. #include "Vector3.h"
  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. C3DBezierSurface::C3DBezierSurface()
  19. {
  20. m_position.m_x = m_position.m_y = m_position.m_z = 0.0f;
  21. m_pCtrlPoints = NULL;
  22. m_nNumOFVCtrl = m_nNumOFUCtrl = 0;
  23. }
  24. C3DBezierSurface::~C3DBezierSurface()
  25. {
  26. }
  27. void C3DBezierSurface::DestroyBezierSurface()
  28. {
  29. if(m_pCtrlPoints)
  30. {
  31. delete[] m_pCtrlPoints;
  32. m_pCtrlPoints = NULL;
  33. }
  34. }
  35. void C3DBezierSurface::DefaultBezierSuf()
  36. {
  37. if(m_pCtrlPoints)
  38. {
  39. DestroyBezierSurface();
  40. }
  41. m_nNumOFVCtrl = m_nNumOFUCtrl = 4;
  42. m_pCtrlPoints = new float[m_nNumOFUCtrl*m_nNumOFVCtrl*3];
  43. m_pCtrlPoints[0] = -0.1875; m_pCtrlPoints[1] = -0.1875; m_pCtrlPoints[2] = 0.5;
  44. m_pCtrlPoints[3] = -0.0625; m_pCtrlPoints[4] = -0.1875; m_pCtrlPoints[5] = 0.25;
  45. m_pCtrlPoints[6] = 0.0625;  m_pCtrlPoints[7] = -0.1875; m_pCtrlPoints[8] = -0.125;
  46. m_pCtrlPoints[9] = 0.1875;  m_pCtrlPoints[10] = -0.1875;m_pCtrlPoints[11] = 0.25;
  47. m_pCtrlPoints[12] = -0.1875;m_pCtrlPoints[13]=-0.0625;  m_pCtrlPoints[14] = 0.125;
  48. m_pCtrlPoints[15] = -0.0625;m_pCtrlPoints[16]=-0.0625; m_pCtrlPoints[17]=0.375;
  49. m_pCtrlPoints[18]=0.0625;  m_pCtrlPoints[19]=-0.0625;  m_pCtrlPoints[20]=0.0;
  50. m_pCtrlPoints[21]=0.1875;  m_pCtrlPoints[22]=-0.0625;  m_pCtrlPoints[23]=-0.125;
  51. m_pCtrlPoints[24]=-0.1875; m_pCtrlPoints[25]=0.0625;  m_pCtrlPoints[26]= 0.5;
  52. m_pCtrlPoints[27]=-0.0625; m_pCtrlPoints[28]=0.0625;  m_pCtrlPoints[29]=0.0;
  53. m_pCtrlPoints[30]=0.0625;  m_pCtrlPoints[31]=0.0625;  m_pCtrlPoints[32]=0.375;
  54. m_pCtrlPoints[33]=0.1875;  m_pCtrlPoints[34]=0.0625;  m_pCtrlPoints[35]=0.5;
  55. m_pCtrlPoints[36]=-0.1875; m_pCtrlPoints[37]=0.1875;  m_pCtrlPoints[38]=-0.25;
  56. m_pCtrlPoints[39]=-0.0625; m_pCtrlPoints[40]=0.1875;  m_pCtrlPoints[41]=-0.25;
  57. m_pCtrlPoints[42]=0.0625;  m_pCtrlPoints[43]=0.1875;  m_pCtrlPoints[44]=0.0;
  58. m_pCtrlPoints[45]=0.1875;  m_pCtrlPoints[46]=0.1875;  m_pCtrlPoints[47]=-0.125;
  59. }
  60. void C3DBezierSurface::RenderTextureSolidBeizerSurface()
  61. {
  62. if(m_pCtrlPoints == NULL)
  63. return;
  64. float sTexCoords[2][2][2] = {{{0.0,0.0},{0.0,1.0}},{{1.0,1.0}}};
  65. glEnable(GL_MAP2_VERTEX_3);
  66. glEnable(GL_MAP2_TEXTURE_COORD_2);
  67. glEnable(GL_AUTO_NORMAL);
  68. glEnable(GL_NORMALIZE);
  69. glMaterialfv(GL_FRONT,GL_AMBIENT,m_matBezier.m_vAmbient);
  70. glMaterialfv(GL_FRONT,GL_DIFFUSE,m_matBezier.m_vDiffuse);
  71. glMaterialfv(GL_FRONT,GL_SPECULAR,m_matBezier.m_vSpecular);
  72. glMaterialf(GL_FRONT,GL_SHININESS,m_matBezier.m_vShininess);
  73. glMaterialfv(GL_FRONT,GL_EMISSION,m_matBezier.m_vEmission);
  74. glPushMatrix();
  75. glTranslatef(m_position.m_x,m_position.m_y,m_position.m_z);
  76. glMap2f(GL_MAP2_VERTEX_3,0,1,3,m_nNumOFUCtrl,0,1,m_nNumOFUCtrl * 3,m_nNumOFVCtrl,m_pCtrlPoints);
  77. glMap2f(GL_MAP2_TEXTURE_COORD_2,0,1,2,2,0,1,4,2,&sTexCoords[0][0][0]);
  78. glMapGrid2f(20,0,1,20,0,1);
  79. glEvalMesh2(GL_FILL,0,20,0,20);
  80. glPopMatrix();
  81. glDisable(GL_AUTO_NORMAL);
  82. glDisable(GL_NORMALIZE);
  83. }