Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1452 | chris | 1 | using System; |
| 2 | using System.Collections.Generic; |
||
| 3 | |||
| 4 | using OpenTK; |
||
| 5 | |||
| 6 | namespace Examples.Shapes |
||
| 7 | { |
||
| 8 | public sealed partial class MengerSponge: DrawableShape |
||
| 9 | { |
||
| 10 | |||
| 11 | public enum eSubdivisions |
||
| 12 | { |
||
| 13 | None = 0, |
||
| 14 | One = 1, |
||
| 15 | Two = 2, |
||
| 16 | Three = 3, |
||
| 17 | } |
||
| 18 | |||
| 19 | public MengerSponge( double scale, eSubdivisions subdivs, bool useDL ) |
||
| 20 | : base( useDL ) |
||
| 21 | { |
||
| 22 | List<MengerCube> Cubes; |
||
| 23 | switch ( subdivs ) |
||
| 24 | { |
||
| 25 | case eSubdivisions.None: |
||
| 26 | CreateDefaultMengerSponge( scale, out Cubes ); |
||
| 27 | break; |
||
| 28 | case eSubdivisions.One: |
||
| 29 | case eSubdivisions.Two: |
||
| 30 | case eSubdivisions.Three: |
||
| 31 | CreateDefaultMengerSponge( scale, out Cubes ); |
||
| 32 | for ( int i = 0; i < (int)subdivs; i++ ) |
||
| 33 | { |
||
| 34 | List<MengerCube> temp; |
||
| 35 | SubdivideMengerSponge( ref Cubes, out temp ); |
||
| 36 | Cubes = temp; |
||
| 37 | } |
||
| 38 | break; |
||
| 39 | default: throw new ArgumentOutOfRangeException( "Subdivisions other than contained in the enum cause overflows and are not allowed." ); |
||
| 40 | } |
||
| 41 | |||
| 42 | PrimitiveMode = OpenTK.Graphics.OpenGL.BeginMode.Triangles; |
||
| 43 | |||
| 44 | #region Get Array Dimensions |
||
| 45 | uint |
||
| 46 | VertexCount = 0, |
||
| 47 | IndexCount = 0; |
||
| 48 | |||
| 49 | foreach ( MengerCube c in Cubes ) |
||
| 50 | { |
||
| 51 | uint t1, t2; |
||
| 52 | c.GetArraySizes( out t1, out t2 ); |
||
| 53 | VertexCount += t1; |
||
| 54 | IndexCount += t2; |
||
| 55 | } |
||
| 56 | |||
| 57 | VertexArray = new VertexT2dN3dV3d[VertexCount]; |
||
| 58 | IndexArray = new uint[IndexCount]; |
||
| 59 | #endregion Get Array Dimensions |
||
| 60 | |||
| 61 | List<Chunk> AllChunks = new List<Chunk>(); |
||
| 62 | |||
| 63 | #region Build a temporary List of all loose pieces |
||
| 64 | foreach ( MengerCube c in Cubes ) |
||
| 65 | { |
||
| 66 | c.GetVboAndIbo( ref AllChunks ); |
||
| 67 | } |
||
| 68 | #endregion Build a temporary List of all loose pieces |
||
| 69 | |||
| 70 | #region Assemble pieces into a single VBO and IBO |
||
| 71 | VertexCount = 0; |
||
| 72 | IndexCount = 0; |
||
| 73 | |||
| 74 | foreach ( Chunk ch in AllChunks ) |
||
| 75 | { |
||
| 76 | for ( int i = 0; i < ch.Vertices.Length; i++ ) |
||
| 77 | { |
||
| 78 | VertexArray[VertexCount + i] = ch.Vertices[i]; |
||
| 79 | } |
||
| 80 | |||
| 81 | for ( int i = 0; i < ch.Indices.Length; i++ ) |
||
| 82 | { |
||
| 83 | IndexArray[IndexCount + i] = ch.Indices[i] + VertexCount; |
||
| 84 | } |
||
| 85 | |||
| 86 | VertexCount += (uint)ch.Vertices.Length; |
||
| 87 | IndexCount += (uint)ch.Indices.Length; |
||
| 88 | } |
||
| 89 | |||
| 90 | #endregion Assemble pieces into a single VBO and IBO |
||
| 91 | |||
| 92 | AllChunks.Clear(); |
||
| 93 | } |
||
| 94 | |||
| 95 | private void CreateDefaultMengerSponge( double halfwidth, out List<MengerCube> output ) |
||
| 96 | { |
||
| 97 | output = new List<MengerCube>( 1 ); |
||
| 98 | output.Add( new MengerCube( Vector3d.Zero, halfwidth, MengerCube.AllSides, MengerCube.AllSides ) ); |
||
| 99 | } |
||
| 100 | |||
| 101 | private void SubdivideMengerSponge( ref List<MengerCube> input, out List<MengerCube> output ) |
||
| 102 | { |
||
| 103 | output = new List<MengerCube>( input.Count * 20 ); |
||
| 104 | foreach ( MengerCube InputCube in input ) |
||
| 105 | { |
||
| 106 | MengerCube[] SubdividedCubes; |
||
| 107 | InputCube.Subdivide( out SubdividedCubes ); |
||
| 108 | for ( int i = 0; i < SubdividedCubes.Length; i++ ) |
||
| 109 | { |
||
| 110 | output.Add( SubdividedCubes[i] ); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | } |
||
| 116 | } |