cyberarm_engine 0.14.0 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +8 -0
  3. data/Gemfile +1 -1
  4. data/Rakefile +1 -1
  5. data/assets/textures/default.png +0 -0
  6. data/cyberarm_engine.gemspec +10 -8
  7. data/lib/cyberarm_engine.rb +13 -2
  8. data/lib/cyberarm_engine/animator.rb +6 -4
  9. data/lib/cyberarm_engine/background.rb +19 -15
  10. data/lib/cyberarm_engine/background_nine_slice.rb +125 -0
  11. data/lib/cyberarm_engine/bounding_box.rb +18 -18
  12. data/lib/cyberarm_engine/cache.rb +4 -0
  13. data/lib/cyberarm_engine/cache/download_manager.rb +121 -0
  14. data/lib/cyberarm_engine/common.rb +13 -13
  15. data/lib/cyberarm_engine/config_file.rb +2 -2
  16. data/lib/cyberarm_engine/game_object.rb +63 -72
  17. data/lib/cyberarm_engine/game_state.rb +6 -3
  18. data/lib/cyberarm_engine/model.rb +207 -0
  19. data/lib/cyberarm_engine/model/material.rb +21 -0
  20. data/lib/cyberarm_engine/model/model_object.rb +131 -0
  21. data/lib/cyberarm_engine/model/parser.rb +74 -0
  22. data/lib/cyberarm_engine/model/parsers/collada_parser.rb +138 -0
  23. data/lib/cyberarm_engine/model/parsers/wavefront_parser.rb +154 -0
  24. data/lib/cyberarm_engine/model_cache.rb +31 -0
  25. data/lib/cyberarm_engine/opengl.rb +28 -0
  26. data/lib/cyberarm_engine/opengl/light.rb +50 -0
  27. data/lib/cyberarm_engine/opengl/orthographic_camera.rb +46 -0
  28. data/lib/cyberarm_engine/opengl/perspective_camera.rb +38 -0
  29. data/lib/cyberarm_engine/opengl/renderer/bounding_box_renderer.rb +249 -0
  30. data/lib/cyberarm_engine/opengl/renderer/g_buffer.rb +164 -0
  31. data/lib/cyberarm_engine/opengl/renderer/opengl_renderer.rb +289 -0
  32. data/lib/cyberarm_engine/opengl/renderer/renderer.rb +22 -0
  33. data/lib/cyberarm_engine/{shader.rb → opengl/shader.rb} +51 -43
  34. data/lib/cyberarm_engine/opengl/texture.rb +69 -0
  35. data/lib/cyberarm_engine/ray.rb +5 -5
  36. data/lib/cyberarm_engine/stats.rb +2 -2
  37. data/lib/cyberarm_engine/text.rb +41 -27
  38. data/lib/cyberarm_engine/timer.rb +1 -1
  39. data/lib/cyberarm_engine/transform.rb +43 -20
  40. data/lib/cyberarm_engine/ui/border_canvas.rb +4 -3
  41. data/lib/cyberarm_engine/ui/dsl.rb +25 -11
  42. data/lib/cyberarm_engine/ui/element.rb +30 -20
  43. data/lib/cyberarm_engine/ui/elements/button.rb +86 -16
  44. data/lib/cyberarm_engine/ui/elements/check_box.rb +1 -1
  45. data/lib/cyberarm_engine/ui/elements/container.rb +44 -20
  46. data/lib/cyberarm_engine/ui/elements/edit_box.rb +175 -2
  47. data/lib/cyberarm_engine/ui/elements/edit_line.rb +121 -37
  48. data/lib/cyberarm_engine/ui/elements/flow.rb +1 -1
  49. data/lib/cyberarm_engine/ui/elements/image.rb +12 -9
  50. data/lib/cyberarm_engine/ui/elements/label.rb +93 -14
  51. data/lib/cyberarm_engine/ui/elements/list_box.rb +64 -2
  52. data/lib/cyberarm_engine/ui/elements/progress.rb +5 -5
  53. data/lib/cyberarm_engine/ui/elements/radio.rb +1 -1
  54. data/lib/cyberarm_engine/ui/elements/slider.rb +13 -16
  55. data/lib/cyberarm_engine/ui/elements/stack.rb +1 -1
  56. data/lib/cyberarm_engine/ui/elements/toggle_button.rb +27 -19
  57. data/lib/cyberarm_engine/ui/event.rb +7 -7
  58. data/lib/cyberarm_engine/ui/gui_state.rb +44 -10
  59. data/lib/cyberarm_engine/ui/style.rb +10 -9
  60. data/lib/cyberarm_engine/ui/theme.rb +28 -20
  61. data/lib/cyberarm_engine/vector.rb +33 -30
  62. data/lib/cyberarm_engine/version.rb +2 -2
  63. data/lib/cyberarm_engine/window.rb +27 -18
  64. metadata +65 -15
@@ -0,0 +1,38 @@
1
+ module CyberarmEngine
2
+ class PerspectiveCamera
3
+ attr_accessor :position, :orientation, :aspect_ratio, :field_of_view,
4
+ :min_view_distance, :max_view_distance
5
+
6
+ def initialize(position:, aspect_ratio:, orientation: Vector.new(0, 0,
7
+ 0), field_of_view: 70.0, min_view_distance: 0.1, max_view_distance: 155.0)
8
+ @position = position
9
+ @orientation = orientation
10
+
11
+ @aspect_ratio = aspect_ratio
12
+ @field_of_view = field_of_view
13
+
14
+ @min_view_distance = min_view_distance
15
+ @max_view_distance = max_view_distance
16
+ end
17
+
18
+ def draw
19
+ glMatrixMode(GL_PROJECTION)
20
+ glLoadIdentity
21
+ gluPerspective(@field_of_view, @aspect_ratio, @min_view_distance, @max_view_distance)
22
+ glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
23
+ glRotatef(@orientation.x, 1, 0, 0)
24
+ glRotatef(@orientation.y, 0, 1, 0)
25
+ glTranslatef(-@position.x, -@position.y, -@position.z)
26
+ glMatrixMode(GL_MODELVIEW)
27
+ glLoadIdentity
28
+ end
29
+
30
+ def projection_matrix
31
+ Transform.perspective(@field_of_view, @aspect_ratio, @min_view_distance, @max_view_distance)
32
+ end
33
+
34
+ def view_matrix
35
+ Transform.translate_3d(@position * -1) * Transform.rotate_3d(@orientation)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,249 @@
1
+ module CyberarmEngine
2
+ class BoundingBoxRenderer
3
+ attr_reader :bounding_boxes, :vertex_count
4
+
5
+ def initialize
6
+ @bounding_boxes = {}
7
+ @vertex_count = 0
8
+ end
9
+
10
+ def render(entities)
11
+ entities.each do |entity|
12
+ create_bounding_box(entity, color = nil)
13
+ draw_bounding_boxes
14
+ end
15
+
16
+ (@bounding_boxes.keys - entities.map { |e| e.object_id }).each do |key|
17
+ @bounding_boxes.delete(key)
18
+ end
19
+ end
20
+
21
+ def create_bounding_box(entity, color = nil)
22
+ color ||= entity.debug_color
23
+ entity_id = entity.object_id
24
+
25
+ if @bounding_boxes[entity_id]
26
+ if @bounding_boxes[entity_id][:color] != color
27
+ @bounding_boxes[entity_id][:colors] = mesh_colors(color).pack("f*")
28
+ @bounding_boxes[entity_id][:color] = color
29
+ return
30
+ else
31
+ return
32
+ end
33
+ end
34
+
35
+ @bounding_boxes[entity_id] = {
36
+ entity: entity,
37
+ color: color,
38
+ objects: []
39
+ }
40
+
41
+ box = entity.normalize_bounding_box
42
+
43
+ normals = mesh_normals
44
+ colors = mesh_colors(color)
45
+ vertices = mesh_vertices(box)
46
+
47
+ @vertex_count += vertices.size
48
+
49
+ @bounding_boxes[entity_id][:vertices_size] = vertices.size
50
+ @bounding_boxes[entity_id][:vertices] = vertices.pack("f*")
51
+ @bounding_boxes[entity_id][:normals] = normals.pack("f*")
52
+ @bounding_boxes[entity_id][:colors] = colors.pack("f*")
53
+
54
+ entity.model.objects.each do |mesh|
55
+ data = {}
56
+ box = mesh.bounding_box.normalize(entity)
57
+
58
+ normals = mesh_normals
59
+ colors = mesh_colors(mesh.debug_color)
60
+ vertices = mesh_vertices(box)
61
+
62
+ @vertex_count += vertices.size
63
+
64
+ data[:vertices_size] = vertices.size
65
+ data[:vertices] = vertices.pack("f*")
66
+ data[:normals] = normals.pack("f*")
67
+ data[:colors] = colors.pack("f*")
68
+
69
+ @bounding_boxes[entity_id][:objects] << data
70
+ end
71
+ end
72
+
73
+ def mesh_normals
74
+ [
75
+ 0, 1, 0,
76
+ 0, 1, 0,
77
+ 0, 1, 0,
78
+ 0, 1, 0,
79
+ 0, 1, 0,
80
+ 0, 1, 0,
81
+
82
+ 0, -1, 0,
83
+ 0, -1, 0,
84
+ 0, -1, 0,
85
+ 0, -1, 0,
86
+ 0, -1, 0,
87
+ 0, -1, 0,
88
+
89
+ 0, 0, 1,
90
+ 0, 0, 1,
91
+ 0, 0, 1,
92
+ 0, 0, 1,
93
+ 0, 0, 1,
94
+ 0, 0, 1,
95
+
96
+ 1, 0, 0,
97
+ 1, 0, 0,
98
+ 1, 0, 0,
99
+ 1, 0, 0,
100
+ 1, 0, 0,
101
+ 1, 0, 0,
102
+
103
+ -1, 0, 0,
104
+ -1, 0, 0,
105
+ -1, 0, 0,
106
+ -1, 0, 0,
107
+ -1, 0, 0,
108
+ -1, 0, 0,
109
+
110
+ -1, 0, 0,
111
+ -1, 0, 0,
112
+ -1, 0, 0,
113
+
114
+ -1, 0, 0,
115
+ -1, 0, 0,
116
+ -1, 0, 0
117
+ ]
118
+ end
119
+
120
+ def mesh_colors(color)
121
+ [
122
+ color.red, color.green, color.blue,
123
+ color.red, color.green, color.blue,
124
+ color.red, color.green, color.blue,
125
+ color.red, color.green, color.blue,
126
+ color.red, color.green, color.blue,
127
+ color.red, color.green, color.blue,
128
+ color.red, color.green, color.blue,
129
+ color.red, color.green, color.blue,
130
+ color.red, color.green, color.blue,
131
+ color.red, color.green, color.blue,
132
+ color.red, color.green, color.blue,
133
+ color.red, color.green, color.blue,
134
+ color.red, color.green, color.blue,
135
+ color.red, color.green, color.blue,
136
+ color.red, color.green, color.blue,
137
+ color.red, color.green, color.blue,
138
+ color.red, color.green, color.blue,
139
+ color.red, color.green, color.blue,
140
+ color.red, color.green, color.blue,
141
+ color.red, color.green, color.blue,
142
+ color.red, color.green, color.blue,
143
+ color.red, color.green, color.blue,
144
+ color.red, color.green, color.blue,
145
+ color.red, color.green, color.blue,
146
+ color.red, color.green, color.blue,
147
+ color.red, color.green, color.blue,
148
+ color.red, color.green, color.blue,
149
+ color.red, color.green, color.blue,
150
+ color.red, color.green, color.blue,
151
+ color.red, color.green, color.blue,
152
+ color.red, color.green, color.blue,
153
+ color.red, color.green, color.blue,
154
+ color.red, color.green, color.blue,
155
+ color.red, color.green, color.blue,
156
+ color.red, color.green, color.blue,
157
+ color.red, color.green, color.blue
158
+ ]
159
+ end
160
+
161
+ def mesh_vertices(box)
162
+ [
163
+ box.min.x, box.max.y, box.max.z,
164
+ box.min.x, box.max.y, box.min.z,
165
+ box.max.x, box.max.y, box.min.z,
166
+
167
+ box.min.x, box.max.y, box.max.z,
168
+ box.max.x, box.max.y, box.max.z,
169
+ box.max.x, box.max.y, box.min.z,
170
+
171
+ box.max.x, box.min.y, box.min.z,
172
+ box.max.x, box.min.y, box.max.z,
173
+ box.min.x, box.min.y, box.max.z,
174
+
175
+ box.max.x, box.min.y, box.min.z,
176
+ box.min.x, box.min.y, box.min.z,
177
+ box.min.x, box.min.y, box.max.z,
178
+
179
+ box.min.x, box.max.y, box.max.z,
180
+ box.min.x, box.max.y, box.min.z,
181
+ box.min.x, box.min.y, box.min.z,
182
+
183
+ box.min.x, box.min.y, box.max.z,
184
+ box.min.x, box.min.y, box.min.z,
185
+ box.min.x, box.max.y, box.max.z,
186
+
187
+ box.max.x, box.max.y, box.max.z,
188
+ box.max.x, box.max.y, box.min.z,
189
+ box.max.x, box.min.y, box.min.z,
190
+
191
+ box.max.x, box.min.y, box.max.z,
192
+ box.max.x, box.min.y, box.min.z,
193
+ box.max.x, box.max.y, box.max.z,
194
+
195
+ box.min.x, box.max.y, box.max.z,
196
+ box.max.x, box.max.y, box.max.z,
197
+ box.max.x, box.min.y, box.max.z,
198
+
199
+ box.min.x, box.max.y, box.max.z,
200
+ box.max.x, box.min.y, box.max.z,
201
+ box.min.x, box.min.y, box.max.z,
202
+
203
+ box.max.x, box.min.y, box.min.z,
204
+ box.min.x, box.min.y, box.min.z,
205
+ box.min.x, box.max.y, box.min.z,
206
+
207
+ box.max.x, box.min.y, box.min.z,
208
+ box.min.x, box.max.y, box.min.z,
209
+ box.max.x, box.max.y, box.min.z
210
+ ]
211
+ end
212
+
213
+ def draw_bounding_boxes
214
+ @bounding_boxes.each do |key, bounding_box|
215
+ glPushMatrix
216
+
217
+ glTranslatef(
218
+ bounding_box[:entity].position.x,
219
+ bounding_box[:entity].position.y,
220
+ bounding_box[:entity].position.z
221
+ )
222
+ draw_bounding_box(bounding_box)
223
+ @bounding_boxes[key][:objects].each { |o| draw_bounding_box(o) }
224
+
225
+ glPopMatrix
226
+ end
227
+ end
228
+
229
+ def draw_bounding_box(bounding_box)
230
+ glEnableClientState(GL_VERTEX_ARRAY)
231
+ glEnableClientState(GL_COLOR_ARRAY)
232
+ glEnableClientState(GL_NORMAL_ARRAY)
233
+
234
+ glVertexPointer(3, GL_FLOAT, 0, bounding_box[:vertices])
235
+ glColorPointer(3, GL_FLOAT, 0, bounding_box[:colors])
236
+ glNormalPointer(GL_FLOAT, 0, bounding_box[:normals])
237
+
238
+ glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
239
+ glDisable(GL_LIGHTING)
240
+ glDrawArrays(GL_TRIANGLES, 0, bounding_box[:vertices_size] / 3)
241
+ glEnable(GL_LIGHTING)
242
+ glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
243
+
244
+ glDisableClientState(GL_VERTEX_ARRAY)
245
+ glDisableClientState(GL_COLOR_ARRAY)
246
+ glDisableClientState(GL_NORMAL_ARRAY)
247
+ end
248
+ end
249
+ end
@@ -0,0 +1,164 @@
1
+ module CyberarmEngine
2
+ class GBuffer
3
+ attr_reader :screen_vbo, :vertices, :uvs
4
+
5
+ def initialize(width:, height:)
6
+ @width = width
7
+ @height = height
8
+
9
+ @framebuffer = nil
10
+ @buffers = %i[position diffuse normal texcoord]
11
+ @textures = {}
12
+ @screen_vbo = nil
13
+ @ready = false
14
+
15
+ @vertices = [
16
+ -1.0, -1.0, 0,
17
+ 1.0, -1.0, 0,
18
+ -1.0, 1.0, 0,
19
+
20
+ -1.0, 1.0, 0,
21
+ 1.0, -1.0, 0,
22
+ 1.0, 1.0, 0
23
+ ].freeze
24
+
25
+ @uvs = [
26
+ 0, 0,
27
+ 1, 0,
28
+ 0, 1,
29
+
30
+ 0, 1,
31
+ 1, 0,
32
+ 1, 1
33
+ ].freeze
34
+
35
+ create_framebuffer
36
+ create_screen_vbo
37
+ end
38
+
39
+ def create_framebuffer
40
+ buffer = " " * 4
41
+ glGenFramebuffers(1, buffer)
42
+ @framebuffer = buffer.unpack1("L2")
43
+
44
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, @framebuffer)
45
+
46
+ create_textures
47
+
48
+ status = glCheckFramebufferStatus(GL_FRAMEBUFFER)
49
+
50
+ if status != GL_FRAMEBUFFER_COMPLETE
51
+ message = ""
52
+
53
+ message = case status
54
+ when GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT
55
+ "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT"
56
+ when GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT
57
+ "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT"
58
+ when GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER
59
+ "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER"
60
+ when GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER
61
+ "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER"
62
+ when GL_FRAMEBUFFER_UNSUPPORTED
63
+ "GL_FRAMEBUFFER_UNSUPPORTED"
64
+ else
65
+ "Unknown error!"
66
+ end
67
+ puts "Incomplete framebuffer: #{status}\nError: #{message}"
68
+ else
69
+ @ready = true
70
+ end
71
+
72
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)
73
+ end
74
+
75
+ def create_textures
76
+ @buffers.size.times do |i|
77
+ buffer = " " * 4
78
+ glGenTextures(1, buffer)
79
+ texture_id = buffer.unpack1("L2")
80
+ @textures[@buffers[i]] = texture_id
81
+
82
+ glBindTexture(GL_TEXTURE_2D, texture_id)
83
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, @width, @height, 0, GL_RGBA, GL_FLOAT, nil)
84
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
85
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
86
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, texture_id, 0)
87
+ end
88
+
89
+ buffer = " " * 4
90
+ glGenTextures(1, buffer)
91
+ texture_id = buffer.unpack1("L2")
92
+ @textures[:depth] = texture_id
93
+
94
+ glBindTexture(GL_TEXTURE_2D, texture_id)
95
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, @width, @height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nil)
96
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, texture_id, 0)
97
+
98
+ draw_buffers = [GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3]
99
+ glDrawBuffers(draw_buffers.size, draw_buffers.pack("I*"))
100
+ end
101
+
102
+ def create_screen_vbo
103
+ buffer = " " * 4
104
+ glGenVertexArrays(1, buffer)
105
+ @screen_vbo = buffer.unpack1("L2")
106
+
107
+ buffer = " " * 4
108
+ glGenBuffers(1, buffer)
109
+ @positions_buffer_id = buffer.unpack1("L2")
110
+
111
+ buffer = " " * 4
112
+ glGenBuffers(1, buffer)
113
+ @uvs_buffer_id = buffer.unpack1("L2")
114
+
115
+ glBindVertexArray(@screen_vbo)
116
+ glBindBuffer(GL_ARRAY_BUFFER, @positions_buffer_id)
117
+ glBufferData(GL_ARRAY_BUFFER, @vertices.size * Fiddle::SIZEOF_FLOAT, @vertices.pack("f*"), GL_STATIC_DRAW)
118
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nil)
119
+
120
+ glBindBuffer(GL_ARRAY_BUFFER, @uvs_buffer_id)
121
+ glBufferData(GL_ARRAY_BUFFER, @uvs.size * Fiddle::SIZEOF_FLOAT, @uvs.pack("f*"), GL_STATIC_DRAW)
122
+ glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, nil)
123
+
124
+ glEnableVertexAttribArray(0)
125
+ glEnableVertexAttribArray(1)
126
+
127
+ glBindVertexArray(0)
128
+ end
129
+
130
+ def bind_for_writing
131
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, @framebuffer)
132
+ end
133
+
134
+ def bind_for_reading
135
+ glBindFramebuffer(GL_READ_FRAMEBUFFER, @framebuffer)
136
+ end
137
+
138
+ def set_read_buffer(buffer)
139
+ glReadBuffer(GL_COLOR_ATTACHMENT0 + @textures.keys.index(buffer))
140
+ end
141
+
142
+ def set_read_buffer_depth
143
+ glReadBuffer(GL_DEPTH_ATTACHMENT)
144
+ end
145
+
146
+ def unbind_framebuffer
147
+ glBindFramebuffer(GL_FRAMEBUFFER, 0)
148
+ end
149
+
150
+ def texture(type)
151
+ @textures[type]
152
+ end
153
+
154
+ def clean_up
155
+ glDeleteFramebuffers(1, [@framebuffer].pack("L"))
156
+
157
+ glDeleteTextures(@textures.values.size, @textures.values.pack("L*"))
158
+
159
+ glDeleteBuffers(2, [@positions_buffer_id, @uvs_buffer_id].pack("L*"))
160
+ glDeleteVertexArrays(1, [@screen_vbo].pack("L"))
161
+ gl_error?
162
+ end
163
+ end
164
+ end