class GLWidget

Constants

NumSectors

Public Class Methods

new(parent = nil) click to toggle source
Calls superclass method
# File examples/opengl/grabber/glwidget.rb, line 41
def initialize(parent = nil)
    super(parent)
    @xRot = 0
    @yRot = 0
    @zRot = 0
    @gear1Rot = 0
    @lastPos = Qt::Point.new

    timer = Qt::Timer.new(self)
    connect(timer, SIGNAL('timeout()'), self, SLOT('advanceGears()'))
    timer.start(20)
end

Public Instance Methods

advanceGears() click to toggle source
# File examples/opengl/grabber/glwidget.rb, line 158
def advanceGears
    @gear1Rot += 2 * 16
    updateGL()
end
clearColor=(color) click to toggle source
# File examples/opengl/textures/glwidget.rb, line 67
def clearColor=(color)
    @clearColor = color
    updateGL()
end
dispose() click to toggle source
Calls superclass method
# File examples/opengl/grabber/glwidget.rb, line 58
def dispose()
    makeCurrent()
    GL.DeleteLists(@gear1, 1)
    GL.DeleteLists(@gear2, 1)
    GL.DeleteLists(@gear3, 1)
    super
end
drawGear(gear, dx, dy, dz, angle) click to toggle source
# File examples/opengl/grabber/glwidget.rb, line 248
def drawGear(gear, dx, dy, dz, angle)
    GL.PushMatrix
    GL.Translated(dx, dy, dz)
    GL.Rotated(angle, 0.0, 0.0, 1.0)
    GL.CallList(gear)
    GL.PopMatrix
end
extrude(x1, y1, x2, y2) click to toggle source
# File examples/opengl/hellogl/glwidget.rb, line 208
def extrude(x1, y1, x2, y2)
    qglColor(@trolltechGreen.dark(250 + (100 * x1)))

    GL.Vertex3d(x1, y1, +0.05)
    GL.Vertex3d(x2, y2, +0.05)
    GL.Vertex3d(x2, y2, -0.05)
    GL.Vertex3d(x1, y1, -0.05)
end
initializeGL() click to toggle source
# File examples/opengl/grabber/glwidget.rb, line 93
def initializeGL
    lightPos = [ 5.0, 5.0, 10.0, 1.0 ]
    reflectance1 = [ 0.8, 0.1, 0.0, 1.0 ]
    reflectance2 = [ 0.0, 0.8, 0.2, 1.0 ]
    reflectance3 = [ 0.2, 0.2, 1.0, 1.0 ]

    GL.Lightfv(GL::LIGHT0, GL::POSITION, lightPos)
    GL.Enable(GL::LIGHTING)
    GL.Enable(GL::LIGHT0)
    GL.Enable(GL::DEPTH_TEST)

    @gear1 = makeGear(reflectance1, 1.0, 4.0, 1.0, 0.7, 20)
    @gear2 = makeGear(reflectance2, 0.5, 2.0, 2.0, 0.7, 10)
    @gear3 = makeGear(reflectance3, 1.3, 2.0, 0.5, 0.7, 10)

    GL.Enable(GL::NORMALIZE)
