绘制图形与3D增强技巧(二)----直线图元之点画

一.直线的点画模式:即并不完全填充所有像素来画一条直线,而是用点画的形式,间隔地画一条直线

首先启用点画模式:

glEnable(GL_LINE_STIPPLE);

然后自定义创建自己的点画模式

glLineStipple(GLlint factor,GLushort pattern);

其中第一个参数factor为乘法因子,第二个参数pattern为自定义的点画模式:一个16位的值,每个位表示线段的一部分状态为打开或者关闭,采用16进制,例如:0x5555

直线点画的实例程序:

// STIPPLE.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<GL\glut.h>
void Init()
{
    glClearColor(0.0f,0.0f,0.0f,0.0f);


}

void MyDisplay()
{
    glClear(GL_COLOR_BUFFER_BIT);

    GLint factor=1;
    GLushort pattern=0x5555;
    GLfloat y;
    glEnable(GL_LINE_STIPPLE);
    
    

    glColor3f(0.0,1.0f,0.0f);

    
    for ( y = -80.0f; y < 80.0f; y+=20.0f)
    {
    glLineStipple(factor,pattern);
    glBegin(GL_LINES);
    glVertex2f(-80.0f,y);
    glVertex2f(80.0,y);
    factor++;
    glEnd();
    
    }
    
    glutSwapBuffers();

}
void ChangeSize(int w,int h)
{
    if (h==0)
    {
        h=1;
    }
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (w<=h)
    {
        gluOrtho2D(-100.0,100.0,-100.0*(GLfloat)h/(GLfloat)w,100.0*(GLfloat)h/(GLfloat)w);
    }
    else
    {
        gluOrtho2D(-100.0*(GLfloat)w/(GLfloat)h,100.0*(GLfloat)w/(GLfloat)h,-100.0,100.0);
    }
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    

}
int main(int argc, char ** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
    glutInitWindowPosition(500,300);
    glutInitWindowSize(500,500);
    glutCreateWindow("Line_STIPPLE");

    Init();
    glutDisplayFunc(MyDisplay);
    glutReshapeFunc(ChangeSize);
    
    glutMainLoop();



}

 

posted @ 2016-07-23 20:17  LT.C#  阅读(356)  评论(0编辑  收藏  举报