Implemented the first version of the 3D engine. Added support for loading 'obj' files as a mesh.
This commit is contained in:
@@ -3,44 +3,23 @@ import math
|
||||
import copy
|
||||
from matrix import Matrix4x4, Matrix3x3
|
||||
from vectors import Vector4d, Vector3d, Vector2d
|
||||
from engine import Mesh
|
||||
|
||||
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 ]]
|
||||
]
|
||||
tris = Mesh()
|
||||
|
||||
def main():
|
||||
tris.load_obj_file('shuttle.obj')
|
||||
|
||||
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.bind('<B1-Motion>', mouse_end)
|
||||
ui.canvas.old_coords = None
|
||||
ui.window.after(0, draw_scene)
|
||||
ui.show()
|
||||
@@ -97,30 +76,25 @@ def draw_scene():
|
||||
|
||||
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)
|
||||
|
||||
for tri in tris.triangles:
|
||||
# Rotate on the z-axis
|
||||
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)
|
||||
|
||||
# Rotate on the x-axis
|
||||
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)
|
||||
|
||||
# The offset into the screen
|
||||
translated = copy.copy(rotated_x)
|
||||
translated.row0.z += 3.0
|
||||
translated.row1.z += 3.0
|
||||
translated.row2.z += 3.0
|
||||
|
||||
translated.row0.z += 8.0
|
||||
translated.row1.z += 8.0
|
||||
translated.row2.z += 8.0
|
||||
# Scale into view
|
||||
row0 = projection_matrix.multiply_3d(translated.row0)
|
||||
row1 = projection_matrix.multiply_3d(translated.row1)
|
||||
row2 = projection_matrix.multiply_3d(translated.row2)
|
||||
|
||||
Reference in New Issue
Block a user