Class: RetroRender::Camera
- Inherits:
-
Object
- Object
- RetroRender::Camera
- Defined in:
- lib/retro_render.rb
Instance Attribute Summary collapse
-
#angles ⇒ Object
Returns the value of attribute angles.
-
#distance ⇒ Object
Returns the value of attribute distance.
-
#far ⇒ Object
Returns the value of attribute far.
-
#fovy ⇒ Object
Returns the value of attribute fovy.
-
#near ⇒ Object
Returns the value of attribute near.
-
#position ⇒ Object
Returns the value of attribute position.
-
#ratio ⇒ Object
Returns the value of attribute ratio.
-
#target ⇒ Object
Returns the value of attribute target.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ Camera
constructor
ARGS : Hash that can take the following keys :.
-
#look ⇒ Object
Sets the OpenGL matrices according to the camera.
- #move_backward(velocity) ⇒ Object
- #move_forward(velocity) ⇒ Object
- #move_left(velocity) ⇒ Object
- #move_right(velocity) ⇒ Object
- #rotate(axis, value) ⇒ Object
- #update ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Camera
ARGS :
Hash that can take the following keys :
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/retro_render.rb', line 95 def initialize(args = {}) @mode = args.has_key?(:mode) ? args[:mode] : :fps @position = args.has_key?(:position) ? args[:position] : Vector3.new @target = args.has_key?(:target) ? args[:target] : Vector3.new @angles = args.has_key?(:angles) ? args[:angles] : Vector3.new @fovy = args.has_key?(:fovy) ? args[:fovy] : 45.0 @ratio = args.has_key?(:ratio) ? args[:ratio] : 1.333 @near = args.has_key?(:near) ? args[:near] : 0.1 @far = args.has_key?(:far) ? args[:far] : 1000.0 @distance = args.has_key?(:distance) ? args[:distance] : 64.0 @deg_to_rad = Math::PI / 180.0 end |
Instance Attribute Details
#angles ⇒ Object
Returns the value of attribute angles.
83 84 85 |
# File 'lib/retro_render.rb', line 83 def angles @angles end |
#distance ⇒ Object
Returns the value of attribute distance.
83 84 85 |
# File 'lib/retro_render.rb', line 83 def distance @distance end |
#far ⇒ Object
Returns the value of attribute far.
83 84 85 |
# File 'lib/retro_render.rb', line 83 def far @far end |
#fovy ⇒ Object
Returns the value of attribute fovy.
83 84 85 |
# File 'lib/retro_render.rb', line 83 def fovy @fovy end |
#near ⇒ Object
Returns the value of attribute near.
83 84 85 |
# File 'lib/retro_render.rb', line 83 def near @near end |
#position ⇒ Object
Returns the value of attribute position.
83 84 85 |
# File 'lib/retro_render.rb', line 83 def position @position end |
#ratio ⇒ Object
Returns the value of attribute ratio.
83 84 85 |
# File 'lib/retro_render.rb', line 83 def ratio @ratio end |
#target ⇒ Object
Returns the value of attribute target.
83 84 85 |
# File 'lib/retro_render.rb', line 83 def target @target end |
Instance Method Details
#look ⇒ Object
Sets the OpenGL matrices according to the camera
177 178 179 180 181 182 183 184 |
# File 'lib/retro_render.rb', line 177 def look glMatrixMode(GL_PROJECTION) glLoadIdentity gluPerspective(@fovy, @ratio, @near, @far) glMatrixMode(GL_MODELVIEW) glLoadIdentity gluLookAt(@position.x, @position.y, @position.z, @target.x, @target.y, @target.z, 0, 1, 0) end |
#move_backward(velocity) ⇒ Object
138 139 140 141 142 143 144 145 146 |
# File 'lib/retro_render.rb', line 138 def move_backward(velocity) if @mode == :fps @position.x -= velocity * Math::cos(@angles.x * @deg_to_rad) @position.z -= velocity * Math::sin(@angles.x * @deg_to_rad) else @target.x -= velocity * Math::cos(@angles.x * @deg_to_rad) @target.z -= velocity * Math::sin(@angles.x * @deg_to_rad) end end |
#move_forward(velocity) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/retro_render.rb', line 108 def move_forward(velocity) if @mode == :fps @position.x += velocity * Math::cos(@angles.x * @deg_to_rad) @position.z += velocity * Math::sin(@angles.x * @deg_to_rad) else @target.x += velocity * Math::cos(@angles.x * @deg_to_rad) @target.z += velocity * Math::sin(@angles.x * @deg_to_rad) end end |
#move_left(velocity) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/retro_render.rb', line 118 def move_left(velocity) if @mode == :fps @position.x += velocity * Math::cos((@angles.x - 90.0) * @deg_to_rad) @position.z += velocity * Math::sin((@angles.x - 90.0) * @deg_to_rad) else @target.x += velocity * Math::cos((@angles.x - 90.0) * @deg_to_rad) @target.z += velocity * Math::sin((@angles.x - 90.0) * @deg_to_rad) end end |
#move_right(velocity) ⇒ Object
128 129 130 131 132 133 134 135 136 |
# File 'lib/retro_render.rb', line 128 def move_right(velocity) if @mode == :fps @position.x -= velocity * Math::cos((@angles.x - 90.0) * @deg_to_rad) @position.z -= velocity * Math::sin((@angles.x - 90.0) * @deg_to_rad) else @target.x -= velocity * Math::cos((@angles.x - 90.0) * @deg_to_rad) @target.z -= velocity * Math::sin((@angles.x - 90.0) * @deg_to_rad) end end |
#rotate(axis, value) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/retro_render.rb', line 148 def rotate(axis, value) if axis == :x @angles.x += value elsif axis == :y @angles.y += value elsif axis == :z @angles.z += value else raise("axis must be a symbol as :x, :y or :z") end end |
#update ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/retro_render.rb', line 160 def update if @mode == :fps @target.x = @position.x + @distance * Math::cos(@angles.y * @deg_to_rad) @target.z = @position.z + @distance * Math::cos(@angles.y * @deg_to_rad) @target.x = @position.x + @distance * Math::cos(@angles.x * @deg_to_rad) @target.y = @position.y + @distance * Math::sin(@angles.y * @deg_to_rad) @target.z = @position.z + @distance * Math::sin(@angles.x * @deg_to_rad) else @position.x = @target.x - @distance * Math::cos(@angles.y * @deg_to_rad) @position.z = @target.z - @distance * Math::cos(@angles.y * @deg_to_rad) @position.x = @target.x - @distance * Math::cos(@angles.x * @deg_to_rad) @position.y = @target.y - @distance * Math::sin(@angles.y * @deg_to_rad) @position.z = @target.z - @distance * Math::sin(@angles.x * @deg_to_rad) end end |