forked from adamredwoods/minib3d-monkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmesh.monkey
1976 lines (1305 loc) · 46.5 KB
/
tmesh.monkey
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Import minib3d
''Notes
'' - anim_surf now connnect to surface with surf_id
'' -- pre-bake animations which converts bone animation to vertex animation
'' - separated update() and render()
'' -- render() now in TRender.monkey
Class TMesh Extends TEntity
''mesh bounds
Field min_x#,min_y#,min_z#,max_x#,max_y#,max_z#
Field no_surfs:Int=0
Field surf_list:List<TSurface>= New List<TSurface>
Field anim_surf:TSurface[] ' contains animated vertex coords set; connected to surf by surf_id
Field no_bones=0
Field bones:TBone[]
Field col_tree:TColTree=New TColTree
' reset flags - these are set when mesh shape is changed by various commands in TMesh
Field reset_bounds=True
Field center_x:Float,center_y:Float,center_z:Float
Field bounds_radius:Float '' is this used?
'Field reset_col_tree=True
Field is_sprite:Bool = False '' used for sprites
Field is_update:Bool = False ''used for batching, etc.
Field wireframe:Bool
Private
Field vec_temp:Vector = New Vector
Public
Method New()
End
Method CopyEntity:TEntity(parent_ent:TEntity=Null)
' new mesh
Local mesh:TMesh=New TMesh
Self.CopyBaseMeshTo(mesh, parent_ent)
Return mesh
End
Method CopyBaseMeshTo:Void(mesh:TMesh, parent_ent:TEntity=Null)
Self.CopyBaseEntityTo(mesh, parent_ent)
mesh.is_sprite = is_sprite
mesh.is_update = is_update
mesh.wireframe = wireframe
' copy mesh info
mesh.min_x=min_x
mesh.min_y=min_y
mesh.min_z=min_z
mesh.max_x=max_x
mesh.max_y=max_y
mesh.max_z=max_z
mesh.no_surfs=no_surfs
' pointer to surf list
mesh.surf_list=surf_list
' copy anim surf list
mesh.anim_surf = CopyAnimSurfs()
mesh.col_tree=col_tree
mesh.reset_bounds=reset_bounds
mesh.bones = CopyBonesList(mesh, New List<TBone>, 0).ToArray()
''set bones
ResetBones()
End
'' CopyAnimSurfs()
'' -- full copy will keep new animation copies instead of pointer
Method CopyAnimSurfs:TSurface[]( fullcopy:Int =0 )
Local new_surf:TSurface[] = New TSurface[no_surfs] 'mesh.anim_surf[surf.surf_id]
For Local surf:TSurface=Eachin surf_list
Local id:Int = surf.surf_id
Local anim_surf2:TSurface = anim_surf[surf.surf_id] ''original anim_surf shortcut
If Not anim_surf2 Then Continue
' copy vertex, full copy
'If fullcopy
new_surf[id] = anim_surf2.Copy()
'Else
'new_surf[id] = New TSurface
'Endif
new_surf[id].no_verts=anim_surf2.no_verts
new_surf[id].vert_array_size=anim_surf2.vert_array_size
new_surf[id].tri_array_size=anim_surf2.tri_array_size
new_surf[id].vmin=anim_surf2.vmin
new_surf[id].vmax=anim_surf2.vmax
surf.surf_id = anim_surf2.surf_id
surf.vbo_dyn=anim_surf2.vbo_dyn
surf.alpha_enable=anim_surf2.alpha_enable
new_surf[id].reset_vbo=-1 ' (-1 = all)
'' 1=bones, 2=vertanims
If anim=1 And Not fullcopy
'mesh.anim_surf = New TSurface[no_surfs]
'Local new_surf:TSurface=New TSurface
'Local new_surf:TSurface = mesh.anim_surf[surf.surf_id]
'' dont need below, since Copy() method does this
'new_surf.vert_coords = CopyFloatBuffer(anim_surf2.vert_coords, FloatBuffer.Create(anim_surf2.no_verts*3) )
' pointers to arrays, don't need separate copies unles fullcopy=1
new_surf[id].tris = anim_surf2.tris
new_surf[id].vert_bone1_no=anim_surf2.vert_bone1_no
new_surf[id].vert_bone2_no=anim_surf2.vert_bone2_no
new_surf[id].vert_bone3_no=anim_surf2.vert_bone3_no
new_surf[id].vert_bone4_no=anim_surf2.vert_bone4_no
new_surf[id].vert_weight1=anim_surf2.vert_weight1
new_surf[id].vert_weight2=anim_surf2.vert_weight2
new_surf[id].vert_weight3=anim_surf2.vert_weight3
new_surf[id].vert_weight4=anim_surf2.vert_weight4
Elseif anim=2
'mesh.anim_surf = New TSurface[no_surfs]
'Local new_surf:TSurface=New TSurface
'mesh.anim_surf[surf.surf_id] = anim_surf2.Copy()
'Local new_surf:TSurface = mesh.anim_surf[surf.surf_id]
new_surf[id].vert_anim = anim_surf2.vert_anim ''pointer to shared anim data
Endif
Next
Return new_surf
End
Method FreeEntity()
''dont clear surf_list or will clear all entities
'Print "free mesh "+classname
anim_surf = New TSurface[0]
bones = New TBone[0]
Super.FreeEntity()
End
Function CreateMesh:TMesh(parent_ent:TEntity=Null)
Local mesh:TMesh=New TMesh
mesh.classname="Mesh"
If parent_ent Then mesh.AddParent(parent_ent)
mesh.entity_link = entity_list.EntityListAdd(mesh)
' update matrix
If mesh.parent<>Null
mesh.mat.Overwrite(mesh.parent.mat)
mesh.UpdateMat()
Else
mesh.UpdateMat(True)
Endif
Return mesh
End
Function LoadMesh:TMesh(file$,parent_ent:TEntity=Null, override_texflags:Int=-1)
Local xmesh:TMesh=LoadAnimMesh(file, Null, override_texflags)
Local mesh:TMesh = CreateMesh()
''
xmesh.HideEntity()
xmesh.CollapseAnimMesh(mesh)
xmesh.FreeEntity()
mesh.classname="Model"
mesh.name = file
mesh.AddParent(parent_ent)
'mesh.entity_link = entity_list.EntityListAdd(mesh)
' update matrix
If mesh.parent<>Null
mesh.mat.Overwrite(mesh.parent.mat)
mesh.UpdateMat()
Else
mesh.UpdateMat(True)
Endif
Return TMesh(mesh)
End
Function LoadAnimMesh:TMesh(file$,parent_ent:TEntity=Null, override_texflags:Int=-1)
Local mesh:TMesh
If file.EndsWith(".obj") Or file.EndsWith(".obj.txt")
mesh = TModelObj.LoadMesh(file, override_texflags)
Elseif file.EndsWith(".txt")
mesh = TModelB3D.LoadAnimB3D(file,parent_ent, override_texflags)
Elseif file.EndsWith(".b3d")
mesh = TModelB3D.LoadAnimB3DBin(file,parent_ent, override_texflags)
Else
Print "**Error: File type not recognized (use .txt, .obj, or .b3d)"
Endif
If Not mesh Then Return TMesh.CreateCube()
Return mesh
End
Method CreateSurfaceID:Int()
no_surfs=no_surfs+1 'putting this here may cause trouble
Return no_surfs-1
End
Method CreateSurface:TSurface(bru:TBrush=Null)
Local surf:TSurface=New TSurface
surf_list.AddLast(surf)
If bru<>Null
surf.brush=bru.Copy()
Endif
'' anim surf
surf.surf_id = CreateSurfaceID()
anim_surf = anim_surf.Resize(no_surfs)
' new mesh surface - update reset flags
reset_bounds=True
col_tree.reset_col_tree=True
Return surf
End
'' AddSurface()
'' -- helper for AddMesh()
Method AddSurface:TSurface(surf:TSurface)
surf_list.AddLast(surf)
'' anim surf
surf.surf_id = CreateSurfaceID()
anim_surf = anim_surf.Resize(no_surfs)
' new mesh surface - update reset flags
reset_bounds=True
col_tree.reset_col_tree=True
Return surf
End
'' CreateGrid(x,y,parent)
'' creates a plane of quads x segments by y segments each segment 1x1 unit large
'' can create either joined grid or separate grid (for mobile textures)
Function CreateGrid:TMesh(x_seg:Int,y_seg:Int, repeat_tex:Bool=False ,parent_ent:TEntity=Null)
Local mesh:TMesh=TMesh.CreateMesh(parent_ent)
Local surf:TSurface=mesh.CreateSurface()
Local yhalf# = y_seg*0.5
Local xhalf# = x_seg*0.5
Local txstep# = 1.0/x_seg
Local tystep# = -1.0/y_seg
Local texx:Float = 0.0, texy:Float = 1.0
Local v0:Int,v1:Int,v2:Int,v3:Int
Local pv2:Int[x_seg+1]
Local qv2:Int[x_seg+1]
If Not repeat_tex
For Local y:Float = -yhalf To yhalf-1.0 Step 1.0
v0= surf.AddVertex( -xhalf-0.5, 0, y-0.5)
v1= surf.AddVertex( -xhalf-0.5, 0, y+0.5)
For Local x:Float = -xhalf To xhalf-1.0 Step 1.0
If x<>-xhalf Then v1 = v2; v0 = v3
If y= -yhalf
v3= surf.AddVertex( x+0.5, 0, y-0.5)
Else
v3 = pv2[xhalf + x]
Endif
v2= surf.AddVertex( x+0.5, 0, y+0.5)
qv2[xhalf + x] = v2
surf.VertexNormal(v0,0.0,1.0,0.0)
surf.VertexNormal(v1,0.0,1.0,0.0)
surf.VertexNormal(v2,0.0,1.0,0.0)
surf.VertexNormal(v3,0.0,1.0,0.0)
surf.VertexTexCoords(v0,texx,texy)
surf.VertexTexCoords(v1,texx,texy+tystep)
surf.VertexTexCoords(v2,texx+txstep,texy+tystep)
surf.VertexTexCoords(v3,texx+txstep,texy)
surf.AddTriangle(v0,v1,v2)
surf.AddTriangle(v0,v2,v3)
'Print "tx "+texx+" "+texy+" "+(texx+txstep)+" "+(texy+tystep)+" :: "+txstep+" "+tystep
texx += txstep
If texx>1.0 Then texx=0.0
Next
For Local i:Int = 0 To x_seg
pv2[i] = qv2[i]
Next
qv2 = New Int[x_seg+1]
texx =0.0
texy += tystep
If texy<0.0 Then texy=1.0
Next
Else
''create segmented grid, separate squares for textures
For Local y:Float = -yhalf To yhalf-1.0 Step 1.0
For Local x:Float = -xhalf To xhalf-1.0 Step 1.0
v0= surf.AddVertex( x-0.5, 0, y-0.5)
v1= surf.AddVertex( x-0.5, 0, y+0.5)
v2= surf.AddVertex( x+0.5, 0, y+0.5)
v3= surf.AddVertex( x+0.5, 0, y-0.5)
surf.VertexNormal(v0,0.0,1.0,0.0)
surf.VertexNormal(v1,0.0,1.0,0.0)
surf.VertexNormal(v2,0.0,1.0,0.0)
surf.VertexNormal(v3,0.0,1.0,0.0)
surf.VertexTexCoords(v0,0.0,0.0)
surf.VertexTexCoords(v1,0.0,1.0)
surf.VertexTexCoords(v2,1.0,1.0)
surf.VertexTexCoords(v3,1.0,0.0)
surf.AddTriangle(v0,v1,v2)
surf.AddTriangle(v0,v2,v3)
Next
Next
Endif
surf.CropSurfaceBuffers()
'mesh.UpdateNormals()
mesh.classname = "MeshGrid"
Return mesh
End
Function CreateCube:TMesh(parent_ent:TEntity=Null)
Local mesh:TMesh=TMesh.CreateMesh(parent_ent)
Local surf:TSurface=mesh.CreateSurface()
surf.AddVertex(-1.0,-1.0,-1.0)
surf.AddVertex(-1.0, 1.0,-1.0)
surf.AddVertex( 1.0, 1.0,-1.0)
surf.AddVertex( 1.0,-1.0,-1.0)
surf.AddVertex(-1.0,-1.0, 1.0)
surf.AddVertex(-1.0, 1.0, 1.0)
surf.AddVertex( 1.0, 1.0, 1.0)
surf.AddVertex( 1.0,-1.0, 1.0)
surf.AddVertex(-1.0,-1.0, 1.0)
surf.AddVertex(-1.0, 1.0, 1.0)
surf.AddVertex( 1.0, 1.0, 1.0)
surf.AddVertex( 1.0,-1.0, 1.0)
surf.AddVertex(-1.0,-1.0,-1.0)
surf.AddVertex(-1.0, 1.0,-1.0)
surf.AddVertex( 1.0, 1.0,-1.0)
surf.AddVertex( 1.0,-1.0,-1.0)
surf.AddVertex(-1.0,-1.0, 1.0)
surf.AddVertex(-1.0, 1.0, 1.0)
surf.AddVertex( 1.0, 1.0, 1.0)
surf.AddVertex( 1.0,-1.0, 1.0)
surf.AddVertex(-1.0,-1.0,-1.0)
surf.AddVertex(-1.0, 1.0,-1.0)
surf.AddVertex( 1.0, 1.0,-1.0)
surf.AddVertex( 1.0,-1.0,-1.0)
surf.VertexNormal(0,0.0,0.0,-1.0)
surf.VertexNormal(1,0.0,0.0,-1.0)
surf.VertexNormal(2,0.0,0.0,-1.0)
surf.VertexNormal(3,0.0,0.0,-1.0)
surf.VertexNormal(4,0.0,0.0,1.0)
surf.VertexNormal(5,0.0,0.0,1.0)
surf.VertexNormal(6,0.0,0.0,1.0)
surf.VertexNormal(7,0.0,0.0,1.0)
surf.VertexNormal(8,0.0,-1.0,0.0)
surf.VertexNormal(9,0.0,1.0,0.0)
surf.VertexNormal(10,0.0,1.0,0.0)
surf.VertexNormal(11,0.0,-1.0,0.0)
surf.VertexNormal(12,0.0,-1.0,0.0)
surf.VertexNormal(13,0.0,1.0,0.0)
surf.VertexNormal(14,0.0,1.0,0.0)
surf.VertexNormal(15,0.0,-1.0,0.0)
surf.VertexNormal(16,-1.0,0.0,0.0)
surf.VertexNormal(17,-1.0,0.0,0.0)
surf.VertexNormal(18,1.0,0.0,0.0)
surf.VertexNormal(19,1.0,0.0,0.0)
surf.VertexNormal(20,-1.0,0.0,0.0)
surf.VertexNormal(21,-1.0,0.0,0.0)
surf.VertexNormal(22,1.0,0.0,0.0)
surf.VertexNormal(23,1.0,0.0,0.0)
surf.VertexTexCoords(0,0.0,1.0)
surf.VertexTexCoords(1,0.0,0.0)
surf.VertexTexCoords(2,1.0,0.0)
surf.VertexTexCoords(3,1.0,1.0)
surf.VertexTexCoords(4,1.0,1.0)
surf.VertexTexCoords(5,1.0,0.0)
surf.VertexTexCoords(6,0.0,0.0)
surf.VertexTexCoords(7,0.0,1.0)
surf.VertexTexCoords(8,0.0,1.0)
surf.VertexTexCoords(9,0.0,0.0)
surf.VertexTexCoords(10,1.0,0.0)
surf.VertexTexCoords(11,1.0,1.0)
surf.VertexTexCoords(12,0.0,0.0)
surf.VertexTexCoords(13,0.0,1.0)
surf.VertexTexCoords(14,1.0,1.0)
surf.VertexTexCoords(15,1.0,0.0)
surf.VertexTexCoords(16,0.0,1.0)
surf.VertexTexCoords(17,0.0,0.0)
surf.VertexTexCoords(18,1.0,0.0)
surf.VertexTexCoords(19,1.0,1.0)
surf.VertexTexCoords(20,1.0,1.0)
surf.VertexTexCoords(21,1.0,0.0)
surf.VertexTexCoords(22,0.0,0.0)
surf.VertexTexCoords(23,0.0,1.0)
surf.VertexTexCoords(0,0.0,1.0,0.0,1)
surf.VertexTexCoords(1,0.0,0.0,0.0,1)
surf.VertexTexCoords(2,1.0,0.0,0.0,1)
surf.VertexTexCoords(3,1.0,1.0,0.0,1)
surf.VertexTexCoords(4,1.0,1.0,0.0,1)
surf.VertexTexCoords(5,1.0,0.0,0.0,1)
surf.VertexTexCoords(6,0.0,0.0,0.0,1)
surf.VertexTexCoords(7,0.0,1.0,0.0,1)
surf.VertexTexCoords(8,0.0,1.0,0.0,1)
surf.VertexTexCoords(9,0.0,0.0,0.0,1)
surf.VertexTexCoords(10,1.0,0.0,0.0,1)
surf.VertexTexCoords(11,1.0,1.0,0.0,1)
surf.VertexTexCoords(12,0.0,0.0,0.0,1)
surf.VertexTexCoords(13,0.0,1.0,0.0,1)
surf.VertexTexCoords(14,1.0,1.0,0.0,1)
surf.VertexTexCoords(15,1.0,0.0,0.0,1)
surf.VertexTexCoords(16,0.0,1.0,0.0,1)
surf.VertexTexCoords(17,0.0,0.0,0.0,1)
surf.VertexTexCoords(18,1.0,0.0,0.0,1)
surf.VertexTexCoords(19,1.0,1.0,0.0,1)
surf.VertexTexCoords(20,1.0,1.0,0.0,1)
surf.VertexTexCoords(21,1.0,0.0,0.0,1)
surf.VertexTexCoords(22,0.0,0.0,0.0,1)
surf.VertexTexCoords(23,0.0,1.0,0.0,1)
surf.AddTriangle(0,1,2) ' front
surf.AddTriangle(0,2,3)
surf.AddTriangle(6,5,4) ' back
surf.AddTriangle(7,6,4)
surf.AddTriangle(6+8,5+8,1+8) ' top
surf.AddTriangle(2+8,6+8,1+8)
surf.AddTriangle(0+8,4+8,7+8) ' bottom
surf.AddTriangle(0+8,7+8,3+8)
surf.AddTriangle(6+16,2+16,3+16) ' right
surf.AddTriangle(7+16,6+16,3+16)
surf.AddTriangle(0+16,1+16,5+16) ' left
surf.AddTriangle(0+16,5+16,4+16)
surf.CropSurfaceBuffers()
mesh.classname = "MeshCube"
Return mesh
End
' Function by Coyote
Function CreateSphere:TMesh(segments:Int=8,parent_ent:TEntity=Null)
If segments<2 Or segments>100 Then Return Null
Local thissphere:TMesh=TMesh.CreateMesh(parent_ent)
Local thissurf:TSurface=thissphere.CreateSurface()
Local div#=Float(360.0/(segments*2))
Local height#=1.0
Local upos#=1.0
Local udiv#=Float(1.0/(segments*2))
Local vdiv#=Float(1.0/segments)
Local RotAngle#=90
If segments=2 ' diamond shape - no center strips
For Local i=1 To (segments*2)
Local np=thissurf.AddVertex(0.0,height,0.0,upos-(udiv/2.0),0)'northpole
Local sp=thissurf.AddVertex(0.0,-height,0.0,upos-(udiv/2.0),1)'southpole
Local XPos#=-Cos(RotAngle)
Local ZPos#=Sin(RotAngle)
Local v0=thissurf.AddVertex(XPos,0,ZPos,upos,0.5)
RotAngle=RotAngle+div
If RotAngle>=360.0 Then RotAngle=RotAngle-360.0
XPos=-Cos(RotAngle)
ZPos=Sin(RotAngle)
upos=upos-udiv
Local v1=thissurf.AddVertex(XPos,0,ZPos,upos,0.5)
thissurf.AddTriangle(np,v0,v1)
thissurf.AddTriangle(v1,v0,sp)
Next
Else ' have center strips now
' poles first
Local YPos#=Cos(div)
For Local i=1 To (segments*2)
Local np=thissurf.AddVertex(0.0,height,0.0,upos-(udiv/2.0),0.0)'northpole
Local sp=thissurf.AddVertex(0.0,-height,0.0,upos-(udiv/2.0),1.0)'southpole
Local XPos#=-Cos(RotAngle)*(Sin(div))
Local ZPos#=Sin(RotAngle)*(Sin(div))
Local v0t=thissurf.AddVertex(XPos,YPos,ZPos,upos,vdiv)
Local v0b=thissurf.AddVertex(XPos,-YPos,ZPos,upos,1.0-vdiv)
RotAngle=RotAngle+div
XPos=-Cos(RotAngle)*(Sin(div))
ZPos=Sin(RotAngle)*(Sin(div))
upos=upos-udiv
Local v1t=thissurf.AddVertex(XPos,YPos,ZPos,upos,vdiv)
Local v1b=thissurf.AddVertex(XPos,-YPos,ZPos,upos,1.0-vdiv)
thissurf.AddTriangle(np,v0t,v1t)
thissurf.AddTriangle(v1b,v0b,sp)
Next
' then center strips
upos=1.0
RotAngle=90
For Local i=1 To (segments*2)
Local mult#=1
Local YPos#=Cos(div*(mult))
Local YPos2#=Cos(div*(mult+1.0))
Local Thisvdiv#=vdiv
For Local j=1 To (segments-2)
Local XPos#=-Cos(RotAngle)*(Sin(div*(mult)))
Local ZPos#=Sin(RotAngle)*(Sin(div*(mult)))
Local XPos2#=-Cos(RotAngle)*(Sin(div*(mult+1.0)))
Local ZPos2#=Sin(RotAngle)*(Sin(div*(mult+1.0)))
Local v0t=thissurf.AddVertex(XPos,YPos,ZPos,upos,Thisvdiv)
Local v0b=thissurf.AddVertex(XPos2,YPos2,ZPos2,upos,Thisvdiv+vdiv)
' 2nd tex coord set
thissurf.VertexTexCoords(v0t,upos,Thisvdiv,0.0,1)
thissurf.VertexTexCoords(v0b,upos,Thisvdiv+vdiv,0.0,1)
Local tempRotAngle#=RotAngle+div
Local sdm# = Sin(div*(mult))
XPos=-Cos(tempRotAngle)*(sdm)
ZPos=Sin(tempRotAngle)*(sdm)
sdm = Sin(div*(mult+1.0))
XPos2=-Cos(tempRotAngle)*(sdm)
ZPos2=Sin(tempRotAngle)*(sdm)
Local temp_upos#=upos-udiv
Local v1t=thissurf.AddVertex(XPos,YPos,ZPos,temp_upos,Thisvdiv)
Local v1b=thissurf.AddVertex(XPos2,YPos2,ZPos2,temp_upos,Thisvdiv+vdiv)
' 2nd tex coord set
thissurf.VertexTexCoords(v1t,temp_upos,Thisvdiv,0.0,1)
thissurf.VertexTexCoords(v1b,temp_upos,Thisvdiv+vdiv,0.0,1)
thissurf.AddTriangle(v1t,v0t,v0b)
thissurf.AddTriangle(v1b,v1t,v0b)
Thisvdiv=Thisvdiv+vdiv
mult=mult+1
YPos=Cos(div*(mult))
YPos2=Cos(div*(mult+1.0))
Next
upos=upos-udiv
RotAngle=RotAngle+div
Next
Endif
thissurf.CropSurfaceBuffers()
'thissphere.UpdateNormals() ''slow
thissurf.SnapVerts()
thissurf.UpdateNormals()
' mesh state has changed - update reset flags
thissurf.reset_vbo = thissurf.reset_vbo|4
thissphere.classname = "MeshSphere"
Return thissphere
End
' Function by Coyote
Function CreateCylinder:TMesh(verticalsegments=8,solid=True,parent_ent:TEntity=Null)
Local ringsegments=0 ' default?
Local tr,tl,br,bl' side of cylinder
Local ts0,ts1,newts' top side vertexs
Local bs0,bs1,newbs' bottom side vertexs
If verticalsegments<3 Or verticalsegments>100 Then Return Null
If ringsegments<0 Or ringsegments>100 Then Return Null
Local thiscylinder:TMesh=TMesh.CreateMesh(parent_ent)
Local thissurf:TSurface=thiscylinder.CreateSurface()
Local thissidesurf:TSurface
If solid=True
thissidesurf=thiscylinder.CreateSurface()
Endif
Local div#=Float(360.0/(verticalsegments))
Local height#=1.0
Local ringSegmentHeight#=(height*2.0)/(ringsegments+1)
Local upos#=1.0
Local udiv#=Float(1.0/(verticalsegments))
Local vpos#=1.0
Local vdiv#=Float(1.0/(ringsegments+1))
Local SideRotAngle#=90
' re-diminsion arrays to hold needed memory.
' this is used just for helping to build the ring segments...
Local tRing[verticalsegments+1]
Local bRing[verticalsegments+1]
' render end caps if solid
If solid=True
Local XPos#=-Cos(SideRotAngle)
Local ZPos#=Sin(SideRotAngle)
ts0=thissidesurf.AddVertex(XPos,height,ZPos,XPos/2.0+0.5,ZPos/2.0+0.5)
bs0=thissidesurf.AddVertex(XPos,-height,ZPos,XPos/2.0+0.5,ZPos/2.0+0.5)
' 2nd tex coord set
thissidesurf.VertexTexCoords(ts0,XPos/2.0+0.5,ZPos/2.0+0.5,0.0,1)
thissidesurf.VertexTexCoords(bs0,XPos/2.0+0.5,ZPos/2.0+0.5,0.0,1)
SideRotAngle=SideRotAngle+div
XPos=-Cos(SideRotAngle)
ZPos=Sin(SideRotAngle)
ts1=thissidesurf.AddVertex(XPos,height,ZPos,XPos/2.0+0.5,ZPos/2.0+0.5)
bs1=thissidesurf.AddVertex(XPos,-height,ZPos,XPos/2.0+0.5,ZPos/2.0+0.5)
' 2nd tex coord set
thissidesurf.VertexTexCoords(ts1,XPos/2.0+0.5,ZPos/2.0+0.5,0.0,1)
thissidesurf.VertexTexCoords(bs1,XPos/2.0+0.5,ZPos/2.0+0.5,0.0,1)
For Local i=1 To (verticalsegments-2)
SideRotAngle=SideRotAngle+div
XPos=-Cos(SideRotAngle)
ZPos=Sin(SideRotAngle)
newts=thissidesurf.AddVertex(XPos,height,ZPos,XPos/2.0+0.5,ZPos/2.0+0.5)
newbs=thissidesurf.AddVertex(XPos,-height,ZPos,XPos/2.0+0.5,ZPos/2.0+0.5)
' 2nd tex coord set
thissidesurf.VertexTexCoords(newts,XPos/2.0+0.5,ZPos/2.0+0.5,0.0,1)
thissidesurf.VertexTexCoords(newbs,XPos/2.0+0.5,ZPos/2.0+0.5,0.0,1)
thissidesurf.AddTriangle(ts0,ts1,newts)
thissidesurf.AddTriangle(newbs,bs1,bs0)
If i<(verticalsegments-2)
ts1=newts
bs1=newbs
Endif
Next
Endif
' -----------------------
' middle part of cylinder
Local thisHeight#=height
' top ring first
SideRotAngle=90
Local XPos#=-Cos(SideRotAngle)
Local ZPos#=Sin(SideRotAngle)
Local thisUPos#=upos
Local thisVPos#=0
tRing[0]=thissurf.AddVertex(XPos,thisHeight,ZPos,thisUPos,thisVPos)
thissurf.VertexTexCoords(tRing[0],thisUPos,thisVPos,0.0,1) ' 2nd tex coord set
For Local i=0 To (verticalsegments-1)
SideRotAngle=SideRotAngle+div
XPos=-Cos(SideRotAngle)
ZPos=Sin(SideRotAngle)
thisUPos=thisUPos-udiv
tRing[i+1]=thissurf.AddVertex(XPos,thisHeight,ZPos,thisUPos,thisVPos)
thissurf.VertexTexCoords(tRing[i+1],thisUPos,thisVPos,0.0,1) ' 2nd tex coord set
Next
For Local ring=0 To ringsegments
' decrement vertical segment
Local thisHeight=thisHeight-ringSegmentHeight
' now bottom ring
SideRotAngle=90
XPos=-Cos(SideRotAngle)
ZPos=Sin(SideRotAngle)
thisUPos=upos
thisVPos=thisVPos+vdiv
bRing[0]=thissurf.AddVertex(XPos,thisHeight,ZPos,thisUPos,thisVPos)
thissurf.VertexTexCoords(bRing[0],thisUPos,thisVPos,0.0,1) ' 2nd tex coord set
For Local i=0 To (verticalsegments-1)
SideRotAngle=SideRotAngle+div
XPos=-Cos(SideRotAngle)
ZPos=Sin(SideRotAngle)
thisUPos=thisUPos-udiv
bRing[i+1]=thissurf.AddVertex(XPos,thisHeight,ZPos,thisUPos,thisVPos)
thissurf.VertexTexCoords(bRing[i+1],thisUPos,thisVPos,0.0,1) ' 2nd tex coord set
Next
' Fill in ring segment sides with triangles
For Local v=1 To (verticalsegments)
tl=tRing[v]
tr=tRing[v-1]
bl=bRing[v]
br=bRing[v-1]
thissurf.AddTriangle(tl,tr,br)
thissurf.AddTriangle(bl,tl,br)
Next
' make bottom ring segmentthe top ring segment for the next loop.
For Local v=0 To (verticalsegments)
tRing[v]=bRing[v]
Next
Next
thissurf.CropSurfaceBuffers()
thiscylinder.UpdateNormals()
thiscylinder.classname = "MeshCylinder"
Return thiscylinder
End
' Function by Coyote
Function CreateCone:TMesh(segments=8,solid=True,parent_ent:TEntity=Null)
Local top,br,bl' side of cone
Local bs0,bs1,newbs' bottom side vertices
If segments<3 Or segments>100 Then Return Null
Local thiscone:TMesh=TMesh.CreateMesh(parent_ent)
Local thissurf:TSurface=thiscone.CreateSurface()
Local thissidesurf:TSurface
If solid=True
thissidesurf=thiscone.CreateSurface()
Endif
Local div#=Float(360.0/(segments))
Local height#=1.0
Local upos#=1.0
Local udiv#=Float(1.0/(segments))
Local RotAngle#=90
' first side
Local XPos#=-Cos(RotAngle)
Local ZPos#=Sin(RotAngle)
top=thissurf.AddVertex(0.0,height,0.0,upos-(udiv/2.0),0)
br=thissurf.AddVertex(XPos,-height,ZPos,upos,1)
' 2nd tex coord set
thissurf.VertexTexCoords(top,upos-(udiv/2.0),0,0.0,1)
thissurf.VertexTexCoords(br,upos,1,0.0,1)
If solid=True Then bs0=thissidesurf.AddVertex(XPos,-height,ZPos,XPos/2.0+0.5,ZPos/2.0+0.5)
If solid=True Then thissidesurf.VertexTexCoords(bs0,XPos/2.0+0.5,ZPos/2.0+0.5,0.0,1) ' 2nd tex coord set
RotAngle= RotAngle+div
XPos= -Cos(RotAngle)
ZPos= Sin(RotAngle)
bl=thissurf.AddVertex(XPos,-height,ZPos,upos-udiv,1)
thissurf.VertexTexCoords(bl,upos-udiv,1,0.0,1) ' 2nd tex coord set
If solid=True Then bs1=thissidesurf.AddVertex(XPos,-height,ZPos,XPos/2.0+0.5,ZPos/2.0+0.5)
If solid=True Then thissidesurf.VertexTexCoords(bs1,XPos/2.0+0.5,ZPos/2.0+0.5,0.0,1) ' 2nd tex coord set
thissurf.AddTriangle(bl,top,br)
' rest of sides
For Local i=1 To (segments-1)
br=bl
upos=upos-udiv
top=thissurf.AddVertex(0.0,height,0.0,upos-(udiv/2.0),0)
thissurf.VertexTexCoords(top,upos-(udiv/2.0),0,0.0,1) ' 2nd tex coord set
RotAngle=RotAngle+div
XPos=-Cos(RotAngle)
ZPos=Sin(RotAngle)
bl=thissurf.AddVertex(XPos,-height,ZPos,upos-udiv,1)
thissurf.VertexTexCoords(bl,upos-udiv,1,0.0,1) ' 2nd tex coord set
If solid=True Then newbs=thissidesurf.AddVertex(XPos,-height,ZPos,XPos/2.0+0.5,ZPos/2.0+0.5)
If solid=True Then thissidesurf.VertexTexCoords(newbs,XPos/2.0+0.5,ZPos/2.0+0.5,0.0,1) ' 2nd tex coord set
thissurf.AddTriangle(bl,top,br)
If solid=True
thissidesurf.AddTriangle(newbs,bs1,bs0)'AddTriangle(newbs,bs1,bs0)
If i<(segments-1)
bs1=newbs
Endif
Endif
Next
thissurf.CropSurfaceBuffers()
thissidesurf.CropSurfaceBuffers()
thiscone.UpdateNormals()
thiscone.classname = "MeshCone"
Return thiscone
End
Function CreateLine:TMesh(v1:Vector, v2:Vector, thick:Float=1.0, parent_ent:TEntity=Null)
Return CreateLine(v1.x,v1.y,v1.z,v2.x,v2.y,v2.z,thick,parent_ent)
End
Function CreateLine:TMesh(v1:Vector, v2:Vector, r:Int,g:Int,b:Int)
Local m:TMesh = CreateLine(v1.x,v1.y,v1.z,v2.x,v2.y,v2.z)
m.EntityColor(r,g,b)
Return m
End
Function CreateLine:TMesh(x:Float, y:Float, z:Float, x2:Float, y2:Float, z2:Float, thick:Float=1.0, parent_ent:TEntity=Null)
Local mesh:TMesh=TMesh.CreateMesh(parent_ent)
Local surf:TSurface=mesh.CreateSurface()
surf.AddVertex(x,y,z)
surf.AddVertex(x2,y2,z2)
surf.AddVertex(x,y,z)
surf.AddTriangle(0,1,2)
surf.CropSurfaceBuffers()
mesh.classname = "Line"
mesh.wireframe = True
Return mesh
End
Method CopyMesh:TMesh(parent_ent:TEntity=Null)
Local mesh:TMesh=TMesh.CreateMesh(parent_ent)
Self.AddMesh(mesh) ''add self TO mesh
''copy children
For Local ent:TEntity=Eachin child_list
ent.CopyEntity(mesh)
Next
mesh.name=name
mesh.classname=classname
mesh.order=order
mesh.hide=hide
mesh.auto_fade=auto_fade
mesh.fade_near=fade_near
mesh.fade_far=fade_far
mesh.use_cam_layer = use_cam_layer
mesh.cam_layer = cam_layer
mesh.anim=anim
mesh.anim_render=anim_render
mesh.anim_mode=anim_mode
mesh.anim_time=anim_time
mesh.anim_speed=anim_speed
mesh.anim_seq=anim_seq
mesh.anim_trans=anim_trans
mesh.anim_dir=anim_dir
mesh.anim_seqs_first=anim_seqs_first[..]
mesh.anim_seqs_last=anim_seqs_last[..]
mesh.no_seqs=no_seqs