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
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "glfww.h"
#include "util.h"
#include <unistd.h>
typedef struct {
double x;
double y;
double z;
int vertex;
} cord;
cord poi_d(double x1,double y1,double x2, double y2,int len,GLfloat* pixels,int shortest){
double m1 = (y2-y1)/(x2-x1);
double b1 = y1 - m1 * x1;
if(x2-x1==0){
m1=0;
b1=0;
}
cord aa;
aa.x = 0;
aa.y = 0;
aa.z = -1;
for(int yyu = 0; yyu!=len-1; yyu++){
double x3 = pixels[yyu*2];
double x4 = pixels[(yyu+1)*2];
double y3 = pixels[yyu*2+1];
double y4 = pixels[(yyu+1)*2+1];
double m2 = (y4-y3)/(x4-x3);
double b2 = y3 - m2 * x3;
double nsx = (b2-b1)/(m1-m2);
double nsy = m1*nsx+b1;
if (x4-x3==0){
nsx=x3;
nsy=m1*nsx+b1;
}
//printf("[%i] %f %f %f %f\n",yyu,m2,b2,nsx,nsy);
if(!(nsx >= greater(lesser(x1, x2), lesser(x3, x4)) && nsx <= lesser(greater(x1, x2), greater(x3, x4)))
||!(nsy >= greater(lesser(y1, y2), lesser(y3, y4)) && nsy <= lesser(greater(y1, y2), greater(y3, y4)))
||(diff(nsx,x2)<FL_DIS&&diff(nsy,y2)<FL_DIS)
||(diff(nsx,x1)<FL_DIS&&diff(nsy,y1)<FL_DIS)
||(diff(nsx,x3)<FL_DIS&&diff(nsy,y3)<FL_DIS)
||(diff(nsx,x4)<FL_DIS&&diff(nsy,y4)<FL_DIS))
continue;
if(aa.z==-1||pow(x1-nsx,2)+pow(y1-nsy,2)<pow(x1-aa.x,2)+pow(y1-aa.y,2)){
aa.x = nsx;
aa.y = nsy;
}
aa.z++;
//if(!shortest)
// printf("%f %f | %i %i > %f %f | %f\n",nsx,nsy,fabs(nsx-x2)<=FL_DIS,fabs(nsy-y2)<=FL_DIS,fabs(nsx-x2),fabs(nsy-y2),FL_DIS);
if(!shortest)
break;
}
return aa;
}
typedef struct {
float r;
float g;
float b;
} color;
typedef struct {
cord at;
color color;
} point;
typedef struct {
point* c;
point* vert;
double vlen;
double len;
} point_arr;
typedef struct {
GLfloat* at;
int len;
} glfl_a;
typedef struct {
glfl_a* at;
int len;
} glfl_m;
GLuint prog;
static const char* vshader_src =
"#version 330\n"
"layout (location = 0) in vec3 pos;\n"
"layout (location = 1) in vec3 color;\n"
"out vec3 ncolor;\n"
"void main(){\n"
"ncolor = color;\n"
"gl_Position = vec4(pos,1.0);\n"
"};";
static const char* fshader_src =
"#version 330\n"
"in vec3 ncolor;\n"
"out vec3 color;\n"
"void main(){\n"
"gl_FragColor = vec4(ncolor,1.0);\n"
"};";
point_arr* basier2d(double*xx,double*yy,int n,float rr, float gg, float bb){
n-=1;
int lle = diff(get_w(),greater(yy[0]+yy[1],xx[0]+xx[1]))/2;
double aaar = (1.0/lle);
point_arr* pa;
pa = malloc(sizeof(*pa));
pa->c = malloc(sizeof(*pa->c)*(lle*60));
pa->vert = malloc(sizeof(*pa->vert)*(n*60));
if(pa->c==NULL||pa->vert==NULL)
err("failed to allocate basier array",pexit);
pa->len = lle;
for(int iy = 0; iy<=lle;iy+=1){
double t = aaar*iy;
double bcx = 0;
double bcy = 0;
for(int i = 0; i <=n;i++){
double pp = binomial(n,i) * pow((1 - t),(n - i)) * pow(t,i);
bcx += pp * xx[i];
bcy += pp * yy[i];
}
pa->c[iy].at.x = bcx;
pa->c[iy].at.y = bcy;
pa->c[iy].at.vertex = 0;
for(int as = 0; as<=n; as++){
if(xx[as]==bcx&&yy[as]==bcy){
pa->c[iy].at.vertex = 1;
break;
}
}
pa->c[iy].color.r = rr;
pa->c[iy].color.g = gg;
pa->c[iy].color.b = bb;
}
for(int i = 0; i<=n; i++){
pa->vert[i].at.x = xx[i];
pa->vert[i].at.y = yy[i];
}
pa->vlen = n;
return pa;
}
typedef struct {
GLfloat* pix;
GLfloat* col;
int len;
} glfl_ar;
void render_p(glfl_ar* bba){
GLuint verta;
glGenVertexArrays(1,&verta);
glBindVertexArray(verta);
GLuint vetb;
glGenBuffers(1,&vetb);
glBindBuffer(GL_ARRAY_BUFFER,vetb);
glBufferData(GL_ARRAY_BUFFER,sizeof(*bba->pix)*(bba->len*3),bba->pix,GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER,vetb);
glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,0,(void*)0);
GLuint colb;
glGenBuffers(1,&colb);
glBindBuffer(GL_ARRAY_BUFFER,colb);
glBufferData(GL_ARRAY_BUFFER,sizeof(*bba->col)*(bba->len*4),bba->col,GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER,colb);
glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,(void*)0);
glDrawArrays(GL_POINTS,0,bba->len);
glDeleteBuffers(1,&vetb);
glDeleteBuffers(1,&colb);
}
glfl_ar* perspective_proj(GLFWwindow* b,point_arr* c,double ctx,double cty,double ctz,double cx, double cy, double cz){
GLfloat* pixels = malloc(sizeof(*pixels)*((1+c->len)*4));
GLfloat* colors = malloc(sizeof(*colors)*((1+c->len)*4));
if(pixels==NULL||colors==NULL)
err("failed to allocate perspective array:(",pexit);
double coy = cos(cty);
double siz = sin(ctz);
double coz = cos(ctz);
double six = sin(ctx);
double siy = sin(cty);
double cox = cos(ctx);
double fov = 0.002;
double ex = cx;
double ey = cy;
refresh_size(b);
GLuint fb = 0;
int c_len = 0;
//double ez=1/tan(fov/2); //i dont get this at all
double ez=get_w()*2;
glEnableClientState(GL_VERTEX_ARRAY);
for(int i = 0; i!=c->len; i++){
double ax = c->c[i].at.x;
double ay = c->c[i].at.y;
double az = c->c[i].at.z;
double eyz = (coz*(ay-cy)-siz*ax-cx);
double yzm = (coy*(az-cz) + siy*(siz*(ay-cy) + coz*(ax-cx)));
double dx = coy * (siz*(ay-cy) + coz*(ax-cx)) - (siy*(az-cz));
double dy = six * yzm + cox*eyz;
double dz = cox * yzm - six*eyz;
double bx = ez/dz*dx+dx;
double by = ez/dz*dy+dy;
if(dz>-1){
ab_to_vp(xa,ya,get_w(),get_h(),bx,by);
pixels[c_len*2] = xa+1;
pixels[c_len*2+1] = ya;
colors[c_len*3] = c->c[i].color.r;
colors[c_len*3+1] = c->c[i].color.g;
colors[c_len*3+2] = c->c[i].color.b;
c_len++;
}
}
double fc_len = c_len;
for(int i = 0; i<=fc_len-1; i++){
double x22[2] = {pixels[i*2],pixels[(i+1)*2]};
double y22[2] = {pixels[i*2+1],pixels[(i+1)*2+1]};
point_arr* bas = basier2d(x22,y22,2,0.1f,0.1f,0.1f);
for(int zaa=0; zaa<=bas->len; zaa++){
pixels = realloc(pixels,sizeof *pixels *((c_len+1)*3));
colors = realloc(colors,sizeof *colors *((c_len+1)*4));
pixels[c_len*2] = bas->c[zaa].at.x;
pixels[c_len*2+1] = bas->c[zaa].at.y;
colors[c_len*3] = 0.1f;
colors[c_len*3+1] = 1.0f;
colors[c_len*3+2] = 1.0f;
c_len++;
}
free(bas->c);
free(bas->vert);
free(bas);
}
double dclen = c_len;
int vvi = 0;
//printf("---\n");
glfl_m* trline = malloc(sizeof(*trline)*get_w()*30);
trline->len = 0;
trline->at = malloc(sizeof(*trline->at)*get_w()*40);
for(int i = 0; i<=fc_len; i++){
if(c->c[i].at.vertex==1||1){
if(pixels==NULL||colors==NULL)
abort();
vvi++;
trline->at[trline->len].at = malloc(sizeof(*trline->at[trline->len].at)*(c_len+get_w()*2)*30);
trline->at[trline->len].len = 0;
//printf("%f\n",dclen);
int le = 2222;
cord aaa = poi_d(pixels[i*2],pixels[i*2+1],le,pixels[i*2+1],c->len*2,pixels,1);
cord aab = poi_d(pixels[i*2],pixels[i*2+1],-le,pixels[i*2+1],c->len*2,pixels,1);
cord aac = poi_d(pixels[i*2],pixels[i*2+1],pixels[i*2],-le,c->len*2,pixels,1);
cord aad = poi_d(pixels[i*2],pixels[i*2+1],pixels[i*2],le,c->len*2,pixels,1);
/*if(aac.z||aad.z==-1){
free(trline->at[trline->len].at);
continue;
}*/
//printf("%f %f %f %f\n",aaa.z,aab.z,aac.z,aad.z);
if(fmod(aaa.z,2)==1||fmod(aab.z,2)==1||
fmod(aac.z,2)==1||fmod(aad.z,2)==1){
free(trline->at[trline->len].at);
continue;
}
//printf("a\n");
if(aab.z!=-1)
{double aax[] = {pixels[i*2],aab.x};
double aay[] = {pixels[i*2+1],aab.y};
point_arr* frl = basier2d(aax,aay,2,0.1,0.1,0.1);
for(int cci = 0; cci<=frl->len; cci++){
trline->at[trline->len].at[trline->at[trline->len].len*2] = frl->c[cci].at.x;
trline->at[trline->len].at[trline->at[trline->len].len*2+1] = frl->c[cci].at.y;
pixels = realloc(pixels,sizeof *pixels *((c_len+1)*3));
colors = realloc(colors,sizeof *colors *((c_len+1)*4));
pixels[c_len*2] = frl->c[cci].at.x;
pixels[c_len*2+1] = frl->c[cci].at.y;
colors[c_len*3] = 0.1f;
colors[c_len*3+1] = 0.3f;
colors[c_len*3+2] = 1.0f;
c_len++;
//trline->len++;
trline->at[trline->len].len++;
}
free(frl->c);
free(frl->vert);
free(frl);}
if(aaa.z!=-1)
{double aax[] = {pixels[i*2],aaa.x};
double aay[] = {pixels[i*2+1],aaa.y};
point_arr* frl = basier2d(aax,aay,2,0.1,0.1,0.1);
for(int cci = 0; cci<=frl->len; cci++){
trline->at[trline->len].at[trline->at[trline->len].len*2] = frl->c[cci].at.x;
trline->at[trline->len].at[trline->at[trline->len].len*2+1] = frl->c[cci].at.y;
pixels = realloc(pixels,sizeof *pixels *((c_len+1)*3));
colors = realloc(colors,sizeof *colors *((c_len+1)*4));
pixels[c_len*2] = frl->c[cci].at.x;
pixels[c_len*2+1] = frl->c[cci].at.y;
colors[c_len*3] = 0.1f;
colors[c_len*3+1] = 0.3f;
colors[c_len*3+2] = 1.0f;
c_len++;
trline->at[trline->len].len++;
}
free(frl->c);
free(frl->vert);
free(frl);}
/*pixels = realloc(pixels,sizeof *pixels *((c_len+1)*3));
colors = realloc(colors,sizeof *colors *((c_len+1)*4));
//printf("%f %f\n",aaa.x,aaa.y);
pixels[c_len*2] = aaa.x;
pixels[c_len*2+1] = aaa.y;
colors[c_len*3] = 0.1f;
colors[c_len*3+1] = 0.3f;
colors[c_len*3+2] = 1.0f;
c_len++;*/
/*
double ttt = -1.0;
int p_b = 0;
int p_b2 = 0;
int p_b3 = 0;
int p_b4 = 0;
double ttt2 = 1.0;
int found1 = 1;
int found2 = 1;
for(int jj = 0; jj<=dclen-1; jj++){
float sad = 3.0f;
float sad2 = 30.0f;
if(pixels[jj*2]<pixels[i*2]&&diff(pixels[jj*2],pixels[i*2])>(float)sad2/get_w()
&&diff(pixels[jj*2+1],pixels[i*2+1])<(float)sad/get_w()&&i!=jj){
p_b=1;
if(pixels[jj*2]>ttt)
ttt=pixels[jj*2];
}
if(diff(pixels[jj*2],pixels[i*2])>(float)sad2/get_w()
&&diff(pixels[jj*2+1],pixels[i*2+1])<(float)sad/get_w()&&i!=jj&&
pixels[jj*2]>pixels[i*2]){
p_b2=1;
if(pixels[jj*2]<ttt2)
ttt2=pixels[jj*2];
}
if(diff(pixels[jj*2+1],pixels[i*2+1])>(float)sad/get_w()
&&diff(pixels[jj*2],pixels[i*2])<(float)sad2/get_w()&&i!=jj&&pixels[jj*2+1]>pixels[i*2+1]){
p_b3=1;
}
if(diff(pixels[jj*2+1],pixels[i*2+1])>(float)sad/get_w()
&&diff(pixels[jj*2],pixels[i*2])<(float)sad2/get_w()&&i!=jj&&pixels[jj*2+1]<pixels[i*2+1]){
p_b4=1;
}
}
if(!p_b3||!p_b4){
free(trline->at[trline->len].at);
continue;
}
double cb = 1.0;
double sb = 1.0/get_w();
double ib = cb/get_w();
if(p_b)
for(double zz = pixels[i*2]; diff(zz,ttt)>ib&&zz>-1;zz-=(float)sb){
pixels = realloc(pixels,sizeof *pixels *((c_len+1)*3));
colors = realloc(colors,sizeof *colors *((c_len+1)*4));
trline->at[trline->len].at[trline->at[trline->len].len*2] = zz;
trline->at[trline->len].at[trline->at[trline->len].len*2+1] = pixels[i*2+1];
pixels[c_len*2] = zz;
pixels[c_len*2+1] = pixels[i*2+1];
colors[c_len*3] = 0.1f;
colors[c_len*3+1] = vvi==3?0.1f:vvi==4?0.5f:1.0f;
colors[c_len*3+2] = 1.0f;
c_len++;
trline->at[trline->len].len++;
}
if(p_b2)
for(double zz = pixels[i*2]; diff(zz,ttt2)>ib&&zz<1;zz+=(float)sb){
pixels = realloc(pixels,sizeof *pixels *((c_len+1)*3));
colors = realloc(colors,sizeof *colors *((c_len+1)*4));
int brea = 0;
trline->at[trline->len].at[trline->at[trline->len].len*2] = zz;
trline->at[trline->len].at[trline->at[trline->len].len*2+1] = pixels[i*2+1];
pixels[c_len*2] = zz;
pixels[c_len*2+1] = pixels[i*2+1];
colors[c_len*3] = 0.1f;
colors[c_len*3+1] = vvi==3?0.1f:vvi==4?0.5f:1.0f;
colors[c_len*3+2] = 1.0f;
c_len++;
trline->at[trline->len].len++;
}
trline->len++;
*/
trline->len++;
}
}
if(trline->len>1){
for(int ii = 0; ii!=trline->len-1; ii++){
if(trline->at[ii].at[3]<trline->at[ii+1].at[3]){
glfl_a temp = trline->at[ii];
trline->at[ii] = trline->at[ii+1];
trline->at[ii+1] = temp;
ii=-1;
}
}}
double ffclen = c_len;
double lmax_t = -2.0;
double lmin_t = 2.0;
double lmax2_t = -2.0;
double lmin2_t = 2.0;
for(int zzi = 0; zzi!=trline->len; zzi++){
double max_t = -2.0;
double min_t = 2.0;
double max2_t = -2.0;
double min2_t = 2.0;
for(int zzi2 = 0; zzi2!=trline->at[zzi].len;zzi2++){
if(max_t<trline->at[zzi].at[zzi2*2]){
max_t = trline->at[zzi].at[zzi2*2];
max2_t = trline->at[zzi].at[zzi2*2+1];
}
if(min_t>=trline->at[zzi].at[zzi2*2]){
min_t = trline->at[zzi].at[zzi2*2];
min2_t = trline->at[zzi].at[zzi2*2+1];
}
}
free(trline->at[zzi].at);
if (trline->at[zzi].len == 0)
continue;
if(lmax2_t!=max2_t&&lmin_t!=2.0&&lmax_t!=-2.0){
float color = (float)zzi/trline->len;
float color2 = 1.0f-((float)zzi/trline->len);
double di = pow(lmin_t-max_t,2)+pow(lmin2_t-max2_t,2);//sqrt should be used here
double di2 = pow(lmax_t-min_t,2)+pow(lmax2_t-min2_t,2);
double ux1, uy1, ux2, uy2;
int ma_to_mi = 0;
if (di > di2) {
ux1 = max_t;
uy1 = max2_t;
ux2 = lmin_t;
uy2 = lmin2_t;
ma_to_mi=1;
} else {
ux1 = min_t;
uy1 = min2_t;
ux2 = lmax_t;
uy2 = lmax2_t;
}
color2 = 1.0f;
double x1 = ma_to_mi?max_t:min_t;
double x2 = ma_to_mi?lmin_t:lmax_t;
double ite = ((float)get_w()/4);
double ite1 = fabs(max_t/ite);
double ite2 = fabs(lmax_t/ite);
double y1 = uy1;
double y2 = uy2;
for(;(ma_to_mi?x1>=x2:x1<=x2);(ma_to_mi?(x1-=ite1):(x1+=ite2))){
cord aaaa = poi_d(x1,y1,x2,y2,fc_len,pixels,0);
if(aaaa.z!=-1){
/*pixels = realloc(pixels,sizeof *pixels *((c_len+1)*3));
colors = realloc(colors,sizeof *colors *((c_len+1)*4));
//printf("%f %f\n",aaa.x,aaa.y);
pixels[c_len*2] = x1;
pixels[c_len*2+1] = y1;
colors[c_len*3] = 1.0f;
colors[c_len*3+1] = 0.3f;
colors[c_len*3+2] = 1.0f;
c_len++;
pixels = realloc(pixels,sizeof *pixels *((c_len+1)*3));
colors = realloc(colors,sizeof *colors *((c_len+1)*4));
//printf("%f %f\n",aaa.x,aaa.y);
pixels[c_len*2] = aaaa.x;
pixels[c_len*2+1] = aaaa.y;
colors[c_len*3] = 1.0f;
colors[c_len*3+1] = 0.0f;
colors[c_len*3+2] = 0.0f;
c_len++;*/
//printf("%f %f\n",aaaa.x,aaaa.y);
continue;
}
double bb[] = {x1,x2};
double bb2[] = {y1, y2};
point_arr* asd = basier2d(bb,bb2,2,0.1,0.1,0.1);
for(int lli = 0; lli!=asd->len; lli++){
pixels = realloc(pixels,sizeof *pixels *((c_len+1)*4));
colors = realloc(colors,sizeof *colors *((c_len+1)*5));
double dd = 10;
pixels[c_len*2] = asd->c[lli].at.x;
pixels[c_len*2+1] = asd->c[lli].at.y;
colors[c_len*3] = color2;
colors[c_len*3+1] = color;//vvi==3?0.1f:vvi==4?0.5f:1.0f;
colors[c_len*3+2] = 0.0f;
c_len++;
}
free(asd->c);
free(asd->vert);
free(asd);
break;
}/*
pixels = realloc(pixels,sizeof *pixels *((c_len+1)*3));
colors = realloc(colors,sizeof *colors *((c_len+1)*4));
//printf("%f %f\n",aaa.x,aaa.y);
pixels[c_len*2] = ux1;
pixels[c_len*2+1] = uy1;
colors[c_len*3] = 1.0f;
colors[c_len*3+1] = 0.3f;
colors[c_len*3+2] = 1.0f;
c_len++;
pixels = realloc(pixels,sizeof *pixels *((c_len+1)*3));
colors = realloc(colors,sizeof *colors *((c_len+1)*4));
//printf("%f %f\n",aaa.x,aaa.y);
pixels[c_len*2] = ux2;
pixels[c_len*2+1] = uy2;
colors[c_len*3] = 1.0f;
colors[c_len*3+1] = 1.0f;
colors[c_len*3+2] = 1.0f;
c_len++;*/
}
lmax_t = max_t;
lmin_t = min_t;
lmax2_t = max2_t;
lmin2_t = min2_t;
}
free(trline->at);
free(trline);
glPointSize(4.0f);
glfl_ar* rea = malloc(sizeof(*rea));
rea->col = colors;
rea->pix = pixels;
rea->len = c_len;
return rea;
}
point_arr* basier3d(double*xx,double*yy,double*zz,int n,float rr, float gg, float bb){
point_arr* pa;
pa = malloc(sizeof(*pa));
pa->c = malloc(sizeof(*pa->c)*(get_w()*60));
pa->vert = malloc(sizeof(*pa->vert)*(n*60));
if(pa->c==NULL||pa->vert==NULL)
err("failed to allocate basier array",pexit);
n-=1;
double aaar = (1.0/get_w());
double am = 2;
pa->len = am;
for(int iy = 0; iy<=2;iy+=1){
double t = (1.0/am)*iy;
double bcx = 0;
double bcy = 0;
double bcz = 0;
for(int i = 0; i <=n;i++){
double pp = binomial(n,i) * pow((1 - t),(n - i)) * pow(t,i);
bcx += pp * xx[i];
bcy += pp * yy[i];
bcz += pp * zz[i];
}
pa->c[iy].at.x = bcx;
pa->c[iy].at.y = bcy;
pa->c[iy].at.z = bcz;
pa->c[iy].at.vertex = 0;
for(int as = 0; as<=n; as++){
if(xx[as]==bcx&&yy[as]==bcy&&zz[as]==bcz){
pa->c[iy].at.vertex = 1;
break;
}
}
pa->c[iy].color.r = rr;
pa->c[iy].color.g = gg;
pa->c[iy].color.b = bb;
}
for(int i = 0; i<=n; i++){
pa->vert[i].at.x = xx[i];
pa->vert[i].at.y = yy[i];
pa->vert[i].at.z = zz[i];
}
pa->vlen = n;
return pa;
}
void join_cords(point_arr* a, point_arr* b){
int a_len = a->len;
int a_vlen = a->vlen;
a->c = realloc(a->c,sizeof(*a->c)*(a->len+b->len)*60);
a->vert = realloc(a->vert,sizeof(*a->vert)*(a->vlen+b->vlen)*60);
a->len+=b->len;
a->vlen+=b->vlen;
if(a->c==NULL)
err("failed to reallocate cords",pexit);
for(int i = 0; i<=b->len; i++){
a->c[a_len+i].at = b->c[i].at;
a->c[a_len+i].color = b->c[i].color;
}
for(int i = 0; i<=b->vlen; i++){
a->vert[a_vlen+i].at = b->vert[i].at;
}
}
void join_glfl_a(glfl_ar* a, glfl_ar* b){
int a_len = a->len;
a->pix = realloc(a->pix,sizeof(*a->pix)*(a->len+b->len)*60);
a->col = realloc(a->col,sizeof(*a->col)*(a->len+b->len)*60);
a->len+=b->len;
if(a->pix==NULL||a->col==NULL)
err("failed to reallocate float array",pexit);
for(int i = 0; i<=b->len*2; i++){
a->pix[a_len*2+i] = b->pix[i];
}
for(int i = 0; i<=b->len*3; i++){
a->col[a_len*3+i] = b->col[i];
}
}
point_arr* polygon3d(double* vx, double*vy, double* vz, int n){
/*double xx[2] = {vx[0],vx[1]};
double yy[2] = {vy[0],vy[1]};
double zz[2] = {vz[0],vz[1]};
point_arr* y = basier3d(xx,yy,zz,2,1.0f,0.0f,0.5f);
for(int i = 1; i<=n-2; i++){
double xx1[2] = {vx[i],vx[i+1]};
double yy1[2] = {vy[i],vy[i+1]};
double zz1[2] = {vz[i],vz[i+1]};
point_arr* aa = basier3d(xx1,yy1,zz1,2,1.0f,0.0f,0.5f);
join_cords(y,aa);
free(aa->c);
free(aa->vert);
free(aa);
}*/
point_arr* pa;
pa = malloc(sizeof(*pa));
pa->c = malloc(sizeof(*pa->c)*(get_w()*60));
pa->vert = malloc(sizeof(*pa->vert)*(n*60));
if(pa->c==NULL||pa->vert==NULL)
err("failed to allocate polygon array",pexit);
n-=1;
for(int i = 0; i<=n; i++){
pa->c[i].at.x = vx[i];
pa->c[i].at.y = vy[i];
pa->c[i].at.z = vz[i];
pa->vert[i].at.x = vx[i];
pa->vert[i].at.y = vy[i];
pa->vert[i].at.z = vz[i];
pa->c[i].color.r = 0.1f;
pa->c[i].color.g = 0.1f;
pa->c[i].color.b = 1.0f;
}
pa->len = n;
return pa;
}
point_arr* square_gen(double* tl, double* tr, double* bl, double*br,float rr, float gg, float bb){
double xx[3] = {tl[0],tr[0]};
double yy[3] = {tl[1],tr[1]};
double zz[3] = {tl[2],tr[2]};
point_arr* a = polygon3d(xx,yy,zz,2);
double xx1[3] = {tl[0],bl[0]};
double yy1[3] = {tl[1],bl[1]};
double zz1[3] = {tl[2],bl[2]};
point_arr* b = polygon3d(xx1,yy1,zz1,2);
double xx2[3] = {tr[0],br[0]};
double yy2[3] = {tr[1],br[1]};
double zz2[3] = {tr[2],br[2]};
point_arr* c = polygon3d(xx2,yy2,zz2,2);
double xx3[3] = {bl[0],br[0]};
double yy3[3] = {bl[1],br[1]};
double zz3[3] = {bl[2],br[2]};
point_arr* d = polygon3d(xx3,yy3,zz3,2);
join_cords(a,b);
join_cords(a,c);
join_cords(a,d);
free(b->c);
free(b->vert);
free(b);
free(c->c);
free(c->vert);
free(c);
free(d->c);
free(d->vert);
free(d);
return a;
}
void fill3d(point_arr* a){
warn("please dont use this lol");
double m_x = 0.0;
double m_y = 0.0;
double m_z = 0.0;
double mi_x = 0.0;
double mi_y = 0.0;
double mi_z = 0.0;
for(int i = 0; i<=a->len; i++){
if(a->c[i].at.x>m_x)
m_x = a->c[i].at.x;
if(a->c[i].at.y>m_y)
m_y = a->c[i].at.y;
if(a->c[i].at.z>m_z)
m_z = a->c[i].at.z;
if(a->c[i].at.x<mi_x||mi_x==0)
mi_x = a->c[i].at.x;
if(a->c[i].at.y<mi_y||mi_y==0)
mi_y = a->c[i].at.y;
if(a->c[i].at.z<mi_z||mi_z==0)
mi_z = a->c[i].at.z;
}
int a_l = a->len;
a->c = realloc(a->c,sizeof(*a->c)*(m_x*m_y+a->len)*60);
for(double y = mi_y; y<=m_y; y+=.1){
double* zz = malloc(sizeof(*zz));
int zzl = 0;
double* xx = malloc(sizeof(*xx));
double* yy = malloc(sizeof(*yy));
for(int i = 0; i<=a_l; i++){
if(diff(a->c[i].at.y,y)<.1){
zz = realloc(zz,sizeof(*zz)*(zzl+1));
zz[zzl] = a->c[i].at.z;
xx = realloc(xx,sizeof(*xx)*(zzl+1));
xx[zzl] = a->c[i].at.x;
yy = realloc(yy,sizeof(*yy)*(zzl+1));
yy[zzl] = y;
zzl++;
}
}
point_arr* pp = basier3d(xx,yy,zz,zzl,1.0,0.0,0.0);
join_cords(a,pp);
free(pp->c);
free(pp);
free(xx);
free(yy);
free(zz);
}
}
point_arr* cube_gen(double* tl, double* tr, double* bl, double*br,
double* tl2, double* tr2, double* bl2, double*br2,
float rr, float gg, float bb){
point_arr* a = square_gen(tl,tr,bl,br,rr,gg,bb);
point_arr* b = square_gen(tl2,tr2,bl2,br2,rr,gg,bb);
double s;
join_cords(a,b);
free(b->c);
free(b->vert);
free(b);
point_arr* c = square_gen(tl2,tr2,tl,tr,rr,gg,bb);
join_cords(a,c);
free(c->c);
free(c->vert);
free(c);
point_arr* d = square_gen(bl2,br2,bl,br,rr,gg,bb);
join_cords(a,d);
free(d->c);
free(d->vert);
free(d);
point_arr* e = square_gen(tl,tl2,bl,bl2,rr,gg,bb);
join_cords(a,e);
free(e->c);
free(e->vert);
free(e);
point_arr* f = square_gen(br2,br,tr2,tr,rr,gg,bb);
join_cords(a,f);
free(f->c);
free(f->vert);
free(f);
return a;
}
int main(int argc,char*argv[]){
flag_handle(argc,argv);
atexit(sig_handle);
GLFWwindow* w = glfw_init();
refresh_size(w);
GLenum err = glewInit();
if (err != GLEW_OK)
exit(1); // or handle the error in a nicer way
if (!GLEW_VERSION_2_1) // check that the machine supports the 2.1 API.
exit(1); // or handle the error in a nicer way
GLuint vid = vshader_comp(vshader_src);
GLuint fid = fshader_comp(fshader_src);
prog = build_shader(vid,fid);
glUseProgram(prog);
logm("built shaders");
/*
double tl2[3] = {5.0,200.0,400.0};
double tr2[3] = {200.0,200.0,400.0};
double bl2[3] = {5.0,5.0,200.0};
double br2[3] = {200.0,5.0,200.0};
double tl1[3] = {5.0,200.0,200.0};
double tr1[3] = {200.0,200.0,200.0};
double bl1[3] = {5.0,5.0,5.0};
double br1[3] = {200.0,5.0,5.0};
point_arr* a = cube_gen(tl1,tr1,bl1,br1,tl2,tr2,bl2,br2,0.1f,0.1f,1.0f);
*/double xx[8] = {0.0, 15.0, 50.0, 60.0,40.0,30.0, 0.0,0.0};
double yy[8] = {5.0, 15.0, 30.0, 45.0,64.0, 45.0,55.0,5.0};
double zz[8] = {50.0,50.0,50.0,50.0,50.0,50.0,50.0,50.0};
point_arr* a = polygon3d(xx,yy,zz,8);
/*
double xx2[8] = {0.0, 15.0, 50.0, 60.0,20.0,10.0, 0.0,0.0};
double yy2[8] = {5.0, 15.0, 30.0, 20.0,64.0, 45.0,55.0,5.0};
double zz2[8] = {50.0,50.0,50.0,50.0,50.0,50.0,50.0,50.0};
point_arr* a2 = polygon3d(xx2,yy2,zz2,8);
*/
int max_r = 630;
double half_max_r = (double)max_r/2/2;
double pl_x = 0;
double pl_y = 0;
double pl_z = 0;
double plr_x = 0;
double plr_y = 0;
clock_t t;
double frames = 0;
t = clock();
for(;;){
double p1 = plr_x*0.01;
double p2 = plr_y*0.01;
double p3 = 0;
double p4 = pl_y;
double p5 = -pl_y+pl_z;
double p6 = pl_x;
glfl_ar* bba = perspective_proj(w,a,p1,p2,p3,p4,p5,p6);
render_p(bba);
free(bba->col);
free(bba->pix);
free(bba);
glfw_load(w);
int mod_move=2;
double run_mul=2;
glfwPollEvents();
if(glfwGetKey(w,GLFW_KEY_R)){
pl_x = 0;
pl_y = 0;
pl_z = 0;
plr_x = 0;
plr_y = 0;
}
if(glfwGetKey(w,GLFW_KEY_P))
printf("(x:%f,y:%f,z:%f),rot(x:%f,y:%f,z:%f)||l(p1:%f,p2:%f,p3:%f,p4:%f,p5:%f,p6:%f)\n",pl_x,pl_y,pl_z,plr_x,plr_y,0.0,p1,p2,p3,p4,p5,p6);
if(glfwGetKey(w,GLFW_KEY_I)){
if(glfwGetKey(w,GLFW_KEY_LEFT_SHIFT))
plr_x-=mod_move;
else
plr_x--;
plr_x = fmod(plr_x,max_r);
}
if(glfwGetKey(w,GLFW_KEY_K)){
if(glfwGetKey(w,GLFW_KEY_LEFT_SHIFT))
plr_x+=mod_move;
else
plr_x++;
plr_x = fmod(plr_x,max_r);
}
if(glfwGetKey(w,GLFW_KEY_J)){
if(glfwGetKey(w,GLFW_KEY_LEFT_SHIFT))
plr_y-=mod_move;
else
plr_y--;
plr_y = fmod(plr_y,max_r);
}
if(glfwGetKey(w,GLFW_KEY_L)){
if(glfwGetKey(w,GLFW_KEY_LEFT_SHIFT))
plr_y+=mod_move;
else
plr_y++;
plr_y = fmod(plr_y,max_r);
}
if(glfwGetKey(w,GLFW_KEY_W)){
double mul = 1;
if(glfwGetKey(w,GLFW_KEY_LEFT_SHIFT))
mul = run_mul;
pl_x+=cosf(plr_y*0.01)*mul;
pl_y+=sinf(plr_y*0.01)*mul;
pl_z-=sinf(plr_x*0.01)*mul;
}
if(glfwGetKey(w,GLFW_KEY_S)){
double mul = 1;
if(glfwGetKey(w,GLFW_KEY_LEFT_SHIFT))
mul = run_mul;
pl_x-=cosf(plr_y*0.01)*mul;
pl_y-=sinf(plr_y*0.01)*mul;
pl_z+=sinf(plr_x*0.01)*mul;
}
if(glfwGetKey(w,GLFW_KEY_A)){
double mul = 1;
if(glfwGetKey(w,GLFW_KEY_LEFT_SHIFT))
mul = run_mul;
pl_x-=cosf((half_max_r+plr_y)*0.01)*mul;
pl_y-=sinf((half_max_r+plr_y)*0.01)*mul;
}
if(glfwGetKey(w,GLFW_KEY_D)){
double mul = 1;
if(glfwGetKey(w,GLFW_KEY_LEFT_SHIFT))
mul = run_mul;
pl_x+=cosf((half_max_r+plr_y)*0.01)*mul;
pl_y+=sinf((half_max_r+plr_y)*0.01)*mul;
}
usleep(1000*1000/60);
glfw_clear(w);
if(glfwWindowShouldClose(w)||(glfwGetKey(w,GLFW_KEY_Q)))break;
frames+=1;
if(((double)clock() - t)/CLOCKS_PER_SEC > 1){
printf("%f fps\n",frames);
frames=0;
t = clock();
}
}
free(a->c);
free(a->vert);
free(a);
glfwDestroyWindow(w);
win_clean();
glDeleteShader(vid);
glDeleteShader(fid);
glDeleteShader(prog);
logm("killed window:p");
return 0;
}
|