class View

A lightweight view

Constants

SHADOW_COLOR

Attributes

color[RW]
model[RW]
object[RW]
shadow[RW]

Public Class Methods

new(model, color = nil) click to toggle source
Calls superclass method
# File examples/ruboids/ruboids/View.rb, line 15
def initialize(model, color = nil)
    super()
    @model = model
    @color = color
    @object = nil
    @shadow = nil
end

Public Instance Methods

draw() click to toggle source
# File examples/ruboids/ruboids/View.rb, line 39
def draw
    # We don't always have enough information to make the 3D objects
    # at initialize() time.
    makeObject() unless @object
    makeShadow() unless @shadow

    rot = Graphics.rotations(model.vector)

    PushMatrix()

    # Translate and rotate shadow. Rotation around y axis only.
    Translate(model.position.x, 0, model.position.z)
    Rotate(rot.y, 0, 1, 0) if rot.y.nonzero?

    # Draw shadow.
    drawShadow() unless @shadow.nil?

    # Translate and rotate object. Rotate object around x and z axes (y
    # axis already done for shadow).
    Translate(0, model.position.y, 0)
    Rotate(rot.x, 1, 0, 0) if rot.x.nonzero?
    Rotate(rot.z, 0, 0, 1) if rot.z.nonzero?

    # Draw object.
    drawObject()

    PopMatrix()
end
drawObject() click to toggle source
# File examples/ruboids/ruboids/View.rb, line 31
def drawObject
    CallList(@object)
end
drawShadow() click to toggle source
# File examples/ruboids/ruboids/View.rb, line 35
def drawShadow
    CallList(@shadow) if @shadow
end
makeObject() click to toggle source
# File examples/ruboids/ruboids/View.rb, line 23
def makeObject
    raise "subclass should implement"
end
makeShadow() click to toggle source
# File examples/ruboids/ruboids/View.rb, line 27
def makeShadow
    # Don't raise error; some models may not have a shadow
end
shadowColorForHeight(height) click to toggle source

Given the height of an object, return a shadow color. The shadow color gets lighter as heigt increases.

# File examples/ruboids/ruboids/View.rb, line 70
def shadowColorForHeight(height)
    wh = $PARAMS['world_height']
    ratio = (height + wh / 2.0) / wh

    shadowColor = []
    SHADOW_COLOR.each_with_index { | c0, i |
        min = c0
        max = Canvas::GRASS_COLOR[i]
        if min > max
            tmp = min
            min = max
            max = tmp
        end
        shadowColor << min + ratio * (max - min)
    }
    return shadowColor
end