Class: Quake::Renderer::GLViewBlend

Inherits:
Object
  • Object
show all
Defined in:
lib/quake/renderer/gl_view_blend.rb

Overview

Renders Quake GL polyblend overlays from view.c color shifts.

Defined Under Namespace

Classes: CShift

Constant Summary collapse

EMPTY_CSHIFT =
CShift.new(color: [130, 80, 50], percent: 0)
WATER_CSHIFT =
CShift.new(color: [130, 80, 50], percent: 128)
SLIME_CSHIFT =
CShift.new(color: [0, 25, 5], percent: 150)
LAVA_CSHIFT =
CShift.new(color: [255, 80, 0], percent: 150)
BONUS_COLOR =
[215, 186, 69].freeze
DAMAGE_ARMOR_COLOR =
[200, 100, 100].freeze
DAMAGE_MIXED_COLOR =
[220, 50, 50].freeze
DAMAGE_BLOOD_COLOR =
[255, 0, 0].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bonus_cshift(percent) ⇒ Object



52
53
54
# File 'lib/quake/renderer/gl_view_blend.rb', line 52

def self.bonus_cshift(percent)
  CShift.new(color: BONUS_COLOR, percent: percent.to_f.clamp(0.0, 50.0))
end

.calc_blend(cshifts, cshift_percent: 100.0) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/quake/renderer/gl_view_blend.rb', line 72

def self.calc_blend(cshifts, cshift_percent: 100.0)
  r = 0.0
  g = 0.0
  b = 0.0
  a = 0.0

  cshifts.each do |shift|
    next if cshift_percent.zero?

    a2 = ((shift.percent.to_f * cshift_percent.to_f) / 100.0) / 255.0
    next if a2.zero?

    a += a2 * (1.0 - a)
    a2 /= a
    r = r * (1.0 - a2) + shift.color[0] * a2
    g = g * (1.0 - a2) + shift.color[1] * a2
    b = b * (1.0 - a2) + shift.color[2] * a2
  end

  [r / 255.0, g / 255.0, b / 255.0, a.clamp(0.0, 1.0)]
end

.contents_cshift(contents) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/quake/renderer/gl_view_blend.rb', line 21

def self.contents_cshift(contents)
  case contents
  when Physics::CONTENTS_EMPTY, Physics::CONTENTS_SOLID
    EMPTY_CSHIFT
  when Physics::CONTENTS_LAVA
    LAVA_CSHIFT
  when Physics::CONTENTS_SLIME
    SLIME_CSHIFT
  else
    WATER_CSHIFT
  end
end

.damage_cshift(armor:, blood:, current_percent: 0.0) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/quake/renderer/gl_view_blend.rb', line 56

def self.damage_cshift(armor:, blood:, current_percent: 0.0)
  armor = armor.to_f
  blood = blood.to_f
  count = blood * 0.5 + armor * 0.5
  count = 10.0 if count < 10.0
  percent = (current_percent.to_f + 3.0 * count).clamp(0.0, 150.0)
  color = if armor > blood
            DAMAGE_ARMOR_COLOR
          elsif armor.positive?
            DAMAGE_MIXED_COLOR
          else
            DAMAGE_BLOOD_COLOR
          end
  CShift.new(color: color, percent: percent)
end

.powerup_cshift(player_state, game_time:) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/quake/renderer/gl_view_blend.rb', line 34

def self.powerup_cshift(player_state, game_time:)
  if powerup_item_active?(player_state, :quad, game_time)
    CShift.new(color: [0, 0, 255], percent: 30)
  elsif powerup_item_active?(player_state, :biosuit, game_time)
    CShift.new(color: [0, 255, 0], percent: 20)
  elsif powerup_item_active?(player_state, :ring, game_time)
    CShift.new(color: [100, 100, 100], percent: 100)
  elsif powerup_item_active?(player_state, :pentagram, game_time)
    CShift.new(color: [255, 255, 0], percent: 30)
  else
    CShift.new(color: [0, 0, 0], percent: 0)
  end
end

.powerup_item_active?(player_state, powerup, game_time) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/quake/renderer/gl_view_blend.rb', line 48

def self.powerup_item_active?(player_state, powerup, game_time)
  player_state&.powerup_item_active?(powerup, game_time: game_time)
end

Instance Method Details

#render(blend) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/quake/renderer/gl_view_blend.rb', line 94

def render(blend)
  return if blend[3].to_f <= 0.0

  GL.MatrixMode(GL::PROJECTION)
  GL.PushMatrix
  GL.LoadIdentity
  GL.Ortho(0, 1, 0, 1, -1, 1)
  GL.MatrixMode(GL::MODELVIEW)
  GL.PushMatrix
  GL.LoadIdentity

  GL.Disable(GL::DEPTH_TEST)
  GL.Disable(GL::CULL_FACE) # 2D pass; scene re-enables culling next frame
  GL.Disable(GL::TEXTURE_2D)
  GL.Enable(GL::BLEND)
  GL.BlendFunc(GL::SRC_ALPHA, GL::ONE_MINUS_SRC_ALPHA)
  GL.Color4f(*blend)
  GL.Begin(GL::QUADS)
  GL.Vertex2f(0.0, 0.0)
  GL.Vertex2f(1.0, 0.0)
  GL.Vertex2f(1.0, 1.0)
  GL.Vertex2f(0.0, 1.0)
  GL.End

  GL.Color4f(1.0, 1.0, 1.0, 1.0)
  GL.Disable(GL::BLEND)
  GL.Enable(GL::TEXTURE_2D)
  GL.Enable(GL::DEPTH_TEST)
  GL.MatrixMode(GL::MODELVIEW)
  GL.PopMatrix
  GL.MatrixMode(GL::PROJECTION)
  GL.PopMatrix
  GL.MatrixMode(GL::MODELVIEW)
end