end
makeGear(reflectance, innerRadius, outerRadius, thickness, toothSize, toothCount) click to toggle source
# File examples/opengl/grabber/glwidget.rb, line 163
def makeGear(reflectance, innerRadius,
                          outerRadius, thickness,
                          toothSize, toothCount)
    list = GL.GenLists(1)
    GL.NewList(list, GL::COMPILE)
    GL.Material(GL::FRONT, GL::AMBIENT_AND_DIFFUSE, reflectance)

    r0 = innerRadius
    r1 = outerRadius - toothSize / 2.0
    r2 = outerRadius + toothSize / 2.0
    delta = (2.0 * PI / toothCount) / 4.0
    z = thickness / 2.0

    GL.ShadeModel(GL::FLAT)

    for i in 0...2
        sign = (i == 0) ? +1.0 : -1.0

        GL.Normal3d(0.0, 0.0, sign)

        GL.Begin(GL::QUAD_STRIP)
        for j in 0..toothCount
            angle = 2.0 * PI * j / toothCount
            GL.Vertex3d(r0 * cos(angle), r0 * sin(angle), sign * z)
            GL.Vertex3d(r1 * cos(angle), r1 * sin(angle), sign * z)
            GL.Vertex3d(r0 * cos(angle), r0 * sin(angle), sign * z)
            GL.Vertex3d(r1 * cos(angle + 3 * delta), r1 * sin(angle + 3 * delta),
                        sign * z)
        end
        GL.End

        GL.Begin(GL::QUADS)
        for j in 0...toothCount
            angle = 2.0 * PI * j / toothCount
            GL.Vertex3d(r1 * cos(angle), r1 * sin(angle), sign * z)
            GL.Vertex3d(r2 * cos(angle + delta), r2 * sin(angle + delta),
                        sign * z)
            GL.Vertex3d(r2 * cos(angle + 2 * delta), r2 * sin(angle + 2 * delta),
                        sign * z)
            GL.Vertex3d(r1 * cos(angle + 3 * delta), r1 * sin(angle + 3 * delta),
                        sign * z)
        end
        GL.End
    end

    GL.Begin(GL::QUAD_STRIP)
    for i in 0...toothCount
        for j in 0...2
            angle = 2.0 * PI * (i + (j / 2.0)) / toothCount
            s1 = r1
            s2 = r2
            if j == 1
                s1, s2 = s2, s1
            end

            GL.Normal3d(cos(angle), sin(angle), 0.0)
            GL.Vertex3d(s1 * cos(angle), s1 * sin(angle), +z)
            GL.Vertex3d(s1 * cos(angle), s1 * sin(angle), -z)
    
            GL.Normal3d(s2 * sin(angle + delta) - s1 * sin(angle),
                        s1 * cos(angle) - s2 * cos(angle + delta), 0.0)
            GL.Vertex3d(s2 * cos(angle + delta), s2 * sin(angle + delta), +z)
            GL.Vertex3d(s2 * cos(angle + delta), s2 * sin(angle + delta), -z)
        end
    end
    GL.Vertex3d(r1, 0.0, +z)
    GL.Vertex3d(r1, 0.0, -z)
    GL.End

    GL.ShadeModel(GL::SMOOTH)

    GL.Begin(GL::QUAD_STRIP)
    for i in 0..toothCount
        angle = i * 2.0 * PI / toothCount
        GL.Normal3d(-cos(angle), -sin(angle), 0.0)
        GL.Vertex3d(r0 * cos(angle), r0 * sin(angle), +z)
        GL.Vertex3d(r0 * cos(angle), r0 * sin(angle), -z)
    end
    GL.End

    GL.EndList

    return list
end
makeObject() click to toggle source
# File examples/opengl/hellogl/glwidget.rb, line 143
def makeObject()
    list = GL.GenLists(1)
    GL.NewList(list, GL::COMPILE)

    GL.Begin(GL::QUADS)

    x1 = +0.06
    y1 = -0.14
    x2 = +0.14
    y2 = -0.06
    x3 = +0.08
    y3 = +0.00
    x4 = +0.30
    y4 = +0.22

    quad(x1, y1, x2, y2, y2, x2, y1, x1)
    quad(x3, y3, x4, y4, y4, x4, y3, x3)

    extrude(x1, y1, x2, y2)
    extrude(x2, y2, y2, x2)
    extrude(y2, x2, y1, x1)
    extrude(y1, x1, x1, y1)
    extrude(x3, y3, x4, y4)
    extrude(x4, y4, y4, x4)
    extrude(y4, x4, y3, x3)

    (0...NumSectors).each do |i|
        angle1 = (i * 2 * PI) / NumSectors
        x5 = 0.30 * sin(angle1)
        y5 = 0.30 * cos(angle1)
        x6 = 0.20 * sin(angle1)
        y6 = 0.20 * cos(angle1)

        angle2 = ((i + 1) * 2 * PI) / NumSectors
        x7 = 0.20 * sin(angle2)
        y7 = 0.20 * cos(angle2)
        x8 = 0.30 * sin(angle2)
        y8 = 0.30 * cos(angle2)

        quad(x5, y5, x6, y6, x7, y7, x8, y8)

        extrude(x6, y6, x7, y7)
        extrude(x8, y8, x5, y5)
    end

    GL.End()

    GL.EndList()
    return list
end
minimumSizeHint() click to toggle source
# File examples/opengl/hellogl/glwidget.rb, line 62
def minimumSizeHint()
    return Qt::Size.new(50, 50)
end
mouseMoveEvent(event) click to toggle source
# File examples/opengl/grabber/glwidget.rb, line 144
def mouseMoveEvent(event)
    dx = event.x - @lastPos.x
    dy = event.y - @lastPos.y

    if event.buttons & Qt::LeftButton.to_i != 0
        setXRotation(@xRot + 8 * dy)
        setYRotation(@yRot + 8 * dx)
    elsif event.buttons & Qt::RightButton.to_i != 0
        setXRotation(@xRot + 8 * dy)
        setZRotation(@zRot + 8 * dx)
    end
    @lastPos = event.pos
