Class: View

Inherits:
Object show all
Defined in:
ext/ruby/qtruby/examples/ruboids/ruboids/View.rb

Overview

A lightweight view

Direct Known Subclasses

BoidView, CloudView

Constant Summary collapse

SHADOW_COLOR =
[ 0.25, 0.25, 0.25 ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, color = nil) ⇒ View

Returns a new instance of View.



15
16
17
18
19
20
21
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/View.rb', line 15

def initialize(model, color = nil)
	super()
	@model = model
	@color = color
	@object = nil
	@shadow = nil
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



13
14
15
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/View.rb', line 13

def color
  @color
end

#modelObject

Returns the value of attribute model.



13
14
15
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/View.rb', line 13

def model
  @model
end

#objectObject

Returns the value of attribute object.



13
14
15
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/View.rb', line 13

def object
  @object
end

#shadowObject

Returns the value of attribute shadow.



13
14
15
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/View.rb', line 13

def shadow
  @shadow
end

Instance Method Details

#drawObject



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 'ext/ruby/qtruby/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

#drawObjectObject



31
32
33
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/View.rb', line 31

def drawObject
	CallList(@object)
end

#drawShadowObject



35
36
37
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/View.rb', line 35

def drawShadow
	CallList(@shadow) if @shadow
end

#makeObjectObject



23
24
25
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/View.rb', line 23

def makeObject
	raise "subclass should implement"
end

#makeShadowObject



27
28
29
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/View.rb', line 27

def makeShadow
	# Don't raise error; some models may not have a shadow
end

#shadowColorForHeight(height) ⇒ Object

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'ext/ruby/qtruby/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