Opengl画线方法还是比较简单的。
Opengl画线一般步骤,设置线颜色、线型等属性值,开始绘制,设置线起点与终点坐标。
画线代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
#include <windows.h> #include <GL/glut.h> void init(void); void reshape(int w,int h); void display(void); void drawGrid(int xmin, int xmax, int ymin, int ymax); #define NGRID 6 double pnts[][2] = { 0, 6, 1, 0, 2, 6, 3, 0, 4, 6, 5, 0, 6, 6 }; void init(void) { } void reshape(int w, int h) { glViewport(0, 0, w, h); } void display(void) { int i, n=6; glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, NGRID, 0.0, NGRID); //窗口坐标范围 glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //画网格 glColor3f(0.0f, 1.0f, 0.0f); //绿色 drawGrid(0, NGRID, 0, NGRID); //画控制点 glColor3f(1.0f, 0.0f, 0.0f); //红色 glPointSize(10.0f); //点大小 for(i=0; i<=n; i++) { glBegin(GL_POINTS); glVertex2d(pnts[i][0], pnts[i][1]); glEnd(); } //画折线 glColor3f(1.0f, 1.0f, 1.0f); //白色 for(i=0; i<n; i++) { glBegin(GL_LINES); glVertex2d(pnts[i][0], pnts[i][1]); glVertex2d(pnts[i+1][0], pnts[i+1][1]); glEnd(); } glFlush(); } void drawGrid(int xmin, int xmax, int ymin, int ymax) { int i, j; for(j=ymin; j<=ymax; j++) //水平线 { glBegin(GL_LINES); glVertex2d(xmin, j); glVertex2d(xmax, j); glEnd(); } for(i=xmin; i<=xmax; i++) //竖线 { glBegin(GL_LINES); glVertex2d(i, ymin); glVertex2d(i, ymax); glEnd(); } } int main(int argc,char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("drawLines"); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0; } |
运行结果: