Class: RetroRender::Camera

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Camera

ARGS :

Hash that can take the following keys :

Parameters:

  • :mode (Symbol)

    (defaults to: :fps)

  • :position (Vector3)

    (defaults to: Vector3.new(0.0, 0.0, 0.0))

  • :target (Vector3)

    (defaults to: Vector3.new(0.0, 0.0, 0.0))

  • :angles (Vector3)

    (defaults to: Vector3.new(0.0, 0.0, 0.0))

  • :fovy (Float)

    (defaults to: 45.0)

  • :ratio (Float)

    (defaults to: 1.333)

  • :near (Float)

    (defaults to: 0.1)

  • :far (Float)

    (defaults to: 1000.0)

  • :distance (Float)

    (defaults to: 64.0 - distance between the position and the target)



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

#anglesObject

Returns the value of attribute angles.



83
84
85
# File 'lib/retro_render.rb', line 83

def angles
  @angles
end

#distanceObject

Returns the value of attribute distance.



83
84
85
# File 'lib/retro_render.rb', line 83

def distance
  @distance
end

#farObject

Returns the value of attribute far.



83
84
85
# File 'lib/retro_render.rb', line 83

def far
  @far
end

#fovyObject

Returns the value of attribute fovy.



83
84
85
# File 'lib/retro_render.rb', line 83

def fovy
  @fovy
end

#nearObject

Returns the value of attribute near.



83
84
85
# File 'lib/retro_render.rb', line 83

def near
  @near
end

#positionObject

Returns the value of attribute position.



83
84
85
# File 'lib/retro_render.rb', line 83

def position
  @position
end

#ratioObject

Returns the value of attribute ratio.



83
84
85
# File 'lib/retro_render.rb', line 83

def ratio
  @ratio
end

#targetObject

Returns the value of attribute target.



83
84
85
# File 'lib/retro_render.rb', line 83

def target
  @target
end

Instance Method Details

#lookObject

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

#updateObject



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