-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsgd.cc
1321 lines (1029 loc) · 34.8 KB
/
sgd.cc
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
/*
PLIB - A Suite of Portable Game Libraries
Copyright (C) 2001 Steve Baker
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For further information visit http://plib.sourceforge.net
*/
#include "sg.h"
void sgdVectorProductVec3 ( sgdVec3 dst, const sgdVec3 a, const sgdVec3 b )
{
dst[0] = a[1] * b[2] - a[2] * b[1] ;
dst[1] = a[2] * b[0] - a[0] * b[2] ;
dst[2] = a[0] * b[1] - a[1] * b[0] ;
}
inline SGDfloat _sgdClampToUnity ( const SGDfloat x )
{
if ( x > SGD_ONE ) return SGD_ONE ;
if ( x < -SGD_ONE ) return -SGD_ONE ;
return x ;
}
int sgdCompare3DSqdDist( const sgdVec3 v1, const sgdVec3 v2, const SGDfloat sqd_dist )
{
sgdVec3 tmp ;
sgdSubVec3 ( tmp, v2, v1 ) ;
SGDfloat sqdist = tmp[0] * tmp[0] + tmp[1] * tmp[1] + tmp[2] * tmp[2] ;
if ( sqdist > sqd_dist ) return 1 ;
if ( sqdist < sqd_dist ) return -1 ;
return 0 ;
}
void sgdMakeRotMat4( sgdMat4 mat, const SGDfloat angle, const sgdVec3 axis )
{
sgdVec3 ax ;
sgdNormalizeVec3 ( ax, axis ) ;
SGDfloat temp_angle = angle * SGD_DEGREES_TO_RADIANS ;
SGDfloat s = sin ( temp_angle ) ;
SGDfloat c = cos ( temp_angle ) ;
SGDfloat t = SGD_ONE - c ;
mat[0][0] = t * ax[0] * ax[0] + c ;
mat[0][1] = t * ax[0] * ax[1] + s * ax[2] ;
mat[0][2] = t * ax[0] * ax[2] - s * ax[1] ;
mat[0][3] = SGD_ZERO ;
mat[1][0] = t * ax[1] * ax[0] - s * ax[2] ;
mat[1][1] = t * ax[1] * ax[1] + c ;
mat[1][2] = t * ax[1] * ax[2] + s * ax[0] ;
mat[1][3] = SGD_ZERO ;
mat[2][0] = t * ax[2] * ax[0] + s * ax[1] ;
mat[2][1] = t * ax[2] * ax[1] - s * ax[0] ;
mat[2][2] = t * ax[2] * ax[2] + c ;
mat[2][3] = SGD_ZERO ;
mat[3][0] = SGD_ZERO ;
mat[3][1] = SGD_ZERO ;
mat[3][2] = SGD_ZERO ;
mat[3][3] = SGD_ONE ;
}
/*********************\
* sgdBox routines *
\*********************/
void sgdBox::extend ( const sgdVec3 v )
{
if ( isEmpty () )
{
sgdCopyVec3 ( min, v ) ;
sgdCopyVec3 ( max, v ) ;
}
else
{
if ( v[0] < min[0] ) min[0] = v[0] ;
if ( v[1] < min[1] ) min[1] = v[1] ;
if ( v[2] < min[2] ) min[2] = v[2] ;
if ( v[0] > max[0] ) max[0] = v[0] ;
if ( v[1] > max[1] ) max[1] = v[1] ;
if ( v[2] > max[2] ) max[2] = v[2] ;
}
}
void sgdBox::extend ( const sgdBox *b )
{
if ( b -> isEmpty () )
return ;
if ( isEmpty () )
{
sgdCopyVec3 ( min, b->getMin() ) ;
sgdCopyVec3 ( max, b->getMax() ) ;
}
else
{
extend ( b->getMin() ) ;
extend ( b->getMax() ) ;
}
}
void sgdBox::extend ( const sgdSphere *s )
{
if ( s -> isEmpty () )
return ;
/*
In essence, this extends around a box around the sphere - which
is still a perfect solution because both boxes are axially aligned.
*/
sgdVec3 x ;
sgdSetVec3 ( x, s->getCenter()[0]+s->getRadius(),
s->getCenter()[1]+s->getRadius(),
s->getCenter()[2]+s->getRadius() ) ;
extend ( x ) ;
sgdSetVec3 ( x, s->getCenter()[0]-s->getRadius(),
s->getCenter()[1]-s->getRadius(),
s->getCenter()[2]-s->getRadius() ) ;
extend ( x ) ;
}
int sgdBox::intersects ( const sgdVec4 plane ) const
{
/*
Save multiplies by not redoing Ax+By+Cz+D for each point.
*/
SGDfloat Ax_min = plane[0] * min[0] ;
SGDfloat By_min = plane[1] * min[1] ;
SGDfloat Cz_min_plus_D = plane[2] * min[2] + plane[3] ;
SGDfloat Ax_max = plane[0] * max[0] ;
SGDfloat By_max = plane[1] * max[1] ;
SGDfloat Cz_max_plus_D = plane[2] * max[2] + plane[3] ;
/*
Count the number of vertices on the positive side of the plane.
*/
int count = ( Ax_min + By_min + Cz_min_plus_D > SGD_ZERO ) +
( Ax_min + By_min + Cz_max_plus_D > SGD_ZERO ) +
( Ax_min + By_max + Cz_min_plus_D > SGD_ZERO ) +
( Ax_min + By_max + Cz_max_plus_D > SGD_ZERO ) +
( Ax_max + By_min + Cz_min_plus_D > SGD_ZERO ) +
( Ax_max + By_min + Cz_max_plus_D > SGD_ZERO ) +
( Ax_max + By_max + Cz_min_plus_D > SGD_ZERO ) +
( Ax_max + By_max + Cz_max_plus_D > SGD_ZERO ) ;
/*
The plane intersects the box unless all 8 are positive
or none of them are positive.
*/
return count != 0 && count != 8 ;
}
/**********************\
* sgdSphere routines *
\**********************/
void sgdSphere::extend ( const sgdVec3 v )
{
if ( isEmpty () )
{
sgdCopyVec3 ( center, v ) ;
radius = SGD_ZERO ;
return ;
}
SGDfloat d = sgdDistanceVec3 ( center, v ) ;
if ( d <= radius ) /* Point is already inside sphere */
return ;
SGDfloat new_radius = (radius + d) / SGD_TWO ; /* Grow radius */
SGDfloat ratio = (new_radius - radius) / d ;
center[0] += (v[0]-center[0]) * ratio ; /* Move center */
center[1] += (v[1]-center[1]) * ratio ;
center[2] += (v[2]-center[2]) * ratio ;
radius = new_radius ;
}
void sgdSphere::extend ( const sgdBox *b )
{
if ( b -> isEmpty () )
return ;
if ( isEmpty() )
{
sgdAddVec3 ( center, b->getMin(), b->getMax() ) ;
sgdScaleVec3 ( center, SGD_HALF ) ;
radius = sgdDistanceVec3 ( center, b->getMax() ) ;
return ;
}
/*
I can't think of a faster way to get an
utterly minimal sphere.
The tighter algorithm:- enclose each
of eight vertices of the box in turn - it
looks like being pretty costly.
[8 sqrt()'s]
The looser algorithm:- enclose the box
with an empty sphere and then do a
sphere-extend-sphere. This algorithm
does well for close-to-cube boxes, but
makes very poor spheres for long, thin
boxes.
[2 sqrt()'s]
*/
#ifdef DONT_REALLY_NEED_A_TIGHT_SPHERE_EXTEND_BOX
/* LOOSER/FASTER sphere-around-sphere-around-box */
sgdSphere s ;
s.empty () ;
s.enclose ( b ) ; /* Fast because s is empty */
enclose ( s ) ;
#else
/* TIGHTER/EXPENSIVE sphere-around-eight-points */
sgdVec3 x ;
extend ( b->getMin() ) ;
sgdSetVec3 ( x, b->getMin()[0],b->getMin()[1],b->getMax()[2] ) ; extend ( x ) ;
sgdSetVec3 ( x, b->getMin()[0],b->getMax()[1],b->getMin()[2] ) ; extend ( x ) ;
sgdSetVec3 ( x, b->getMin()[0],b->getMax()[1],b->getMax()[2] ) ; extend ( x ) ;
sgdSetVec3 ( x, b->getMax()[0],b->getMin()[1],b->getMin()[2] ) ; extend ( x ) ;
sgdSetVec3 ( x, b->getMax()[0],b->getMin()[1],b->getMax()[2] ) ; extend ( x ) ;
sgdSetVec3 ( x, b->getMax()[0],b->getMax()[1],b->getMin()[2] ) ; extend ( x ) ;
extend ( b->getMax() ) ;
#endif
}
void sgdSphere::extend ( const sgdSphere *s )
{
if ( s->isEmpty () )
return ;
if ( isEmpty () )
{
sgdCopyVec3 ( center, s->getCenter() ) ;
radius = s->getRadius() ;
return ;
}
/*
d == The distance between the sphere centers
*/
SGDfloat d = sgdDistanceVec3 ( center, s->getCenter() ) ;
if ( d + s->getRadius() <= radius ) /* New sphere is already inside this one */
return ;
if ( d + radius <= s->getRadius() ) /* New sphere completely contains this one */
{
sgdCopyVec3 ( center, s->getCenter() ) ;
radius = s->getRadius() ;
return ;
}
/*
Build a new sphere that completely contains the other two:
The center point lies halfway along the line between
the furthest points on the edges of the two spheres.
Computing those two points is ugly - so we'll use similar
triangles
*/
SGDfloat new_radius = (radius + d + s->getRadius() ) / SGD_TWO ;
SGDfloat ratio = ( new_radius - radius ) / d ;
center[0] += ( s->getCenter()[0] - center[0] ) * ratio ;
center[1] += ( s->getCenter()[1] - center[1] ) * ratio ;
center[2] += ( s->getCenter()[2] - center[2] ) * ratio ;
radius = new_radius ;
}
int sgdSphere::intersects ( const sgdBox *b ) const
{
sgdVec3 closest ;
if ( b->getMin()[0] > center[0] ) closest[0] = b->getMin()[0] ; else
if ( b->getMax()[0] < center[0] ) closest[0] = b->getMax()[0] ; else
closest[0] = center[0] ;
if ( b->getMin()[1] > center[1] ) closest[1] = b->getMin()[1] ; else
if ( b->getMax()[1] < center[1] ) closest[1] = b->getMax()[1] ; else
closest[1] = center[1] ;
if ( b->getMin()[2] > center[2] ) closest[2] = b->getMin()[2] ; else
if ( b->getMax()[2] < center[2] ) closest[2] = b->getMax()[2] ; else
closest[2] = center[2] ;
return sgdCompare3DSqdDist ( closest, center, sgdSquare ( radius ) ) <= 0 ;
}
/************************\
* sgdFrustum routines *
\************************/
void sgdFrustum::update ()
{
if ( fabs ( ffar - nnear ) < 0.1 )
{
ulSetError ( UL_WARNING, "sgdFrustum: Can't support depth of view <0.1 units.");
return ;
}
if ( hfov != SGD_ZERO && vfov != SGD_ZERO )
{
if ( fabs ( hfov ) < 0.1 || fabs ( vfov ) < 0.1 )
{
ulSetError ( UL_WARNING, "sgdFrustum: Can't support fields of view narrower than 0.1 degrees.");
return ;
}
/* Corners of screen relative to eye... */
right = nnear * tan ( hfov * SGD_DEGREES_TO_RADIANS / SGD_TWO ) ;
top = nnear * tan ( vfov * SGD_DEGREES_TO_RADIANS / SGD_TWO ) ;
left = -right ;
bot = -top ;
}
/*
Compute plane equations for the four sloping faces of the frustum.
These are useful for FrustContains(sphere) tests.
Noting that those planes always go through the origin, their 'D'
components will always be zero - so the plane equation is really
just the normal - which is the cross-product of two edges of each face,
and since we can pick two edges that go through the origin, the
vectors for the edges are just the normalised corners of the near plane.
*/
sgdVec3 v1, v2, v3, v4 ;
sgdSetVec3 ( v1, left , top, -nnear ) ;
sgdSetVec3 ( v2, right, top, -nnear ) ;
sgdSetVec3 ( v3, left , bot, -nnear ) ;
sgdSetVec3 ( v4, right, bot, -nnear ) ;
sgdNormaliseVec3 ( v1 ) ;
sgdNormaliseVec3 ( v2 ) ;
sgdNormaliseVec3 ( v3 ) ;
sgdNormaliseVec3 ( v4 ) ;
/*
Take care of the order of the parameters so that all the planes
are oriented facing inwards...
*/
sgdVectorProductVec3 ( top_plane, v1, v2 ) ;
sgdVectorProductVec3 ( right_plane, v2, v4 ) ;
sgdVectorProductVec3 ( bot_plane, v4, v3 ) ;
sgdVectorProductVec3 ( left_plane, v3, v1 ) ;
/*
At this point, you could call
glMatrixMode ( GL_PROJECTION ) ;
glLoadIdentity () ;
glFrustum ( left, right, bot, top, nnear, ffar ) ;
Or...
pfMakePerspFrust ( frust, left, right, bot, top ) ;
pfFrustNearFar ( frust, nnear, ffar ) ;
Or...
just use the matrix we generate below:
*/
/* Width, height, depth */
SGDfloat w = right - left ;
SGDfloat h = top - bot ;
SGDfloat d = ffar - nnear ;
mat[0][0] = SGD_TWO * nnear / w ;
mat[0][1] = SGD_ZERO ;
mat[0][2] = SGD_ZERO ;
mat[0][3] = SGD_ZERO ;
mat[1][0] = SGD_ZERO ;
mat[1][1] = SGD_TWO * nnear / h ;
mat[1][2] = SGD_ZERO ;
mat[1][3] = SGD_ZERO ;
mat[2][0] = ( right + left ) / w ;
mat[2][1] = ( top + bot ) / h ;
mat[2][2] = -( ffar + nnear ) / d ;
mat[2][3] = -SGD_ONE ;
mat[3][0] = SGD_ZERO ;
mat[3][1] = SGD_ZERO ;
mat[3][2] = -SGD_TWO * nnear * ffar/ d ;
mat[3][3] = SGD_ZERO ;
}
#define OC_LEFT_SHIFT 0
#define OC_RIGHT_SHIFT 1
#define OC_TOP_SHIFT 2
#define OC_BOT_SHIFT 3
#define OC_NEAR_SHIFT 4
#define OC_FAR_SHIFT 5
#define OC_ALL_ON_SCREEN 0x3F
#define OC_OFF_TRF ((1<<OC_TOP_SHIFT)|(1<<OC_RIGHT_SHIFT)|(1<<OC_FAR_SHIFT))
#define OC_OFF_BLN ((1<<OC_BOT_SHIFT)|(1<<OC_LEFT_SHIFT)|(1<<OC_NEAR_SHIFT))
int sgdFrustum::getOutcode ( const sgdVec3 pt ) const
{
/* Transform the point by the Frustum's transform. */
sgdVec4 tmp ;
tmp [ 0 ] = pt [ 0 ] ;
tmp [ 1 ] = pt [ 1 ] ;
tmp [ 2 ] = pt [ 2 ] ;
tmp [ 3 ] = SGD_ONE ;
sgdXformPnt4 ( tmp, tmp, mat ) ;
/*
No need to divide by the 'w' component since we are only checking for
results in the range 0..1
*/
return (( tmp[0] <= tmp[3] ) << OC_RIGHT_SHIFT ) |
(( tmp[0] >= -tmp[3] ) << OC_LEFT_SHIFT ) |
(( tmp[1] <= tmp[3] ) << OC_TOP_SHIFT ) |
(( tmp[1] >= -tmp[3] ) << OC_BOT_SHIFT ) |
(( tmp[2] <= tmp[3] ) << OC_FAR_SHIFT ) |
(( tmp[2] >= -tmp[3] ) << OC_NEAR_SHIFT ) ;
}
int sgdFrustum::contains ( const sgdVec3 pt ) const
{
return getOutcode ( pt ) == OC_ALL_ON_SCREEN ;
}
int sgdFrustum::contains ( const sgdSphere *s ) const
{
/*
Lop off half the database (roughly) with a quick near-plane test - and
lop off a lot more with a quick far-plane test
*/
if ( -s->getCenter() [ 2 ] + s->getRadius() < nnear ||
-s->getCenter() [ 2 ] - s->getRadius() > ffar )
return SGD_OUTSIDE ;
/*
OK, so the sphere lies between near and far.
Measure the distance of the center point from the four sides of the frustum,
if it's outside by more than the radius then it's history.
It's tempting to do a quick test to see if the center point is
onscreen using sgdFrustumContainsPt - but that takes a matrix transform
which is 16 multiplies and 12 adds - versus this test which does the
whole task using only 12 multiplies and 8 adds.
*/
SGDfloat sp1 = sgdScalarProductVec3 ( left_plane, s->getCenter() ) ;
SGDfloat sp2 = sgdScalarProductVec3 ( right_plane, s->getCenter() ) ;
SGDfloat sp3 = sgdScalarProductVec3 ( bot_plane, s->getCenter() ) ;
SGDfloat sp4 = sgdScalarProductVec3 ( top_plane, s->getCenter() ) ;
if ( -sp1 >= s->getRadius() || -sp2 >= s->getRadius() ||
-sp3 >= s->getRadius() || -sp4 >= s->getRadius() )
return SGD_OUTSIDE ;
/*
If it's inside by more than the radius then it's *completely* inside
and we can save time elsewhere if we know that for sure.
*/
if ( -s->getCenter() [ 2 ] - s->getRadius() > nnear &&
-s->getCenter() [ 2 ] + s->getRadius() < ffar &&
sp1 >= s->getRadius() && sp2 >= s->getRadius() &&
sp3 >= s->getRadius() && sp4 >= s->getRadius() )
return SGD_INSIDE ;
return SGD_STRADDLE ;
}
void sgdMakeCoordMat4 ( sgdMat4 m, const SGDfloat x, const SGDfloat y, const SGDfloat z, const SGDfloat h, const SGDfloat p, const SGDfloat r )
{
SGDfloat ch, sh, cp, sp, cr, sr, srsp, crsp, srcp ;
if ( h == SGD_ZERO )
{
ch = SGD_ONE ;
sh = SGD_ZERO ;
}
else
{
sh = sin( h * SGD_DEGREES_TO_RADIANS) ;
ch = cos( h * SGD_DEGREES_TO_RADIANS) ;
}
if ( p == SGD_ZERO )
{
cp = SGD_ONE ;
sp = SGD_ZERO ;
}
else
{
sp = sin( p * SGD_DEGREES_TO_RADIANS) ;
cp = cos( p * SGD_DEGREES_TO_RADIANS) ;
}
if ( r == SGD_ZERO )
{
cr = SGD_ONE ;
sr = SGD_ZERO ;
srsp = SGD_ZERO ;
srcp = SGD_ZERO ;
crsp = sp ;
}
else
{
sr = sin( r * SGD_DEGREES_TO_RADIANS) ;
cr = cos( r * SGD_DEGREES_TO_RADIANS) ;
srsp = sr * sp ;
crsp = cr * sp ;
srcp = sr * cp ;
}
m[0][0] = ch * cr - sh * srsp ;
m[1][0] = -sh * cp ;
m[2][0] = sr * ch + sh * crsp ;
m[3][0] = x ;
m[0][1] = cr * sh + srsp * ch ;
m[1][1] = ch * cp ;
m[2][1] = sr * sh - crsp * ch ;
m[3][1] = y ;
m[0][2] = -srcp ;
m[1][2] = sp ;
m[2][2] = cr * cp ;
m[3][2] = z ;
m[0][3] = SGD_ZERO ;
m[1][3] = SGD_ZERO ;
m[2][3] = SGD_ZERO ;
m[3][3] = SGD_ONE ;
}
void sgdMakeTransMat4 ( sgdMat4 m, const sgdVec3 xyz )
{
m[0][1] = m[0][2] = m[0][3] =
m[1][0] = m[1][2] = m[1][3] =
m[2][0] = m[2][1] = m[2][3] = SGD_ZERO ;
m[0][0] = m[1][1] = m[2][2] = m[3][3] = SGD_ONE ;
sgdCopyVec3 ( m[3], xyz ) ;
}
void sgdMakeTransMat4 ( sgdMat4 m, const SGDfloat x, const SGDfloat y, const SGDfloat z )
{
m[0][1] = m[0][2] = m[0][3] =
m[1][0] = m[1][2] = m[1][3] =
m[2][0] = m[2][1] = m[2][3] = SGD_ZERO ;
m[0][0] = m[1][1] = m[2][2] = m[3][3] = SGD_ONE ;
sgdSetVec3 ( m[3], x, y, z ) ;
}
void sgdSetCoord ( sgdCoord *dst, const sgdMat4 src )
{
sgdCopyVec3 ( dst->xyz, src[3] ) ;
sgdMat4 mat ;
SGDfloat s = sgdLengthVec3 ( src[0] ) ;
if ( s <= 0.00001 )
{
ulSetError ( UL_WARNING, "sgdMat4ToCoord: ERROR - Bad Matrix." ) ;
sgdSetVec3 ( dst -> hpr, SGD_ZERO, SGD_ZERO, SGD_ZERO ) ;
return ;
}
sgdScaleMat4 ( mat, src, SGD_ONE / s ) ;
dst->hpr[1] = asin ( _sgdClampToUnity ( mat[1][2] ) ) ;
SGDfloat cp = cos ( dst->hpr[1] ) ;
/* If pointing nearly vertically up - then heading is ill-defined */
if ( cp > -0.00001 && cp < 0.00001 )
{
SGDfloat cr = _sgdClampToUnity ( mat[0][1] ) ;
SGDfloat sr = _sgdClampToUnity (-mat[2][1] ) ;
dst->hpr[0] = SGD_ZERO ;
dst->hpr[2] = atan2 ( sr, cr ) ;
}
else
{
SGDfloat sr = _sgdClampToUnity ( -mat[0][2] / cp ) ;
SGDfloat cr = _sgdClampToUnity ( mat[2][2] / cp ) ;
SGDfloat sh = _sgdClampToUnity ( -mat[1][0] / cp ) ;
SGDfloat ch = _sgdClampToUnity ( mat[1][1] / cp ) ;
if ( (sh == SGD_ZERO && ch == SGD_ZERO) || (sr == SGD_ZERO && cr == SGD_ZERO) )
{
cr = _sgdClampToUnity ( mat[0][1] ) ;
sr = _sgdClampToUnity (-mat[2][1] ) ;
dst->hpr[0] = SGD_ZERO ;
}
else
dst->hpr[0] = atan2 ( sh, ch ) ;
dst->hpr[2] = atan2 ( sr, cr ) ;
}
sgdScaleVec3 ( dst->hpr, SGD_RADIANS_TO_DEGREES ) ;
}
void sgdMakeNormal(sgdVec3 dst, const sgdVec3 a, const sgdVec3 b, const sgdVec3 c )
{
sgdVec3 ab ; sgdSubVec3 ( ab, b, a ) ; sgdNormaliseVec3 ( ab ) ;
sgdVec3 ac ; sgdSubVec3 ( ac, c, a ) ; sgdNormaliseVec3 ( ac ) ;
sgdVectorProductVec3 ( dst, ab,ac ) ; sgdNormaliseVec3 ( dst ) ; /* XXX DO WE REALLY NEED THIS? */
}
void sgdPreMultMat4( sgdMat4 dst, const sgdMat4 src )
{
sgdMat4 mat ;
sgdMultMat4 ( mat, dst, src ) ;
sgdCopyMat4 ( dst, mat ) ;
}
void sgdPostMultMat4( sgdMat4 dst, const sgdMat4 src )
{
sgdMat4 mat ;
sgdMultMat4 ( mat, src, dst ) ;
sgdCopyMat4 ( dst, mat ) ;
}
void sgdMultMat4( sgdMat4 dst, const sgdMat4 m1, const sgdMat4 m2 )
{
for ( int j = 0 ; j < 4 ; j++ )
{
dst[0][j] = m2[0][0] * m1[0][j] +
m2[0][1] * m1[1][j] +
m2[0][2] * m1[2][j] +
m2[0][3] * m1[3][j] ;
dst[1][j] = m2[1][0] * m1[0][j] +
m2[1][1] * m1[1][j] +
m2[1][2] * m1[2][j] +
m2[1][3] * m1[3][j] ;
dst[2][j] = m2[2][0] * m1[0][j] +
m2[2][1] * m1[1][j] +
m2[2][2] * m1[2][j] +
m2[2][3] * m1[3][j] ;
dst[3][j] = m2[3][0] * m1[0][j] +
m2[3][1] * m1[1][j] +
m2[3][2] * m1[2][j] +
m2[3][3] * m1[3][j] ;
}
}
void sgdTransposeNegateMat4 ( sgdMat4 dst, const sgdMat4 src )
{
/* Poor man's invert - can be used when matrix is a simple rotate-translate */
dst[0][0] = src[0][0] ;
dst[1][0] = src[0][1] ;
dst[2][0] = src[0][2] ;
dst[3][0] = - sgdScalarProductVec3 ( src[3], src[0] ) ;
dst[0][1] = src[1][0] ;
dst[1][1] = src[1][1] ;
dst[2][1] = src[1][2] ;
dst[3][1] = - sgdScalarProductVec3 ( src[3], src[1] ) ;
dst[0][2] = src[2][0] ;
dst[1][2] = src[2][1] ;
dst[2][2] = src[2][2] ;
dst[3][2] = - sgdScalarProductVec3 ( src[3], src[2] ) ;
dst[0][3] = SGD_ZERO ;
dst[1][3] = SGD_ZERO ;
dst[2][3] = SGD_ZERO ;
dst[3][3] = SGD_ONE ;
}
void sgdTransposeNegateMat4 ( sgdMat4 dst )
{
sgdMat4 src ;
sgdCopyMat4 ( src, dst ) ;
sgdTransposeNegateMat4 ( dst, src ) ;
}
void sgdInvertMat4 ( sgdMat4 dst, const sgdMat4 src )
{
sgdMat4 tmp ;
sgdCopyMat4 ( tmp, src ) ;
sgdMakeIdentMat4 ( dst ) ;
for ( int i = 0 ; i != 4 ; i++ )
{
SGDfloat val = tmp[i][i] ;
int ind = i ;
int j ;
for ( j = i + 1 ; j != 4 ; j++ )
{
if ( fabs ( tmp[i][j] ) > fabs(val) )
{
ind = j;
val = tmp[i][j] ;
}
}
if ( ind != i )
{ /* swap columns */
for ( j = 0 ; j != 4 ; j++ )
{
SGDfloat t ;
t = dst[j][i]; dst[j][i] = dst[j][ind]; dst[j][ind] = t ;
t = tmp[j][i]; tmp[j][i] = tmp[j][ind]; tmp[j][ind] = t ;
}
}
// if ( val == SG_ZERO)
if ( fabs(val) <= DBL_EPSILON )
{
ulSetError ( UL_WARNING, "sg: ERROR - Singular matrix, no inverse!" ) ;
sgdMakeIdentMat4 ( dst ) ; /* Do *something* */
return;
}
SGDfloat ival = SGD_ONE / val ;
for ( j = 0 ; j != 4 ; j++ )
{
tmp[j][i] *= ival ;
dst[j][i] *= ival ;
}
for (j = 0; j != 4; j++)
{
if ( j == i )
continue ;
val = tmp[i][j] ;
for ( int k = 0 ; k != 4 ; k++ )
{
tmp[k][j] -= tmp[k][i] * val ;
dst[k][j] -= dst[k][i] * val ;
}
}
}
}
void sgdXformVec3 ( sgdVec3 dst, const sgdVec3 src, const sgdMat4 mat )
{
SGDfloat t0 = src[ 0 ] ;
SGDfloat t1 = src[ 1 ] ;
SGDfloat t2 = src[ 2 ] ;
dst[0] = ( t0 * mat[ 0 ][ 0 ] +
t1 * mat[ 1 ][ 0 ] +
t2 * mat[ 2 ][ 0 ] ) ;
dst[1] = ( t0 * mat[ 0 ][ 1 ] +
t1 * mat[ 1 ][ 1 ] +
t2 * mat[ 2 ][ 1 ] ) ;
dst[2] = ( t0 * mat[ 0 ][ 2 ] +
t1 * mat[ 1 ][ 2 ] +
t2 * mat[ 2 ][ 2 ] ) ;
}
void sgdXformPnt3 ( sgdVec3 dst, const sgdVec3 src, const sgdMat4 mat )
{
SGDfloat t0 = src[ 0 ] ;
SGDfloat t1 = src[ 1 ] ;
SGDfloat t2 = src[ 2 ] ;
dst[0] = ( t0 * mat[ 0 ][ 0 ] +
t1 * mat[ 1 ][ 0 ] +
t2 * mat[ 2 ][ 0 ] +
mat[ 3 ][ 0 ] ) ;
dst[1] = ( t0 * mat[ 0 ][ 1 ] +
t1 * mat[ 1 ][ 1 ] +
t2 * mat[ 2 ][ 1 ] +
mat[ 3 ][ 1 ] ) ;
dst[2] = ( t0 * mat[ 0 ][ 2 ] +
t1 * mat[ 1 ][ 2 ] +
t2 * mat[ 2 ][ 2 ] +
mat[ 3 ][ 2 ] ) ;
}
void sgdXformPnt4 ( sgdVec4 dst, const sgdVec4 src, const sgdMat4 mat )
{
SGDfloat t0 = src[ 0 ] ;
SGDfloat t1 = src[ 1 ] ;
SGDfloat t2 = src[ 2 ] ;
SGDfloat t3 = src[ 3 ] ;
dst[0] = ( t0 * mat[ 0 ][ 0 ] +
t1 * mat[ 1 ][ 0 ] +
t2 * mat[ 2 ][ 0 ] +
t3 * mat[ 3 ][ 0 ] ) ;
dst[1] = ( t0 * mat[ 0 ][ 1 ] +
t1 * mat[ 1 ][ 1 ] +
t2 * mat[ 2 ][ 1 ] +
t3 * mat[ 3 ][ 1 ] ) ;
dst[2] = ( t0 * mat[ 0 ][ 2 ] +
t1 * mat[ 1 ][ 2 ] +
t2 * mat[ 2 ][ 2 ] +
t3 * mat[ 3 ][ 2 ] ) ;
dst[3] = ( t0 * mat[ 0 ][ 3 ] +
t1 * mat[ 1 ][ 3 ] +
t2 * mat[ 2 ][ 3 ] +
t3 * mat[ 3 ][ 3 ] ) ;
}
void sgdFullXformPnt3 ( sgdVec3 dst, const sgdVec3 src, const sgdMat4 mat )
{
sgdVec4 tmp ;
tmp [ 0 ] = src [ 0 ] ;
tmp [ 1 ] = src [ 1 ] ;
tmp [ 2 ] = src [ 2 ] ;
tmp [ 3 ] = SGD_ONE ;
sgdXformPnt4 ( tmp, tmp, mat ) ;
sgdScaleVec3 ( dst, tmp, SGD_ONE / tmp [ 3 ] ) ;
}
void sgdHPRfromVec3 ( sgdVec3 hpr, sgdVec3 src )
{
sgdVec3 tmp ;
sgdCopyVec3 ( tmp, src ) ;
sgdNormaliseVec3 ( tmp ) ;
hpr[0] = -atan2 ( tmp [ 0 ], tmp [ 1 ] ) * SGD_RADIANS_TO_DEGREES ;
hpr[1] = -atan2 ( tmp [ 2 ], sqrt ( sgdSquare ( tmp [ 0 ] ) +
sgdSquare ( tmp [ 1 ] ) ) ) *
SGD_RADIANS_TO_DEGREES ;
hpr[2] = SGD_ZERO ;
}
/*
Quaternion routines are Copyright (C) 1999
Kevin B. Thompson <[email protected]>
Modified by Sylvan W. Clebsch <[email protected]>
Largely rewritten by "Negative0" <[email protected]>
*/
void sgdQuatToAngleAxis ( SGDfloat *angle,
SGDfloat *x, SGDfloat *y, SGDfloat *z,
const sgdQuat src )
{
sgdVec3 axis ;
sgdQuatToAngleAxis ( angle, axis, src ) ;
*x = axis [ 0 ] ;
*y = axis [ 1 ] ;
*z = axis [ 2 ] ;
}
void sgdQuatToAngleAxis ( SGDfloat *angle, sgdVec3 axis, const sgdQuat src )
{
SGDfloat a = (SGDfloat) acos ( src[SGD_W] ) ;
SGDfloat s = (SGDfloat) sin ( a ) ;
*angle = a * SGD_RADIANS_TO_DEGREES * SGD_TWO ;
if ( s == SGD_ZERO )
sgdSetVec3 ( axis, SGD_ZERO, SGD_ZERO, SGD_ONE );
else
{
sgdSetVec3 ( axis, src[SGD_X], src[SGD_Y], src[SGD_Z] ) ;
sgdScaleVec3 ( axis, SGD_ONE / s ) ;
}
}
void sgdAngleAxisToQuat ( sgdQuat dst,
const SGDfloat angle,
const SGDfloat x, const SGDfloat y, const SGDfloat z )
{
sgdVec3 axis ;
sgdSetVec3 ( axis, x, y, z ) ;
sgdAngleAxisToQuat ( dst, angle, axis ) ;
}
void sgdAngleAxisToQuat ( sgdQuat dst, const SGDfloat angle, const sgdVec3 axis )
{
SGDfloat temp_angle = angle * SGD_DEGREES_TO_RADIANS / SGD_TWO ;
sgdVec3 ax ;
sgdNormaliseVec3 ( ax, axis ) ;
SGDfloat s = - (SGDfloat) sin ( temp_angle ) ;
dst[SGD_W] = (SGDfloat) cos ( temp_angle ) ;
sgdScaleVec3 ( dst, ax, s ) ;
}
//from gamasutra.com
//by nb
void sgdMatrixToQuat( sgdQuat quat, sgdMat4 m )
{
SGDfloat tr, s, q[4] ;
int i, j, k ;
int nxt[3] = {1, 2, 0};
tr = m[0][0] + m[1][1] + m[2][2];
// check the diagonal