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
| #include <math.h> #include <windows.h> #include <gl/gl.h> #include <gl/glu.h> #include <gl/glut.h> #pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
const int WINDOW_WIDTH = 640; const int WINDOW_HEIGHT = 480; const int WINDOW_POS_X = 300; const int WINDOW_POS_Y = 150; const int NUM = 8; const int BOARD_WIDTH = WINDOW_WIDTH / NUM; const int BOARD_HEIGHT = WINDOW_HEIGHT / NUM; const GLfloat r1 = 0.0, g1 = 0.0, b1 = 0.0; const GLfloat r2 = 1.0, g2 = 1.0, b2 = 1.0;
struct GLintPoint { GLint x; GLint y; };
void myInit() { glClearColor(1.0, 1.0, 1.0, 0.0); glColor3f(0.0f, 0.0f, 0.0f); glPointSize(2.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, WINDOW_WIDTH, 0.0, WINDOW_HEIGHT); }
void drawRectangleCenter(GLintPoint center, double fWidth, double fHeight) { glRectf(center.x - fWidth / 2.0, center.y - fHeight / 2.0, center.x + fWidth / 2.0, center.y + fHeight / 2.0); }
void drawRectangleCornersize(GLintPoint topleft, double fHeight, double fScale) { glRectf(topleft.x, topleft.y, topleft.x + fHeight * fScale, topleft.y + fHeight); }
void myDisplay() { glClear(GL_COLOR_BUFFER_BIT); GLintPoint center, topleft;
for (int i = 0; i < NUM; ++i) { for (int j = 0; j < NUM; ++j) { if ((i + j) % 2 == 0) { glColor3f(r1, g1, b1); center.x = i * BOARD_WIDTH + BOARD_WIDTH / 2.0; center.y = j * BOARD_HEIGHT + BOARD_HEIGHT / 2.0; drawRectangleCenter(center, BOARD_WIDTH, BOARD_HEIGHT); } else { glColor3f(r2, g2, b2); topleft.x = i * BOARD_WIDTH; topleft.y = j * BOARD_HEIGHT; drawRectangleCornersize(topleft, BOARD_HEIGHT, BOARD_HEIGHT * 1.0 / BOARD_WIDTH); } } } glFlush(); }
int main(int argc, char** argv) { glutInit(argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); glutInitWindowPosition(WINDOW_POS_X, WINDOW_POS_Y); glutCreateWindow("画矩形"); glutDisplayFunc(myDisplay); myInit(); glutMainLoop();
return 0; }
|