Class: Rugl::Resources::Framebuffer

Inherits:
Base
  • Object
show all
Defined in:
lib/rugl/resources/framebuffer.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#context, #id

Instance Method Summary collapse

Methods inherited from Base

#destroyed?

Constructor Details

#initialize(context, opts) ⇒ Framebuffer

Returns a new instance of Framebuffer.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rugl/resources/framebuffer.rb', line 8

def initialize(context, opts)
  super(context)
  @width = opts.fetch(:width).to_i
  @height = opts.fetch(:height).to_i
  @target = gl_const(:FRAMEBUFFER)
  @owned_attachments = []
  @id = allocate_gl_id(:GenFramebuffers)

  begin
    bind
    setup_color_attachment(opts.fetch(:color, true))
    setup_depth_and_stencil(opts)
    validate_complete!
    unbind
  rescue StandardError
    cleanup_failed_initialization
    raise
  end
end

Instance Attribute Details

#color_attachmentObject (readonly)

Returns the value of attribute color_attachment.



6
7
8
# File 'lib/rugl/resources/framebuffer.rb', line 6

def color_attachment
  @color_attachment
end

#depth_attachmentObject (readonly)

Returns the value of attribute depth_attachment.



6
7
8
# File 'lib/rugl/resources/framebuffer.rb', line 6

def depth_attachment
  @depth_attachment
end

#depth_stencil_attachmentObject (readonly)

Returns the value of attribute depth_stencil_attachment.



6
7
8
# File 'lib/rugl/resources/framebuffer.rb', line 6

def depth_stencil_attachment
  @depth_stencil_attachment
end

#heightObject (readonly)

Returns the value of attribute height.



6
7
8
# File 'lib/rugl/resources/framebuffer.rb', line 6

def height
  @height
end

#stencil_attachmentObject (readonly)

Returns the value of attribute stencil_attachment.



6
7
8
# File 'lib/rugl/resources/framebuffer.rb', line 6

def stencil_attachment
  @stencil_attachment
end

#widthObject (readonly)

Returns the value of attribute width.



6
7
8
# File 'lib/rugl/resources/framebuffer.rb', line 6

def width
  @width
end

Instance Method Details

#bindObject



28
29
30
31
32
# File 'lib/rugl/resources/framebuffer.rb', line 28

def bind
  ensure_alive!
  gl_call(:BindFramebuffer, @target, @id)
  self
end

#destroyObject



68
69
70
71
72
73
74
75
# File 'lib/rugl/resources/framebuffer.rb', line 68

def destroy
  return if destroyed?

  delete_gl_id(:DeleteFramebuffers, @id)
  @owned_attachments.each(&:destroy)
  mark_destroyed!
  nil
end

#resize(width, height) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rugl/resources/framebuffer.rb', line 52

def resize(width, height)
  ensure_alive!
  @width = width.to_i
  @height = height.to_i

  [@color_attachment, @depth_attachment, @stencil_attachment, @depth_stencil_attachment].compact.each do |attachment|
    attachment.resize(@width, @height) if attachment.respond_to?(:resize)
  end

  bind
  reattach_all
  validate_complete!
  unbind
  self
end

#unbindObject



34
35
36
37
38
# File 'lib/rugl/resources/framebuffer.rb', line 34

def unbind
  return unless gl_supports?(:BindFramebuffer)

  gl_call(:BindFramebuffer, @target, 0)
end

#useObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rugl/resources/framebuffer.rb', line 40

def use
  ensure_alive!
  bind
  return self unless block_given?

  begin
    yield self
  ensure
    unbind
  end
end