blackbook3d 0.5.0

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.
Files changed (134) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +341 -0
  3. data/README.md +92 -0
  4. data/SETUP.md +70 -0
  5. data/data/assets/Texture/A011a.tga +0 -0
  6. data/data/assets/untitled.awp3d +0 -0
  7. data/data/cube.mtl +12 -0
  8. data/data/cube.obj +246 -0
  9. data/data/cube.raw +12 -0
  10. data/data/engine_conf.json +9 -0
  11. data/data/fighter.raw +19198 -0
  12. data/data/font/100.raw +78 -0
  13. data/data/font/101.raw +99 -0
  14. data/data/font/102.raw +58 -0
  15. data/data/font/103.raw +258 -0
  16. data/data/font/104.raw +55 -0
  17. data/data/font/105.raw +48 -0
  18. data/data/font/106.raw +72 -0
  19. data/data/font/107.raw +6 -0
  20. data/data/font/108.raw +2 -0
  21. data/data/font/109.raw +105 -0
  22. data/data/font/110.raw +55 -0
  23. data/data/font/111.raw +96 -0
  24. data/data/font/112.raw +78 -0
  25. data/data/font/113.raw +78 -0
  26. data/data/font/114.raw +53 -0
  27. data/data/font/115.raw +132 -0
  28. data/data/font/116.raw +56 -0
  29. data/data/font/117.raw +54 -0
  30. data/data/font/118.raw +5 -0
  31. data/data/font/119.raw +11 -0
  32. data/data/font/120.raw +10 -0
  33. data/data/font/121.raw +6 -0
  34. data/data/font/122.raw +6 -0
  35. data/data/font/33.raw +118 -0
  36. data/data/font/37.raw +194 -0
  37. data/data/font/38.raw +200 -0
  38. data/data/font/39.raw +4 -0
  39. data/data/font/40.raw +48 -0
  40. data/data/font/41.raw +48 -0
  41. data/data/font/42.raw +226 -0
  42. data/data/font/43.raw +10 -0
  43. data/data/font/45.raw +2 -0
  44. data/data/font/46.raw +46 -0
  45. data/data/font/47.raw +2 -0
  46. data/data/font/48.raw +96 -0
  47. data/data/font/49.raw +2 -0
  48. data/data/font/50.raw +87 -0
  49. data/data/font/51.raw +145 -0
  50. data/data/font/52.raw +15 -0
  51. data/data/font/53.raw +76 -0
  52. data/data/font/54.raw +121 -0
  53. data/data/font/55.raw +4 -0
  54. data/data/font/56.raw +194 -0
  55. data/data/font/57.raw +133 -0
  56. data/data/font/61.raw +4 -0
  57. data/data/font/63.raw +117 -0
  58. data/data/font/65.raw +11 -0
  59. data/data/font/66.raw +108 -0
  60. data/data/font/67.raw +96 -0
  61. data/data/font/68.raw +54 -0
  62. data/data/font/69.raw +10 -0
  63. data/data/font/70.raw +8 -0
  64. data/data/font/71.raw +100 -0
  65. data/data/font/72.raw +10 -0
  66. data/data/font/73.raw +2 -0
  67. data/data/font/74.raw +39 -0
  68. data/data/font/75.raw +9 -0
  69. data/data/font/76.raw +4 -0
  70. data/data/font/77.raw +13 -0
  71. data/data/font/78.raw +8 -0
  72. data/data/font/79.raw +96 -0
  73. data/data/font/80.raw +56 -0
  74. data/data/font/81.raw +124 -0
  75. data/data/font/82.raw +94 -0
  76. data/data/font/83.raw +120 -0
  77. data/data/font/84.raw +6 -0
  78. data/data/font/85.raw +76 -0
  79. data/data/font/86.raw +5 -0
  80. data/data/font/87.raw +11 -0
  81. data/data/font/88.raw +10 -0
  82. data/data/font/89.raw +7 -0
  83. data/data/font/90.raw +6 -0
  84. data/data/font/91.raw +6 -0
  85. data/data/font/93.raw +6 -0
  86. data/data/font/94.raw +5 -0
  87. data/data/font/95.raw +2 -0
  88. data/data/font/97.raw +174 -0
  89. data/data/font/98.raw +100 -0
  90. data/data/font/99.raw +96 -0
  91. data/data/ground.raw +12 -0
  92. data/data/man.mtl +12 -0
  93. data/data/man.obj +7547 -0
  94. data/data/robo.mtl +11 -0
  95. data/data/robo.obj +42065 -0
  96. data/data/space.json +49 -0
  97. data/data/sphere.raw +1216 -0
  98. data/data/texture/Charcoal-2.jpg +0 -0
  99. data/data/texture/man.png +0 -0
  100. data/data/texture/man.tif +0 -0
  101. data/data/texture/t.jpg +0 -0
  102. data/data/texture/wood.jpg +0 -0
  103. data/data/texture/wood.png +0 -0
  104. data/data/texture/x.jpg +0 -0
  105. data/data/untitled.raw +124 -0
  106. data/data/w.raw +12474 -0
  107. data/data/wings.raw +45670 -0
  108. data/lib/BlackBook/anim.rb +184 -0
  109. data/lib/BlackBook/b3dobject.rb +475 -0
  110. data/lib/BlackBook/base.rb +79 -0
  111. data/lib/BlackBook/camera.rb +202 -0
  112. data/lib/BlackBook/color.rb +29 -0
  113. data/lib/BlackBook/constants.rb +327 -0
  114. data/lib/BlackBook/engine.rb +172 -0
  115. data/lib/BlackBook/functions.rb +650 -0
  116. data/lib/BlackBook/light.rb +91 -0
  117. data/lib/BlackBook/logger.rb +86 -0
  118. data/lib/BlackBook/material.rb +98 -0
  119. data/lib/BlackBook/registry.rb +71 -0
  120. data/lib/BlackBook/space.rb +396 -0
  121. data/lib/BlackBook/stime.rb +64 -0
  122. data/lib/Plugins/string_color.rb +74 -0
  123. data/lib/blackbook/version.rb +5 -0
  124. data/lib/blackbook.rb +52 -0
  125. data/lib/plugins.rb +32 -0
  126. data/lib/ui/button.rb +115 -0
  127. data/lib/ui/edit.rb +105 -0
  128. data/lib/ui/text.rb +284 -0
  129. data/lib/ui/ui.rb +88 -0
  130. data/lib/ui/ui_element.rb +45 -0
  131. data/lib/ui/window.rb +138 -0
  132. data/lib/ui.rb +30 -0
  133. data/lib/zipfile.rb +27 -0
  134. metadata +254 -0
