@CoolqB/

3D Engine

Python (with Turtle)

View 3D wireframes. Strange freeze issue. I think I'm running turtle too fast.

fork
loading
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""
  --- 3D Visualizer ---
  - Created by Rohan  -
  -  Created in Repl  -
  ---     Note      ---
  - I had to reduce
    the framerate due
    to freezing.      -
"""

import turtle, math as maths, time

# Error classes,
class ObjectNotFoundError(Exception):
  
  pass

class RendererNotFoundError(Exception):
  
  pass

  

# Helper classes,
class Vector2:

  def __init__(self, x=0, y=0, z=0):
    self.x, self.y = x, y

class Vector3:

  def __init__(self, x=0, y=0, z=0):
    self.x, self.y, self.z = x, y, z

class Object:

  def __init__(self, position=Vector3(), rotation=Vector2(), _id=None):
    self.position, self.rotation, self._id = position, rotation, _id

  VERTICIES = []
  TRIANGLES = []

class Cube(Object):

  VERTICIES = [
    # X,  Y,  Z.
    (-1, -1, -1),
    ( 1, -1, -1),
    ( 1,  1, -1),
    (-1,  1, -1),
    (-1, -1,  1),
    ( 1, -1,  1),
    ( 1,  1,  1),
    (-1,  1,  1)
  ]

  TRIANGLES = [
    #P1,P2,P3
    (0, 1, 2), (2, 3, 0),
    (0, 4, 5), (5, 1, 0),
    (0, 4, 3), (4, 7, 3),
    (5, 4, 7), (7, 6, 5),
    (7, 6, 3), (6, 2, 3),
    (5, 1, 2), (2, 6, 5)
  ]

class Camera(Object):

  def __init__(self, position=Vector3(), rotation=Vector2(), fov=400, _id=None):
    self.position, self.rotation, self._id = position, rotation, _id
    self.fov = fov

class Scene:

  def __init__(self):
    self.camera = None
    self.objects = {}
  
  def set_camera(self, cam):
    self.camera = cam

  def add_object(self, obj):
    if obj._id == None:
      obj._id = len(self.objects)
    self.objects[obj._id] = obj
  
  def del_object(self, _id):
    if _id in self.objects:
      del self.objects[_id]
    else:
      raise ObjectNotFoundError('Could not find object with specified `id` in scene!')

  def find(self, _id):
    if _id in self.objects:
      return self.objects[_id]
    else:
      raise ObjectNotFoundError('Could not find object with specified `id` in scene!')

  def get_all(self):
    return self.objects.values()

class Project:

  def project(self, x, y, z, obj, camera):
    x, y, z = self.rotate3d(x, y, z, obj.rotation.x, obj.rotation.y)
    x, y, z = x - camera.position.x, y - camera.position.y, z - camera.position.z
    f = camera.fov / z
    sx, sy = x * f, -y * f
    return sx, sy

  def rotate2d(self, x, y, rot):
    s, c = maths.sin(rot), maths.cos(rot)
    return x * c - y * s, \
           x * s + y * c
  
  def rotate3d(self, x, y, z, rx, ry):
    x, z = self.rotate2d(x, z, rx)
    y, z = self.rotate2d(y, z, ry)
    return x, y, z

class Engine:

  def __init__(self, init_callback=None, update_callback=None, draw_callback=None):
    self.pointer = turtle.Turtle()
    self.pointer.tracer(0, 20)
    self.pointer.speed(0)
    self.pointer.ht()
    self.init_callback, self.update_callback, self.draw_callback = \
      init_callback, update_callback, draw_callback
    
    self.project = Project()
    self.scene = Scene()

  def line(self, p1, p2):
    self.pointer.penup()
    self.pointer.goto(p1)
    self.pointer.pendown()
    self.pointer.goto(p2)

  def draw(self):
    self.pointer.clear()
    if self.scene.camera != None:
      for obj in self.scene.get_all():
        for tri in obj.TRIANGLES:
          POINTS = []
          for vert in (obj.VERTICIES[tri[0]],
                      obj.VERTICIES[tri[1]],
                      obj.VERTICIES[tri[2]]):
            x, y, z = vert
            POINTS.append(self.project.project(x, y, z, obj, self.scene.camera))
          self.line(POINTS[0], POINTS[1])
          self.line(POINTS[1], POINTS[2])
          self.line(POINTS[2], POINTS[0])
      self.pointer.update()
    else:
      raise RendererNotFoundError('No camera set!')
  
  # Run callbacks,
  def cycle(self):
    if self.update_callback != None:
      self.update_callback()

    if self.draw_callback != None:
      self.draw_callback  ()

  def mainloop(self):
    if self.init_callback != None:
      self.init_callback  ()
    while True:
      self.cycle()
      time.sleep(1.0 / 60.0)

if __name__ == '__main__':
  def demo_init():
    demo.scene.set_camera(
      Camera(Vector3(0, 0, -5))
    )
    demo.scene.add_object(
      Cube()
    )
  
  def demo_update():
    demo.scene.objects[0].rotation.x += 0.025
    demo.scene.objects[0].rotation.y += 0.025
  
  def demo_draw():
    demo.draw()
  
  demo = Engine(demo_init, demo_update, demo_draw)
  demo.mainloop()
result
console