| 1 |
/****************************************************************************** |
|---|
| 2 |
x42view.net - .x42 viewer and auditing tool |
|---|
| 3 |
Copyright (C) 2007 HermitWorks Entertainment Corporation |
|---|
| 4 |
|
|---|
| 5 |
This program is free software; you can redistribute it and/or modify it |
|---|
| 6 |
under the terms of the GNU General Public License as published by the Free |
|---|
| 7 |
Software Foundation; either version 2 of the License, or (at your option) |
|---|
| 8 |
any later version. |
|---|
| 9 |
|
|---|
| 10 |
This program is distributed in the hope that it will be useful, but |
|---|
| 11 |
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
|---|
| 12 |
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|---|
| 13 |
for more details. |
|---|
| 14 |
|
|---|
| 15 |
You should have received a copy of the GNU General Public License along |
|---|
| 16 |
with this program; if not, write to the |
|---|
| 17 |
|
|---|
| 18 |
Free Software Foundation, Inc. |
|---|
| 19 |
51 Franklin Street, Fifth Floor |
|---|
| 20 |
Boston, MA 02110-1301, USA. |
|---|
| 21 |
******************************************************************************/ |
|---|
| 22 |
|
|---|
| 23 |
using System; |
|---|
| 24 |
using System.Collections.Generic; |
|---|
| 25 |
|
|---|
| 26 |
namespace x42view |
|---|
| 27 |
{ |
|---|
| 28 |
partial class Helpers |
|---|
| 29 |
{ |
|---|
| 30 |
public struct LineIndices |
|---|
| 31 |
{ |
|---|
| 32 |
public ushort Index0; |
|---|
| 33 |
public ushort Index1; |
|---|
| 34 |
|
|---|
| 35 |
public LineIndices( ushort i0, ushort i1 ) |
|---|
| 36 |
{ |
|---|
| 37 |
Index0 = i0; |
|---|
| 38 |
Index1 = i1; |
|---|
| 39 |
} |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
public struct TriangleIndices |
|---|
| 43 |
{ |
|---|
| 44 |
public ushort Index0; |
|---|
| 45 |
public ushort Index1; |
|---|
| 46 |
public ushort Index2; |
|---|
| 47 |
|
|---|
| 48 |
public TriangleIndices( ushort i0, ushort i1, ushort i2 ) |
|---|
| 49 |
{ |
|---|
| 50 |
Index0 = i0; |
|---|
| 51 |
Index1 = i1; |
|---|
| 52 |
Index2 = i2; |
|---|
| 53 |
} |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
public sealed class PrimitiveIterator |
|---|
| 57 |
{ |
|---|
| 58 |
private int currPrim; |
|---|
| 59 |
private int numPrims; |
|---|
| 60 |
private Libx42.PrimitiveType primType; |
|---|
| 61 |
private Libx42.IndexCollection indices; |
|---|
| 62 |
|
|---|
| 63 |
public PrimitiveIterator( Libx42.Group group ) |
|---|
| 64 |
{ |
|---|
| 65 |
primType = group.PrimitiveType; |
|---|
| 66 |
indices = group.Indices; |
|---|
| 67 |
numPrims = group.PrimitiveCount; |
|---|
| 68 |
|
|---|
| 69 |
Reset(); |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
public PrimitiveIterator( Libx42.PrimitiveType primType, |
|---|
| 73 |
Libx42.IndexCollection indices, int numPrims ) |
|---|
| 74 |
{ |
|---|
| 75 |
this.primType = primType; |
|---|
| 76 |
this.indices = indices; |
|---|
| 77 |
this.numPrims = numPrims; |
|---|
| 78 |
|
|---|
| 79 |
Reset(); |
|---|
| 80 |
} |
|---|
| 81 |
|
|---|
| 82 |
public void Reset() |
|---|
| 83 |
{ |
|---|
| 84 |
currPrim = -1; |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
public bool MoveNext() |
|---|
| 88 |
{ |
|---|
| 89 |
if( currPrim == -1 ) |
|---|
| 90 |
{ |
|---|
| 91 |
if( numPrims == 0 ) |
|---|
| 92 |
return false; |
|---|
| 93 |
|
|---|
| 94 |
currPrim = 0; |
|---|
| 95 |
return true; |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
if( currPrim == numPrims - 1 ) |
|---|
| 99 |
return false; |
|---|
| 100 |
|
|---|
| 101 |
currPrim++; |
|---|
| 102 |
return true; |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
public int IndicesPerPrimitive |
|---|
| 106 |
{ |
|---|
| 107 |
get |
|---|
| 108 |
{ |
|---|
| 109 |
switch( primType ) |
|---|
| 110 |
{ |
|---|
| 111 |
case Libx42.PrimitiveType.PointList: |
|---|
| 112 |
return 1; |
|---|
| 113 |
|
|---|
| 114 |
case Libx42.PrimitiveType.LineList: |
|---|
| 115 |
case Libx42.PrimitiveType.LineStrip: |
|---|
| 116 |
return 2; |
|---|
| 117 |
|
|---|
| 118 |
case Libx42.PrimitiveType.TriangleList: |
|---|
| 119 |
case Libx42.PrimitiveType.TriangleStrip: |
|---|
| 120 |
case Libx42.PrimitiveType.TriangleFan: |
|---|
| 121 |
return 3; |
|---|
| 122 |
|
|---|
| 123 |
default: |
|---|
| 124 |
throw new Exception( "Invalid primitive type." ); |
|---|
| 125 |
} |
|---|
| 126 |
} |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
public ushort Current( int iVert ) |
|---|
| 130 |
{ |
|---|
| 131 |
if( iVert < 0 || iVert >= IndicesPerPrimitive ) |
|---|
| 132 |
throw new ArgumentOutOfRangeException( "iVert" ); |
|---|
| 133 |
|
|---|
| 134 |
int idx; |
|---|
| 135 |
switch( primType ) |
|---|
| 136 |
{ |
|---|
| 137 |
case Libx42.PrimitiveType.PointList: |
|---|
| 138 |
idx = currPrim; |
|---|
| 139 |
break; |
|---|
| 140 |
|
|---|
| 141 |
case Libx42.PrimitiveType.LineList: |
|---|
| 142 |
idx = currPrim * 2 + iVert; |
|---|
| 143 |
break; |
|---|
| 144 |
|
|---|
| 145 |
case Libx42.PrimitiveType.LineStrip: |
|---|
| 146 |
idx = currPrim + iVert; |
|---|
| 147 |
break; |
|---|
| 148 |
|
|---|
| 149 |
case Libx42.PrimitiveType.TriangleList: |
|---|
| 150 |
idx = currPrim * 3 + iVert; |
|---|
| 151 |
break; |
|---|
| 152 |
|
|---|
| 153 |
case Libx42.PrimitiveType.TriangleStrip: |
|---|
| 154 |
if( (currPrim & 1) != 0 ) |
|---|
| 155 |
{ |
|---|
| 156 |
switch( iVert ) |
|---|
| 157 |
{ |
|---|
| 158 |
case 0: |
|---|
| 159 |
idx = currPrim + 1; |
|---|
| 160 |
break; |
|---|
| 161 |
|
|---|
| 162 |
case 1: |
|---|
| 163 |
idx = currPrim + 0; |
|---|
| 164 |
break; |
|---|
| 165 |
|
|---|
| 166 |
case 2: |
|---|
| 167 |
idx = currPrim + 2; |
|---|
| 168 |
break; |
|---|
| 169 |
|
|---|
| 170 |
default: |
|---|
| 171 |
idx = 0; |
|---|
| 172 |
break; |
|---|
| 173 |
} |
|---|
| 174 |
} |
|---|
| 175 |
else |
|---|
| 176 |
{ |
|---|
| 177 |
idx = currPrim + iVert; |
|---|
| 178 |
} |
|---|
| 179 |
break; |
|---|
| 180 |
|
|---|
| 181 |
case Libx42.PrimitiveType.TriangleFan: |
|---|
| 182 |
idx = iVert == 0 ? 0 : currPrim + iVert; |
|---|
| 183 |
break; |
|---|
| 184 |
|
|---|
| 185 |
default: |
|---|
| 186 |
throw new Exception( "Invalid primitive type." ); |
|---|
| 187 |
} |
|---|
| 188 |
|
|---|
| 189 |
return indices[idx]; |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
public static IEnumerable<ushort> EnumeratePoints( Libx42.Group group ) |
|---|
| 193 |
{ |
|---|
| 194 |
switch( group.PrimitiveType ) |
|---|
| 195 |
{ |
|---|
| 196 |
case Libx42.PrimitiveType.PointList: |
|---|
| 197 |
break; |
|---|
| 198 |
|
|---|
| 199 |
default: |
|---|
| 200 |
throw new InvalidOperationException(); |
|---|
| 201 |
} |
|---|
| 202 |
|
|---|
| 203 |
PrimitiveIterator tmp = new PrimitiveIterator( group ); |
|---|
| 204 |
while( tmp.MoveNext() ) |
|---|
| 205 |
yield return tmp.Current( 0 ); |
|---|
| 206 |
} |
|---|
| 207 |
|
|---|
| 208 |
public static IEnumerable<LineIndices> EnumerateLines( Libx42.Group group ) |
|---|
| 209 |
{ |
|---|
| 210 |
switch( group.PrimitiveType ) |
|---|
| 211 |
{ |
|---|
| 212 |
case Libx42.PrimitiveType.LineList: |
|---|
| 213 |
case Libx42.PrimitiveType.LineStrip: |
|---|
| 214 |
break; |
|---|
| 215 |
|
|---|
| 216 |
default: |
|---|
| 217 |
throw new InvalidOperationException(); |
|---|
| 218 |
} |
|---|
| 219 |
|
|---|
| 220 |
PrimitiveIterator tmp = new PrimitiveIterator( group ); |
|---|
| 221 |
while( tmp.MoveNext() ) |
|---|
| 222 |
yield return new LineIndices( tmp.Current( 0 ), tmp.Current( 1 ) ); |
|---|
| 223 |
} |
|---|
| 224 |
|
|---|
| 225 |
public static IEnumerable<TriangleIndices> EnumerateTriangles( Libx42.Group group ) |
|---|
| 226 |
{ |
|---|
| 227 |
switch( group.PrimitiveType ) |
|---|
| 228 |
{ |
|---|
| 229 |
case Libx42.PrimitiveType.TriangleList: |
|---|
| 230 |
case Libx42.PrimitiveType.TriangleStrip: |
|---|
| 231 |
case Libx42.PrimitiveType.TriangleFan: |
|---|
| 232 |
break; |
|---|
| 233 |
|
|---|
| 234 |
default: |
|---|
| 235 |
throw new InvalidOperationException(); |
|---|
| 236 |
} |
|---|
| 237 |
|
|---|
| 238 |
PrimitiveIterator tmp = new PrimitiveIterator( group ); |
|---|
| 239 |
while( tmp.MoveNext() ) |
|---|
| 240 |
yield return new TriangleIndices( tmp.Current( 0 ), tmp.Current( 1 ), tmp.Current( 2 ) ); |
|---|
| 241 |
} |
|---|
| 242 |
} |
|---|
| 243 |
} |
|---|
| 244 |
|
|---|
| 245 |
} |
|---|