forked from tipam/pi3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForestWalk.py
159 lines (132 loc) · 4.25 KB
/
ForestWalk.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
# Forest walk example using pi3d module
# =====================================
# Copyright (c) 2012 - Tim Skillman
# Version 0.02 - 03Jul12
#
# grass added
# This example does not reflect the finished pi3d module in any way whatsoever!
# It merely aims to demonstrate a working concept in simplfying 3D programming on the Pi
#
# PLEASE INSTALL PIL imaging with:
#
# $ sudo apt-get install python-imaging
#
# before running this example
#
import pi3d,math,random
rads = 0.017453292512 # degrees to radians
# Setup display and initialise pi3d
display = pi3d.display()
display.create3D(100,100,1600,800, 0.5, 800.0, 60.0) # x,y,width,height,near,far,aspect
display.setBackColour(0.4,0.8,0.8,1) # r,g,b,alpha
# Load textures
tree2img = pi3d.loadTextureAlpha("textures/tree2.png")
tree1img = pi3d.loadTextureAlpha("textures/tree1.png")
grassimg = pi3d.loadTextureAlpha("textures/grass.png")
hb2img = pi3d.loadTextureAlpha("textures/hornbeam2.png")
ectex = pi3d.loadTexture("textures/SkyBox.png")
myecube = pi3d.createEnvironmentCube(900.0,"CROSS")
# Create elevation map
mapwidth=1000.0
mapdepth=1000.0
mapheight=60.0
mountimg1 = pi3d.loadTexture("textures/mountains3_512.jpg")
mymap = pi3d.createElevationMapFromTexture("textures/mountainsHgt.jpg",mapwidth,mapdepth,mapheight,64,64) #testislands.jpg
#Create tree models
treeplane = pi3d.createPlane(4.0,5.0)
treemodel1 = pi3d.createMergeShape("baretree")
treemodel1.add(treeplane, 0,0,0)
treemodel1.add(treeplane, 0,0,0, 0,90,0)
treemodel2 = pi3d.createMergeShape("bushytree")
treemodel2.add(treeplane, 0,0,0)
treemodel2.add(treeplane, 0,0,0, 0,60,0)
treemodel2.add(treeplane, 0,0,0, 0,120,0)
#Create grass model
grassplane = pi3d.createPlane(1.0,0.3,"",0,-2,0)
grassmodel = pi3d.createMergeShape("grass")
grassmodel.add(grassplane, 0,0,0)
grassmodel.add(grassplane, 0,0,0, 0,60,0)
grassmodel.add(grassplane, 0,0,0, 0,120,0)
#Scatter them on map using Merge shape's cluster function
mytrees1 = pi3d.createMergeShape("trees1")
mytrees1.cluster(treemodel1, mymap,0.0,0.0,200.0,200.0,30,"",8.0,3.0)
# (shape,elevmap,xpos,zpos,w,d,count,options,minscl,maxscl)
mytrees2 = pi3d.createMergeShape("trees2")
mytrees2.cluster(treemodel2, mymap,0.0,0.0,200.0,200.0,30,"",6.0,3.0)
# (shape,elevmap,xpos,zpos,w,d,count,options,minscl,maxscl)
mytrees3 = pi3d.createMergeShape("trees3")
mytrees3.cluster(treemodel2, mymap,0.0,0.0,300.0,300.0,30,"",4.0,2.0)
# (shape,elevmap,xpos,zpos,w,d,count,options,minscl,maxscl)
mygrass = pi3d.createMergeShape("grass")
mygrass.cluster(grassmodel, mymap,0.0,0.0,100.0,100.0,100,"",10.0,4.0)
mygrass2 = pi3d.createMergeShape("grass2")
mygrass2.cluster(mygrass, mymap,100.0,0.0,100.0,100.0,1,"",1.0,1.0)
# (shape,elevmap,xpos,zpos,w,d,count,options,minscl,maxscl)
#screenshot number
scshots = 1
#avatar camera
rot=0.0
tilt=0.0
avhgt = 2.0
xm=0.0
zm=0.0
ym= -(mymap.calcHeight(xm,zm)+avhgt)
# Fetch key presses
mykeys = pi3d.key()
mymouse = pi3d.mouse()
mymouse.start()
omx=mymouse.x
omy=mymouse.y
# Display scene and rotate cuboid
while 1:
display.clear()
pi3d.identity()
pi3d.rotate(tilt,0,0)
pi3d.rotate(0,rot,0)
pi3d.position(xm,ym,zm)
myecube.draw(ectex,xm,ym,zm)
mymap.draw(mountimg1)
mygrass.drawAll(grassimg)
mytrees1.drawAll(tree2img)
mytrees2.drawAll(tree1img)
mytrees3.drawAll(hb2img)
mx=mymouse.x
my=mymouse.y
#if mx>display.left and mx<display.right and my>display.top and my<display.bottom:
rot += (mx-omx)*0.2
tilt -= (my-omy)*0.2
omx=mx
omy=my
#Press ESCAPE to terminate
k = mykeys.read()
if k >-1:
if k==119: #key W
xm-=math.sin(rot*rads)
zm+=math.cos(rot*rads)
ym = -(mymap.calcHeight(xm,zm)+avhgt)
elif k==115: #kry S
xm+=math.sin(rot*rads)
zm-=math.cos(rot*rads)
ym = -(mymap.calcHeight(xm,zm)+avhgt)
elif k==39: #key '
tilt -= 2.0
print tilt
elif k==47: #key /
tilt += 2.0
elif k==97: #key A
rot -= 2
elif k==100: #key D
rot += 2
elif k==112: #key P
display.screenshot("forestWalk"+str(scshots)+".jpg")
scshots += 1
elif k==10: #key RETURN
mc = 0
elif k==27: #Escape key
#pi3d.quit()
display.destroy()
mykeys.close()
break
else:
print k
display.swapBuffers()