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

来源:互联网 发布:淘宝店消保金 编辑:程序博客网 时间:2024/06/15 14:36

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

基于MFC的OpenGL编程需要基于OpenGL的框架才能实现,后面的文章都是基于这个基本的框架。详情见:

《基于MFC的OpenGL编程》Part 0 基于MFC的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,2OpenGLView.h中加入下列变量:

<pre name="code" class="cpp">BOOL m_bPoint;        //Status of Point    BOOL m_bLine;        //Status of Line    BOOL m_bPolygon;    //Status of Polygon    BOOL m_bTriangle;    //Status of Triangle

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

注意:1、菜单项在Resource中的ToolBar中添加设置;

      2、添加四个菜单按钮工具:ID设置:ID_SHAPES_POINT、ID_SHAPES_LINE、ID_SHAPES_POLYGEN、ID_SHAPES_TRIANGLE

       提示(与ID对应):画点\n点、画一条直线\n直线、画一个多边形\n多边形、画三角形\n三角形;

      3、类视图中添加类向导的消息映射函数:CMy2OpenGLView中添加类向导(CTRL+W),分别为D_SHAPES_POINT、ID_SHAPES_LINE、                               ID_SHAPES_POLYGEN、ID_SHAPES_TRIANGLE添加Command函数,会分别创建四个对应函数void CMy2OpenGLView::OnShapesPoint() 、

void CMy2OpenGLView::OnShapesLine()、void CMy2OpenGLView::OnShapesPolygen() 、void CMy2OpenGLView::OnShapesTriangle();

代码如下:

void CMy2OpenGLView::OnShapesPoint() {// TODO: Add your command handler code here//画点    m_bPoint = TRUE;    m_bLine = FALSE;    m_bPolygon = FALSE;    m_bTriangle = FALSE;    InvalidateRect(NULL,FALSE); }void CMy2OpenGLView::OnShapesLine() {// TODO: Add your command handler code here//画线    m_bPoint = FALSE;    m_bLine = TRUE;    m_bPolygon = FALSE;    m_bTriangle = FALSE;            InvalidateRect(NULL,FALSE); }void CMy2OpenGLView::OnShapesPolygen() {// TODO: Add your command handler code here//画多边形    m_bPoint = FALSE;    m_bLine     = FALSE;    m_bPolygon = TRUE;    m_bTriangle = FALSE;            InvalidateRect(NULL,FALSE); }void CMy2OpenGLView::OnShapesTriangle() {// TODO: Add your command handler code here//画三角形    m_bPoint = FALSE;    m_bLine     = FALSE;    m_bPolygon = FALSE;    m_bTriangle = TRUE;            InvalidateRect(NULL,FALSE);}
3,修改第二篇文章中(转载原文)的OnSize()函数,因为本文中只绘制2维图形.

void CMy2OpenGLView::OnSize(UINT nType, int cx, int cy) {CView::OnSize(nType, cx, cy);// TODO: Add your message handler code hereGLdouble aspect_ratio; // width/height ratio        if ( 0 >= cx || 0 >= cy )    {        return;    }    // select the full client area    ::glViewport(0, 0, cx, cy);    // compute the aspect ratio    // this will keep all dimension scales equal    aspect_ratio = (GLdouble)cx/(GLdouble)cy;    // select the projection matrix and clear it    ::glMatrixMode(GL_PROJECTION);    ::glLoadIdentity();    // select the viewing volume//gluOrtho2D(60.0,100.0*aspect_ratio,0.0,100.0);gluOrtho2D(-10.0,10.0,-10.0,10.0);//画三个点时,一点要设置成这样,不然编译出来显示不了图形//主要是这个函数,注意参数设置,不然看不到效果的    //gluPerspective(60.0f,500* aspect_ratio, 0.1f, 500.0f);        // switch back to the modelview matrix and clear it    ::glMatrixMode(GL_MODELVIEW);    ::glLoadIdentity();::glDrawBuffer(GL_BACK);}

4,RenderScene中加入具体的绘制代码(代码中有第一篇原文文章中的测试代码,可以忽略掉,同时对原文中的四个画图的点的坐标经行了修改):

此处有个注意点:一定要在glEnd();代码后面加上glFlush();来触发绘图,这句代码没在RenderScene()中写出来,但是在void   CMy2OpenGLView::OnDraw(CDC* pDC)的代码里面必须加上,当然,也可以直接在RenderScene()中写出来,OnDraw中就可以不写了。

void CMy2OpenGLView::RenderScene (){//第一个玩具嘛,先空着,后面慢慢填/*    //Clear the color buffer     glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);    glLoadIdentity();        glTranslatef(200.0,200.0,0.0);    glBegin(GL_TRIANGLES);    glColor4f(0.0f,1.0f,0.0f,0.0f);glVertex3f(0.0f,100.0f,0.0f);    glColor4f(1.0f,1.0f,0.0f,0.0f);    glVertex3f(-100.0f,0.0f,0.0f);    glColor4f(255.0f,0.0f,0.0f,0.0f);    glVertex3f(100.0f,0.0f,0.0f);    glEnd();            //Flush the rendering pipeline           glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glLoadIdentity();glTranslatef(100.0,200.0,0.0);glBegin(GL_POLYGON);glColor3f(0.0f,255.0f,255.0f);glVertex3f(-1.0,-2.0,0.0);glColor3f(0.0f,0.0f,255.0f);glVertex3f(1.0,2.0,0.0);glColor3f(0.0f,255.0f,0.0f);glVertex3f(3.0,4.0,0.0);glColor3f(255.0f,255.0f,0.0f);glVertex3f(2.0,2.0,0.0);glColor3f(0.0f,0.0f,255.0f);glVertex3f(0.0,1.0,0.0);glColor3f(155.0f,255.0f,255.0f); glVertex3f(-1.0,-2.0,-0.0);glEnd();glFlush(); glLoadIdentity();  glClear(GL_COLOR_BUFFER_BIT); glTranslatef(-50.0,-350.0,0.0);    glBegin(GL_POLYGON);        glColor4f(1.0f,0.0f,0.0f,1.0f);        glVertex2f(200.0f,250.0f);        glColor4f(0.0f,1.0f,0.0f,1.0f);        glVertex2f(650.0f,600.0f);        glColor4f(0.0f,0.0f,1.0f,1.0f);        glVertex2f(650.0f,250.0f);    glEnd();    glFlush();*///绘制函数    if(m_bPoint==TRUE)    {        glPointSize(3.0f);        glBegin(GL_POINTS);//坐标原点在窗口视图的中间位置glVertex2f(0.0f,0.0f);glVertex2f(1.0f,0.0f);glVertex2f(0.5f,1.0f);        glEnd();    }    else if(m_bLine==TRUE)    {        glBegin(GL_LINES);glVertex2f(-1.0f,-1.0f);glVertex2f(1.0f,1.0f);        glEnd();    }    else if(m_bTriangle==TRUE)    {        glBegin(GL_TRIANGLES);glVertex2f(-2.0f,0.0f);glVertex2f(2.0f,0.0f);glVertex2f(0.0f,4.0f);        glEnd();    }    else 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();}} BOOL CMy2OpenGLView::OnEraseBkgnd(CDC* pDC)  { // TODO: Add your message handler code here and/or call default  return TRUE; }

运行结果:







0 0
原创粉丝点击