Class: CyberarmEngine::Light

Inherits:
Object
  • Object
show all
Includes:
OpenGL
Defined in:
lib/cyberarm_engine/opengl/light.rb

Constant Summary collapse

DIRECTIONAL =
0
POINT =
1
SPOT =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, type: Light::POINT, ambient: Vector.new(0.5, 0.5, 0.5), diffuse: Vector.new(1, 1, 1), specular: Vector.new(0.2, 0.2, 0.2), position: Vector.new(0, 0, 0), direction: Vector.new(0, 0, 0), intensity: 1) ⇒ Light

Returns a new instance of Light.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cyberarm_engine/opengl/light.rb', line 12

def initialize(
  id:,
  type: Light::POINT,
  ambient: Vector.new(0.5, 0.5, 0.5),
  diffuse: Vector.new(1, 1, 1),
  specular: Vector.new(0.2, 0.2, 0.2),
  position: Vector.new(0, 0, 0),
  direction: Vector.new(0, 0, 0),
  intensity: 1
)
  @light_id = id
  @type = type

  @ambient  = ambient
  @diffuse  = diffuse
  @specular = specular
  @position = position
  @direction = direction

  @intensity = intensity
end

Instance Attribute Details

#ambientObject

Returns the value of attribute ambient.



10
11
12
# File 'lib/cyberarm_engine/opengl/light.rb', line 10

def ambient
  @ambient
end

#diffuseObject

Returns the value of attribute diffuse.



10
11
12
# File 'lib/cyberarm_engine/opengl/light.rb', line 10

def diffuse
  @diffuse
end

#directionObject

Returns the value of attribute direction.



10
11
12
# File 'lib/cyberarm_engine/opengl/light.rb', line 10

def direction
  @direction
end

#intensityObject

Returns the value of attribute intensity.



10
11
12
# File 'lib/cyberarm_engine/opengl/light.rb', line 10

def intensity
  @intensity
end

#light_idObject (readonly)

Returns the value of attribute light_id.



9
10
11
# File 'lib/cyberarm_engine/opengl/light.rb', line 9

def light_id
  @light_id
end

#positionObject

Returns the value of attribute position.



10
11
12
# File 'lib/cyberarm_engine/opengl/light.rb', line 10

def position
  @position
end

#specularObject

Returns the value of attribute specular.



10
11
12
# File 'lib/cyberarm_engine/opengl/light.rb', line 10

def specular
  @specular
end

#typeObject

Returns the value of attribute type.



10
11
12
# File 'lib/cyberarm_engine/opengl/light.rb', line 10

def type
  @type
end

Instance Method Details

#convert(struct, apply_intensity = false) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/cyberarm_engine/opengl/light.rb', line 44

def convert(struct, apply_intensity = false)
  if apply_intensity
    struct.to_a.compact.map { |i| i * @intensity }
  else
    struct.to_a.compact
  end
end

#drawObject



34
35
36
37
38
39
40
41
42
# File 'lib/cyberarm_engine/opengl/light.rb', line 34

def draw
  glLightfv(@light_id, GL_AMBIENT, convert(@ambient).pack("f*"))
  glLightfv(@light_id, GL_DIFFUSE, convert(@diffuse, true).pack("f*"))
  glLightfv(@light_id, GL_SPECULAR, convert(@specular, true).pack("f*"))
  glLightfv(@light_id, GL_POSITION, convert(@position).pack("f*"))
  glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1)
  glEnable(GL_LIGHTING)
  glEnable(@light_id)
end