repl.it
@MinecraftMrGabe/

3d graphics engine

Pygame

No description

fork
loading
Files
  • main.py
  • nohup.out
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
193
194
195
196
197
198
199
200
201
import pygame
from pygame.locals import *
import math

screen = pygame.display.set_mode((600,400))
clock = pygame.time.Clock()

red = (255,0,0)
blue= (0,0,255)
green = (0,255,0)
yellow = (255,255,0)
orange = (255,255,125)

camx = 0
camy = 0
camz = 0
camrx = 0
camry = 0
triangles = pygame.sprite.Group()

def cam_overlap(tri):
  if tri.visible:
    (x,y,z) = tri.average_pos
    x-=camx
    y-=camy
    z-=camz
    tyr = tri.yr
    txr = tri.xr
    for s in triangles.sprites():
      if s!=tri:
        if max(abs(s.xr-txr),abs(s.yr,tyr))<=tri.max_diviation+s.max_diviation:
          pass
def limit(x,minn,maxx):
  if x<=minn:
    return minn
  elif x>=maxx:
    return maxx
  else:
    return x
class point():
  def __init__(self,x,y,z):
    self.coords = (x,-y,z)
    self.visualcoords = (x,y)
    self.visible=True
    self.tick=0
  def update_visual(self):
    x = -1
    y = -1
    if True:
      (x,y,z) = self.coords
      x-=camx
      y-=camy
      z-=camz
      xr = math.atan2(x,z)+camrx
      hl = math.sqrt(x*x+z*z)
      x = math.sin(xr)*hl
      z = math.cos(xr)*hl
      yr = math.atan2(y,z)+camry
      vl = math.sqrt(y*y+z*z)
      y = math.sin(yr)*vl
      z = math.cos(yr)*vl
      x = x/z*300+300
      y = y/z*300+200
      self.visualcoords = (x,y)
      self.visible=abs(x-300)<=300 and abs(y-200)<=200 and z>0
      return (x,y)
    else:
      return (-1,-1)

class triangle(pygame.sprite.DirtySprite):
  def __init__(self,points,color = red):
    pygame.sprite.DirtySprite.__init__(self)
    self.points = points
    self.color = color
    self.image = pygame.Surface((800,400))
    #self.image = self.image.convert_alpha()
    self.image.set_colorkey((0,0,0))
    self.rect = self.image.get_rect()
    self.add(triangles)
    self.average_pos = ()
    self.max_deviation = 0
    self.xr = 0
    self.yr = 0
  def update(self,current_tick):
    points = []
    x = (self.points[0].coords[0]+self.points[1].coords[0]+self.points[2].coords[0])/3
    y = (self.points[0].coords[1]+self.points[1].coords[1]+self.points[2].coords[1])/3
    z = (self.points[0].coords[2]+self.points[1].coords[2]+self.points[2].coords[2])/3
    self.average_pos = (x,y,z)
    visible = False
    for p in self.points:
      if p.tick==current_tick:
        points.append(p.update_visual())
        p.tick+=1
      elif p.tick-2>=current_tick:
        p.tick=current_tick+1
        points.append(p.update_visual())
      else:
        points.append(p.visualcoords)
      visible = visible or p.visible
    self.image.fill((0,0,0))
    #find max diviation
    maxx = 0
    maxy = 0
    for p1 in self.points:
      for p2 in self.points:
        d = abs(p1.visualcoords[0]-p2.visualcoords[0])
        if d>maxx:
          maxx=d
        d = abs(p1.visualcoords[1]-p2.visualcoords[1])
        if d>maxy:
          maxy=d
    fmax = max(maxx/600,maxy/400) #final max
    #turn into radians
    self.max_deviation = fmax*math.pi/2
    self.xr = math.atan2(self.average_pos[2]-camz,self.average_pos[0]-camx)
    self.yr = math.atan2(self.average_pos[2]-camz,self.average_pos[1]-camy)
    x-=camx
    y-=camy
    z-=camz
    xd = math.sqrt(x*x+z*z)
    self.dist = math.sqrt(xd*xd+y*y)
    if visible:
      pygame.draw.polygon(self.image,self.color,points)

test = triangle((point(0,1,2),point(-1,1,3),point(-1,0,2)))
test2 = triangle((test.points[0],test.points[2],point(0,0,1)),blue)
test3 = triangle((test.points[0],test2.points[2],point(1,0,2)),green)
test4 = triangle((test.points[0],test3.points[2],point(1,1,3)),orange)
test5 = triangle((test.points[0],test4.points[2],test.points[1]),yellow)
tick = 0


mousepos = (0,0)
pygame.mouse.set_visible(False)
def mouse_update():
  global camrx,camry
  mousepos = pygame.mouse.get_pos()
  #camrx-=(mousepos[0]-300)/600*math.pi
  #pygame.mouse.set_pos(300,200)
  (rmx,rmy) = pygame.mouse.get_rel()
  camrx-=rmx/150*math.pi
  camry-=rmy/150*math.pi
  if camry>math.pi/2:
    camry=math.pi/2
  elif camry<-math.pi/2:
    camry=-math.pi/2


def main():
  global camz, camx, camy, camrx, tick
  up = False
  down = False
  left = False
  right = False
  forward = False
  back = False
  while(True):
    mouse_update()
    camrx+=0.0
    screen.fill((255,255,255))
    triangles.update(tick)
    triangles.draw(screen)
    pygame.display.update()
    clock.tick(30)
    for event in pygame.event.get():
      if event.type in [KEYDOWN,KEYUP]:
        u = event.type==KEYDOWN
        if event.key==K_w:
          forward = u
        elif event.key==K_s:
          back = u
        elif event.key==K_a:
          left = u
        elif event.key==K_d:
          right = u
        elif event.key==K_LSHIFT:
          down = u
        elif event.key==K_SPACE:
          up = u
    if up:
      camy-=0.1
    if down:
      camy+=0.1
    if forward:
      camx-=0.1*math.sin(camrx)
      camz+=0.1*math.cos(camrx)
    if back:
      camx+=0.1*math.sin(camrx)
      camz-=0.1*math.cos(camrx)
    if left:
      camz-=0.1*math.sin(camrx)
      camx-=0.1*math.cos(camrx)
    if right:
      camz+=0.1*math.sin(camrx)
      camx+=0.1*math.cos(camrx)
    if tick>=30:
      tick = 0
    tick+=1

main()