-
Notifications
You must be signed in to change notification settings - Fork 0
/
primitives.py
303 lines (247 loc) · 8.15 KB
/
primitives.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#------------------------------------------------
#
#
#------------------------------------------------
import pyglet
from pyglet.gl import *
import os
import pprint
import sys
import random
textures = {}
default_texture = None
gap_asso = {}
def set_gap_association(asso):
global gap_asso
gap_asso = asso
def loading_textures():
global textures, default_texture
def walk(list_, datas, texture):
#generate recursively tree structure
if len(list_)>2:
if not list_[0] in datas:
datas[list_[0]]={}
walk(list_[1:], datas[list_[0]], texture)
else:
if not list_[0] in datas:
datas[list_[0]]={}
datas[list_[0]][list_[-1]]=texture
return texture
list_of_textures = [os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser(os.sep.join(["datas","textures"]))) for f in fn]
for e in list_of_textures:
try:
image = pyglet.image.load(e)
except pyglet.image.codecs.dds.DDSException:
print '"%s" is not a valid image file' % e
continue
texture = image.get_texture()
walk(e.split(os.sep)[2:], textures, image.get_texture())
default_texture = textures["wall"]["wall1.jpg"]
print "finish loading textures"
#pprint.pprint(textures)
return textures
def draw_plane(texture=default_texture ,color = (1,0,0), type_texturing="texture", scale_uv=(1,1)):
if type_texturing != "texture":
glDisable(GL_TEXTURE_2D)
glBegin(GL_QUADS)
glColor3f(*color)
glVertex3f( 1.0, 1.0, 0.0 )
glVertex3f( -1.0, 1.0, 0.0 )
glVertex3f( -1.0, -1.0, 0.0 )
glVertex3f( 1.0, -1.0, 0.0 )
glEnd()
else:
glEnable(GL_TEXTURE_2D)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE)
glBindTexture(texture.target, texture.id)
glBegin(GL_QUADS)
glTexCoord2f(0.0, 0.0)
glVertex3f( -1.0, -1.0, 0.0 )
glTexCoord2f(1.0*scale_uv[0], 0.0)
glVertex3f( 1.0, -1.0, 0.0 )
glTexCoord2f(1.0*scale_uv[0], 1.0*scale_uv[1])
glVertex3f( 1.0, 1.0, 0.0 )
glTexCoord2f(0.0, 1.0*scale_uv[1])
glVertex3f( -1.0, 1.0, 0.0 )
glEnd()
def draw_cube(textures_=[default_texture]*6, colors= [[1,0,0]]*6, type_texturing="texture", scale_uv=(1,1)):
#avoid trouble with colors
#tests if all colors are present, whereas adds some number of default color to array
colors.extend([[1,0,0]]*(6-len(colors)))
textures_.extend([default_texture]*(6-len(textures_)))
#draw front face
glPushMatrix()
glTranslatef(0,0,1)
draw_plane(color=colors[0], texture=textures_[0], type_texturing=type_texturing, scale_uv=scale_uv)
glPopMatrix()
#draw back face
glPushMatrix()
glTranslatef(0,0,-1)
draw_plane(color=colors[1], texture=textures_[1], type_texturing=type_texturing, scale_uv=scale_uv)
glPopMatrix()
#draw left face
glPushMatrix()
glTranslatef(1,0,0)
glRotatef(90, 0,1,0)
draw_plane(color=colors[2], texture=textures_[2], type_texturing=type_texturing, scale_uv=scale_uv)
glPopMatrix()
#draw right face
glPushMatrix()
glTranslatef(-1,0,0)
glRotatef(90, 0,1,0)
draw_plane(color=colors[3], texture=textures_[3], type_texturing=type_texturing, scale_uv=scale_uv)
glPopMatrix()
#draw bottom face
glPushMatrix()
glTranslatef(0,-1,0)
glRotatef(90, 1,0,0)
draw_plane(color=colors[4], texture=textures_[4], type_texturing=type_texturing, scale_uv=scale_uv)
glPopMatrix()
#draw top face
glPushMatrix()
glTranslatef(0,1,0)
glRotatef(90, 1,0,0)
draw_plane(color=colors[5], texture=textures_[5], type_texturing=type_texturing, scale_uv=scale_uv)
glPopMatrix()
def draw_wall(gap="wall", dimensions=(10,11,0.1), textures_=default_texture, colors= [[1,0,0]]*6, type_texturing="texture", pediment=False, paintings=[]):
#A wall involve two flattened cube separe by a gap in the middle
gap_name = gap
#draw paintings
textures_= [textures_]*6
gap = gap_asso[gap]
if gap<=0:
gap = 0
if gap >= dimensions[0]:
return
l = (dimensions[0]-gap)/2.0
x1 = -gap/1.0 - l
x2 = -x1
#draw painting
n = len(paintings)
if gap == "wall":
def drange(start, stop, step):
r = start
while r < stop:
yield r
r += step
begin = -dimensions[0]/2.0 + dimensions[0]/float(n+1)
end = -dimensions[0]/2.0 + (n+1)*dimensions[0]/float(n+1)
step = dimensions[0]/float(n+1)
k=0
for x in drange(begin, end, step):
glPushMatrix()
glTranslatef(2*x,dimensions[1]/2.0,0.2)
draw_painting(paintings[k])
glPopMatrix()
k+=1
else:
#place painting on 1st doorpost
if n:
glPushMatrix()
glTranslatef(x1,dimensions[1]/2.0,0.2)
draw_painting(paintings[0])
glPopMatrix()
if n>1:
glPushMatrix()
glTranslatef(x2,dimensions[1]/2.0,0.2)
draw_painting(paintings[1])
glPopMatrix()
#draw first doorpost
glPushMatrix()
glTranslatef(x1, 1, 0)
glScalef(l, dimensions[1]-1, dimensions[2])
draw_cube(textures_=textures_, colors= [[1,0,0]]*6, type_texturing="texture", scale_uv=(dimensions[0], dimensions[1]/2))
glPopMatrix()
#draw second doorpost
glPushMatrix()
glTranslatef(x2, 1, 0)
glScalef(l, dimensions[1]-1, dimensions[2])
draw_cube(textures_=textures_, colors= [[1,0,0]]*6, type_texturing="texture", scale_uv=(dimensions[0], dimensions[1]/2))
glPopMatrix()
#draw pediment
if pediment and gap:
h = dimensions[1]/3.0
y = 2*h
x3 = 0
glPushMatrix()
glTranslatef(x3, y+1, 0)
glScalef(gap, h-1, dimensions[2])
draw_cube(textures_=textures_, colors= [[1,0,0]]*6, type_texturing="texture", scale_uv=(gap/2.0, h))
glPopMatrix()
def draw_room(gap=[0]*4, dimensions=[[10,11,0.1]]*4, textures_=None, colors= [[[1,0,0]]*6]*4, type_texturing=["texture"]*4, pediment=[False]*4, paintings=[], signalisation=-1):
if not textures_:
textures_=[[default_texture]*6]*4+[default_texture]*2
#draw northern wall
dim = dimensions[0]
glPushMatrix()
glTranslatef(0,0,-dim[0]-dim[2]/2.0)
draw_wall(gap=gap[0], dimensions=dim, textures_=textures_[0], colors=colors[0], type_texturing=type_texturing[0], pediment=pediment[0], paintings=paintings[0])
glPopMatrix()
#draw southern wall
dim = dimensions[1]
glPushMatrix()
glTranslatef(0,0,dim[0]+dim[2]/2.0)
glRotatef(180,0,1,0)
draw_wall(gap=gap[1], dimensions=dim, textures_=textures_[1], colors=colors[1], type_texturing=type_texturing[1], pediment=pediment[1], paintings=paintings[1])
glPopMatrix()
#draw eastern wall
dim = dimensions[2]
glPushMatrix()
glTranslatef(-dim[0]-dim[2]/2.0,0,0)
glRotatef(90, 0, 1, 0)
draw_wall(gap=gap[2], dimensions=dim, textures_=textures_[2], colors=colors[2], type_texturing=type_texturing[2], pediment=pediment[2], paintings=paintings[2])
glPopMatrix()
#draw western wall
dim = dimensions[3]
glPushMatrix()
glTranslatef(dim[0]+dim[2]/2.0,0,0)
glRotatef(-90, 0, 1, 0)
draw_wall(gap=gap[3], dimensions=dim, textures_=textures_[3], colors=colors[3], type_texturing=type_texturing[3], pediment=pediment[3], paintings=paintings[3])
glPopMatrix()
#draw ground
glPushMatrix()
glTranslatef(0,-1,0)
glRotatef(90, 1,0,0)
glScalef(dimensions[0][0], dimensions[1][0],0)
draw_plane(texture=textures_[4], scale_uv=(dimensions[0][0],dimensions[1][0]))
glPopMatrix()
#draw ceiling
glPushMatrix()
glTranslatef(0, dimensions[1][1],0)
glRotatef(90, 1,0,0)
glScalef(dimensions[0][0], dimensions[1][0],0)
draw_plane(texture=textures_[5], scale_uv=(dimensions[0][0],dimensions[1][0]))
glPopMatrix()
#draw signalisation
if (signalisation != -1): #Don't draw anything
if signalisation == -2 : #Begin
glPushMatrix()
glTranslatef(0,-0.95,0);
#Rotate to put it on the ground
glRotatef(90, 1,0,0)
draw_plane(texture = textures["signalisation"]["home.png"])
glPopMatrix()
elif signalisation == -3 : #end
glPushMatrix()
glTranslatef(0,-0.95,0);
#Rotate to put it on the ground
glRotatef(90, 1,0,0)
draw_plane(texture = textures["signalisation"]["door.png"])
glPopMatrix()
else :
glPushMatrix()
glTranslatef(0,-0.95,0);
glRotatef(signalisation, 0, 1, 0)
#Rotate to put it on the ground
glRotatef(90, 1,0,0)
draw_plane(texture = textures["signalisation"]["arrow.png"])
glPopMatrix()
def draw_painting(texture):
glPushMatrix()
glScalef(1,1,0.05)
draw_cube(textures_=[texture], colors= [[1,0,0]]*6, type_texturing="texture", scale_uv=(1,1))
glPopMatrix()