-
Notifications
You must be signed in to change notification settings - Fork 0
/
EggMaker.py
514 lines (475 loc) · 22.2 KB
/
EggMaker.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# Dylan Tom
# andrew id: dtom
# for creating eggs
import random, math
class EggObject(object):
gridWidth = 100
# the y axis representation of length
gridDepth = 100
gridHeight = 100
def __init__(self, name, texture="rocks.jpg", posX=0, posY=0, posZ=0, size=10):
self.eggString = ""
self.defaultSettings()
self.posX = posX
self.posY = posY
self.posZ = posZ
self.size = size
self.filename = name + ".egg"
self.texture = texture
def getNormal(self, pos1, pos2, pos3):
# will need to fix this somehow sometime, the normals are sometimes
# negative of what they are supposed to be!
# the 0th index is x, first y, second z in each pos
x = (pos1[1]-pos2[1])*(pos1[2]-pos3[2])-(pos1[2]-pos2[2])*(pos1[1]-pos3[1])
y = (pos1[2]-pos2[2])*(pos1[0]-pos3[0])-(pos1[0]-pos2[0])*(pos1[2]-pos3[2])
z = (pos1[0]-pos2[0])*(pos1[1]-pos3[1])-(pos1[1]-pos2[1])*(pos1[0]-pos3[0])
return (x,y,z)
@staticmethod
def setGridWidthAndHeight(width, depth, height):
gridWidth = width
gridHeight = height
gridDepth = depth
def editFile(self):
file = open(self.filename, "w")
file.write(self.eggString)
file.close()
def __repr__(self):
return self.filename
# basically so that it is formatted well
def addCommand(self, newCommand):
self.eggString = self.eggString + "\n" + newCommand
def defaultSettings(self):
self.eggString += "<CoordinateSystem> { Z-Up }"
def addTexture(self, textureName, textureLocation):
self.addCommand("<Texture> "+textureName+" {")
self.addCommand(" "+textureLocation)
self.addCommand(" <Scalar> format { rgb }")
self.addCommand(" <Scalar> wrapu { repeat }")
self.addCommand(" <Scalar> wrapv { repeat }")
self.addCommand(" <Scalar> minfilter { linear_mipmap_linear }")
self.addCommand(" <Scalar> magfilter { linear }")
self.addCommand(" <Scalar> envtype { normal_height }")
self.addCommand("}")
class Car(EggObject):
def __init__(self, name, posX, posY, posZ):
super().__init__(name, posX, posY, posZ)
self.createVertexes()
self.addCollisionToPolygons()
def createPoints(self):
pass
def addCollisionToPolygons(self):
# used this to get how to add the collision to the egg file:
# https://www.panda3d.org/forums/viewtopic.php?t=782
self.addCommand("<Group> %s {" % ("car"))# add self.__repr__ later
#self.addCommand(" <Collide> mountainCollide { Polygon level keep descend event }")
self.createPolygons()
self.addCommand("}")
# make filename something that is initially set so that it is easy to name the
# collision array
def createVertexes(self):
# update to have normal vectors
self.addCommand("<VertexPool> %s.verts {" % ("CarVertexes"))
for i in range(2):
for j in range(2):
for k in range(2):
self.addCommand(" <Vertex> %d { %d %d %d " %
(i*100+j*10+k,i*10,j*2*10,k*10))
self.addCommand(" <UV> { %d %d }" % ((i+2*k)*.5,(j+2*k)*.5))
self.addCommand("}")
self.addCommand("}")
def createPolygons(self):
texture = "silver.jpg"
k=0
j=0
for i in range(2):
self.addCommand(" <Collide> poleCollide { Polygon keep descend event }")
pos1 = [i,j,k]
pos2 = [i,j+1,k]
pos3 = [i,j,k+1]
normalX, normalY, normalZ = self.getNormal(pos1,pos2,pos3)
self.addCommand("<Polygon> {")
self.addCommand(" <Texture> { %s }" % (texture))
self.addCommand(" <Normal> { %f %f %f }"%(normalX, normalY, normalZ))
self.addCommand(" <VertexRef> { %d %d %d %d <Ref> { %s.verts } }"
% (i*100+j*10+k, i*100+j*10+(k+1),i*100+10*(j+1)+(k+1),
i*100+(j+1)*10+k, "CarVertexes"))
self.addCommand("}")
i = 0
k=0
for j in range(2):
self.addCommand(" <Collide> poleCollide { Polygon keep descend event }")
pos1 = [i,j,k]
pos2 = [i+1,j,k]
pos3 = [i,j,k+1]
normalX, normalY, normalZ = self.getNormal(pos1,pos2,pos3)
self.addCommand("<Polygon> {")
self.addCommand(" <Texture> { %s }" % (texture))
self.addCommand(" <Normal> { %f %f %f }"%(normalX, normalY, normalZ))
self.addCommand(" <VertexRef> { %d %d %d %d <Ref> { %s.verts } }"
% (i*100+j*10+k, i*100+j*10+(k+1),(i+1)*100+10*j+(k+1),
(i+1)*100+j*10+k, "CarVertexes"))
self.addCommand("}")
i = 0
j = 0
for k in range(2):
self.addCommand(" <Collide> poleCollide { Polygon keep descend event }")
pos1 = [i,j,k]
pos2 = [i,j+1,k]
pos3 = [i+1,j,k]
normalX, normalY, normalZ = self.getNormal(pos1,pos2,pos3)
self.addCommand("<Polygon> {")
self.addCommand(" <Texture> { %s }" % (texture))
self.addCommand(" <Normal> { %f %f %f }"%(normalX, normalY, normalZ))
self.addCommand(" <VertexRef> { %d %d %d %d <Ref> { %s.verts } }"
% (i*100+j*10+k, i*100+(j+1)*10+k,(i+1)*100+10*(j+1)+k,
(i+1)*100+j*10+k, "CarVertexes"))
self.addCommand("}")
class BeaconLight(Car):
def createPolygons(self):
# got yellow from here: https://upload.wikimedia.org/wikipedia/commons/4/44/WO_Yellow.jpg
texture = "yellow.jpg"
k=0
j=0
for i in range(2):
self.addCommand(" <Collide> poleCollide { Polygon keep descend event }")
pos1 = [i,j,k]
pos2 = [i,j+1,k]
pos3 = [i,j,k+1]
normalX, normalY, normalZ = self.getNormal(pos1,pos2,pos3)
self.addCommand("<Polygon> {")
self.addCommand(" <Texture> { %s }" % (texture))
self.addCommand(" <Normal> { %f %f %f }"%(normalX, normalY, normalZ))
self.addCommand(" <VertexRef> { %d %d %d %d <Ref> { %s.verts } }"
% (i*100+j*10+k, i*100+j*10+(k+1),i*100+10*(j+1)+(k+1),
i*100+(j+1)*10+k, "CarVertexes"))
self.addCommand("}")
i = 0
k=0
for j in range(2):
self.addCommand(" <Collide> poleCollide { Polygon keep descend event }")
pos1 = [i,j,k]
pos2 = [i+1,j,k]
pos3 = [i,j,k+1]
normalX, normalY, normalZ = self.getNormal(pos1,pos2,pos3)
self.addCommand("<Polygon> {")
self.addCommand(" <Texture> { %s }" % (texture))
self.addCommand(" <Normal> { %f %f %f }"%(normalX, normalY, normalZ))
self.addCommand(" <VertexRef> { %d %d %d %d <Ref> { %s.verts } }"
% (i*100+j*10+k, i*100+j*10+(k+1),(i+1)*100+10*j+(k+1),
(i+1)*100+j*10+k, "CarVertexes"))
self.addCommand("}")
i = 0
j = 0
for k in range(2):
self.addCommand(" <Collide> poleCollide { Polygon keep descend event }")
pos1 = [i,j,k]
pos2 = [i,j+1,k]
pos3 = [i+1,j,k]
normalX, normalY, normalZ = self.getNormal(pos1,pos2,pos3)
self.addCommand("<Polygon> {")
self.addCommand(" <Texture> { %s }" % (texture))
self.addCommand(" <Normal> { %f %f %f }"%(normalX, normalY, normalZ))
self.addCommand(" <VertexRef> { %d %d %d %d <Ref> { %s.verts } }"
% (i*100+j*10+k, i*100+(j+1)*10+k,(i+1)*100+10*(j+1)+k,
(i+1)*100+j*10+k, "CarVertexes"))
self.addCommand("}")
class Pole(EggObject):
def __init__(self, name, posX, posY, posZ):
super().__init__(name, posX, posY, posZ)
self.createVertexes()
self.addCollisionToPolygons()
def createPoints(self):
pass
def addCollisionToPolygons(self):
# used this to get how to add the collision to the egg file:
# https://www.panda3d.org/forums/viewtopic.php?t=782
self.addCommand("<Group> %s {" % ("pole"))# add self.__repr__ later
self.createPolygons()
self.addCommand("}")
# make filename something that is initially set so thatit is easy to name the
# collision array
def createVertexes(self):
# update to have normal vectors
self.addCommand("<VertexPool> %s.verts {" % ("CarVertexes"))
for i in range(2):
for j in range(2):
for k in range(2):
self.addCommand(" <Vertex> %d { %d %d %d " %
(i*100+j*10+k,i*10,j*10,k*100))
self.addCommand(" <UV> { %d %d }" % ((i+2*k)*.5,(j+2*k)*.5))
self.addCommand("}")
self.addCommand("}")
def createPolygons(self):
texture = "silver.jpg"
k=0
j=0
for i in range(2):
self.addCommand(" <Collide> poleCollide { Polygon keep descend event }")
self.addCommand("<Polygon> {")
self.addCommand(" <Texture> { %s }" % (texture))
self.addCommand(" <VertexRef> { %d %d %d %d <Ref> { %s.verts } }"
% (i*100+j*10+k, i*100+j*10+(k+1),i*100+10*(j+1)+(k+1),
i*100+(j+1)*10+k, "CarVertexes"))
self.addCommand("}")
i = 0
k=0
for j in range(2):
self.addCommand(" <Collide> poleCollide { Polygon keep descend event }")
self.addCommand("<Polygon> {")
self.addCommand(" <Texture> { %s }" % (texture))
self.addCommand(" <VertexRef> { %d %d %d %d <Ref> { %s.verts } }"
% (i*100+j*10+k, i*100+j*10+(k+1),(i+1)*100+10*j+(k+1),
(i+1)*100+j*10+k, "CarVertexes"))
self.addCommand("}")
i = 0
j = 0
for k in range(2):
self.addCommand(" <Collide> poleCollide { Polygon keep descend event }")
self.addCommand("<Polygon> {")
self.addCommand(" <Texture> { %s }" % (texture))
self.addCommand(" <VertexRef> { %d %d %d %d <Ref> { %s.verts } }"
% (i*100+j*10+k, i*100+(j+1)*10+k,(i+1)*100+10*(j+1)+k,
(i+1)*100+j*10+k, "CarVertexes"))
self.addCommand("}")
class Rock(EggObject):
rocks = 0
def __init__(self, name, randPos=True, size=10, posX=0, posY=0,posZ=0,roughness=2):
if(randPos):
# replace later with a way of getting the exact height
self.posX = random.randint(-EggObject.gridWidth, EggObject.gridWidth)
self.posY = random.randint(-EggObject.gridDepth, EggObject.gridDepth)
self.posZ = random.randint(-EggObject.gridHeight, EggObject.gridHeight)
self.filename = name + ".egg"
else:
super().__init__(name, posX, posY, posZ, size)
self.roughness = roughness
# number of points defining center of the rock
self.circumRockGrid = 20
# how many rows of points there are
# easier with odd numbers
self.latsRockGrid = 9
self.rockGrid = [[0]*self.circumRockGrid]*self.latsRockGrid
self.makeRockGrid()
Rock.rocks+=1
# self.createRandomDistances
#self.decreaseHugeGradient(roughness)
self.createVertexPool()
self.createPolygons()
def makeRockGrid(self):
# can later make this so that it doesn't use magic numbers, but for
# now it is just going to be a rock difined by the parameters that I
# give it so it doesn't have to be generalized namely 8 rows, 20 cols
# the number of points in concentric circle is difined by circum*cos(pi/2/lats)
# rock grid starts from bottom
circum = self.curcumRockGrid
lats = self.latsRockGrid
self.rockGrid[0][cirum//2]+=1
# maybe do this with list comprehension
for point in range(circum//4,3*cirum//4):
self.rockGrid[1][point]+=1
for point in range(circum//2-cirum//(2**1.5), circum//2+cirum//(2**1.5)):
self.rockGrid[2][point]+=1
for point in range(circum//2-cirum//(4)*3**.5, circum//2+cirum//(4)*3**.5):
self.rockGrid[3][point]+=1
for point in range(0, circum):
self.rockGrid[4][point]+=1
for point in range(circum//2-cirum//(4)*3**.5, circum//2+cirum//(4)*3**.5):
self.rockGrid[5][point]+=1
for point in range(circum//2-cirum//(2**1.5), circum//2+cirum//(2**1.5)):
self.rockGrid[6][point]+=1
for point in range(circum//4,3*cirum//4):
self.rockGrid[7][point]+=1
self.rockGrid[8][cirum//2]+=1
rockAdjustment = 2
def makeRockPoints(self):
# the greek letter row for spherical coordinates
prow = -math.pi/2
deltaProw = math.pi/8
self.addCommand("<VertexPool> %s.verts {" % ("Rock Vertexes"))
for row in self.rockGrid:
theta = 0
trueRow = takeZerosOut(row)
print(trueRow)
deltaTheta = 2*math.pi/len(trueRow)
for col in trueRow:
x = 0 # edit later
y = 0
z = 0
self.addCommand(" <Vertex> %d { %d %d %d }" %
(row*10**Rock.rockAdjustment+col,row,col,0))
prow += deltaProw
self.addCommand("}")
def takeZerosOut(array):
newArray = []
for item in array:
if item != 0:
newArray.append(item)
return newArray
class Mountain(EggObject):
mountains=0
def __init__(self, name, points=50, maxGradient=10):
super().__init__(name)
self.createMountain()
self.heightGrid = []
self.heightGrid += [[0]*points for time in range(points)]
Mountain.mountains+=1
# use current mountain later so that when editting the file, the exact
# mountain that this is known.
# current Mountain
self.createRandomVertexes()
# call this the amount of times it takes to make all of the gradients
# look good, isn't expected to make the entire thing have gradient below
# measure, just makes sure after going over the grid once that the
# gradient of anything over this gradient is decreased
self.decreaseHugeGradient(5)
self.createVertexPool()
self.addCollisionToPolygons()
#self.createPolygons()
def nicePrint(self, array):
for row in array:
print(row)
def createMountain(self):
filename = "TestMountain.egg"
self.addTexture("ground", "models/ground.jpg")
self.addTexture("tree", "models/tree.jpg")
def createRandomVertexes(self):
# adapting algorithm that was found here:
# http://www.lighthouse3d.com/opengl/terrain/index.php3?particle
size = len(self.heightGrid)
# put the x and y in center of grid
x, y = size//2, size//2
totalHeight = 10000
for addition in range(totalHeight):
# increase height at specified point
self.heightGrid[x][y] += 1
x, y = self.newPoints(x,y)
def newPoints(self, curX, curY):
size = len(self.heightGrid)
newX =curX + random.randint(-1,1)
newY =curY + random.randint(-1,1)
if(newX<0 or newX>=size or newY<0 or newY>=size):
return curX, curY
else:
return newX, newY
def createVertexPool(self):
# currently made so that it can only run with single digits,
# will change later
# +2 so that it also gets the outer edges
size = len(self.heightGrid)+2
# distance between the points
distance = 500
# can use self.mountains because mountain is created before new one
# needed.
self.addCommand("<VertexPool> %s.verts {" % ("MountVertexes"+str(
self.mountains)))
distanceBetweenPoints = 20
for i in range(size):
for j in range(size):
# doing this so that the position in grid is maintained
# however the position in real space is corrected since the grid
# should start at 0,0,0 and surrounding area should be 1 away
xPoint = i-1
yPoint = j-1
# this is for the values that are not in grid
if(xPoint<0 or yPoint<0 or xPoint>=size-2 or yPoint>=size-2):
# the reason why the values are not negative to begin with
# is so that I can number the points by index
self.addCommand(" <Vertex> %d { %d %d %d " %
(i*Mountain.vertexAdjustment+j,
i*distanceBetweenPoints,
j*distanceBetweenPoints,0))
self.addCommand(" <UV> { %d %d }" % (i*.5,j*.5))
self.addCommand(" }")
else:
self.addCommand(" <Vertex> %d { %d %d %d " %
(i*Mountain.vertexAdjustment+j,
i*distanceBetweenPoints,
j*distanceBetweenPoints,
self.heightGrid[xPoint][yPoint]))
self.addCommand(" <UV> { %d %d }" % (i*.5,j*.5))
self.addCommand(" }")
# adds u,v,w so that the polygons has the right place to put the image
self.addCommand("}")
def addCollisionToPolygons(self):
# used this to get how to add the collision to the egg file:
# https://www.panda3d.org/forums/viewtopic.php?t=782
self.addCommand("<Group> %s {" % ("mountain"))# add self.__repr__ later
#self.addCommand(" <Collide> mountainCollide { Polygon level keep descend event }")
self.createPolygons()
self.addCommand("}")
vertexAdjustment = 1000
def createPolygons(self, texture="ground"):# texture="rocks.jpg"):#texture="ground"):
size = len(self.heightGrid)+1
for i in range(size):
for j in range(size):
# these if statements are to create a pattern, take out when
# figured out how to use actual textures
self.addCommand(" <Collide> mountainCollide { Polygon level keep descend event }")
if((j+i)%2 == 0):
self.addCommand("<Polygon> {")
self.addCommand(" <Texture> { %s }" % ("ground.jpg"))
# testing if this works for displaying texture
# self.addCommand("<TRef> { %s }" % (texture))
self.addCommand(" <VertexRef> { %d %d %d %d <Ref> { %s.verts } }"
% (i*Mountain.vertexAdjustment+j, (i+1)*Mountain.vertexAdjustment+j,
(i+1)*Mountain.vertexAdjustment+j+1, i*Mountain.vertexAdjustment+j+1,
"MountVertexes"+str(self.mountains)))
self.addCommand("}")
else:
self.addCommand("<Polygon> {")
self.addCommand(" <Texture> { %s }" % ("tree.jpg"))
# testing if this works for displaying texture
# self.addCommand("<TRef> { %s }" % ("tree"))
self.addCommand(" <VertexRef> { %d %d %d %d <Ref> { %s.verts } }"
% (i*Mountain.vertexAdjustment+j, (i+1)*Mountain.vertexAdjustment+j,
(i+1)*Mountain.vertexAdjustment+j+1, i*Mountain.vertexAdjustment+j+1,
"MountVertexes"+str(self.mountains)))
self.addCommand("}")
def decreaseHugeGradient(self, gradientMax = 20):
# given the random number that is choosen this will give a number
direction = [(-1,-1), (-1, 0), (-1, 1),
(0, -1), (0, 1),
(1, -1), (1, 0), (1, 1)]
for row in range(len(self.heightGrid)):
for col in range(len(self.heightGrid)):
if(Mountain.maxGradientSize(self.heightGrid, row, col
)>gradientMax):
# randomly place part of height somewhere else
self.heightGrid[row][col] -= gradientMax//4
(addRow, addCol) = direction[random.randint(0,7)]
while(not Mountain.isValidRowAndCol(self.heightGrid,
row+addRow, col+addCol)):
(addRow, addCol) = direction[random.randint(0,7)]
self.heightGrid[row+addRow][col+addCol] += gradientMax//4
def maxGradientSize(array, row, col):
maxGradient = 0
# the size of box to see how large gradient is
boxRadius = 1
for curRow in range(row-boxRadius, row+boxRadius+1):
for curCol in range(col-boxRadius, col+boxRadius+1):
# need to not cause error
if(Mountain.isValidRowAndCol(array, curRow, curCol)):
# need to do it this way with row and col first so that we
# only detect if the current row and col is too large
gradient = array[row][col]-array[curRow][curCol]
if(gradient>maxGradient):
maxGradient = gradient
return maxGradient
def isValidRowAndCol(array, row, col):
if(row>=0 and col>=0 and row<len(array) and col<len(array[0])):
return True
else:
return False
def main():
# testMountain1 so that I do not mess up current 1
# 999 is too large
m = Mountain("TestMountain1", 60)
m.editFile()
p = Pole("TestPole", 0, 0, 0)
p.editFile()
c = Car("TestCar", 0,0,0)
c.editFile()
b = BeaconLight("BeaconLight",0,0,0)
b.editFile()
if __name__ == '__main__':
main()