Sha256: 58380445fdf7abdbf52478443056a4b9a5db9c21ca7a7ad78e0d5215abf1bcbd

Contents?: true

Size: 1.33 KB

Versions: 10

Compression:

Stored size: 1.33 KB

Contents

module CyberarmEngine
  class Light
    DIRECTIONAL = 0
    POINT = 1
    SPOT = 2

    attr_reader :light_id
    attr_accessor :type, :ambient, :diffuse, :specular, :position, :direction, :intensity

    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

    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

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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
cyberarm_engine-0.24.4 lib/cyberarm_engine/opengl/light.rb
cyberarm_engine-0.24.3 lib/cyberarm_engine/opengl/light.rb
cyberarm_engine-0.24.2 lib/cyberarm_engine/opengl/light.rb
cyberarm_engine-0.24.1 lib/cyberarm_engine/opengl/light.rb
cyberarm_engine-0.24.0 lib/cyberarm_engine/opengl/light.rb
cyberarm_engine-0.23.0 lib/cyberarm_engine/opengl/light.rb
cyberarm_engine-0.22.0 lib/cyberarm_engine/opengl/light.rb
cyberarm_engine-0.21.0 lib/cyberarm_engine/opengl/light.rb
cyberarm_engine-0.20.0 lib/cyberarm_engine/opengl/light.rb
cyberarm_engine-0.19.1 lib/cyberarm_engine/opengl/light.rb