rmath3d_plain 1.0.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -104,7 +104,7 @@ class App
104
104
  def size_callback( window_handle, w, h )
105
105
  glViewport( 0, 0, w, h )
106
106
  glMatrixMode( GL_PROJECTION )
107
- @mtxProj.perspectiveFovRH( 30.0*Math::PI/180.0, w.to_f/h.to_f, 0.1, 1000.0 )
107
+ @mtxProj.perspectiveFovRH( 30.0*Math::PI/180.0, w.to_f/h.to_f, 0.1, 1000.0, true )
108
108
  glLoadMatrixf( @mtxProj.to_a.pack('F16') )
109
109
 
110
110
  @window_width = w
@@ -119,7 +119,7 @@ class App
119
119
  @at = RVec3.new(0.0, 0.0, 0.0)
120
120
  @up = RVec3.new(0.0, 1.0, 0.0)
121
121
  @mtxLookAt = RMtx4.new.lookAtRH( @eye, @at, @up )
122
- @mtxProj = RMtx4.new.perspectiveFovRH( 30.0*Math::PI/180.0, @window_width.to_f/@window_height.to_f, 0.1, 1000.0 )
122
+ @mtxProj = RMtx4.new.perspectiveFovRH( 30.0*Math::PI/180.0, @window_width.to_f/@window_height.to_f, 0.1, 1000.0, true )
123
123
 
124
124
  @light_pos = [2.5,0,5,1]
125
125
  @light_diffuse = [1,1,1,1]
@@ -42,7 +42,7 @@ class App
42
42
  def reshape( width, height )
43
43
  glViewport( 0, 0, width, height )
44
44
  glMatrixMode( GL_PROJECTION )
45
- @mtxProj.perspectiveFovRH( 30.0*Math::PI/180.0, width.to_f/height.to_f, 0.1, 1000.0 )
45
+ @mtxProj.perspectiveFovRH( 30.0*Math::PI/180.0, width.to_f/height.to_f, 0.1, 1000.0, true )
46
46
  glLoadMatrix( @mtxProj )
47
47
 
48
48
  @window_width = width
@@ -74,7 +74,7 @@ class App
74
74
  @at = RVec3.new(0.0, 0.0, 0.0)
75
75
  @up = RVec3.new(0.0, 1.0, 0.0)
76
76
  @mtxLookAt = RMtx4.new.lookAtRH( @eye, @at, @up )
77
- @mtxProj = RMtx4.new.perspectiveFovRH( 30.0*Math::PI/180.0, @window_width.to_f/@window_height.to_f, 0.1, 1000.0 )
77
+ @mtxProj = RMtx4.new.perspectiveFovRH( 30.0*Math::PI/180.0, @window_width.to_f/@window_height.to_f, 0.1, 1000.0, true )
78
78
 
79
79
  @light_pos = [2.5,0,5,1]
80
80
  @light_diffuse = [1,1,1,1]
@@ -1,4 +1,4 @@
1
- begin
1
+ begin
2
2
  require 'rmath3d/rmath3d'
3
3
  rescue LoadError
4
4
  require 'rmath3d/rmath3d_plain'
@@ -11,8 +11,10 @@ include RMath3D
11
11
  require 'minitest/autorun'
12
12
 
13
13
  # Test cases
14
+ require_relative 'test_RVec2.rb'
14
15
  require_relative 'test_RVec3.rb'
15
16
  require_relative 'test_RVec4.rb'
16
17
  require_relative 'test_RQuat.rb'
18
+ require_relative 'test_RMtx2.rb'
17
19
  require_relative 'test_RMtx3.rb'
18
20
  require_relative 'test_RMtx4.rb'
@@ -0,0 +1,363 @@
1
+ class TC_RMtx2 < Minitest::Test
2
+
3
+ def setup
4
+ @tolerance = RMath3D::TOLERANCE
5
+ @mZero = RMtx2.new.setZero
6
+ @mIdentity = RMtx2.new.setIdentity
7
+ end
8
+
9
+ def teardown
10
+ end
11
+
12
+ def test_RMtx_initialize
13
+ m0 = RMtx2.new
14
+ for r in 0...2 do
15
+ for c in 0...2 do
16
+ assert_equal( 0.0, m0.getElement(r,c) )
17
+ end
18
+ end
19
+
20
+ m1 = RMtx2.new( 0, 1,
21
+ 2, 3 )
22
+ assert_equal( 0, m1.getElement(0,0) )
23
+ assert_equal( 1, m1.getElement(0,1) )
24
+ assert_equal( 2, m1.getElement(1,0) )
25
+ assert_equal( 3, m1.getElement(1,1) )
26
+
27
+ m2 = RMtx2.new( m1 )
28
+ for r in 0...2 do
29
+ for c in 0...2 do
30
+ assert_equal( 2*r+c, m2.getElement(r,c) )
31
+ end
32
+ end
33
+ end
34
+
35
+ def test_to_s
36
+ assert_respond_to( @mZero, :to_s )
37
+ end
38
+
39
+ def test_coerce
40
+ assert_respond_to( @mZero, :coerce )
41
+ end
42
+
43
+ def test_setElements
44
+ @mZero.setElements( 0, 1, 2, 3 )
45
+ for r in 0...2 do
46
+ for c in 0...2 do
47
+ assert_equal( 2*r+c, @mZero.getElement(r,c) )
48
+ end
49
+ end
50
+ end
51
+
52
+ def test_setElement
53
+ for r in 0...2 do
54
+ for c in 0...2 do
55
+ @mZero.setElement( r, c, 2*r+c )
56
+ end
57
+ end
58
+ for r in 0...2 do
59
+ for c in 0...2 do
60
+ assert_equal( 2*r+c, @mZero.getElement(r,c) )
61
+ end
62
+ end
63
+
64
+ for r in 0...2 do
65
+ for c in 0...2 do
66
+ @mZero[r, c] = 2*c+r
67
+ end
68
+ end
69
+ for r in 0...2 do
70
+ for c in 0...2 do
71
+ assert_equal( 2*c+r, @mZero[r,c] )
72
+ end
73
+ end
74
+ end
75
+
76
+ def test_getElement
77
+ assert_respond_to( @mIdentity, :getElement )
78
+ for r in 0...2 do
79
+ for c in 0...2 do
80
+ e = @mIdentity.getElement( r, c )
81
+ if ( r == c )
82
+ assert_equal( 1.0, e )
83
+ else
84
+ assert_equal( 0.0, e )
85
+ end
86
+ end
87
+ end
88
+
89
+ for r in 0...2 do
90
+ for c in 0...2 do
91
+ e = @mIdentity[ r, c ]
92
+ if ( r == c )
93
+ assert_equal( 1.0, e )
94
+ else
95
+ assert_equal( 0.0, e )
96
+ end
97
+ end
98
+ end
99
+
100
+ mtx = RMtx2.new(1,2,
101
+ 3,4)
102
+ assert_equal( mtx.e00, 1 )
103
+ assert_equal( mtx.e01, 2 )
104
+ assert_equal( mtx.e10, 3 )
105
+ assert_equal( mtx.e11, 4 )
106
+ end
107
+
108
+ def test_getRowColumn
109
+ mtx = RMtx2.new(1,2,
110
+ 3,4)
111
+
112
+ v = mtx.getRow(0)
113
+ assert_equal( v.x, 1 )
114
+ assert_equal( v.y, 2 )
115
+
116
+ v = mtx.getRow(1)
117
+ assert_equal( v.x, 3 )
118
+ assert_equal( v.y, 4 )
119
+
120
+ v = mtx.getColumn(0)
121
+ assert_equal( v.x, 1 )
122
+ assert_equal( v.y, 3 )
123
+
124
+ v = mtx.getColumn(1)
125
+ assert_equal( v.x, 2 )
126
+ assert_equal( v.y, 4 )
127
+ end
128
+
129
+ def test_setRowColumn
130
+ mtx = RMtx2.new
131
+
132
+ vr = [RVec2.new(1,2),RVec2.new(3,4)]
133
+ mtx.setRow(vr[0],0)
134
+ mtx.setRow(vr[1],1)
135
+ assert_equal( mtx.e00, 1 )
136
+ assert_equal( mtx.e01, 2 )
137
+ assert_equal( mtx.e10, 3 )
138
+ assert_equal( mtx.e11, 4 )
139
+
140
+ vc = [RVec2.new(1,2),RVec2.new(3,4)]
141
+ mtx.setColumn(vc[0],0)
142
+ mtx.setColumn(vc[1],1)
143
+ assert_equal( mtx.e00, 1 )
144
+ assert_equal( mtx.e01, 3 )
145
+ assert_equal( mtx.e10, 2 )
146
+ assert_equal( mtx.e11, 4 )
147
+ end
148
+
149
+ def test_setZero
150
+ m = RMtx2.new( 1, 2, 3, 4 )
151
+ m.setZero
152
+ for r in 0...2 do
153
+ for c in 0...2 do
154
+ assert_equal( 0.0, m.getElement( r, c ) )
155
+ end
156
+ end
157
+ end
158
+
159
+ def test_setIdentity
160
+ m = RMtx2.new( 1, 2, 3, 4 )
161
+ m.setIdentity
162
+ for r in 0...2 do
163
+ for c in 0...2 do
164
+ e = @mIdentity.getElement( r, c )
165
+ if ( r == c )
166
+ assert_equal( 1.0, e )
167
+ else
168
+ assert_equal( 0.0, e )
169
+ end
170
+ end
171
+ end
172
+ end
173
+
174
+ def test_getDeterminant
175
+ m1 = RMtx2.new( 2, -3,
176
+ 1, 3 )
177
+ assert_equal( 9.0, m1.getDeterminant )
178
+ end
179
+
180
+ def test_transpose
181
+ m0 = RMtx2.new( -2, -3,
182
+ -1, 3 )
183
+ # RMtx2#getTransposed
184
+ m1 = m0.getTransposed
185
+ for r in 0...2 do
186
+ for c in 0...2 do
187
+ assert_equal( m0.getElement(c,r), m1.getElement(r,c) )
188
+ end
189
+ end
190
+
191
+ # RMtx2#transpose!
192
+ m0.transpose!
193
+ for r in 0...2 do
194
+ for c in 0...2 do
195
+ assert_equal( m0.getElement(r,c), m1.getElement(r,c) )
196
+ end
197
+ end
198
+ end
199
+
200
+ def test_inverse
201
+ # RMtx2#getInverse
202
+ m0 = RMtx2.new( 1, 2,
203
+ 3, 4 )
204
+
205
+ m0inv = RMtx2.new(-2.0, 1.0,
206
+ 1.5, -0.5 )
207
+ m1 = m0.getInverse
208
+ for r in 0...2 do
209
+ for c in 0...2 do
210
+ assert_in_delta( m0inv.getElement(r,c), m1.getElement(r,c), @tolerance )
211
+ end
212
+ end
213
+
214
+ # RMtx2#invert!
215
+ m0.invert!
216
+
217
+ for r in 0...2 do
218
+ for c in 0...2 do
219
+ assert_in_delta( m0inv.getElement(r,c), m0.getElement(r,c), @tolerance )
220
+ end
221
+ end
222
+ end
223
+
224
+ def test_rotation
225
+ m0 = RMtx2.new( Math::sqrt(2)/2, -Math::sqrt(2)/2,
226
+ Math::sqrt(2)/2, Math::sqrt(2)/2 )
227
+ m1 = RMtx2.new.rotation( Math::PI/4.0 )
228
+
229
+ for r in 0...2 do
230
+ for c in 0...2 do
231
+ assert_in_delta( m0.getElement(r,c), m1.getElement(r,c), @tolerance )
232
+ end
233
+ end
234
+ end
235
+
236
+ def test_scaling
237
+ m0 = RMtx2.new( 10.0, 0.0,
238
+ 0.0, 20.0 )
239
+ m1 = RMtx2.new.scaling( 10.0, 20.0 )
240
+ for r in 0...2 do
241
+ for c in 0...2 do
242
+ assert_in_delta( m0.getElement(r,c), m1.getElement(r,c), @tolerance )
243
+ end
244
+ end
245
+ end
246
+
247
+ def test_unary_operators
248
+ # RMtx2#+@
249
+ m0 = RMtx2.new( 0, 1, 2, 3 )
250
+ m1 = RMtx2.new( 0, 1, 2, 3 )
251
+ m2 = +m0
252
+
253
+ assert_same( m0, m2 )
254
+ assert( m1 == m2 )
255
+
256
+ for r in 0...2 do
257
+ for c in 0...2 do
258
+ assert_in_delta( 2*r+c, m2.getElement(r,c), @tolerance )
259
+ end
260
+ end
261
+
262
+ # RMtx2#-@
263
+ m2 = -m0
264
+ for r in 0...2 do
265
+ for c in 0...2 do
266
+ assert_in_delta( m0.getElement(r,c), -m2.getElement(r,c), @tolerance )
267
+ end
268
+ end
269
+ end
270
+
271
+ def test_binary_plus
272
+ m0 = RMtx2.new( 0, 1, 2, 3 )
273
+ m1 = RMtx2.new( 9,10,11,12 )
274
+ m2 = RMtx2.new( 9,11,13,15 )
275
+
276
+ # RMtx2#+
277
+ m3 = m0 + m1
278
+ for r in 0...2 do
279
+ for c in 0...2 do
280
+ assert_in_delta( m2.getElement(r,c), m3.getElement(r,c), @tolerance )
281
+ end
282
+ end
283
+
284
+ # RMtx2#add!
285
+ m0.add!( m1 )
286
+ for r in 0...2 do
287
+ for c in 0...2 do
288
+ assert_in_delta( m2.getElement(r,c), m0.getElement(r,c), @tolerance )
289
+ end
290
+ end
291
+ end
292
+
293
+ def test_binary_minus
294
+ m0 = RMtx2.new( 0, 1, 2, 3 )
295
+ m1 = RMtx2.new( 9,10,11,12 )
296
+ m2 = RMtx2.new(-9,-9,-9,-9 )
297
+
298
+ # RMtx2#-
299
+ m3 = m0 - m1
300
+ for r in 0...2 do
301
+ for c in 0...2 do
302
+ assert_in_delta( m2.getElement(r,c), m3.getElement(r,c), @tolerance )
303
+ end
304
+ end
305
+
306
+ # RMtx2#sub!
307
+ m0.sub!( m1 )
308
+ for r in 0...2 do
309
+ for c in 0...2 do
310
+ assert_in_delta( m2.getElement(r,c), m0.getElement(r,c), @tolerance )
311
+ end
312
+ end
313
+ end
314
+
315
+ def test_binary_mult
316
+ m0 = RMtx2.new( 0, 1, 2, 3 )
317
+ m1 = RMtx2.new( 9,10,11,12 )
318
+ m0x1 = RMtx2.new( 11, 12, 51, 56)
319
+ m1x0 = RMtx2.new( 20, 39, 24, 47)
320
+
321
+ # RMtx2#*
322
+ m2 = m0 * m1
323
+ for r in 0...2 do
324
+ for c in 0...2 do
325
+ assert_in_delta( m0x1.getElement(r,c), m2.getElement(r,c), @tolerance )
326
+ end
327
+ end
328
+
329
+ m2 = m1 * m0
330
+ for r in 0...2 do
331
+ for c in 0...2 do
332
+ assert_in_delta( m1x0.getElement(r,c), m2.getElement(r,c), @tolerance )
333
+ end
334
+ end
335
+
336
+ # RMtx2#mul!
337
+ m2 = RMtx2.new( m0 )
338
+ m2.mul!( m1 )
339
+ for r in 0...2 do
340
+ for c in 0...2 do
341
+ assert_in_delta( m0x1.getElement(r,c), m2.getElement(r,c), @tolerance )
342
+ end
343
+ end
344
+
345
+ m2 = RMtx2.new( m1 )
346
+ m2.mul!( m0 )
347
+ for r in 0...2 do
348
+ for c in 0...2 do
349
+ assert_in_delta( m1x0.getElement(r,c), m2.getElement(r,c), @tolerance )
350
+ end
351
+ end
352
+ end
353
+
354
+ def test_equality_operators
355
+ m0 = RMtx2.new( 0, 1, 2, 3 )
356
+ m1 = RMtx2.new( 0, 1, 2, 3 )
357
+ m2 = RMtx2.new( 9,10,11,12 )
358
+
359
+ assert( m0 == m1 )
360
+ assert( m0 != m2 )
361
+ end
362
+
363
+ end
@@ -526,7 +526,7 @@ class TC_RMtx4 < Minitest::Test
526
526
  0.0, 2*z_n/height, 0.0, 0.0,
527
527
  0.0, 0.0, -(z_f+z_n)/(z_f-z_n), -2.0*z_f*z_n / (z_f-z_n),
528
528
  0.0, 0.0, -1.0, 0.0 )
529
- m1 = RMtx4.new.perspectiveRH( width, height, z_n, z_f )
529
+ m1 = RMtx4.new.perspectiveRH( width, height, z_n, z_f, true )
530
530
 
531
531
  for r in 0...4 do
532
532
  for c in 0...4 do
@@ -544,7 +544,7 @@ class TC_RMtx4 < Minitest::Test
544
544
  0.0, f, 0.0, 0.0,
545
545
  0.0, 0.0, (z_f+z_n)/(z_n-z_f), 2*z_f*z_n/(z_n-z_f),
546
546
  0.0, 0.0, -1.0, 0.0 )
547
- m3 = RMtx4.new.perspectiveFovRH( fovy, aspect, z_n, z_f );
547
+ m3 = RMtx4.new.perspectiveFovRH( fovy, aspect, z_n, z_f, true );
548
548
 
549
549
  for r in 0...4 do
550
550
  for c in 0...4 do
@@ -563,7 +563,7 @@ class TC_RMtx4 < Minitest::Test
563
563
  0.0, 0.0, c, d,
564
564
  0.0, 0.0, -1.0, 0.0 )
