《基于MFC的OpenGL编程》Part 4 Transformations - Rotations, Translations and Scaling

《基于MFC的OpenGL编程》Part 4 Transformations - Rotations, Translations and Scaling

原文链接
Transformations - Translation, Rotation and Scaling

Translation is nothing but moving along an arbitrary axis. Rotation is spinning about an arbitrary(任意的) axis. Scaling is increase or decrease in size along an arbitrary axis. One important point to remember is that OpenGL uses a right hand coordinate system where Z-ve goes into the Screen. An object said to be undergoing a transformation could be undergoing a translation, rotation and/or scaling. Understanding how different types of transformations work together is very important.

本文将对地球进行建模,让其围绕着太阳,此外它还会有月球伴随,而在下文中将会加入对鼠标和键盘的事件响应机制。

绘制代码:

1、新加了一个protected void RenderSceneSun()的方法函数:

<span style="font-size:18px;">void CMy2OpenGLView::RenderSceneSun()
{
	//Draw the sun 
	glLoadIdentity();
	glTranslatef(0.0f,0.0f,-5.0f);
	glRotatef(6.0f,1.0f,0.0f,0.0f);
	glutWireSphere(1.0f,20,20);
		//Draw the Planet
		glPushMatrix();
			glTranslatef(-1.2f,-0.5f,-2.0f);
			glRotatef(6.0f,1.0f,0.0f,0.0f);
			glutWireSphere(0.5f,20,20);
		//Draw the moon
		glPushMatrix();
			glTranslatef(-1.2f,-0.5f,-2.0f);
			glRotatef(6.0f,1.0f,0.0f,0.0f);
			glutWireSphere(0.05f,20,20);
		glPopMatrix();
		glPopMatrix();
}</span>


2、将RenderSceneSun加入到OnDraw()中后可以看看效果:

<span style="font-size:18px;">void CMy2OpenGLView::OnDraw(CDC* pDC)
{
	CMy2OpenGLDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here

	  // Clear out the color & depth buffers
    <span style="white-space:pre">	</span>::glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	::glClearColor(0.0,0.0,0.5,0.5);//此函数在InitialOpenGL函数中设置默认的背景,此处重新设置,为蓝色背景
    RenderScene();
	RenderScene3D();
	RenderSceneSun();
	glFlush();
    <span style="white-space:pre">	</span>// Tell OpenGL to flush its pipeline
    <span style="white-space:pre">	</span>::glFinish();
    <span style="white-space:pre">	</span>// Now Swap the buffers
   <span style="white-space:pre">	</span> ::SwapBuffers( m_pDC->GetSafeHdc() );
 <span style="white-space:pre">	</span>//SwapBuffers(pDC->m_hDC);

}</span>

运行效果:


你可能感兴趣的:(编程,mfc,3D,图形,OpenGL)