// NurbsSurfView.cpp : implementation of the CNurbsSurfView class
//
#include "stdafx.h"
#include "NurbsSurf.h"
#include "NurbsSurfDoc.h"
#include "NurbsSurfView.h"
//add down
#include "gl\gl.h"
#include "gl\glu.h"
#include "gl\glaux.h"
//add up
//定义控制顶点add down
GLfloat ctrlpoints[4][4][3];
GLUnurbsObj* theNurb;
//定义控制顶点add up
//add down函数说明
void init_surface(void);
//add up函数说明
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CNurbsSurfView
IMPLEMENT_DYNCREATE(CNurbsSurfView, CView)
BEGIN_MESSAGE_MAP(CNurbsSurfView, CView)
//{{AFX_MSG_MAP(CNurbsSurfView)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_ERASEBKGND()
ON_WM_SIZE()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNurbsSurfView construction/destruction
CNurbsSurfView::CNurbsSurfView()
{
//add down
m_pDC = NULL;
//add up
}
CNurbsSurfView::~CNurbsSurfView()
{
}
BOOL CNurbsSurfView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
//add down
cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
//add up
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CNurbsSurfView drawing
void CNurbsSurfView::OnDraw(CDC* pDC)
{
CNurbsSurfDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//add down
DrawScene();
//add up
}
/////////////////////////////////////////////////////////////////////////////
// CNurbsSurfView printing
BOOL CNurbsSurfView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CNurbsSurfView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CNurbsSurfView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CNurbsSurfView diagnostics
#ifdef _DEBUG
void CNurbsSurfView::AssertValid() const
{
CView::AssertValid();
}
void CNurbsSurfView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CNurbsSurfDoc* CNurbsSurfView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CNurbsSurfDoc)));
return (CNurbsSurfDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CNurbsSurfView message handlers
int CNurbsSurfView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
//add down
Init(); //初始化OpenGL
//add up
return 0;
}
void CNurbsSurfView::OnDestroy()
{
//add down
HGLRC hrc;
hrc = ::wglGetCurrentContext();
::wglMakeCurrent(NULL, NULL);
if (hrc)
::wglDeleteContext(hrc);
if (m_pDC)
delete m_pDC;
//add up
CView::OnDestroy();
}
BOOL CNurbsSurfView::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
void CNurbsSurfView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
//add down
int w=cx;
int h=cy;
//避免除数为0
if(h==0) h=1;
//设置视口与窗口匹配
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
//重新设置坐标系统
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//建立透视变换下的剪切体
gluPerspective(45.0,(GLdouble)w/(GLdouble)h,3.0,8.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 1.0, -5.0);
//add up
}
//add down
void CNurbsSurfView::Init()
{
PIXELFORMATDESCRIPTOR pfd;
int n;
HGLRC hrc;
m_pDC = new CClientDC(this);
ASSERT(m_pDC != NULL);
if (!bSetupPixelFormat())
return;
n = ::GetPixelFormat(m_pDC->GetSafeHdc());
::DescribePixelFormat(m_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd);
hrc = wglCreateContext(m_pDC->GetSafeHdc());
wglMakeCurrent(m_pDC->GetSafeHdc(), hrc);
//定义曲面材质为金色
GLfloat mat_diffuse[]={0.88,0.66,0.22,1.0};
GLfloat mat_specular[]={0.92,0.9,0.0,1.0};
GLfloat mat_shininess[]={80.0};
//将初始屏幕设置为白色
glClearColor(1.0,1.0,1.0,1.0);
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
init_surface();
theNurb=gluNewNurbsRenderer();
gluNurbsProperty(theNurb,GLU_SAMPLING_TOLERANCE,25.0);
gluNurbsProperty(theNurb,GLU_DISPLAY_MODE,GLU_FILL);
}
BOOL CNurbsSurfView::bSetupPixelFormat()
{
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
1, // version number
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DOUBLEBUFFER, // double buffered
PFD_TYPE_RGBA, // RGBA type
24, // 24-bit color depth
0, 0, 0, 0, 0, 0, // color bits ignored
0, // no alpha buffer
0, // shift bit ignored
0, // no accumulation buffer
0, 0, 0, 0, // accum bits ignored
32, // 32-bit z-buffer
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored
};
int pixelformat;
if ( (pixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd)) == 0 )
{
MessageBox("ChoosePixelFormat failed");
return FALSE;
}
if (SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE)
{
MessageBox("SetPixelFormat failed");
return FALSE;
}
return TRUE;
}
void CNurbsSurfView::DrawScene(void)
{
GLfloat knots[8]={0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0};
//清除颜色缓冲区和深度缓冲区
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(330.0,1.0,0.0,0.0);
glScalef(0.5,0.5,0.5);
gluBeginSurface(theNurb);
gluNurbsSurface(theNurb,
8,knots,
8,knots,
4*3,3,
&ctrlpoints[0][0][0],
4,4,
GL_MAP2_VERTEX_3);
gluEndSurface(theNurb);
glPopMatrix();
glFinish();
SwapBuffers(wglGetCurrentDC());
}
//初始化控制点坐标,x,y,z范围从-3到3
void init_surface(void)
{
int u,v;
for(u=0;u<4;u++)
{
for(v=0;v<4;v++)
{
ctrlpoints[u][v][0]=2.0*((GLfloat)u-1.5);
ctrlpoints[u][v][1]=2.0*((GLfloat)v-1.5);
if((u==1||u==2)&&(v==1||v==2))
ctrlpoints[u][v][2]=3.0;
else
ctrlpoints[u][v][2]=-3.0;
}
}
}
//add up
- 1
- 2
前往页