forked from pi3d/pi3d_demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Silo.py
209 lines (171 loc) · 6.68 KB
/
Silo.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
202
203
204
205
206
207
208
209
#!/usr/bin/python
from __future__ import absolute_import, division, print_function, unicode_literals
""" Show how to use the Building class. This has the facility to define solid
objects that cannot pass through other solid objects. The walls of Building
are constructed of solid object. There are two examples of solid corridor
created near to the silo.
This demonstration also shows in a fairly rough way how lighting can be changed
as the view point moves (from inside to outside in this instance).
"""
import math
import demo
import pi3d
# Setup display and initialise pi3d
DISPLAY = pi3d.Display.create(x=150, y=150,
background=(0.4, 0.8, 0.8, 1), frames_per_second=30)
shader = pi3d.Shader("uv_reflect")
flatsh = pi3d.Shader("uv_flat")
#############################
# Load textures
blockimg = pi3d.Texture("textures/squareblocks4.png")
roofedgeimg = pi3d.Texture("textures/roofedging.png")
roofimg = pi3d.Texture("textures/Roof.png")
greenimg = pi3d.Texture("textures/squareblocks4green.png")
ectex = pi3d.loadECfiles("textures/ecubes", "sbox", "jpg")
myecube = pi3d.EnvironmentCube(size=900.0, maptype="FACES")
myecube.set_draw_details(flatsh, ectex)
# Create elevation map
mapwidth=1000.0
mapdepth=1000.0
mapheight=60.0
floorimg = pi3d.Texture("textures/dunes3_512.jpg")
bumpimg = pi3d.Texture("textures/mudnormal.jpg")
mymap = pi3d.ElevationMap(mapfile="textures/mountainsHgt2.png",
width=mapwidth, depth=mapdepth, height=mapheight,
divx=64, divy=64)
mymap.set_draw_details(shader,[floorimg, bumpimg],128.0, 0.0)
mymap.set_fog((0.3,0.25,0.1,0.8), 500.0)
#Create some random block models elsewhere on the map!
pi3d.corridor(
150, 10, mymap,
details=[shader, [blockimg, blockimg], 1.0, 0.0, 4.0, 4.0], walls="ns")
pi3d.corridor(
120, -40, mymap,
details=[shader, [blockimg, blockimg], 1.0, 0.0, 4.0, 4.0], walls="ew")
# openSectionScheme: black is wall, white is corridor or room, grey has no ceiling, there is one model
# Model 0: wall
# Model 1: wall in the open
# Model 2: roof
# Model 3: roof edge
openSectionSchemeMultimodel = {
"#models": 4,
(0,None): [["R",2]], # black cell has a roof
(2,None): [["C", 0], ["R", 2]], # white cell has a ceiling and roof
(0,0,"edge"): [["W",0], ["CE", 3]], # black cell on the edge next to a black cell has a wall and ceiling edge
(1,0,"edge"): [["W",0], ["CE", 3]], # grey cell on the edge next to a black cell has a wall and ceiling edge
(2,0,"edge"): [["W",0], ["CE", 3]], # white cell on the edge next to a black cell has a wall and ceiling edge
(2,2,"edge"): [["CE", 3]], # white cell on the edge next to a white cell a ceiling edge
(2,0): [["W", 0]], # white cell next to a black cell has a wall
(1,0): [["W", 1], ["CE", 3]], # grey cell next to a black cell has a wall and ceiling edge
(1,2): [["CE", 3]] } # grey cell next to a white cell has a ceiling edge
# details is an array of the same size as models array (defined above as part of the dict {"#models":4,..} )
# contains information corresponding with Buffer.set_draw_details()
details = [
[shader, [blockimg, blockimg], 1.0, 0.0, 4.0, 12.0],
[shader, [greenimg, greenimg], 1.0, 0.0, 4.0, 4.0],
[shader, [roofimg, blockimg], 1.0, 0.0, 4.0, 4.0],
[shader, [roofedgeimg], 0.0, 0.0, 4.0, 4.0],
]
building = pi3d.Building(
"textures/silo_map.png", 0, 0, mymap,
width=15.0, depth=15.0, height=70.0, name="", draw_details=details,
yoff=-13, scheme=openSectionSchemeMultimodel)
outLight = pi3d.Light(lightpos=(10, -10, 20), lightcol =(1.0, 1.0, 0.7), lightamb=(0.35, 0.35, 0.4))
inLight = pi3d.Light(lightpos=(10, -10, 20), lightcol =(0.1, 0.1, 0.15), lightamb=(0.05, 0.15, 0.1))
for b in building.model:
b.set_light(inLight, 0)
mymap.set_light(inLight, 0)
inFlag = True
#screenshot number
scshots = 1
#avatar camera
rot=0.0
tilt=0.0
avhgt = 5.0
aveyelevel = 4.0
aveyeleveladjust = aveyelevel - avhgt/2
man = pi3d.SolidObject(
"man", pi3d.Size(1, avhgt, 1),
pi3d.Position(0, (mymap.calcHeight(5, 5) + avhgt/2), 0), 1)
inputs = pi3d.InputEvents()
inputs.get_mouse_movement()
mouseOn = True
frame = 400
record = False
CAMERA = pi3d.Camera.instance()
while DISPLAY.loop_running() and not inputs.key_state("KEY_ESC"):
CAMERA.reset()
CAMERA.rotate(tilt, rot, 0)
CAMERA.position((man.x(), man.y(), man.z() - aveyeleveladjust))
myecube.position(man.x(), man.y(), man.z() - aveyeleveladjust)
pi3d.SolidObject.drawall()
building.drawAll()
mymap.draw()
myecube.draw()
inputs.do_input_events()
"""On some combined keyboard/mouse arrangements you might need to change
this to inputs.get_mouse_movement(1) in order to get the correct input
"""
mx, my, mv, mh, md = inputs.get_mouse_movement()
rot -= (mx)*0.2
tilt -= (my)*0.2
#jrx, jry, jrz = inputs.get_joystickB3d() # xbox 360
# sudo apt-get install xboxdrv
# sudo xboxdrv -s -i 0
jrx, jry = inputs.get_joystickR() # gamepad
if abs(jrx) > 0.1:
rot -= jrx*3
if abs(jry) > 0.1:
tilt -= jry*3
xm = man.x()
ym = man.y()
zm = man.z()
jx, jy = inputs.get_joystick()
if abs(jy) > 0.2:
xm += math.sin(math.radians(rot))*jy
zm -= math.cos(math.radians(rot))*jy
if abs(jx) > 0.2:
xm -= math.sin(math.radians(rot-90))*jx
zm += math.cos(math.radians(rot-90))*jx
if inputs.key_state("BTN_LEFT"):
xm -= math.sin(math.radians(rot))
zm += math.cos(math.radians(rot))
if inputs.key_state("KEY_W"): #key W
xm -= math.sin(math.radians(rot))
zm += math.cos(math.radians(rot))
if inputs.key_state("KEY_S"): #key S
xm += math.sin(math.radians(rot))
zm -= math.cos(math.radians(rot))
ym = (mymap.calcHeight(xm,zm)+avhgt)
NewPos = pi3d.Position(xm, ym, zm)
collisions = man.CollisionList(NewPos)
if not collisions:
man.move(NewPos)
if inFlag and abs(man.z() - building.zpos) > 55:
inFlag = False
for b in building.model:
b.set_light(outLight, 0)
mymap.set_light(outLight, 0)
if inputs.key_state("KEY_APOSTROPHE"): #key '
tilt -= 2.0
if inputs.key_state("KEY_SLASH"): #key /
tilt += 2.0
if inputs.key_state("KEY_A"): #key A
rot += 2
if inputs.key_state("KEY_D"): #key D
rot -= 2
if inputs.key_state("KEY_P"): #key P
#record = not(record)
while inputs.key_state("KEY_P"):
inputs.do_input_events() # wait for key to go up
pi3d.screenshot("silo"+str(scshots)+".jpg")
scshots += 1
if inputs.key_state("KEY_ENTER"): #key RETURN
mc = 0
if inputs.key_state("KEY_X"): #key RETURN
while inputs.key_state("KEY_X"):
inputs.do_input_events() # wait for key to go up
mouseOn != mouseOn
inputs.grab_by_type("keyboard", grab=mouseOn)
inputs.release()
DISPLAY.destroy()