代码改变世界

OpenGL直线点画模式

2014-04-02 22:57  采药的蜗牛  阅读(1859)  评论(0编辑  收藏  举报

程序来自《OpenGL编程指南第七版》

 1 #include "stdafx.h"
 2 #include <Windows.h>//头文件一定要加上,因为windows版本的gl.h和glu.h文件内部使用的宏是在windows.h中定义的
 3 #include <gl/GL.h>
 4 #include <gl/GLU.h>
 5 #include <gl/glut.h>
 6 static GLfloat spin=0.0;
 7 int drawOneLine(float x1,float y1,float x2,float y2) 
 8 {
 9     glBegin(GL_LINES);
10     glVertex2f((x1),(y1));
11     glVertex2f((x2),(y2));
12     glEnd();
13     return 0;
14 }
15 
16 void init (void)
17 {
18     glClearColor(0.5,0.5,0.5,0.0);
19     glShadeModel(GL_FLAT);
20 }
21 void display(void)
22 {
23     int i;
24     glClear(GL_COLOR_BUFFER_BIT);
25     glColor3f(1.0,1.0,0.0);
26     glEnable(GL_LINE_STIPPLE);
27     glLineStipple(1,0x0101);//注意glLineStipple用法以及每个参数的意义
28     drawOneLine(50.0,125.0,150.0,125.0);
29     glLineStipple(1,0x00FF);
30     drawOneLine(150.0,125.0,250.0,125.0);
31     glLineStipple(1,0x1C47);
32     drawOneLine(250.0,125.0,350.0,125.0);
33     glLineWidth(5.0);
34     glLineStipple(1,0x0101);
35     drawOneLine(50.0,100.0,150.0,100.0);
36     glLineStipple(1,0x00FF);
37     drawOneLine(150.0,100.0,250.0,100.0);
38     glLineStipple(1,0x1C47);
39     drawOneLine(250.0,100.0,350.0,100.0);
40     glLineWidth(1.0);
41     glLineStipple(1,0x1C47);
42     glBegin(GL_LINE_STRIP);
43         for (i=0;i<7;i++)
44         glVertex2f(50.0+((GLfloat)i+50.0),75.0);
45     glEnd();
46     for (i=0;i<6;i++)
47         drawOneLine(50.0+((GLfloat)i+50.0),50.0,50.0+((GLfloat)(i+1)+50.0),50.0);
48     glLineStipple(5,0x1C47);
49     drawOneLine(50.0,25.0,350.0,25.0);
50     glDisable(GL_LINE_STIPPLE);
51     glFlush();
52 }
53 
54 void reshape(int w,int h)
55 {
56     glViewport(0,0,(GLsizei)w,(GLsizei)h);
57     glMatrixMode(GL_PROJECTION);
58     glLoadIdentity();
59     gluOrtho2D(0.0,(GLdouble)w,0.0,(GLdouble)h);
60 }
61 
62 int main(int argc,char**argv)
63 {
64     glutInit(&argc,argv);
65     glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
66     glutInitWindowSize(400,150);
67     glutInitWindowPosition(100,100);
68     glutCreateWindow(argv[0]);
69     init();
70     glutDisplayFunc(display);
71     glutReshapeFunc(reshape);
72     glutMainLoop();
73     return 0;
74 }



运行的效果图: