Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. from geometry import Point
  2. from matrix import Matrix, MatrixFromList, MatrixFromPoint
  3. import math
  4. from polygon import Polygon
  5.  
  6. class Scene:
  7. def __init__(self):
  8. self.modelMatrices = [identity()]
  9. self.vertices = []
  10. self.lines = []
  11. self.polys = []
  12. def pushMatrix(self):
  13. self.modelMatrices.append(self.modelMatrices[-1].copy())
  14. def popMatrix(self):
  15. self.modelMatrices.pop()
  16. def translate(self, x,y,z):
  17. self.modelMatrices[-1] *= getTranslateMatrix(x,y,z)
  18. def scale(self, x,y,z):
  19. self.modelMatrices[-1]*= getScaleMatrix(x,y,z)
  20. def rotate(self, angle, x, y, z):
  21. self.modelMatrices[-1] *= getRotationMatrix(angle, x, y, z)
  22. def vertex(self, p):
  23. v = MatrixFromPoint(p)
  24. v = self.modelMatrices[-1] * v
  25. self.vertices.append(v)
  26. def line(self, p1, p2):
  27. p1 = self.modelMatrices[-1] * MatrixFromPoint(p1)
  28. p2 = self.modelMatrices[-1] * MatrixFromPoint(p2)
  29. self.lines.append((p1, p2))
  30. def polygon(self, *points, **kargs):
  31. points = [self.modelMatrices[-1] * MatrixFromPoint(p) for p in points]
  32. self.polys.append(Polygon(points, **kargs))
  33. def projected(self):
  34. """
  35. outputs a dict containing the projected elements of the scene.
  36. keys are: "vertices", "lines", "triangles"
  37. """
  38. projection = getProjectionMatrix(-1, 1, -1, 1, 2, 10)
  39. ret = {}
  40. ret["vertices"] = [projection * v for v in self.vertices]
  41. ret["lines"] = [[projection * p for p in l] for l in self.lines]
  42. ret["polygons"] = [poly.mult(projection) for poly in self.polys]
  43. return ret
  44.  
  45. def identity():
  46. return MatrixFromList([
  47. [1,0,0,0],
  48. [0,1,0,0],
  49. [0,0,1,0],
  50. [0,0,0,1]
  51. ])
  52.  
  53. def getTranslateMatrix(x,y,z):
  54. return MatrixFromList([
  55. [1,0,0,x],
  56. [0,1,0,y],
  57. [0,0,1,z],
  58. [0,0,0,1]
  59. ])
  60.  
  61. def getScaleMatrix(x,y,z):
  62. return MatrixFromList([
  63. [x,0,0,0],
  64. [0,y,0,0],
  65. [0,0,z,0],
  66. [0,0,0,1]
  67. ])
  68.  
  69. def getRotationMatrix(angle, x, y, z):
  70. c = math.cos(angle)
  71. s = math.sin(angle)
  72. m = Matrix(4,4)
  73. #magic values courtesy of:
  74. #http://www.opengl.org/sdk/docs/man2/xhtml/glRotate.xml
  75. return MatrixFromList([
  76. [x*x*(1-c)+c , x*y*(1-c)-z*s, x*z*(1-c)+y*s, 0],
  77. [y*x*(1-c)+z*s, y*y*(1-c)+c , y*z*(1-c)-x*s, 0],
  78. [x*z*(1-c)-y*s, y*z*(1-c)+x*s, z*z*(1-c)+c , 0],
  79. [0 , 0 , 0 , 1]
  80. ])
  81.  
  82. def getProjectionMatrix(left, right, bottom, top, near, far):
  83. left, right, bottom, top, near, far = map(float, [left, right, bottom, top, near, far])
  84. a = (right + left) / (right - left)
  85. b = (top + bottom) / (top - bottom)
  86. c = -(far + near) / (far - near)
  87. d = -(2*far*near)/(far-near)
  88. p = 2 * near / (right - left)
  89. q = 2 * near / (top - bottom)
  90. return MatrixFromList([
  91. [p,0, a,0],
  92. [0,q, b,0],
  93. [0,0, c,d],
  94. [0,0,-1,0]
  95. ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement