-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu (1).cpp
167 lines (133 loc) · 3.14 KB
/
menu (1).cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include<GL/freeglut.h>
#include<math.h>
#include<GL/gl.h>
struct Point {
GLint x;
GLint y;
};
struct GLColor {
GLfloat red;
GLfloat green;
GLfloat blue;
};
GLColor colors[6] = {
{0.0f, 0.0f, 0.0f}, // Black
{1.0f, 0.0f, 0.0f}, // Red
{0.0f, 1.0f, 0.0f}, // Green
{0.0f, 0.0f, 1.0f}, // Blue
{1.0f, 1.0f, 0.0f}, // Yellow
{1.0f, 0.0f, 1.0f} // Purple
};
GLColor color = colors[0]; // Default: Black
void init() {
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1.0f, 1.0f, 1.0f, 0);
glColor3f(color.red, color.green, color.blue);
glPointSize(1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void display(void) {
}
void draw_dda(Point p1, Point p2) {
GLfloat dx = p2.x - p1.x;
GLfloat dy = p2.y - p1.y;
GLfloat x1 = p1.x;
GLfloat y1 = p1.y;
GLfloat step = 0;
if(abs(dx) > abs(dy)) {
step = abs(dx);
} else {
step = abs(dy);
}
GLfloat xInc = dx/step;
GLfloat yInc = dy/step;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(color.red, color.green, color.blue);
glBegin(GL_POINTS);
for(float i = 1; i <= step; i++) {
glVertex2i(x1, y1);
x1 += xInc;
y1 += yInc;
}
glEnd();
glFlush();
}
void draw_circle(Point pC, GLfloat radius) {
GLfloat step = 1/radius;
GLfloat x, y;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(color.red, color.green, color.blue);
glBegin(GL_POINTS);
for(GLfloat theta = 0; theta <= 360; theta += step) {
x = pC.x + (radius * cos(theta));
y = pC.y + (radius * sin(theta));
glVertex2i(x, y);
}
glEnd();
glFlush();
}
void draw_ellipse(Point pC, GLfloat radiusY, GLfloat radiusX) {
GLfloat step = 1/radiusX;
GLfloat x, y;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(color.red, color.green, color.blue);
glBegin(GL_POINTS);
for(GLfloat theta = 0; theta <= 360; theta += step) {
x = pC.x + (radiusX * cos(theta));
y = pC.y + (radiusY * sin(theta));
glVertex2i(x, y);
}
glEnd();
glFlush();
}
void mainMenuHandler(int choice) {
Point p = {320, 240}; // draw_pixel
Point p1 = {10, 100}; // draw_line
Point p2 = {200, 100}; // --
Point pC = {320, 240}; // Circle center point
GLfloat radius = 200; // Circle radius
switch(choice) {
case 1: // Line
draw_dda(p1, p2);
break;
case 2: // Circle
draw_circle(pC, radius);
break;
case 3: // Ellipse
draw_ellipse(pC, 60.0f, 200.0f);
break;
case 4: // Exit
exit(0);
break;
}
}
void subMenuHandler(int choice) {
color = colors[choice];
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(200, 200);
glutInitWindowSize(640, 480);
glutCreateWindow("OpenGL Circle and Menus");
glutDisplayFunc(display);
init();
int subMenu = glutCreateMenu(subMenuHandler);
glutAddMenuEntry("Default", 0);
glutAddMenuEntry("Red", 1);
glutAddMenuEntry("Green", 2);
glutAddMenuEntry("Blue", 3);
glutAddMenuEntry("Yellow", 4);
glutAddMenuEntry("Purple", 5);
glutCreateMenu(mainMenuHandler);
glutAddSubMenu("Change Color", subMenu);
glutAddMenuEntry("Line", 1);
glutAddMenuEntry("Circle", 2);
glutAddMenuEntry("Ellipse", 3);
glutAddMenuEntry("Exit", 4);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
return 0;
}