-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
141 lines (99 loc) · 3.61 KB
/
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
# ----------------------------------------
# file: main.py
# author: coppermouse
# ----------------------------------------
import os
import pygame
import math
from numba.typed import List
from textures import Textures
from project import project
from scene import setup_scene
from scene import scene_on_key
from scene import scene_while_key
from scene import on_scene_update
from horizon import draw_horizon
from globals import g
FPS = 60
screen_size = ( 700*2, 400*2 )
half_screen_size = [ c//2 for c in screen_size ]
def get_main_path():
return os.path.dirname( os.path.realpath(__file__) )
def get_sun_vector():
from scene import sun_vector
return sun_vector
def get_fog():
from scene import fog
return fog
def get_fog_thresholds_strength():
from scene import fog_thresholds_strength
return fog_thresholds_strength
def get_fog_thresholds():
from scene import fog_thresholds
return fog_thresholds
def get_sun_thresholds():
from scene import sun_thresholds
return sun_thresholds
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode( screen_size )
clock = pygame.time.Clock()
textures = Textures()
objects = setup_scene()
fnd = List( objects.fnd )
keys_down = set()
_quit = False
while not _quit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
_quit = True
elif event.type == pygame.KEYDOWN:
scene_on_key( event.key )
keys_down.add( event.key )
if event.key == pygame.K_o:
fov[0] += 1
if event.key == pygame.K_p:
fov[0] -= 1
elif event.type == pygame.KEYUP:
keys_down.discard( event.key )
for key in keys_down:
scene_while_key( key )
g['fov'][1] = g['fov'][0] * ( screen_size[1] / screen_size[0] ) # <--- not sure about this one
# but it looks kind of right
on_scene_update()
screen.blit( draw_horizon(), (0,0) )
# draw faces on screen
polygons = project(
objects.faces,
tuple( g['fov'] ),
tuple( half_screen_size ),
tuple( get_fog_thresholds() ),
tuple( get_sun_thresholds() ),
tuple( get_sun_vector() ),
fnd,
)
for polygon, _type, key in polygons:
# this is a very hard coded solution. it works for this first demo...
if _type == 0:
warped = textures.get('box',key)[0].warp( polygon )
screen.blit( *warped )
else:
color = textures.get( 'teapot', key )[0]
pygame.draw.polygon( screen, color, polygon )
# draw overlay map (debug)
if 0:
c = 700
k = math.tan( math.radians( g['fov'][0])/2 )
pygame.draw.polygon( screen, 'white', [ (0,0), (c,c), (c*2*k,0) ], 1 )
for face in objects.faces:
for vertex in face:
v = vertex
screen.set_at( [ int( v + c ) for v in [ v[0], v[1] ] ], 'white' )
sun_vector_center = svc = pygame.math.Vector2(c,c//2)
sub_vector_length = svl = -100 # actual its half length
sv = get_sun_vector()
pygame.draw.line(
screen, 'yellow', *[ svc + ( sv[0]*-svl*f, sv[1]*-svl*f ) for f in [-1,1] ] )
print(clock.get_fps())
clock.tick( FPS )
pygame.display.update()