www.pudn.com > sun-moon-earth.rar > sunView.cpp


// sunView.cpp : implementation of the CSunView class 
// 
 
#include "stdafx.h" 
#include "sun.h" 
 
#include "sunDoc.h" 
#include "sunView.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CSunView 
 
IMPLEMENT_DYNCREATE(CSunView, CView) 
 
BEGIN_MESSAGE_MAP(CSunView, CView) 
	//{{AFX_MSG_MAP(CSunView) 
	ON_WM_CREATE() 
	ON_WM_DESTROY() 
	ON_WM_KEYDOWN() 
	ON_WM_SIZE() 
	ON_WM_ERASEBKGND() 
	//}}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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CSunView construction/destruction 
 
CSunView::CSunView() 
{ 
	// TODO: add construction code here 
 
} 
 
CSunView::~CSunView() 
{ 
} 
 
BOOL CSunView::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); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CSunView drawing 
 
void CSunView::OnDraw(CDC* pDC) 
{ 
	CSunDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	// TODO: add draw code for native data here 
	//设置一个线程的当前绘图描述表 
	wglMakeCurrent(pDC->m_hDC,hglrc); 
	//调用用户绘图主程序 
	drawscene(); 
	//释放绘图描述表所使用的设备描述表 
	wglMakeCurrent(pDC->m_hDC,NULL); 
	//忽略设备描述表句柄pDC->m_hDC 
	//交换前后缓冲区 
	SwapBuffers(pDC->m_hDC); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CSunView printing 
 
BOOL CSunView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CSunView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CSunView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CSunView diagnostics 
 
#ifdef _DEBUG 
void CSunView::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CSunView::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
 
CSunDoc* CSunView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSunDoc))); 
	return (CSunDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CSunView message handlers 
 
int CSunView::OnCreate(LPCREATESTRUCT lpCreateStruct)  
{ 
	if (CView::OnCreate(lpCreateStruct) == -1) 
		return -1; 
	 
	// TODO: Add your specialized creation code here 
	//初始化像素格式 
	PIXELFORMATDESCRIPTOR pfd = {  
	    sizeof(PIXELFORMATDESCRIPTOR),    // pfd结构的大小  
	    1,                                // 版本号  
	    PFD_DRAW_TO_WINDOW |              // 支持在窗口中绘图  
	    PFD_SUPPORT_OPENGL |              // 支持 OpenGL  
	    PFD_DOUBLEBUFFER,                 // 双缓存模式  
	    PFD_TYPE_RGBA,                    // RGBA 颜色模式  
	    24,                               // 24 位颜色深度  
	    0, 0, 0, 0, 0, 0,                 // 忽略颜色位  
	    0,                                // 没有非透明度缓存  
	    0,                                // 忽略移位位  
	    0,                                // 无累加缓存  
	    0, 0, 0, 0,                       // 忽略累加位  
	    32,                               // 32 位深度缓存      
	    0,                                // 无模板缓存  
	    0,                                // 无辅助缓存  
	    PFD_MAIN_PLANE,                   // 主层  
	    0,                                // 保留  
	    0, 0, 0                           // 忽略层,可见性和损毁掩模  
	}; 	 
	CClientDC clientdc(this); 
	int pixelformat; 
	pixelformat = ::ChoosePixelFormat(clientdc.m_hDC, &pfd);//选择像素格式 
	BOOL rt=SetPixelFormat(clientdc.m_hDC,pixelformat,&pfd); 
	hglrc=wglCreateContext(clientdc.m_hDC); 
////////////////////////////////////////////////////////////////// 
	 
	return 0; 
} 
 
void CSunView::OnDestroy()  
{ 
	CView::OnDestroy(); 
	 
	// TODO: Add your message handler code here 
	//删除调色板和渲染上下文、定时器 
	if(wglGetCurrentContext()!=NULL) 
		wglMakeCurrent(NULL,NULL); 
	if(hglrc!=NULL) 
	{ 
		wglDeleteContext(hglrc); 
		hglrc=NULL; 
	} 
} 
 
void CSunView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)  
{ 
	// TODO: Add your message handler code here and/or call default 
	switch(nChar)  
	{ 
	case VK_LEFT: 
		revanticlockwise(); 
		break; 
	case VK_RIGHT: 
		revlockwise(); 
		break; 
	case VK_UP: 
		rotationclockwise(); 
		break; 
	case VK_DOWN: 
		rotationclockwise(); 
		break; 
	default: 
		break; 
	} 
 
	CView::OnKeyDown(nChar, nRepCnt, nFlags); 
	Invalidate(FALSE); 
} 
 
void CSunView::OnSize(UINT nType, int cx, int cy)  
{ 
	CView::OnSize(nType, cx, cy); 
	 
	// TODO: Add your message handler code here 
	GLsizei w=cx; 
	GLsizei h=cy; 
	if(!h) return; 
	//获取客户应用窗口区的设备描述表 
	CClientDC clientDC(this); 
	//设置一个线程的当前绘图描述表 
	wglMakeCurrent(clientDC.m_hDC,hglrc);   
	//重新设置视口 
	glViewport(0,0,w,h); 
    //prepare for and then  
	glMatrixMode(GL_PROJECTION); 
	//define the projection transformation 
	glLoadIdentity(); 
	gluPerspective(60.0,(GLfloat)w/(GLfloat)h,1.0,20.0); 
	glMatrixMode(GL_MODELVIEW); 
	glLoadIdentity(); 
	glTranslatef(0.0,0.0,-5.0); 
	//释放绘图描述表所使用的设备描述表 
	wglMakeCurrent(NULL,NULL); 
	 
} 
 
void CSunView::drawscene() 
{ 
 
	GLfloat amoon,rmoon,xcord,ycord; 
 
	glClearColor(0.0,0.0,0.0,0.0); 
	glClear(GL_COLOR_BUFFER_BIT); 
	glColor3f(1.0,1.0,1.0); 
/*	auxWireTeapot(0.5); 
	glFlush();*/ 
 
	//save initial model_matrix into stack 
	glPushMatrix(); 
 
	//draw sun 
	glRotatef(60.0,1.0,0.0,0.0); 
	auxWireSphere(0.5); 
 
	//draw earth 
	glPopMatrix(); 
	glPushMatrix(); 
	glRotatef(60.0,1.0,0.0,0.0); 
	glRotatef((GLfloat)revolution,0.0,0.0,1.0); 
	glTranslatef(2.5,0.0,0.0); 
	glRotatef((GLfloat)rotation,0.0,0.0,1.0); 
	auxWireSphere(0.3); 
 
	//draw moon 
	glPopMatrix(); 
	glPushMatrix(); 
	glRotatef(60.0,1.0,0.0,0.0); 
	xcord=2.5*cos(revolution*PI/180.)+cos(12.*revolution*PI/180); 
	ycord=2.5*sin(revolution*PI/180.)+sin(12.*revolution*PI/180); 
	rmoon=sqrt(xcord*xcord+ycord*ycord); 
	amoon=180./PI*atan2(ycord,xcord); 
	glRotatef(amoon,0.0,0.0,1.0); 
	glTranslatef(rmoon,0.0,0.0); 
	glRotatef((GLfloat)rotation/30.,0.0,0.0,1.0); 
	auxWireSphere(0.2); 
 
	//draw Earth Obrit 
	glPopMatrix(); 
	glPushMatrix(); 
	glRotatef(60.0,1.0,0.0,0.0); 
	glScalef(2.5,2.5,2.5); 
	drawcircle(); 
 
	//draw moon obrit 
	glPopMatrix(); 
	glPushMatrix(); 
	glRotatef(60.0,1.0,0.0,0.0); 
	glRotatef((GLfloat)revolution,0.0,0.0,1.0); 
	glTranslatef(2.5,0.0,0.0); 
	glScalef(1.0,1.0,1.0); 
	drawcircle();   
 
	//vacate the model_matrix stack 
	glPopMatrix(); 
	glFlush();   
} 
 
void CSunView::revanticlockwise(void) 
{ 
	revolution=(revolution+1)%360; 
} 
 
void CSunView::revlockwise(void) 
{ 
	revolution=(revolution-1)%360; 
} 
 
void CSunView::rotationanticlockwise(void) 
{ 
	revolution=(revolution+10)%360; 
} 
 
void CSunView::rotationclockwise(void) 
{ 
	revolution=(revolution-10)%360; 
 
} 
 
void CSunView::drawcircle() 
{ 
	GLint i; 
	GLfloat cosine,sine; 
	//Generate the points of the circle 
	glBegin(GL_LINE_LOOP); 
	for (i=0;i<100;i++) 
	{ 
		cosine=cos(i*2*PI/100.0); 
		sine=sin(i*2*PI/100.0); 
		glVertex2f(cosine,sine); 
	} 
	glEnd(); 
 
} 
 
BOOL CSunView::OnEraseBkgnd(CDC* pDC)  
{ 
	// TODO: Add your message handler code here and/or call default 
	 
//	return CView::OnEraseBkgnd(pDC); 
	return TRUE; 
}