-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometry.rb
84 lines (76 loc) · 2.58 KB
/
geometry.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require_relative './lib/application'
require_relative './lib/utils'
require_relative './lib/data'
require 'optimist'
require 'rmath3d/rmath3d'
class Geometry
include Logging
def initialize(window)
@window = window
@name = 'geometry'
vertex_source = File.join('shaders', @name, 'vertexShader.glsl')
frag_source = File.join('shaders', @name, 'fragShader.glsl')
geometry_source = File.join('shaders', @name, 'geometryShader.glsl')
@shader_program = Utils::ShaderProgram.new
@shader_program.load_and_attach(:vertex, File.open(vertex_source, 'r', &:read))
@shader_program.load_and_attach(:fragment, File.open(frag_source, 'r', &:read))
@shader_program.load_and_attach(:geometry, File.open(geometry_source, 'r', &:read))
@shader_program.link
@shader_program.use
@vbo = Utils::VertexBuffer.new
points = [
# Red point
[-0.45, 0.45, 1.0, 0.0, 0.0, 4.0],
# Green point
[ 0.45, 0.45, 0.0, 1.0, 0.0, 8.0],
# Blue point
[ 0.45, -0.45, 0.0, 0.0, 1.0, 16.0],
# Yellow point
[-0.45, -0.45, 1.0, 1.0, 0.0, 32.0]
]
@vbo.bind
@vbo.load_buffer(points, :float)
@vao = Utils::VertexArray.new
@vao.bind
# specify layout of point data:
# The position is the first two items
@shader_program.enable_vertex_attrib('pos', 2, :float, 6)
# Color is the next three items
@shader_program.enable_vertex_attrib('color', 3, :float, 6, 2)
# Sides-per-object is the last item
@shader_program.enable_vertex_attrib('sides', 1, :float, 6, 5)
end
def draw
@running = true
@shader_program.use
while @running
event = SDL2::Event.poll
case event
when SDL2::Event::Quit
@running = false
when SDL2::Event::KeyUp
case event.sym
when SDL2::Key::ESCAPE, SDL2::Key::Q
@running = false
end
end
glClearColor(0.0, 0.0, 0.0, 1.0)
glClear(GL_COLOR_BUFFER_BIT)
glDrawArrays(GL_POINTS, 0, 4)
@window.window.gl_swap
end
end
end
opts = Optimist.options do
opt :size, 'width X height string', default: '800x600'
opt :verbose, 'say a lot', default: false
end
window_size = Utils.parse_window_size(opts[:size])
Optimist.die('Valid size string is required') unless window_size
Optimist.die('Valid width is required') unless window_size[:width] > 0
Optimist.die('Valid height is required') unless window_size[:height] > 0
window = Application.new(window_size[:width],
window_size[:height],
'geometry',
opts[:verbose])
Geometry.new(window).draw