Class: Canvas

Inherits:
Qt::GLWidget show all
Defined in:
ext/ruby/qtruby/examples/ruboids/ruboids/Canvas.rb

Constant Summary collapse

GRASS_COLOR =
[0, 0.75, 0]
MDA_ROTATE =
:MDA_ROTATE
MDA_ZOOM =
:MDA_ZOOM
MDA_CHANGE_FOCUS =
:MDA_CHANGE_FOCUS

Instance Method Summary collapse

Methods inherited from Qt::GLWidget

#format

Methods inherited from Qt::Base

#%, #&, #*, #**, #+, #-, #-@, #/, #<, #<<, #<=, #==, #>, #>=, #>>, #QCOMPARE, #QEXPECT_FAIL, #QFAIL, #QSKIP, #QTEST, #QVERIFY, #QVERIFY2, #QWARN, #^, ancestors, #is_a?, #methods, private_slots, #protected_methods, #public_methods, q_classinfo, q_signal, q_slot, signals, #singleton_methods, slots, #|, #~

Constructor Details

#initialize(parent = nil, name = '') ⇒ Canvas

Returns a new instance of Canvas.



25
26
27
28
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/Canvas.rb', line 25

def initialize(parent = nil, name = '')
    super(parent)
    @grassObject = nil
end

Instance Method Details

#initializeGLObject



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

def initializeGL()
    ClearColor(0.4, 0.4, 1.0, 0.0) # Let OpenGL clear to light blue
    @grassObject = makeGrassObject()
    ShadeModel(FLAT)
end

#makeGrassObjectObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/Canvas.rb', line 81

def makeGrassObject
    halfXSize = $PARAMS['world_width']
    halfYSize = $PARAMS['world_depth'] / 2
    halfZSize = $PARAMS['world_height']

    list = GenLists(1)
    NewList(list, COMPILE)
    LineWidth(2.0)
    Begin(QUADS)

    Color(GRASS_COLOR)
    # Counter-clockwise
    Vertex( halfXSize, -halfYSize,  halfZSize)
    Vertex(-halfXSize, -halfYSize,  halfZSize)
    Vertex(-halfXSize, -halfYSize, -halfZSize)
    Vertex( halfXSize, -halfYSize, -halfZSize)

    End()
    EndList()
    return list
end

#mouseMoveEvent(e) ⇒ Object

Rotate around sphere with right (#2) button. Zoom with left button. Change focus with left button.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/Canvas.rb', line 117

def mouseMoveEvent(e)
    return if @mouseLoc.nil?

    dx = dy = 0
    if e.x() != @mouseLoc.x()
        dx = e.x() - @mouseLoc.x() # move right increases dx
        @mouseLoc.setX(e.x())
    end
    if e.y() != @mouseLoc.y()
        dy = @mouseLoc.y() - e.y() # move up increases dy
        @mouseLoc.setY(e.y())
    end

    return if dx == 0 && dy == 0

    case @mouseDragAction
    when MDA_ZOOM
        return if (dy == 0)
        World.instance.camera.zoom += 0.1 * -dy
    when MDA_ROTATE
        break
    when MDA_CHANGE_FOCUS
        break
    end
    World.instance.setupTranslation()
end

#mousePressEvent(e) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/Canvas.rb', line 103

def mousePressEvent(e)
    @mouseLoc = e.pos()
    case e.button()
    when Qt::LeftButton
        @mouseDragAction = MDA_ZOOM
    when Qt::RightButton
        @mouseDragAction = MDA_ROTATE
    when Qt::MidButton
        @mouseDragAction = MDA_CHANGE_FOCUS
    end
end

#paintGLObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/Canvas.rb', line 40

def paintGL()
    Enable(DEPTH_TEST)
    Clear(COLOR_BUFFER_BIT | DEPTH_BUFFER_BIT)

    MatrixMode(MODELVIEW)

    camera = World.instance.camera

    LoadIdentity()
    Rotate(camera.rotation.x, 1, 0, 0)
    Rotate(camera.rotation.y, 0, 1, 0)
    Rotate(camera.rotation.z, 0, 0, 1.0)
    Translate(-camera.position.x, -camera.position.y, -camera.position.z)
    Scale(camera.zoom, camera.zoom, camera.zoom)

    CallList(@grassObject)

    World.instance.clouds.each { | cloud | cloud.draw() }
    World.instance.flock.draw()
end

#resizeGL(w, h) ⇒ Object

Set up the OpenGL view port, matrix mode, etc.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/Canvas.rb', line 62

def resizeGL(w, h)
    Viewport(0, 0, w, h)
    MatrixMode(PROJECTION)
    LoadIdentity()

#          # left, right, bottom, top, front, back (focal_length)
    halfXSize = $PARAMS['world_width'] / 2 * 1.25
    halfYSize = $PARAMS['world_height'] / 2 * 1.25
    halfZSize = $PARAMS['world_depth'] / 2 * 1.25

#            Frustum(-halfXSize, halfXSize, -halfYSize, halfYSize,
#                  5, halfZSize * 2)

    Ortho(-halfXSize, halfXSize, -halfYSize, halfYSize,
          -halfZSize, halfZSize)

    MatrixMode(MODELVIEW)
end

#updateObject



30
31
32
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/Canvas.rb', line 30

def update
    updateGL()
end