Updated how the vertices and matrices can be accessed. That is via indices ([x][y]) or just by named properties (like x or y).
This commit is contained in:
@@ -30,7 +30,7 @@ Matrix3x3(Vector3d(1.0,0.0,1.0),Vector3d(0.0,0.0,0.0),Vector3d(1.0,0.0,0.0))
|
||||
def main():
|
||||
#tris.load_obj_file('teddy.obj')
|
||||
ui.thetax = 1
|
||||
ui.thetay = 1
|
||||
ui.thetaz = 1
|
||||
ui.window.bind('<Left>', left)
|
||||
ui.window.bind('<Right>', right)
|
||||
ui.window.bind('<Up>', up)
|
||||
@@ -42,19 +42,19 @@ def main():
|
||||
ui.show()
|
||||
|
||||
def left(event):
|
||||
ui.thetax -= .1
|
||||
ui.thetaz += .1
|
||||
draw_scene()
|
||||
|
||||
def right(event):
|
||||
ui.thetax += .1
|
||||
ui.thetaz -= .1
|
||||
draw_scene()
|
||||
|
||||
def up(event):
|
||||
ui.thetay += .1
|
||||
ui.thetax += .1
|
||||
draw_scene()
|
||||
|
||||
def down(event):
|
||||
ui.thetay -= .1
|
||||
ui.thetax -= .1
|
||||
draw_scene()
|
||||
|
||||
def mouse_start(event):
|
||||
@@ -68,9 +68,9 @@ def mouse_end(event):
|
||||
ui.thetax += .01
|
||||
|
||||
if y < 0:
|
||||
ui.thetay -= .01
|
||||
ui.thetaz -= .01
|
||||
else:
|
||||
ui.thetay += .01
|
||||
ui.thetaz += .01
|
||||
|
||||
draw_scene()
|
||||
#ui.theta += x / ui.width
|
||||
@@ -81,18 +81,25 @@ def draw_scene():
|
||||
aspect_ratio = ui.height / ui.width
|
||||
|
||||
projection_matrix = Matrix4x4.get_projection_matrix(fov, aspect_ratio, near_plane, far_plane)
|
||||
|
||||
rotation_matrix_y = Matrix4x4.get_y_rotation_matrix(ui.thetax)
|
||||
|
||||
rotation_matrix_x = Matrix4x4.get_x_rotation_matrix(ui.thetay)
|
||||
rotation_matrix_z = Matrix4x4.get_y_rotation_matrix(ui.thetaz * .5)
|
||||
|
||||
rotation_matrix_x = Matrix4x4.get_x_rotation_matrix(ui.thetax)
|
||||
|
||||
#translation = Matrix4x4.get_translation_matrix(Vector3d(0, 0, 16))
|
||||
|
||||
#world_matrix = Matrix4x4.get_identity_matrix()
|
||||
#world_matrix = rotation_matrix_z.multiply_matrix(rotation_matrix_x)
|
||||
#world_matrix = world_matrix.multiply_matrix(translation)
|
||||
|
||||
|
||||
triangles = []
|
||||
|
||||
for tri in tris:
|
||||
# Rotate on the z-axis
|
||||
row1 = rotation_matrix_y.multiply_3d(tri.row1)
|
||||
row2 = rotation_matrix_y.multiply_3d(tri.row2)
|
||||
row3 = rotation_matrix_y.multiply_3d(tri.row3)
|
||||
row1 = rotation_matrix_z.multiply_3d(tri.row1)
|
||||
row2 = rotation_matrix_z.multiply_3d(tri.row2)
|
||||
row3 = rotation_matrix_z.multiply_3d(tri.row3)
|
||||
|
||||
rotated_y = Matrix3x3(row1, row2, row3)
|
||||
# Rotate on the x-axis
|
||||
|
||||
@@ -1,7 +1,30 @@
|
||||
import math
|
||||
from vectors import Vector3d, Vector4d
|
||||
|
||||
class Matrix4x4:
|
||||
class Matrix3x3:
|
||||
|
||||
def __init__(self, row1, row2, row3):
|
||||
self.rows = [row1, row2, row3]
|
||||
|
||||
@property
|
||||
def row1(self):
|
||||
return self.rows[0]
|
||||
|
||||
@property
|
||||
def row2(self):
|
||||
return self.rows[1]
|
||||
|
||||
@property
|
||||
def row3(self):
|
||||
return self.rows[2]
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self.rows[key]
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
self.rows[key] = value
|
||||
|
||||
class Matrix4x4(Matrix3x3):
|
||||
|
||||
def __init__(self, row1, row2, row3, row4):
|
||||
if not isinstance(row1, Vector4d):
|
||||
@@ -13,10 +36,11 @@ class Matrix4x4:
|
||||
if not isinstance(row4, Vector4d):
|
||||
raise ValueError("Row must be a Vector4d object.", row4)
|
||||
|
||||
self.row1 = row1
|
||||
self.row2 = row2
|
||||
self.row3 = row3
|
||||
self.row4 = row4
|
||||
self.rows = [row1, row2, row3, row4]
|
||||
|
||||
@property
|
||||
def row4(self):
|
||||
return self.rows[3]
|
||||
|
||||
def multiply_3d(self, vector3d):
|
||||
x = vector3d.x * self.row1.x + vector3d.y * self.row2.x + vector3d.z * self.row3.x + self.row4.x
|
||||
@@ -31,7 +55,14 @@ class Matrix4x4:
|
||||
|
||||
return Vector3d(x, y, z)
|
||||
|
||||
|
||||
def multiply_matrix(self, matrix4x4):
|
||||
out = Matrix4x4.get_identity_matrix()
|
||||
|
||||
for column in range(4):
|
||||
for row in range(4):
|
||||
out[row][column] = self.rows[row][0] * matrix4x4.rows[0][column] + self.rows[row][1] * matrix4x4.rows[1][column] + self.rows[row][2] * matrix4x4.rows[2][column] + self.rows[row][3] * matrix4x4.rows[3][column]
|
||||
return out
|
||||
|
||||
def get_projection_matrix(fov_degrees, aspect_ratio, near_plane, far_plane):
|
||||
fov_rad = 1 / math.tan(fov_degrees * 0.5 / 180.0 * math.pi)
|
||||
|
||||
@@ -73,10 +104,11 @@ class Matrix4x4:
|
||||
row4 = Vector4d(0, 0, 0, 1)
|
||||
|
||||
return Matrix4x4(row1, row2, row3, row4)
|
||||
|
||||
class Matrix3x3:
|
||||
|
||||
def __init__(self, row1, row2, row3):
|
||||
self.row1 = row1
|
||||
self.row2 = row2
|
||||
self.row3 = row3
|
||||
def get_translation_matrix(vector3d):
|
||||
row1 = Vector4d(1, 0, 0, 0)
|
||||
row2 = Vector4d(0, 1, 0, 0)
|
||||
row3 = Vector4d(0, 0, 1, 0)
|
||||
row4 = Vector4d(vector3d.x, vector3d.y, vector3d.z, 1)
|
||||
|
||||
return Matrix4x4(row1, row2, row3, row4)
|
||||
|
||||
+46
-14
@@ -1,15 +1,32 @@
|
||||
class Vector4d:
|
||||
def __init__(self, x, y, z, w):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.z = z
|
||||
self.w = w
|
||||
class Vector2d:
|
||||
def __init__(self, x, y):
|
||||
self.points = [x, y]
|
||||
|
||||
class Vector3d:
|
||||
@property
|
||||
def x(self):
|
||||
return self.points[0]
|
||||
|
||||
@x.setter
|
||||
def x(self, value):
|
||||
self.points[0] = value
|
||||
|
||||
@property
|
||||
def y(self):
|
||||
return self.points[1]
|
||||
|
||||
@y.setter
|
||||
def y(self, value):
|
||||
self.points[1] = value
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self.points[key]
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
self.points[key] = value
|
||||
|
||||
class Vector3d(Vector2d):
|
||||
def __init__(self, x, y, z):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.z = z
|
||||
self.points = [x, y, z]
|
||||
|
||||
def cross_product(self, vector3d):
|
||||
# Returns a line that is perpendicular to the parameter
|
||||
@@ -25,8 +42,23 @@ class Vector3d:
|
||||
# Returns a scalar that defines how similar two
|
||||
# vectors are to one another.
|
||||
return self.x * vector3d.x + self.y * vector3d.y + self.z * vector3d.z
|
||||
|
||||
@property
|
||||
def z(self):
|
||||
return self.points[2]
|
||||
|
||||
class Vector2d:
|
||||
def __init__(self, x, y):
|
||||
self.x = x
|
||||
self.y = y
|
||||
@z.setter
|
||||
def z(self, value):
|
||||
self.points[2] = value
|
||||
|
||||
class Vector4d(Vector3d):
|
||||
def __init__(self, x, y, z, w):
|
||||
self.points = [x, y, z, w]
|
||||
|
||||
@property
|
||||
def w(self):
|
||||
return self.points[3]
|
||||
|
||||
@w.setter
|
||||
def w(self, value):
|
||||
self.points[3] = value
|
||||
|
||||
Reference in New Issue
Block a user