视见体
Viewing Volume is nothing but the region of 3D Cartesian space in that will occupy the window. It is nothing but the minimum and maximum x, y and z values that are inside the window. So if a vertex is outside this range of x, y and z values then they are clipped by OpenGL before rendering can occur.
Z Buffer
The new term we have to deal with in addition to width and height of an object in 3D graphics is depth. The depth of an object is its distance from the viewpoint. The viewpoint is the location from which we are looking at that point. This depth value goes into the depth or Z-buffer. If we are drawing 2 objects that have some pixels that overlap, the first object will after it is rendered have its depth value in the depth buffer. When the next object is rendered, OpenGL will check to see whether the pixel it’s about to draw is in front of (with respect to the viewpoint) any pixel from the first object that’s already drawn. It does this by checking the Z value of the current pixel with the value that is already in the buffer. If the new pixel is closer to the viewpoint, OpenGL places its depth value in the depth buffer. This is how the Z-buffer works.
正交投影和透视投影
One term we need to understand very well to learn 3D Graphics well is projection. Well, computer graphics at its simplest is all about setting a color to a pixel on screen. And a pixel on a screen can have only two dimensions. So 3D graphics is merely an illusion. The 3D coordinates that we specify will have to be projected onto a 2D surface to create this illusion for us. And we have to specify how this projection works. By specifying a projection we specify the clipping or viewing volume.
基本3D图形绘制
1,在CCY457OpenGLView.h中加入下列变量
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->BOOLm_bPoint;//StatusofPoint
BOOLm_bLine;//StatusofLine
BOOLm_bPolygon;//StatusofPolygon
BOOLm_bTriangle;//StatusofTriangle
并且在构造函数中初始化
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->CCY457OpenGLView::CCY457OpenGLView()
{
m_bPoint=FALSE;
m_bLine=FALSE;
m_bPolygon=FALSE;
m_bTriangle=FALSE;
m_bCube=FALSE;
m_bTorus=FALSE;
m_bTeapot=FALSE;
m_bIcosahedron=FALSE;
m_bSimpleCube=FALSE;
}
2,加入五个菜单项及其对应的事件处理程序。
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->voidCCY457OpenGLView::OnObjectsTeapot()
{//画茶壶
m_bCube=FALSE;
m_bTorus=FALSE;
m_bTeapot=TRUE;
m_bIcosahedron=FALSE;
m_bSimpleCube=FALSE;
InvalidateRect(NULL,FALSE);
}
voidCCY457OpenGLView::OnObjectsCube()
{//画立方体
m_bCube=TRUE;
m_bTorus=FALSE;
m_bTeapot=FALSE;
m_bIcosahedron=FALSE;
m_bSimpleCube=FALSE;
InvalidateRect(NULL,FALSE);
}
voidCCY457OpenGLView::OnObjectsIcosahedron()
{//画二十面体
m_bCube=FALSE;
m_bTorus=FALSE;
m_bTeapot=FALSE;
m_bIcosahedron=TRUE;
m_bSimpleCube=FALSE;
InvalidateRect(NULL,FALSE);
}
voidCCY457OpenGLView::OnObjectsTorus()
{//画环面
m_bCube=FALSE;
m_bTorus=TRUE;
m_bTeapot=FALSE;
m_bIcosahedron=FALSE;
m_bSimpleCube=FALSE;
InvalidateRect(NULL,FALSE);
}
voidCCY457OpenGLView::OnObjectsSimplecube()
{//画简单立方体
m_bCube=FALSE;
m_bTorus=FALSE;
m_bTeapot=FALSE;
m_bIcosahedron=FALSE;
m_bSimpleCube=TRUE;
InvalidateRect(NULL,FALSE);
}
3,由于开始绘制3维图形,因此使用第一篇文章的OnSize()函数。
4,在RenderScene中加入具体的绘制代码:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->voidCCY457OpenGLView::RenderScene()
{//绘制函数
if(m_bPoint==TRUE)
{
glPointSize(3.0f);
glBegin(GL_POINTS);
glVertex2f(0.0f,0.0f);
glVertex2f(1.0f,0.0f);
glVertex2f(0.0f,1.0f);
glEnd();
}
if(m_bLine==TRUE)
{
glBegin(GL_LINES);
glVertex2f(0.0f,0.0f);
glVertex2f(1.0f,0.0f);
glEnd();
}
if(m_bTriangle==TRUE)
{
glBegin(GL_TRIANGLES);
glVertex2f(0.0f,0.0f);
glVertex2f(2.0f,0.0f);
glVertex2f(0.0f,2.0f);
glEnd();
}
if(m_bPolygon==TRUE)
{
glBegin(GL_POLYGON);
glVertex2f(0.0f,0.0f);
glVertex2f(3.0f,0.0f);
glVertex2f(4.0f,3.0f);
glVertex2f(1.5f,6.0f);
glVertex2f(-1.0f,3.0f);
glEnd();
}
//ReplacethecurrentmatrixwithIdentityMatrix
glLoadIdentity();
glTranslatef(0.0f,0.0f,-5.0f);
glRotatef(-30.0f,1.0f,1.0f,0.0f);
//DrawaCube
if(m_bCube)
{
glutWireCube(1.0f);
}
//DrawaTorus
if(m_bTorus)
{
glutWireTorus(0.5f,1.0f,50,50);
}
//DrawaTeapot
if(m_bTeapot)
{
glutWireTeapot(1.0f);
}
//DrawaIcosahedron
if(m_bIcosahedron)
{
glutWireIcosahedron();
}
//Drawacubebyspecifyingtheverticesindividually
if(m_bSimpleCube)
{
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
//FrontFace
glBegin(GL_POLYGON);
glVertex3f(-1.0f,-1.0f,0.0f);
glVertex3f(1.0f,-1.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);
glVertex3f(-1.0f,1.0f,0.0f);
glEnd();
//BackFace
glBegin(GL_POLYGON);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,1.0f,-1.0f);
glVertex3f(1.0f,1.0f,-1.0f);
glVertex3f(1.0f,-1.0f,-1.0f);
glEnd();
//LeftFace
glBegin(GL_POLYGON);
glVertex3f(-1.0f,-1.0f,0.0f);
glVertex3f(-1.0f,1.0f,0.0f);
glVertex3f(-1.0f,1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glEnd();
//RightFace
glBegin(GL_POLYGON);
glVertex3f(1.0f,-1.0f,0.0f);
glVertex3f(1.0f,-1.0f,-1.0f);
glVertex3f(1.0f,1.0f,-1.0f);
glVertex3f(1.0f,1.0f,0.0f);
glEnd();
//TopFace
glBegin(GL_POLYGON);
glVertex3f(-1.0f,1.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);
glVertex3f(1.0f,1.0f,-1.0f);
glVertex3f(-1.0f,1.0f,-1.0f);
glEnd();
//BottonFace
glBegin(GL_POLYGON);
glVertex3f(-1.0f,-1.0f,0.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(1.0f,-1.0f,-1.0f);
glVertex3f(1.0f,-1.0f,0.0f);
glEnd();
}
}
分享到:
相关推荐
基于MFC的OpenGL编程part5源码
基于MFC的OpenGL编程part6源码
基于MFC的OpenGL编程part4源码
基于MFC的OpenGL编程part3源码
基于MFC的OpenGL编程part7的源码 出处:http://phinecos.cnblogs.com/ 代码被我修改过,可以调试出来。原文中的一些细节地方重新注释了一下,方便理解。 很多地方被我修改过,但是大致的内容不会发生变化。 我自己...
基于MFC的OpenGL编程Part10源码 作者:洞庭散人 出处:http://phinecos.cnblogs.com/
基于MFC的OpenGL编程part2
基于MFC的OpenGL编程 基于MFC的OpenGL编程 基于MFC的OpenGL编程 基于MFC的OpenGL编程 基于MFC的OpenGL编程
基于MFC的OpenGL编程part8源码 洞庭散人的博客源码
基于MFC的OpenGL编程Part9源码 作者:洞庭散人 出处:http://phinecos.cnblogs.com/
使用单文档视图框架,解决拖动时产生的抖动问题
《基于MFC的OpenGL编程》Part 7 Colors的源码
C++ openGL库文件,有普通的openGL库资源,主要是基于MFC编程的库文件
本程序是基于基于MFC和OPENGL的编程,希望对大家的学习会有帮助。
对于OpenGL的应用,常常是基于GDI接口来实现的。在Windows下做开发,自然 就需要了解OpenGL在MFC下的开发,本工程是MFC下OpenGL开发的一个简单实例。
基于MFC单文档OpenGL打开3D模型,可以打开3DMAX另存为的3ds文件,并将读取文件进行移动、旋转和缩放功能
基于MFC的OpenGL编程.pdf