end
mousePressEvent(event) click to toggle source
# File examples/opengl/grabber/glwidget.rb, line 140
def mousePressEvent(event)
    @lastPos = event.pos
end
mouseReleaseEvent(event) click to toggle source
# File examples/opengl/textures/glwidget.rb, line 120
def mouseReleaseEvent(event)
    emit clicked()
end
normalizeAngle(angle) click to toggle source
# File examples/opengl/grabber/glwidget.rb, line 256
def normalizeAngle(angle)
    while angle < 0 do
        angle += 360 * 16
    end
    while angle > 360 * 16 do
        angle -= 360 * 16
    end
    return angle
end
paintGL() click to toggle source
# File examples/opengl/grabber/glwidget.rb, line 111
def paintGL()
    GL.Clear(GL::COLOR_BUFFER_BIT | GL::DEPTH_BUFFER_BIT)

    GL.PushMatrix
    GL.Rotated(@xRot / 16.0, 1.0, 0.0, 0.0)
    GL.Rotated(@yRot / 16.0, 0.0, 1.0, 0.0)
    GL.Rotated(@zRot / 16.0, 0.0, 0.0, 1.0)

    drawGear(@gear1, -3.0, -2.0, 0.0, @gear1Rot / 16.0)
    drawGear(@gear2, +3.1, -2.0, 0.0, -2.0 * (@gear1Rot / 16.0) - 9.0)

    GL.Rotated(+90.0, 1.0, 0.0, 0.0)
    drawGear(@gear3, -3.1, -1.8, -2.2, +2.0 * (@gear1Rot / 16.0) - 2.0)

    GL.PopMatrix
end
quad(x1, y1, x2, y2, x3, y3, x4, y4) click to toggle source
# File examples/opengl/hellogl/glwidget.rb, line 194
def quad(x1, y1, x2, y2, x3, y3, x4, y4)
    qglColor(@trolltechGreen)

    GL.Vertex3d(x1, y1, -0.05)
    GL.Vertex3d(x2, y2, -0.05)
    GL.Vertex3d(x3, y3, -0.05)
    GL.Vertex3d(x4, y4, -0.05)

    GL.Vertex3d(x4, y4, +0.05)
    GL.Vertex3d(x3, y3, +0.05)
    GL.Vertex3d(x2, y2, +0.05)
    GL.Vertex3d(x1, y1, +0.05)
end
resizeGL(width, height) click to toggle source
# File examples/opengl/grabber/glwidget.rb, line 128
def resizeGL(width, height)
    side = [width, height].min
    GL.Viewport((width - side) / 2, (height - side) / 2, side, side)

    GL.MatrixMode(GL::PROJECTION)
    GL.LoadIdentity
    GL.Frustum(-1.0, +1.0, -1.0, 1.0, 5.0, 60.0)
    GL.MatrixMode(GL::MODELVIEW)
    GL.LoadIdentity
    GL.Translated(0.0, 0.0, -40.0)
end
rotateBy(xAngle, yAngle, zAngle) click to toggle source
# File examples/opengl/textures/glwidget.rb, line 60
def rotateBy(xAngle, yAngle, zAngle)
    @xRot += xAngle
    @yRot += yAngle
    @zRot += zAngle
    updateGL()
end
setXRotation(angle) click to toggle source
# File examples/opengl/grabber/glwidget.rb, line 66
def setXRotation(angle)
    angle = normalizeAngle(angle)
    if angle != @xRot
        @xRot = angle
        emit xRotationChanged(angle)
        updateGL()
    end
end
setYRotation(angle) click to toggle source
# File examples/opengl/grabber/glwidget.rb, line 75
def setYRotation(angle)
    angle = normalizeAngle(angle)
    if angle != @yRot
        @yRot = angle
        emit yRotationChanged(angle)
        updateGL()
    end
end
setZRotation(angle) click to toggle source
# File examples/opengl/grabber/glwidget.rb, line 84
def setZRotation(angle)
    angle = normalizeAngle(angle)
    if angle != @zRot
        @zRot = angle
        emit zRotationChanged(angle)
        updateGL()
    end
end
sizeHint() click to toggle source
# File examples/opengl/hellogl/glwidget.rb, line 66
def sizeHint()
    return Qt::Size.new(400, 400)
end
xRotation() click to toggle source
# File examples/opengl/grabber/glwidget.rb, line 54
def xRotation() return @xRot end
yRotation() click to toggle source
# File examples/opengl/grabber/glwidget.rb, line 55
def yRotation() return @yRot end
zRotation() click to toggle source
# File examples/opengl/grabber/glwidget.rb, line 56
def zRotation() return @zRot end