예제 #1
0
        // Convert the positions and all the vertices in an ExtendedPrim from one
        //     coordinate space to another. ExtendedPrim.coordSpace gives the current
        //     coordinates and we specify a new one here.
        // This is not a general solution -- it pretty much only works to convert
        //     right-handed,Z-up coordinates (OpenSimulator) to right-handed,Y-up
        //     (OpenGL).
        public static void FixCoordinates(BInstance inst, CoordAxis newCoords)
        {
            if (inst.coordAxis.system != newCoords.system)
            {
                OMV.Matrix4    coordTransform  = OMV.Matrix4.Identity;
                OMV.Quaternion coordTransformQ = OMV.Quaternion.Identity;
                if (inst.coordAxis.getUpDimension == CoordAxis.Zup &&
                    newCoords.getUpDimension == CoordAxis.Yup)
                {
                    // The one thing we know to do is change from Zup to Yup
                    coordTransformQ = OMV.Quaternion.CreateFromAxisAngle(1.0f, 0.0f, 0.0f, -(float)Math.PI / 2f);
                    // Make a clean matrix version.
                    // The libraries tend to create matrices with small numbers (1.119093e-07) for zero.
                    coordTransform = new OMV.Matrix4(
                        1, 0, 0, 0,
                        0, 0, -1, 0,
                        0, 1, 0, 0,
                        0, 0, 0, 1);
                }

                OMV.Vector3    oldPos = inst.Position; // DEBUG DEBUG
                OMV.Quaternion oldRot = inst.Rotation; // DEBUG DEBUG
                // Fix the location in space
                inst.Position = inst.Position * coordTransformQ;
                inst.Rotation = coordTransformQ * inst.Rotation;

                inst.coordAxis = newCoords;
                // ConvOAR.Globals.log.DebugFormat("{0} FixCoordinates. dispID={1}, oldPos={2}, newPos={3}, oldRot={4}, newRot={5}",
                //     _logHeader, inst.handle, oldPos, inst.Position, oldRot, inst.Rotation);

                // Go through all the vertices and change the UV coords if necessary
                List <MeshInfo> meshInfos = CollectMeshesFromDisplayable(inst.Representation);

                meshInfos.ForEach(meshInfo => {
                    if (meshInfo.coordAxis.getUVOrigin != newCoords.getUVOrigin)
                    {
                        for (int ii = 0; ii < meshInfo.vertexs.Count; ii++)
                        {
                            var vert             = meshInfo.vertexs[ii];
                            vert.TexCoord.Y      = 1f - vert.TexCoord.Y;
                            meshInfo.vertexs[ii] = vert;
                        }
                        meshInfo.coordAxis = newCoords;
                    }
                });
            }
            else
            {
                ConvOAR.Globals.log.DebugFormat("FixCoordinates. Not converting coord system. dispID={0}", inst.handle);
            }
        }
예제 #2
0
 public static void SimpleOutputValue(StreamWriter outt, Object val, int level)
 {
     if (val == null)
     {
         ConvOAR.Globals.log.ErrorFormat("SimpleJSONOutput: called with NULL value");
         val = "null";
     }
     if (val is string)
     {
         // escape any double quotes in the string value
         outt.Write("\"" + ((string)val).Replace("\"", "\\\"") + "\"");
     }
     else if (val is bool)
     {
         outt.Write((bool)val ? "true" : "false");
     }
     else if (val is OMV.Color4)
     {
         OMV.Color4 col = (OMV.Color4)val;
         outt.Write(ParamsToJSONArray(col.R, col.G, col.B, col.A));
     }
     else if (val is OMV.Matrix4)
     {
         OMV.Matrix4 mat = (OMV.Matrix4)val;
         outt.Write(ParamsToJSONArray(
                        mat[0, 0], mat[0, 1], mat[0, 2], mat[0, 3],
                        mat[1, 0], mat[1, 1], mat[1, 2], mat[1, 3],
                        mat[2, 0], mat[2, 1], mat[2, 2], mat[2, 3],
                        mat[3, 0], mat[3, 1], mat[3, 2], mat[3, 3]
                        ));
     }
     else if (val is OMV.Vector3)
     {
         OMV.Vector3 vect = (OMV.Vector3)val;
         outt.Write(ParamsToJSONArray(vect.X, vect.Y, vect.Z));
     }
     else if (val is OMV.Quaternion)
     {
         OMV.Quaternion quan = (OMV.Quaternion)val;
         outt.Write(ParamsToJSONArray(quan.X, quan.Y, quan.Z, quan.W));
     }
     // else if (val.GetType().IsArray) {
     else if (val is Array)
     {
         outt.Write(" [ ");
         // Object[] values = (Object[])val;
         Array values = val as Array;
         bool  first  = true;
         for (int ii = 0; ii < values.Length; ii++)
         {
             if (!first)
             {
                 outt.Write(",");
             }
             first = false;
             SimpleOutputValue(outt, values.GetValue(ii), level + 1);
         }
         outt.Write(" ]");
     }
     else if (val is Dictionary <string, Object> )
     {
         Dictionary <string, Object> dict = (Dictionary <string, Object>)val;
         outt.Write(" { ");
         bool first = true;
         foreach (var key in dict.Keys)
         {
             if (!first)
             {
                 outt.Write(",");
             }
             first = false;
             outt.Write("\n" + Indent(level) + "\"" + key + "\": ");
             SimpleOutputValue(outt, dict[key], level + 1);
         }
         outt.Write("\n" + Indent(level) + " }");
     }
     else if (val is float && Single.IsNaN((float)val))
     {
         ConvOAR.Globals.log.ErrorFormat("JSONHelpers: Value is Single.NaN!!");
         outt.Write("0");
     }
     else if (val is double && Double.IsNaN((double)val))
     {
         ConvOAR.Globals.log.ErrorFormat("JSONHelpers: Value is Double.NaN!!");
         outt.Write("0");
     }
     else
     {
         var ret = val.ToString();
         if (ret == "NaN")
         {
             ConvOAR.Globals.log.ErrorFormat("JSONHelpers: Value is NaN!!");
         }
         else
         {
             outt.Write(val);
         }
     }
 }