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

本文介绍OpenGL的基本绘图原理,包括如何设置坐标系、定义视口、指定剪裁区域,并通过实例演示了如何绘制点、线、三角形及多边形等基本图元。

剪裁区域

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,0 at the centre of the screen. Before we can start plotting points, lines and shapes in a window we must also specify how to translate coordinate pairs into screen coordinates, by specifying the clipping area i.e the region of Cartesian space that occupies the window.

视口

The clipping area height and width will rarely match the width and height of the window in pixels. The coordinate system must therefore be mapped from logical Cartesian coordinates to physical screen coordinates. This mapping is specified by a setting known as the viewport, which is the region within the window's client area that is used for drawing the clipping area.

顶点和基本图元

A vertex is nothing more than a coordinate in 2D or 3D space. In both 2D and 3D, when we draw an object we compose it with several smaller shapes called primitives which as 1 or 2 dimensional entities such as points, lines, and polygons. Each corner of an object composed of primitives is a vertex

基本图形绘制程序

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

2,并且加入四个菜单项及其对应的事件处理程序。

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->voidCCY457OpenGLView::OnShapesPoint()
{
//画点
m_bPoint=TRUE;
m_bLine
=FALSE;
m_bPolygon
=FALSE;
m_bTriangle
=FALSE;
InvalidateRect(NULL,FALSE);
}
voidCCY457OpenGLView::OnShapesLine()
{
//画线
m_bPoint=FALSE;
m_bLine
=TRUE;
m_bPolygon
=FALSE;
m_bTriangle
=FALSE;
InvalidateRect(NULL,FALSE);
}
voidCCY457OpenGLView::OnShapesPolygon()
{
//画多边形
m_bPoint=FALSE;
m_bLine
=FALSE;
m_bPolygon
=TRUE;
m_bTriangle
=FALSE;
InvalidateRect(NULL,FALSE);
}
voidCCY457OpenGLView::OnShapesTriangle()
{
//画三角形
m_bPoint=FALSE;
m_bLine
=FALSE;
m_bPolygon
=FALSE;
m_bTriangle
=TRUE;
InvalidateRect(NULL,FALSE);
}

3,修改第二篇文章中的OnSize()函数,因为本文中只绘制2维图形.

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->voidCCY457OpenGLView::OnSize(UINTnType,intcx,intcy)
{
CView::OnSize(nType,cx,cy);
GLdoubleaspect_ratio;
//width/heightratio
if(0>=cx||0>=cy)
{
return;
}
//selectthefullclientarea
::glViewport(0,0,cx,cy);
//computetheaspectratio
//thiswillkeepalldimensionscalesequal
aspect_ratio=(GLdouble)cx/(GLdouble)cy;
//selecttheprojectionmatrixandclearit
::glMatrixMode(GL_PROJECTION);
::glLoadIdentity();
//selecttheviewingvolume
//::gluPerspective(45.0f,aspect_ratio,.01f,200.0f);
::gluOrtho2D(-10.0f,10.0f,-10.0f,10.0f);
//switchbacktothemodelviewmatrixandclearit
::glMatrixMode(GL_MODELVIEW);
::glLoadIdentity();
}

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();
}
elseif(m_bLine==TRUE)
{
glBegin(GL_LINES);
glVertex2f(
0.0f,0.0f);
glVertex2f(
1.0f,0.0f);
glEnd();
}
elseif(m_bTriangle==TRUE)
{
glBegin(GL_TRIANGLES);
glVertex2f(
0.0f,0.0f);
glVertex2f(
2.0f,0.0f);
glVertex2f(
0.0f,2.0f);
glEnd();
}
elseif(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();
}
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值