// 范例P37.cpp : Defines the entry point for the console application.
//
#include <gl/glut.h>
#define drawOneLine(x1,y1,x2,y2) glBegin(GL_LINES); \
glVertex2f((x1),(y1));glVertex2f((x2),(y2));glEnd();
void display()
{
glClear(GL_COLOR_BUFFER_BIT); //清除所有像素
glColor3f(1.0,1.0,1.0); //设置画笔白色
/*在第1行,三条线段不同点画模式*/
glEnable(GL_LINE_STIPPLE);
glLineStipple(1,0x0101);
drawOneLine(50,125,150,125);
glLineStipple(1,0x00ff);
drawOneLine(150,125,250,125);
glLineStipple(1,0x1c47);
drawOneLine(250,125,350,125);
/*在第2行,三条宽线使用不同的点画模式*/
glLineWidth(5);
glLineStipple(1,0x0101);
drawOneLine(50,100,150,100);
glLineStipple(1,0x00ff);
drawOneLine(150,100,250,100);
glLineStipple(1,0x1c47);
drawOneLine(250,100,350,100);
glLineWidth(1);
/*在第3行,6条使用“短划线/点/短划线”模式的线段组成一个相连的线带*/
glLineStipple(1,0x1c47);
glBegin(GL_LINE_STRIP);
for(int i=0;i<7;i++)
glVertex2f((50+i*50),75);
glEnd();
/*在第4行,绘制6条独立,使用相同点画模式的线段*/
for(i=0;i<6;i++)
drawOneLine((50+(i*50)),50,(50+(50*(i+1))),50);//???
/*在第5行,绘制一条线段,点画模式重复因子为5*/
glLineStipple(5,0x1c47);
drawOneLine(50,25,350,25);
glDisable(GL_LINE_STIPPLE);
glFlush();
}
void init()
{
glClearColor(0.0,0.0,0.0,0.0); //用黑色清除背景
/*初始化视景体*/
glShadeModel(GL_FLAT);
}
void reshape(int w,int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,w,0,h);
}
int main(int argc,char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(400,150); //初始化窗口大小
glutInitWindowPosition(100,100); //初始化窗口位置
glutCreateWindow("hello"); //创建标题为“hello”的窗口
init();//调用初始化函数
glutDisplayFunc(display); //注册回调函数
glutReshapeFunc(reshape);
glutMainLoop(); //进入主循环处理事件
return 0;
}
评论