Class: Disp3D::Manipulator

Inherits:
Object
  • Object
show all
Defined in:
lib/manipulator.rb

Instance Method Summary collapse

Constructor Details

#initialize(camera, picker) ⇒ Manipulator

Returns a new instance of Manipulator.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/manipulator.rb', line 6

def initialize(camera, picker)
  @camera = camera
  @picker = picker

  @start_x = 0
  @start_y = 0

  @rotating = false
  @scalling = false
  @translating = false
  @trackball_size = 0.8
end

Instance Method Details

#centering(scene_graph) ⇒ Object



19
20
21
22
# File 'lib/manipulator.rb', line 19

def centering(scene_graph)
  @camera.pre_translate = Vector3.new()
  set_rotation_ceter(scene_graph.center)
end

#fit(scene_graph) ⇒ Object



24
25
26
27
# File 'lib/manipulator.rb', line 24

def fit(scene_graph)
  centering(scene_graph)
  @camera.fit(scene_graph.radius)
end

#motion(x, y) ⇒ Object

return true if should be redisplay



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/manipulator.rb', line 68

def motion(x,y)
  dmy, dmy, width, height = @camera.viewport()
  if( @translating )
    delta_x = 2*((@start_x - x).to_f/width)
    delta_y = 2*((@start_y - y).to_f/height)
    @start_x = x
    @start_y = y

    @camera.pre_translate.x -= delta_x*@camera.obj_rep_length
    @camera.pre_translate.y += delta_y*@camera.obj_rep_length
    return true
  elsif ( @scalling )
    @camera.scale *= (1.0+(@start_y - y).to_f/height)
    @start_y = y
    return true
  elsif ( @rotating )
    @lastquat =trackball(
               (2.0 * @start_x - width) / width,
               (height - 2.0 * @start_y) / height,
               (2.0 * x - width) / width,
               (height - 2.0 * y) / height)
    @start_x = x
    @start_y = y
    @camera.rotate = @lastquat * @camera.rotate if( @lastquat )
    return true
  end
  return false
end

#mouse(button, state, x, y) ⇒ Object



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
# File 'lib/manipulator.rb', line 41

def mouse(button,state,x,y)
  if (state == GLUT::GLUT_DOWN &&
      ((button == GLUT::GLUT_RIGHT_BUTTON && @rotating == true) ||
      (button == GLUT::GLUT_LEFT_BUTTON && @scalling == true) ))
    # pushed left and right at the same time
    @translating = true
    @start_x = x
    @start_y = y
  elsif (button == GLUT::GLUT_RIGHT_BUTTON && state == GLUT::GLUT_DOWN)
    @scalling = true
    @start_y = y
  elsif ( button == GLUT::GLUT_LEFT_BUTTON && state == GLUT::GLUT_DOWN )
    @rotating = true
    @start_x = x
    @start_y = y
  elsif ( button == GLUT::GLUT_MIDDLE_BUTTON && state == GLUT::GLUT_DOWN )
    set_clicked_point_as_rotation_center(x,y)
  elsif ( button == GLUT::GLUT_RIGHT_BUTTON && state == GLUT::GLUT_UP )
    @scalling = false
    @translating = false
  elsif ( button == GLUT::GLUT_LEFT_BUTTON && state == GLUT::GLUT_UP )
    @rotating = false
    @translating = false
  end
end

#set_rotation_ceter(rot_center) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/manipulator.rb', line 29

def set_rotation_ceter(rot_center)
  scale = @camera.scale
  post_trans_vec = @camera.post_translate.to_column_vector
  pre_trans_vec = @camera.pre_translate.to_column_vector
  rot_mat = Matrix.from_quat(@camera.rotate)
  new_pre_translate = ( rot_mat.t * post_trans_vec * scale + pre_trans_vec ) +
    rot_mat.t * rot_center.to_column_vector * scale
  new_pre_translate = GMath3D::Vector3.new(new_pre_translate[0,0], new_pre_translate[1,0], new_pre_translate[2,0])
  @camera.pre_translate = new_pre_translate
  @camera.post_translate = rot_center*-1.0
end