@@ -0,0 +1,79 @@
1
+ ##############################################################################
2
+ # BlackBook 3D Engine
3
+ # Copyright (C) 2015 Sinan ISLEKDEMIR
4
+ #
5
+ # This program is free software; you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation; either version 2 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License along
16
+ # with this program; if not, write to the Free Software Foundation, Inc.,
17
+ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
+ ##############################################################################
19
+
20
+ ################################################################
21
+ # Project BlackBook
22
+ # Lead Engineer: Sinan ISLEKDEMIR
23
+ # Simulation Engine Ruby Sources
24
+ ################################################################
25
+
26
+ require 'BlackBook/logger'
27
+ require 'BlackBook/registry'
28
+
29
+ $FOLDER = []
30
+
31
+ def dir_make(name)
32
+ unless File.directory?(name)
33
+ Dir.mkdir(name)
34
+ return 1, name # create ok
35
+ else
36
+ return 2, name #already exists
37
+ end
38
+ rescue Exception=>e
39
+ return 0, name
40
+ end
41
+
42
+ def dir_del(dir_path)
43
+ if File.directory?(dir_path)
44
+ Dir.foreach(dir_path) do |sub_file|
45
+ dir_del(File.join(dir_path, sub_file)) unless ['.', '..'].include? sub_file
46
+ end
47
+ Dir.rmdir(dir_path)
48
+ else
49
+ File.delete(dir_path)
50
+ end
51
+ return 1
52
+ rescue Exception => e
53
+ return 0
54
+ end
55
+
56
+ def folder_gen() # year-mon-day-min-sec-nsec
57
+ t = Time.now
58
+ tmp = ENV["temp"]
59
+ ss = t.year.to_s
60
+ [t.month, t.day, t.hour, t.min, t.sec, t.nsec].map do |opt| ss += '-' + opt.to_s end
61
+ return tmp + "/#{ss}"
62
+ end
63
+
64
+ def clear_temp()
65
+ $FOLDER.each { |f| dir_del f }
66
+ end
67
+
68
+ module BlackBook
69
+ # BlackBook Base Class
70
+ class Base
71
+ attr_accessor :logger, :registry
72
+
73
+ def initialize(*_args)
74
+ logging_params = { filename: 'debug.log', stdout: true }
75
+ Logger.instance.define logging_params
76
+ @registry = Registry.instance
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,202 @@
1
+ ##############################################################################
2
+ # BlackBook 3D Engine
3
+ # Copyright (C) 2015 Sinan ISLEKDEMIR
4
+ #
5
+ # This program is free software; you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation; either version 2 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License along
16
+ # with this program; if not, write to the Free Software Foundation, Inc.,
17
+ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
+ ##############################################################################
19
+
20
+ ################################################################
21
+ # Project BlackBook
22
+ # Lead Engineer: Sinan ISLEKDEMIR
23
+ # Simulation Engine Ruby Sources
24
+ ################################################################
25
+
26
+ require 'opengl'
27
+ require 'pp'
28
+
29
+ # Local Libs
30
+ require 'BlackBook/base'
31
+ require 'BlackBook/functions'
32
+
33
+ module BlackBook
34
+ #
35
+ # Camera Class
36
+ # @attr eye_position [CVector] Eye - Camera position
37
+ # @attr target [CVector] Camera target
38
+ # @attr up_vector [CVector] Camera Up Vector
39
+ # @attr frame_x [Integer] Camera Frame X Location
40
+ # @attr frame_y [Integer] Camera Frame Y Location
41
+ # @attr frame_width [Integer] Camera Frame Width
42
+ # @attr frame_height [Integer] Camera Frame Height
43
+ # @attr fov [Float] Camera Field Of View
44
+ # @attr znear [Float] Near Plane Distance
45
+ # @attr zfar [Float] Far Plane Distance
46
+ # @attr on_update [Array] [0] -> Lambda | [1] -> Variable Array
47
+ # Your Lambda function MUST include 4 arguements like;
48
+ # on_update = -> (camera, x, y, extra_array_to_pass) { ... }
49
+ #
50
+ # @author [sinan islekdemir]
51
+ #
52
+ class Camera < Base
53
+ attr_accessor :eye_position, :target, :up_vector, :frame_x, :frame_y,
54
+ :frame_width, :frame_height, :fov, :znear, :zfar, :on_update
55
+
56
+ # options -> :eye_position, :target_position, :up, :fov, :znear, :zfar
57
+ # :frame_x, :frame_y, :frame_width, :frame_height
58
+
59
+ #
60
+ # Initialize Camera
61
+ # @param options [Hash]
62
+ # :eye_position
63
+ # :target
64
+ # :up_vector
65
+ # :frame_x
66
+ # :frame_y
67
+ # :frame_width
68
+ # :frame_height
69
+ # :fov
70
+ # :znear
71
+ # :zfar
72
+ # :on_update
73
+ #
74
+ # @return [type] [description]
75
+ def initialize( opts = {} )
76
+ super
77
+ @eye_position = CVector.new(
78
+ opts[:eye_position].x,
79
+ opts[:eye_position].y,
80
+ opts[:eye_position].z
81
+ )
82
+ @target = CVector.new(
83
+ opts[:target_position].x,
84
+ opts[:target_position].y,
85
+ opts[:target_position].z
86
+ )
87
+ @up_vector = CVector.new(
88
+ opts[:up].x,
89
+ opts[:up].y,
90
+ opts[:up].z
91
+ )
92
+ @on_update = nil
93
+ @frame_x = opts[:frame_x] || 0
94
+ @frame_y = opts[:frame_y] || 0
95
+ @frame_width = opts[:frame_width] || 0
96
+ @frame_height = opts[:frame_height] || 0
97
+ @fov = opts[:fov] || 30.0
98
+ @znear = opts[:znear] || 1.0
99
+ @zfar = opts[:zfar] || 1000.0
100
+ @modified = false
101
+ end
102
+
103
+ #
104
+ # Turn screen coordinates to world coordinates
105
+ # @param x [Integer] Cursor X
106
+ # @param y [Integer] Cursor Y
107
+ #
108
+ # @return [CVector] World position
109
+ def screen_to_world(x, y)
110
+ mv = GL.GetFloatv GL::MODELVIEW_MATRIX
111
+ pm = GL.GetFloatv GL::PROJECTION_MATRIX
112
+ vp = GL.GetFloatv GL::VIEWPORT
113
+ y = vp[3] - y
114
+ z = GL.ReadPixels(x, y, 1, 1, GL::DEPTH_COMPONENT, GL::FLOAT)
115
+ z = z.unpack('f')[0]
116
+ glh_unprojectf(x, y, z, mv, pm, vp)
117
+ end
118
+
119
+ #
120
+ # World to screen coordinates
121
+ # @param v [CVector] World coordinates
122
+ #
123
+ # @return [Array] Array of screen coordinates
124
+ def world_to_screen(v)
125
+ mv = GL.GetFloatv GL::MODELVIEW_MATRIX
126
+ pm = GL.GetFloatv GL::PROJECTION_MATRIX
127
+ vp = GL.GetFloatv GL::VIEWPORT
128
+ BlackBook.glh_project_f(v, mv, pm, vp)
129
+ end
130
+
131
+ #
132
+ # Is camera framed?
133
+ #
134
+ # @return [Boolean] Is camera framed?
135
+ def framed
136
+ (@frame_width > 0 && @frame_height > 0)
137
+ end
138
+
139
+ #
140
+ # Begin rendering
141
+ #
142
+ # @return [Boolean] Success
143
+ def begin_render
144
+ if @frame_width > 0 && @frame_height > 0
145
+ GL.PushAttrib(GL::SCISSOR_BIT)
146
+ GL.Enable(GL::SCISSOR_TEST)
147
+ GL.Scissor(@frame_x, @frame_y, @frame_width, @frame_height)
148
+ GL.Viewport(@frame_x, @frame_y, @frame_width, @frame_height)
149
+ BlackBook.perspective(
150
+ 30.0, @frame_width.to_f / @frame_height.to_f, 1.0, 1000.0
151
+ )
152
+ end
153
+ GL.LoadIdentity
154
+ BlackBook.look_at(
155
+ @eye_position,
156
+ @target,
157
+ @up_vector)
158
+ true
159
+ end
160
+
161
+ #
162
+ # End Rendering
163
+ #
164
+ # @return [Boolean] Success
165
+ def end_render
166
+ if @modified && !@on_update.nil?
167
+ lambda = @on_update[0]
168
+ variable = @on_update[1]
169
+ lambda.call(self, @pos_x, @pos_y, variable)
170
+ @modified = false
171
+ end
172
+ return false until framed
173
+ GL.Disable(GL::SCISSOR_TEST)
174
+ GL.PopAttrib
175
+ return true
176
+ end
177
+
178
+ #
179
+ # Mouse event handler
180
+ # @param x [Integer] Cursor X Position
181
+ # @param y [Integer] Cursor Y Position
182
+ # @param right_b [Integer] Right Mouse Button
183
+ # @param left_b [Integer] Left Mouse Button
184
+ # @param middle_b [Integer] Middle Mouse Button
185
+ #
186
+ def mouse_move(x, y, _right_b, left_b, _middle_b)
187
+ @pos_x, @pos_y = x, y if @pos_x.nil?
188
+ if left_b == 1
189
+ diff = @eye_position.sub(@target)
190
+ diff.update_spherical
191
+ diff.phi = diff.phi - BlackBook.deg_to_rad((x.to_f - @pos_x.to_f).to_f)
192
+ diff.theta = diff.theta - BlackBook.deg_to_rad(
193
+ (y.to_f - @pos_y.to_f).to_f
194
+ )
195
+ diff.update_cartesian
196
+ @eye_position = @target.add(diff)
197
+ @modified = true
198
+ end
199
+ @pos_x, @pos_y = x, y
200
+ end
201
+ end
202
+ end
@@ -0,0 +1,29 @@
1
+ ##############################################################################
2
+ # BlackBook 3D Engine
3
+ # Copyright (C) 2015 Sinan ISLEKDEMIR
4
+ #
5
+ # This program is free software; you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation; either version 2 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License along
16
+ # with this program; if not, write to the Free Software Foundation, Inc.,
17
+ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
+ ##############################################################################
19
+
20
+ require 'BlackBook/constants'
21
+
22
+ # Some standard colors
23
+ module BlackBook
24
+ BLACK = CVector.new(0.0, 0.0, 0.0, 1.0)
25
+ WHITE = CVector.new(1.0, 1.0, 1.0, 1.0)
26
+ RED = CVector.new(1.0, 0.0, 0.0, 1.0)
27
+ GREEN = CVector.new(0.0, 1.0, 0.0, 1.0)
28
+ BLUE = CVector.new(0.0, 0.0, 1.0, 1.0)
29
+ end
@@ -0,0 +1,327 @@
1
+ ##############################################################################
2
+ # BlackBook 3D Engine
3
+ # Copyright (C) 2015 Sinan ISLEKDEMIR
4
+ #
5
+ # This program is free software; you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation; either version 2 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License along
16
+ # with this program; if not, write to the Free Software Foundation, Inc.,
17
+ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
+ ##############################################################################
19
+
20
+ ################################################################
21
+ # Project BlackBook
22
+ # Lead Engineer: Sinan ISLEKDEMIR
23
+ # Simulation Engine Ruby Sources
24
+ ################################################################
25
+
26
+ require 'BlackBook/base'
27
+ require 'BlackBook/functions'
28
+
29
+ module BlackBook
30
+ #
31
+ # CVector Class (From Dust Engine by Sinan ISLEKDEMIR)
32
+ #
33
+ # @author [sinan islekdemir]
34
+ #
35
+ # @attr x [Float] Vector - X magnitude
36
+ # @attr y [Float] Vector - Y magnitude
37
+ # @attr z [Float] Vector - Z magnitude
38
+ # @attr w [Float] Vector - W
39
+ # @attr phi [Float] Phi - Spherical Coordinates
40
+ # @attr theta [Float] Theta - Spherical Coordinates
41
+ # @attr r [Float] Radius - Spherical Coordinates
42
+ class CVector < Base
43
+ # phi dikey, theta yatay aci| r orijine mesafe - yaricap
44
+ attr_accessor :x, :y, :z, :w, :phi, :theta, :r
45
+
46
+ #
47
+ # Initialize Vector
48
+ # @param x [Float] X
49
+ # @param y [Float] Y
50
+ # @param z [Float] Z
51
+ # @param w = 1 [Float] W
52
+ #
53
+ # @return [Boolean] Success
54
+ def initialize(x, y, z, w = 1)
55
+ super
56
+ @x, @y, @z, @w = x, y, z, w
57
+ update_spherical
58
+ return true
59
+ end
60
+
61
+ #
62
+ # Scalar Multiply Vector with a factor float
63
+ # @param factor [Float] Multiplication Float
64
+ def multiply(factor)
65
+ @x *= factor
66
+ @y *= factor
67
+ @z *= factor
68
+ end
69
+
70
+ #
71
+ # Scalar Division Vector with a factor float
72
+ # @param [Float] Division Float
73
+ def div(factor)
74
+ @x /= factor
75
+ @y /= factor
76
+ @z /= factor
77
+ end
78
+
79
+ #
80
+ # Set Vector
81
+ # @param x [Float] X Magnitude
82
+ # @param y [Float] Y Magnitude
83
+ # @param z [Float] Z Magnitude
84
+ # @param w = 1 [Float] W Magnitude
85
+ #
86
+ # @return [CVector] Self
87
+ def set(x, y, z, w = 1)
88
+ @x, @y, @z, @w = x, y, z, w
89
+ end
90
+
91
+ # Set vector from array
92
+ # @param arr [Array] set of elements
93
+ def from_array(arr = [])
94
+ @x = arr[0] if arr[0]
95
+ @y = arr[1] if arr[1]
96
+ @z = arr[2] if arr[2]
97
+ @w = arr[3] if arr[3]
98
+ end
99
+
100
+ #
101
+ # Update Spherical Coordinates
102
+ #
103
+ # @return [Boolean] Success
104
+ def update_spherical
105
+ @r = length
106
+ @theta = Math.acos(@z / r)
107
+ @phi = Math.atan2(@y, @x)
108
+ return true
109
+ end
110
+
111
+ #
112
+ # Update Cartesian Coordinates
113
+ #
114
+ # @return [Boolean] Success
115
+ def update_cartesian
116
+ @x = @r * Math.sin(@theta) * Math.cos(@phi)
117
+ @y = @r * Math.sin(@theta) * Math.sin(@phi)
118
+ @z = @r * Math.cos(@theta)
119
+ return true
120
+ end
121
+
122
+ #
123
+ # Normalize Vector
124
+ #
125
+ # @return [Boolean] Success
126
+ def normalize
127
+ d = length
128
+ d *= -1 if d.negative?
129
+ @x /= d
130
+ @y /= d
131
+ @z /= d
132
+ return self
133
+ end
134
+
135
+ #
136
+ # Calculate Vector Length
137
+ #
138
+ # @return [Float] Length of vector
139
+ def length
140
+ Math.sqrt((@x * @x) + (@y * @y) + (@z * @z))
141
+ end
142
+
143
+ #
144
+ # Vector Cross Product
145
+ # @param vector [CVector] Multiplication Vector
146
+ #
147
+ # @return [CVector] Product Vector
148
+ def cross(vector)
149
+ CVector.new(
150
+ @y * vector.z - @z * vector.y,
151
+ @z * vector.x - @x * vector.z,
152
+ @x * vector.y - @y * vector.x,
153
+ 0
154
+ )
155
+ end
156
+
157
+ #
158
+ # Vector Dot Product
159
+ # @param vector [CVector] Multiplication Vector
160
+ #
161
+ # @return [Float] Dot Product
162
+ def dot(vector)
163
+ @x * vector.x + @y * vector.y + @z * vector.z + @w * vector.w
164
+ end
165
+
166
+ #
167
+ # Distance between two vectors
168
+ # @param vector [CVector] Target Vector
169
+ #
170
+ # @return [Float] Distance
171
+ def distance(vector)
172
+ sub(vector).length
173
+ end
174
+
175
+ #
176
+ # Rotate Vector around X Axis
177
+ # @param angle [Float] Angle in Degrees
178
+ #
179
+ # @return [Boolean] Success
180
+ def rotate_around_x(angle)
181
+ angle = angle * Math::PI / 180
182
+ s = Math.sin(angle)
183
+ c = Math.cos(angle)
184
+ @y = c * @y + s * @z
185
+ @z = c * @z - s * @y
186
+ return true
187
+ end
188
+
189
+ #
190
+ # Rotate Vector around Y Axis
191
+ # @param angle [Float] Angle in Degrees
192
+ #
193
+ # @return [Float] Success
194
+ def rotate_around_y(angle)
195
+ angle = angle * Math::PI / 180
196
+ s = Math.sin(angle)
197
+ c = Math.cos(angle)
198
+ @x = c * @x + s * @z
199
+ @z = c * @z - s * @x
200
+ return true
201
+ end
202
+
203
+ #
204
+ # Rotate Vector around Z Axis
205
+ # @param angle [Float] Angle in Degrees
206
+ #
207
+ # @return [Float] Success
208
+ def rotate_around_z(angle)
209
+ angle *= Math::PI / 180
210
+ s = Math.sin(angle)
211
+ c = Math.cos(angle)
212
+ @x = c * @x + s * @y
213
+ @y = c * @y - s * @x
214
+ return true
215
+ end
216
+
217
+ #
218
+ # Vector Subtraction
219
+ # @param v [CVector] Target Vector
220
+ #
221
+ # @return [CVector] Result
222
+ def sub(v)
223
+ CVector.new(@x - v.x, @y - v.y, @z - v.z, @w)
224
+ end
225
+
226
+ #
227
+ # Vector Addition
228
+ # @param v [CVector] Addition Vector
229
+ #
230
+ # @return [CVector] Result
231
+ def add(v)
232
+ CVector.new(@x + v.x, @y + v.y, @z + v.z, @w)
233
+ end
234
+
235
+ #
236
+ # Convert CVector into one dimensional array
237
+ #
238
+ # @return [type] [description]
239
+ def to_array
240
+ [@x, @y, @z, @w]
241
+ end
242
+
243
+ #
244
+ # Convert CVector into string
245
+ # @return [String] Array
246
+ def to_s
247
+ to_array.to_s
248
+ end
249
+ end
250
+
251
+ # CMatrix Class
252
+ class CMatrix
253
+ attr_accessor :left, :dir, :up, :pos
254
+
255
+ # initialize
256
+ def initialize
257
+ @left = CVector.new(1.0, 0.0, 0.0, 0.0)
258
+ @dir = CVector.new(0.0, 1.0, 0.0, 0.0)
259
+ @up = CVector.new(0.0, 0.0, 1.0, 0.0)
260
+ @pos = CVector.new(0.0, 0.0, 0.0, 1.0)
261
+ end
262
+
263
+ def to_array
264
+ m = [@left.to_array, @dir.to_array, @up.to_array, @pos.to_array]
265
+ m.flatten
266
+ end
267
+
268
+ def to_f
269
+ [@left.to_array, @dir.to_array, @up.to_array, @pos.to_array]
270
+ end
271
+
272
+ def rotate(x, y, z)
273
+ m = to_array
274
+ x = BlackBook.deg_to_rad x
275
+ y = BlackBook.deg_to_rad y
276
+ z = BlackBook.deg_to_rad z
277
+ m[12] = 0.0
278
+ m[13] = 0.0
279
+ m[14] = 0.0
280
+ unified = [
281
+ 1.0, 0.0, 0.0, 0.0,
282
+ 0.0, 1.0, 0.0, 0.0,
283
+ 0.0, 0.0, 1.0, 0.0,
284
+ 0.0, 0.0, 0.0, 1.0
285
+ ]
286
+ x_matrix = unified.clone
287
+ x_matrix[5] = Math.cos y
288
+ x_matrix[6] = -1 * Math.sin(y)
289
+ x_matrix[9] = Math.sin y
290
+ x_matrix[10] = Math.cos y
291
+ y_matrix = unified.clone
292
+ y_matrix[0] = Math.cos x
293
+ y_matrix[2] = Math.sin x
294
+ y_matrix[8] = -1 * Math.sin(x)
295
+ y_matrix[10] = Math.cos x
296
+ z_matrix = unified.clone
297
+ z_matrix[0] = Math.cos z
298
+ z_matrix[1] = -1 * Math.sin(z)
299
+ z_matrix[4] = Math.sin z
300
+ z_matrix[5] = Math.cos z
301
+ m_1 = BlackBook.multiply_matrices_4by4 m, x_matrix
302
+ m_2 = BlackBook.multiply_matrices_4by4 m_1, y_matrix
303
+ m_3 = BlackBook.multiply_matrices_4by4 m_2, z_matrix
304
+ m_3[12] = @pos.x
305
+ m_3[13] = @pos.y
306
+ m_3[14] = @pos_z
307
+ @left.set m_3[0], m_3[1], m_3[2], m_3[3]
308
+ @dir.set m_3[4], m_3[5], m_3[6], m_3[7]
309
+ @up.set m_3[8], m_3[9], m_3[10], m_3[11]
310
+ m_3
311
+ end
312
+ end
313
+
314
+ # Keep vertices and texcoords together at indices
315
+ class CIndice
316
+ attr_accessor :texcoord_index, :vertice_index
317
+
318
+ def initialize
319
+ @vertice_index = []
320
+ @texcoord_index = []
321
+ end
322
+
323
+ def to_s
324
+ @vertice_index.to_s + '-' + @texcoord_index.to_s
325
+ end
326
+ end
327
+ end