Class: RBGL::Engine::Texture

Inherits:
Object
  • Object
show all
Defined in:
lib/rbgl/engine/texture.rb

Constant Summary collapse

PPM_HEADER =
/\A(P[36])(?:\s+|#[^\n]*\n)+(\d+)(?:\s+|#[^\n]*\n)+(\d+)(?:\s+|#[^\n]*\n)+(\d+)\s/m
WRAP_MODES =
i[repeat clamp mirror].freeze
FILTER_MODES =
i[nearest linear].freeze
WRAP_REPEAT =
:repeat
WRAP_CLAMP =
:clamp
WRAP_MIRROR =
:mirror
FILTER_NEAREST =
:nearest
FILTER_LINEAR =
:linear

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, data = nil) ⇒ Texture

Returns a new instance of Texture.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rbgl/engine/texture.rb', line 19

def initialize(width, height, data = nil)
  @width = width
  @height = height
  @data = data ? normalize_data!(data) : Array.new(width * height) { Larb::Color.black }
  @levels = [@data]
  @mipmaps_dirty = true
  @wrap_s = WRAP_REPEAT
  @wrap_t = WRAP_REPEAT
  @filter_min = FILTER_LINEAR
  @filter_mag = FILTER_LINEAR
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



10
11
12
# File 'lib/rbgl/engine/texture.rb', line 10

def data
  @data
end

#filter_magObject

Returns the value of attribute filter_mag.



10
11
12
# File 'lib/rbgl/engine/texture.rb', line 10

def filter_mag
  @filter_mag
end

#filter_minObject

Returns the value of attribute filter_min.



10
11
12
# File 'lib/rbgl/engine/texture.rb', line 10

def filter_min
  @filter_min
end

#heightObject (readonly)

Returns the value of attribute height.



10
11
12
# File 'lib/rbgl/engine/texture.rb', line 10

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



10
11
12
# File 'lib/rbgl/engine/texture.rb', line 10

def width
  @width
end

#wrap_sObject

Returns the value of attribute wrap_s.



10
11
12
# File 'lib/rbgl/engine/texture.rb', line 10

def wrap_s
  @wrap_s
end

#wrap_tObject

Returns the value of attribute wrap_t.



10
11
12
# File 'lib/rbgl/engine/texture.rb', line 10

def wrap_t
  @wrap_t
end

Class Method Details

.checker(width, height, size, color1 = Larb::Color.white, color2 = Larb::Color.black) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/rbgl/engine/texture.rb', line 84

def self.checker(width, height, size, color1 = Larb::Color.white, color2 = Larb::Color.black)
  data = Array.new(width * height)
  height.times do |y|
    width.times do |x|
      checker = ((x / size) + (y / size)) % 2
      data[y * width + x] = checker == 0 ? color1 : color2
    end
  end
  new(width, height, data)
end

.from_ppm(filename) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/rbgl/engine/texture.rb', line 76

def self.from_ppm(filename)
  content = File.binread(filename)
  format, width, height, max_val, body_offset = parse_ppm_header(content)
  samples = parse_ppm_samples(content, format, width * height, max_val, body_offset)

  new(width, height, colors_from_ppm_samples(samples, max_val))
end

.solid(width, height, color) ⇒ Object



95
96
97
# File 'lib/rbgl/engine/texture.rb', line 95

def self.solid(width, height, color)
  new(width, height, Array.new(width * height) { color })
end

Instance Method Details

#generate_mipmaps!Object



55
56
57
58
# File 'lib/rbgl/engine/texture.rb', line 55

def generate_mipmaps!
  rebuild_mipmap_chain!
  self
end

#get_pixel(x, y) ⇒ Object



44
45
46
# File 'lib/rbgl/engine/texture.rb', line 44

def get_pixel(x, y)
  pixel_for_level(0, x, y)
end

#sample(u, v, lod: 0) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rbgl/engine/texture.rb', line 31

def sample(u, v, lod: 0)
  u = wrap_coord(u, @wrap_s)
  v = wrap_coord(v, @wrap_t)
  level = mip_level_for(lod)
  level_width = width_for_level(level)
  level_height = height_for_level(level)

  x = u * (level_width - 1)
  y = v * (level_height - 1)

  sample_with_filter(level, x, y, filter_for_lod(lod))
end

#set_pixel(x, y, color) ⇒ Object



48
49
50
51
52
53
# File 'lib/rbgl/engine/texture.rb', line 48

def set_pixel(x, y, color)
  return if x < 0 || x >= @width || y < 0 || y >= @height

  @data[y.to_i * @width + x.to_i] = validate_color!(color)
  @mipmaps_dirty = true
end