class Disp3D::Camera

Constants

ORTHOGONAL
PERSPECTIVE

Attributes

eye[R]
obj_rep_length[R]
post_translate[RW]
pre_translate[RW]
projection[RW]
rotate[RW]
scale[RW]

Public Class Methods

new() click to toggle source
# 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

Public Instance Methods

apply_attitude() click to toggle source
# 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_position() click to toggle source
# 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_rotate() click to toggle source
# File lib/camera.rb, line 52
def apply_rotate
  GL.MultMatrix(@rotate.to_array)
end
fit(radius) click to toggle source
# 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_pos() click to toggle source
# 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) click to toggle source
# 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_scene() click to toggle source
# 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_scene() click to toggle source
# 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) click to toggle source
# 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_projection() click to toggle source
# File lib/camera.rb, line 35
def switch_projection
  if(@projection == PERSPECTIVE)
    @projection = ORTHOGONAL
  else
    @projection = PERSPECTIVE
  end
end
unproject(screen_pos) click to toggle source
# 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
viewport() click to toggle source
# File lib/camera.rb, line 71
def viewport
  return GL.GetIntegerv(GL::VIEWPORT)
end