Class: RBGL::Engine::Framebuffer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height) ⇒ Framebuffer

Returns a new instance of Framebuffer.



8
9
10
11
12
13
# File 'lib/rbgl/engine/framebuffer.rb', line 8

def initialize(width, height)
  @width = width
  @height = height
  @color_buffer = Array.new(width * height) { Larb::Color.black }
  @depth_buffer = Array.new(width * height) { Float::INFINITY }
end

Instance Attribute Details

#color_bufferObject (readonly)

Returns the value of attribute color_buffer.



6
7
8
# File 'lib/rbgl/engine/framebuffer.rb', line 6

def color_buffer
  @color_buffer
end

#depth_bufferObject (readonly)

Returns the value of attribute depth_buffer.



6
7
8
# File 'lib/rbgl/engine/framebuffer.rb', line 6

def depth_buffer
  @depth_buffer
end

#heightObject (readonly)

Returns the value of attribute height.



6
7
8
# File 'lib/rbgl/engine/framebuffer.rb', line 6

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



6
7
8
# File 'lib/rbgl/engine/framebuffer.rb', line 6

def width
  @width
end

Instance Method Details

#clear(color: Larb::Color.black, depth: Float::INFINITY) ⇒ Object



57
58
59
60
# File 'lib/rbgl/engine/framebuffer.rb', line 57

def clear(color: Larb::Color.black, depth: Float::INFINITY)
  @color_buffer.fill(color)
  @depth_buffer.fill(depth)
end

#clear_color(color) ⇒ Object



62
63
64
# File 'lib/rbgl/engine/framebuffer.rb', line 62

def clear_color(color)
  @color_buffer.fill(color)
end

#clear_depth(depth = Float::INFINITY) ⇒ Object



66
67
68
# File 'lib/rbgl/engine/framebuffer.rb', line 66

def clear_depth(depth = Float::INFINITY)
  @depth_buffer.fill(depth)
end

#get_depth(x, y) ⇒ Object



34
35
36
37
38
# File 'lib/rbgl/engine/framebuffer.rb', line 34

def get_depth(x, y)
  return Float::INFINITY if x < 0 || x >= @width || y < 0 || y >= @height

  @depth_buffer[y * @width + x]
end

#get_pixel(x, y) ⇒ Object



22
23
24
25
26
# File 'lib/rbgl/engine/framebuffer.rb', line 22

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

  @color_buffer[y * @width + x]
end

#resize(width, height) ⇒ Object



15
16
17
18
19
20
# File 'lib/rbgl/engine/framebuffer.rb', line 15

def resize(width, height)
  @width = width
  @height = height
  @color_buffer = Array.new(width * height) { Larb::Color.black }
  @depth_buffer = Array.new(width * height) { Float::INFINITY }
end

#set_depth(x, y, depth) ⇒ Object



40
41
42
43
44
# File 'lib/rbgl/engine/framebuffer.rb', line 40

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

  @depth_buffer[y * @width + x] = depth
end

#set_pixel(x, y, color) ⇒ Object



28
29
30
31
32
# File 'lib/rbgl/engine/framebuffer.rb', line 28

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

  @color_buffer[y * @width + x] = color
end

#to_bgra_bytesObject



93
94
95
# File 'lib/rbgl/engine/framebuffer.rb', line 93

def to_bgra_bytes
  packed_color_bytes(i[b g r a])
end

#to_ppmObject



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rbgl/engine/framebuffer.rb', line 70

def to_ppm
  ppm = "P3\n#{@width} #{@height}\n255\n"
  @height.times do |y|
    row = @width.times.map do |x|
      c = @color_buffer[y * @width + x]
      bytes = c.to_bytes
      "#{bytes[0]} #{bytes[1]} #{bytes[2]}"
    end
    ppm += row.join(" ") + "\n"
  end
  ppm
end

#to_ppm_binaryObject



83
84
85
86
87
# File 'lib/rbgl/engine/framebuffer.rb', line 83

def to_ppm_binary
  header = "P6\n#{@width} #{@height}\n255\n"
  pixels = packed_color_bytes(i[r g b])
  header + pixels
end

#to_rgba_bytesObject



89
90
91
# File 'lib/rbgl/engine/framebuffer.rb', line 89

def to_rgba_bytes
  packed_color_bytes(i[r g b a])
end

#write_pixel(x, y, color, depth, depth_test: true, depth_write: true, blend_mode: :none) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/rbgl/engine/framebuffer.rb', line 46

def write_pixel(x, y, color, depth, depth_test: true, depth_write: true, blend_mode: :none)
  return false if x < 0 || x >= @width || y < 0 || y >= @height

  idx = y * @width + x
  return false if depth_test && depth >= @depth_buffer[idx]

  @color_buffer[idx] = blend_color(@color_buffer[idx], color, blend_mode)
  @depth_buffer[idx] = depth if depth_write
  true
end