-
Notifications
You must be signed in to change notification settings - Fork 75
/
entity.c
1524 lines (1267 loc) · 43.4 KB
/
entity.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
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
/*
Copyright (C) 2010 Stephen M. Cameron
Author: Stephen M. Cameron
This file is part of Spacenerds In Space.
Spacenerds in Space is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Spacenerds in Space is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Spacenerds in Space; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* Need _GNU_SOURCE for qsort_r, must be defined before any include directives */
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
#include "mtwist.h"
#include "mathutils.h"
#include "matrix.h"
#include "vertex.h"
#include "triangle.h"
#include "quat.h"
#include "mesh.h"
#include "stl_parser.h"
#include "snis_graph.h"
#include "material.h"
#include "entity.h"
#include "mathutils.h"
#include "my_point.h"
#include "snis_font.h"
#include "snis_typeface.h"
#include "snis_alloc.h"
#include "entity_private.h"
#include "graph_dev.h"
static int clip_line(struct mat41* vtx0, struct mat41* vtx1);
struct entity *add_entity(struct entity_context *cx,
struct mesh *m, float x, float y, float z, int color)
{
int n;
static int throttle = 0;
#if ADD_ENTITY_CHAOS_MONKEY
/* for testing that code can withstand add_entity failures */
if (snis_randn(1000) < 50)
return NULL;
#endif
n = snis_object_pool_alloc_obj(cx->entity_pool);
if (n < 0) {
if (throttle < 10 || (throttle & (0x3f)) == 0) { /* Throttle these messages */
printf("Out of entities at %s:%d\n", __FILE__, __LINE__);
fflush(stdout);
}
throttle++;
return NULL;
} else {
if (throttle > 0)
throttle--;
}
cx->entity_list[n].visible = 1;
cx->entity_list[n].e_visible = 1;
cx->entity_list[n].m = m;
cx->entity_list[n].high_poly = m;
cx->entity_list[n].low_poly = m;
cx->entity_list[n].x = x;
cx->entity_list[n].y = y;
cx->entity_list[n].z = z;
vec3_init(&cx->entity_list[n].e_pos, x, y, z);
vec3_init(&cx->entity_list[n].scale, 1, 1, 1);
vec3_init(&cx->entity_list[n].e_scale, 1, 1, 1);
cx->entity_list[n].color = color;
cx->entity_list[n].render_style = RENDER_NORMAL;
cx->entity_list[n].user_data = NULL;
cx->entity_list[n].shadecolor = 0;
cx->entity_list[n].orientation = identity_quat;
cx->entity_list[n].e_orientation = identity_quat;
cx->entity_list[n].material_ptr = 0;
cx->entity_list[n].parent = 0;
cx->entity_list[n].entity_child_index = -1;
cx->entity_list[n].emit_intensity = 1.0;
cx->entity_list[n].sx = -1;
cx->entity_list[n].sy = -1;
cx->entity_list[n].onscreen = 0;
if (m && m->material)
update_entity_material(&cx->entity_list[n], m->material);
return &cx->entity_list[n];
}
static void remove_entity_children(struct entity_context *cx, struct entity *e)
{
int entity_child_index = e->entity_child_index;
while (entity_child_index >= 0) {
struct entity_child *this_ec = &cx->entity_child_list[entity_child_index];
struct entity *this_child = &cx->entity_list[this_ec->child_entity_index];
if (this_child->entity_child_index >= 0)
remove_entity_children(cx, this_child);
snis_object_pool_free_object(cx->entity_pool, this_ec->child_entity_index);
int next_entity_child_index = this_ec->next_entity_child_index;
snis_object_pool_free_object(cx->entity_child_pool, entity_child_index);
entity_child_index = next_entity_child_index;
}
e->entity_child_index = -1;
}
void remove_entity(struct entity_context *cx, struct entity *e)
{
int index;
if (!e)
return;
if (e->entity_child_index >= 0)
remove_entity_children(cx, e);
index = e - &cx->entity_list[0];
snis_object_pool_free_object(cx->entity_pool, index);
}
void remove_all_entity(struct entity_context *cx)
{
snis_object_pool_free_all_objects(cx->entity_pool);
snis_object_pool_free_all_objects(cx->entity_child_pool);
}
void update_entity_parent(struct entity_context *cx, struct entity *child, struct entity *parent)
{
if (child->parent == parent)
return;
int new_entity_child_index = -1;
int child_index = child - &cx->entity_list[0];
if (parent) {
/* preallocate this so that in case we can't get one, at least we don't crash. */
new_entity_child_index = snis_object_pool_alloc_obj(cx->entity_child_pool);
if (new_entity_child_index < 0) {
printf("entity_child_pool exhausted at %s:%d\n", __FILE__, __LINE__);
return;
}
}
if (child->parent) {
/* remove this child out of the old parent child_entity_list */
int entity_child_index = child->parent->entity_child_index;
struct entity_child *last_ec = 0;
while (entity_child_index >= 0) {
struct entity_child *this_ec = &cx->entity_child_list[entity_child_index];
if (this_ec->child_entity_index == child_index) {
/* we found the child, fix the list */
if (!last_ec) /* first link */
child->parent->entity_child_index = this_ec->next_entity_child_index;
else
last_ec->next_entity_child_index = this_ec->next_entity_child_index;
snis_object_pool_free_object(cx->entity_child_pool, entity_child_index);
break; /* found the child, done */
}
entity_child_index = this_ec->next_entity_child_index;
last_ec = this_ec;
}
}
child->parent = parent;
if (parent) {
/* add child into new parent child_entity_list */
struct entity_child *new_ec = &cx->entity_child_list[new_entity_child_index];
/* insert entity_child at the front of the list */
new_ec->child_entity_index = child_index;
new_ec->next_entity_child_index = parent->entity_child_index;
parent->entity_child_index = new_entity_child_index;
}
}
void update_entity_pos(struct entity *e, float x, float y, float z)
{
vec3_init(&e->e_pos, x, y, z);
}
void update_entity_orientation(struct entity *e, const union quat *orientation)
{
e->e_orientation = *orientation;
}
float entity_get_scale(struct entity *e)
{
return vec3_cwise_max(&e->e_scale);
}
void update_entity_scale(struct entity *e, float scale)
{
vec3_init(&e->e_scale, scale, scale, scale);
}
void entity_get_non_uniform_scale(struct entity *e, float *x_scale, float *y_scale, float *z_scale)
{
*x_scale = e->e_scale.v.x;
*y_scale = e->e_scale.v.y;
*z_scale = e->e_scale.v.z;
}
void update_entity_non_uniform_scale(struct entity *e, float x_scale, float y_scale, float z_scale)
{
vec3_init(&e->e_scale, x_scale, y_scale, z_scale);
}
void update_entity_color(struct entity *e, int color)
{
e->color = color;
}
void update_entity_shadecolor(struct entity *e, int color)
{
e->shadecolor = color;
}
void update_entity_visibility(struct entity *e, int visible)
{
e->e_visible = visible;
}
int entity_get_visibility(struct entity *e)
{
return e->e_visible;
}
void update_entity_material(struct entity *e, struct material *material_ptr)
{
e->material_ptr = material_ptr;
}
struct material *entity_get_material(struct entity *e)
{
return e->material_ptr;
}
static inline float wx_screen(struct entity_context *cx, float wx)
{
struct camera_info *c = &cx->camera;
return (wx * c->xvpixels / 2) + c->xvpixels / 2 + cx->window_offset_x;
}
static inline float wy_screen(struct entity_context *cx, float wy)
{
struct camera_info *c = &cx->camera;
return (-wy * c->yvpixels / 2) + c->yvpixels / 2 + cx->window_offset_y;
}
static void calculate_model_matrices(struct camera_info *c, struct frustum *f, struct entity *e,
struct entity_transform *transform)
{
transform->v = &f->v_matrix;
transform->vp = &f->vp_matrix;
/* Model = (T*R)*S
T - translation matrix
R - rotation matrix
S - scale matrix
Combine for post multiply of vector */
struct mat44d mat_t = {{
{ 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ e->x, e->y, e->z, 1 }}};
struct mat44d mat_s = {{
{ e->scale.v.x, 0, 0, 0 },
{ 0, e->scale.v.y, 0, 0 },
{ 0, 0, e->scale.v.z, 0 },
{ 0, 0, 0, 1 }}};
struct mat44d mat_r = {{
{ 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 } } };
if (e->material_ptr && e->material_ptr->billboard_type != MATERIAL_BILLBOARD_TYPE_NONE) {
switch (e->material_ptr->billboard_type) {
/* aligned so that +y axis = camera up and normal parallel with camera look direction */
case MATERIAL_BILLBOARD_TYPE_SCREEN:
{
/* take the corner 3x3 from the view matrix, transpose, and pad back to 4x4 */
struct mat33d tm1, tm2;
mat44_to_mat33_dd(transform->v, &tm1);
mat33_transpose_dd(&tm1, &tm2);
mat33_to_mat44_dd(&tm2, &mat_r);
}
break;
/* aligned so that +y axis = camera up and normal is pointing to camera position */
case MATERIAL_BILLBOARD_TYPE_SPHERICAL:
{
union vec3 cam_up = { { c->ux, c->uy, c->uz } };
union vec3 look = { { c->x - e->x, c->y - e->y, c->z - e->z } };
vec3_normalize_self(&look);
union vec3 right;
vec3_cross(&right, &cam_up, &look);
vec3_normalize_self(&right);
union vec3 up;
vec3_cross(&up, &look, &right);
vec3_normalize_self(&up);
if (e->material_ptr->rotate_randomly) {
/* Rotate randomly about normal. */
union quat rotation;
float angle = (snis_randn(1000) % 360) * M_PI / 180.0;
quat_init_axis(&rotation, look.v.x, look.v.y, look.v.z, angle);
quat_rot_vec_self(&up, &rotation);
quat_rot_vec_self(&right, &rotation);
}
/* a rotation matrix to align with up, right, and look unit vectors
r.x r.y r.z 0
u.x u.y u.z 0
l.x l.y l.z 0
0 0 0 1 */
mat_r.m[0][0] = right.v.x;
mat_r.m[0][1] = right.v.y;
mat_r.m[0][2] = right.v.z;
mat_r.m[0][3] = 0;
mat_r.m[1][0] = up.v.x;
mat_r.m[1][1] = up.v.y;
mat_r.m[1][2] = up.v.z;
mat_r.m[1][3] = 0;
mat_r.m[2][0] = look.v.x;
mat_r.m[2][1] = look.v.y;
mat_r.m[2][2] = look.v.z;
mat_r.m[2][3] = 0;
mat_r.m[3][0] = 0;
mat_r.m[3][1] = 0;
mat_r.m[3][2] = 0;
mat_r.m[3][3] = 1;
}
break;
/* aligned so that +y axis = quaternion axis and normal is pointing in direction of camera position */
case MATERIAL_BILLBOARD_TYPE_AXIS:
{
union vec3 look_to_cam = { { c->x - e->x, c->y - e->y, c->z - e->z } };
vec3_normalize_self(&look_to_cam);
union vec3 up = { { 1, 0, 0 } };
quat_rot_vec_self(&up, &e->orientation);
vec3_normalize_self(&up);
union vec3 right;
vec3_cross(&right, &up, &look_to_cam);
vec3_normalize_self(&right);
union vec3 look;
vec3_cross(&look, &right, &up);
vec3_normalize_self(&look);
/* a rotation matrix to align with up, right, and look unit vectors
r.x r.y r.z 0
u.x u.y u.z 0
l.x l.y l.z 0
0 0 0 1 */
struct mat44d mat_r_y;
mat_r_y.m[0][0] = right.v.x;
mat_r_y.m[0][1] = right.v.y;
mat_r_y.m[0][2] = right.v.z;
mat_r_y.m[0][3] = 0;
mat_r_y.m[1][0] = up.v.x;
mat_r_y.m[1][1] = up.v.y;
mat_r_y.m[1][2] = up.v.z;
mat_r_y.m[1][3] = 0;
mat_r_y.m[2][0] = look.v.x;
mat_r_y.m[2][1] = look.v.y;
mat_r_y.m[2][2] = look.v.z;
mat_r_y.m[2][3] = 0;
mat_r_y.m[3][0] = 0;
mat_r_y.m[3][1] = 0;
mat_r_y.m[3][2] = 0;
mat_r_y.m[3][3] = 1;
/* rotate the model by 90 degrees so +x is up before applying
the billboarding matrix */
struct mat44d mat_y_to_x = { {
{ 0, 1, 0, 0 },
{ -1, 0, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 } } };
mat44_product_ddd(&mat_r_y, &mat_y_to_x, &mat_r);
}
break;
}
} else {
/* normal quaternion based rotation */
quat_to_rh_rot_matrix_fd(&e->orientation, &mat_r.m[0][0]);
}
mat44_product_ddd(&mat_t, &mat_r, &transform->m_no_scale);
mat44_product_ddd(&transform->m_no_scale, &mat_s, &transform->m);
/* calculate the final model-view-proj and model-view matrices */
mat44_product_ddf(transform->vp, &transform->m, &transform->mvp);
mat44_product_ddf(transform->v, &transform->m, &transform->mv);
/* normal transform is the inverse transpose of the upper left 3x3 of the model-view matrix */
struct mat33 mat_tmp33;
mat33_inverse_transpose_ff(mat44_to_mat33_ff(&transform->mv, &mat_tmp33), &transform->normal);
}
int transform_vertices(const struct mat44 *matrix, struct vertex *v, int len)
{
int total_clip_flag = 0x3f;
int i;
for (i = 0; i < len; i++) {
/* Set homogeneous coord to 1 */
v[i].w = 1.0;
/* Do the transformation... */
struct mat41 *m1 = (struct mat41 *) &v[i].x;
struct mat41 *m2 = (struct mat41 *) &v[i].wx;
mat44_x_mat41(matrix, m1, m2);
/* check all 6 clip planes for this vertex */
v[i].clip = 0;
v[i].clip |= (v[i].wx < -v[i].ww) ? (1<<0) : 0;
v[i].clip |= (v[i].wx > v[i].ww) ? (1<<1) : 0;
v[i].clip |= (v[i].wy < -v[i].ww) ? (1<<2) : 0;
v[i].clip |= (v[i].wy > v[i].ww) ? (1<<3) : 0;
v[i].clip |= (v[i].wz < -v[i].ww) ? (1<<4) : 0;
v[i].clip |= (v[i].wz > v[i].ww) ? (1<<5) : 0;
/* total is the intersection of all the vertices */
total_clip_flag &= v[i].clip;
}
/* if all vertices were clipped by the same plane then this set is all out */
return total_clip_flag > 0;
}
int transform_line(struct entity_context *cx, float x1, float y1, float z1, float x2, float y2, float z2,
float *sx1, float *sy1, float *sx2, float *sy2)
{
struct vertex v[2] = {
VERTEX_INIT(x1, y1, z1, 1),
VERTEX_INIT(x2, y2, z2, 1) };
/* there is no model transform on a point */
struct mat44 mat_vp;
mat44_convert_df(&cx->camera.frustum.vp_matrix, &mat_vp);
if (transform_vertices(&mat_vp, &v[0], 2)) {
/* both ends outside frustum */
*sx1 = -1;
*sy1 = -1;
*sx2 = -1;
*sy2 = -1;
return 1;
}
if (v[0].clip || v[1].clip) {
if (clip_line((struct mat41 *) &v[0].wx, (struct mat41 *) &v[1].wx)) {
/* both ends clipped */
return 1;
}
}
/* perspective divide */
v[0].wx /= v[0].ww;
v[0].wy /= v[0].ww;
v[1].wx /= v[1].ww;
v[1].wy /= v[1].ww;
/* convert to screen coordinates */
*sx1 = wx_screen(cx, v[0].wx);
*sy1 = wy_screen(cx, v[0].wy);
*sx2 = wx_screen(cx, v[1].wx);
*sy2 = wy_screen(cx, v[1].wy);
return 0;
}
static int transform_point_in_frustum(struct entity_context *cx, struct frustum *f,
float x, float y, float z, float *sx, float *sy)
{
struct vertex v = VERTEX_INIT(x, y, z, 1);
/* there is no model transform on a point */
struct mat44 mat_vp;
mat44_convert_df(&f->vp_matrix, &mat_vp);
if (transform_vertices(&mat_vp, &v, 1)) {
*sx = -1;
*sy = -1;
return 1;
}
v.wx /= v.ww;
v.wy /= v.ww;
*sx = wx_screen(cx, v.wx);
*sy = wy_screen(cx, v.wy);
return 0;
}
int transform_point(struct entity_context *cx, float x, float y, float z, float *sx, float *sy)
{
return transform_point_in_frustum(cx, &cx->camera.frustum, x, y, z, sx, sy);
}
static void render_entity(struct entity_context *cx, struct frustum *f, struct entity *e, union vec3 *camera_light_pos)
{
/* calculate screen coords of entity as a whole */
transform_point_in_frustum(cx, f, e->x, e->y, e->z, &e->sx, &e->sy);
if (e->sx >=0 && e->sy >= 0)
e->onscreen = 1;
/* Do not render entities with scale of zero. Doing so will lead to infinities
* in the model normal matrix, besides being pointless.
*/
if (e->scale.v.x == 0 && e->scale.v.y == 0 && e->scale.v.z == 0) {
e->onscreen = 0;
return;
}
struct entity_transform transform;
calculate_model_matrices(&cx->camera, f, e, &transform);
graph_dev_draw_entity(cx, e, camera_light_pos, &transform);
}
void render_line(struct entity_context *cx, float x1, float y1, float z1, float x2, float y2, float z2)
{
calculate_camera_transform(cx);
struct mat44 mat_vp;
mat44_convert_df(&cx->camera.frustum.vp_matrix, &mat_vp);
graph_dev_draw_3d_line(cx, &mat_vp, x1, y1, z1, x2, y2, z2);
}
void render_skybox(struct entity_context *cx)
{
graph_dev_draw_skybox(&cx->camera.frustum.vp_matrix_no_translate);
}
#if defined(__APPLE__) || defined(__FreeBSD__)
static int object_depth_compare_greater(void *vcx, const void *a, const void *b)
#else
static int object_depth_compare_greater(const void *a, const void *b, void *vcx)
#endif
{
struct entity_context *cx = vcx;
struct entity *A = &cx->entity_list[*(const int *) a];
struct entity *B = &cx->entity_list[*(const int *) b];
if (A->dist3dsqrd < B->dist3dsqrd)
return 1;
if (A->dist3dsqrd > B->dist3dsqrd)
return -1;
return 0;
}
#if defined(__APPLE__) || defined(__FreeBSD__)
static int object_depth_compare_less(void *vcx, const void *a, const void *b)
#else
static int object_depth_compare_less(const void *a, const void *b, void *vcx)
#endif
{
struct entity_context *cx = vcx;
struct entity *A = &cx->entity_list[*(const int *) a];
struct entity *B = &cx->entity_list[*(const int *) b];
if (A->dist3dsqrd > B->dist3dsqrd)
return 1;
if (A->dist3dsqrd < B->dist3dsqrd)
return -1;
return 0;
}
static void sort_entity_distances(struct entity_context *cx)
{
#if defined(__APPLE__) || defined(__FreeBSD__)
qsort_r(cx->far_to_near_entity_depth, cx->nfar_to_near_entity_depth, sizeof(cx->far_to_near_entity_depth[0]),
cx, object_depth_compare_greater);
qsort_r(cx->near_to_far_entity_depth, cx->nnear_to_far_entity_depth, sizeof(cx->near_to_far_entity_depth[0]),
cx, object_depth_compare_less);
#else
qsort_r(cx->far_to_near_entity_depth, cx->nfar_to_near_entity_depth, sizeof(cx->far_to_near_entity_depth[0]),
object_depth_compare_greater, cx);
qsort_r(cx->near_to_far_entity_depth, cx->nnear_to_far_entity_depth, sizeof(cx->near_to_far_entity_depth[0]),
object_depth_compare_less, cx);
#endif
}
/* from http://www.crownandcutlass.com/features/technicaldetails/frustum.html
This page and its contents are Copyright 2000 by Mark Morley
Unless otherwise noted, you may use any and all code examples provided herein in any way you want. */
int sphere_in_frustum(struct frustum *f, float x, float y, float z, float radius)
{
int p;
for (p = 0; p < 6; p++)
if (f->planes[p][0] * x + f->planes[p][1] * y + f->planes[p][2] * z + f->planes[p][3] <= -radius)
return 0;
return 1;
}
/* from http://www.crownandcutlass.com/features/technicaldetails/frustum.html
This page and its contents are Copyright 2000 by Mark Morley
Unless otherwise noted, you may use any and all code examples provided herein in any way you want. */
static void extract_frustum_planes(struct frustum *f)
{
const double *clip = &f->vp_matrix.m[0][0];
float t;
/* Extract the numbers for the RIGHT plane */
f->planes[0][0] = clip[3] - clip[0];
f->planes[0][1] = clip[7] - clip[4];
f->planes[0][2] = clip[11] - clip[8];
f->planes[0][3] = clip[15] - clip[12];
/* Normalize the result */
t = dist3d(f->planes[0][0], f->planes[0][1], f->planes[0][2]);
f->planes[0][0] /= t;
f->planes[0][1] /= t;
f->planes[0][2] /= t;
f->planes[0][3] /= t;
/* Extract the numbers for the LEFT plane */
f->planes[1][0] = clip[3] + clip[0];
f->planes[1][1] = clip[7] + clip[4];
f->planes[1][2] = clip[11] + clip[8];
f->planes[1][3] = clip[15] + clip[12];
/* Normalize the result */
t = dist3d(f->planes[1][0], f->planes[1][1], f->planes[1][2]);
f->planes[1][0] /= t;
f->planes[1][1] /= t;
f->planes[1][2] /= t;
f->planes[1][3] /= t;
/* Extract the BOTTOM plane */
f->planes[2][0] = clip[3] + clip[1];
f->planes[2][1] = clip[7] + clip[5];
f->planes[2][2] = clip[11] + clip[9];
f->planes[2][3] = clip[15] + clip[13];
/* Normalize the result */
t = dist3d(f->planes[2][0], f->planes[2][1], f->planes[2][2]);
f->planes[2][0] /= t;
f->planes[2][1] /= t;
f->planes[2][2] /= t;
f->planes[2][3] /= t;
/* Extract the TOP plane */
f->planes[3][0] = clip[3] - clip[1];
f->planes[3][1] = clip[7] - clip[5];
f->planes[3][2] = clip[11] - clip[9];
f->planes[3][3] = clip[15] - clip[13];
/* Normalize the result */
t = dist3d(f->planes[3][0], f->planes[3][1], f->planes[3][2]);
f->planes[3][0] /= t;
f->planes[3][1] /= t;
f->planes[3][2] /= t;
f->planes[3][3] /= t;
/* Extract the FAR plane */
f->planes[4][0] = clip[3] - clip[2];
f->planes[4][1] = clip[7] - clip[6];
f->planes[4][2] = clip[11] - clip[10];
f->planes[4][3] = clip[15] - clip[14];
/* Normalize the result */
t = dist3d(f->planes[4][0], f->planes[4][1], f->planes[4][2]);
f->planes[4][0] /= t;
f->planes[4][1] /= t;
f->planes[4][2] /= t;
f->planes[4][3] /= t;
/* Extract the NEAR plane */
f->planes[5][0] = clip[3] + clip[2];
f->planes[5][1] = clip[7] + clip[6];
f->planes[5][2] = clip[11] + clip[10];
f->planes[5][3] = clip[15] + clip[14];
/* Normalize the result */
t = dist3d(f->planes[5][0], f->planes[5][1], f->planes[5][2]);
f->planes[5][0] /= t;
f->planes[5][1] /= t;
f->planes[5][2] /= t;
f->planes[5][3] /= t;
}
static void calculate_camera_transform_near_far(struct camera_info *c, struct frustum *f, float near, float far)
{
f->near = near;
f->far = far;
/* based on gluPerspective to find the right, left, top, and bottom */
float scale = tan(c->angle_of_view * 0.5) * f->near;
f->right = ((float) c->xvpixels / (float) c->yvpixels * scale);
f->left = -f->right;
f->top = scale;
f->bottom = -f->top;
struct mat41 *v; /* camera relative y axis (up/down) */
struct mat41 *n; /* camera relative z axis (into view plane) */
struct mat41 *u; /* camera relative x axis (left/right) */
struct mat41 up;
up.m[0] = c->ux;
up.m[1] = c->uy;
up.m[2] = c->uz;
up.m[3] = 1;
/* Translate to camera position... */
struct mat44d cameratrans_transform = { { { 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ -c->x, -c->y, -c->z, 1} } };
/* Calculate camera rotation based on look direction ...
http://ksimek.github.io/2012/08/22/extrinsic/ 'The "Look-At" Camera'
p = look at point, C = camera, u = up
L = p - C
s = L x u
u' = s x L
camera rotation = | s1 s2 s3 |
| u'1 u'2 u'3 |
| -L1 -L2 -L3 |
snis n = L
snis u = s
snis v = u' */
struct mat41 look_direction;
look_direction.m[0] = (c->lx - c->x);
look_direction.m[1] = (c->ly - c->y);
look_direction.m[2] = (c->lz - c->z);
look_direction.m[3] = 1.0;
normalize_vector(&look_direction, &look_direction);
n = &look_direction;
/* Calculate x direction relative to camera, "camera_x" */
struct mat41 camera_x;
mat41_cross_mat41(&look_direction, &up, &camera_x);
normalize_vector(&camera_x, &camera_x);
u = &camera_x;
/* Calculate camera relative x axis */
struct mat41 x_cross_look;
mat41_cross_mat41(&camera_x, &look_direction, &x_cross_look);
/* v should not need normalizing as n and u are already
* unit, and are perpendicular */
normalize_vector(&x_cross_look, &x_cross_look);
v = &x_cross_look;
/* Make a rotation matrix...
| ux uy uz 0 |
| vx vy vz 0 |
| -nx -ny -nz 0 |
| 0 0 0 1 |
*/
struct mat44d cameralook_transform;
cameralook_transform.m[0][0] = u->m[0];
cameralook_transform.m[0][1] = v->m[0];
cameralook_transform.m[0][2] = -n->m[0];
cameralook_transform.m[0][3] = 0.0;
cameralook_transform.m[1][0] = u->m[1];
cameralook_transform.m[1][1] = v->m[1];
cameralook_transform.m[1][2] = -n->m[1];
cameralook_transform.m[1][3] = 0.0;
cameralook_transform.m[2][0] = u->m[2];
cameralook_transform.m[2][1] = v->m[2];
cameralook_transform.m[2][2] = -n->m[2];
cameralook_transform.m[2][3] = 0.0;
cameralook_transform.m[3][0] = 0.0;
cameralook_transform.m[3][1] = 0.0;
cameralook_transform.m[3][2] = 0.0;
cameralook_transform.m[3][3] = 1.0;
/* Make perspective transform based on OpenGL frustum
http://www.scratchapixel.com/lessons/3d-advanced-lessons/perspective-and-orthographic-projection-matrix/opengl-perspective-projection-matrix/
*/
struct mat44d perspective_transform;
perspective_transform.m[0][0] = (2 * f->near) / (f->right - f->left);
perspective_transform.m[0][1] = 0.0;
perspective_transform.m[0][2] = 0.0;
perspective_transform.m[0][3] = 0.0;
perspective_transform.m[1][0] = 0.0;
perspective_transform.m[1][1] = (2.0 * f->near) / (f->top - f->bottom);
perspective_transform.m[1][2] = 0.0;
perspective_transform.m[1][3] = 0.0;
perspective_transform.m[2][0] = (f->right + f->left) / (f->right - f->left);
perspective_transform.m[2][1] = (f->top + f->bottom) / (f->top - f->bottom);
perspective_transform.m[2][2] = -(f->far + f->near) / (f->far - f->near);
perspective_transform.m[2][3] = -1.0;
perspective_transform.m[3][0] = 0.0;
perspective_transform.m[3][1] = 0.0;
perspective_transform.m[3][2] = (-2 * f->far * f->near) / (f->far - f->near);
perspective_transform.m[3][3] = 0.0;
/* save perspective */
f->p_matrix = perspective_transform;
/* make the view matrix, V = L * T */
mat44_product_ddd(&cameralook_transform, &cameratrans_transform, &f->v_matrix);
/* make the view-perspective matrix, VP = P * V */
mat44_product_ddd(&perspective_transform, &f->v_matrix, &f->vp_matrix);
/* save vp matrix without any camera translation */
mat44_product_ddf(&perspective_transform, &cameralook_transform, &f->vp_matrix_no_translate);
/* pull out the frustum planes for entity culling */
extract_frustum_planes(f);
}
void calculate_camera_transform(struct entity_context *cx)
{
/* calculate for the entire frustum */
calculate_camera_transform_near_far(&cx->camera, &cx->camera.frustum, cx->camera.near, cx->camera.far);
}
static void reposition_fake_star(struct entity_context *cx, struct vertex *fs, float radius);
static void update_entity_child_state(struct entity *e)
{
int visible = e->e_visible;
union vec3 pos = e->e_pos;
union vec3 scale = e->e_scale;
union quat orientation = e->e_orientation;
struct entity *parent = e->parent;
while (parent) {
visible = visible && parent->e_visible;
quat_rot_vec_self(&pos, &parent->e_orientation);
vec3_add_self(&pos, &parent->e_pos);
vec3_cwise_product_self(&scale, &parent->e_scale);
quat_mul_self_right(&parent->e_orientation, &orientation);
parent = parent->parent;
}
e->visible = visible;
e->x = pos.v.x;
e->y = pos.v.y;
e->z = pos.v.z;
e->scale = scale;
e->orientation = orientation;
}
void render_entities(struct entity_context *cx)
{
int i, j, k, n;
struct camera_info *c = &cx->camera;
sng_set_3d_viewport(cx->window_offset_x, cx->window_offset_y, c->xvpixels, c->yvpixels);
calculate_camera_transform(cx);
/* see if the fake stars have wandered outside of our immediate area */
if (cx->nfakestars > 0) {
float r2 = cx->fakestars_radius * cx->fakestars_radius;
for (i = 0; i < cx->nfakestars; i++) {
struct vertex *fs = &cx->fake_stars_mesh->v[i];
float dist2 = dist3dsqrd(c->x - fs->x, c->y - fs->y, c->z - fs->z);
if (dist2 > r2)
reposition_fake_star(cx, fs, cx->fakestars_radius);
}
mesh_graph_dev_init(cx->fake_stars_mesh);
}
/* For better depth buffer precision do the draw in multiple (two) passes based on the
* dynamic range of near/far, and clear the depth buffer between passes. A good rule of
* thumb is to have far / near < 10000 on 24-bit depth buffer. At the boundary between
* passes a slight seam can become visible. To avoid this visible seam we should try to
* avoid having the boundary between passes intersect any large objects. To this end we
* have 3 reasonable candidate boundary distances and choose the boundary distance which
* intersects the fewest large objects.
*/
float boundary_candidate[3] = { /* distance candidates for the boundary between passes */
cx->camera.near * 4000.0,
cx->camera.near * 7000.0,
cx->camera.near * 10000.0,
};
int boundary_intersects[3] = { 0, 0, 0 }; /* Count of objects intersecting each boundary distance plane */
int last_candidate, candidate = 0;
float render_pass_boundary = boundary_candidate[0];
union vec3 camera_pos, camera_look, camera_to_entity;
int n_passes;
struct frustum rendering_pass[2];
camera_pos.v.x = cx->camera.x;
camera_pos.v.y = cx->camera.y;
camera_pos.v.z = cx->camera.z;
camera_look.v.x = cx->camera.lx - cx->camera.x;
camera_look.v.y = cx->camera.ly - cx->camera.y;
camera_look.v.z = cx->camera.lz - cx->camera.z;
vec3_normalize_self(&camera_look);
if (cx->camera.far / cx->camera.near < render_pass_boundary) {
n_passes = 1;
rendering_pass[0] = c->frustum;
} else {
n_passes = 2;
calculate_camera_transform_near_far(&cx->camera, &rendering_pass[0],
render_pass_boundary, cx->camera.far);
calculate_camera_transform_near_far(&cx->camera, &rendering_pass[1],
cx->camera.near, render_pass_boundary + render_pass_boundary / 1000.0);
/* the additional 0.1% is to render a little farther to cover seam */
}
int pass;
for (pass = 0; pass < n_passes; pass++) {
struct frustum *f = &rendering_pass[pass];
/* find all entities in view frustum and sort them by distance */
n = snis_object_pool_highest_object(cx->entity_pool);
cx->nnear_to_far_entity_depth = 0;
cx->nfar_to_near_entity_depth = 0;
if (pass == 0) {
boundary_intersects[0] = 0;
boundary_intersects[1] = 0;
boundary_intersects[2] = 0;
}
for (j = 0; j <= n; j++) {
if (!snis_object_pool_is_allocated(cx->entity_pool, j))
continue;
struct entity *e = &cx->entity_list[j];
if (e->m == NULL)
continue;
/* clear on the first pass and accumulate the state */
if (pass == 0) {
update_entity_child_state(e);
e->onscreen = 0;
}
if (!e->visible)
continue;
float max_scale = vec3_cwise_max(&e->scale);
if (!sphere_in_frustum(f, e->x, e->y, e->z, e->m->radius * max_scale))
continue;
e->dist3dsqrd = dist3dsqrd(c->x - e->x, c->y - e->y, c->z - e->z);
/* See: http://stackoverflow.com/questions/3717226/radius-of-projected-sphere */
float approx_pixel_size = c->yvpixels * e->m->radius * max_scale /
tan(cx->camera.angle_of_view * 0.5) / sqrt(e->dist3dsqrd);
/* only cull stuff entirely that is too small on flat shader */
if (c->renderer & FLATSHADING_RENDERER && e->dist3dsqrd != 0 && approx_pixel_size < 2.0)