-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain2.cpp
More file actions
109 lines (101 loc) · 3.14 KB
/
Copy pathmain2.cpp
File metadata and controls
109 lines (101 loc) · 3.14 KB
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
105
106
107
108
109
//
// Created by 86155 on 2023/11/23.
//
#include <GL/glut.h>
int shape = 1; // 0表示线段,1表示三角形,2表示四边形,3表示多边形
float colorRed = 1.0, colorGreen = 1.0, colorBlue = 1.0; // 初始化颜色为白色
double posX = 0, posY = 0; // 初始化图形位置为原点
void drawShape() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(colorRed, colorGreen, colorBlue);
if (shape == 0) {
glBegin(GL_LINES);
glVertex2f(-0.5 + posX, 0.0 + posY);
glVertex2f(0.5 + posX, 0.0 + posY);
glEnd();
} else if (shape == 1) {
glBegin(GL_TRIANGLES);
glVertex2f(-0.5 + posX, -0.5 + posY);
glVertex2f(0.5 + posX, -0.5 + posY);
glVertex2f(0.0 + posX, 0.5 + posY);
glEnd();
} else if (shape == 2) {
glBegin(GL_QUADS);
glVertex2f(-0.5 + posX, -0.5 + posY);
glVertex2f(0.5 + posX, -0.5 + posY);
glVertex2f(0.5 + posX, 0.5 + posY);
glVertex2f(-0.5 + posX, 0.5 + posY);
glEnd();
} else if (shape == 3) {
glBegin(GL_POLYGON);
glVertex2f(-0.5 + posX, -0.5 + posY);
glVertex2f(0.5 + posX, -0.5 + posY);
glVertex2f(0.7 + posX, 0.0 + posY);
glVertex2f(0.5 + posX, 0.5 + posY);
glVertex2f(-0.5 + posX, 0.5 + posY);
glEnd();
}
glFlush();
}
void keyboard(unsigned char key, int x, int y) {
if (key == 't') {
shape = 1;
} else if (key == 's') {
shape = 2;
} else if (key == 'd') {
shape = 3;
} else if (key == 'l') {
shape = 0;
} else if (key == 'r') {
colorRed = 1.0; colorGreen = 0.0; colorBlue = 0.0; // 红色
} else if (key == 'g') {
colorRed = 0.0; colorGreen = 1.0; colorBlue = 0.0; // 绿色
} else if (key == 'b') {
colorRed = 0.0; colorGreen = 0.0; colorBlue = 1.0; // 蓝色
}
drawShape();
}
void specialKeys(int key, int x, int y) {
if (key == GLUT_KEY_UP) {
posY += 0.3;
} else if (key == GLUT_KEY_DOWN) {
posY -= 0.3;
} else if (key == GLUT_KEY_LEFT) {
posX -= 0.3;
} else if (key == GLUT_KEY_RIGHT) {
posX += 0.3;
}
drawShape();
}
void mouse(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
shape = 2; // 左键点击改为四边形
} else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
colorRed = 1.0; colorGreen = 0.5; colorBlue = 0.0; // 右键点击改为橙色
}
drawShape();
}
void myreshape(GLint w, GLint h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
gluOrtho2D(-2.0, 2.0, -2.0*(GLfloat)h/(GLfloat)w,
2.0*(GLfloat)h/(GLfloat)w);
else gluOrtho2D(-2.0*(GLfloat)w/(GLfloat)h,
2.0*(GLfloat)w/(GLfloat)h, -2.0, 2.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutCreateWindow("Interactive Program");
glutInitWindowSize(400, 400);
glutDisplayFunc(drawShape);
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialKeys);
glutMouseFunc(mouse);
glutReshapeFunc(myreshape);
glOrtho(-1, 1, -1, 1, -1, 1);
glutMainLoop();
return 0;
}