opengl画网格,画横线11条,画竖线11条,再画对角线与网格交点打白点。
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 |
#include <windows.h> #include <GL/glut.h> void init(void); void reshape(int w, int h); void display(void); void init(void) { } void reshape(int w, int h) { glViewport(0, 0, w, h); } void display(void) { int i; glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-1.0, 11.0, -1.0, 11.0); //窗口坐标范围 glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //画10*10网格 glColor3f(0.0f, 1.0f, 0.0f); //绿色 for (i = 0; i <= 10; i++) //11条水平线 { glBegin(GL_LINES); glVertex2d(0.0, i * 1.0); glVertex2d(10.0, i * 1.0); glEnd(); } glBegin(GL_LINES); //11条竖线 for (i = 0; i <= 10; i++) { glVertex2d(i * 1.0, 0.0); glVertex2d(i * 1.0, 10.0); } glEnd(); //在对角线画点 glColor3f(1.0f, 1.0f, 1.0f); //白色 glPointSize(10.0f); //点大小 glBegin(GL_POINTS); for (i = 0; i <= 10; i++) glVertex2d(i * 1.0, i * 1.0); glEnd(); for (i = 0; i <= 10; i++) { glBegin(GL_POINTS); glVertex2d(i * 1.0, 10.0 - i * 1.0); glEnd(); } glFlush(); } int main(int argc,char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(0, 0); glutCreateWindow("GL_0_2d"); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0; } |
opengl画网格测试结果: