forked from sprintr/opengl-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
OpenGL-Menu.cpp
195 lines (159 loc) · 3.95 KB
/
OpenGL-Menu.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright (c) 2013 Amin Ullah Khan. All rights reserved.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Changelog
* 1.1
* - Added support for ellipse
*/
#include <iostream>
#include <gl/glut.h>
#include <math.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() {
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_pixel(Point p) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(color.red, color.green, color.blue);
glBegin(GL_POINTS);
glVertex2i(p.x, p.y);
glEnd();
glFlush();
}
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: // Pixel
draw_pixel(p);
break;
case 2: // Line
draw_dda(p1, p2);
break;
case 3: // Circle
draw_circle(pC, radius);
break;
case 4: // Ellipse
draw_ellipse(pC, 60.0f, 200.0f);
break;
case 5: // 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("Pixel", 1);
glutAddMenuEntry("Line", 2);
glutAddMenuEntry("Circle", 3);
glutAddMenuEntry("Ellipse", 4);
glutAddMenuEntry("Exit", 5);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
return 0;
}