1 #include "stdafx.h" 2 #include <GL/glut.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <stdio.h> 6 7 using namespace std; 8 9 10 void myDisplay(void){ 11 glClear(GL_COLOR_BUFFER_BIT); 12 //glRectf(-0.5f, -0.5f, 0.5f, 0.5f); 13 14 glBegin(GL_LINES); 15 glVertex2f(-0.5f, -0.5f); //直线起始坐标 16 glVertex2f(0.5f, 0.5f); //直线结束坐标 17 glEnd(); 18 19 glBegin(GL_LINES); 20 glVertex2f(-0.5f, 0.5f); 21 glVertex2f(0.5f, -0.5f); 22 glEnd(); 23 24 glBegin(GL_LINES); 25 glVertex2f(-0.5f, 0.5f); 26 glVertex2f(-0.5f, -0.5f); 27 glEnd(); 28 29 glBegin(GL_LINES); 30 glVertex2f(0.5f, 0.5f); 31 glVertex2f(0.5f, -0.5f); 32 glEnd(); 33 34 glBegin(GL_LINES); 35 glVertex2f(-0.5f, 0.5f); 36 glVertex2f(0.5f, 0.5f); 37 glEnd(); 38 39 glBegin(GL_LINES); 40 glVertex2f(-0.5f, -0.5f); 41 glVertex2f(-0.5f, 0.5f); 42 glEnd(); 43 44 glBegin(GL_LINES); 45 glVertex2f(0, 0.5f); 46 glVertex2f(0, -0.5f); 47 glEnd(); 48 49 glBegin(GL_LINES); 50 glVertex2f(-0.5f, -0.5f); 51 glVertex2f(0.5, -0.5f); 52 glEnd(); 53 54 glBegin(GL_LINES); 55 glVertex2f(-0.5f, 0); 56 glVertex2f(0.5, 0); 57 glEnd(); 58 59 glFlush(); 60 } 61 int main(int argc, char *argv[]) { 62 glutInit(&argc, argv); 63 glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); 64 glutInitWindowPosition(100, 100); 65 glutInitWindowSize(500, 500); 66 glutCreateWindow("第一个 OpenGL 程序"); 67 glutDisplayFunc(&myDisplay); 68 glutMainLoop(); 69 return 0; 70 }
运行图像: