Annotation of /trunk/IdealistViewer/Application/PrimMesherG.cs
Parent Directory
|
Revision Log
Revision 112 - (view) (download)
| 1 : | teravus | 2 | using System; |
| 2 : | using System.Collections.Generic; | ||
| 3 : | using System.Reflection; | ||
| 4 : | using System.Text; | ||
| 5 : | using OpenMetaverse; | ||
| 6 : | using PrimMesher; | ||
| 7 : | using IrrlichtNETCP; | ||
| 8 : | using log4net; | ||
| 9 : | using log4net.Appender; | ||
| 10 : | using log4net.Core; | ||
| 11 : | using log4net.Repository; | ||
| 12 : | |||
| 13 : | namespace IdealistViewer | ||
| 14 : | { | ||
| 15 : | teravus | 112 | public enum LevelOfDetail |
| 16 : | { | ||
| 17 : | Low, | ||
| 18 : | Medium, | ||
| 19 : | High | ||
| 20 : | } | ||
| 21 : | teravus | 2 | public static class PrimMesherG |
| 22 : | { | ||
| 23 : | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
| 24 : | |||
| 25 : | public static Vector2D convVect2d(UVCoord uv) | ||
| 26 : | { | ||
| 27 : | return new Vector2D(uv.U, uv.V); | ||
| 28 : | } | ||
| 29 : | |||
| 30 : | public static Vector3D convVect3d(Coord c) | ||
| 31 : | {// translate coordinates XYZ to XZY | ||
| 32 : | return new Vector3D(c.X, c.Z, c.Y); | ||
| 33 : | } | ||
| 34 : | |||
| 35 : | public static Vector3D convNormal(Coord c) | ||
| 36 : | {// translate coordinates XYZ to XZY | ||
| 37 : | return new Vector3D(c.X, c.Z, c.Y); | ||
| 38 : | } | ||
| 39 : | |||
| 40 : | dahlia | 103 | private static Mesh FacesToIrrMesh(List<ViewerFace> viewerFaces, int numPrimFaces) |
| 41 : | { | ||
| 42 : | Color color = new Color(255, 255, 0, 50); | ||
| 43 : | |||
| 44 : | Mesh mesh = new Mesh(); | ||
| 45 : | |||
| 46 : | int numViewerFaces = viewerFaces.Count; | ||
| 47 : | |||
| 48 : | MeshBuffer[] mb = new MeshBuffer[numPrimFaces]; | ||
| 49 : | |||
| 50 : | for (int i = 0; i < mb.Length; i++) | ||
| 51 : | mb[i] = new MeshBuffer(VertexType.Standard); | ||
| 52 : | |||
| 53 : | try | ||
| 54 : | { | ||
| 55 : | uint[] index = new uint[mb.Length]; | ||
| 56 : | |||
| 57 : | for (int i = 0; i < index.Length; i++) | ||
| 58 : | index[i] = 0; | ||
| 59 : | |||
| 60 : | for (uint i = 0; i < numViewerFaces; i++) | ||
| 61 : | { | ||
| 62 : | ViewerFace vf = viewerFaces[(int)i]; | ||
| 63 : | |||
| 64 : | try | ||
| 65 : | { | ||
| 66 : | mb[vf.primFaceNumber].SetVertex(index[vf.primFaceNumber], new Vertex3D(convVect3d(vf.v1), convNormal(vf.n1), color, convVect2d(vf.uv1))); | ||
| 67 : | mb[vf.primFaceNumber].SetVertex(index[vf.primFaceNumber] + 1, new Vertex3D(convVect3d(vf.v2), convNormal(vf.n2), color, convVect2d(vf.uv2))); | ||
| 68 : | mb[vf.primFaceNumber].SetVertex(index[vf.primFaceNumber] + 2, new Vertex3D(convVect3d(vf.v3), convNormal(vf.n3), color, convVect2d(vf.uv3))); | ||
| 69 : | |||
| 70 : | } | ||
| 71 : | catch (OutOfMemoryException) | ||
| 72 : | { | ||
| 73 : | return null; | ||
| 74 : | } | ||
| 75 : | |||
| 76 : | mb[vf.primFaceNumber].SetIndex(index[vf.primFaceNumber], (ushort)index[vf.primFaceNumber]); | ||
| 77 : | mb[vf.primFaceNumber].SetIndex(index[vf.primFaceNumber] + 1, (ushort)(index[vf.primFaceNumber] + 2)); | ||
| 78 : | mb[vf.primFaceNumber].SetIndex(index[vf.primFaceNumber] + 2, (ushort)(index[vf.primFaceNumber] + 1)); | ||
| 79 : | |||
| 80 : | index[vf.primFaceNumber] += 3; | ||
| 81 : | } | ||
| 82 : | |||
| 83 : | for (int i = 0; i < mb.Length; i++) | ||
| 84 : | mesh.AddMeshBuffer(mb[i]); | ||
| 85 : | |||
| 86 : | // don't dispose here | ||
| 87 : | //mb.Dispose(); | ||
| 88 : | } | ||
| 89 : | catch (AccessViolationException) | ||
| 90 : | { | ||
| 91 : | m_log.Error("ACCESSVIOLATION"); | ||
| 92 : | mesh = null; | ||
| 93 : | } | ||
| 94 : | |||
| 95 : | return mesh; | ||
| 96 : | } | ||
| 97 : | |||
| 98 : | dahlia | 109 | // experimental - build sculpt mesh using indexed access to vertex, normal, and UV lists |
| 99 : | private static Mesh SculptMeshToIrrMesh(SculptMesh sculptMesh) | ||
| 100 : | { | ||
| 101 : | Color color = new Color(255, 255, 0, 50); | ||
| 102 : | |||
| 103 : | Mesh mesh = new Mesh(); | ||
| 104 : | |||
| 105 : | int numFaces = sculptMesh.faces.Count; | ||
| 106 : | |||
| 107 : | MeshBuffer mb = new MeshBuffer(VertexType.Standard); | ||
| 108 : | |||
| 109 : | int numVerts = sculptMesh.coords.Count; | ||
| 110 : | |||
| 111 : | try | ||
| 112 : | { | ||
| 113 : | for (int i = 0; i < numVerts; i++) | ||
| 114 : | mb.SetVertex((uint)i, new Vertex3D(convVect3d(sculptMesh.coords[i]), convNormal(sculptMesh.normals[i]), color, convVect2d(sculptMesh.uvs[i]))); | ||
| 115 : | |||
| 116 : | ushort index = 0; | ||
| 117 : | foreach (Face face in sculptMesh.faces) | ||
| 118 : | { | ||
| 119 : | mb.SetIndex(index++, (ushort)face.v1); | ||
| 120 : | mb.SetIndex(index++, (ushort)face.v3); | ||
| 121 : | mb.SetIndex(index++, (ushort)face.v2); | ||
| 122 : | } | ||
| 123 : | |||
| 124 : | mesh.AddMeshBuffer(mb); | ||
| 125 : | |||
| 126 : | // don't dispose here | ||
| 127 : | //mb.Dispose(); | ||
| 128 : | } | ||
| 129 : | |||
| 130 : | catch (AccessViolationException) | ||
| 131 : | { | ||
| 132 : | m_log.Error("ACCESSVIOLATION"); | ||
| 133 : | mesh = null; | ||
| 134 : | } | ||
| 135 : | |||
| 136 : | return mesh; | ||
| 137 : | } | ||
| 138 : | dahlia | 103 | |
| 139 : | teravus | 112 | public static Mesh PrimitiveToIrrMesh(Primitive prim, LevelOfDetail detail) |
| 140 : | teravus | 2 | { |
| 141 : | Primitive.ConstructionData primData = prim.PrimData; | ||
| 142 : | int sides = 4; | ||
| 143 : | int hollowsides = 4; | ||
| 144 : | |||
| 145 : | float profileBegin = primData.ProfileBegin; | ||
| 146 : | float profileEnd = primData.ProfileEnd; | ||
| 147 : | dahlia | 15 | bool isSphere = false; |
| 148 : | teravus | 2 | |
| 149 : | if ((ProfileCurve)(primData.profileCurve & 0x07) == ProfileCurve.Circle) | ||
| 150 : | teravus | 112 | { |
| 151 : | switch (detail) | ||
| 152 : | { | ||
| 153 : | case LevelOfDetail.Low: | ||
| 154 : | sides = 6; | ||
| 155 : | break; | ||
| 156 : | case LevelOfDetail.Medium: | ||
| 157 : | sides = 12; | ||
| 158 : | break; | ||
| 159 : | default: | ||
| 160 : | sides = 24; | ||
| 161 : | break; | ||
| 162 : | } | ||
| 163 : | } | ||
| 164 : | teravus | 2 | else if ((ProfileCurve)(primData.profileCurve & 0x07) == ProfileCurve.EqualTriangle) |
| 165 : | sides = 3; | ||
| 166 : | else if ((ProfileCurve)(primData.profileCurve & 0x07) == ProfileCurve.HalfCircle) | ||
| 167 : | dahlia | 11 | { // half circle, prim is a sphere |
| 168 : | dahlia | 15 | isSphere = true; |
| 169 : | teravus | 112 | switch (detail) |
| 170 : | { | ||
| 171 : | case LevelOfDetail.Low: | ||
| 172 : | sides = 6; | ||
| 173 : | break; | ||
| 174 : | case LevelOfDetail.Medium: | ||
| 175 : | sides = 12; | ||
| 176 : | break; | ||
| 177 : | default: | ||
| 178 : | sides = 24; | ||
| 179 : | break; | ||
| 180 : | } | ||
| 181 : | dahlia | 11 | profileBegin = 0.5f * profileBegin + 0.5f; |
| 182 : | profileEnd = 0.5f * profileEnd + 0.5f; | ||
| 183 : | } | ||
| 184 : | teravus | 2 | |
| 185 : | if ((HoleType)primData.ProfileHole == HoleType.Same) | ||
| 186 : | hollowsides = sides; | ||
| 187 : | else if ((HoleType)primData.ProfileHole == HoleType.Circle) | ||
| 188 : | teravus | 112 | { |
| 189 : | switch (detail) | ||
| 190 : | { | ||
| 191 : | case LevelOfDetail.Low: | ||
| 192 : | hollowsides = 6; | ||
| 193 : | break; | ||
| 194 : | case LevelOfDetail.Medium: | ||
| 195 : | hollowsides = 12; | ||
| 196 : | break; | ||
| 197 : | default: | ||
| 198 : | hollowsides = 24; | ||
| 199 : | break; | ||
| 200 : | } | ||
| 201 : | |||
| 202 : | } | ||
| 203 : | teravus | 2 | else if ((HoleType)primData.ProfileHole == HoleType.Triangle) |
| 204 : | hollowsides = 3; | ||
| 205 : | |||
| 206 : | PrimMesh newPrim = new PrimMesh(sides, profileBegin, profileEnd, (float)primData.ProfileHollow, hollowsides); | ||
| 207 : | newPrim.viewerMode = true; | ||
| 208 : | newPrim.holeSizeX = primData.PathScaleX; | ||
| 209 : | newPrim.holeSizeY = primData.PathScaleY; | ||
| 210 : | newPrim.pathCutBegin = primData.PathBegin; | ||
| 211 : | newPrim.pathCutEnd = primData.PathEnd; | ||
| 212 : | newPrim.topShearX = primData.PathShearX; | ||
| 213 : | newPrim.topShearY = primData.PathShearY; | ||
| 214 : | newPrim.radius = primData.PathRadiusOffset; | ||
| 215 : | newPrim.revolutions = primData.PathRevolutions; | ||
| 216 : | newPrim.skew = primData.PathSkew; | ||
| 217 : | teravus | 112 | switch (detail) |
| 218 : | { | ||
| 219 : | case LevelOfDetail.Low: | ||
| 220 : | newPrim.stepsPerRevolution = 6; | ||
| 221 : | break; | ||
| 222 : | case LevelOfDetail.Medium: | ||
| 223 : | newPrim.stepsPerRevolution = 12; | ||
| 224 : | break; | ||
| 225 : | default: | ||
| 226 : | newPrim.stepsPerRevolution = 24; | ||
| 227 : | break; | ||
| 228 : | } | ||
| 229 : | teravus | 2 | |
| 230 : | teravus | 112 | |
| 231 : | |||
| 232 : | teravus | 2 | if (primData.PathCurve == PathCurve.Line) |
| 233 : | { | ||
| 234 : | newPrim.taperX = 1.0f - primData.PathScaleX; | ||
| 235 : | newPrim.taperY = 1.0f - primData.PathScaleY; | ||
| 236 : | newPrim.twistBegin = (int)(180 * primData.PathTwistBegin); | ||
| 237 : | newPrim.twistEnd = (int)(180 * primData.PathTwist); | ||
| 238 : | newPrim.ExtrudeLinear(); | ||
| 239 : | } | ||
| 240 : | else | ||
| 241 : | { | ||
| 242 : | newPrim.taperX = primData.PathTaperX; | ||
| 243 : | newPrim.taperY = primData.PathTaperY; | ||
| 244 : | newPrim.twistBegin = (int)(360 * primData.PathTwistBegin); | ||
| 245 : | newPrim.twistEnd = (int)(360 * primData.PathTwist); | ||
| 246 : | newPrim.ExtrudeCircular(); | ||
| 247 : | } | ||
| 248 : | |||
| 249 : | teravus | 67 | int numViewerFaces = newPrim.viewerFaces.Count; |
| 250 : | teravus | 2 | |
| 251 : | dahlia | 103 | for (uint i = 0; i < numViewerFaces; i++) |
| 252 : | teravus | 2 | { |
| 253 : | dahlia | 103 | ViewerFace vf = newPrim.viewerFaces[(int)i]; |
| 254 : | teravus | 67 | |
| 255 : | dahlia | 103 | if (isSphere) |
| 256 : | teravus | 2 | { |
| 257 : | dahlia | 103 | vf.uv1.U = (vf.uv1.U - 0.5f) * 2.0f; |
| 258 : | vf.uv2.U = (vf.uv2.U - 0.5f) * 2.0f; | ||
| 259 : | vf.uv3.U = (vf.uv3.U - 0.5f) * 2.0f; | ||
| 260 : | teravus | 2 | } |
| 261 : | } | ||
| 262 : | teravus | 67 | |
| 263 : | dahlia | 103 | return FacesToIrrMesh(newPrim.viewerFaces, newPrim.numPrimFaces); |
| 264 : | teravus | 2 | } |
| 265 : | dahlia | 79 | |
| 266 : | dahlia | 92 | public static Mesh SculptIrrMesh(System.Drawing.Bitmap bitmap, OpenMetaverse.SculptType omSculptType) |
| 267 : | { | ||
| 268 : | switch (omSculptType) | ||
| 269 : | { | ||
| 270 : | case OpenMetaverse.SculptType.Cylinder: | ||
| 271 : | return SculptIrrMesh(bitmap, SculptMesh.SculptType.cylinder); | ||
| 272 : | case OpenMetaverse.SculptType.Plane: | ||
| 273 : | return SculptIrrMesh(bitmap, SculptMesh.SculptType.plane); | ||
| 274 : | case OpenMetaverse.SculptType.Sphere: | ||
| 275 : | return SculptIrrMesh(bitmap, SculptMesh.SculptType.sphere); | ||
| 276 : | case OpenMetaverse.SculptType.Torus: | ||
| 277 : | return SculptIrrMesh(bitmap, SculptMesh.SculptType.torus); | ||
| 278 : | default: | ||
| 279 : | return SculptIrrMesh(bitmap, SculptMesh.SculptType.plane); | ||
| 280 : | } | ||
| 281 : | } | ||
| 282 : | dahlia | 79 | |
| 283 : | public static Mesh SculptIrrMesh(System.Drawing.Bitmap bitmap) | ||
| 284 : | { | ||
| 285 : | dahlia | 93 | return SculptIrrMesh(bitmap, PrimMesher.SculptMesh.SculptType.plane); |
| 286 : | dahlia | 92 | } |
| 287 : | dahlia | 79 | |
| 288 : | dahlia | 92 | public static Mesh SculptIrrMesh(System.Drawing.Bitmap bitmap, PrimMesher.SculptMesh.SculptType sculptType) |
| 289 : | { | ||
| 290 : | SculptMesh newSculpty = new SculptMesh(bitmap, sculptType, 32, true); | ||
| 291 : | dahlia | 79 | |
| 292 : | dahlia | 109 | //return FacesToIrrMesh(newSculpty.viewerFaces, 1); |
| 293 : | |||
| 294 : | // experimental - build sculpt mesh using vertex, normal, and coord lists | ||
| 295 : | return SculptMeshToIrrMesh(newSculpty); | ||
| 296 : | dahlia | 79 | } |
| 297 : | teravus | 2 | } |
| 298 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

