-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
200 lines (170 loc) · 6.23 KB
/
main.cpp
File metadata and controls
200 lines (170 loc) · 6.23 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
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
196
197
198
199
200
#include "proj.h"
std::vector<point> control_points;
std::vector<std::tuple<point, int>> circles;
int DLflag = 0, circleFlag = 0;
point final_point = {-1,-1};
std::vector<line> drawingLines = {
{435, 550, 460, 460},{460, 460, 480, 520},
{480, 520, 500, 460},{500, 460, 525, 550},
{535, 460, 570, 550},{570, 550, 605, 460},
{555, 510, 585, 510},{620, 550, 620, 460},
{620, 505, 660, 550},{620, 505, 660, 460},
{685, 460, 685, 550},{685, 550, 730, 550},
{685, 505, 730, 505},{685, 460, 730, 460},
};
void openGLLine(float x1, float y1, float x2, float y2) {
glBegin(GL_LINES);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glEnd();
}
void openGLDrawing() { // "wake" line drawing
glColor3f(1, 1, 1);
// draw with OpenGL lines
for (int i = 0; i < drawingLines.size(); i++) {
openGLLine(drawingLines[i].x1, drawingLines[i].y1, drawingLines[i].x2, drawingLines[i].y2);
}
// draw with OpenGL lines translated down with stipple, color, and thickness
glLineStipple(1,0x0FFF);
glEnable(GL_LINE_STIPPLE);
for (int i = 0; i < 4; i++) {
openGLLine(drawingLines[i].x1, drawingLines[i].y1-200, drawingLines[i].x2, drawingLines[i].y2-200);
}
glLineStipple(1, 0x1C47);
for (int i = 4; i < 7; i++) {
openGLLine(drawingLines[i].x1, drawingLines[i].y1-200, drawingLines[i].x2, drawingLines[i].y2-200);
}
glDisable(GL_LINE_STIPPLE);
glLineWidth(4.0);
glColor3f(1.0f, 0, 0);
for (int i = 7; i < 10; i++) {
openGLLine(drawingLines[i].x1, drawingLines[i].y1-200, drawingLines[i].x2, drawingLines[i].y2-200);
}
glLineWidth(1);
glColor3f(1, 1, 1);
glLineStipple(1, 0x00FF);
glEnable(GL_LINE_STIPPLE);
for (int i = 10; i < 14; i++) {
openGLLine(drawingLines[i].x1, drawingLines[i].y1-200, drawingLines[i].x2, drawingLines[i].y2-200);
}
glDisable(GL_LINE_STIPPLE);
}
void GSDrawing() {
for (int i = 0; i < drawingLines.size(); i++) {
GSLine(drawingLines[i].x1-400, drawingLines[i].y1, drawingLines[i].x2-400, drawingLines[i].y2, GS_WHITE, GS_SOLID, 1);
}
// second line
for (int i = 0; i < 4; i++) {
GSLine(drawingLines[i].x1-400, drawingLines[i].y1-200, drawingLines[i].x2-400, drawingLines[i].y2-200, GS_WHITE, GS_3Q, 1);
}
for (int i = 4; i < 7; i++) {
GSLine(drawingLines[i].x1-400, drawingLines[i].y1-200, drawingLines[i].x2-400, drawingLines[i].y2-200, GS_WHITE, GS_FUNKY, 1);
}
for (int i = 7; i < 10; i++) {
GSLine(drawingLines[i].x1-400, drawingLines[i].y1-200, drawingLines[i].x2-400, drawingLines[i].y2-200, GS_RED, GS_SOLID, 3);
}
for (int i = 10; i < 14; i++) {
GSLine(drawingLines[i].x1-400, drawingLines[i].y1-200, drawingLines[i].x2-400, drawingLines[i].y2-200, GS_WHITE, GS_DASHED, 1);
}
}
void init()
{
// clear color buffer
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
// set the viewport as the full screen
glViewport(0, 0, 800, 600);
// set the projection as orthogonal projection
glMatrixMode(GL_PROJECTION);
glOrtho(0.0, 800.0, 0.0, 600.0, 1.0, -1.0);
// set the matrix mode back to modelview mode
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// draw "wake" with openGL and GS implementation
openGLDrawing();
GSDrawing();
midpoint_ellipse(300, 310, 200, 100);
for (int i = 0; i < circles.size(); i++) {
midpoint_circle(std::get<0>(circles[i]).x, std::get<0>(circles[i]).y, std::get<1>(circles[i]));
}
/* draw lines determined by control points and dynamic final
point using GS implementation */
if (circleFlag == 0) { // only do when not circle drawing
for (int i = 0; i < ((int)control_points.size())-1; i++) {
GSLine(control_points[i].x, control_points[i].y, control_points[i+1].x, control_points[i+1].y, GS_WHITE, GS_SOLID, 1);
}
if (final_point.x != -1) {
GSLine(control_points[control_points.size()-1].x, control_points[control_points.size()-1].y, final_point.x, final_point.y, GS_WHITE, GS_SOLID, 1);
}
}
glutSwapBuffers();
}
// key handlers
void processKeys(unsigned char key, int x, int y) {
switch (key) {
case 'f':
exit(0);
break;
case 'c': // controls whether program is in "circle" mode
if (circleFlag == 0) {
control_points.clear();
final_point = {-1, -1};
circleFlag = 1;
glutPostRedisplay();
}
else if (circleFlag == 1) circleFlag = 0;
break;
}
}
void processMouse(int button, int state, int x, int y) {
if (circleFlag == 0) {
// start new drawing with control point
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
control_points.clear();
control_points.push_back({x, 600-y});
final_point = {x, 600-y};
DLflag = 1;
glutPostRedisplay();
} // add new control point
else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN && DLflag == 1) {
control_points.push_back({x, 600-y});
glutPostRedisplay();
}
} else {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
// add to circle list
circles.push_back(std::make_tuple(point {x, 600-y}, 100));
glutPostRedisplay();
}
}
}
void dynamicDraw(int x, int y) {
// update final point on mouse movement
if (circleFlag == 0) {
if (DLflag == 1) final_point = {x, 600-y};
glutPostRedisplay();
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
// create a window
glutInitWindowPosition(0, 0);
glutInitWindowSize(800, 600);
glutCreateWindow("Computer Graphics");
// init settings
init();
glutDisplayFunc(display);
// register a callback for keyboard events
glutKeyboardFunc(processKeys);
// register callback for mouse
glutMouseFunc(processMouse);
// register callback for passive mouse movement
glutPassiveMotionFunc(dynamicDraw);
// enter main loop
glutMainLoop();
return 0;
}