Class: Disp3D::Camera

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

Constant Summary collapse

PERSPECTIVE =
0
ORTHOGONAL =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCamera

Returns a new instance of Camera.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/camera.rb', line 19

def initialize()
  @rotate = Quat.from_axis(Vector3.new(1,0,0),0)
  @pre_translate = Vector3.new(0,0,0)
  @post_translate = Vector3.new(0,0,0)
  @eye = Vector3.new(0,0,1)
  @center = Vector3.new(0,0,0)
  @scale = 1.0

  @obj_rep_length = 10.0

  @angle = 30

  @projection = PERSPECTIVE
  @orth_scale = 1.0
end

Instance Attribute Details

#eyeObject (readonly)

Returns the value of attribute eye.



9
10
11
# File 'lib/camera.rb', line 9

def eye
  @eye
end

#obj_rep_lengthObject (readonly)

Returns the value of attribute obj_rep_length.



12
13
14
# File 'lib/camera.rb', line 12

def obj_rep_length
  @obj_rep_length
end

#post_translateObject

Returns the value of attribute post_translate.



7
8
9
# File 'lib/camera.rb', line 7

def post_translate
  @post_translate
end

#pre_translateObject

Returns the value of attribute pre_translate.



6
7
8
# File 'lib/camera.rb', line 6

def pre_translate
  @pre_translate
end

#projectionObject

Returns the value of attribute projection.



14
15
16
# File 'lib/camera.rb', line 14

def projection
  @projection
end

#rotateObject

Returns the value of attribute rotate.



5
6
7
# File 'lib/camera.rb', line 5

def rotate
  @rotate
end

#scaleObject

Returns the value of attribute scale.



10
11
12
# File 'lib/camera.rb', line 10

def scale
  @scale
end

Instance Method Details

#apply_attitudeObject



56
57
58
59
60
61
# File 'lib/camera.rb', line 56

def apply_attitude
  GL.Translate(pre_translate.x, pre_translate.y, pre_translate.z)
  apply_rotate
  GL.Scale(@scale, @scale, @scale)
  GL.Translate(post_translate.x, post_translate.y, post_translate.z)
end

#apply_positionObject



48
49
50
# File 'lib/camera.rb', line 48

def apply_position
  GLU.LookAt(@eye.x, @eye.y, @eye.z, @center.x, @center.y, @center.z, 0.0, 1.0, 0.0)
end

#apply_rotateObject



52
53
54
# File 'lib/camera.rb', line 52

def apply_rotate
  GL.MultMatrix(@rotate.to_array)
end

#fit(radius) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/camera.rb', line 112

def fit(radius)
  @obj_rep_length = radius
  fit_camera_pos
  @scale = 1.0
  dmy, dmy, w, h = viewport
  set_screen(w,h)
end

#fit_camera_posObject



120
121
122
123
124
125
126
127
# File 'lib/camera.rb', line 120

def fit_camera_pos
  # move camera pos for showing all objects
  dmy, dmy, w, h = viewport
  min_screen_size = [w, h].min
  eye_z = @obj_rep_length*(Math.sqrt(w*w+h*h)/min_screen_size.to_f)/(Math.tan(@angle/2.0*Math::PI/180.0))
  @eye = Vector3.new(0,0,eye_z)
  @orth_scale = @obj_rep_length/(min_screen_size.to_f)*2.0
end

#reshape(w, h) ⇒ Object



43
44
45
46
# File 'lib/camera.rb', line 43

def reshape(w,h)
  GL.Viewport(0.0,0.0,w,h)
  set_projection_for_world_scene
end

#set_projection_for_camera_sceneObject



82
83
84
85
86
87
# File 'lib/camera.rb', line 82

def set_projection_for_camera_scene
  GL.MatrixMode(GL::GL_PROJECTION)
  GL.LoadIdentity()
  dmy, dmy, w,h = viewport
  GL.Ortho(-w/2, w/2, -h/2, h/2, -100, 100)
end

#set_projection_for_world_sceneObject



75
76
77
78
79
80
# File 'lib/camera.rb', line 75

def set_projection_for_world_scene
  GL.MatrixMode(GL::GL_PROJECTION)
  GL.LoadIdentity()
  dmy, dmy, w,h = viewport
  set_screen(w,h)
end

#set_screen(w, h) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/camera.rb', line 63

def set_screen(w,h)
  if @projection == ORTHOGONAL
    GL.Ortho(-w*@orth_scale/2.0, w*@orth_scale/2.0, -h*@orth_scale/2.0, h*@orth_scale/2.0, -@obj_rep_length*10, @obj_rep_length*10)
  else
    GLU.Perspective(@angle, w.to_f()/h.to_f(), 0.1, @eye.z + @obj_rep_length*5.0)
  end
end

#switch_projectionObject



35
36
37
38
39
40
41
# File 'lib/camera.rb', line 35

def switch_projection
  if(@projection == PERSPECTIVE)
    @projection = ORTHOGONAL
  else
    @projection = PERSPECTIVE
  end
end

#unproject(screen_pos) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/camera.rb', line 89

def unproject(screen_pos)
  set_projection_for_world_scene

  GL.MatrixMode(GL::GL_MODELVIEW)
  GL.PushMatrix()
  GL.LoadIdentity()
  apply_position()
  vp = viewport
  projection = GL::GetDoublev(GL::PROJECTION_MATRIX)
  model_view = GL::GetDoublev(GL::MODELVIEW_MATRIX)
  GL.PopMatrix()

  unprojected = GLU::UnProject(screen_pos.x, vp[3] - screen_pos.y - 1, screen_pos.z, model_view, projection, vp)
  unprojected = Vector3.new(unprojected[0], unprojected[1], unprojected[2])

  unprojected -= @pre_translate
  rot_matrix = Matrix.from_quat(@rotate)
  unprojected = rot_matrix*unprojected
  unprojected /= @scale
  unprojected -= @post_translate
  return unprojected
end

#viewportObject



71
72
73
# File 'lib/camera.rb', line 71

def viewport
  return GL.GetIntegerv(GL::VIEWPORT)
end