geo3d 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ec800a7b43a09953bbbcb5ed8c56c556366e2f67
4
- data.tar.gz: 456e73c6c0a3897743b0f309d319e81593109bee
3
+ metadata.gz: d922bc02f451ded2c7a44b2b3b8aa96f0606b023
4
+ data.tar.gz: 9d02cbf3e7580833d546447d011e7f360a879727
5
5
  SHA512:
6
- metadata.gz: 5d5890e94efad1febb36e354dbb5dab3ce8383c11a335014d15b58677f572cbd12d4940150e0c0d2593e466a5703e4b734776a307e8bf3042a2a2977cbac3faf
7
- data.tar.gz: a8ece5e4e6942f37ea0ed6cb633a64228a4c6fff72c3ea5de2adab025221902fc0572975543bb1f54e4ceb9df3f6e2541223b83e2ff8b19e6da99968410e0470
6
+ metadata.gz: 09bc0f46c7f284253784270a80937bc41c7e610a0b869fee9d1333f86d5a1231b4d428f834ad4eb42645c26be71e15fb567cd397c102dabe33cde6ac5b0a57aa
7
+ data.tar.gz: 2c0a45d96a0824b1ed1e1b5be2e9e8fe6a0c7751c0f8081f69afbc09e34b8245c3299bfdce9106237377e2940933921fcc5d53b345d53dd8ab37657b8e02819b
data/README.md CHANGED
@@ -140,10 +140,22 @@ Scaling
140
140
  Geo3d::Matrix.scaling x,y,z #returns a scaling matrix
141
141
  Geo3d::Matrix.uniform_scaling scale #returns a uniform scaling matrix
142
142
  ```
143
+ Rotation
144
+ ```
145
+ Geo3d::Matrix.rotation_x 0.44 #rotate .44 radians about x axis
146
+ Geo3d::Matrix.rotation_y 0.44 #rotate .44 radians about y axis
147
+ Geo3d::Matrix.rotation_z 0.44 #rotate .44 radians about z axis
148
+
149
+ axis = Geo3d::Vector.new 1,1,0
150
+ angle = 0.9
151
+ Geo3d::Matrix.rotation axis, angle #rotate about an arbitrary axis
152
+ ```
143
153
  Projection matrix constructors
144
154
  ```
145
- Geo3d::Matrix.matrix_perspective_fov_rh fovy, aspect, z_near, z_far #returns a right handed perspective projection matrix
146
- Geo3d::Matrix.matrix_perspective_fov_lh fovy, aspect, z_near, z_far #returns a left handed perspective projection matrix
155
+ Geo3d::Matrix.perspective_fov_rh fovy, aspect, z_near, z_far #returns a right handed perspective projection matrix
156
+ Geo3d::Matrix.perspective_fov_lh fovy, aspect, z_near, z_far #returns a left handed perspective projection matrix
157
+ Geo3d::Matrix.ortho_off_center_rh left, right, bottom, top, z_near, z_far #returns a right handed orthographic projection matrix
158
+ Geo3d::Matrix.ortho_off_center_lh left, right, bottom, top, z_near, z_far #returns a left handed orthographic projection matrix
147
159
  ```
148
160
  View matrix constructors
149
161
  ```
