www.pudn.com > 太阳地球月亮旋转.rar > circleView.cpp


// circleView.cpp : implementation of the CCircleView class 
// 
 
#include "stdafx.h" 
#include "circle.h" 
 
#include "circleDoc.h" 
#include "circleView.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CCircleView 
 
IMPLEMENT_DYNCREATE(CCircleView, CView) 
 
BEGIN_MESSAGE_MAP(CCircleView, CView) 
	//{{AFX_MSG_MAP(CCircleView) 
	ON_WM_CREATE() 
	ON_WM_DESTROY() 
	ON_WM_ERASEBKGND() 
	ON_WM_SIZE() 
	ON_WM_TIMER() 
	//}}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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CCircleView construction/destruction 
 
CCircleView::CCircleView() 
{ 
	// TODO: add construction code here 
    m_hDC = NULL; 
	m_hRC = NULL; 
	moon=0; 
    sun=0; 
	day=0; 
    year=0; 
} 
 
CCircleView::~CCircleView() 
{ 
} 
 
BOOL CCircleView::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); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CCircleView drawing 
 
void CCircleView::OnDraw(CDC* pDC) 
{ 
	CCircleDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	// TODO: add draw code for native data here 
	circle(); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CCircleView printing 
 
BOOL CCircleView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CCircleView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CCircleView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CCircleView diagnostics 
 
#ifdef _DEBUG 
void CCircleView::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CCircleView::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
 
CCircleDoc* CCircleView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCircleDoc))); 
	return (CCircleDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CCircleView message handlers 
 
int CCircleView::OnCreate(LPCREATESTRUCT lpCreateStruct)  
{ 
	if (CView::OnCreate(lpCreateStruct) == -1) 
		return -1; 
	 
	// TODO: Add your specialized creation code here 
	// 设置像素格式 
	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, 32, 0, 0, 
       PFD_MAIN_PLANE, 0, 0, 0, 0 
	}; 
    // 获得当前的设备上下文 
	m_hDC = GetDC()->GetSafeHdc(); 
	// 选择匹配的像素格式,即选择当前设备上下文m_hDC支// 持的,且与pfd最为接近的像素格式 
	int nPixelFormat = ::ChoosePixelFormat(m_hDC, &pfd); 
		 
	// 设置像素格式 
		::SetPixelFormat(m_hDC, nPixelFormat, &pfd); 
	// 建立新的渲染上下文 
		m_hRC = ::wglCreateContext(m_hDC); 
    // 设置新的渲染上下文为当前渲染上下文 
	::wglMakeCurrent(m_hDC, m_hRC); 
	init();		// 初始化新建的Opengl窗口 
	return 0; 
} 
 
void CCircleView::OnDestroy()  
{ 
	CView::OnDestroy(); 
	 
	// TODO: Add your message handler code here 
	if (wglGetCurrentContext() != NULL) { 
		 // 设置当前图形操作表为空 
		 ::wglMakeCurrent(NULL, NULL); 	 
	} 
	if (m_hRC != NULL) { 
		// 销毁渲染上下文 
	   ::wglDeleteContext(m_hRC); 
 		 m_hRC = NULL; 
	}	 
 
} 
 
BOOL CCircleView::OnEraseBkgnd(CDC* pDC)  
{ 
	// TODO: Add your message handler code here and/or call default 
	 
	//return CView::OnEraseBkgnd(pDC); 
	return true; 
} 
 
void CCircleView::OnSize(UINT nType, int w, int h)  
{ 
	CView::OnSize(nType, w, h); 
	 
	// TODO: Add your message handler code here 
   glViewport (0, 0, (GLsizei) w, (GLsizei) h);  
   glMatrixMode (GL_PROJECTION); 
   glLoadIdentity (); 
   gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0); 
   glMatrixMode(GL_MODELVIEW); 
   glLoadIdentity(); 
   gluLookAt (0.0, 2.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); 
	 
} 
void CCircleView::init(void) 
{ 
 glClearColor (0.0, 0.0, 0.0, 0.0); 
 glShadeModel (GL_FLAT);  
 SetTimer(1,100,NULL); 
} 
void CCircleView::circle(void) 
{ 
glClear (GL_COLOR_BUFFER_BIT); 
   glColor3f (1.0, 1.0, 1.0); 
   glPushMatrix(); 
   glRotatef ((GLfloat) sun, 0.0, 1.0, 0.0); 
   glRotatef ((GLfloat) sun, 1.0, 0.0, 0.0); 
    
   glutWireSphere(1.0, 20, 16);   /* draw sun */ 
   glRotatef ((GLfloat) year, 0.0, 1.0, 0.0); 
    
   glTranslatef (2.0, 0.0, 0.0); 
   glRotatef ((GLfloat) day, 0.0, 1.0, 0.0); 
   glutWireSphere(0.3, 12, 10);    /* draw earth */ 
    
   glTranslatef (1.0, 0.0, 0.0); 
   glRotatef ((GLfloat) moon, 0.0, 1.0, 0.0);	 
   glutWireSphere(0.2, 12, 10);    /* draw moon */ 
    
   glPopMatrix(); 
   glFlush(); 
   SwapBuffers(m_hDC); 
} 
void CCircleView::OnTimer(UINT nIDEvent)  
{ 
	// TODO: Add your message handler code here and/or call default 
	sun+=1; 
	moon+=3; 
	year+=6; 
	day+=9; 
	this->circle(); 
	CView::OnTimer(nIDEvent); 
	 
}