📄 heartview.cpp
字号:
// HeartView.cpp : implementation of the CHeartView class
//
#include "stdafx.h"
#include "Heart.h"
#include "HeartDoc.h"
#include "HeartView.h"
#include "gl\gl.h"
#include "gl\glu.h"
#include "gl\glaux.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define INREAL float
#define S_NUMPOINTS 13
#define S_ORDER 3
#define S_NUMKNOTS (S_NUMPOINTS + S_ORDER)
#define T_NUMPOINTS 3
#define T_ORDER 3
#define T_NUMKNOTS (T_NUMPOINTS + T_ORDER)
#define SQRT_TWO 1.41421356237309504880
typedef INREAL Point[4];
GLenum doubleBuffer;
GLenum expectedError;
GLint rotX = 40, rotY = 40;
INREAL sknots[S_NUMKNOTS] = {
-1.0, -1.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0,
4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 9.0, 9.0
};
INREAL glutnots[T_NUMKNOTS] = {
1.0, 1.0, 1.0, 2.0, 2.0, 2.0
};
Point ctlpoints[S_NUMPOINTS][T_NUMPOINTS] = {
{
{
4.0, 2.0, 2.0, 1.0
},
{
4.0, 1.6, 2.5, 1.0
},
{
4.0, 2.0, 3.0, 1.0
}
},
{
{
5.0, 4.0, 2.0, 1.0
},
{
5.0, 4.0, 2.5, 1.0
},
{
5.0, 4.0, 3.0, 1.0
}
},
{
{
6.0, 5.0, 2.0, 1.0
},
{
6.0, 5.0, 2.5, 1.0
},
{
6.0, 5.0, 3.0, 1.0
}
},
{
{
SQRT_TWO*6.0, SQRT_TWO*6.0, SQRT_TWO*2.0, SQRT_TWO
},
{
SQRT_TWO*6.0, SQRT_TWO*6.0, SQRT_TWO*2.5, SQRT_TWO
},
{
SQRT_TWO*6.0, SQRT_TWO*6.0, SQRT_TWO*3.0, SQRT_TWO
}
},
{
{
5.2, 6.7, 2.0, 1.0
},
{
5.2, 6.7, 2.5, 1.0
},
{
5.2, 6.7, 3.0, 1.0
}
},
{
{
SQRT_TWO*4.0, SQRT_TWO*6.0, SQRT_TWO*2.0, SQRT_TWO
},
{
SQRT_TWO*4.0, SQRT_TWO*6.0, SQRT_TWO*2.5, SQRT_TWO
},
{
SQRT_TWO*4.0, SQRT_TWO*6.0, SQRT_TWO*3.0, SQRT_TWO
}
},
{
{
4.0, 5.2, 2.0, 1.0
},
{
4.0, 4.6, 2.5, 1.0
},
{
4.0, 5.2, 3.0, 1.0
}
},
{
{
SQRT_TWO*4.0, SQRT_TWO*6.0, SQRT_TWO*2.0, SQRT_TWO
},
{
SQRT_TWO*4.0, SQRT_TWO*6.0, SQRT_TWO*2.5, SQRT_TWO
},
{
SQRT_TWO*4.0, SQRT_TWO*6.0, SQRT_TWO*3.0, SQRT_TWO
}
},
{
{
2.8, 6.7, 2.0, 1.0
},
{
2.8, 6.7, 2.5, 1.0
},
{
2.8, 6.7, 3.0, 1.0
}
},
{
{
SQRT_TWO*2.0, SQRT_TWO*6.0, SQRT_TWO*2.0, SQRT_TWO
},
{
SQRT_TWO*2.0, SQRT_TWO*6.0, SQRT_TWO*2.5, SQRT_TWO
},
{
SQRT_TWO*2.0, SQRT_TWO*6.0, SQRT_TWO*3.0, SQRT_TWO
}
},
{
{
2.0, 5.0, 2.0, 1.0
},
{
2.0, 5.0, 2.5, 1.0
},
{
2.0, 5.0, 3.0, 1.0
}
},
{
{
3.0, 4.0, 2.0, 1.0
},
{
3.0, 4.0, 2.5, 1.0
},
{
3.0, 4.0, 3.0, 1.0
}
},
{
{
4.0, 2.0, 2.0, 1.0
},
{
4.0, 1.6, 2.5, 1.0
},
{
4.0, 2.0, 3.0, 1.0
}
}
};
GLUnurbsObj *theNurbs;
/////////////////////////////////////////////////////////////////////////////
// CHeartView
IMPLEMENT_DYNCREATE(CHeartView, CView)
BEGIN_MESSAGE_MAP(CHeartView, CView)
//{{AFX_MSG_MAP(CHeartView)
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()
/////////////////////////////////////////////////////////////////////////////
// CHeartView construction/destruction
CHeartView::CHeartView()
{
//add down
m_pDC = NULL;
}
CHeartView::~CHeartView()
{
}
BOOL CHeartView::PreCreateWindow(CREATESTRUCT& cs)
{
//add down
cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
//add up
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CHeartView drawing
void CHeartView::OnDraw(CDC* pDC)
{
CHeartDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
//add down
DrawScene();
//add up
}
/////////////////////////////////////////////////////////////////////////////
// CHeartView printing
BOOL CHeartView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CHeartView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CHeartView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CHeartView diagnostics
#ifdef _DEBUG
void CHeartView::AssertValid() const
{
CView::AssertValid();
}
void CHeartView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CHeartDoc* CHeartView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHeartDoc)));
return (CHeartDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CHeartView message handlers
int CHeartView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
//add down
Init(); //初始化OpenGL
//add up
return 0;
}
void CHeartView::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 CHeartView::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
void CHeartView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
glViewport(0, 0, cx, cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-2.0, 2.0, -2.0, 2.0, 0.8, 10.0);
gluLookAt(7.0, 4.5, 4.0, 4.5, 4.5, 2.5, 6.0, -3.0, 2.0);
glMatrixMode(GL_MODELVIEW);
}
//add down
void CHeartView::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);
theNurbs = gluNewNurbsRenderer();
// gluNurbsCallback(theNurbs, GLU_ERROR, ErrorCallback);
gluNurbsProperty(theNurbs, GLU_SAMPLING_TOLERANCE, 15.0);
gluNurbsProperty(theNurbs, GLU_DISPLAY_MODE, GLU_OUTLINE_PATCH);
expectedError = GLU_INVALID_ENUM;
gluNurbsProperty(theNurbs, ~0, 15.0);
expectedError = GLU_NURBS_ERROR13;
gluEndSurface(theNurbs);
expectedError = 0;
glColor3f(1.0, 1.0, 1.0);
}
BOOL CHeartView::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 CHeartView::DrawScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(4.0, 4.5, 2.5);
glRotatef(rotY, 1, 0, 0);
glRotatef(rotX, 0, 1, 0);
glTranslatef(-4.0, -4.5, -2.5);
gluBeginSurface(theNurbs);
gluNurbsSurface(theNurbs, S_NUMKNOTS, sknots, T_NUMKNOTS, glutnots,
4*T_NUMPOINTS, 4, &ctlpoints[0][0][0], S_ORDER,
T_ORDER, GL_MAP2_VERTEX_4);
gluEndSurface(theNurbs);
glPopMatrix();
glFinish();
SwapBuffers(wglGetCurrentDC());
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -