Class: Roglew::RenderContext

Inherits:
Object
  • Object
show all
Includes:
RenderContextExtension
Defined in:
lib/roglew/render_context.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RenderContextExtension

included

Constructor Details

#initialize(rh) ⇒ RenderContext

Returns a new instance of RenderContext.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/roglew/render_context.rb', line 20

def initialize(rh)
  @rh = rh

  c = singleton_class
  @rh.loaded_extensions.each do |ext|
    next unless Object.const_defined?(ext)
    mod = Object.const_get(ext)
    next unless mod.const_defined?(:RenderContext)
    c.send(:include, mod.const_get(:RenderContext))
  end
end

Class Method Details

.currentObject



6
7
8
# File 'lib/roglew/render_context.rb', line 6

def current
  stack.empty? ? nil : peek.last
end

Instance Method Details

#bindObject



32
33
34
35
36
37
# File 'lib/roglew/render_context.rb', line 32

def bind
  return unless current?
  @rh.class.send(:push, @rh, self)
  @rh.send(:make_current)
  nil
end

#blend_func(src, dst) ⇒ Object



40
41
42
# File 'lib/roglew/render_context.rb', line 40

def blend_func(src, dst)
  @rh.glBlendFunc(src, dst)
end

#clear(*flags) ⇒ Object



45
46
47
48
49
# File 'lib/roglew/render_context.rb', line 45

def clear(*flags)
  @rh.glClear(flags
    .map { |f| f.is_a?(Integer) ? f : GL.const_get("#{f.to_s.upcase}_BUFFER_BIT") }
    .reduce(&:|))
end

#clear_colorObject



52
53
54
# File 'lib/roglew/render_context.rb', line 52

def clear_color
  get_floats(GL::COLOR_CLEAR_VALUE, 4)
end

#clear_color=(c) ⇒ Object



57
58
59
# File 'lib/roglew/render_context.rb', line 57

def clear_color=(c)
  @rh.glClearColor(*c)
end

#clear_depth=(v) ⇒ Object



62
63
64
# File 'lib/roglew/render_context.rb', line 62

def clear_depth=(v)
  @rh.glClearDepth(v)
end

#clear_stencil=(v) ⇒ Object



67
68
69
# File 'lib/roglew/render_context.rb', line 67

def clear_stencil=(v)
  @rh.glClearStencil(v)
end

#color_mask(r, g, b, a) ⇒ Object



72
73
74
# File 'lib/roglew/render_context.rb', line 72

def color_mask(r, g, b, a)
  @rh.glColorMask(r, g, b, a)
end

#create_texture2d(*args) ⇒ Object



78
79
80
# File 'lib/roglew/render_context.rb', line 78

def create_texture2d(*args)
  Roglew::Texture2d.new(@rh, *args)
end

#current?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/roglew/render_context.rb', line 82

def current?
  RenderContext.current == self
end

#disable(*caps) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/roglew/render_context.rb', line 87

def disable(*caps)
  attribs = @rh.instance_variable_get(:@attribs)
  a1 = attribs.dup

  caps = Set[*caps] & attribs
  caps.each { |cap| @rh.glDisable(cap) }
  attribs.subtract(caps)
  return unless block_given?
  yield

  disable *(attribs - a1)
  enable  *(a1 - attribs)
end

#enable(*caps) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/roglew/render_context.rb', line 102

def enable(*caps)
  attribs = @rh.instance_variable_get(:@attribs)
  a1 = attribs.dup

  caps = Set[*caps] - attribs
  caps.each { |cap| @rh.glEnable(cap) }
  attribs.merge(caps)
  return unless block_given?
  yield

  disable *(attribs - a1)
  enable  *(a1 - attribs)
end

#finishedObject



117
118
119
120
121
122
123
124
125
126
# File 'lib/roglew/render_context.rb', line 117

def finished
  stack = RenderContext.send(:stack)
  stack.pop
  if stack.empty?
    @rh.send(:remove_current)
  elsif stack.last.last != self
    stack.last.first.send(:make_current)
  end
  nil
end

#get_errorsObject



129
130
131
132
133
134
135
# File 'lib/roglew/render_context.rb', line 129

def get_errors
  errors = []
  while (error = @rh.glGetError) != 0
    errors << GL::ERROR[error] || error
  end
  errors
end

#get_floats(pname, count = 1) ⇒ Object



138
139
140
141
142
143
# File 'lib/roglew/render_context.rb', line 138

def get_floats(pname, count = 1)
  p = FFI::MemoryPointer.new(:float, count)
  @rh.glGetFloatv(pname, p)
  result = p.read_array_of_float(count)
  count == 1 ? result[0] : result
end

#get_function(function_name, parameters, return_type) ⇒ Object



146
147
148
149
150
151
152
153
154
155
# File 'lib/roglew/render_context.rb', line 146

def get_function(function_name, parameters, return_type)
  ptr = get_proc_address(function_name.to_s)
  if ptr.null?
    LOGGER.warn "couldn't find function: #{return_type} #{function_name}(#{parameters.join(', ')})"
    return
  end
  return_type = GL.find_type(return_type) || return_type
  parameters = parameters.map { |p| GL.find_type(p) || p }
  FFI::Function.new(return_type, parameters, ptr, convention: :stdcall)
end

#get_integers(pname, count = 1) ⇒ Object



158
159
160
161
162
163
# File 'lib/roglew/render_context.rb', line 158

def get_integers(pname, count = 1)
  p = FFI::MemoryPointer.new(:int, count)
  @rh.glGetIntegerv(pname, p)
  result = p.read_array_of_int(count)
  count == 1 ? result[0] : result
end

#get_proc_address(function_name) ⇒ Object



166
167
168
# File 'lib/roglew/render_context.rb', line 166

def get_proc_address(function_name)
  @rh.send(:get_proc_address, function_name)
end

#num_extensionsObject



170
171
172
# File 'lib/roglew/render_context.rb', line 170

def num_extensions
  get_integers(GL::NUM_EXTENSIONS)
end

#render_handleObject Also known as: handle, rh



174
175
176
# File 'lib/roglew/render_context.rb', line 174

def render_handle
  @rh
end

#swap_buffersObject



179
180
181
# File 'lib/roglew/render_context.rb', line 179

def swap_buffers
  @rh.send(:swap_buffers)
end

#tex_parameter(target, pname, *params) ⇒ Object



184
185
186
187
188
189
190
# File 'lib/roglew/render_context.rb', line 184

def tex_parameter(target, pname, *params)
  params.flatten!
  type = params.all? { |param| param.is_a? Integer } ? 'int' : 'float'
  ptr = FFI::MemoryPointer.new(type.to_sym, params.length)
  ptr.send("write_array_of_#{type}", params)
  @rh.send("glTexParameter#{type[0]}v", target, pname, ptr)
end