565
565
 
566
- m5 = RMtx4.new.perspectiveOffCenterRH( left, right, bottom, top, z_n, z_f )
566
+ m5 = RMtx4.new.perspectiveOffCenterRH( left, right, bottom, top, z_n, z_f, true )
567
567
 
568
568
  for r in 0...4 do
569
569
  for c in 0...4 do
@@ -584,14 +584,14 @@ class TC_RMtx4 < Minitest::Test
584
584
  height = top - bottom
585
585
 
586
586
  # RMtx4#orthoRH
587
- tx = (right+left)/width
588
- ty = (top+bottom)/height
589
- tz = (z_f+z_n)/(z_f-z_n)
587
+ tx = -(right+left)/width
588
+ ty = -(top+bottom)/height
589
+ tz = -(z_f+z_n)/(z_f-z_n)
590
590
  m0 = RMtx4.new( 2.0/width, 0.0, 0.0, tx,
591
591
  0.0, 2.0/height, 0.0, ty,
592
592
  0.0, 0.0, -2.0/(z_f-z_n), tz,
593
593
  0.0, 0.0, 0.0, 1.0 )
594
- m1 = RMtx4.new.orthoRH( width, height, z_n, z_f )
594
+ m1 = RMtx4.new.orthoRH( width, height, z_n, z_f, true )
595
595
 
596
596
  for r in 0...4 do
597
597
  for c in 0...4 do
@@ -600,14 +600,14 @@ class TC_RMtx4 < Minitest::Test
600
600
  end
601
601
 
602
602
  # RMtx4#orthoOffCenterRH
603
- tx = (right+left)/(right-left)
604
- ty = (top+bottom)/(top-bottom)
605
- tz = (z_f+z_n)/(z_f-z_n)
603
+ tx = -(right+left)/(right-left)
604
+ ty = -(top+bottom)/(top-bottom)
605
+ tz = -(z_f+z_n)/(z_f-z_n)
606
606
  m2 = RMtx4.new( 2.0/(right-left), 0.0, 0.0, tx,
607
607
  0.0, 2.0/(top-bottom), 0.0, ty,
608
608
  0.0, 0.0, -2.0/(z_f-z_n), tz,
609
609
  0.0, 0.0, 0.0, 1.0 )
610
- m3 = RMtx4.new.orthoOffCenterRH( left, right, bottom, top, z_n, z_f )
610
+ m3 = RMtx4.new.orthoOffCenterRH( left, right, bottom, top, z_n, z_f, true )
611
611
 
612
612
  for r in 0...4 do
613
613
  for c in 0...4 do