-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wall.bb
367 lines (302 loc) · 9.56 KB
/
Wall.bb
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
Global WALL_TEXTURE
Global WALL_MESH
Function InitialiseWallFiles()
;UnPackAsset(PACK_WALL_ANIM_START,PACK_WALL_ANIM_LENGTH)
UnPackAsset(PACK_WALL_MAT_START,PACK_WALL_MAT_LENGTH)
End Function
Function UnInitialiseWallFiles()
If (TEST) Then Return
;DeleteFile WallAnimFile()
DeleteFile WallMatFile()
End Function
Function WallMatFile$()
Return MatFile(MAP_WALL_NAME)
End Function
Function BuildWallGeometries()
InitialiseWallFiles
BuildWallMesh
BuildGhostMesh
CombineGhostGeometry
FinaliseWalls
UnInitialiseWallFiles
End Function
Function BuildWallMesh()
If (WALL_MESH)
FreeEntity WALL_MESH
WALL_MESH=0
End If
WALL_MESH=BuildWallGeometry()
End Function
Function FinaliseWalls()
PaintWall
SetWallPhysics
End Function
Function PaintWall()
If (WALL_TEXTURE)
FreeTexture WALL_TEXTURE
WALL_TEXTURE=0
End If
If (Not(SPECTRUM_MODE))
WALL_TEXTURE=AcquireTextureMap(WallMatFile())
ScaleTexture WALL_TEXTURE,1/MAPSIZEX,1.0
EntityTexture WALL_MESH,WALL_TEXTURE
FreeTexture WALL_TEXTURE
Else
EntityColor WALL_MESH,255,255,255
EntityShininess WALL_MESH,1.0
EntityFX WALL_MESH,4
End If
End Function
Function SetWallPhysics()
EntityPickMode WALL_MESH,2,True
ScaleMesh WALL_MESH,1,1+(SPECTRUM_MODE*1),1
End Function
Function BuildWallGeometry(Parent=0,AddRoof=True)
Local Mesh=CreateMesh(Parent)
Local Surf=CreateSurface(Mesh)
Dim WALL_FLAG%(MAPSIZEX,MAPSIZEY);
Local W=MAPSIZEX-1
Local H=MAPSIZEY-1
Local i%,j%
Local x%,y%
Local x0%,y0%
Local x1%,y1%
Local v
For j = 0 To H
For i = 0 To W
; this cell has a bottom wall
If ((HAS_WALL(i,j) And MAP_BOTTOM_BOUND))
; if this wall hasn't already been built
If ((WALL_FLAG(i,j) And MAP_BOTTOM_BOUND) = 0)
x0 = i : x1 = i : y0 = j : y1 = j;
; find last cell that has this wall
For x = x0 To W
If ((HAS_WALL(x,j) And MAP_BOTTOM_BOUND)=0) Then Exit;
WALL_FLAG(x,j) = WALL_FLAG(x,j) Or MAP_BOTTOM_BOUND : x1=x;
Next;
; build the wall.
v = AddVertex (Surf, x0 , 1,y0 ) : VertexNormal(Surf, v , 0, 0,-1);
AddVertex (Surf, x1+1, 1,y0 ) : VertexNormal(Surf, v+1, 0, 0,-1);
AddVertex (Surf, x1+1, 0,y0 ) : VertexNormal(Surf, v+2, 0, 0,-1);
AddVertex (Surf, x0 , 0,y0 ) : VertexNormal(Surf, v+3, 0, 0,-1);
AddTriangle (Surf, v,v+1,v+2);
AddTriangle (Surf, v,v+2,v+3);
EndIf;
EndIf;
; this cell has a top wall
If ((HAS_WALL(i,j) And MAP_TOP_BOUND))
If ((WALL_FLAG(i,j) And MAP_TOP_BOUND) = 0)
x0 = i : x1 = i : y0 = j : y1 = j;
For x = x0 To W
If ((HAS_WALL(x,j) And MAP_TOP_BOUND)=0) Then Exit;
WALL_FLAG(x,j) = WALL_FLAG(x,j) Or MAP_TOP_BOUND : x1=x;
Next;
v = AddVertex (Surf, x1+1, 1,y0+1) : VertexNormal(Surf, v , 0, 0,+1);
AddVertex (Surf, x0 , 1,y0+1) : VertexNormal(Surf, v+1, 0, 0,+1);
AddVertex (Surf, x0 , 0,y0+1) : VertexNormal(Surf, v+2, 0, 0,+1);
AddVertex (Surf, x1+1, 0,y0+1) : VertexNormal(Surf, v+3, 0, 0,+1);
AddTriangle (Surf, v,v+1,v+2);
AddTriangle (Surf, v,v+2,v+3);
EndIf;
EndIf;
; this cell has a left wall
If ((HAS_WALL(i,j) And MAP_LEFT_BOUND))
If ((WALL_FLAG(i,j) And MAP_LEFT_BOUND) = 0)
x0 = i : x1 = i : y0 = j : y1 = j;
For y = y0 To H
If ((HAS_WALL(i,y) And MAP_LEFT_BOUND)=0) Then Exit;
WALL_FLAG(i,y) = WALL_FLAG(i,y) Or MAP_LEFT_BOUND : y1=y;
Next;
v = AddVertex (Surf, x0, 1,y1+1) : VertexNormal(Surf, v , -1, 0, 0);
AddVertex (Surf, x0, 1,y0 ) : VertexNormal(Surf, v+1, -1, 0, 0);
AddVertex (Surf, x0, 0,y0 ) : VertexNormal(Surf, v+2, -1, 0, 0);
AddVertex (Surf, x0, 0,y1+1) : VertexNormal(Surf, v+3, -1, 0, 0);
AddTriangle (Surf, v,v+1,v+2);
AddTriangle (Surf, v,v+2,v+3);
EndIf;
EndIf;
; this cell has a right wall
If ((HAS_WALL(i,j) And MAP_RIGHT_BOUND))
If ((WALL_FLAG(i,j) And MAP_RIGHT_BOUND) = 0)
x0 = i : x1 = i : y0 = j : y1 = j;
For y = y0 To H
If ((HAS_WALL(i,y) And MAP_RIGHT_BOUND)=0) Then Exit;
WALL_FLAG(i,y) = WALL_FLAG(i,y) Or MAP_RIGHT_BOUND: y1=y;
Next
v = AddVertex (Surf, x0+1, 1,y0 ) : VertexNormal(Surf, v , +1, 0, 0);
AddVertex (Surf, x0+1, 1,y1+1) : VertexNormal(Surf, v+1, +1, 0, 0);
AddVertex (Surf, x0+1, 0,y1+1) : VertexNormal(Surf, v+2, +1, 0, 0);
AddVertex (Surf, x0+1, 0,y0 ) : VertexNormal(Surf, v+3, +1, 0, 0);
AddTriangle (Surf, v,v+1,v+2);
AddTriangle (Surf, v,v+2,v+3);
EndIf
EndIf
Next
Next
Dim WALL_FLAG(0,0)
Dim HAS_WALL(0,0)
If (AddRoof) Then AddRoofGeometry(Surf)
UpdateNormals Mesh
CalculateUVs(Surf)
Return Mesh
End Function
Function AddRoofGeometry(Surf)
Local W=MAPSIZEX-1
Local H=MAPSIZEY-1
Local LengthH
Local LengthV
Local v
Local X
Local Y
Local i
Local j
Local x0,x1
Local y0,y1
; build roofs
Dim WALL_FLAG(W+1,H+1) ; reset wall flags and reuse for roofs
For j = 0 To H
For i = 0 To W
If (PeekArrayValue(i,j)=MAP_WALL)
If (WALL_FLAG(i,j)=MAP_GROUND)
LengthH = 0;
LengthV = 0;
; check farthest available cell to the right
x0 = i : y0 = j;
X = x0 : Y = y0;
For X = x0 To W
If (PeekArrayValue(X,j)<>MAP_WALL) Then Exit;
If (WALL_FLAG(X,j)<>MAP_GROUND) Then Exit;
x1 = X;
Next;
LengthH = x1-x0;
; check farthest available to the bottom
For Y = y0 To H
If (PeekArrayValue(i,Y)<>MAP_WALL) Then Exit;
If (WALL_FLAG(i,Y)<>MAP_GROUND) Then Exit;
y1 = Y;
Next;
LengthV = y1-y0
; according to the longest segment build a quad
If (LengthH>=LengthV)
; horizontal
For X = x0 To x1
; tag the cell as built
WALL_FLAG(X,j) = 1
Next
; from x0 to x1+1 (+1 because we need the right segment of the cell)
v = AddVertex (Surf, x0,1,y0+1) : VertexNormal(Surf, v , 0,+1, 0);
AddVertex (Surf, x1+1,1,y0+1) : VertexNormal(Surf, v+1, 0,+1, 0);
AddVertex (Surf, x1+1,1,y0 ) : VertexNormal(Surf, v+2, 0,+1, 0);
AddVertex (Surf, x0 ,1,y0 ) : VertexNormal(Surf, v+3, 0,+1, 0);
AddTriangle (Surf, v,v+1,v+2);
AddTriangle (Surf, v,v+2,v+3);
Else
; vertical
For Y = y0 To y1
; tag the cell as built
WALL_FLAG(i,Y) = 1
Next
v = AddVertex (Surf, x0 , 1,y1+1) : VertexNormal(Surf, v , 0,+1, 0);
AddVertex (Surf, x0+1, 1,y1+1) : VertexNormal(Surf, v+1, 0,+1, 0);
AddVertex (Surf, x0+1, 1,y0 ) : VertexNormal(Surf, v+2, 0,+1, 0);
AddVertex (Surf, x0 , 1,y0 ) : VertexNormal(Surf, v+3, 0,+1, 0);
AddTriangle (Surf, v,v+1,v+2);
AddTriangle (Surf, v,v+2,v+3);
EndIf;
EndIf
EndIf;
Next;
Next;
Dim WALL_FLAG(0,0)
End Function
Function CalculateUVs(Surface)
Local MaxTri=CountTriangles(Surface)
Local Triangle
Local Vertex
Local XMax#
Local XMin#
Local YMax#
Local YMin#
Local ZMax#
Local ZMin#
Local TNX#
Local TNY#
Local TNZ#
Local v
For Triangle= 0 To MaxTri-1
XMin=9999
YMin=9999
ZMin=9999
XMax=-9999
YMax=-9999
ZMax=-9999
;First pass : Determine Normals and size of triangle-----------------------------------------------------------------------------
For Vertex=0 To 2
v=TriangleVertex(Surface,Triangle,Vertex)
If (VertexX(Surface,v)<XMin) Then XMin=VertexX(Surface,v)
If (VertexX(Surface,v)>XMax) Then XMax=VertexX(Surface,v)
If (VertexY(Surface,v)<YMin) Then YMin=VertexY(Surface,v)
If (VertexY(Surface,v)>YMax) Then YMax=VertexY(Surface,v)
If (VertexZ(Surface,v)<ZMin) Then ZMin=VertexZ(Surface,v)
If (VertexZ(Surface,v)>ZMax) Then ZMax=VertexZ(Surface,v)
Next
;The good thing is, all vertices of a triangle will all have equal Normals since walls are straight and aligned with axes.
TNX=VertexNX(Surface,v)
TNY=VertexNY(Surface,v)
TNZ=VertexNZ(Surface,v)
Local LengthX#=XMax-XMin
Local LengthY#=YMax-YMin
Local LengthZ#=ZMax-ZMin
;Second Pass : populate UV----------------------------------------------------------------------------------------------------------
Local TexU#
Local TexV#
For Vertex=0 To 2
v=TriangleVertex(Surface,Triangle,Vertex)
If (TNY>0)
;Roof
TexU=(VertexX(Surface,v)-XMin)/LengthX
TexV=(VertexZ(Surface,v)-ZMin)/LengthZ
TexV=(TexV*0.5)+0.5
Else
If (TNX<0)
;Bottom
TexU=(VertexX(Surface,v)-XMin)/LengthX
TexV=1.0 - ((VertexY(Surface,v)-YMin)/LengthY)
TexV=TexV*0.5
Else
If (TNX>0)
;Top
TexU=1.0 - ( (VertexX(Surface,v)-XMin)/LengthX )
TexV=1.0 - ((VertexY(Surface,v)-YMin)/LengthY)
TexV=TexV*0.5
Else
If(TNZ>0)
;Right
TexU=(VertexZ(Surface,v)-ZMin)/LengthZ
TexV=1.0 - ((VertexY(Surface,v)-YMin)/LengthY)
TexV=TexV*0.5
Else
If (TNZ<0)
;Left
TexU=1.0 - ( (VertexZ(Surface,v)-ZMin)/LengthZ)
TexV=1.0 - ((VertexY(Surface,v)-YMin)/LengthY)
TexV=TexV*0.5
Else
TexU=0
TexV=0
End If
End If
End If
End If
End If
VertexTexCoords Surface,v,TexU,TexV
Next
Next
End Function
Function RemoveWalls()
FreeEntity WALL_MESH
WALL_MESH=0
End Function
;~IDEal Editor Parameters:
;~F#3#8#F#13#1D#26#2B#3D#42#A9#F8#168
;~C#Blitz3D