151 lines
4.4 KiB
Python
151 lines
4.4 KiB
Python
import window
|
|
import math
|
|
import copy
|
|
from matrix import Matrix4x4, Matrix3x3
|
|
from vectors import Vector4d, Vector3d, Vector2d
|
|
|
|
ui = window.Window(600, 600)
|
|
near_plane = 0.1
|
|
far_plane = 1000.0
|
|
fov = 90.0
|
|
|
|
tris = [
|
|
# SOUTH
|
|
[ [0.0, 0.0, 0.0], [0.0, 1.0, 0.0], [1.0, 1.0, 0.0] ],
|
|
[ [0.0, 0.0, 0.0], [1.0, 1.0, 0.0], [1.0, 0.0, 0.0 ]],
|
|
|
|
# EAST
|
|
[ [1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [1.0, 1.0, 1.0 ]],
|
|
[ [1.0, 0.0, 0.0], [1.0, 1.0, 1.0], [1.0, 0.0, 1.0 ]],
|
|
|
|
# NORTH
|
|
[ [1.0, 0.0, 1.0], [1.0, 1.0, 1.0], [0.0, 1.0, 1.0 ]],
|
|
[ [1.0, 0.0, 1.0], [0.0, 1.0, 1.0], [0.0, 0.0, 1.0 ]],
|
|
|
|
# WEST
|
|
[[ 0.0, 0.0, 1.0], [0.0, 1.0, 1.0], [0.0, 1.0, 0.0 ]],
|
|
[ [0.0, 0.0, 1.0], [0.0, 1.0, 0.0], [0.0, 0.0, 0.0 ]],
|
|
|
|
# TOP
|
|
[[ 0.0, 1.0, 0.0], [0.0, 1.0, 1.0], [1.0, 1.0, 1.0 ]],
|
|
[[ 0.0, 1.0, 0.0], [1.0, 1.0, 1.0], [1.0, 1.0, 0.0 ]],
|
|
|
|
# BOTTOM
|
|
[[ 1.0, 0.0, 1.0], [0.0, 0.0, 1.0], [0.0, 0.0, 0.0 ]],
|
|
[[ 1.0, 0.0, 1.0], [0.0, 0.0, 0.0], [1.0, 0.0, 0.0 ]]
|
|
]
|
|
|
|
def main():
|
|
ui.thetax = 1
|
|
ui.window.bind('<Left>', left)
|
|
ui.window.bind('<Right>', right)
|
|
ui.window.bind('<ButtonPress-1>', mouse_start)
|
|
ui.window.bind('<B1-Motion>', mouse_end)
|
|
ui.canvas.old_coords = None
|
|
ui.window.after(0, draw_scene)
|
|
ui.show()
|
|
|
|
def left(event):
|
|
ui.theta -= .01
|
|
|
|
def right(event):
|
|
ui.theta += .01
|
|
|
|
def mouse_start(event):
|
|
#x, y =
|
|
ui.canvas.old_coords = event.x, event.y #x, y
|
|
|
|
def mouse_end(event):
|
|
x, y = ui.canvas.old_coords[0] - event.x, ui.canvas.old_coords[1] - event.y
|
|
if x < 0:
|
|
ui.theta += .01
|
|
else:
|
|
ui.theta -= .01
|
|
|
|
print(ui.theta)
|
|
|
|
if y < 0:
|
|
ui.thetax += .01
|
|
else:
|
|
ui.thetax -= .01
|
|
#ui.theta += x / ui.width
|
|
|
|
def draw_scene():
|
|
ui.canvas.delete("all")
|
|
#ui.theta += .01 # scaling factor
|
|
aspect_ratio = ui.height / ui.width
|
|
fov_rad = ui.theta / math.tan(fov * 0.5 / 180.0 * math.pi)
|
|
|
|
row0 = Vector4d(aspect_ratio * fov_rad, 0, 0, 0)
|
|
row1 = Vector4d(0, fov_rad, 0, 0)
|
|
row2 = Vector4d(0, 0, far_plane / (far_plane - near_plane), 1.0)
|
|
row3 = Vector4d(0, 0, -far_plane * near_plane / (far_plane - near_plane), 0)
|
|
|
|
projection_matrix = Matrix4x4(row0, row1, row2, row3)
|
|
|
|
row0 = Vector4d(math.cos(ui.theta), math.sin(ui.theta), 0, 0)
|
|
row1 = Vector4d(-math.sin(ui.theta), math.cos(ui.theta), 0, 0)
|
|
row2 = Vector4d(0, 0, 1, 0)
|
|
row3 = Vector4d(0, 0, 0, 1)
|
|
|
|
rotation_matrix_z = Matrix4x4(row0, row1, row2, row3)
|
|
|
|
row0 = Vector4d(1, 0, 0, 0)
|
|
row1 = Vector4d(0, math.cos(ui.thetax * 0.5), math.sin(ui.thetax * 0.5), 0)
|
|
row2 = Vector4d(0, -math.sin(ui.thetax * 0.5), math.cos(ui.thetax * 0.5), 0)
|
|
row3 = Vector4d(0, 0, 0, 1)
|
|
|
|
rotation_matrix_x = Matrix4x4(row0, row1, row2, row3)
|
|
|
|
for t in tris:
|
|
row0 = Vector3d(t[0][0], t[0][1], t[0][2])
|
|
row1 = Vector3d(t[1][0], t[1][1], t[1][2])
|
|
row2 = Vector3d(t[2][0], t[2][1], t[2][2])
|
|
|
|
tri = Matrix3x3(row0, row1, row2)
|
|
|
|
row0 = rotation_matrix_z.multiply_3d(tri.row0)
|
|
row1 = rotation_matrix_z.multiply_3d(tri.row1)
|
|
row2 = rotation_matrix_z.multiply_3d(tri.row2)
|
|
|
|
rotated_z = Matrix3x3(row0, row1, row2)
|
|
|
|
row0 = rotation_matrix_x.multiply_3d(rotated_z.row0)
|
|
row1 = rotation_matrix_x.multiply_3d(rotated_z.row1)
|
|
row2 = rotation_matrix_x.multiply_3d(rotated_z.row2)
|
|
|
|
rotated_x = Matrix3x3(row0, row1, row2)
|
|
|
|
translated = copy.copy(rotated_x)
|
|
translated.row0.z += 3.0
|
|
translated.row1.z += 3.0
|
|
translated.row2.z += 3.0
|
|
|
|
row0 = projection_matrix.multiply_3d(translated.row0)
|
|
row1 = projection_matrix.multiply_3d(translated.row1)
|
|
row2 = projection_matrix.multiply_3d(translated.row2)
|
|
|
|
projected = Matrix3x3(row0, row1, row2)
|
|
|
|
projected.row0.x += 1
|
|
projected.row0.y += 1
|
|
projected.row1.x += 1
|
|
projected.row1.y += 1
|
|
projected.row2.x += 1
|
|
projected.row2.y += 1
|
|
projected.row0.x *= .5 * ui.width
|
|
projected.row0.y *= .5 * ui.height
|
|
projected.row1.x *= .5 * ui.width
|
|
projected.row1.y *= .5 * ui.height
|
|
projected.row2.x *= .5 * ui.width
|
|
projected.row2.y *= .5 * ui.height
|
|
|
|
coords = [projected.row0.x, projected.row0.y, projected.row1.x, projected.row1.y, projected.row2.x, projected.row2.y]
|
|
ui.canvas.create_polygon(coords, fill="", outline="black")
|
|
|
|
ui.canvas.update()
|
|
|
|
ui.canvas.after(100, draw_scene)
|
|
|
|
main()
|