Class: Rugl::Resources::Texture

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

Constant Summary collapse

FILTER_MAP =
Util::TEXTURE_FILTER_MAP
WRAP_MAP =
Util::TEXTURE_WRAP_MAP
FORMAT_MAP =
Util::TEXTURE_FORMAT_MAP
PIXEL_TYPE_MAP =
{
  uint8: :UNSIGNED_BYTE,
  uint16: :UNSIGNED_SHORT,
  uint32: :UNSIGNED_INT,
  float: :FLOAT
}.freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#context, #id

Instance Method Summary collapse

Methods inherited from Base

#destroyed?

Constructor Details

#initialize(context, source = {}) ⇒ Texture

Returns a new instance of Texture.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rugl/resources/texture.rb', line 19

def initialize(context, source = {})
  super(context)
  opts = normalize_source(source)

  @width = opts.fetch(:width).to_i
  @height = opts.fetch(:height).to_i
  @format = opts.fetch(:format, :rgba).to_sym
  @type = opts.fetch(:type, :uint8).to_sym
  @min_filter = opts.fetch(:min_filter, :linear).to_sym
  @mag_filter = opts.fetch(:mag_filter, :linear).to_sym
  @wrap_s = opts.fetch(:wrap_s, :clamp_to_edge).to_sym
  @wrap_t = opts.fetch(:wrap_t, :clamp_to_edge).to_sym
  @mipmap = !!opts[:mipmap]
  @target = gl_const(:TEXTURE_2D)

  @id = allocate_gl_id(:GenTextures)
  bind
  apply_parameters
  upload_image(opts[:data])
  generate_mipmap_if_needed
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



17
18
19
# File 'lib/rugl/resources/texture.rb', line 17

def format
  @format
end

#heightObject (readonly)

Returns the value of attribute height.



17
18
19
# File 'lib/rugl/resources/texture.rb', line 17

def height
  @height
end

#mag_filterObject (readonly)

Returns the value of attribute mag_filter.



17
18
19
# File 'lib/rugl/resources/texture.rb', line 17

def mag_filter
  @mag_filter
end

#min_filterObject (readonly)

Returns the value of attribute min_filter.



17
18
19
# File 'lib/rugl/resources/texture.rb', line 17

def min_filter
  @min_filter
end

#mipmapObject (readonly)

Returns the value of attribute mipmap.



17
18
19
# File 'lib/rugl/resources/texture.rb', line 17

def mipmap
  @mipmap
end

#typeObject (readonly)

Returns the value of attribute type.



17
18
19
# File 'lib/rugl/resources/texture.rb', line 17

def type
  @type
end

#widthObject (readonly)

Returns the value of attribute width.



17
18
19
# File 'lib/rugl/resources/texture.rb', line 17

def width
  @width
end

#wrap_sObject (readonly)

Returns the value of attribute wrap_s.



17
18
19
# File 'lib/rugl/resources/texture.rb', line 17

def wrap_s
  @wrap_s
end

#wrap_tObject (readonly)

Returns the value of attribute wrap_t.



17
18
19
# File 'lib/rugl/resources/texture.rb', line 17

def wrap_t
  @wrap_t
end

Instance Method Details

#bind(unit = nil) ⇒ Object



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

def bind(unit = nil)
  ensure_alive!

  if unit && gl_supports?(:ActiveTexture)
    gl_call(:ActiveTexture, gl_const(:TEXTURE0) + unit.to_i)
  end
  gl_call(:BindTexture, @target, @id)

  self
end

#destroyObject



92
93
94
95
96
97
98
# File 'lib/rugl/resources/texture.rb', line 92

def destroy
  return if destroyed?

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

#resize(width, height) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/rugl/resources/texture.rb', line 82

def resize(width, height)
  ensure_alive!
  @width = width.to_i
  @height = height.to_i
  bind
  upload_image(nil)
  generate_mipmap_if_needed
  self
end

#subimage(data:, x:, y:, width:, height:) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rugl/resources/texture.rb', line 60

def subimage(data:, x:, y:, width:, height:)
  ensure_alive!
  bind

  payload = normalize_pixels(data, width: width, height: height)
  gl_call(
    :TexSubImage2D,
    @target,
    0,
    x.to_i,
    y.to_i,
    width.to_i,
    height.to_i,
    gl_format,
    gl_pixel_type,
    payload
  )

  generate_mipmap_if_needed
  self
end

#update(data:) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/rugl/resources/texture.rb', line 52

def update(data:)
  ensure_alive!
  bind
  upload_image(data)
  generate_mipmap_if_needed
  self
end