3DNurbsSurface.cpp
资源名称:Jhy3D.rar [点击查看]
上传用户:eehhbb
上传日期:2022-08-03
资源大小:2550k
文件大小:4k
源码类别:
OpenGL
开发平台:
Visual C++
- // 3DNurbsSurface.cpp: implementation of the C3DNurbsSurface class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "jhy3d.h"
- #include "3DNurbsSurface.h"
- #include "material.h" // Added by ClassView
- #include "Vector3.h" // Added by ClassView
- #include "glglut.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- C3DNurbsSurface::C3DNurbsSurface()
- {
- m_position.m_x = m_position.m_y = m_position.m_z = 0.0f;
- m_uNumOfVKnot = m_uNumOfUKnot = m_uNumOfUCtrl = m_uNumOfVCtrl = 0;
- m_pCtrlPoints = NULL;
- m_pUKnot = NULL;
- m_pVKnot = NULL;
- }
- C3DNurbsSurface::~C3DNurbsSurface()
- {
- }
- void C3DNurbsSurface::DestroyNurbsSurface()
- {
- if(m_pCtrlPoints)
- {
- delete[] m_pCtrlPoints;
- m_pCtrlPoints = NULL;
- }
- if(m_pUKnot)
- {
- delete[] m_pUKnot;
- m_pUKnot = NULL;
- }
- if(m_pVKnot)
- {
- delete[] m_pVKnot;
- m_pVKnot = NULL;
- }
- }
- void C3DNurbsSurface::DefaultNurbsSuf()
- {
- if(m_pCtrlPoints || m_pUKnot || m_pVKnot)
- DestroyNurbsSurface();
- m_uNumOfVKnot = m_uNumOfUKnot = 8;
- m_uNumOfVCtrl = m_uNumOfUCtrl = 4;
- m_pCtrlPoints = new float[m_uNumOfVCtrl*m_uNumOfUCtrl*3];
- m_pCtrlPoints[0] = -0.25f; m_pCtrlPoints[1] = -0.5f; m_pCtrlPoints[2] = 0.5f;
- m_pCtrlPoints[3] = -0.1f; m_pCtrlPoints[4] = -0.5f; m_pCtrlPoints[5] = 0.25f;
- m_pCtrlPoints[6] = 0.1f; m_pCtrlPoints[7] = -0.5f; m_pCtrlPoints[8] = -0.0f;
- m_pCtrlPoints[9] = 0.5f; m_pCtrlPoints[10] = -0.5f;m_pCtrlPoints[11] = 0.25f;
- m_pCtrlPoints[12] = -0.25f;m_pCtrlPoints[13]=-0.25; m_pCtrlPoints[14] = 0.5f;
- m_pCtrlPoints[15] = -0.1f;m_pCtrlPoints[16]=-0.25; m_pCtrlPoints[17]=0.25f;
- m_pCtrlPoints[18]=0.1f; m_pCtrlPoints[19]=-0.25f; m_pCtrlPoints[20]=0.0f;
- m_pCtrlPoints[21]=0.5f; m_pCtrlPoints[22]=-0.25f; m_pCtrlPoints[23]=-0.25f;
- m_pCtrlPoints[24]=-0.25f; m_pCtrlPoints[25]=0.25; m_pCtrlPoints[26]= 0.5f;
- m_pCtrlPoints[27]=-0.1f; m_pCtrlPoints[28]=0.25f; m_pCtrlPoints[29]=0.25f;
- m_pCtrlPoints[30]=0.1f; m_pCtrlPoints[31]=0.25f; m_pCtrlPoints[32]=0.0f;
- m_pCtrlPoints[33]=0.5f; m_pCtrlPoints[34]=0.25f; m_pCtrlPoints[35]=0.25f;
- m_pCtrlPoints[36]=-0.25f; m_pCtrlPoints[37]=0.5f; m_pCtrlPoints[38]=0.5f;
- m_pCtrlPoints[39]=-0.25f; m_pCtrlPoints[40]=0.5f; m_pCtrlPoints[41]=0.25f;
- m_pCtrlPoints[42]=0.1f; m_pCtrlPoints[43]=0.5f; m_pCtrlPoints[44]=0.0f;
- m_pCtrlPoints[45]=0.5f; m_pCtrlPoints[46]=0.5f; m_pCtrlPoints[47]=0.25f;
- m_pUKnot = new float(m_uNumOfUKnot);
- for(unsigned i = 0;i<m_uNumOfUKnot/2;i++)
- m_pUKnot[i] = 0.0f;
- for(i = m_uNumOfUKnot/2;i<m_uNumOfUKnot;i++)
- m_pUKnot[i] = 1.0f;
- m_pVKnot = new float(m_uNumOfVKnot);
- for(i = 0;i<m_uNumOfVKnot/2;i++)
- m_pVKnot[i] = 0.0f;
- for(i = m_uNumOfVKnot/2;i<m_uNumOfVKnot;i++)
- m_pVKnot[i] = 1.0f;
- }
- void C3DNurbsSurface::RenderWireNurbsSurface()
- {
- if(m_pCtrlPoints == NULL)
- return;
- glColor3f(m_matNurbs.m_vDiffuse[0],m_matNurbs.m_vDiffuse[1],m_matNurbs.m_vDiffuse[2]);
- GLUnurbs *theNurb;
- theNurb = gluNewNurbsRenderer();
- gluNurbsProperty(theNurb,GLU_SAMPLING_TOLERANCE,25.0);
- gluNurbsProperty(theNurb,GLU_DISPLAY_MODE,GLU_OUTLINE_POLYGON);
- glPushMatrix();
- glTranslatef(m_position.m_x,m_position.m_y,m_position.m_z);
- gluBeginSurface(theNurb);
- gluNurbsSurface(theNurb,
- m_uNumOfUKnot,m_pUKnot,
- m_uNumOfVKnot,m_pVKnot,
- 3,
- m_uNumOfUCtrl*3,
- m_pCtrlPoints,
- m_uNumOfUCtrl,m_uNumOfVCtrl,
- GL_MAP2_VERTEX_3);
- gluEndSurface(theNurb);
- glPopMatrix();
- gluDeleteNurbsRenderer(theNurb);
- }