《基于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,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中加入下列变量:

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

复制代码
void CCY457OpenGLView::OnShapesPoint() 
{//画点
    m_bPoint = TRUE;
    m_bLine = FALSE;
    m_bPolygon = FALSE;
    m_bTriangle = FALSE;
    InvalidateRect(NULL,FALSE);    
}
void CCY457OpenGLView::OnShapesLine() 
{//画线
    m_bPoint = FALSE;
    m_bLine = TRUE;
    m_bPolygon = FALSE;
    m_bTriangle = FALSE;        
    InvalidateRect(NULL,FALSE);    
}
void CCY457OpenGLView::OnShapesPolygon() 
{//画多边形
    m_bPoint = FALSE;
    m_bLine     = FALSE;
    m_bPolygon = TRUE;
    m_bTriangle = FALSE;        
    InvalidateRect(NULL,FALSE);    
}
void CCY457OpenGLView::OnShapesTriangle() 
{//画三角形
    m_bPoint = FALSE;
    m_bLine     = FALSE;
    m_bPolygon = FALSE;
    m_bTriangle = TRUE;        
    InvalidateRect(NULL,FALSE);    
}
复制代码
3,修改第二篇文章中的OnSize()函数,因为本文中只绘制2维图形.

复制代码
void CCY457OpenGLView::OnSize(UINT nType, int cx, int cy) 
{
    CView::OnSize(nType, cx, cy);
    GLdouble 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
    //::gluPerspective(45.0f, aspect_ratio, .01f, 200.0f);
    ::gluOrtho2D(-10.0f, 10.0f, -10.0f, 10.0f);    
    // switch back to the modelview matrix and clear it
    ::glMatrixMode(GL_MODELVIEW);
    ::glLoadIdentity();
}
复制代码
4,在RenderScene中加入具体的绘制代码:

复制代码
void CCY457OpenGLView::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();
    }
    else if(m_bLine==TRUE)
    {
        glBegin(GL_LINES);
            glVertex2f(0.0f,0.0f);
            glVertex2f(1.0f,0.0f);
        glEnd();
    }
    else if(m_bTriangle==TRUE)
    {
        glBegin(GL_TRIANGLES);
            glVertex2f(0.0f,0.0f);
            glVertex2f(2.0f,0.0f);
            glVertex2f(0.0f,2.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();
    }
}
复制代码


本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/11/05/1327343.html,如需转载请自行联系原作者
目录
相关文章
|
Android开发 异构计算
Android OpenGL ES(八)----纹理编程框架(二)
Android OpenGL ES(八)----纹理编程框架(二)
190 0
Android OpenGL ES(八)----纹理编程框架(二)
|
存储 Java API
Android OpenGL ES(八)----纹理编程框架(一)
Android OpenGL ES(八)----纹理编程框架(一)
317 0
Android OpenGL ES(八)----纹理编程框架(一)
|
Android开发
Android OpenGL ES(三)----编程框架(二)
Android OpenGL ES(三)----编程框架(二)
121 0
Android OpenGL ES(三)----编程框架(二)
|
Java API Android开发
Android OpenGL ES(三)----编程框架(一)
Android OpenGL ES(三)----编程框架(一)
127 0
|
iOS开发 异构计算
了解 OpenGL ES实现自定义编程粒子效果 思路
本案例旨在于了解OpenGL ES中自定义编程粒子效果的整体实现思路。
199 0
了解 OpenGL ES实现自定义编程粒子效果 思路
|
6月前
|
XML 小程序 Java
【Android App】三维投影OpenGL ES的讲解及着色器实现(附源码和演示 超详细)
【Android App】三维投影OpenGL ES的讲解及着色器实现(附源码和演示 超详细)
121 0
|
缓存 C++
Opengl ES之FBO
Opengl ES连载系列
146 0