-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathUtil3d.cs
More file actions
1030 lines (810 loc) · 36.2 KB
/
Copy pathUtil3d.cs
File metadata and controls
1030 lines (810 loc) · 36.2 KB
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
//Copyright © 2014 Sony Computer Entertainment America LLC. See License.txt.
using System;
using System.Collections.Generic;
using System.Drawing;
using Sce.Atf.VectorMath;
using Vector2 = Sce.Atf.VectorMath.Vec2F;
using Vector3 = Sce.Atf.VectorMath.Vec3F;
using Vector4 = Sce.Atf.VectorMath.Vec4F;
using LevelEditorCore.VectorMath;
namespace RenderingInterop
{
// utility class for drawing simple 2d/3d shapes
public static class Util3D
{
public static BasicRendererFlags RenderFlag
{
set { GameEngine.SetRendererFlag(value);}
}
public static void DrawSphere(Sphere3F sphere, Color c)
{
// create matrix from AABB
T1.Set(sphere.Center);
float scale = 2*sphere.Radius;
T1.M11 = scale;
T1.M22 = scale;
T1.M33 = scale;
DrawSphere(T1, c);
}
public static ulong CaptionFont
{
get { return s_captionFont; }
}
public static void DrawPivot(Matrix4F xform, Color c)
{
GameEngine.DrawPrimitive(PrimitiveType.LineStrip,
s_pivotVerts,
0,
s_pivotVertexCount,
c,
xform);
}
public static void DrawAABB(AABB bound)
{
// create matrix from AABB
Vec3F trans = bound.Center;
Vec3F diag = bound.Max - bound.Min;
T1.Set(trans);
T1.M11 = diag.X;
T1.M22 = diag.Y;
T1.M33 = diag.Z;
GameEngine.DrawIndexedPrimitive(PrimitiveType.LineList,
s_boxVertsId,
s_boxIndicesId,
0,
s_boxIndicesCount,
0,
Color.White,
T1);
}
public static void DrawCircle(Matrix4F xform, Color color)
{
GameEngine.DrawPrimitive(PrimitiveType.LineStrip,
s_circleVerts,
0,
s_circleVertexCount,
color,
xform);
}
public static void DrawUnitSquare(Matrix4F xform, Color color)
{
GameEngine.DrawPrimitive(PrimitiveType.LineList,
s_linesVertId,
s_unitSquareStartVertex,
s_unitSquareVertexCount,
color,
xform);
}
public static void DrawRect(Matrix4F xform, Color color)
{
GameEngine.DrawPrimitive(PrimitiveType.LineList,
s_linesVertId,
s_rectStartVertex,
s_rectVertexCount,
color,
xform);
}
public static void DrawAxis(Matrix4F xform, Color color)
{
GameEngine.DrawPrimitive(PrimitiveType.LineList,
s_linesVertId,
s_axisStartVertex,
s_axisVertexCount,
color,
xform);
}
public static void DrawX(Matrix4F xform, Color color)
{
GameEngine.DrawPrimitive(PrimitiveType.LineList,
s_linesVertId,
s_axisStartVertex,
2,
color,
xform);
}
public static void DrawY(Matrix4F xform, Color color)
{
GameEngine.DrawPrimitive(PrimitiveType.LineList,
s_linesVertId,
s_axisStartVertex + 2,
2,
color,
xform);
}
public static void DrawZ(Matrix4F xform, Color color)
{
GameEngine.DrawPrimitive(PrimitiveType.LineList,
s_linesVertId,
s_axisStartVertex + 4,
2,
color,
xform);
}
public static void DrawSphere(Matrix4F xform, System.Drawing.Color color)
{
GameEngine.DrawIndexedPrimitive(PrimitiveType.TriangleList,
s_sphereVertId,
s_sphereIndexId,
0,
s_sphereIndexCount,
0,
color,
xform);
}
// few constants used for creating Ring (a thin torus).
public const float RingInnerRadias = 0.47f;
public const float RingOuterRadias = 0.5f;
public const float RingThickness = (RingOuterRadias - RingInnerRadias);
public const float RingCenterRadias = 0.5f * (RingInnerRadias + RingOuterRadias);
public static void DrawRing(Matrix4F xform, Color color)
{
GameEngine.DrawIndexedPrimitive(PrimitiveType.TriangleList,
s_torusVertId,
s_torusIndexId,
0,
s_torusIndexCount,
0,
color,
xform);
}
public static void DrawCylinder(Matrix4F xform, Color color)
{
GameEngine.DrawIndexedPrimitive(PrimitiveType.TriangleList,
s_cylinderVertId,
s_cylinderIndexId,
0,
s_cylinderIndexCount,
0,
color,
xform);
}
public static void DrawCone(Matrix4F xform, Color color)
{
GameEngine.DrawIndexedPrimitive(PrimitiveType.TriangleList,
s_coneVertId,
s_coneIndexId,
0,
s_coneIndexCount,
0,
color,
xform);
}
public static void DrawCube(Matrix4F xform, Color color)
{
GameEngine.DrawIndexedPrimitive(PrimitiveType.TriangleList,
s_cubeVertId,
s_cubeIndexId,
0,
s_cubeIndexCount,
0,
color,
xform);
}
public static void Init()
{
if (s_inited) return;
List<VertexPN> vertList = new List<VertexPN>();
List<Vector3> spherePositions = new List<Vec3F>();
List<Vector3> sphereNormals = new List<Vec3F>();
List<uint> sphereIndices = new List<uint>();
GeometryHelper.CreateSphere(0.5f,FastSphereSlices,FastSphereStacks, spherePositions
, sphereNormals, null, sphereIndices);
GeometryHelper.AssembleVertexPN(spherePositions, sphereNormals, vertList);
s_sphereVertId = GameEngine.CreateVertexBuffer(vertList.ToArray());
s_sphereIndexId = GameEngine.CreateIndexBuffer(sphereIndices.ToArray());
s_sphereIndexCount = (uint) sphereIndices.Count;
List<Vec3F> conePos = new List<Vec3F>();
List<uint> coneIndices = new List<uint>();
List<Vec3F> coneNorms = new List<Vec3F>();
GeometryHelper.CreateCylinder(1.0f, 0.0f, 1.0f, 16, 1, conePos, coneNorms, coneIndices);
s_coneIndexCount = (uint)coneIndices.Count;
GeometryHelper.AssembleVertexPN(conePos, coneNorms, vertList);
s_coneVertId = GameEngine.CreateVertexBuffer(vertList.ToArray());
s_coneIndexId = GameEngine.CreateIndexBuffer(coneIndices.ToArray());
// create unit cylinder
List<Vec3F> cyPos = new List<Vec3F>();
List<uint> cyIndices = new List<uint>();
List<Vec3F> cyNorms = new List<Vec3F>();
GeometryHelper.CreateCylinder(0.5f, 0.5f, 1.0f, 16, 2, cyPos, cyNorms, cyIndices);
GeometryHelper.AssembleVertexPN(cyPos, cyNorms, vertList);
s_cylinderVertId = GameEngine.CreateVertexBuffer(vertList.ToArray());
s_cylinderIndexId = GameEngine.CreateIndexBuffer(cyIndices.ToArray());
s_cylinderIndexCount =(uint)cyIndices.Count;
// create slim unit torus.
List<Vec3F> torPos = new List<Vec3F>();
List<Vec3F> torNorms = new List<Vec3F>();
List<uint> torIndices = new List<uint>();
GeometryHelper.CreateTorus(RingInnerRadias, RingOuterRadias, 40, 6, torPos, torNorms, null, torIndices);
GeometryHelper.AssembleVertexPN(torPos, torNorms, vertList);
s_torusVertId = GameEngine.CreateVertexBuffer(vertList.ToArray());
s_torusIndexId = GameEngine.CreateIndexBuffer(torIndices.ToArray());
s_torusIndexCount = (uint)torIndices.Count;
List<Vector3> cubePos = new List<Vector3>();
List<Vector3> cubeNormals = new List<Vector3>();
List<uint> cubeIndices = new List<uint>();
GeometryHelper.CreateUnitCube(cubePos,cubeNormals,null,cubeIndices);
GeometryHelper.AssembleVertexPN(cubePos, cubeNormals, vertList);
s_cubeVertId = GameEngine.CreateVertexBuffer(vertList.ToArray());
s_cubeIndexId = GameEngine.CreateIndexBuffer(cubeIndices.ToArray());
s_cubeIndexCount = (uint) cubeIndices.Count;
var linePos = new List<Vec3F>();
linePos.Add(new Vector3(0, 0, 0));
linePos.Add(new Vector3(1, 0, 0));
linePos.Add(new Vector3(0, 0, 0));
linePos.Add(new Vector3(0, 1, 0));
linePos.Add(new Vector3(0, 0, 0));
linePos.Add(new Vector3(0, 0, 1));
linePos.Add(new Vector3(0, 0, 0));
linePos.Add(new Vector3(-1, 0, 0));
linePos.Add(new Vector3(0, 0, 0));
linePos.Add(new Vector3(0, -1, 0));
linePos.Add(new Vector3(0, 0, 0));
linePos.Add(new Vector3(0, 0, -1));
s_axisStartVertex = 0;
s_axisVertexCount = (uint)linePos.Count;
// rect pos;
linePos.Add(new Vector3(1, 1, 0));
linePos.Add(new Vector3(-1, 1, 0));
linePos.Add(new Vector3(-1, 1, 0));
linePos.Add(new Vector3(-1, -1, 0));
linePos.Add(new Vector3(-1, -1, 0));
linePos.Add(new Vector3(1, -1, 0));
linePos.Add(new Vector3(1, -1, 0));
linePos.Add(new Vector3(1, 1, 0));
s_rectVertexCount = 8;
s_rectStartVertex = (uint)linePos.Count - s_rectVertexCount;
// unit square
linePos.Add(new Vector3(0.5f, 0.5f, 0));
linePos.Add(new Vector3(-0.5f, 0.5f, 0));
linePos.Add(new Vector3(-0.5f, 0.5f, 0));
linePos.Add(new Vector3(-0.5f, -0.5f, 0));
linePos.Add(new Vector3(-0.5f, -0.5f, 0));
linePos.Add(new Vector3(0.5f, -0.5f, 0));
linePos.Add(new Vector3(0.5f, -0.5f, 0));
linePos.Add(new Vector3(0.5f, 0.5f, 0));
s_unitSquareVertexCount = 8;
s_unitSquareStartVertex = (uint) linePos.Count - s_unitSquareVertexCount;
s_linesVertId = GameEngine.CreateVertexBuffer(linePos.ToArray());
List<Vec3F> circlePos = new List<Vec3F>();
GeometryHelper.CreateCircle(1.0f,32,circlePos);
s_circleVertexCount = (uint) circlePos.Count;
s_circleVerts = GameEngine.CreateVertexBuffer(circlePos.ToArray());
List<Vec3F> boxVerts = new List<Vec3F>();
List<uint> boxIndices = new List<uint>();
GeometryHelper.CreateUnitBox(boxVerts,boxIndices);
s_boxVertsId = GameEngine.CreateVertexBuffer(boxVerts.ToArray());
s_boxIndicesId = GameEngine.CreateIndexBuffer(boxIndices.ToArray());
s_boxIndicesCount =(uint)boxIndices.Count;
List<Vec3F> pivotVerts = new List<Vec3F>();
GeometryHelper.CreateCircle(0.5f, 16, pivotVerts);
GeometryHelper.CreateCircle(0.375f, 16, pivotVerts);
GeometryHelper.CreateCircle(0.25f, 16, pivotVerts);
GeometryHelper.CreateCircle(0.125f, 16, pivotVerts);
s_pivotVerts = GameEngine.CreateVertexBuffer(pivotVerts.ToArray());
s_pivotVertexCount = (uint)pivotVerts.Count;
s_captionFont = GameEngine.CreateFont("Arial", 14, FontStyle.BOLD);
s_inited = true;
}
public static void Shutdown()
{
GameEngine.DeleteBuffer(s_torusVertId);
GameEngine.DeleteBuffer(s_torusIndexId);
GameEngine.DeleteBuffer(s_cylinderVertId );
GameEngine.DeleteBuffer(s_cylinderIndexId);
GameEngine.DeleteBuffer(s_circleVerts);
GameEngine.DeleteBuffer(s_sphereVertId);
GameEngine.DeleteBuffer(s_sphereIndexId);
GameEngine.DeleteBuffer(s_coneVertId);
GameEngine.DeleteBuffer(s_coneIndexId);
GameEngine.DeleteBuffer(s_cubeVertId);
GameEngine.DeleteBuffer(s_cubeIndexId);
GameEngine.DeleteBuffer(s_linesVertId);
GameEngine.DeleteBuffer(s_boxVertsId);
GameEngine.DeleteBuffer(s_boxIndicesId);
GameEngine.DeleteBuffer(s_pivotVerts);
GameEngine.DeleteFont(s_captionFont);
}
private static bool s_inited;
private static ulong s_circleVerts;
private static uint s_circleVertexCount;
private static ulong s_pivotVerts;
private static uint s_pivotVertexCount;
private static ulong s_sphereVertId;
private static ulong s_sphereIndexId;
private static uint s_sphereIndexCount;
private static ulong s_torusVertId;
private static ulong s_torusIndexId;
private static uint s_torusIndexCount;
private static ulong s_coneVertId;
private static ulong s_coneIndexId;
private static uint s_coneIndexCount;
private static ulong s_cylinderVertId;
private static ulong s_cylinderIndexId;
private static uint s_cylinderIndexCount;
private static ulong s_boxVertsId;
private static ulong s_boxIndicesId;
private static uint s_boxIndicesCount;
private static ulong s_cubeVertId;
private static ulong s_cubeIndexId;
private static uint s_cubeIndexCount;
private static ulong s_linesVertId;
private static uint s_axisStartVertex;
private static uint s_axisVertexCount;
private static uint s_rectStartVertex;
private static uint s_rectVertexCount;
private static uint s_unitSquareStartVertex;
private static uint s_unitSquareVertexCount;
private const int FastSphereSlices = 16;
private const int FastSphereStacks = 16;
private static ulong s_captionFont;
// few temp matrices.
// this no longer be needed if/when
// Matrix4F converted to struct.
private static Matrix4F T1 = new Matrix4F();
// private static Matrix4F T2 = new Matrix4F();
// private static Matrix4F T3 = new Matrix4F();
// private static Matrix4F T4 = new Matrix4F();
}
public static class GeometryHelper
{
public static void AssembleVertexPN(List<Vec3F> pos, List<Vec3F> norm,
List<VertexPN> outVertList)
{
if (pos.Count != norm.Count)
throw new ArgumentException("pos count not equal norm count");
outVertList.Clear();
VertexPN vertex = new VertexPN();
for(int k = 0; k < pos.Count; k++)
{
vertex.Position = pos[k];
vertex.Normal = norm[k];
outVertList.Add(vertex);
}
}
/// <summary>
/// Create circle from linestrip.</summary>
public static void CreateCircle(float radius, uint segs, List<Vector3> pos)
{
float step = MathHelper.TwoPi/(float)segs;
float theta = 0;
for (int i = 0; i < segs; i++,theta+=step)
{
float x = radius*(float) Math.Cos(theta);
float y = radius*(float) Math.Sin(theta);
pos.Add(new Vector3(x,y,0));
}
pos.Add(pos[0]); // close the loop.
}
/// <summary>
/// Create bi-polar sphere
/// </summary>
/// <param name="radius">radias of the sphere</param>
/// <param name="slices">number of slices</param>
/// <param name="stacks">number of stacks</param>
/// <param name="pos">output positions</param>
/// <param name="normal">output normals</param>
/// <param name="tex">output texture coordinates</param>
/// <param name="indices">output indices</param>
public static void CreateSphere(float radius, uint slices, uint stacks,
List<Vector3> pos, List<Vector3> normal, List<Vector2> tex, List<uint> indices)
{
if (radius <= 0)
throw new ArgumentOutOfRangeException("radius");
if (slices < 2 || stacks < 2)
throw new ArgumentException("invalid number slices or stacks");
// caches sin cos.
float[] cosPhi = new float[stacks];
float[] sinPhi = new float[stacks];
float phiStep = MathHelper.Pi / stacks;
float[] cosTheta = new float[slices];
float[] sinTheta = new float[slices];
float thetaStep = MathHelper.TwoPi / slices;
float phi = 0;
for (int s = 0; s < stacks; s++, phi += phiStep)
{
sinPhi[s] = (float)Math.Sin(phi);
cosPhi[s] = (float)Math.Cos(phi);
}
float theta = 0;
for (int s = 0; s < slices; s++, theta += thetaStep)
{
sinTheta[s] = (float)Math.Sin(theta);
cosTheta[s] = (float)Math.Cos(theta);
}
uint numVerts = 2 + (stacks - 1) * slices;
Vector3 northPole = new Vector3(0, radius, 0);
pos.Add(northPole); // north pole.
normal.Add(Vector3.Normalize(northPole)); // north pole.
for (int s = 1; s < stacks; s++)
{
float y = radius * cosPhi[s];
float r = radius * sinPhi[s];
for (int l = 0; l < slices; l++)
{
Vector3 p;
p.Y = y;
p.Z = r * cosTheta[l];
p.X = r * sinTheta[l];
pos.Add(p);
if(normal != null)
normal.Add(Vector3.Normalize(p));
}
}
Vector3 southPole = new Vector3(0, -radius, 0);
pos.Add(southPole);
normal.Add(Vector3.Normalize(southPole));
// 2l + (s-2) * 2l
// 2l * ( 1 + s-2)
// 2l * ( s- 1)
//
uint numTris = 2 * slices * (stacks - 1);
// create index of north pole cap.
for (uint l = 1; l < slices; l++)
{
indices.Add(l + 1);
indices.Add(0);
indices.Add(l);
}
indices.Add(1);
indices.Add(0);
indices.Add(slices);
for (uint s = 0; s < (stacks - 2); s++)
{
uint l = 1;
for (; l < slices; l++)
{
indices.Add( (s + 1) * slices + l + 1); // bottom right.
indices.Add( s * slices + l + 1); // top right.
indices.Add(s * slices + l); // top left.
indices.Add( (s + 1) * slices + l); // bottom left.
indices.Add( (s + 1) * slices + l + 1); // bottom right.
indices.Add( s * slices + l); // top left.
}
indices.Add( (s + 1) * slices + 1); // bottom right.
indices.Add( s * slices + 1); // top right.
indices.Add( s * slices + slices); // top left.
indices.Add( (s + 1) * slices + slices); // bottom left.
indices.Add( (s + 1) * slices + 1); // bottom right.
indices.Add( s * slices + slices); // top left.
}
// create index for south pole cap.
uint baseIndex = slices * (stacks - 2);
uint lastIndex = (uint)pos.Count - 1;
for (uint l = 1; l < slices; l++)
{
indices.Add( baseIndex + l);
indices.Add( lastIndex);
indices.Add( baseIndex + l + 1);
}
indices.Add(baseIndex + slices);
indices.Add(lastIndex);
indices.Add(baseIndex + 1);
}
public static void CreateCone(float rad,float height, uint slices, uint stacks,
List<Vector3> pos, List<Vector3> normal, List<uint> indices)
{
CreateCylinder(rad, 0, height, slices, stacks, pos, normal, indices);
}
public static void CreateCylinder(float rad1, float rad2,float height, uint slices, uint stacks,
List<Vector3> pos, List<Vector3> normal, List<uint> indices)
{
float stackHeight = height / stacks;
// Amount to increment radius as we move up each stack level from bottom to top.
float radiusStep = (rad2 - rad1) / stacks;
uint numRings = stacks+1;
// Compute vertices for each stack ring.
for (uint i = 0; i < numRings; ++i)
{
float y = i*stackHeight;
float r = rad1 + i*radiusStep;
// Height and radius of next ring up.
float y_next = (i + 1)*stackHeight;
float r_next = rad1 + (i + 1)*radiusStep;
// vertices of ring
float dTheta = 2.0f*MathHelper.Pi/slices;
for (uint j = 0; j <= slices; ++j)
{
float c = (float) Math.Cos(j*dTheta);
float s = (float) Math.Sin(j*dTheta);
// tex coord if needed.
//float u = j/(float) slices;
//float v = 1.0f - (float) i/stacks;
// Partial derivative in theta direction to get tangent vector (this is a unit vector).
Vector3 T = new Vector3(-s, 0.0f, c);
// Compute tangent vector down the slope of the cone (if the top/bottom
// radii differ then we get a cone and not a true cylinder).
Vector3 P = new Vector3(r*c, y, r*s);
Vector3 P_next = new Vector3(r_next*c, y_next, r_next*s);
Vector3 B = P - P_next;
B.Normalize();
Vector3 N = Vector3.Cross(T, B);
N.Normalize();
P.Z *= -1;
N.Z *= -1;
pos.Add(P);
if (normal != null)
normal.Add(N);
}
}
uint numRingVertices = slices+1;
// Compute indices for each stack.
for(uint i = 0; i < stacks; ++i)
{
for (uint j = 0; j < slices; ++j)
{
indices.Add(i*numRingVertices + j);
indices.Add((i + 1)*numRingVertices + j + 1);
indices.Add((i + 1) * numRingVertices + j);
indices.Add(i*numRingVertices + j);
indices.Add(i*numRingVertices + j + 1);
indices.Add((i + 1) * numRingVertices + j + 1);
}
}
// build bottom cap.
if(rad1 > 0)
{
uint baseIndex = (uint) pos.Count;
// Duplicate cap vertices because the texture coordinates and normals differ.
float y = 0;
// vertices of ring
float dTheta = 2.0f * MathHelper.Pi / slices;
for (uint i = 0; i <= slices; ++i)
{
float x = rad1 * (float)Math.Cos(i * dTheta);
float z = rad1 * (float)Math.Sin(i * dTheta);
// Map [-1,1]-->[0,1] for planar texture coordinates.
//float u = +0.5f * x / mBottomRadius + 0.5f;
//float v = -0.5f * z / mBottomRadius + 0.5f;
Vector3 p = new Vec3F(x,y,-z);
pos.Add(p);
if(normal != null)
normal.Add(new Vec3F(0.0f, -1.0f, 0.0f));
}
// cap center vertex
pos.Add( new Vec3F(0.0f, y, 0.0f));
if(normal != null)
normal.Add(new Vec3F(0.0f, -1.0f, 0.0f));
// tex coord for center cap 0.5f, 0.5f
// index of center vertex
uint centerIndex = (uint)pos.Count - 1;
for (uint i = 0; i < slices; ++i)
{
indices.Add(centerIndex);
indices.Add(baseIndex + i + 1);
indices.Add(baseIndex + i);
}
}
// build top cap.
if(rad2 > 0)
{
uint baseIndex = (uint) pos.Count;
// Duplicate cap vertices because the texture coordinates and normals differ.
float y = height;
// vertices of ring
float dTheta = 2.0f * MathHelper.Pi / slices;
for (uint i = 0; i <= slices; ++i)
{
float x = rad2 * (float)Math.Cos(i * dTheta);
float z = rad2 * (float)Math.Sin(i * dTheta);
// Map [-1,1]-->[0,1] for planar texture coordinates.
//float u = +0.5f * x / mTopRadius + 0.5f;
//float v = -0.5f * z / mTopRadius + 0.5f;
pos.Add(new Vec3F(x, y, -z));
if(normal != null)
normal.Add( new Vec3F(0.0f, 1.0f, 0.0f) );
}
// pos, norm, tex1 for cap center vertex
pos.Add( new Vec3F(0.0f, y, 0.0f));
if(normal != null)
normal.Add(new Vec3F(0.0f, 1.0f, 0.0f));
// tex coord 0.5f, 0.5f
// index of center vertex
uint centerIndex = (uint)pos.Count - 1;
for (uint i = 0; i < slices; ++i)
{
indices.Add(centerIndex);
indices.Add(baseIndex + i);
indices.Add(baseIndex + i + 1);
}
}
}
public static void CreateTorus(float innerRadius,
float outerRadius,
uint rings,
uint sides,
List<Vec3F> pos,
List<Vec3F> nor,
List<Vec2F> tex,
List<uint> indices)
{
uint ringStride = rings + 1;
uint sideStride = sides + 1;
// radiusC: distance to center of the ring
float radiusC = (innerRadius + outerRadius) * 0.5f;
//radiusR: the radius of the ring
float radiusR = (outerRadius - radiusC);
for (uint i = 0; i <= rings; i++)
{
float u = (float)i / rings;
float outerAngle = i * MathHelper.TwoPi / rings;
// xform from ring space to torus space.
Matrix4F trans = new Matrix4F();
trans.Translation = new Vec3F(radiusC, 0, 0);
Matrix4F roty = new Matrix4F();
roty.RotY(outerAngle);
Matrix4F transform = trans * roty;
// create vertices for each ring.
for (uint j = 0; j <= sides; j++)
{
float v = (float)j / sides;
float innerAngle = j * MathHelper.TwoPi / sides + MathHelper.Pi;
float dx = (float)Math.Cos(innerAngle);
float dy = (float)Math.Sin(innerAngle);
// normal, position ,and texture coordinates
Vec3F n = new Vec3F(dx, dy, 0);
Vec3F p = n * radiusR;
if (tex != null)
{
Vec2F t = new Vec2F(u, v);
tex.Add(t);
}
transform.Transform(ref p);
transform.TransformVector(n, out n);
pos.Add(p);
nor.Add(n);
// And create indices for two triangles.
uint nextI = (i + 1) % ringStride;
uint nextJ = (j + 1) % sideStride;
indices.Add(nextI * sideStride + j);
indices.Add(i * sideStride + nextJ);
indices.Add(i * sideStride + j);
indices.Add(nextI * sideStride + j);
indices.Add(nextI * sideStride + nextJ);
indices.Add(i * sideStride + nextJ);
}
}
}
public static void CreateUnitBox(List<Vector3> pos, List<uint> indices)
{
// corner vertices
// top verts
pos.Add(new Vec3F(-0.5f, 0.5f, -0.5f)); // 0
pos.Add(new Vec3F(0.5f, 0.5f, -0.5f)); // 1
pos.Add(new Vec3F(0.5f, 0.5f, 0.5f)); // 2
pos.Add(new Vec3F(-0.5f, 0.5f, 0.5f)); // 3
// bottoms verts.
pos.Add(new Vec3F(-0.5f, -0.5f, -0.5f)); // 4
pos.Add(new Vec3F(0.5f, -0.5f, -0.5f)); // 5
pos.Add(new Vec3F(0.5f, -0.5f, 0.5f)); // 6
pos.Add(new Vec3F(-0.5f, -0.5f, 0.5f)); // 7
// create indices for line list.
// top
indices.Add(0); indices.Add(1);
indices.Add(1); indices.Add(2);
indices.Add(2); indices.Add(3);
indices.Add(3); indices.Add(0);
// bottom
indices.Add(4); indices.Add(5);
indices.Add(5); indices.Add(6);
indices.Add(6); indices.Add(7);
indices.Add(7); indices.Add(4);
// front
indices.Add(3); indices.Add(2);
indices.Add(2); indices.Add(6);
indices.Add(6); indices.Add(7);
indices.Add(7); indices.Add(3);
// back
indices.Add(0); indices.Add(1);
indices.Add(1); indices.Add(5);
indices.Add(5); indices.Add(4);
indices.Add(4); indices.Add(0);
}
public static void CreateUnitCube(List<Vector3> pos, List<Vector3> normal, List<Vector2> tex, List<uint> indices)
{
VertexPNT[] v = new VertexPNT[24];
// Fill in the front face vertex data.
v[0] = new VertexPNT(-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
v[1] = new VertexPNT(-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
v[2] = new VertexPNT( 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
v[3] = new VertexPNT( 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);
// Fill in the back face vertex data.
v[4] = new VertexPNT(-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f);
v[5] = new VertexPNT( 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f);
v[6] = new VertexPNT( 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
v[7] = new VertexPNT(-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f);
// Fill in the top face vertex data.
v[8] = new VertexPNT(-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f);
v[9] = new VertexPNT(-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f);
v[10] = new VertexPNT( 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);
v[11] = new VertexPNT( 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f);
// Fill in the bottom face vertex data.
v[12] = new VertexPNT(-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f);
v[13] = new VertexPNT( 0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f);
v[14] = new VertexPNT( 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f);
v[15] = new VertexPNT(-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f);
// Fill in the left face vertex data.
v[16] = new VertexPNT(-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
v[17] = new VertexPNT(-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f);
v[18] = new VertexPNT(-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f);
v[19] = new VertexPNT(-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f);
// Fill in the right face vertex data.
v[20] = new VertexPNT( 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
v[21] = new VertexPNT( 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f);
v[22] = new VertexPNT( 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f);
v[23] = new VertexPNT( 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f);
for(int i = 0; i < v.Length; ++i)
{
v[i].Position.Z *= -1;
v[i].Normal.Z *= -1;
pos.Add(v[i].Position);
normal.Add(v[i].Normal);
if(tex != null)
tex.Add(v[i].Tex);
}
uint[] index = new uint[36];
// Fill in the front face index data
index[0] = 0; index[1] = 1; index[2] = 2;
index[3] = 0; index[4] = 2; index[5] = 3;
// Fill in the back face index data
index[6] = 4; index[7] = 5; index[8] = 6;
index[9] = 4; index[10] = 6; index[11] = 7;
// Fill in the top face index data
index[12] = 8; index[13] = 9; index[14] = 10;
index[15] = 8; index[16] = 10; index[17] = 11;
// Fill in the bottom face index data
index[18] = 12; index[19] = 13; index[20] = 14;
index[21] = 12; index[22] = 14; index[23] = 15;
// Fill in the left face index data
index[24] = 16; index[25] = 17; index[26] = 18;
index[27] = 16; index[28] = 18; index[29] = 19;
// Fill in the right face index data
index[30] = 20; index[31] = 21; index[32] = 22;
index[33] = 20; index[34] = 22; index[35] = 23;
// reverse windings
for (int i = 0; i < 34; i += 3)
{
indices.Add(index[i + 2]);
indices.Add(index[i + 1]);
indices.Add(index[i]);
}
}
}
public static class MathHelper
{
/// <summary>
/// Represents the mathematical constant e. </summary>
public const float E = 2.71828f;
/// <summary>
/// Represents the log base ten of e.</summary>
public const float Log10E = 0.434294f;
/// <summary>
/// Represents the log base two of e.</summary>
public const float Log2E = 1.4427f;
//
// Summary:
// Represents the value of pi.
public const float Pi = (float)Math.PI;
//
// Summary: