-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
904 lines (690 loc) · 23.3 KB
/
main.c
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
#include <cairo.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include "rt.h"
#include "rt_debug_output.h"
#include "rt_meshes.h"
#define SPHERE_COUNT 10
#define POINT_LIGHTS_COUNT 1
#define SQUARES_COUNT_SQRT 150
#define BOTTOM 1
#define MATERIAL_COUNT 2 + SPHERE_COUNT
#define RENDER_TO_PNGS 0
#define RENDER_TO_SCREEN 1
#define PNGS_PATH "pngs/"
#define WIDTH 1280
#define HEIGHT 768
typedef struct _wave
{
float period;
float amplitude;
float angle;
float speed;
} wave;
GtkWidget *mainWindow;
GtkWidget *drawArea;
cairo_surface_t *csur;
rt_argb *data;
guint rotTID;
rt_render_pipe renderPipe;
rt_mesh fluttershy;
rt_mesh bottom;
rt_mesh water;
rt_sphere sp[SPHERE_COUNT];
rt_point_light lt[POINT_LIGHTS_COUNT];
rt_float koef[SPHERE_COUNT];
rt_material mt[MATERIAL_COUNT];
rt_float t = 0.0f;
int w, h;
rt_float plSz = 150.0f;
rt_float plHt = 20.0f;
double avr = 0.0f;
int frm = 0;
unsigned long int pngN = 0;
float centralSpherePosY = 0.0f;
float dT = 0.0001f;
float oldDT = 0.001f;
int paused = 0;
rt_vector3 camPos;
//parameter for spheres moving equasions
int rotStep( gpointer data )
{
if ( t > 2*M_PI )
t = 0.0f;
t += dT;
gtk_widget_queue_draw(mainWindow);
return 1;
}
void windowDestroy( GtkWidget *wgt, gpointer u )
{
if ( RENDER_TO_PNGS )
{
int save = open( "save", O_TRUNC | O_CREAT | O_WRONLY,
S_IRWXU | S_IRWXG | S_IRWXO );
write( save, (void *)(&t), sizeof(rt_float) );
write( save, (void *)(&dT), sizeof(rt_float) );
write( save, (void *)(&pngN), sizeof(unsigned long int) );
close(save);
}
else
printf( "t = %1.10f, dT = %1.10f\n", t, dT );
g_source_remove( rotTID );
}
void keyPress( GtkWidget *w, GdkEvent *e, gpointer usrData )
{
rt_camera *cam = NULL;
rt_matrix4 m;
rt_matrix4 tmp;
rt_render_pipe_get_camera( &renderPipe, &cam );
switch ( e->key.keyval )
{
case GDK_KEY_u:
rt_matrix4_create_rotate( &m, M_PI*0.01f, RT_AXIS_Z );
rt_matrix4_create_translate( &tmp, -camPos.x,
-camPos.y, -camPos.z );
rt_matrix4_mult( &m, &tmp, &m );
rt_matrix4_create_translate( &tmp, camPos.x,
camPos.y, camPos.z );
rt_matrix4_mult( &tmp, &m, &m );
goto applyTransform;
case GDK_KEY_bracketleft:
rt_matrix4_create_rotate( &m, -M_PI*0.01f, RT_AXIS_Z );
rt_matrix4_create_translate( &tmp, -camPos.x,
-camPos.y, -camPos.z );
rt_matrix4_mult( &m, &tmp, &m );
rt_matrix4_create_translate( &tmp, camPos.x,
camPos.y, camPos.z );
rt_matrix4_mult( &tmp, &m, &m );
goto applyTransform;
case GDK_KEY_p:
rt_matrix4_create_rotate( &m, -M_PI*0.01f, RT_AXIS_Y );
rt_matrix4_create_translate( &tmp, -camPos.x,
-camPos.y, -camPos.z );
rt_matrix4_mult( &m, &tmp, &m );
rt_matrix4_create_translate( &tmp, camPos.x,
camPos.y, camPos.z );
rt_matrix4_mult( &tmp, &m, &m );
goto applyTransform;
case GDK_KEY_i:
rt_matrix4_create_rotate( &m, M_PI*0.01f, RT_AXIS_Y );
rt_matrix4_create_translate( &tmp, -camPos.x,
-camPos.y, -camPos.z );
rt_matrix4_mult( &m, &tmp, &m );
rt_matrix4_create_translate( &tmp, camPos.x,
camPos.y, camPos.z );
rt_matrix4_mult( &tmp, &m, &m );
goto applyTransform;
case GDK_KEY_apostrophe:
rt_matrix4_create_rotate( &m, M_PI*0.01f, RT_AXIS_X );
rt_matrix4_create_translate( &tmp, -camPos.x,
-camPos.y, -camPos.z );
rt_matrix4_mult( &m, &tmp, &m );
rt_matrix4_create_translate( &tmp, camPos.x,
camPos.y, camPos.z );
rt_matrix4_mult( &tmp, &m, &m );
goto applyTransform;
case GDK_KEY_j:
rt_matrix4_create_rotate( &m, -M_PI*0.01f,RT_AXIS_X );
rt_matrix4_create_translate( &tmp, -camPos.x,
-camPos.y, -camPos.z );
rt_matrix4_mult( &m, &tmp, &m );
rt_matrix4_create_translate( &tmp, camPos.x,
camPos.y, camPos.z );
rt_matrix4_mult( &tmp, &m, &m );
goto applyTransform;
case GDK_KEY_semicolon:
rt_matrix4_create_translate( &m, 0.0f, 1.0f, 0.0f );
camPos.y -= 1.0f;
goto applyTransform;
case GDK_KEY_k:
rt_matrix4_create_translate( &m, 0.0f, -1.0f, 0.0f );
camPos.y += 1.0f;
goto applyTransform;
case GDK_KEY_o:
rt_matrix4_create_translate( &m, 1.0f, 0.0f, 0.0f );
camPos.x -= 1.0f;
goto applyTransform;
case GDK_KEY_l:
rt_matrix4_create_translate( &m, -1.0f, 0.0f, 0.0f );
camPos.x += 1.0f;
goto applyTransform;
case GDK_KEY_space:
camPos.z += 1.0f;
rt_matrix4_create_translate( &m, 0.0f, 0.0f, -1.0f );
goto applyTransform;
case GDK_KEY_comma:
camPos.z -= 1.0f;
rt_matrix4_create_translate( &m, 0.0f, 0.0f, 1.0f );
goto applyTransform;
case GDK_KEY_t:
centralSpherePosY -= 0.5f;
break;
case GDK_KEY_y:
centralSpherePosY += 0.5f;
break;
case GDK_KEY_g:
if ( paused )
dT = oldDT;
else
{
oldDT = dT;
dT = 0.0f;
}
paused = !paused;
break;
case GDK_KEY_f:
rt_dout_matrix4( &(cam->world) );
printf( "\n" );
break;
case GDK_KEY_equal:
dT += 0.0001f;
break;
case GDK_KEY_minus:
dT -= 0.0001f;
break;
default:
break;
applyTransform:
rt_matrix4_mult( &(cam->world), &m,
&(cam->world) );
gtk_widget_queue_draw(mainWindow);
}
rt_render_pipe_free_camera( &renderPipe, cam );
}
void resizeImage()
{
GtkAllocation alloc;
rt_camera *cam = NULL;
gtk_widget_get_allocation( drawArea, &alloc );
w = alloc.width;
h = alloc.height;
csur = cairo_image_surface_create( CAIRO_FORMAT_RGB24, w, h );
data = (rt_argb *) cairo_image_surface_get_data( csur );
rt_render_pipe_get_camera( &renderPipe, &cam );
rt_matrix4_create_projection( &(cam->viewToPersp), (float)w/(float)h,
0.125*M_PI );
rt_render_pipe_free_camera( &renderPipe, cam );
rt_render_pipe_set_image_size( &renderPipe, w, h );
}
void buildPlaneOfTriangles()
{
rt_vector3 v[4];
rt_color col[5];
wave wv[7];
int wavesCount = 10;
int i, j, k;
int pointsInRow = SQUARES_COUNT_SQRT+1;
rt_color_create( col, 0.5f, 0.223f, 0.345f, 0.474f ); //color
rt_color_create( col+1, 0.0f, 0.05f, 0.05f, 0.05f ); //ambient
rt_color_create( col+2, 0.0f, 0.5f, 0.5f, 0.5f ); //diffuse
rt_color_create( col+3, 3.5f, 0.25f, 0.25f, 0.25f ); //specular
rt_color_create( col+4, 0.0f, 0.3f, 0.3f, 0.3f ); //reflect
rt_material_create( mt, col, col+1, col+2,
col+3, col+4, 0.25, 1.33f );
#pragma GCC diagnostic ignored "-Wdiv-by-zero"
rt_vector3 e1, e2;
rt_float dt = plSz/SQUARES_COUNT_SQRT;
rt_float xPos = -plSz*0.5f, zPos = 50.0f-plSz*0.5f;
#pragma GCC diagnostic push
wv[0].period = 100.0f; wv[0].amplitude = 1.5f;
wv[0].angle = M_PI*0.5f; wv[0].speed = 500.0f;
wv[1].period = 79.16677f; wv[1].amplitude = 0.25f;
wv[1].angle = 2.9428474f; wv[1].speed = 256.9786f;
wv[2].period = 144.04169f; wv[2].amplitude = 0.25f;
wv[2].angle = -1.8729727f; wv[2].speed = 160.6270f;
wv[3].period = 79.15634f; wv[3].amplitude = 0.25f;
wv[3].angle = 1.8583484f; wv[3].speed = 239.9041f;
wv[4].period = 121.51403f; wv[4].amplitude = 0.5f;
wv[4].angle = -1.5343947f; wv[4].speed = 230.2120f;
wv[5].period = 72.40281f; wv[5].amplitude = 0.5f;
wv[5].angle = 1.4335813f; wv[5].speed = 300.9681f;
wv[6].period = 81.57271f; wv[6].amplitude = 0.5f;
wv[6].angle = -1.2057098f; wv[6].speed = 280.5781f;
//create vertex
for ( i = 0; i < pointsInRow; ++i )
for ( j = 0; j < pointsInRow; ++j )
{
float u = ((float) i) / pointsInRow;
float v = ((float) j) / pointsInRow;
float y = plHt;
for ( k = 0; k < wavesCount; ++k )
{
y += sin(wv[k].period
* (-cos(wv[k].angle)*u+sin(wv[k].angle)*v)
+ t*wv[k].speed) * wv[k].amplitude;
}
rt_vector3_create(
&(water.v[i*pointsInRow+j].pos),
xPos+i*dt, y,
zPos+j*dt );
rt_vector3_create(
&(water.v[i*pointsInRow+j].norm),
0.0f, -1.0f, 0.0f );
}
//compute indeces
k = 0;
for ( i = 0; i < SQUARES_COUNT_SQRT; ++i )
for ( j = 0; j < SQUARES_COUNT_SQRT; ++j )
{
water.t[k].pV0 = pointsInRow*i + j;
water.t[k].pV1 = pointsInRow*(i+1) + j;
water.t[k].pV2 = pointsInRow*i + (j+1);
water.t[k].mat = 0;
++k;
water.t[k].pV0 = pointsInRow*(i+1) + (j+1);
water.t[k].pV1 = pointsInRow*i + (j+1);
water.t[k].pV2 = pointsInRow*(i+1) + j;
water.t[k].mat = 0;
++k;
}
//compute normals
for ( i = 0; i < SQUARES_COUNT_SQRT*SQUARES_COUNT_SQRT*2; ++i )
{
int i0, i1, i2;
i0 = water.t[i].pV0;
i1 = water.t[i].pV1;
i2 = water.t[i].pV2;
rt_vector3_sub( &(water.v[i0].pos),
&(water.v[i1].pos), &e1 );
rt_vector3_sub( &(water.v[i0].pos),
&(water.v[i2].pos), &e2 );
rt_vector3_cross( &e1, &e2, v );
rt_vector3_add( &(water.v[i0].norm),
v, &(water.v[i0].norm) );
rt_vector3_add( &(water.v[i1].norm),
v, &(water.v[i1].norm) );
rt_vector3_add( &(water.v[i2].norm),
v, &(water.v[i2].norm) );
}
//normalize normals
for ( i = 0; i < pointsInRow*pointsInRow; ++i )
rt_vector3_normalize( &(water.v[i].norm),
&(water.v[i].norm) );
}
void buildSpheres()
{
rt_vector3 v[4];
rt_color col[5];
int i;
//create fisrt sphere
rt_color_create( col, 0.5f, 0.0f, 0.5f, 1.0f ); //color
rt_color_create( col+1, 0.0f, 0.05f, 0.05f, 0.05f ); //ambient
rt_color_create( col+2, 0.0f, 0.2f, 0.2f, 0.2f ); //diffuse
rt_color_create( col+3, 15.0f, 0.2f, 0.2f, 0.2f ); //specular
rt_color_create( col+4, 0.0f, 0.5f, 0.5f, 0.5f ); //reflect
rt_material_create( mt+1, col, col+1, col+2,
col+3, col+4, 0.0f, 1.33f );
rt_vector3_create( v, 0.0f, 0.0f, 50.0f );
rt_sphere_create( sp, v, 10.0f, 1 );
//create other spheres
for ( i = 1; i < SPHERE_COUNT; ++i )
{
rt_color_create( col, 1.0f,
(rt_float)(rand()%16365)/16384.0f,
(rt_float)(rand()%16365)/16384.0f,
(rt_float)(rand()%16365)/16384.0f );
rt_color_create( col+1, 0.0f, 0.05f, 0.05f, 0.05f ); //ambient
rt_color_create( col+2, 0.0f, 0.5f, 0.5f, 0.5f ); //diffuse
rt_color_create( col+3, 15.0f, 0.5f, 0.5f, 0.5f ); //specular
rt_color_create( col+4, 0.0f, 0.5f, 0.5f, 0.5f ); //reflect
rt_material_create( mt+1+i, col, col+1, col+2,
col+3, col+4, 0.0f, 1.0f );
rt_sphere_create( &sp[i], v, 3.66541f, 1+i );
koef[i] = (rt_float)(rand()%101);
}
}
void buildBottom()
{
rt_ulong vertexOffset = 0;
float bottomHeight = 25.0f;
float topHeight = 3.0f;
rt_color col[5];
rt_color_create( col, 1.0f, 1.0f, 1.0f, 1.0f ); //color
rt_color_create( col+1, 0.0f, 0.05f, 0.05f, 0.05f ); //ambient
rt_color_create( col+2, 0.0f, 1.0f, 1.0f, 1.0f ); //diffuse
rt_color_create( col+3, 5.0f, 1.0f, 1.0f, 1.0f ); //specular
rt_color_create( col+4, 0.0f, 0.0f, 0.0f, 0.0f ); //reflect
rt_material_create( mt + SPHERE_COUNT + 1, col, col+1, col+2,
col+3, col+4, 0.0, 1.0f );
rt_vector3_create( &(bottom.v[vertexOffset].pos),
plSz*0.5f, plHt - topHeight + bottomHeight, plSz*0.5f+50.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 1].pos),
plSz*0.5f, plHt - topHeight + bottomHeight, -plSz*0.5f+50.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 2].pos),
-plSz*0.5f, plHt - topHeight + bottomHeight, plSz*0.5f+50.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 3].pos),
-plSz*0.5f, plHt - topHeight + bottomHeight, -plSz*0.5f+50.0f );
rt_vector3_create( &(bottom.v[vertexOffset].norm),
0.0f, -1.0f, 0.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 1].norm),
0.0f, -1.0f, 0.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 2].norm),
0.0f, -1.0f, 0.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 3].norm),
0.0f, -1.0f, 0.0f );
bottom.t[0].pV0 = vertexOffset;
bottom.t[0].pV1 = vertexOffset + 1;
bottom.t[0].pV2 = vertexOffset + 2;
bottom.t[1].pV0 = vertexOffset + 3;
bottom.t[1].pV1 = vertexOffset + 1;
bottom.t[1].pV2 = vertexOffset + 2;
rt_vector3_create( &(bottom.v[vertexOffset + 4].pos),
-plSz*0.5f, plHt - topHeight + bottomHeight, plSz*0.5f+50.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 5].pos),
-plSz*0.5f, plHt - topHeight + bottomHeight, -plSz*0.5f+50.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 6].pos),
-plSz*0.5f, plHt - topHeight, plSz*0.5f+50.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 7].pos),
-plSz*0.5f, plHt - topHeight, -plSz*0.5f+50.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 4].norm),
1.0f, 0.0f, 0.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 5].norm),
1.0f, 0.0f, 0.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 6].norm),
1.0f, 0.0f, 0.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 7].norm),
1.0f, 0.0f, 0.0f );
bottom.t[2].pV0 = vertexOffset + 4;
bottom.t[2].pV1 = vertexOffset + 5;
bottom.t[2].pV2 = vertexOffset + 6;
bottom.t[3].pV0 = vertexOffset + 7;
bottom.t[3].pV1 = vertexOffset + 6;
bottom.t[3].pV2 = vertexOffset + 5;
rt_vector3_create( &(bottom.v[vertexOffset + 8].pos),
plSz*0.5f, plHt - topHeight + bottomHeight, plSz*0.5f+50.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 9].pos),
plSz*0.5f, plHt - topHeight + bottomHeight, -plSz*0.5f+50.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 10].pos),
plSz*0.5f, plHt - topHeight, plSz*0.5f+50.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 11].pos),
plSz*0.5f, plHt - topHeight, -plSz*0.5f+50.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 8].norm),
-1.0f, 0.0f, 0.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 9].norm),
-1.0f, 0.0f, 0.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 10].norm),
-1.0f, 0.0f, 0.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 11].norm),
-1.0f, 0.0f, 0.0f );
bottom.t[4].pV0 = vertexOffset + 8;
bottom.t[4].pV1 = vertexOffset + 9;
bottom.t[4].pV2 = vertexOffset + 10;
bottom.t[5].pV0 = vertexOffset + 11;
bottom.t[5].pV1 = vertexOffset + 10;
bottom.t[5].pV2 = vertexOffset + 9;
rt_vector3_create( &(bottom.v[vertexOffset + 12].pos),
-plSz*0.5f, plHt - topHeight + bottomHeight, plSz*0.5f+50.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 13].pos),
plSz*0.5f, plHt - topHeight + bottomHeight, plSz*0.5f+50.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 14].pos),
-plSz*0.5f, plHt - topHeight, plSz*0.5f+50.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 15].pos),
plSz*0.5f, plHt - topHeight, plSz*0.5f+50.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 12].norm),
0.0f, 0.0f, -1.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 13].norm),
0.0f, 0.0f, -1.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 14].norm),
0.0f, 0.0f, -1.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 15].norm),
0.0f, 0.0f, -1.0f );
bottom.t[6].pV0 = vertexOffset + 12;
bottom.t[6].pV1 = vertexOffset + 13;
bottom.t[6].pV2 = vertexOffset + 14;
bottom.t[7].pV0 = vertexOffset + 15;
bottom.t[7].pV1 = vertexOffset + 14;
bottom.t[7].pV2 = vertexOffset + 13;
rt_vector3_create( &(bottom.v[vertexOffset + 16].pos),
-plSz*0.5f, plHt - topHeight + bottomHeight, -plSz*0.5f+50.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 17].pos),
plSz*0.5f, plHt - topHeight + bottomHeight, -plSz*0.5f+50.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 18].pos),
-plSz*0.5f, plHt - topHeight, -plSz*0.5f+50.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 19].pos),
plSz*0.5f, plHt - topHeight, -plSz*0.5f+50.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 16].norm),
0.0f, 0.0f, 1.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 17].norm),
0.0f, 0.0f, 1.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 18].norm),
0.0f, 0.0f, 1.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 19].norm),
0.0f, 0.0f, 1.0f );
bottom.t[8].pV0 = vertexOffset + 16;
bottom.t[8].pV1 = vertexOffset + 17;
bottom.t[8].pV2 = vertexOffset + 18;
bottom.t[9].pV0 = vertexOffset + 19;
bottom.t[9].pV1 = vertexOffset + 18;
bottom.t[9].pV2 = vertexOffset + 17;
}
/*
void buildBottom()
{
rt_ulong vertexOffset = 0;
rt_color col[5];
float bottomHeight = -5.5f;
float bottomSize = 100.0f;
rt_color_create( col, 1.0f, 0.223f, 0.345f, 0.474f ); //color
rt_color_create( col+1, 0.0f, 0.0f, 0.0f, 0.0f ); //ambient
rt_color_create( col+2, 0.0f, 0.64f, 0.64f, 0.64f ); //diffuse
rt_color_create( col+3, 96.0f, 0.5f, 0.5f, 0.5f ); //specular
rt_color_create( col+4, 0.0f, 1.0f, 1.0f, 1.0f ); //reflect
rt_material_create( mt, col, col+1, col+2,
col+3, col+4, 0.0, 1.0f );
rt_vector3_create( &(bottom.v[vertexOffset].pos),
bottomSize*0.5f, bottomHeight, bottomSize*0.5f );
rt_vector3_create( &(bottom.v[vertexOffset + 1].pos),
bottomSize*0.5f, bottomHeight, -bottomSize*0.5f );
rt_vector3_create( &(bottom.v[vertexOffset + 2].pos),
-bottomSize*0.5f, bottomHeight, bottomSize*0.5f );
rt_vector3_create( &(bottom.v[vertexOffset + 3].pos),
-bottomSize*0.5f, bottomHeight, -bottomSize*0.5f );
rt_vector3_create( &(bottom.v[vertexOffset].norm),
0.0f, 1.0f, 0.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 1].norm),
0.0f, 1.0f, 0.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 2].norm),
0.0f, 1.0f, 0.0f );
rt_vector3_create( &(bottom.v[vertexOffset + 3].norm),
0.0f, 1.0f, 0.0f );
bottom.t[0].pV0 = vertexOffset;
bottom.t[0].pV1 = vertexOffset + 1;
bottom.t[0].pV2 = vertexOffset + 2;
bottom.t[1].pV0 = vertexOffset + 3;
bottom.t[1].pV1 = vertexOffset + 1;
bottom.t[1].pV2 = vertexOffset + 2;
}
*/
void draw( GtkWidget *wgt, cairo_t *cr, gpointer ud )
{
struct timespec curTime;
long long int c, p;
int i, s;
rt_float radius;
rt_vector3 center;
rt_argb *renderedImage;
//set timer for fps counter
clock_gettime( CLOCK_REALTIME, &curTime );
p = curTime.tv_sec*1000000000LL + curTime.tv_nsec;
s = curTime.tv_sec;
if ( SQUARES_COUNT_SQRT )
buildPlaneOfTriangles();
for ( i = 0; i < MATERIAL_COUNT; ++i )
rt_render_pipe_add_material( &renderPipe, mt+i, i );
//add light to render pipe
for ( i = 0; i < POINT_LIGHTS_COUNT; ++i )
rt_render_pipe_add_light( &renderPipe, (void *)(lt+i),
RT_LT_POINT );
//add primitives to render pipe
if ( SPHERE_COUNT > 0 )
{
sp[0].pos.y = centralSpherePosY;
rt_render_pipe_add_primitive( &renderPipe,
(void *)(&sp[0]), RT_PT_SPHERE );
}
//make the other spheres flying around the first one,
radius = 25.0f + 25.0f*(0.5f+sin(10.0f*t)*0.5f);
center.x = 0.0f;
center.y = 0.0f;
center.z = 50.0f;
for ( i = 1; i < SPHERE_COUNT; ++i )
{
sp[i].pos.x = center.x + sin( t + (rt_float)(i-1) * 2.0f
/ (rt_float)(SPHERE_COUNT-1) * M_PI) * radius;
sp[i].pos.y = center.y + koef[i]*0.5f*sin(koef[i]*0.5f*t);
sp[i].pos.z = center.z + cos( t + (rt_float)(i-1) * 2.0f
/ (rt_float)(SPHERE_COUNT-1) * M_PI) * radius;
//add sphere to render pipe
rt_render_pipe_add_primitive( &renderPipe, (void *)(&sp[i]),
RT_PT_SPHERE );
}
//add meshes
rt_mesh_add( &renderPipe, &bottom, SPHERE_COUNT + 1 );
// rt_render_pipe_add_material( &renderPipe, mt, 300 );
// rt_mesh_add( &renderPipe, &bottom, 300 );
rt_mesh_add( &renderPipe, &water, 0 );
// for ( i = 0; i < fluttershy.gc; ++i )
// rt_mesh_add_group( &renderPipe, &fluttershy, i, i );
//draw to buffer
renderedImage = rt_render_pipe_draw( &renderPipe );
memcpy( data, renderedImage, sizeof(rt_argb) * w * h );
//draw buffer to screen
if ( RENDER_TO_SCREEN )
{
cairo_set_source_surface(cr, csur, 0, 0 );
cairo_paint(cr);
}
//draw to files
if ( RENDER_TO_PNGS )
{
char tmp[255];
sprintf( tmp, "%s%lu.png", PNGS_PATH, pngN++ );
cairo_surface_write_to_png( csur, tmp );
}
//fps counter
clock_gettime( CLOCK_REALTIME, &curTime );
c = curTime.tv_sec*1000000000LL + curTime.tv_nsec - p;
avr += 1000000000.0/(double)(c);
++frm;
if ( curTime.tv_sec != s )
{
printf( "fps %f\n", (double) avr / (double) frm );
avr = 0.0;
frm = 0;
}
}
int main( int argc, char *argv[] )
{
int i;
time_t initialTime = time(0);
rt_camera frust;
rt_vector3 v[4];
rt_color col[5];
srand(time(0));
//create surface for render
w = WIDTH;
h = HEIGHT;
csur = cairo_image_surface_create( CAIRO_FORMAT_RGB24, w, h );
data = (rt_argb *) cairo_image_surface_get_data( csur );
//init rt
rt_init( *argv );
//initiliaze render pipe
rt_render_pipe_create( &renderPipe, w, h );
rt_camera_create( &frust, (float)w / (float)h, 0.125*M_PI );
rt_render_pipe_set_camera( &renderPipe, &frust );
//create dir for writing images, if it doesn't exists
if ( RENDER_TO_PNGS )
{
struct stat s = {0};
int save;
if ( stat( PNGS_PATH, &s ) == -1 )
mkdir( PNGS_PATH, S_IRWXU | S_IRWXG | S_IRWXO );
if ( (save = open( "save", O_RDONLY )) >= 0 )
{
read( save, (void *)(&t), sizeof(rt_float) );
read( save, (void *)(&dT), sizeof(rt_float) );
read( save, (void *)(&pngN), sizeof(unsigned long int) );
t += dT;
}
}
//initialize render pipe and create objects to draw
if ( rt_mesh_load_from_obj( &fluttershy, "flutter.obj" ) < 0 )
printf( "obj load error\n" );
else
printf( "model loaded: %lu verticies, %lu triangles\n",
fluttershy.vc, fluttershy.tc );
{
rt_camera *cam = NULL;
rt_render_pipe_get_camera( &renderPipe, &cam );
//set camera position
cam->world._11 = 1.0f; cam->world._12 = 0.0f;
cam->world._13 = 0.0f; cam->world._14 = 0.0f;
cam->world._21 = 0.0f; cam->world._22 = 0.968582f;
cam->world._23 = -0.248690f; cam->world._24 = 0.0f;
cam->world._31 = 0.0f; cam->world._32 = 0.248690f;
cam->world._33 = 0.968582f; cam->world._34 = 0.0f;
cam->world._41 = 0.0f; cam->world._42 = -12.0f;
cam->world._43 = -43.0f; cam->world._44 = 1.0f;
camPos.x = 0.0f; camPos.y = 12.0f; camPos.z = 43.0f;
rt_render_pipe_free_camera( &renderPipe, cam );
}
//create point light
rt_color_create( col, 1.0f, 1.0f, 1.0f, 1.0f );
rt_vector3_create( v, -20.0f, -35.0f, 0.0f );
for ( i = 0; i < POINT_LIGHTS_COUNT; ++i )
rt_point_light_create( lt+i, v, 100.0f, col );
// alloc memory for meshes
rt_mesh_reserve( &water, (SQUARES_COUNT_SQRT+1)*(SQUARES_COUNT_SQRT+1),
SQUARES_COUNT_SQRT*SQUARES_COUNT_SQRT*2, 1 );
// rt_mesh_reserve( &bottom, 4, 2, 1 );
// buildBottom();
rt_mesh_reserve( &bottom, 20, 10, 1 );
// make spheres
if ( SPHERE_COUNT )
buildSpheres();
// make bottom
if ( BOTTOM )
buildBottom();
//set timeout for render
rotTID = g_timeout_add( (guint)1, rotStep, NULL );
//create main window
gtk_init( &argc, &argv );
mainWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
drawArea = gtk_drawing_area_new();
gtk_widget_set_hexpand( drawArea, 1 );
gtk_widget_set_vexpand( drawArea, 1 );
gtk_container_add( GTK_CONTAINER(mainWindow), drawArea );
g_signal_connect( mainWindow, "destroy", G_CALLBACK(gtk_main_quit),
NULL );
g_signal_connect( G_OBJECT(drawArea), "draw", G_CALLBACK(draw), NULL );
if ( RENDER_TO_SCREEN )
{
g_signal_connect( G_OBJECT(drawArea), "size_allocate",
G_CALLBACK(resizeImage), NULL );
}
g_signal_connect( mainWindow, "key-press-event",
G_CALLBACK(keyPress), NULL );
g_signal_connect( mainWindow, "destroy",
G_CALLBACK(windowDestroy), NULL );
gtk_window_resize( GTK_WINDOW(mainWindow), w, h );
gtk_widget_show_all(mainWindow);
gtk_main();
initialTime = time(0) - initialTime;
printf( "total rendering time: %ldh, %ldm, %lds\n",
initialTime / 3600,
(initialTime % 3600) / 60,
(initialTime % 60) );
rt_render_pipe_cleanup( &renderPipe );
rt_mesh_release( &water );
rt_mesh_release( &bottom );
rt_mesh_release( &fluttershy );
return 0;
}