-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_main.cpp
593 lines (522 loc) · 14.6 KB
/
01_main.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
* Lab 01 - Bézier Curves
*
* Solution by Michele Righi (0001025005)
* GitHub: https://github.com/mikyll/Fondamenti-di-Computer-Graphics-M/tree/main/laboratorio/01es
*
*
* Description:
* This program allows the user to draw Beziér curves by adding control points
* and selecting the algorithm.
*
* Usage:
* - Left click to place a control point.
* (Maximum number of control points allowed is currently set at 300)
* - Drag and drop the mouse while hovering a control point to move it.
* - Press 'f' to remove the first control point.
* - Press 'l' to remove the last control point.
* - Press 'i' to print control points.
* - Press 'o' to print curve points.
* - Press 's' to show control points/segments/curve.
* - Press 'c'/'r' to clear (reset) control points and curve points.
* - Press ESC to exit.
*
* Curves rendering:
* - (default) Press 'F1' to select "de Casteljau Uniform" method.
* - Press 'F2' to select "Catmull-Rom Spline" method.
* - Press 'F3' to select "de Casteljau Adaptive Subdivision" method.
* - Press 'F4' to select "cubic segments" subdivision.
*/
#include <iostream>
#include "ShaderMaker.h"
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <vector>
// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform.hpp>
// Personal Libraries
#include "defs.h"
#include "utils.h"
// Exercises
#include "01_3.h"
#include "01_5a.h"
#include "01_5b.h"
#include "01_5c.h" // TO-DO
enum {
DRAW_EVERYTHING = 0,
DRAW_ONLY_CONTROL_POINTS,
DRAW_CONTROL_POINTS_AND_SEGMENTS,
DRAW_CONTROL_POINTS_AND_CURVE,
DRAW_ONLY_CURVE,
} DRAW_MODE;
// FUNCTION DECLARATIONS ========================
static void initShader();
static void init();
static void printControls();
static void clearControlPoints();
static void clearCurvePoints();
static void clearAllPoints();
static void addNewPoint(Point2D point);
static void addNewPoint(float x, float y);
static void removeFirstPoint();
static void removeLastPoint();
static void printControlPoints();
static void printCurvePoints();
static void update(int value);
static void resizeWindow(int w, int h);
static void inputKeyboard(unsigned char key, int x, int y);
static void inputMouseClick(int button, int state, int x, int y);
static void inputMouseDrag(int x, int y);
static void inputMousePassiveMove(int x, int y);
static void drawScene();
// GLOBAL VARIABLES =============================
static unsigned int programId;
// Window size in pixels
int width = 500;
int height = 500;
// Vertex Array Objects & Vertex Buffer Objects
// Control Polygon (Points and Segments)
unsigned int VAO_ControlPoints;
unsigned int VBO_ControlPoints;
// Curves
unsigned int VAO_CurvePoints;
unsigned int VBO_CurvePoints;
// Size of the Control Points
float ctrlPointSize = 8.0;
int incrementCtrlPtSize = 0;
// Current control points
Point2D ctrlPointArray[MAX_NUM_PTS];
int numCtrlPts = 0;
// Current curve points
Point2D curvePointArray[(MAX_NUM_PTS*2) - 1];
int numCurvePts = 0;
// Index of the Control Point the mouse is hovering
int iHoverCtrlPt = -1;
// Used to detect while the user is dragging the mouse
int dragging = 0;
// Function pointer to calculate the curve
static void (*getCurve)(Point2D* ctrlPts, int numCtrl, Point2D* curvePts, int* numCurve);
int drawMode = DRAW_EVERYTHING;
// INIT ===================================================
static void initShader()
{
GLenum ErrorCheckValue = glGetError();
char* vertexShader = (char*)"resources/shaders/vertexShader.glsl";
char* fragmentShader = (char*)"resources/shaders/fragmentShader.glsl";
programId = ShaderMaker::createProgram(vertexShader, fragmentShader);
glUseProgram(programId);
}
/*
Init VAOs and VBOs
*/
static void init()
{
// VAO & VBO for control polygon
glGenVertexArrays(1, &VAO_ControlPoints);
glBindVertexArray(VAO_ControlPoints);
glGenBuffers(1, &VBO_ControlPoints);
glBindBuffer(GL_ARRAY_BUFFER, VBO_ControlPoints);
// VAO & VBO for curve
glGenVertexArrays(1, &VAO_CurvePoints);
glBindVertexArray(VAO_CurvePoints);
glGenBuffers(1, &VBO_CurvePoints);
glBindBuffer(GL_ARRAY_BUFFER, VBO_CurvePoints);
// Background color
glClearColor(1.0, 1.0, 1.0, 1.0);
glViewport(0, 0, 500, 500);
}
static void printControls()
{
std::cout << "CONTROLS ==============================" << std::endl;
std::cout << " Left mouse click - place a control point" << std::endl;
std::cout << " Drag/Drop mouse - move a control point (while hovering one)" << std::endl;
std::cout << " F - remove the first control point" << std::endl;
std::cout << " L - remove the last control point" << std::endl;
std::cout << " I - print control points" << std::endl;
std::cout << " O - print curve points" << std::endl;
std::cout << " S - enable/disable control and/or curve segments" << std::endl;
std::cout << " C/R - clear (reset) control and curve points" << std::endl;
std::cout << " ESC - quit" << std::endl;
std::cout << std::endl;
std::cout << "CURVES:" << std::endl;
std::cout << " F1 (default) - De Casteljau Uniform method" << std::endl;
std::cout << " F2 - Catmull-Rom Spline method" << std::endl;
std::cout << " F3 - De Casteljau Adaptive Subdivision method" << std::endl;
std::cout << " F4 - Cubic segments subdivision method" << std::endl;
std::cout << " ESC - quit" << std::endl;
std::cout << "=======================================" << std::endl << std::endl;
}
// LOGIC ==================================================
/*
Clear control points.
*/
static void clearControlPoints()
{
// Clear control points
for (int i = 0; i < numCtrlPts; i++)
{
ctrlPointArray[i] = { 0.0f, 0.0f };
}
numCtrlPts = 0;
iHoverCtrlPt = -1;
glutSetCursor(CURSOR_NORMAL);
}
/*
Clear curve points.
*/
static void clearCurvePoints()
{
// Clear curve points
for (int i = 0; i < numCurvePts; i++)
{
curvePointArray[i] = { 0.0f, 0.0f };
}
numCurvePts = 0;
}
/*
Clear all the control points and curve points.
*/
static void clearAllPoints()
{
clearControlPoints();
clearCurvePoints();
}
/*
Remove the first Control Point, moving the rest
of the array by one position.
*/
static void removeFirstPoint()
{
if (numCtrlPts > 0)
{
if (iHoverCtrlPt == 0)
{
iHoverCtrlPt = -1;
glutSetCursor(CURSOR_NORMAL);
}
else if(iHoverCtrlPt > 0)
iHoverCtrlPt--;
// Remove the first point, slide the rest down
numCtrlPts--;
for (int i = 0; i < numCtrlPts; i++)
{
ctrlPointArray[i] = ctrlPointArray[i + 1];
}
}
}
/*
Remove the last Control Point
*/
static void removeLastPoint()
{
if (numCtrlPts > 0)
{
if (iHoverCtrlPt == numCtrlPts - 1)
{
iHoverCtrlPt = -1;
glutSetCursor(CURSOR_NORMAL);
}
numCtrlPts--;
}
}
/*
Add a new control point (to the end of the array).
If there are too many (if MAX is reached) we remove the
first of the array.
*/
static void addNewPoint(Point2D point)
{
if (numCtrlPts >= MAX_NUM_PTS)
{
removeFirstPoint();
}
ctrlPointArray[numCtrlPts] = point;
numCtrlPts++;
}
static void addNewPoint(float x, float y)
{
addNewPoint({x, y});
}
static void printControlPoints()
{
std::cout << std::endl << "CONTROL POINTS: " << numCtrlPts << std::endl;
for (int i = 0; i < numCtrlPts; i++)
{
std::cout << "P" << (i+1) << "(" << ctrlPointArray[i].x << "," << ctrlPointArray[i].y << ")" << std::endl;
}
std::cout << std::endl;
}
static void printCurvePoints()
{
std::cout << "CURVE POINTS: " << numCurvePts << std::endl;
for (int i = 0; i < numCurvePts; i++)
{
std::cout << "P" << (i + 1) << "(" << curvePointArray[i].x << "," << ctrlPointArray[i].y << ")" << std::endl;
}
std::cout << std::endl;
}
// INPUT ==================================================
static void resizeWindow(int w, int h)
{
height = (h > 1) ? h : 2;
width = (w > 1) ? w : 2;
gluOrtho2D(-1.0f, 1.0f, -1.0f, 1.0f);
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
}
/*
Keyboard Input: normal keys
*/
static void inputKeyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'f':
removeFirstPoint();
break;
case 'l':
removeLastPoint();
break;
case 'i':
printControlPoints();
break;
case 'o':
printCurvePoints();
break;
case 's':
drawMode++;
if (drawMode > DRAW_ONLY_CURVE)
drawMode = DRAW_EVERYTHING;
break;
case 'c':
case 'r':
clearAllPoints();
break;
case 27: // Escape key
exit(0);
break;
}
}
/*
Keyboard Input: special keys (down)
*/
static void specialKeyDown(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_UP:
incrementCtrlPtSize = 1;
break;
case GLUT_KEY_DOWN:
incrementCtrlPtSize = -1;
break;
// Algorithm Selection
case GLUT_KEY_F1:
getCurve = getCurve_deCasteljau;
std::cout << "SELECTED ALGORITHM: de Casteljau with uniform method" << std::endl;
break;
case GLUT_KEY_F2:
getCurve = getCurve_CatmullRomSpline;
std::cout << "SELECTED ALGORITHM: Catmull-Rom Spline" << std::endl;
break;
case GLUT_KEY_F3:
getCurve = getCurve_adaptiveSubdivision;
std::cout << "SELECTED ALGORITHM: de Casteljau with adaptive subdivision method" << std::endl;
break;
case GLUT_KEY_F4:
//getCurve = getCurve_cubicSegments;
std::cout << "SELECTED ALGORITHM: cubic segments" << std::endl;
std::cout << "\tNot yet implemented." << std::endl; // TO-DO
break;
case GLUT_KEY_F5:
getCurve = NULL;
clearCurvePoints();
std::cout << "SELECTED ALGORITHM: none" << std::endl;
break;
}
}
/*
Keyboard Input: special keys (up)
*/
static void specialKeyUp(int key, int x, int y)
{
if(key == GLUT_KEY_UP || key == GLUT_KEY_DOWN)
incrementCtrlPtSize = 0;
}
/*
Mouse Input: click
*/
static void inputMouseClick(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON)
{
if (state == GLUT_DOWN)
{
// If there is already a Control Point in (x,y) => drag
if (iHoverCtrlPt != -1)
{
dragging = 1;
glutSetCursor(CURSOR_DRAG);
}
// Otherwise => add a new Control Point
else
{
// (x,y) viewport(0,width)x(0,height) --> (xPos,yPos) window(-1,1)x(-1,1)
Point2D point = {
-1.0f + ((float)x) * 2 / ((float)(width)),
-1.0f + ((float)(height - y)) * 2 / ((float)(height)),
};
addNewPoint(point);
glutPostRedisplay();
}
}
if (state == GLUT_UP)
{
dragging = 0;
glutSetCursor(CURSOR_NORMAL);
}
}
}
/*
Mouse Input: move
*/
static void inputMousePassiveMove(int x, int y)
{
float xMousePos = -1.0f + ((float)x) * 2 / ((float)(width));
float yMousePos = -1.0f + ((float)(height - y)) * 2 / ((float)(height));
float sizeOffsetX = ctrlPointSize / (width);
float sizeOffsetY = ctrlPointSize / (height);
/*
Check if the mouse is hovering a Control Point.
If it is: store in iHoverCtrlPt the index of the Control Point
that (xMousePos,yMousePos) are inside.
Otherwise: set iHoverCtrlPt to -1 (it's not hovering
a control point).
*/
for (int i = 0; i < numCtrlPts; i++)
{
float minX, maxX, minY, maxY;
minX = ctrlPointArray[i].x - sizeOffsetX;
maxX = ctrlPointArray[i].x + sizeOffsetX;
minY = ctrlPointArray[i].y - sizeOffsetY;
maxY = ctrlPointArray[i].y + sizeOffsetY;
// Mouse coords inside the point
if (minX <= xMousePos && xMousePos <= maxX &&
minY <= yMousePos && yMousePos <= maxY)
{
iHoverCtrlPt = i;
glutSetCursor(CURSOR_HOVER);
break;
}
else
{
iHoverCtrlPt = -1;
glutSetCursor(CURSOR_NORMAL);
}
}
}
// ES 4: Drag & drop control points
/*
Mouse Input: drag (move while pressing)
*/
static void inputMouseDrag(int x, int y)
{
if (dragging)
{
float xPos = -1.0f + ((float)x) * 2 / ((float)(width));
float yPos = -1.0f + ((float)(height - y)) * 2 / ((float)(height));
// MIN/MAX to limit the dragging to end up inside the window
ctrlPointArray[iHoverCtrlPt].x = MIN(1.0, MAX(-1.0, xPos));
ctrlPointArray[iHoverCtrlPt].y = MIN(1.0, MAX(-1.0, yPos));
}
}
// UPDATE =================================================
static void update(int value)
{
ctrlPointSize = MAX(MIN_CTRL_PTS_SIZE, MIN(MAX_CTRL_PTS_SIZE, ctrlPointSize + 0.1 * incrementCtrlPtSize));
// Update curve: draw the curve only if there are at least 2 control points
if (numCtrlPts > 1)
{
if(getCurve)
getCurve(ctrlPointArray, numCtrlPts, curvePointArray, &numCurvePts);
}
glutPostRedisplay();
glutTimerFunc(UPDATE_DELAY, update, 0);
}
// DRAW ===================================================
static void drawScene()
{
glClear(GL_COLOR_BUFFER_BIT);
// Draw control polygon
glBindVertexArray(VAO_ControlPoints);
glBindBuffer(GL_ARRAY_BUFFER, VBO_ControlPoints);
glBufferData(GL_ARRAY_BUFFER, sizeof(ctrlPointArray), &ctrlPointArray[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// Draw the control points CP
if (drawMode == DRAW_EVERYTHING
|| drawMode == DRAW_ONLY_CONTROL_POINTS
|| drawMode == DRAW_CONTROL_POINTS_AND_SEGMENTS
|| drawMode == DRAW_CONTROL_POINTS_AND_CURVE)
{
glPointSize(ctrlPointSize);
glDrawArrays(GL_POINTS, 0, numCtrlPts);
}
// Draw the segments (lines between CP)
if (drawMode == DRAW_EVERYTHING
|| drawMode == DRAW_CONTROL_POINTS_AND_SEGMENTS)
{
glLineWidth(2.0);
glDrawArrays(GL_LINE_STRIP, 0, numCtrlPts);
}
// Draw curve
if ((drawMode == DRAW_EVERYTHING
|| drawMode == DRAW_CONTROL_POINTS_AND_CURVE)
|| drawMode == DRAW_ONLY_CURVE
&& numCtrlPts > 1)
{
glBindVertexArray(VAO_CurvePoints);
glBindBuffer(GL_ARRAY_BUFFER, VBO_CurvePoints);
glBufferData(GL_ARRAY_BUFFER, sizeof(curvePointArray), &curvePointArray[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// Draw the segments that form the curve
glLineWidth(0.5);
glDrawArrays(GL_LINE_STRIP, 0, numCurvePts);
}
glBindVertexArray(0);
glutSwapBuffers();
}
// MAIN ===================================================
int main(int argc, char** argv)
{
printControls();
glutInit(&argc, argv);
glutInitContextVersion(4, 0);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(width, height);
glutInitWindowPosition(100, 100);
glutCreateWindow("Draw curves 2D");
// Input callbacks
glutReshapeFunc(resizeWindow);
glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF);
glutKeyboardFunc(inputKeyboard);
glutSpecialFunc(specialKeyDown);
glutSpecialUpFunc(specialKeyUp);
glutMouseFunc(inputMouseClick);
glutPassiveMotionFunc(inputMousePassiveMove);
glutMotionFunc(inputMouseDrag);
// Update callback
glutTimerFunc(UPDATE_DELAY, update, 0);
// Draw callback
glutDisplayFunc(drawScene);
glewExperimental = GL_TRUE;
glewInit();
initShader();
init();
// default: deCasteljau
getCurve = getCurve_deCasteljau;
std::cout << std::endl << "SELECTED ALGORITHM: de Casteljau with uniform method" << std::endl;
glutMainLoop();
return 0;
}