Class: RAGE::Camera

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

Overview

Camera is the end user’s view into the world. It is instantiated as part of a viewport and controls the projection matrix through which the 3D world is viewed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCamera

Returns a new instance of Camera.



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

def initialize
  # FIXME this needs to be parameterized more

  # initialize projection params from window
  depth = (Window.width + Window.height)/4
  @projection = [45, Window.width / Window.height, 1, depth]

  # initial position
  @pos    = [0, 0, 1]

  # initial rotation params
  @xrotate, @yrotate, @zrotate = 0,0,0
end

Instance Attribute Details

#posObject

Camera coordinates



17
18
19
# File 'lib/rage/camera.rb', line 17

def pos
  @pos
end

#projectionObject

Projection matrix to use, managed internally



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

def projection
  @projection
end

#xrotateObject

Degrees to rotate camera around x,y,z axes



20
21
22
# File 'lib/rage/camera.rb', line 20

def xrotate
  @xrotate
end

#yrotateObject

Degrees to rotate camera around x,y,z axes



20
21
22
# File 'lib/rage/camera.rb', line 20

def yrotate
  @yrotate
end

#zrotateObject

Degrees to rotate camera around x,y,z axes



20
21
22
# File 'lib/rage/camera.rb', line 20

def zrotate
  @zrotate
end

Instance Method Details

#drawObject

Invoked during draw cycle to setup projection matrix



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rage/camera.rb', line 37

def draw
 # setup projection matrix
 Gl.glMatrixMode(Gl::GL_PROJECTION)
 Gl.glLoadIdentity

 # TODO at some point make this more modular, allowing user to
 # select glOrtho or glFrustrum w/ configurable params
 Glu.gluPerspective(*@projection)

 Gl.glMatrixMode(Gl::GL_MODELVIEW)
 Gl.glLoadIdentity

 # translate the world by inverse camera position
 Gl.glTranslatef *(@pos.collect { |p| p * -1 })

 # rotate camera according to params
 Gl.glRotatef(@xrotate, 1, 0, 0)
 Gl.glRotatef(@yrotate, 0, 1, 0)
 Gl.glRotatef(@zrotate, 0, 0, 1)
end