-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
1135 lines (936 loc) · 34 KB
/
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//=============================================================================================
// Szamitogepes grafika hazi feladat keret. Ervenyes 2017-tol.
// A //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// sorokon beluli reszben celszeru garazdalkodni, mert a tobbit ugyis toroljuk.
// A beadott program csak ebben a fajlban lehet, a fajl 1 byte-os ASCII karaktereket tartalmazhat.
// Tilos:
// - mast "beincludolni", illetve mas konyvtarat hasznalni
// - faljmuveleteket vegezni a printf-et kivéve
// - new operatort hivni a lefoglalt adat korrekt felszabaditasa nelkul
// - felesleges programsorokat a beadott programban hagyni
// - felesleges kommenteket a beadott programba irni a forrasmegjelolest kommentjeit kiveve
// ---------------------------------------------------------------------------------------------
// A feladatot ANSI C++ nyelvu forditoprogrammal ellenorizzuk, a Visual Studio-hoz kepesti elteresekrol
// es a leggyakoribb hibakrol (pl. ideiglenes objektumot nem lehet referencia tipusnak ertekul adni)
// a hazibeado portal ad egy osszefoglalot.
// ---------------------------------------------------------------------------------------------
// A feladatmegoldasokban csak olyan OpenGL/GLUT fuggvenyek hasznalhatok, amelyek az oran a feladatkiadasig elhangzottak
//
// NYILATKOZAT
// ---------------------------------------------------------------------------------------------
// Nev :
// Neptun :
// ---------------------------------------------------------------------------------------------
// ezennel kijelentem, hogy a feladatot magam keszitettem, es ha barmilyen segitseget igenybe vettem vagy
// mas szellemi termeket felhasznaltam, akkor a forrast es az atvett reszt kommentekben egyertelmuen jeloltem.
// A forrasmegjeloles kotelme vonatkozik az eloadas foliakat es a targy oktatoi, illetve a
// grafhazi doktor tanacsait kiveve barmilyen csatornan (szoban, irasban, Interneten, stb.) erkezo minden egyeb
// informaciora (keplet, program, algoritmus, stb.). Kijelentem, hogy a forrasmegjelolessel atvett reszeket is ertem,
// azok helyessegere matematikai bizonyitast tudok adni. Tisztaban vagyok azzal, hogy az atvett reszek nem szamitanak
// a sajat kontribucioba, igy a feladat elfogadasarol a tobbi resz mennyisege es minosege alapjan szuletik dontes.
// Tudomasul veszem, hogy a forrasmegjeloles kotelmenek megsertese eseten a hazifeladatra adhato pontokat
// negativ elojellel szamoljak el es ezzel parhuzamosan eljaras is indul velem szemben.
//=============================================================================================
#define _USE_MATH_DEFINES
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#if defined(__APPLE__)
#include <GLUT/GLUT.h>
#include <OpenGL/gl3.h>
#include <OpenGL/glu.h>
#else
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
#include <windows.h>
#endif
#include <GL/glew.h> // must be downloaded
#include <GL/freeglut.h> // must be downloaded unless you have an Apple
#endif
const unsigned int windowWidth = 600, windowHeight = 600;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// You are supposed to modify the code from here...
const float EPSILON = 0.000001;
// OpenGL major and minor versions
int majorVersion = 3, minorVersion = 3;
const float PI = 3.14156265358979f;
void getErrorInfo(unsigned int handle) {
int logLen;
glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &logLen);
if (logLen > 0) {
char * log = new char[logLen];
int written;
glGetShaderInfoLog(handle, logLen, &written, log);
printf("Shader log:\n%s", log);
delete[] log;
}
}
// check if shader could be compiled
void checkShader(unsigned int shader, const char * message) {
int OK;
glGetShaderiv(shader, GL_COMPILE_STATUS, &OK);
if (!OK) {
printf("%s!\n", message);
getErrorInfo(shader);
}
}
// check if shader could be linked
void checkLinking(unsigned int program) {
int OK;
glGetProgramiv(program, GL_LINK_STATUS, &OK);
if (!OK) {
printf("Failed to link shader program!\n");
getErrorInfo(program);
}
}
// vertex shader in GLSL
const char * vertexSource = R"(
#version 330
precision highp float;
uniform mat4 MVP; // Model-View-Projection matrix in row-major format
layout(location = 0) in vec2 vertexPosition; // Attrib Array 0
layout(location = 1) in vec3 vertexColor; // Attrib Array 1
out vec3 color; // output attribute
void main() {
color = vertexColor; // copy color from input to output
gl_Position = vec4(vertexPosition.x, vertexPosition.y, 0, 1) * MVP; // transform to clipping space
}
)";
// fragment shader in GLSL
const char * fragmentSource = R"(
#version 330
precision highp float;
in vec3 color; // variable input: interpolated color of vertex shader
out vec4 fragmentColor; // output that goes to the raster memory as told by glBindFragDataLocation
void main() {
fragmentColor = vec4(color, 1); // extend RGB to RGBA
}
)";
// row-major matrix 4x4
struct mat4 {
float m[4][4];
public:
constexpr mat4()
:mat4(1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f)
{ }
constexpr mat4(float m00, float m01, float m02, float m03,
float m10, float m11, float m12, float m13,
float m20, float m21, float m22, float m23,
float m30, float m31, float m32, float m33)
:m { { m00, m01, m02, m03 },
{ m10, m11, m12, m13 },
{ m20, m21, m22, m23 },
{ m30, m31, m32, m33 } }
{ }
mat4(const mat4 &m) :mat4(m[0][0], m[0][1], m[0][2], m[0][3],
m[1][0], m[1][1], m[1][2], m[1][3],
m[2][0], m[2][1], m[2][2], m[2][3],
m[3][0], m[3][1], m[3][2], m[3][3])
{ }
mat4 operator*(const mat4& right) const {
mat4 result;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
result.m[i][j] = 0;
for (int k = 0; k < 4; k++) result.m[i][j] += m[i][k] * right.m[k][j];
}
}
return result;
}
operator float*() { return &m[0][0]; }
operator const float*() const { return &m[0][0]; }
float *operator[](unsigned int i) { return &m[i][0]; }
const float *operator[](unsigned int i) const { return &m[i][0]; }
};
// 3D point in homogeneous coordinates
struct vec4 {
float v[4];
explicit vec4(float x = 0, float y = 0, float z = 0, float w = 1) {
v[0] = x; v[1] = y; v[2] = z; v[3] = w;
}
vec4(const vec4 &vec) {
for(int i = 0; i < 4; ++i) {
v[i] = vec[i];
}
}
vec4 operator*(const mat4& mat) const {
vec4 result;
for (int j = 0; j < 4; j++) {
result.v[j] = 0;
for (int i = 0; i < 4; i++) result.v[j] += v[i] * mat.m[i][j];
}
return result;
}
float operator[](size_t index) const {
return v[index];
}
float &operator[](size_t index) {
return v[index];
}
vec4 &operator+=(const vec4 &w) {
for(int i = 0; i < 4; ++i) {
v[i] += w[i];
}
return *this;
}
vec4 operator+(const vec4 &w) const {
return (vec4(*this) += w);
}
vec4 &operator*=(float f) {
for(int i = 0; i < 4; ++i) v[i] *= f;
return *this;
}
vec4 operator*(float f) const {
return (vec4(*this) *= f);
}
friend vec4 operator*(float f, const vec4 &v) {
return v * f;
}
vec4 &operator-=(const vec4 &w) {
return *this += -1.0f * w;
}
vec4 operator-(const vec4 &w) const {
return vec4(*this) -= w;
}
vec4 operator-() const {
return -1.0f * *this;
}
vec4 &operator/=(float f) {
if(f == 1) return *this;
return *this *= (1.0f / f);
}
vec4 operator/(float f) const {
if(f == 1) return *this;
return vec4(*this) /= f;
}
vec4 &normalize() {
if(fabs(v[3] - 1.0f) > EPSILON) {
for(int i = 0; i < 3; ++i) {
v[i] /= v[3];
}
v[3] = 1.0f;
}
float len = sqrt(pow(v[0], 2) + pow(v[1], 2) + pow(v[2], 2));
for(int i = 0; i < 3; ++i) {
v[i] /= len;
}
return *this;
}
vec4 getNormalized() const {
return vec4(*this).normalize();
}
};
float dot(const vec4 &v, const vec4 &w) {
float sum = 0.0f;
if(fabs(v[3] - 1.0f) > EPSILON || fabs(w[3] - 1.0f) > EPSILON) {
return -1.0f;
}
for(int i = 0; i < 3; ++i) {
sum += v[i] * w[i];
}
return sum;
}
// 2D camera
struct Camera {
float wCx, wCy; // center in world coordinates
float wWx, wWy; // width and height in world coordinates
public:
Camera() {
Animate(0);
}
Camera(const Camera &) = delete;
mat4 V() const { // view matrix: translates the center to the origin
return mat4( 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
-wCx, -wCy, 0, 1);
}
mat4 P() const { // projection matrix: scales it to be a square of edge length 2
return mat4(2/wWx, 0, 0, 0,
0, 2/wWy, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
}
mat4 Vinv() const { // inverse view matrix
return mat4( 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
wCx, wCy, 0, 1);
}
mat4 Pinv() const { // inverse projection matrix
return mat4(wWx/2, 0, 0, 0,
0, wWy/2, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
}
void Animate(float /*t*/) {
wCx = 0; // 10 * cosf(t);
wCy = 0;
wWx = 2;
wWy = 2;
}
};
// 2D camera
Camera camera;
// handle of the shader program
unsigned int shaderProgram;
class Triangle {
protected:
unsigned int vao; // vertex array object id
float sx, sy; // scaling
float wTx, wTy; // translation
float rotation; // rotation
public:
Triangle() {
Animate(0);
}
Triangle(const Triangle &) = delete;
void Create() {
glGenVertexArrays(1, &vao); // create 1 vertex array object
glBindVertexArray(vao); // make it active
unsigned int vbo[2]; // vertex buffer objects
glGenBuffers(2, &vbo[0]); // Generate 2 vertex buffer objects
// vertex coordinates: vbo[0] -> Attrib Array 0 -> vertexPosition of the vertex shader
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); // make it active, it is an array
static float vertexCoords[] = { -0.08f, -0.08f, -0.06f, 0.1f, 0.08f, -0.02f }; // vertex data on the CPU
glBufferData(GL_ARRAY_BUFFER, // copy to the GPU
sizeof(vertexCoords), // number of the vbo in bytes
vertexCoords, // address of the data array on the CPU
GL_STATIC_DRAW); // copy to that part of the memory which is not modified
// Map Attribute Array 0 to the current bound vertex buffer (vbo[0])
glEnableVertexAttribArray(0);
// Data organization of Attribute Array 0
glVertexAttribPointer(0, // Attribute Array 0
2, GL_FLOAT, // components/attribute, component type
GL_FALSE, // not in fixed point format, do not normalized
0, NULL); // stride and offset: it is tightly packed
// vertex colors: vbo[1] -> Attrib Array 1 -> vertexColor of the vertex shader
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); // make it active, it is an array
static float vertexColors[] = { 1, 0, 0, 0, 1, 0, 0, 0, 1 }; // vertex data on the CPU
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexColors), vertexColors, GL_STATIC_DRAW); // copy to the GPU
// Map Attribute Array 1 to the current bound vertex buffer (vbo[1])
glEnableVertexAttribArray(1); // Vertex position
// Data organization of Attribute Array 1
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); // Attribute Array 1, components/attribute, component type, normalize?, tightly packed
}
void Animate(float /*t*/) {
sx = 1; // sinf(t);
sy = 1; // cosf(t);
wTx = 0; // 4 * cosf(t / 2);
wTy = 0; // 4 * sinf(t / 2);
}
void Draw() const {
mat4 Mscale(sx, 0, 0, 0,
0, sy, 0, 0,
0, 0, 0, 0,
0, 0, 0, 1); // model matrix
mat4 Mrotate( cos(rotation), -sin(rotation), 0, 0,
sin(rotation), cos(rotation), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
mat4 Mtranslate( 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 0, 0,
wTx, wTy, 0, 1); // model matrix
mat4 MVPTransform = Mscale * Mrotate * Mtranslate * camera.V() * camera.P();
// set GPU uniform matrix variable MVP with the content of CPU variable MVPTransform
int location = glGetUniformLocation(shaderProgram, "MVP");
if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, MVPTransform); // set uniform variable MVP to the MVPTransform
else printf("uniform MVP cannot be set\n");
glBindVertexArray(vao); // make the vao and its vbos active playing the role of the data source
glDrawArrays(GL_TRIANGLES, 0, 3); // draw a single triangle with vertices defined in vao
}
};
class LineStrip {
protected:
static const unsigned int ELEMENTS_PER_VERTEX = 5;
GLuint vao, vbo; // vertex array object, vertex buffer object
std::vector<float> vertexData; // interleaved data of coordinates and colors
void CopyVertexDataToGPU() const {
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertexData.size() * sizeof(float), vertexData.data(), GL_DYNAMIC_DRAW);
}
public:
LineStrip() = default;
LineStrip(const LineStrip &) = delete;
void Create() {
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo); // Generate 1 vertex buffer object
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// Enable the vertex attribute arrays
glEnableVertexAttribArray(0); // attribute array 0
glEnableVertexAttribArray(1); // attribute array 1
// Map attribute array 0 to the vertex data of the interleaved vbo
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, ELEMENTS_PER_VERTEX * sizeof(float), reinterpret_cast<void*>(0)); // attribute array, components/attribute, component type, normalize?, stride, offset
// Map attribute array 1 to the color data of the interleaved vbo
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, ELEMENTS_PER_VERTEX * sizeof(float), reinterpret_cast<void*>(2 * sizeof(float)));
}
void AddPoint(float cX, float cY, float r = 1.0f, float g = 1.0f, float b = 1.0f) {
vec4 wVertex = vec4(cX, cY, 0, 1);
// fill interleaved data
vertexData.push_back(wVertex.v[0]);
vertexData.push_back(wVertex.v[1]);
vertexData.push_back(r); // red
vertexData.push_back(g); // green
vertexData.push_back(b); // blue
}
void Draw() const {
if (vertexData.size() > 0) {
mat4 VPTransform = camera.V() * camera.P();
int location = glGetUniformLocation(shaderProgram, "MVP");
if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, VPTransform);
else printf("uniform MVP cannot be set\n");
glBindVertexArray(vao);
glDrawArrays(GL_LINE_STRIP, 0, vertexData.size() / ELEMENTS_PER_VERTEX);
}
}
virtual ~LineStrip() {}
};
class BezierField {
constexpr static const float CONTROL_POINTS[16] = {
0.2f, 0.0f, 0.0f, 0.0f,
0.0f, 0.8f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.3f,
0.0f, 0.0f, 0.3f, 0.0f,
};
static const unsigned int CONTROL_POINTS_WIDTH = 3;
static const unsigned int GRID_RESOLUTION = 20;
static const unsigned int ELEMENTS_PER_VERTEX = 5;
GLuint vao, vbo;
std::vector<vec4> cps;
std::vector<float> vertexData;
float minHeight, maxHeight;
void CopyVertexDataToGPU() const {
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertexData.size() * sizeof(float), vertexData.data(), GL_STATIC_DRAW);
}
float getBBinom(int i) const {
int n = CONTROL_POINTS_WIDTH;
float choose = 1.0f;
for(int j = 1; j <= i; ++j) {
choose *= static_cast<float>(n-j+1) / j;
}
return choose;
}
float B(int i, float t) const {
int n = CONTROL_POINTS_WIDTH;
return getBBinom(i) * pow(t, i) * pow(1 - t, n - i);
}
float Bderiv(int i, float t) const {
int n = CONTROL_POINTS_WIDTH;
float BBinom = getBBinom(i);
return BBinom * (pow(t, i - 1) * pow(1 - t, n - i) * i - pow(t, i) * (n - i) * pow(1 - t, n - i - 1));
}
vec4 getGradient(float x, float y) const {
vec4 result;
for(unsigned int v = 0; v <= CONTROL_POINTS_WIDTH; ++v) {
for(unsigned int u = 0; u <= CONTROL_POINTS_WIDTH; ++u) {
result[0] += Bderiv(u, x) * B(v, y) * CONTROL_POINTS[v * (CONTROL_POINTS_WIDTH + 1) + u];
result[1] += B(u, x) * Bderiv(v, y) * CONTROL_POINTS[v * (CONTROL_POINTS_WIDTH + 1) + u];
}
}
return result;
}
vec4 getColorByLevel(float height) const {
float r = 1.0f - pow(2.0f * height - 1.0f, 2.0f) * 0.8f;
float g = (1.0f - height * 0.92f) * 0.8f;
return vec4(r, g, 0.0f);
}
void tesselate() {
vertexData.clear();
float tempVertexData[(GRID_RESOLUTION + 1) * (GRID_RESOLUTION + 1) * ELEMENTS_PER_VERTEX];
std::vector<unsigned int> tempIndexData;
float step = 1.0f / GRID_RESOLUTION;
minHeight = maxHeight = getHeight(0.0f, 0.0f);
for(unsigned int i = 0; i <= GRID_RESOLUTION; ++i) {
for(unsigned int j = 0; j <= GRID_RESOLUTION; ++j) {
float v = i * step;
float u = j * step;
float height = getHeight(u, v);
float x = u;
float y = v;
tempVertexData[(i * (GRID_RESOLUTION + 1) + j) * ELEMENTS_PER_VERTEX] = x;
tempVertexData[(i * (GRID_RESOLUTION + 1) + j) * ELEMENTS_PER_VERTEX + 1] = y;
tempVertexData[(i * (GRID_RESOLUTION + 1) + j) * ELEMENTS_PER_VERTEX + 2] = height; // temporary
if(height < minHeight) minHeight = height;
if(height > maxHeight) maxHeight = height;
}
}
for(unsigned int i = 0; i <= GRID_RESOLUTION; ++i) {
for(unsigned int j = 0; j <= GRID_RESOLUTION; ++j) {
float height = tempVertexData[(i * (GRID_RESOLUTION + 1) + j) * ELEMENTS_PER_VERTEX + 2];
vec4 color = getColorByLevel((height - minHeight) / (maxHeight - minHeight));
tempVertexData[(i * (GRID_RESOLUTION + 1) + j) * ELEMENTS_PER_VERTEX + 2] = color[0];
tempVertexData[(i * (GRID_RESOLUTION + 1) + j) * ELEMENTS_PER_VERTEX + 3] = color[1];
tempVertexData[(i * (GRID_RESOLUTION + 1) + j) * ELEMENTS_PER_VERTEX + 4] = color[2];
}
}
for(unsigned int v = 0; v < GRID_RESOLUTION; ++v) {
for(unsigned int u = 0; u < GRID_RESOLUTION; ++u) {
tempIndexData.push_back(u + v * (GRID_RESOLUTION + 1));
tempIndexData.push_back(u + (v + 1) * (GRID_RESOLUTION + 1));
tempIndexData.push_back(u + 1 + v * (GRID_RESOLUTION + 1));
tempIndexData.push_back(u + (v + 1) * (GRID_RESOLUTION + 1));
tempIndexData.push_back(u + 1 + v * (GRID_RESOLUTION + 1));
tempIndexData.push_back(u + 1 + (v + 1) * (GRID_RESOLUTION + 1));
}
}
for(unsigned int index: tempIndexData) {
for(unsigned int i = index * ELEMENTS_PER_VERTEX; i < (index + 1) * ELEMENTS_PER_VERTEX; ++i) {
vertexData.push_back(tempVertexData[i]);
}
}
fflush(stdout);
CopyVertexDataToGPU();
}
public:
float getHeight(float x, float y) const {
float height = 0.0f;
for(unsigned int v = 0; v <= CONTROL_POINTS_WIDTH; ++v) {
for(unsigned int u = 0; u <= CONTROL_POINTS_WIDTH; ++u) {
height += B(u, x) * B(v, y) * CONTROL_POINTS[v * (CONTROL_POINTS_WIDTH + 1) + u];
}
}
return height;
}
void Create() {
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo); // Generate 1 vertex buffer object
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// Enable the vertex attribute arrays
glEnableVertexAttribArray(0); // attribute array 0
glEnableVertexAttribArray(1); // attribute array 1
// Map attribute array 0 to the vertex data of the interleaved vbo
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, ELEMENTS_PER_VERTEX * sizeof(float), reinterpret_cast<void*>(0)); // attribute array, components/attribute, component type, normalize?, stride, offset
// Map attribute array 1 to the color data of the interleaved vbo
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, ELEMENTS_PER_VERTEX * sizeof(float), reinterpret_cast<void*>(2 * sizeof(float)));
tesselate();
}
void Draw() const {
mat4 scale = {
2.0f, 0.0f, 0.0f, 0.0f,
0.0f, 2.0f, 0.0f, 0.0f,
0.0f, 0.0f, 2.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
mat4 translate = {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f, 1.0f
};
mat4 VPTransform = scale * translate * camera.V() * camera.P();
int location = glGetUniformLocation(shaderProgram, "MVP");
if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, VPTransform);
else printf("uniform MVP cannot be set\n");
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, vertexData.size() / ELEMENTS_PER_VERTEX);
}
float getDirectionalDerivative(vec4 position, vec4 direction) const {
position /= 2.0f;
position += vec4(0.5f, 0.5f);
vec4 grad = getGradient(position[0], position[1]);
float dotproduct = dot(grad, direction);
return dotproduct;
}
};
constexpr const float BezierField::CONTROL_POINTS[16];
class LagrangeCurve : protected LineStrip {
static const int RESOLUTION = 100;
std::vector<vec4> cps;
std::vector<float> ts;
float lastAbsoluteTime = -1.0f;
float lastRelativeTime = -1.0f; // ~= lastAbsoluteTime - firstAbsoluteTime
float firstAbsoluteTime = -1.0f;
const BezierField *field;
float L(unsigned int i, float t) const {
float Li = 1.0f;
for(unsigned int j = 0; j < cps.size(); ++j) {
if(j == i) continue;
Li *= (t - ts[j]) / (ts[i] - ts[j]);
}
return Li;
}
float Lderiv(unsigned int i, float t) const {
float sum = 0.0f;
for(unsigned int j = 0; j < cps.size(); ++j) {
if(j == i) continue;
sum += 1.0f / (t - ts[j]);
}
return sum * L(i, t);
}
void tesselate() {
vertexData.clear();
float step = lastRelativeTime / static_cast<float>(RESOLUTION);
for(unsigned i = 0; i <= RESOLUTION; ++i) {
float t = step * i;
vec4 point = r(t);
AddPoint(point[0], point[1]);
}
CopyVertexDataToGPU();
}
public:
LagrangeCurve(const BezierField *field) :field(field) { }
vec4 r(float t) const {
// EPSILON is needed to make a difference between the first and the last point even after fmod
// EPSILON is a very small positive number
t = fmod(t, lastRelativeTime + EPSILON);
vec4 rr(0,0,0);
for(unsigned int i = 0; i < cps.size(); ++i) {
rr += cps[i] * L(i, t);
}
return rr;
}
vec4 direction(float t) const {
if(lastRelativeTime == -1) return vec4();
t = fmod(t, lastRelativeTime + EPSILON);
vec4 rr(0,0,0);
for(unsigned int i = 0; i < cps.size(); ++i) {
rr += cps[i] * Lderiv(i, t);
}
return rr;
}
void AddControlPoint(vec4 cp) {
float currentAbsoluteTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
if(fabs(lastAbsoluteTime + 1.0f) <= EPSILON) {
firstAbsoluteTime = lastAbsoluteTime = currentAbsoluteTime;
lastRelativeTime = 0.0f;
}
float timeDelta = currentAbsoluteTime - lastAbsoluteTime;
lastRelativeTime += timeDelta;
lastAbsoluteTime = currentAbsoluteTime;
ts.push_back(lastRelativeTime);
cps.push_back(cp);
if(cps.size() > 1) tesselate();
printf("LagrangeCurve length: %f km\n", getLength() / 2.0f);
fflush(stdout);
}
void Create() {
LineStrip::Create();
}
void Draw() const {
LineStrip::Draw();
}
float getLength() const {
float length = 0.0f;
for(unsigned int i = 1; i < vertexData.size() / ELEMENTS_PER_VERTEX; ++i) {
float curx = vertexData[i * ELEMENTS_PER_VERTEX];
float cury = vertexData[i * ELEMENTS_PER_VERTEX + 1];
float curz = field->getHeight(0.5f * curx + 0.5f, 0.5f * cury + 0.5f);
float lastx = vertexData[(i - 1) * ELEMENTS_PER_VERTEX];
float lasty = vertexData[(i - 1) * ELEMENTS_PER_VERTEX + 1];
float lastz = field->getHeight(0.5f * lastx + 0.5f, 0.5f * lasty + 0.5f);
length += sqrt(pow(curx - lastx, 2.0f) + pow(cury - lasty, 2.0f) + pow(curz - lastz, 2.0f));
}
return length;
}
};
class Bicycle {
static const unsigned int ELEMENTS_PER_VERTEX = 5;
const LagrangeCurve *curve;
const BezierField *field;
float vertexData[ELEMENTS_PER_VERTEX * 4];
GLuint vao, vbo;
float translation[2];
float rotation;
float sy;
float verticalAngle;
bool started;
float startTime;
public:
Bicycle(const LagrangeCurve *curve, const BezierField *field)
:curve(curve), field(field), translation { 0.0f, 0.0f }, started(false)
{ }
void Create() {
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo); // Generate 1 vertex buffer object
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// Enable the vertex attribute arrays
glEnableVertexAttribArray(0); // attribute array 0
glEnableVertexAttribArray(1); // attribute array 1
// Map attribute array 0 to the vertex data of the interleaved vbo
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, ELEMENTS_PER_VERTEX * sizeof(float), reinterpret_cast<void*>(0)); // attribute array, components/attribute, component type, normalize?, stride, offset
// Map attribute array 1 to the color data of the interleaved vbo
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, ELEMENTS_PER_VERTEX * sizeof(float), reinterpret_cast<void*>(2 * sizeof(float)));
vertexData[0] = -1.0f; // x
vertexData[1] = -1.0f; // y
vertexData[2] = 1.0f; // r
vertexData[3] = 0.0f; // g
vertexData[4] = 1.0f; // b
vertexData[5] = 0.0f;
vertexData[6] = 1.0f;
vertexData[7] = 0.0f;
vertexData[8] = 1.0f;
vertexData[9] = 1.0f;
vertexData[10] = 0.0f;
vertexData[11] = 0.0f;
vertexData[12] = 0.0f;
vertexData[13] = 0.0f;
vertexData[14] = 1.0f;
vertexData[15] = 1.0f;
vertexData[16] = -1.0f;
vertexData[17] = 1.0f;
vertexData[18] = 0.0f;
vertexData[19] = 1.0f;
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 20 * sizeof(float), vertexData, GL_STATIC_DRAW);
}
void Animate(float t) {
if(!started) return;
vec4 pos = curve->r(t - startTime);
translation[0] = pos[0]; // 4 * cosf(t / 2);
translation[1] = pos[1]; // 4 * sinf(t / 2);
vec4 direction = curve->direction(t - startTime).normalize();
this->rotation = atan2(direction[0], direction[1]);;
verticalAngle = atan(field->getDirectionalDerivative(pos, direction));
sy = sin(verticalAngle + PI / 2.0f);
}
void Draw() const {
using namespace std;
if(!started) return;
constexpr float scale = 0.1f;
mat4 Mscale( scale, 0.0f, 0.0f, 0.0f,
0.0f, sy * scale, 0.0f, 0.0f,
0.0f, 0.0f, scale, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
mat4 Mtranslate( 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
translation[0], translation[1], 0.0f, 1.0f);
mat4 Mrotate( cosf(rotation), -sinf(rotation), 0.0f, 0.0f,
sinf(rotation), cosf(rotation), 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
mat4 VPTransform = Mscale * Mrotate * Mtranslate * camera.V() * camera.P();
int location = glGetUniformLocation(shaderProgram, "MVP");
if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, VPTransform);
else printf("uniform MVP cannot be set\n");
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
float getVerticalAngle() const {
return verticalAngle;
}
void start() {
started = true;
startTime = static_cast<float>(glutGet(GLUT_ELAPSED_TIME)) / 1000.0f;
}
};
class DerivativeTriangle {
static const unsigned int ELEMENTS_PER_VERTEX = 5;
GLuint vao, vbo;
float vertexData[ELEMENTS_PER_VERTEX * 3];
const Bicycle *bicycle;
float verticalAngle;
public:
explicit DerivativeTriangle(const Bicycle *bicycle)
:bicycle(bicycle)
{ }
void Create() {
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo); // Generate 1 vertex buffer object
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// Enable the vertex attribute arrays
glEnableVertexAttribArray(0); // attribute array 0
glEnableVertexAttribArray(1); // attribute array 1
// Map attribute array 0 to the vertex data of the interleaved vbo
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, ELEMENTS_PER_VERTEX * sizeof(float), reinterpret_cast<void*>(0)); // attribute array, components/attribute, component type, normalize?, stride, offset
// Map attribute array 1 to the color data of the interleaved vbo
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, ELEMENTS_PER_VERTEX * sizeof(float), reinterpret_cast<void*>(2 * sizeof(float)));
}
void Animate() {
verticalAngle = bicycle->getVerticalAngle();
}
void Draw() {
float height = fabs(tan(verticalAngle));
if(verticalAngle < 0) {
vertexData[0] = 0.0f; // x
vertexData[1] = 0.0f; // y
vertexData[2] = 0.0f; // r
vertexData[3] = 1.0f; // g
vertexData[4] = 1.0f; // b
vertexData[5] = 0.0f; // x
vertexData[6] = height; // y
vertexData[7] = 0.0f; // r
vertexData[8] = 1.0f; // g
vertexData[9] = 1.0f; // b
vertexData[10] = 1.0f; // x
vertexData[11] = 0.0f; // y
vertexData[12] = 0.0f; // r
vertexData[13] = 1.0f; // g
vertexData[14] = 1.0f; // b
} else {
vertexData[0] = 0.0f; // x
vertexData[1] = 0.0f; // y
vertexData[2] = 0.0f; // r
vertexData[3] = 1.0f; // g
vertexData[4] = 1.0f; // b
vertexData[5] = 1.0f; // x
vertexData[6] = height; // y
vertexData[7] = 0.0f; // r
vertexData[8] = 1.0f; // g
vertexData[9] = 1.0f; // b
vertexData[10] = 1.0f; // x
vertexData[11] = 0.0f; // y
vertexData[12] = 0.0f; // r
vertexData[13] = 1.0f; // g
vertexData[14] = 1.0f; // b
}
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 15 * sizeof(float), vertexData, GL_DYNAMIC_DRAW);
mat4 scale = {
0.1f, 0.0f, 0.0f, 0.0f,
0.0f, 0.1f, 0.0f, 0.0f,
0.0f, 0.0f, 0.1f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
mat4 translate = {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.7f, -0.8f, 0.0f, 1.0f
};
mat4 VPTransform = scale * translate * camera.V() * camera.P();
int location = glGetUniformLocation(shaderProgram, "MVP");
if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, VPTransform);
else printf("uniform MVP cannot be set\n");
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
};
// The virtual world: collection of two objects
BezierField field;
LagrangeCurve lagrangeCurve(&field);
Bicycle bicycle(&lagrangeCurve, &field);
DerivativeTriangle dTriangle(&bicycle);
// Initialization, create an OpenGL context
void onInitialization() {
glViewport(0, 0, windowWidth, windowHeight);
// Create objects by setting up their vertex data on the GPU
lagrangeCurve.Create();
field.Create();
bicycle.Create();
dTriangle.Create();
// Create vertex shader from string
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
if (!vertexShader) {