Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import numpy as np
- class Camera(object):
- def __init__(self, aspect_ratio, fov_v, fov_h, ratio, near_dist, far_dist):
- self.aspect_ratio = aspect_ratio
- self.camera_position = np.array((0,0,0))
- self.camera_direction = np.array((0,0,-1))
- up_unit_vector = np.array((0,1,0))
- right_unit_vector = np.cross(up_unit_vector, self.camera_direction)
- near_height = 2*math.tan(fov_v /2.) * near_dist
- near_width = 2*math.tan(fov_h / 2.) * near_dist
- # near_width = near_height*ratio
- far_height = 2*math.tan(fov_v / 2.) * far_dist
- far_width = 2*math.tan(fov_h /2.) * far_dist
- # far_width = far_height*ratio
- far_centre_position = self.camera_position + (self.camera_direction * far_dist)
- near_centre_position = self.camera_position + (self.camera_direction * near_dist)
- self.far_top_left = far_centre_position + (up_unit_vector * far_height/2.) - (right_unit_vector * far_width/2.)
- self.far_top_right = far_centre_position + (up_unit_vector * far_height/2.) + (right_unit_vector * far_width/2.)
- self.far_bottom_right = far_centre_position - (up_unit_vector * far_height/2.) + (right_unit_vector * far_width/2.)
- self.far_bottom_left = far_centre_position - (up_unit_vector * far_height/2.) - (right_unit_vector * far_width/2.)
- # self.far_top_right = self.far_top_left + np.array((far_width, 0, 0))
- # self.far_bottom_right = self.far_top_right + np.array((0, -far_height, 0))
- # self.far_bottom_left = self.far_bottom_right + np.array((-far_width, 0, 0))
- self.near_top_left = near_centre_position + (up_unit_vector * near_height / 2.) - (right_unit_vector * near_width / 2.)
- self.near_top_right = near_centre_position + (up_unit_vector * near_height / 2.) + (right_unit_vector * near_width / 2.)
- self.near_bottom_right = near_centre_position - (up_unit_vector * near_height / 2.) - (right_unit_vector * near_width / 2.)
- self.near_bottom_left = near_centre_position - (up_unit_vector * near_height / 2.) - (right_unit_vector * near_width / 2.)
- # self.near_top_right = self.near_top_left + np.array((near_width, 0, 0))
- # self.near_bottom_right = self.near_top_right + np.array((0, -near_height, 0))
- # self.near_bottom_left = self.near_bottom_right + np.array((-near_width, 0, 0))
- self.frustum = {'ftl':self.far_top_left,
- 'ftr':self.far_top_left,
- 'fbr':self.far_bottom_right,
- 'fbl':self.far_bottom_left,
- 'ntl':self.near_top_left,
- 'ntr':self.near_top_right,
- 'nbr':self.near_bottom_right,
- 'nbl':self.near_bottom_left}
- if __name__ == '__main__':
- from matplotlib import pyplot
- import pylab
- from mpl_toolkits.mplot3d import Axes3D
- ASPECT_RATIO = 4./3
- NEAR_DIST = 10.
- FAR_DIST = 20.
- FOV_V = 0.523599 # 30 degrees
- FOV_H = 0.785398 # 45 degrees
- cam = Camera(ASPECT_RATIO, FOV_V, FOV_H, 30./40, NEAR_DIST, FAR_DIST)
- points = [[0,0,0]]
- for label, vertex in cam.frustum.iteritems():
- points.append(vertex)
- print('{}: {}'.format(label, vertex))
- points = np.array(points)
- fig = pylab.figure()
- ax = Axes3D(fig)
- ax.scatter(points[:,0], points[:,1], points[:,2])
- pyplot.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement