📄 ge_3041222098view.cpp
字号:
// ge_3041222098View.cpp : implementation of the CGe_3041222098View class
//
#include "stdafx.h"
#include "ge_3041222098.h"
#include "ge_3041222098Doc.h"
#include "ge_3041222098View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGe_3041222098View
IMPLEMENT_DYNCREATE(CGe_3041222098View, CView)
BEGIN_MESSAGE_MAP(CGe_3041222098View, CView)
//{{AFX_MSG_MAP(CGe_3041222098View)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_ERASEBKGND()
ON_WM_KEYDOWN()
ON_WM_MOUSEWHEEL()
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_SIZE()
ON_WM_TIMER()
ON_COMMAND(ID_ball_Flat, OnballFlat)
ON_COMMAND(ID_ball_Smooth, OnballSmooth)
ON_COMMAND(ID_ball_Wireframe, OnballWireframe)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CGe_3041222098View construction/destruction
CGe_3041222098View::CGe_3041222098View()
{
// TODO: add construction code here
m_timer = 1;
m_theta[0] = 0;
m_theta[1] = 0;
m_theta[2] = 0;
m_axis = 1;
viewer[0] = 0.0;
viewer[1] = 0.0;
viewer[2] = 3.0;
}
CGe_3041222098View::~CGe_3041222098View()
{
}
BOOL CGe_3041222098View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CGe_3041222098View drawing
void CGe_3041222098View::OnDraw(CDC* pDC)
{
CGe_3041222098Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
DrawScene();
}
/////////////////////////////////////////////////////////////////////////////
// CGe_3041222098View printing
BOOL CGe_3041222098View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CGe_3041222098View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CGe_3041222098View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CGe_3041222098View diagnostics
#ifdef _DEBUG
void CGe_3041222098View::AssertValid() const
{
CView::AssertValid();
}
void CGe_3041222098View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CGe_3041222098Doc* CGe_3041222098View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGe_3041222098Doc)));
return (CGe_3041222098Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CGe_3041222098View message handlers
int CGe_3041222098View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
m_pDC = new CClientDC(this); // get device context
if( m_pDC == NULL )
{
::AfxMessageBox( "fail to get device context" );
return FALSE;
}
if( !SetupPixelFormat( ) ) // setup pixel format
{
::AfxMessageBox( "SetupPixelFormat failed" );
return FALSE;
}
// get rendering context
if( ( m_hRC = wglCreateContext(m_pDC->GetSafeHdc()) ) == 0 )
{
::AfxMessageBox( "wglCreateContext failed" );
return FALSE;
}
// make current rendering context
if( wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC) == FALSE )
{
::AfxMessageBox( "wglMakeCurrent failed" );
return FALSE;
}
// TODO: Add your specialized creation code here
//creat a timer
KillTimer(m_timer);
SetTimer(m_timer,1,NULL);
return 0;
}
void CGe_3041222098View::OnDestroy()
{
CView::OnDestroy();
// TODO: Add your message handler code here
if(wglMakeCurrent(0,0)==FALSE)
::AfxMessageBox("wglMakeCurrent failed");
if( m_hRC && (wglDeleteContext(m_hRC)==FALSE))
::AfxMessageBox("wglDeleteContext fdailed");
if(m_pDC)
delete m_pDC;
}
BOOL CGe_3041222098View::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return TRUE;
}
void CGe_3041222098View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
switch(nChar)
{
case 'X':
m_axis = 0;
break;
case 'Y':
m_axis = 1;
break;
case 'Z':
m_axis = 2;
break;
case 'Q':
case VK_ESCAPE:
exit(1);
if(nChar == VK_LEFT)
viewer[0] -=0.5;
if(nChar == VK_RIGHT)
viewer[0] +=0.5;
if(nChar == VK_DOWN)
viewer[1] -=0.5;
if(nChar == VK_UP)
viewer[1] +=0.5;
if(nChar == VK_HOME)
viewer[2] -=0.5;
if(nChar == VK_END)
viewer[2] +=0.5;
}
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
BOOL CGe_3041222098View::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
// TODO: Add your message handler code here and/or call default
m_axis = 1;
return CView::OnMouseWheel(nFlags, zDelta, pt);
}
void CGe_3041222098View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_axis = 0;
CView::OnLButtonDown(nFlags, point);
}
void CGe_3041222098View::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_axis = 2;
CView::OnRButtonDown(nFlags, point);
}
void CGe_3041222098View::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,cx,cy);
if(cx<=cy)
glOrtho(-2.0,2.0,-2.0*cy/cx,2.0*cy/cx,-20.0,20.0);
else
glOrtho(-2.0*cy/cx,2.0*cy/cx,-2.0,2.0,-20.0,20.0);
//glRotatef(30.0f,1.0f,0.0f,0.0f);
//glRotatef(-135.0f,0.0f,1.0f,0.0f);
}
void CGe_3041222098View::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent == m_timer)
{
m_theta[m_axis] += 2.0;
if( m_theta[m_axis] > 360.0)
m_theta[m_axis] -=360.0;
Invalidate();
}
else
CView::OnTimer(nIDEvent);
}
void CGe_3041222098View::OnballFlat()
{
// TODO: Add your command handler code here
id = 2 ;
DrawScene();
}
void CGe_3041222098View::OnballSmooth()
{
// TODO: Add your command handler code here
id = 3 ;
DrawScene();
}
void CGe_3041222098View::OnballWireframe()
{
// TODO: Add your command handler code here
id = 1 ;
DrawScene();
}
void CGe_3041222098View::CrossProduct(point3 &a, point3 &b, point3 &c, point3 &r)
{
r[0] = (b[1]-a[1])*(c[2]-a[2])-(b[2]-a[2])*(c[1]-a[1]);
r[1] = (b[2]-a[2])*(c[0]-a[0])-(b[0]-a[0])*(c[2]-a[2]);
r[2] = (b[0]-a[0])*(c[1]-a[1])-(b[1]-a[1])*(c[0]-a[0]);
}
void CGe_3041222098View::DrawScene()
{
glEnable(GL_DEPTH_TEST);
glClearColor(1.0f,1.0f,1.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(viewer[0],viewer[1],viewer[2],0.0,0.0,0.0,
0.0,1.0,0.0);
glRotatef(30.0f,1.0f,0.0f,0.0f);
glRotatef(-135.0f,0.0f,1.0f,0.0f);
// glPushMatrix();
glRotatef(m_theta[0],1.0f,0.0f,0.0f);
glRotatef(m_theta[1],0.0f,1.0f,0.0f);
glRotatef(m_theta[2],0.0f,0.0f,1.0f);
//level of subdivision
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
point3 v[4] = {{0.0f,0.0f,1.0f},
{0.0f,0.942809f,-0.333333f},
{-0.816497f,-0.471405f,-0.333333f},
{0.816497f,-0.471405f,-0.333333f}};
int i;
int n =3;
switch(id){
case 3:
//a tetrahedrom
for(i=0;i<7;i++)
{
DivideTriangle(v[0],v[1],v[2],i);
DivideTriangle(v[0],v[2],v[3],i);
DivideTriangle(v[0],v[3],v[1],i);
DivideTriangle(v[3],v[2],v[1],i);
}
break;
case 1:
//a tetrahedrom
for(i=0;i<5;i++)
{
DivideTriangle(v[0],v[1],v[2],i);
DivideTriangle(v[0],v[2],v[3],i);
DivideTriangle(v[0],v[3],v[1],i);
DivideTriangle(v[3],v[2],v[1],i);
DivideTriangle(v[1],v[0],v[2],i);
DivideTriangle(v[1],v[2],v[3],i);
DivideTriangle(v[1],v[3],v[0],i);
DivideTriangle(v[3],v[2],v[0],i);
DivideTriangle(v[2],v[0],v[1],i);
DivideTriangle(v[2],v[1],v[3],i);
DivideTriangle(v[2],v[3],v[0],i);
DivideTriangle(v[3],v[1],v[0],i);
}
break;
}
SwapBuffers(wglGetCurrentDC());
glFlush();
}
void CGe_3041222098View::DrawTriangle(point3 &a, point3 &b, point3 &c)
{
switch(id){
case 3:
glBegin(GL_TRIANGLES);
glNormal3fv(a);
glVertex3fv(a);
glNormal3fv(b);
glVertex3fv(b);
glNormal3fv(c);
glVertex3fv(c);
glEnd();
break;
case 1:
point3 n;
CrossProduct(a,b,c,n);
glBegin(GL_LINES);
glNormal3fv(n);
glNormal3fv(a);
glVertex3fv(a);
glNormal3fv(b);
glVertex3fv(b);
glNormal3fv(c);
glVertex3fv(c);
glEnd();
break;
}
}
void CGe_3041222098View::Normalize(point3 &p)
{
double d = p[0]*p[0] + p[1]*p[1] + p[2]*p[2];
if( d > 0)
{
float len = (float)(1.0 / sqrt(d));
p[0] *=len;
p[1] *=len;
p[2] *=len;
}
}
void CGe_3041222098View::DivideTriangle(point3 &a, point3 &b, point3 &c, int n)
{
if( n > 0)
{
point3 v1,v2,v3;
for(register int i=0;i<3;i++)
{
v2[i]=v1[i] = a[i] + b[i] ;
v2[i] = b[i] + c[i] ;
v3[i] = c[i] + a[i] ;
}
Normalize(v1);
Normalize(v2);
Normalize(v3);
DivideTriangle(a,v1,v3,n-1);
DivideTriangle(b,v2,v1,n-1);
DivideTriangle(c,v3,v2,n-1);
DivideTriangle(v1,v2,v3,n-1);
}
else
DrawTriangle(a, b, c);
}
BOOL CGe_3041222098View::SetupPixelFormat(PIXELFORMATDESCRIPTOR *pPFD)
{
PIXELFORMATDESCRIPTOR pfd = {
sizeof( PIXELFORMATDESCRIPTOR ),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24,
0, 0, 0, 0, 0, 0,
0, 0, 0,
0, 0, 0, 0,
16,
0, 0,
PFD_MAIN_PLANE,
0, 0, 0, 0 };
int pixelformat;
PIXELFORMATDESCRIPTOR* pPFDtoUse;
pPFDtoUse = (0 == pPFD)? &pfd : pPFD;
if ( 0 == ( pixelformat = ::ChoosePixelFormat( m_pDC->GetSafeHdc(), pPFDtoUse ) ) )
{
::AfxMessageBox( "ChoosePixelFormat failed" );
return FALSE;
}
if ( FALSE == ::SetPixelFormat( m_pDC->GetSafeHdc(), pixelformat, pPFDtoUse ) )
{
::AfxMessageBox( "SetPixelFormat failed" );
return FALSE;
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -