Class: Quake::Renderer::GLParticles

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

Overview

Simple particle system for visual effects. Particles are rendered as GL_POINTS with depth test enabled. Matches Quake’s R_DrawParticles (gl_rpart.c).

Defined Under Namespace

Classes: Particle

Constant Summary collapse

GRAVITY =
800.0
MAX_PARTICLES =
2048
RAMP1 =

Quake ramp tables for color animation (palette indices)

[0x6f, 0x6d, 0x6b, 0x69, 0x67, 0x65, 0x63, 0x61].freeze
RAMP2 =
[0x6f, 0x6e, 0x6d, 0x6c, 0x6b, 0x6a, 0x68, 0x66].freeze
RAMP3 =

explosion

[0x6d, 0x6b, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01].freeze

Instance Method Summary collapse

Constructor Details

#initialize(palette) ⇒ GLParticles

Returns a new instance of GLParticles.



23
24
25
26
# File 'lib/quake/renderer/gl_particles.rb', line 23

def initialize(palette)
  @palette = palette
  @particles = []
end

Instance Method Details

#blood(pos, count: 20) ⇒ Object

Blood spurt



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/quake/renderer/gl_particles.rb', line 141

def blood(pos, count: 20)
  count.times do
    color_idx = 67 + (rand * 4).to_i # dark red palette range
    color = @palette.rgb(color_idx)
    emit(
      x: pos.x, y: pos.y, z: pos.z,
      vx: (rand * 128) - 64, vy: (rand * 128) - 64, vz: (rand * 128) - 64,
      r: color[0] / 255.0, g: color[1] / 255.0, b: color[2] / 255.0,
      life: 0.5 + rand * 0.3,
      gravity_scale: 1.0
    )
  end
end

#explosion(pos) ⇒ Object

Explosion burst



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/quake/renderer/gl_particles.rb', line 126

def explosion(pos)
  128.times do
    color = @palette.rgb(0x6d)
    emit(
      x: pos.x + rand * 16 - 8, y: pos.y + rand * 16 - 8, z: pos.z + rand * 16 - 8,
      vx: (rand * 512) - 256, vy: (rand * 512) - 256, vz: (rand * 512) - 256,
      r: color[0] / 255.0, g: color[1] / 255.0, b: color[2] / 255.0,
      life: 0.5 + rand * 0.5,
      gravity_scale: 0.05,
      ramp_type: :explosion
    )
  end
end

#particle_countObject



155
156
157
# File 'lib/quake/renderer/gl_particles.rb', line 155

def particle_count
  @particles.size
end

#pickup_effect(pos) ⇒ Object

Item pickup sparkle



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/quake/renderer/gl_particles.rb', line 107

def pickup_effect(pos)
  20.times do
    angle = rand * ::Math::PI * 2.0
    speed = 50.0 + rand * 50.0
    color = @palette.rgb(0x6f) # orange/yellow
    emit(
      x: pos.x, y: pos.y, z: pos.z + 16.0,
      vx: ::Math.cos(angle) * speed,
      vy: ::Math.sin(angle) * speed,
      vz: 80.0 + rand * 60.0,
      r: color[0] / 255.0, g: color[1] / 255.0, b: color[2] / 255.0,
      life: 0.3 + rand * 0.3,
      gravity_scale: 0.5,
      ramp_type: :fire
    )
  end
end

#renderObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/quake/renderer/gl_particles.rb', line 68

def render
  return if @particles.empty?

  GL.Disable(GL::TEXTURE_2D)
  GL.Enable(GL::BLEND)
  GL.BlendFunc(GL::SRC_ALPHA, GL::ONE_MINUS_SRC_ALPHA)
  GL.PointSize(2.0)

  GL.Begin(GL::POINTS)
  @particles.each do |p|
    GL.Color4f(p.r, p.g, p.b, p.a)
    GL.Vertex3f(p.x, p.y, p.z)
  end
  GL.End

  GL.PointSize(1.0)
  GL.Disable(GL::BLEND)
  GL.Color4f(1.0, 1.0, 1.0, 1.0)
end

#teleport_splash(pos) ⇒ Object

Teleporter sparkle effect at a position



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/quake/renderer/gl_particles.rb', line 89

def teleport_splash(pos)
  80.times do
    dx = (rand * 64.0) - 32.0
    dy = (rand * 64.0) - 32.0
    dz = (rand * 64.0) - 32.0
    color_idx = 7 + (rand * 8).to_i
    color = @palette.rgb(color_idx)
    emit(
      x: pos.x + dx, y: pos.y + dy, z: pos.z + dz,
      vx: dx * 2.0, vy: dy * 2.0, vz: dz * 2.0 + 80.0,
      r: color[0] / 255.0, g: color[1] / 255.0, b: color[2] / 255.0,
      life: 0.5 + rand * 0.3,
      gravity_scale: 0.2
    )
  end
end

#update(dt) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/quake/renderer/gl_particles.rb', line 28

def update(dt)
  @particles.reject! do |p|
    p.life -= dt
    next true if p.life <= 0

    # Apply velocity
    p.x += p.vx * dt
    p.y += p.vy * dt
    p.z += p.vz * dt

    # Apply gravity
    p.vz -= GRAVITY * p.gravity_scale * dt

    # Color ramp animation
    if p.ramp_type
      p.ramp += dt * 10.0
      idx = p.ramp.to_i
      ramp = case p.ramp_type
             when :explosion then RAMP3
             when :fire then RAMP1
             else RAMP2
             end

      if idx >= ramp.size
        next true # particle expired
      end

      color = @palette.rgb(ramp[idx])
      p.r = color[0] / 255.0
      p.g = color[1] / 255.0
      p.b = color[2] / 255.0
    end

    # Fade alpha
    p.a = (p.life * 2.0).clamp(0.0, 1.0)

    false
  end
end