rmath3d 1.0.2 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -347,7 +347,7 @@ RMtx3Scale( RMtx3* out, const RMtx3* m, rmReal f )
347
347
 
348
348
  /*
349
349
  RMath : Ruby math module for 3D Applications
350
- Copyright (c) 2008- vaiorabbit <http://twitter.com/vaiorabbit>
350
+ Copyright (c) 2008-2020 vaiorabbit <http://twitter.com/vaiorabbit>
351
351
 
352
352
  This software is provided 'as-is', without any express or implied
353
353
  warranty. In no event will the authors be held liable for any damages
@@ -72,7 +72,7 @@ void RMtx3Scale( RMtx3* out, const RMtx3* m, rmReal f );
72
72
 
73
73
  /*
74
74
  RMath : Ruby math module for 3D Applications
75
- Copyright (c) 2008- vaiorabbit <http://twitter.com/vaiorabbit>
75
+ Copyright (c) 2008-2020 vaiorabbit <http://twitter.com/vaiorabbit>
76
76
 
77
77
  This software is provided 'as-is', without any express or implied
78
78
  warranty. In no event will the authors be held liable for any damages
@@ -479,37 +479,42 @@ RMtx4LookAtRH( RMtx4* out, const RVec3* eye, const RVec3* at, const RVec3* up )
479
479
  }
480
480
 
481
481
  void
482
- RMtx4PerspectiveRH( RMtx4* out, rmReal width, rmReal height, rmReal znear, rmReal zfar )
482
+ RMtx4PerspectiveRH( RMtx4* out, rmReal width, rmReal height, rmReal znear, rmReal zfar, bool ndc_homogeneous )
483
483
  {
484
- RMtx4PerspectiveOffCenterRH( out, -width/2.0f, width/2.0f, -height/2.0f, height/2.0f, znear, zfar );
484
+ RMtx4PerspectiveOffCenterRH( out, -width/2.0f, width/2.0f, -height/2.0f, height/2.0f, znear, zfar, ndc_homogeneous );
485
485
  }
486
486
 
487
- /* http://pyopengl.sourceforge.net/documentation/manual/gluPerspective.3G.html
487
+ /* https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml
488
+ Game Programming in C++ (2018) https://www.oreilly.com/library/view/game-programming-in/9780134598185/
488
489
  */
489
490
  void
490
- RMtx4PerspectiveFovRH( RMtx4* out, rmReal fovy_radian, rmReal aspect, rmReal znear, rmReal zfar )
491
+ RMtx4PerspectiveFovRH( RMtx4* out, rmReal fovy_radian, rmReal aspect, rmReal znear, rmReal zfar, bool ndc_homogeneous )
491
492
  {
492
493
  rmReal f = rmTan( fovy_radian / 2.0f );
493
494
  f = 1.0f / f;
494
495
 
496
+ rmReal C = ndc_homogeneous ? -(zfar+znear) / (zfar-znear) : zfar / -(zfar-znear);
497
+ rmReal D = ndc_homogeneous ? -(2*znear*zfar) / (zfar-znear) : -(znear*zfar) / (zfar-znear);
498
+
495
499
  RMtx4Identity( out );
496
500
  SET_ELEMENT( out, 0, 0, f / aspect );
497
501
  SET_ELEMENT( out, 1, 1, f );
498
- SET_ELEMENT( out, 2, 2, (zfar+znear)/(znear-zfar) );
499
- SET_ELEMENT( out, 2, 3, 2*zfar*znear/(znear-zfar) );
502
+ SET_ELEMENT( out, 2, 2, C );
503
+ SET_ELEMENT( out, 2, 3, D );
500
504
  SET_ELEMENT( out, 3, 2, -1.0f );
501
505
  SET_ELEMENT( out, 3, 3, 0.0f );
502
506
  }
503
507
 