@@ -156,6 +168,10 @@ Misc constructors
156
168
  ```
157
169
 
158
170
 
171
+ ## Quaternion
172
+
173
+ Documentation coming soon!
174
+
159
175
 
160
176
 
161
177
 
@@ -1,3 +1,3 @@
1
1
  module Geo3d
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -321,7 +321,30 @@ module Geo3d
321
321
  transposed_matrix
322
322
  end
323
323
 
324
- def self.matrix_perspective_fov_rh fovy, aspect, zn, zf
324
+ def print
325
+ puts "_11: #{_11}\n"
326
+ puts "_12: #{_12}\n"
327
+ puts "_13: #{_13}\n"
328
+ puts "_14: #{_14}\n"
329
+ puts "_21: #{_21}\n"
330
+ puts "_22: #{_22}\n"
331
+ puts "_23: #{_23}\n"
332
+ puts "_24: #{_24}\n"
333
+ puts "_31: #{_31}\n"
334
+ puts "_32: #{_32}\n"
335
+ puts "_33: #{_33}\n"
336
+ puts "_34: #{_34}\n"
337
+ puts "_41: #{_41}\n"
338
+ puts "_42: #{_42}\n"
339
+ puts "_43: #{_43}\n"
340
+ puts "_44: #{_44}\n"
341
+ end
342
+
343
+ def self.perspective_fov_rh fovy, aspect, zn, zf
344
+ fovy = fovy.to_f
345
+ aspect = aspect.to_f
346
+ zn = zn.to_f
347
+ zf = zf.to_f
325
348
  y_scale = 1.0 / Math.tan(0.5*fovy)
326
349
  x_scale = y_scale / aspect
327
350
  matrix = Matrix.new
@@ -333,18 +356,56 @@ module Geo3d
333
356
  matrix
334
357
  end
335
358
 
336
- def self.matrix_perspective_fov_lh fovy, aspect, zn, zf
359
+ def self.perspective_fov_lh fovy, aspect, zn, zf
360
+ fovy = fovy.to_f
361
+ aspect = aspect.to_f
362
+ zn = zn.to_f
363
+ zf = zf.to_f
337
364
  y_scale = 1.0 / Math.tan(0.5*fovy)
338
365
  x_scale = y_scale / aspect
339
366
  matrix = Matrix.new
340
367
  matrix._11 = x_scale
341
368
  matrix._22 = y_scale
342
- matrix._33 = zf/(zn - zf)
369
+ matrix._33 = zf/(zf - zn)
343
370
  matrix._34 = 1
344
- matrix._43 = -zn*zf/(zn - zf)
371
+ matrix._43 = zn*zf/(zn - zf)
345
372
  matrix
346
373
  end
347
374
 
375
+ def self.ortho_off_center_rh l, r, b, t, zn, zf
376
+ l = l.to_f
377
+ r = r.to_f
378
+ b = b.to_f
379
+ t = t.to_f
380
+ zn = zn.to_f
381
+ zf = zf.to_f
382
+ m = identity
383
+ m._11 = 2.0 / (r - l)
384
+ m._22 = 2.0 / (t - b)
385
+ m._33 = 1.0 / (zn -zf)
386
+ m._41 = -1.0 -2.0 *l / (r - l)
387
+ m._42 = 1.0 + 2.0 * t / (b - t)
388
+ m._43 = zn / (zn -zf)
389
+ m
390
+ end
391
+
392
+ def self.ortho_off_center_lh l, r, b, t, zn, zf
393
+ l = l.to_f
394
+ r = r.to_f
395
+ b = b.to_f
396
+ t = t.to_f
397
+ zn = zn.to_f
398
+ zf = zf.to_f
399
+ m = identity
400
+ m._11 = 2.0 / (r - l)
401
+ m._22 = 2.0 / (t - b)
402
+ m._33 = 1.0 / (zf -zn)
403
+ m._41 = -1.0 -2.0 *l / (r - l)
404
+ m._42 = 1.0 + 2.0 * t / (b - t)
405
+ m._43 = zn / (zn -zf)
406
+ m
407
+ end
408
+
348
409
  def self.look_at_rh eye_position, look_at_position, up_direction
349
410
  zaxis = (eye_position - look_at_position).normalize
350
411
  xaxis = up_direction.cross(zaxis).normalize
@@ -442,6 +503,33 @@ module Geo3d
442
503
  reflection_matrix
443
504
  end
444
505
 
506
+ def self.shadow light_position, plane
507
+ norm = plane.x * plane.x + plane.y * plane.y + plane.z * plane.z
508
+ normalized_plane = plane / norm
509
+ dot = normalized_plane.dot(light_position)
510
+
511
+ m = Matrix.new
512
+ m._11 = dot - normalized_plane.a * light_position.x
513
+ m._12 = -normalized_plane.a * light_position.y
514
+ m._13 = -normalized_plane.a * light_position.z
515
+ m._14 = -normalized_plane.a * light_position.w
516
+ m._21 = -normalized_plane.b * light_position.x
517
+ m._22 = dot - normalized_plane.b * light_position.y
518
+ m._23 = -normalized_plane.b * light_position.z
519
+ m._24 = -normalized_plane.b * light_position.w
520
+ m._31 = -normalized_plane.c * light_position.x
521
+ m._32 = -normalized_plane.c * light_position.y
522
+ m._33 = dot - normalized_plane.c * light_position.z
523
+ m._34 = -normalized_plane.c * light_position.w
524
+ m._41 = -normalized_plane.d * light_position.x
525
+ m._42 = -normalized_plane.d * light_position.y
526
+ m._43 = -normalized_plane.d * light_position.z
527
+ m._44 = dot - normalized_plane.d * light_position.w
528
+
529
+ m
530
+ end
531
+
532
+
445
533
  def self.translation x, y, z
446
534
  translation_matrix = Matrix.new
447
535
  translation_matrix._11 = translation_matrix._22 = translation_matrix._33 = translation_matrix._44 = 1
@@ -465,23 +553,52 @@ module Geo3d
465
553
  scaling scale, scale, scale
466
554
  end
467
555
 
468
- def self.rotation_y_rh angle
556
+ def self.rotation_x angle
557
+ m = identity
469
558
  sine = Math.sin angle
470
559
  cosine = Math.cos angle
471
- rotation_matrix = Matrix.new
472
- rotation_matrix._11 = cosine
473
- rotation_matrix._12 = 0
474
- rotation_matrix._13 = sine
475
- rotation_matrix._14 = 0
476
- rotation_matrix._21 = rotation_matrix._23 = rotation_matrix._24 = 0
477
- rotation_matrix._22 = 1
478
- rotation_matrix._31 = -sine
479
- rotation_matrix._32 = 0
480
- rotation_matrix._33 = cosine
481
- rotation_matrix._34 = 0
482
- rotation_matrix._41 = rotation_matrix._42 = rotation_matrix._43 = 0
483
- rotation_matrix._44 = 1
484
- rotation_matrix
560
+ m._22 = cosine
561
+ m._33 = cosine
562
+ m._23 = sine
563
+ m._32 = -sine
564
+ m
565
+ end
566
+
567
+ def self.rotation_y angle
568
+ m = identity
569
+ sine = Math.sin angle
570
+ cosine = Math.cos angle
571
+ m._11 = cosine
572
+ m._33 = cosine
573
+ m._13 = -sine
574
+ m._31 = sine
575
+ m
576
+ end
577
+
578
+ def self.rotation_z angle
579
+ m = identity
580
+ sine = Math.sin angle
581
+ cosine = Math.cos angle
582
+ m._11 = cosine
583
+ m._22 = cosine
584
+ m._12 = sine
585
+ m._21 = -sine
586
+ m
587
+ end
588
+
589
+ def self.rotation axis, angle
590
+ v = axis.normalize
591
+ m = identity
592
+ m._11 = (1.0 - Math.cos(angle)) * v.x * v.x + Math.cos(angle)
593
+ m._21 = (1.0 - Math.cos(angle)) * v.x * v.y - Math.sin(angle) * v.z
594
+ m._31 = (1.0 - Math.cos(angle)) * v.x * v.z + Math.sin(angle) * v.y
595
+ m._12 = (1.0 - Math.cos(angle)) * v.y * v.x + Math.sin(angle) * v.z
596
+ m._22 = (1.0 - Math.cos(angle)) * v.y * v.y + Math.cos(angle)
597
+ m._32 = (1.0 - Math.cos(angle)) * v.y * v.z - Math.sin(angle) * v.x
598
+ m._13 = (1.0 - Math.cos(angle)) * v.z * v.x - Math.sin(angle) * v.y
599
+ m._23 = (1.0 - Math.cos(angle)) * v.z * v.y + Math.sin(angle) * v.x
600
+ m._33 = (1.0 - Math.cos(angle)) * v.z * v.z + Math.cos(angle)
601
+ m
485
602
  end
486
603
  end
487
604
  end
@@ -1,13 +1,17 @@
1
1
  module Geo3d
2
2
  class Vector
3
3
  attr_accessor :x, :y, :z, :w
4
+ alias :a :x
5
+ alias :b :y
6
+ alias :c :z
7
+ alias :d :w
4
8
 
5
9
  def initialize *args
6
10
  @x, @y, @z, @w = 0, 0, 0, 0
7
- @x = args[0] if args.size > 0
8
- @y = args[1] if args.size > 1
9
- @z = args[2] if args.size > 2
10
- @w = args[3] if args.size > 3
11
+ @x = args[0].to_f if args.size > 0
12
+ @y = args[1].to_f if args.size > 1
13
+ @z = args[2].to_f if args.size > 2
14
+ @w = args[3].to_f if args.size > 3
11
15
  end
12
16
 
13
17
  def to_s
@@ -55,7 +59,7 @@ module Geo3d
55
59
  end
56
60
 
57
61
  def dot vec
58
- x * vec.x + y * vec.y + z * vec.z
62
+ x * vec.x + y * vec.y + z * vec.z + w * vec.w
59
63
  end
60
64
 
61
65
  def normalize!
@@ -64,11 +68,12 @@ module Geo3d
64
68
  @x /= len
65
69
  @y /= len
66
70
  @z /= len
71
+ @w /= len
67
72
  end
68
73
  end
69
74
 
70
75
  def normalize
71
- v = self.class.new x, y, z
76
+ v = self.class.new x, y, z, w
72
77
  v.normalize!
73
78
  v
74
79
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geo3d
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Misha Conway
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-10 00:00:00.000000000 Z
11
+ date: 2014-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler