cyberarm_engine 0.19.0 → 0.19.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +8 -8
- data/.rubocop.yml +7 -7
- data/.travis.yml +5 -5
- data/Gemfile +6 -6
- data/LICENSE.txt +21 -21
- data/README.md +74 -74
- data/Rakefile +10 -10
- data/bin/console +14 -14
- data/bin/setup +8 -8
- data/cyberarm_engine.gemspec +39 -39
- data/lib/cyberarm_engine/animator.rb +219 -219
- data/lib/cyberarm_engine/background.rb +179 -179
- data/lib/cyberarm_engine/background_nine_slice.rb +142 -142
- data/lib/cyberarm_engine/bounding_box.rb +150 -150
- data/lib/cyberarm_engine/builtin/intro_state.rb +130 -130
- data/lib/cyberarm_engine/cache/download_manager.rb +121 -121
- data/lib/cyberarm_engine/cache.rb +4 -4
- data/lib/cyberarm_engine/common.rb +113 -113
- data/lib/cyberarm_engine/config_file.rb +46 -46
- data/lib/cyberarm_engine/console/command.rb +157 -157
- data/lib/cyberarm_engine/console/commands/help_command.rb +43 -43
- data/lib/cyberarm_engine/console/subcommand.rb +99 -99
- data/lib/cyberarm_engine/console.rb +248 -248
- data/lib/cyberarm_engine/game_object.rb +248 -248
- data/lib/cyberarm_engine/game_state.rb +97 -97
- data/lib/cyberarm_engine/model/material.rb +21 -21
- data/lib/cyberarm_engine/model/model_object.rb +131 -131
- data/lib/cyberarm_engine/model/parser.rb +74 -74
- data/lib/cyberarm_engine/model/parsers/collada_parser.rb +138 -138
- data/lib/cyberarm_engine/model/parsers/wavefront_parser.rb +154 -154
- data/lib/cyberarm_engine/model.rb +212 -212
- data/lib/cyberarm_engine/model_cache.rb +31 -31
- data/lib/cyberarm_engine/opengl/light.rb +50 -50
- data/lib/cyberarm_engine/opengl/orthographic_camera.rb +46 -46
- data/lib/cyberarm_engine/opengl/perspective_camera.rb +38 -38
- data/lib/cyberarm_engine/opengl/renderer/bounding_box_renderer.rb +249 -249
- data/lib/cyberarm_engine/opengl/renderer/g_buffer.rb +164 -164
- data/lib/cyberarm_engine/opengl/renderer/opengl_renderer.rb +298 -298
- data/lib/cyberarm_engine/opengl/renderer/renderer.rb +22 -22
- data/lib/cyberarm_engine/opengl/shader.rb +406 -406
- data/lib/cyberarm_engine/opengl/texture.rb +69 -69
- data/lib/cyberarm_engine/opengl.rb +28 -28
- data/lib/cyberarm_engine/ray.rb +56 -56
- data/lib/cyberarm_engine/stats.rb +21 -21
- data/lib/cyberarm_engine/text.rb +197 -197
- data/lib/cyberarm_engine/timer.rb +23 -23
- data/lib/cyberarm_engine/transform.rb +296 -296
- data/lib/cyberarm_engine/ui/border_canvas.rb +102 -102
- data/lib/cyberarm_engine/ui/dsl.rb +139 -139
- data/lib/cyberarm_engine/ui/element.rb +488 -488
- data/lib/cyberarm_engine/ui/elements/button.rb +97 -97
- data/lib/cyberarm_engine/ui/elements/check_box.rb +54 -54
- data/lib/cyberarm_engine/ui/elements/container.rb +256 -256
- data/lib/cyberarm_engine/ui/elements/edit_box.rb +179 -179
- data/lib/cyberarm_engine/ui/elements/edit_line.rb +263 -263
- data/lib/cyberarm_engine/ui/elements/flow.rb +15 -15
- data/lib/cyberarm_engine/ui/elements/image.rb +72 -72
- data/lib/cyberarm_engine/ui/elements/list_box.rb +88 -82
- data/lib/cyberarm_engine/ui/elements/progress.rb +51 -51
- data/lib/cyberarm_engine/ui/elements/radio.rb +6 -6
- data/lib/cyberarm_engine/ui/elements/slider.rb +104 -104
- data/lib/cyberarm_engine/ui/elements/stack.rb +11 -11
- data/lib/cyberarm_engine/ui/elements/text_block.rb +162 -162
- data/lib/cyberarm_engine/ui/elements/toggle_button.rb +65 -65
- data/lib/cyberarm_engine/ui/event.rb +54 -54
- data/lib/cyberarm_engine/ui/gui_state.rb +256 -256
- data/lib/cyberarm_engine/ui/style.rb +49 -49
- data/lib/cyberarm_engine/ui/theme.rb +207 -207
- data/lib/cyberarm_engine/vector.rb +293 -293
- data/lib/cyberarm_engine/version.rb +4 -4
- data/lib/cyberarm_engine/window.rb +120 -120
- data/lib/cyberarm_engine.rb +71 -71
- metadata +3 -3
@@ -1,298 +1,298 @@
|
|
1
|
-
module CyberarmEngine
|
2
|
-
class OpenGLRenderer
|
3
|
-
@@immediate_mode_warning = false
|
4
|
-
|
5
|
-
attr_accessor :show_wireframe
|
6
|
-
attr_reader :number_of_vertices
|
7
|
-
|
8
|
-
def initialize(width:, height:, show_wireframe: false)
|
9
|
-
@width = width
|
10
|
-
@height = height
|
11
|
-
@show_wireframe = show_wireframe
|
12
|
-
|
13
|
-
@number_of_vertices = 0
|
14
|
-
|
15
|
-
@g_buffer = GBuffer.new(width: @width, height: @height)
|
16
|
-
end
|
17
|
-
|
18
|
-
def canvas_size_changed
|
19
|
-
@g_buffer.unbind_framebuffer
|
20
|
-
@g_buffer.clean_up
|
21
|
-
|
22
|
-
@g_buffer = GBuffer.new(width: @width, height: @height)
|
23
|
-
end
|
24
|
-
|
25
|
-
def render(camera, lights, entities)
|
26
|
-
@number_of_vertices = 0
|
27
|
-
|
28
|
-
glViewport(0, 0, @width, @height)
|
29
|
-
glEnable(GL_DEPTH_TEST)
|
30
|
-
|
31
|
-
if Shader.available?("g_buffer") && Shader.available?("lighting")
|
32
|
-
@g_buffer.bind_for_writing
|
33
|
-
gl_error?
|
34
|
-
|
35
|
-
glClearColor(0.0, 0.0, 0.0, 0.0)
|
36
|
-
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
|
37
|
-
|
38
|
-
Shader.use("g_buffer") do |shader|
|
39
|
-
gl_error?
|
40
|
-
|
41
|
-
entities.each do |entity|
|
42
|
-
next unless entity.visible && entity.renderable
|
43
|
-
|
44
|
-
shader.uniform_transform("projection", camera.projection_matrix)
|
45
|
-
shader.uniform_transform("view", camera.view_matrix)
|
46
|
-
shader.uniform_transform("model", entity.model_matrix)
|
47
|
-
shader.uniform_vec3("cameraPosition", camera.position)
|
48
|
-
|
49
|
-
gl_error?
|
50
|
-
draw_model(entity.model, shader)
|
51
|
-
entity.draw
|
52
|
-
|
53
|
-
@number_of_vertices += entity.model.vertices_count
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
@g_buffer.unbind_framebuffer
|
58
|
-
gl_error?
|
59
|
-
|
60
|
-
@g_buffer.bind_for_reading
|
61
|
-
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)
|
62
|
-
|
63
|
-
lighting(lights)
|
64
|
-
gl_error?
|
65
|
-
|
66
|
-
post_processing
|
67
|
-
gl_error?
|
68
|
-
|
69
|
-
# render_framebuffer
|
70
|
-
gl_error?
|
71
|
-
|
72
|
-
@g_buffer.unbind_framebuffer
|
73
|
-
gl_error?
|
74
|
-
else
|
75
|
-
unless @@immediate_mode_warning
|
76
|
-
puts "Shaders are disabled or failed to compile, using immediate mode for rendering..."
|
77
|
-
end
|
78
|
-
@@immediate_mode_warning = true
|
79
|
-
|
80
|
-
gl_error?
|
81
|
-
lights.each(&:draw)
|
82
|
-
camera.draw
|
83
|
-
|
84
|
-
glEnable(GL_NORMALIZE)
|
85
|
-
entities.each do |entity|
|
86
|
-
next unless entity.visible && entity.renderable
|
87
|
-
|
88
|
-
glPushMatrix
|
89
|
-
|
90
|
-
glTranslatef(entity.position.x, entity.position.y, entity.position.z)
|
91
|
-
glScalef(entity.scale.x, entity.scale.y, entity.scale.z)
|
92
|
-
glRotatef(entity.orientation.x, 1.0, 0, 0)
|
93
|
-
glRotatef(entity.orientation.y, 0, 1.0, 0)
|
94
|
-
glRotatef(entity.orientation.z, 0, 0, 1.0)
|
95
|
-
|
96
|
-
gl_error?
|
97
|
-
draw_mesh(entity.model)
|
98
|
-
entity.draw
|
99
|
-
glPopMatrix
|
100
|
-
|
101
|
-
@number_of_vertices += entity.model.vertices_count
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
gl_error?
|
106
|
-
end
|
107
|
-
|
108
|
-
def copy_g_buffer_to_screen
|
109
|
-
@g_buffer.set_read_buffer(:position)
|
110
|
-
glBlitFramebuffer(0, 0, @g_buffer.width, @g_buffer.height,
|
111
|
-
0, 0, @g_buffer.width / 2, @g_buffer.height / 2,
|
112
|
-
GL_COLOR_BUFFER_BIT, GL_LINEAR)
|
113
|
-
|
114
|
-
@g_buffer.set_read_buffer(:diffuse)
|
115
|
-
glBlitFramebuffer(0, 0, @g_buffer.width, @g_buffer.height,
|
116
|
-
0, @g_buffer.height / 2, @g_buffer.width / 2, @g_buffer.height,
|
117
|
-
GL_COLOR_BUFFER_BIT, GL_LINEAR)
|
118
|
-
|
119
|
-
@g_buffer.set_read_buffer(:normal)
|
120
|
-
glBlitFramebuffer(0, 0, @g_buffer.width, @g_buffer.height,
|
121
|
-
@g_buffer.width / 2, @g_buffer.height / 2, @g_buffer.width, @g_buffer.height,
|
122
|
-
GL_COLOR_BUFFER_BIT, GL_LINEAR)
|
123
|
-
|
124
|
-
@g_buffer.set_read_buffer(:texcoord)
|
125
|
-
glBlitFramebuffer(0, 0, @g_buffer.width, @g_buffer.height,
|
126
|
-
@g_buffer.width / 2, 0, @g_buffer.width, @g_buffer.height / 2,
|
127
|
-
GL_COLOR_BUFFER_BIT, GL_LINEAR)
|
128
|
-
end
|
129
|
-
|
130
|
-
def lighting(lights)
|
131
|
-
Shader.use("lighting") do |shader|
|
132
|
-
glBindVertexArray(@g_buffer.screen_vbo)
|
133
|
-
|
134
|
-
glDisable(GL_DEPTH_TEST)
|
135
|
-
glEnable(GL_BLEND)
|
136
|
-
|
137
|
-
glActiveTexture(GL_TEXTURE0)
|
138
|
-
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:diffuse))
|
139
|
-
shader.uniform_integer("diffuse", 0)
|
140
|
-
|
141
|
-
glActiveTexture(GL_TEXTURE1)
|
142
|
-
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:position))
|
143
|
-
shader.uniform_integer("position", 1)
|
144
|
-
|
145
|
-
glActiveTexture(GL_TEXTURE2)
|
146
|
-
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:texcoord))
|
147
|
-
shader.uniform_integer("texcoord", 2)
|
148
|
-
|
149
|
-
glActiveTexture(GL_TEXTURE3)
|
150
|
-
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:normal))
|
151
|
-
shader.uniform_integer("normal", 3)
|
152
|
-
|
153
|
-
glActiveTexture(GL_TEXTURE4)
|
154
|
-
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:depth))
|
155
|
-
shader.uniform_integer("depth", 4)
|
156
|
-
|
157
|
-
lights.each_with_index do |light, _i|
|
158
|
-
shader.uniform_integer("light[0].type", light.type)
|
159
|
-
shader.uniform_vec3("light[0].direction", light.direction)
|
160
|
-
shader.uniform_vec3("light[0].position", light.position)
|
161
|
-
shader.uniform_vec3("light[0].diffuse", light.diffuse)
|
162
|
-
shader.uniform_vec3("light[0].ambient", light.ambient)
|
163
|
-
shader.uniform_vec3("light[0].specular", light.specular)
|
164
|
-
|
165
|
-
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)
|
166
|
-
end
|
167
|
-
|
168
|
-
glBindVertexArray(0)
|
169
|
-
end
|
170
|
-
end
|
171
|
-
|
172
|
-
def post_processing
|
173
|
-
end
|
174
|
-
|
175
|
-
def render_framebuffer
|
176
|
-
if Shader.available?("lighting")
|
177
|
-
Shader.use("lighting") do |shader|
|
178
|
-
glBindVertexArray(@g_buffer.screen_vbo)
|
179
|
-
|
180
|
-
glDisable(GL_DEPTH_TEST)
|
181
|
-
glEnable(GL_BLEND)
|
182
|
-
|
183
|
-
glActiveTexture(GL_TEXTURE0)
|
184
|
-
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:diffuse))
|
185
|
-
shader.uniform_integer("diffuse_texture", 0)
|
186
|
-
|
187
|
-
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)
|
188
|
-
|
189
|
-
glBindVertexArray(0)
|
190
|
-
end
|
191
|
-
end
|
192
|
-
end
|
193
|
-
|
194
|
-
def draw_model(model, shader)
|
195
|
-
glBindVertexArray(model.vertex_array_id)
|
196
|
-
glEnableVertexAttribArray(0)
|
197
|
-
glEnableVertexAttribArray(1)
|
198
|
-
glEnableVertexAttribArray(2)
|
199
|
-
if model.has_texture?
|
200
|
-
glEnableVertexAttribArray(3)
|
201
|
-
glEnableVertexAttribArray(4)
|
202
|
-
end
|
203
|
-
|
204
|
-
if @show_wireframe
|
205
|
-
glLineWidth(2)
|
206
|
-
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
|
207
|
-
Shader.active_shader.uniform_boolean("disableLighting", true)
|
208
|
-
|
209
|
-
glDrawArrays(GL_TRIANGLES, 0, model.faces.count * 3)
|
210
|
-
|
211
|
-
Shader.active_shader.uniform_boolean("disableLighting", false)
|
212
|
-
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
|
213
|
-
glLineWidth(1)
|
214
|
-
end
|
215
|
-
|
216
|
-
offset = 0
|
217
|
-
model.objects.each do |object|
|
218
|
-
shader.uniform_boolean("hasTexture", object.has_texture?)
|
219
|
-
|
220
|
-
if object.has_texture?
|
221
|
-
glBindTexture(GL_TEXTURE_2D, object.materials.find { |mat| mat.texture_id }.texture_id)
|
222
|
-
else
|
223
|
-
glBindTexture(GL_TEXTURE_2D, 0)
|
224
|
-
end
|
225
|
-
|
226
|
-
glDrawArrays(GL_TRIANGLES, offset, object.faces.count * 3)
|
227
|
-
offset += object.faces.count * 3
|
228
|
-
end
|
229
|
-
|
230
|
-
if model.has_texture?
|
231
|
-
glDisableVertexAttribArray(4)
|
232
|
-
glDisableVertexAttribArray(3)
|
233
|
-
|
234
|
-
glBindTexture(GL_TEXTURE_2D, 0)
|
235
|
-
end
|
236
|
-
glDisableVertexAttribArray(2)
|
237
|
-
glDisableVertexAttribArray(1)
|
238
|
-
glDisableVertexAttribArray(0)
|
239
|
-
|
240
|
-
glBindBuffer(GL_ARRAY_BUFFER, 0)
|
241
|
-
glBindVertexArray(0)
|
242
|
-
end
|
243
|
-
|
244
|
-
def draw_mesh(model)
|
245
|
-
model.objects.each_with_index do |o, _i|
|
246
|
-
glEnable(GL_COLOR_MATERIAL)
|
247
|
-
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
|
248
|
-
glShadeModel(GL_FLAT) unless o.faces.first[4]
|
249
|
-
glShadeModel(GL_SMOOTH) if o.faces.first[4]
|
250
|
-
glEnableClientState(GL_VERTEX_ARRAY)
|
251
|
-
glEnableClientState(GL_COLOR_ARRAY)
|
252
|
-
glEnableClientState(GL_NORMAL_ARRAY)
|
253
|
-
|
254
|
-
if o.has_texture?
|
255
|
-
glEnable(GL_TEXTURE_2D)
|
256
|
-
glBindTexture(GL_TEXTURE_2D, o.materials.find { |mat| mat.texture_id }.texture_id)
|
257
|
-
glEnableClientState(GL_TEXTURE_COORD_ARRAY)
|
258
|
-
glTexCoordPointer(3, GL_FLOAT, 0, o.flattened_uvs)
|
259
|
-
end
|
260
|
-
|
261
|
-
glVertexPointer(4, GL_FLOAT, 0, o.flattened_vertices)
|
262
|
-
glColorPointer(3, GL_FLOAT, 0, o.flattened_materials)
|
263
|
-
glNormalPointer(GL_FLOAT, 0, o.flattened_normals)
|
264
|
-
|
265
|
-
if @show_wireframe # This is kinda expensive
|
266
|
-
glDisable(GL_LIGHTING)
|
267
|
-
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
|
268
|
-
glPolygonOffset(2, 0.5)
|
269
|
-
glLineWidth(3)
|
270
|
-
|
271
|
-
glDrawArrays(GL_TRIANGLES, 0, o.flattened_vertices_size / 4)
|
272
|
-
|
273
|
-
glLineWidth(1)
|
274
|
-
glPolygonOffset(0, 0)
|
275
|
-
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
|
276
|
-
glEnable(GL_LIGHTING)
|
277
|
-
|
278
|
-
glDrawArrays(GL_TRIANGLES, 0, o.flattened_vertices_size / 4)
|
279
|
-
else
|
280
|
-
glDrawArrays(GL_TRIANGLES, 0, o.flattened_vertices_size / 4)
|
281
|
-
end
|
282
|
-
|
283
|
-
# glBindBuffer(GL_ARRAY_BUFFER, 0)
|
284
|
-
|
285
|
-
glDisableClientState(GL_VERTEX_ARRAY)
|
286
|
-
glDisableClientState(GL_COLOR_ARRAY)
|
287
|
-
glDisableClientState(GL_NORMAL_ARRAY)
|
288
|
-
|
289
|
-
if o.has_texture?
|
290
|
-
glDisableClientState(GL_TEXTURE_COORD_ARRAY)
|
291
|
-
glDisable(GL_TEXTURE_2D)
|
292
|
-
end
|
293
|
-
|
294
|
-
glDisable(GL_COLOR_MATERIAL)
|
295
|
-
end
|
296
|
-
end
|
297
|
-
end
|
298
|
-
end
|
1
|
+
module CyberarmEngine
|
2
|
+
class OpenGLRenderer
|
3
|
+
@@immediate_mode_warning = false
|
4
|
+
|
5
|
+
attr_accessor :show_wireframe
|
6
|
+
attr_reader :number_of_vertices
|
7
|
+
|
8
|
+
def initialize(width:, height:, show_wireframe: false)
|
9
|
+
@width = width
|
10
|
+
@height = height
|
11
|
+
@show_wireframe = show_wireframe
|
12
|
+
|
13
|
+
@number_of_vertices = 0
|
14
|
+
|
15
|
+
@g_buffer = GBuffer.new(width: @width, height: @height)
|
16
|
+
end
|
17
|
+
|
18
|
+
def canvas_size_changed
|
19
|
+
@g_buffer.unbind_framebuffer
|
20
|
+
@g_buffer.clean_up
|
21
|
+
|
22
|
+
@g_buffer = GBuffer.new(width: @width, height: @height)
|
23
|
+
end
|
24
|
+
|
25
|
+
def render(camera, lights, entities)
|
26
|
+
@number_of_vertices = 0
|
27
|
+
|
28
|
+
glViewport(0, 0, @width, @height)
|
29
|
+
glEnable(GL_DEPTH_TEST)
|
30
|
+
|
31
|
+
if Shader.available?("g_buffer") && Shader.available?("lighting")
|
32
|
+
@g_buffer.bind_for_writing
|
33
|
+
gl_error?
|
34
|
+
|
35
|
+
glClearColor(0.0, 0.0, 0.0, 0.0)
|
36
|
+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
|
37
|
+
|
38
|
+
Shader.use("g_buffer") do |shader|
|
39
|
+
gl_error?
|
40
|
+
|
41
|
+
entities.each do |entity|
|
42
|
+
next unless entity.visible && entity.renderable
|
43
|
+
|
44
|
+
shader.uniform_transform("projection", camera.projection_matrix)
|
45
|
+
shader.uniform_transform("view", camera.view_matrix)
|
46
|
+
shader.uniform_transform("model", entity.model_matrix)
|
47
|
+
shader.uniform_vec3("cameraPosition", camera.position)
|
48
|
+
|
49
|
+
gl_error?
|
50
|
+
draw_model(entity.model, shader)
|
51
|
+
entity.draw
|
52
|
+
|
53
|
+
@number_of_vertices += entity.model.vertices_count
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
@g_buffer.unbind_framebuffer
|
58
|
+
gl_error?
|
59
|
+
|
60
|
+
@g_buffer.bind_for_reading
|
61
|
+
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)
|
62
|
+
|
63
|
+
lighting(lights)
|
64
|
+
gl_error?
|
65
|
+
|
66
|
+
post_processing
|
67
|
+
gl_error?
|
68
|
+
|
69
|
+
# render_framebuffer
|
70
|
+
gl_error?
|
71
|
+
|
72
|
+
@g_buffer.unbind_framebuffer
|
73
|
+
gl_error?
|
74
|
+
else
|
75
|
+
unless @@immediate_mode_warning
|
76
|
+
puts "Shaders are disabled or failed to compile, using immediate mode for rendering..."
|
77
|
+
end
|
78
|
+
@@immediate_mode_warning = true
|
79
|
+
|
80
|
+
gl_error?
|
81
|
+
lights.each(&:draw)
|
82
|
+
camera.draw
|
83
|
+
|
84
|
+
glEnable(GL_NORMALIZE)
|
85
|
+
entities.each do |entity|
|
86
|
+
next unless entity.visible && entity.renderable
|
87
|
+
|
88
|
+
glPushMatrix
|
89
|
+
|
90
|
+
glTranslatef(entity.position.x, entity.position.y, entity.position.z)
|
91
|
+
glScalef(entity.scale.x, entity.scale.y, entity.scale.z)
|
92
|
+
glRotatef(entity.orientation.x, 1.0, 0, 0)
|
93
|
+
glRotatef(entity.orientation.y, 0, 1.0, 0)
|
94
|
+
glRotatef(entity.orientation.z, 0, 0, 1.0)
|
95
|
+
|
96
|
+
gl_error?
|
97
|
+
draw_mesh(entity.model)
|
98
|
+
entity.draw
|
99
|
+
glPopMatrix
|
100
|
+
|
101
|
+
@number_of_vertices += entity.model.vertices_count
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
gl_error?
|
106
|
+
end
|
107
|
+
|
108
|
+
def copy_g_buffer_to_screen
|
109
|
+
@g_buffer.set_read_buffer(:position)
|
110
|
+
glBlitFramebuffer(0, 0, @g_buffer.width, @g_buffer.height,
|
111
|
+
0, 0, @g_buffer.width / 2, @g_buffer.height / 2,
|
112
|
+
GL_COLOR_BUFFER_BIT, GL_LINEAR)
|
113
|
+
|
114
|
+
@g_buffer.set_read_buffer(:diffuse)
|
115
|
+
glBlitFramebuffer(0, 0, @g_buffer.width, @g_buffer.height,
|
116
|
+
0, @g_buffer.height / 2, @g_buffer.width / 2, @g_buffer.height,
|
117
|
+
GL_COLOR_BUFFER_BIT, GL_LINEAR)
|
118
|
+
|
119
|
+
@g_buffer.set_read_buffer(:normal)
|
120
|
+
glBlitFramebuffer(0, 0, @g_buffer.width, @g_buffer.height,
|
121
|
+
@g_buffer.width / 2, @g_buffer.height / 2, @g_buffer.width, @g_buffer.height,
|
122
|
+
GL_COLOR_BUFFER_BIT, GL_LINEAR)
|
123
|
+
|
124
|
+
@g_buffer.set_read_buffer(:texcoord)
|
125
|
+
glBlitFramebuffer(0, 0, @g_buffer.width, @g_buffer.height,
|
126
|
+
@g_buffer.width / 2, 0, @g_buffer.width, @g_buffer.height / 2,
|
127
|
+
GL_COLOR_BUFFER_BIT, GL_LINEAR)
|
128
|
+
end
|
129
|
+
|
130
|
+
def lighting(lights)
|
131
|
+
Shader.use("lighting") do |shader|
|
132
|
+
glBindVertexArray(@g_buffer.screen_vbo)
|
133
|
+
|
134
|
+
glDisable(GL_DEPTH_TEST)
|
135
|
+
glEnable(GL_BLEND)
|
136
|
+
|
137
|
+
glActiveTexture(GL_TEXTURE0)
|
138
|
+
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:diffuse))
|
139
|
+
shader.uniform_integer("diffuse", 0)
|
140
|
+
|
141
|
+
glActiveTexture(GL_TEXTURE1)
|
142
|
+
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:position))
|
143
|
+
shader.uniform_integer("position", 1)
|
144
|
+
|
145
|
+
glActiveTexture(GL_TEXTURE2)
|
146
|
+
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:texcoord))
|
147
|
+
shader.uniform_integer("texcoord", 2)
|
148
|
+
|
149
|
+
glActiveTexture(GL_TEXTURE3)
|
150
|
+
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:normal))
|
151
|
+
shader.uniform_integer("normal", 3)
|
152
|
+
|
153
|
+
glActiveTexture(GL_TEXTURE4)
|
154
|
+
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:depth))
|
155
|
+
shader.uniform_integer("depth", 4)
|
156
|
+
|
157
|
+
lights.each_with_index do |light, _i|
|
158
|
+
shader.uniform_integer("light[0].type", light.type)
|
159
|
+
shader.uniform_vec3("light[0].direction", light.direction)
|
160
|
+
shader.uniform_vec3("light[0].position", light.position)
|
161
|
+
shader.uniform_vec3("light[0].diffuse", light.diffuse)
|
162
|
+
shader.uniform_vec3("light[0].ambient", light.ambient)
|
163
|
+
shader.uniform_vec3("light[0].specular", light.specular)
|
164
|
+
|
165
|
+
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)
|
166
|
+
end
|
167
|
+
|
168
|
+
glBindVertexArray(0)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
def post_processing
|
173
|
+
end
|
174
|
+
|
175
|
+
def render_framebuffer
|
176
|
+
if Shader.available?("lighting")
|
177
|
+
Shader.use("lighting") do |shader|
|
178
|
+
glBindVertexArray(@g_buffer.screen_vbo)
|
179
|
+
|
180
|
+
glDisable(GL_DEPTH_TEST)
|
181
|
+
glEnable(GL_BLEND)
|
182
|
+
|
183
|
+
glActiveTexture(GL_TEXTURE0)
|
184
|
+
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:diffuse))
|
185
|
+
shader.uniform_integer("diffuse_texture", 0)
|
186
|
+
|
187
|
+
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)
|
188
|
+
|
189
|
+
glBindVertexArray(0)
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
def draw_model(model, shader)
|
195
|
+
glBindVertexArray(model.vertex_array_id)
|
196
|
+
glEnableVertexAttribArray(0)
|
197
|
+
glEnableVertexAttribArray(1)
|
198
|
+
glEnableVertexAttribArray(2)
|
199
|
+
if model.has_texture?
|
200
|
+
glEnableVertexAttribArray(3)
|
201
|
+
glEnableVertexAttribArray(4)
|
202
|
+
end
|
203
|
+
|
204
|
+
if @show_wireframe
|
205
|
+
glLineWidth(2)
|
206
|
+
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
|
207
|
+
Shader.active_shader.uniform_boolean("disableLighting", true)
|
208
|
+
|
209
|
+
glDrawArrays(GL_TRIANGLES, 0, model.faces.count * 3)
|
210
|
+
|
211
|
+
Shader.active_shader.uniform_boolean("disableLighting", false)
|
212
|
+
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
|
213
|
+
glLineWidth(1)
|
214
|
+
end
|
215
|
+
|
216
|
+
offset = 0
|
217
|
+
model.objects.each do |object|
|
218
|
+
shader.uniform_boolean("hasTexture", object.has_texture?)
|
219
|
+
|
220
|
+
if object.has_texture?
|
221
|
+
glBindTexture(GL_TEXTURE_2D, object.materials.find { |mat| mat.texture_id }.texture_id)
|
222
|
+
else
|
223
|
+
glBindTexture(GL_TEXTURE_2D, 0)
|
224
|
+
end
|
225
|
+
|
226
|
+
glDrawArrays(GL_TRIANGLES, offset, object.faces.count * 3)
|
227
|
+
offset += object.faces.count * 3
|
228
|
+
end
|
229
|
+
|
230
|
+
if model.has_texture?
|
231
|
+
glDisableVertexAttribArray(4)
|
232
|
+
glDisableVertexAttribArray(3)
|
233
|
+
|
234
|
+
glBindTexture(GL_TEXTURE_2D, 0)
|
235
|
+
end
|
236
|
+
glDisableVertexAttribArray(2)
|
237
|
+
glDisableVertexAttribArray(1)
|
238
|
+
glDisableVertexAttribArray(0)
|
239
|
+
|
240
|
+
glBindBuffer(GL_ARRAY_BUFFER, 0)
|
241
|
+
glBindVertexArray(0)
|
242
|
+
end
|
243
|
+
|
244
|
+
def draw_mesh(model)
|
245
|
+
model.objects.each_with_index do |o, _i|
|
246
|
+
glEnable(GL_COLOR_MATERIAL)
|
247
|
+
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
|
248
|
+
glShadeModel(GL_FLAT) unless o.faces.first[4]
|
249
|
+
glShadeModel(GL_SMOOTH) if o.faces.first[4]
|
250
|
+
glEnableClientState(GL_VERTEX_ARRAY)
|
251
|
+
glEnableClientState(GL_COLOR_ARRAY)
|
252
|
+
glEnableClientState(GL_NORMAL_ARRAY)
|
253
|
+
|
254
|
+
if o.has_texture?
|
255
|
+
glEnable(GL_TEXTURE_2D)
|
256
|
+
glBindTexture(GL_TEXTURE_2D, o.materials.find { |mat| mat.texture_id }.texture_id)
|
257
|
+
glEnableClientState(GL_TEXTURE_COORD_ARRAY)
|
258
|
+
glTexCoordPointer(3, GL_FLOAT, 0, o.flattened_uvs)
|
259
|
+
end
|
260
|
+
|
261
|
+
glVertexPointer(4, GL_FLOAT, 0, o.flattened_vertices)
|
262
|
+
glColorPointer(3, GL_FLOAT, 0, o.flattened_materials)
|
263
|
+
glNormalPointer(GL_FLOAT, 0, o.flattened_normals)
|
264
|
+
|
265
|
+
if @show_wireframe # This is kinda expensive
|
266
|
+
glDisable(GL_LIGHTING)
|
267
|
+
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
|
268
|
+
glPolygonOffset(2, 0.5)
|
269
|
+
glLineWidth(3)
|
270
|
+
|
271
|
+
glDrawArrays(GL_TRIANGLES, 0, o.flattened_vertices_size / 4)
|
272
|
+
|
273
|
+
glLineWidth(1)
|
274
|
+
glPolygonOffset(0, 0)
|
275
|
+
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
|
276
|
+
glEnable(GL_LIGHTING)
|
277
|
+
|
278
|
+
glDrawArrays(GL_TRIANGLES, 0, o.flattened_vertices_size / 4)
|
279
|
+
else
|
280
|
+
glDrawArrays(GL_TRIANGLES, 0, o.flattened_vertices_size / 4)
|
281
|
+
end
|
282
|
+
|
283
|
+
# glBindBuffer(GL_ARRAY_BUFFER, 0)
|
284
|
+
|
285
|
+
glDisableClientState(GL_VERTEX_ARRAY)
|
286
|
+
glDisableClientState(GL_COLOR_ARRAY)
|
287
|
+
glDisableClientState(GL_NORMAL_ARRAY)
|
288
|
+
|
289
|
+
if o.has_texture?
|
290
|
+
glDisableClientState(GL_TEXTURE_COORD_ARRAY)
|
291
|
+
glDisable(GL_TEXTURE_2D)
|
292
|
+
end
|
293
|
+
|
294
|
+
glDisable(GL_COLOR_MATERIAL)
|
295
|
+
end
|
296
|
+
end
|
297
|
+
end
|
298
|
+
end
|
@@ -1,22 +1,22 @@
|
|
1
|
-
module CyberarmEngine
|
2
|
-
class Renderer
|
3
|
-
attr_reader :opengl_renderer, :bounding_box_renderer
|
4
|
-
|
5
|
-
def initialize
|
6
|
-
@bounding_box_renderer = BoundingBoxRenderer.new
|
7
|
-
@opengl_renderer = OpenGLRenderer.new(width: $window.width, height: $window.height)
|
8
|
-
end
|
9
|
-
|
10
|
-
def draw(camera, lights, entities)
|
11
|
-
@opengl_renderer.render(camera, lights, entities)
|
12
|
-
@bounding_box_renderer.render(entities) if @show_bounding_boxes
|
13
|
-
end
|
14
|
-
|
15
|
-
def canvas_size_changed
|
16
|
-
@opengl_renderer.canvas_size_changed
|
17
|
-
end
|
18
|
-
|
19
|
-
def finalize # cleanup
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
1
|
+
module CyberarmEngine
|
2
|
+
class Renderer
|
3
|
+
attr_reader :opengl_renderer, :bounding_box_renderer
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@bounding_box_renderer = BoundingBoxRenderer.new
|
7
|
+
@opengl_renderer = OpenGLRenderer.new(width: $window.width, height: $window.height)
|
8
|
+
end
|
9
|
+
|
10
|
+
def draw(camera, lights, entities)
|
11
|
+
@opengl_renderer.render(camera, lights, entities)
|
12
|
+
@bounding_box_renderer.render(entities) if @show_bounding_boxes
|
13
|
+
end
|
14
|
+
|
15
|
+
def canvas_size_changed
|
16
|
+
@opengl_renderer.canvas_size_changed
|
17
|
+
end
|
18
|
+
|
19
|
+
def finalize # cleanup
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|