Class: Rugl::Resources::Buffer

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

Instance Attribute Summary collapse

Attributes inherited from Base

#context, #id

Instance Method Summary collapse

Methods inherited from Base

#destroyed?

Constructor Details

#initialize(context, source) ⇒ Buffer



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rugl/resources/buffer.rb', line 8

def initialize(context, source)
  super(context)
  @target = gl_const(:ARRAY_BUFFER)
  @usage = :static
  @type = :float
  @length = 0
  @byte_length = 0
  @id = allocate_gl_id(:GenBuffers)

  bind
  normalized = normalize_source(source)
  upload_payload(*normalized)
end

Instance Attribute Details

#byte_lengthObject (readonly)

Returns the value of attribute byte_length.



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

def byte_length
  @byte_length
end

#lengthObject (readonly)

Returns the value of attribute length.



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

def length
  @length
end

#targetObject (readonly)

Returns the value of attribute target.



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

def target
  @target
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

#usageObject (readonly)

Returns the value of attribute usage.



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

def usage
  @usage
end

Instance Method Details

#bindObject



22
23
24
25
26
# File 'lib/rugl/resources/buffer.rb', line 22

def bind
  ensure_alive!
  gl_call(:BindBuffer, @target, @id) if gl_supports?(:BindBuffer)
  self
end

#destroyObject



58
59
60
61
62
63
64
# File 'lib/rugl/resources/buffer.rb', line 58

def destroy
  return if destroyed?

  delete_gl_id(:DeleteBuffers, @id)
  mark_destroyed!
  nil
end

#gl_typeObject



66
67
68
# File 'lib/rugl/resources/buffer.rb', line 66

def gl_type
  Util.to_gl_buffer_type(@type, gl: gl)
end

#subdata(data, offset: 0) ⇒ Object

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rugl/resources/buffer.rb', line 40

def subdata(data, offset: 0)
  ensure_alive!
  raise ResourceError, "Buffer subdata requires data" if data.nil?

  bind
  payload, _count, bytes = normalize_data(data, type: @type)

  if gl_supports?(:BufferSubData)
    gl_call(:BufferSubData, @target, offset.to_i, bytes, payload)
  elsif gl_supports?(:NamedBufferSubData)
    gl_call(:NamedBufferSubData, @id, offset.to_i, bytes, payload)
  else
    raise ResourceError, "OpenGL backend does not support BufferSubData"
  end

  self
end

#update(data:) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rugl/resources/buffer.rb', line 28

def update(data:)
  ensure_alive!
  bind

  payload, count, bytes = normalize_data(data, type: @type)
  upload(payload, bytes)
  @length = count
  @byte_length = bytes

  self
end