504
- /* http://pyopengl.sourceforge.net/documentation/manual/glFrustum.3G.html
508
+ /* https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glFrustum.xml
509
+ Game Programming in C++ (2018) https://www.oreilly.com/library/view/game-programming-in/9780134598185/
505
510
  */
506
511
  void
507
- RMtx4PerspectiveOffCenterRH( RMtx4* out, rmReal left, rmReal right, rmReal bottom, rmReal top, rmReal znear, rmReal zfar )
512
+ RMtx4PerspectiveOffCenterRH( RMtx4* out, rmReal left, rmReal right, rmReal bottom, rmReal top, rmReal znear, rmReal zfar, bool ndc_homogeneous )
508
513
  {
509
514
  rmReal A = (right+left) / (right-left);
510
515
  rmReal B = (top+bottom) / (top-bottom);
511
- rmReal C = -(zfar+znear) / (zfar-znear);
512
- rmReal D = -(2*znear*zfar) / (zfar-znear);
516
+ rmReal C = ndc_homogeneous ? -(zfar+znear) / (zfar-znear) : -zfar / (zfar-znear);
517
+ rmReal D = ndc_homogeneous ? -(2*znear*zfar) / (zfar-znear) : -(znear*zfar) / (zfar-znear);
513
518
 
514
519
  RMtx4Identity( out );
515
520
  SET_ELEMENT( out, 0, 0, 2*znear/(right-left) );
@@ -523,33 +528,34 @@ RMtx4PerspectiveOffCenterRH( RMtx4* out, rmReal left, rmReal right, rmReal botto
523
528
  }
524
529
 
525
530
  void
526
- RMtx4OrthoRH( RMtx4* out, rmReal width, rmReal height, rmReal znear, rmReal zfar )
531
+ RMtx4OrthoRH( RMtx4* out, rmReal width, rmReal height, rmReal znear, rmReal zfar, bool ndc_homogeneous )
527
532
  {
528
- RMtx4OrthoOffCenterRH( out, -width/2.0f, width/2.0f, -height/2.0f, height/2.0f, znear, zfar );
533
+ RMtx4OrthoOffCenterRH( out, -width/2.0f, width/2.0f, -height/2.0f, height/2.0f, znear, zfar, ndc_homogeneous );
529
534
  }
530
535
 
531
- /* http://pyopengl.sourceforge.net/documentation/manual/glOrtho.3G.html
536
+ /* https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glOrtho.xml
537
+ Game Programming in C++ (2018) https://www.oreilly.com/library/view/game-programming-in/9780134598185/
532
538
  */
533
539
  void
534
- RMtx4OrthoOffCenterRH( RMtx4* out, rmReal left, rmReal right, rmReal bottom, rmReal top, rmReal znear, rmReal zfar )
540
+ RMtx4OrthoOffCenterRH( RMtx4* out, rmReal left, rmReal right, rmReal bottom, rmReal top, rmReal znear, rmReal zfar, bool ndc_homogeneous )
535
541
  {
536
- rmReal tx = (right+left) / (right-left);
537
- rmReal ty = (top+bottom) / (top-bottom);
538
- rmReal tz = (zfar+znear) / (zfar-znear);
542
+ rmReal tx = -(right+left) / (right-left);
543
+ rmReal ty = -(top+bottom) / (top-bottom);
544
+ rmReal tz = ndc_homogeneous ? -(zfar+znear) / (zfar-znear) : -znear / (zfar-znear);
539
545
 
540
546
  RMtx4Identity( out );
541
547
  SET_ELEMENT( out, 0, 0, 2.0f/(right-left) );
542
548
  SET_ELEMENT( out, 0, 3, tx );
543
549
  SET_ELEMENT( out, 1, 1, 2.0f/(top-bottom) );
544
550
  SET_ELEMENT( out, 1, 3, ty );
545
- SET_ELEMENT( out, 2, 2, -2.0f/(zfar-znear) );
551
+ SET_ELEMENT( out, 2, 2, -(ndc_homogeneous ? 2.0f : 1.0f)/(zfar-znear) );
546
552
  SET_ELEMENT( out, 2, 3, tz );
547
553
  }
548
554
 
549
555
 
550
556
  /*
551
557
  RMath : Ruby math module for 3D Applications
552
- Copyright (c) 2008- vaiorabbit <http://twitter.com/vaiorabbit>
558
+ Copyright (c) 2008-2020 vaiorabbit <http://twitter.com/vaiorabbit>
553
559
 
554
560
  This software is provided 'as-is', without any express or implied
555
561
  warranty. In no event will the authors be held liable for any damages
@@ -2,6 +2,7 @@
2
2
  #ifndef RMATHMTX4_H_INCLUDED
3
3
  #define RMATHMTX4_H_INCLUDED
4
4
 
5
+ #include <stdbool.h>
5
6
  #include "RType.h"
6
7
 
7
8
  struct RVec3;
@@ -71,11 +72,11 @@ void RMtx4Mul( RMtx4* out, const RMtx4* m1, const RMtx4* m2 );
71
72
  void RMtx4Scale( RMtx4* out, const RMtx4* m, rmReal f );
72
73
 
73
74
  void RMtx4LookAtRH( RMtx4* out, const struct RVec3* eye, const struct RVec3* at, const struct RVec3* up );
74
- void RMtx4PerspectiveRH( RMtx4* out, rmReal width, rmReal height, rmReal znear, rmReal zfar );
75
- void RMtx4PerspectiveFovRH( RMtx4* out, rmReal fovy_radian, rmReal aspect, rmReal znear, rmReal zfar );
76
- void RMtx4PerspectiveOffCenterRH( RMtx4* out, rmReal left, rmReal right, rmReal bottom, rmReal top, rmReal znear, rmReal zfar );
77
- void RMtx4OrthoRH( RMtx4* out, rmReal width, rmReal height, rmReal znear, rmReal zfar );
78
- void RMtx4OrthoOffCenterRH( RMtx4* out, rmReal left, rmReal right, rmReal bottom, rmReal top, rmReal znear, rmReal zfar );
75
+ void RMtx4PerspectiveRH( RMtx4* out, rmReal width, rmReal height, rmReal znear, rmReal zfar, bool ndc_homogeneous );
76
+ void RMtx4PerspectiveFovRH( RMtx4* out, rmReal fovy_radian, rmReal aspect, rmReal znear, rmReal zfar, bool ndc_homogeneous );
77
+ void RMtx4PerspectiveOffCenterRH( RMtx4* out, rmReal left, rmReal right, rmReal bottom, rmReal top, rmReal znear, rmReal zfar, bool ndc_homogeneous );
78
+ void RMtx4OrthoRH( RMtx4* out, rmReal width, rmReal height, rmReal znear, rmReal zfar, bool ndc_homogeneous );
79
+ void RMtx4OrthoOffCenterRH( RMtx4* out, rmReal left, rmReal right, rmReal bottom, rmReal top, rmReal znear, rmReal zfar, bool ndc_homogeneous );
79
80
 
80
81
  #ifdef __cplusplus
81
82
  }
@@ -87,7 +88,7 @@ void RMtx4OrthoOffCenterRH( RMtx4* out, rmReal left, rmReal right, rmReal bot
87
88
 
88
89
  /*
89
90
  RMath : Ruby math module for 3D Applications
90
- Copyright (c) 2008- vaiorabbit <http://twitter.com/vaiorabbit>
91
+ Copyright (c) 2008-2020 vaiorabbit <http://twitter.com/vaiorabbit>
91
92
 
92
93
  This software is provided 'as-is', without any express or implied
93
94
  warranty. In no event will the authors be held liable for any damages
@@ -344,7 +344,7 @@ RQuatToAxisAngle( const RQuat* in, struct RVec3* axis, rmReal* radian )
344
344
 
345
345
  /*
346
346
  RMath : Ruby math module for 3D Applications
347
- Copyright (c) 2008- vaiorabbit <http://twitter.com/vaiorabbit>
347
+ Copyright (c) 2008-2020 vaiorabbit <http://twitter.com/vaiorabbit>
348
348
 
349
349
  This software is provided 'as-is', without any express or implied
350
350
  warranty. In no event will the authors be held liable for any damages
@@ -69,7 +69,7 @@ void RQuatToAxisAngle( const RQuat* in, struct RVec3* axis, rmReal* radian );
69
69
 
70
70
  /*
71
71
  RMath : Ruby math module for 3D Applications
72
- Copyright (c) 2008- vaiorabbit <http://twitter.com/vaiorabbit>
72
+ Copyright (c) 2008-2020 vaiorabbit <http://twitter.com/vaiorabbit>
73
73
 
74
74
  This software is provided 'as-is', without any express or implied
75
75
  warranty. In no event will the authors be held liable for any damages
@@ -53,7 +53,7 @@ typedef double rmReal;
53
53
 
54
54
  /*
55
55
  RMath : Ruby math module for 3D Applications
56
- Copyright (c) 2008- vaiorabbit <http://twitter.com/vaiorabbit>
56
+ Copyright (c) 2008-2020 vaiorabbit <http://twitter.com/vaiorabbit>
57
57
 
58
58
  This software is provided 'as-is', without any express or implied
59
59
  warranty. In no event will the authors be held liable for any damages
@@ -0,0 +1,167 @@
1
+ #include <math.h>
2
+ #include <string.h>
3
+
4
+ #include "RVec2.h"
5
+ #include "RMtx2.h"
6
+
7
+ #define SET_ELEMENT( out, at, f )
8
+
9
+ void
10
+ RVec2SetElement( RVec2* out, int at, rmReal f )
11
+ {
12
+ out->e[at] = f;
13
+ }
14
+
15
+ void
16
+ RVec2SetX( RVec2* out, rmReal x )
17
+ {
18
+ out->x = x;
19
+ }
20
+
21
+ void
22
+ RVec2SetY( RVec2* out, rmReal y )
23
+ {
24
+ out->y = y;
25
+ }
26
+
27
+ void
28
+ RVec2SetElements( RVec2* out, rmReal x, rmReal y )
29
+ {
30
+ RVec2SetX( out, x );
31
+ RVec2SetY( out, y );
32
+ }
33
+
34
+ rmReal
35
+ RVec2GetElement( const RVec2* in, int at )
36
+ {
37
+ return in->e[at];
38
+ }
39
+
40
+ rmReal
41
+ RVec2GetX( const RVec2* in )
42
+ {
43
+ return in->x;
44
+ }
45
+
46
+ rmReal
47
+ RVec2GetY( const RVec2* in )
48
+ {
49
+ return in->y;
50
+ }
51
+
52
+ int
53
+ RVec2Equal( const RVec2* v1, const RVec2* v2 )
54
+ {
55
+ if ( 0 == memcmp( v1, v2, sizeof(RVec2) ) )
56
+ return !0;
57
+ else
58
+ return 0;
59
+ }
60
+
61
+ void
62
+ RVec2Add( RVec2* out, const RVec2* v1, const RVec2* v2 )
63
+ {
64
+ out->x = v1->x + v2->x;
65
+ out->y = v1->y + v2->y;
66
+ }
67
+
68
+ void
69
+ RVec2Sub( RVec2* out, const RVec2* v1, const RVec2* v2 )
70
+ {
71
+ out->x = v1->x - v2->x;
72
+ out->y = v1->y - v2->y;
73
+ }
74
+
75
+ void
76
+ RVec2Scale( RVec2* out, const RVec2* in, rmReal f )
77
+ {
78
+ out->x = in->x * f;
79
+ out->y = in->y * f;
80
+ }
81
+
82
+ rmReal
83
+ RVec2Cross( const RVec2* v1, const RVec2* v2 )
84
+ {
85
+ rmReal v1x, v1y, v2x, v2y;
86
+
87
+ v1x = v1->x;
88
+ v1y = v1->y;
89
+ v2x = v2->x;
90
+ v2y = v2->y;
91
+
92
+ return v1x*v2y - v1y*v2x;
93
+ }
94
+
95
+ rmReal
96
+ RVec2LengthSq( const RVec2* in )
97
+ {
98
+ return in->x*in->x + in->y*in->y;
99
+ }
100
+
101
+ rmReal
102
+ RVec2Length( const RVec2* in )
103
+ {
104
+ return rmSqrt( in->x*in->x + in->y*in->y );
105
+ }
106
+
107
+ rmReal
108
+ RVec2Dot( const RVec2* v1, const RVec2* v2 )
109
+ {
110
+ return v1->x*v2->x + v1->y*v2->y;
111
+ }
112
+
113
+ void
114
+ RVec2Copy( RVec2* out, const RVec2* in )
115
+ {
116
+ out->x = in->x;
117
+ out->y = in->y;
118
+ }
119
+
120
+ void
121
+ RVec2Normalize( RVec2* out, const RVec2* in )
122
+ {
123
+ rmReal length = RVec2Length( in );
124
+ RVec2Scale( out, in, 1.0f/length );
125
+ }
126
+
127
+ void
128
+ RVec2Transform( RVec2* out, const RMtx2* m, const RVec2* in )
129
+ {
130
+ RVec2 result, in_vec2;
131
+ int row;
132
+
133
+ RVec2Copy( &in_vec2, in );
134
+ for ( row = 0; row < 2; ++row )
135
+ {
136
+ RVec2 row_vector;
137
+ RMtx2GetRow( &row_vector, m, row );
138
+ RVec2SetElement( &result, row, RVec2Dot( &row_vector, &in_vec2 ) );
139
+ }
140
+
141
+ out->x = RVec2GetX( &result );
142
+ out->y = RVec2GetY( &result );
143
+ }
144
+
145
+ /*
146
+ RMath : Ruby math module for 3D Applications
147
+ Copyright (c) 2008-2020 vaiorabbit <http://twitter.com/vaiorabbit>.
148
+
149
+ This software is provided 'as-is', without any express or implied
150
+ warranty. In no event will the authors be held liable for any damages
151
+ arising from the use of this software.
152
+
153
+ Permission is granted to anyone to use this software for any purpose,
154
+ including commercial applications, and to alter it and redistribute it
155
+ freely, subject to the following restrictions:
156
+
157
+ 1. The origin of this software must not be misrepresented; you must not
158
+ claim that you wrote the original software. If you use this software
159
+ in a product, an acknowledgment in the product documentation would be
160
+ appreciated but is not required.
161
+
162
+ 2. Altered source versions must be plainly marked as such, and must not be
163
+ misrepresented as being the original software.
164
+
165
+ 3. This notice may not be removed or altered from any source
166
+ distribution.
167
+ */
@@ -0,0 +1,80 @@
1
+ /* -*- C -*- */
2
+ #ifndef RMATHVEC2_H_INCLUDED
3
+ #define RMATHVEC2_H_INCLUDED
4
+
5
+ #include "RType.h"
6
+
7
+ struct RMtx2;
8
+
9
+ typedef struct RVec2
10
+ {
11
+ union
12
+ {
13
+ struct
14
+ {
15
+ rmReal x, y;
16
+ };
17
+ rmReal e[2];
18
+ };
19
+ } RVec2;
20
+
21
+ #ifdef __cplusplus
22
+ extern "C" {
23
+ #endif
24
+
25
+ void RVec2SetElements( RVec2* out, rmReal x, rmReal y );
26
+ void RVec2SetElement( RVec2* out, int at, rmReal f );
27
+ void RVec2SetX( RVec2* out, rmReal x );
28
+ void RVec2SetY( RVec2* out, rmReal y );
29
+
30
+ rmReal RVec2GetElement( const RVec2* in, int at );
31
+ rmReal RVec2GetX( const RVec2* in );
32
+ rmReal RVec2GetY( const RVec2* in );
33
+
34
+ int RVec2Equal( const RVec2* v1, const RVec2* v2 );
35
+
36
+ void RVec2Add( RVec2* out, const RVec2* v1, const RVec2* v2 );
37
+ void RVec2Sub( RVec2* out, const RVec2* v1, const RVec2* v2 );
38
+ void RVec2Scale( RVec2* out, const RVec2* in, rmReal f );
39
+ rmReal RVec2Cross( const RVec2* v1, const RVec2* v2 );
40
+
41
+ rmReal RVec2Length( const RVec2* in );
42
+ rmReal RVec2LengthSq( const RVec2* in );
43
+
44
+ rmReal RVec2Dot( const RVec2* v1, const RVec2* v2 );
45
+
46
+ void RVec2Copy( RVec2* out, const RVec2* in );
47
+ void RVec2Normalize( RVec2* out, const RVec2* in );
48
+
49
+ void RVec2Transform( struct RVec2* out, const struct RMtx2* m, const RVec2* in );
50
+
51
+
52
+ #ifdef __cplusplus
53
+ }
54
+ #endif
55
+
56
+ #endif
57
+
58
+ /*
59
+ RMath : Ruby math module for 3D Applications
60
+ Copyright (c) 2008-2020 vaiorabbit <http://twitter.com/vaiorabbit>
61
+
62
+ This software is provided 'as-is', without any express or implied
63
+ warranty. In no event will the authors be held liable for any damages
64
+ arising from the use of this software.
65
+
66
+ Permission is granted to anyone to use this software for any purpose,
67
+ including commercial applications, and to alter it and redistribute it
68
+ freely, subject to the following restrictions:
69
+
70
+ 1. The origin of this software must not be misrepresented; you must not
71
+ claim that you wrote the original software. If you use this software
72
+ in a product, an acknowledgment in the product documentation would be
73
+ appreciated but is not required.
74
+
75
+ 2. Altered source versions must be plainly marked as such, and must not be
76
+ misrepresented as being the original software.
77
+
78
+ 3. This notice may not be removed or altered from any source
79
+ distribution.
80
+ */
@@ -262,7 +262,7 @@ RVec3TransformByQuaternion( RVec3* out, const struct RQuat* q, const RVec3* in )
262
262
 
263
263
  /*
264
264
  RMath : Ruby math module for 3D Applications
265
- Copyright (c) 2008- vaiorabbit <http://twitter.com/vaiorabbit>.
265
+ Copyright (c) 2008-2020 vaiorabbit <http://twitter.com/vaiorabbit>.
266
266
 
267
267
  This software is provided 'as-is', without any express or implied
268
268
  warranty. In no event will the authors be held liable for any damages
@@ -66,7 +66,7 @@ void RVec3TransformByQuaternion( RVec3* out, const struct RQuat* q, const RVe
66
66
 
67
67
  /*
68
68
  RMath : Ruby math module for 3D Applications
69
- Copyright (c) 2008- vaiorabbit <http://twitter.com/vaiorabbit>
69
+ Copyright (c) 2008-2020 vaiorabbit <http://twitter.com/vaiorabbit>
70
70
 
71
71
  This software is provided 'as-is', without any express or implied
72
72
  warranty. In no event will the authors be held liable for any damages
@@ -192,7 +192,7 @@ RVec4TransformTransposed( RVec4* out, const struct RMtx4* m, const RVec4* in )
192
192
 
193
193
  /*
194
194
  RMath : Ruby math module for 3D Applications
195
- Copyright (c) 2008- vaiorabbit <http://twitter.com/vaiorabbit>
195
+ Copyright (c) 2008-2020 vaiorabbit <http://twitter.com/vaiorabbit>
196
196
 
197
197
  This software is provided 'as-is', without any express or implied
198
198
  warranty. In no event will the authors be held liable for any damages