Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.43 KB | None | 0 0
  1. import math
  2. import numpy as np
  3.  
  4.  
  5. class Camera(object):
  6.     def __init__(self, aspect_ratio, fov_v, fov_h, ratio, near_dist, far_dist):
  7.         self.aspect_ratio = aspect_ratio
  8.         self.camera_position = np.array((0,0,0))
  9.         self.camera_direction = np.array((0,0,-1))
  10.  
  11.         up_unit_vector = np.array((0,1,0))
  12.         right_unit_vector = np.cross(up_unit_vector, self.camera_direction)
  13.  
  14.         near_height = 2*math.tan(fov_v /2.) * near_dist
  15.         near_width = 2*math.tan(fov_h / 2.) * near_dist
  16.         # near_width = near_height*ratio
  17.         far_height = 2*math.tan(fov_v / 2.) * far_dist
  18.         far_width = 2*math.tan(fov_h /2.) * far_dist
  19.         # far_width = far_height*ratio
  20.  
  21.         far_centre_position = self.camera_position + (self.camera_direction * far_dist)
  22.         near_centre_position = self.camera_position + (self.camera_direction * near_dist)
  23.  
  24.         self.far_top_left = far_centre_position + (up_unit_vector * far_height/2.) - (right_unit_vector * far_width/2.)
  25.         self.far_top_right = far_centre_position + (up_unit_vector * far_height/2.) + (right_unit_vector * far_width/2.)
  26.         self.far_bottom_right = far_centre_position - (up_unit_vector * far_height/2.) + (right_unit_vector * far_width/2.)
  27.         self.far_bottom_left = far_centre_position - (up_unit_vector * far_height/2.) - (right_unit_vector * far_width/2.)
  28.         # self.far_top_right = self.far_top_left + np.array((far_width, 0, 0))
  29.         # self.far_bottom_right = self.far_top_right + np.array((0, -far_height, 0))
  30.         # self.far_bottom_left = self.far_bottom_right + np.array((-far_width, 0, 0))
  31.  
  32.         self.near_top_left = near_centre_position + (up_unit_vector * near_height / 2.) - (right_unit_vector * near_width / 2.)
  33.         self.near_top_right = near_centre_position + (up_unit_vector * near_height / 2.) + (right_unit_vector * near_width / 2.)
  34.         self.near_bottom_right = near_centre_position - (up_unit_vector * near_height / 2.) - (right_unit_vector * near_width / 2.)
  35.         self.near_bottom_left = near_centre_position - (up_unit_vector * near_height / 2.) - (right_unit_vector * near_width / 2.)
  36.         # self.near_top_right = self.near_top_left + np.array((near_width, 0, 0))
  37.         # self.near_bottom_right = self.near_top_right + np.array((0, -near_height, 0))
  38.         # self.near_bottom_left = self.near_bottom_right + np.array((-near_width, 0, 0))
  39.  
  40.         self.frustum = {'ftl':self.far_top_left,
  41.                         'ftr':self.far_top_left,
  42.                         'fbr':self.far_bottom_right,
  43.                         'fbl':self.far_bottom_left,
  44.                         'ntl':self.near_top_left,
  45.                         'ntr':self.near_top_right,
  46.                         'nbr':self.near_bottom_right,
  47.                         'nbl':self.near_bottom_left}
  48.  
  49.  
  50. if __name__ == '__main__':
  51.     from matplotlib import pyplot
  52.     import pylab
  53.     from mpl_toolkits.mplot3d import Axes3D
  54.  
  55.     ASPECT_RATIO = 4./3
  56.     NEAR_DIST = 10.
  57.     FAR_DIST = 20.
  58.     FOV_V = 0.523599 # 30 degrees
  59.     FOV_H = 0.785398 # 45 degrees
  60.  
  61.     cam = Camera(ASPECT_RATIO, FOV_V, FOV_H, 30./40, NEAR_DIST, FAR_DIST)
  62.  
  63.     points = [[0,0,0]]
  64.     for label, vertex in cam.frustum.iteritems():
  65.         points.append(vertex)
  66.         print('{}: {}'.format(label, vertex))
  67.  
  68.     points = np.array(points)
  69.  
  70.     fig = pylab.figure()
  71.     ax = Axes3D(fig)
  72.  
  73.     ax.scatter(points[:,0], points[:,1], points[:,2])
  74.     pyplot.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement