《基于MFC的OpenGL编程》Part 4 Drawing Simple 3D objects

视见体

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中加入下列变量

BOOL m_bPoint;    //Status of Point
BOOL m_bLine;    //Status of Line
BOOL m_bPolygon;  //Status of Polygon
BOOL m_bTriangle;  //Status of Triangle

并且在构造函数中初始化

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,加入五个菜单项及其对应的事件处理程序。

void CCY457OpenGLView::OnObjectsTeapot()
{//画茶壶
  m_bCube      = FALSE;
  m_bTorus    = FALSE;
  m_bTeapot    = TRUE;
  m_bIcosahedron = FALSE;
  m_bSimpleCube = FALSE;
  InvalidateRect(NULL,FALSE);
}
void CCY457OpenGLView::OnObjectsCube()
{//画立方体
  m_bCube      = TRUE;
  m_bTorus    = FALSE;
  m_bTeapot    = FALSE;
  m_bIcosahedron = FALSE;
  m_bSimpleCube = FALSE;
  InvalidateRect(NULL,FALSE);
}
void CCY457OpenGLView::OnObjectsIcosahedron()
{//画二十面体
  m_bCube      = FALSE;
  m_bTorus    = FALSE;
  m_bTeapot    = FALSE;
  m_bIcosahedron = TRUE;
  m_bSimpleCube = FALSE;
  InvalidateRect(NULL,FALSE);
}
void CCY457OpenGLView::OnObjectsTorus()
{//画环面
  m_bCube      = FALSE;
  m_bTorus    = TRUE;
  m_bTeapot    = FALSE;
  m_bIcosahedron = FALSE;
  m_bSimpleCube = FALSE;
  InvalidateRect(NULL,FALSE);
}

void CCY457OpenGLView::OnObjectsSimplecube()
{//画简单立方体
  m_bCube      = FALSE;
  m_bTorus    = FALSE;
  m_bTeapot    = FALSE;
  m_bIcosahedron = FALSE;
  m_bSimpleCube = TRUE;
  InvalidateRect(NULL,FALSE);
}

时间: 2024-08-03 12:10:40

《基于MFC的OpenGL编程》Part 4 Drawing Simple 3D objects的相关文章

《基于MFC的OpenGL编程》Part 2 Setting up OpenGL on Windows

WGL – Windows的 OpenGL扩展层 The WGL extension consists of a set of functions (wglCreateContext, wglDeleteContext etc.) and structures (such as PIXELFORMATDESCRIPTOR, GLYPHMETRICSFLOAT) etc. Thus every OpenGL implementation has a platform-specific portio

《基于MFC的OpenGL编程》Part 19 Creating a Virtual Reality Walkthrough…

<基于MFC的OpenGL编程>Part 19 Creating a Virtual Reality Walkthrough Application 本文是整个系列文章的最后一篇,将创建一个完整的虚拟office应用程序(如图所示)来做为ending. 1,在CCY457OpenGLView类中加入下述变量,用来保存office内各个物体的显示列表 //Display List Names GLuint m_SceneList; GLuint m_ComputerList; GLuint m_

《基于MFC的OpenGL编程》Part 5 Transformations

<基于MFC的OpenGL编程>Part 5 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 incre

《基于MFC的OpenGL编程》Part 3 Drawing Simple 2D Shapes

剪裁区域 In OpenGL when you create a window to draw in we must specify the coordinate system we want to use and how to map the specified coordinates into physical screen coordinates. We would be using the 2D Cartesian coordinate system with the origin 0,

《基于MFC的OpenGL编程》Part 17 Shadows

Shadows Conceptually drawing a shadow is quite simple. A shadow is produced when an object keeps light from a source from striking some object or surface behind the object, casting the shadow. The area on the shadowed object's surface outlined by the

《基于MFC的OpenGL编程》Part 14 Quadrics

本文在第11篇文章的基础上,为其加入显示各种二次曲面的代码: Quadrics Every quadric has a few settings associated with it. We have to create a quadric first and then customize its settings to render the shape we want. The gluNewQuadric function creates a state variable that descr

《基于MFC的OpenGL编程》Part 11 Blending, Antialiasing and Fog

Blending and Transparency Blending in OpenGL provides pixel-level control of RGBA color storage in the color buffer. To enable blending we must first call glEnable(GL_BLEND). We have to set up the blending function glBlendFunc with two arguments: the

《基于MFC的OpenGL编程》Part 1 A Primer

3D图形学基本概念 Perspective Perspective refers to the angles between the lines that lend the illusion of three dimensions. Colors and Shading Moving beyond line drawing, we need to add color to create a solid object. Shading refers to the way the color is

《基于MFC的OpenGL编程》Part 18 Reading objects from the OBJ File Format

本文将介绍如何从Obj文件格式中创建3D对象,我们使用的是Nate Miller的obj格式加载类. This would be very useful to create large Virtual Reality applications as we could make use of the readily available 3D model files or make use of modeling tools to create these models and load them