Skip to content

Commit

Permalink
several updates. This may no longer work on BigEndian CPUs
Browse files Browse the repository at this point in the history
  • Loading branch information
UbitUmarov committed Feb 7, 2024
1 parent 708053f commit 3f705d1
Show file tree
Hide file tree
Showing 12 changed files with 978 additions and 690 deletions.
6 changes: 6 additions & 0 deletions OpenMetaverse.StructuredData/LLSD/NotationLLSD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,9 @@ private static void SerializeLLSDNotationElement(TextWriter writer, OSD osd)
EscapeCharacter(osd.AsString(), singleQuotesNotationMarker, writer);
writer.Write(singleQuotesNotationMarker);
break;
case OSDType.LLSDxml:
writer.Write(osd.AsString());
break;
case OSDType.Binary:
writer.Write(binaryNotationMarker);
writer.Write("64");
Expand Down Expand Up @@ -608,6 +611,9 @@ private static void SerializeLLSDNotationElementFormatted(TextWriter writer, str
EscapeCharacter(osd.AsString(), singleQuotesNotationMarker, writer);
writer.Write(singleQuotesNotationMarker);
break;
case OSDType.LLSDxml:
writer.Write(osd.AsString());
break;
case OSDType.Binary:
writer.Write(binaryNotationMarker);
writer.Write("64");
Expand Down
6 changes: 3 additions & 3 deletions OpenMetaverse.StructuredData/StructuredData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ public virtual string AsString()
case OSDType.Integer:
return ((OSDInteger)this).value.ToString();
case OSDType.Real:
return ((OSDReal)this).value.ToString("r", Utils.EnUsCulture);
return ((OSDReal)this).value.ToString("g", Utils.EnUsCulture);
case OSDType.String:
return ((OSDString)this).value;
case OSDType.OSDUTF8:
Expand Down Expand Up @@ -695,7 +695,7 @@ public override string ToString()
case OSDType.Integer:
return ((OSDInteger)this).value.ToString();
case OSDType.Real:
return ((OSDReal)this).value.ToString("r", Utils.EnUsCulture);
return ((OSDReal)this).value.ToString("g", Utils.EnUsCulture);
case OSDType.String:
return ((OSDString)this).value;
case OSDType.OSDUTF8:
Expand Down Expand Up @@ -1206,7 +1206,7 @@ public override ulong AsULong()
public override double AsReal() { return value; }
// "r" ensures the value will correctly round-trip back through Double.TryParse
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override string AsString() { return value.ToString("r", Utils.EnUsCulture); }
public override string AsString() { return value.ToString("g", Utils.EnUsCulture); }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override byte[] AsBinary() { return Utils.DoubleToBytesBig(value); }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
2 changes: 1 addition & 1 deletion OpenMetaverse/CapsToPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private static object ParseLLSDBlock(OSDMap blockData, Type blockType)
}
else if (fieldType == typeof(byte[]) && blockData[field.Name].Type == OSDType.String)
{
field.SetValue(block, Utils.StringToBytes(blockData[field.Name]));
field.SetValue(block, Utils.StringToBytes(blockData[field.Name].AsString()));
}
}
}
Expand Down
170 changes: 80 additions & 90 deletions OpenMetaverse/Rendering/Rendering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,119 +289,109 @@ public static bool TryDecodeFromAsset(Primitive prim, AssetMesh meshAsset, Detai
break;
}

if (facesOSD == null || !(facesOSD is OSDArray))
{
if (facesOSD is not OSDArray decodedMeshOsdArray)
return false;
}

OSDArray decodedMeshOsdArray = (OSDArray)facesOSD;

for (int faceNr = 0; faceNr < decodedMeshOsdArray.Count; faceNr++)
{
OSD subMeshOsd = decodedMeshOsdArray[faceNr];
if(decodedMeshOsdArray[faceNr] is not OSDMap subMeshMap)
continue;

// Decode each individual face
if (subMeshOsd is OSDMap)
{
Face oface = new Face();
oface.ID = faceNr;
oface.Vertices = new List<Vertex>();
oface.Indices = new List<ushort>();
oface.TextureFace = prim.Textures.GetFace((uint)faceNr);

OSDMap subMeshMap = (OSDMap)subMeshOsd;

Vector3 posMax;
Vector3 posMin;
Face oface = new Face();
oface.ID = faceNr;
oface.Vertices = new List<Vertex>();
oface.Indices = new List<ushort>();
oface.TextureFace = prim.Textures.GetFace((uint)faceNr);

// If PositionDomain is not specified, the default is from -0.5 to 0.5
if (subMeshMap.ContainsKey("PositionDomain"))
{
posMax = ((OSDMap)subMeshMap["PositionDomain"])["Max"];
posMin = ((OSDMap)subMeshMap["PositionDomain"])["Min"];
}
else
{
posMax = new Vector3(0.5f, 0.5f, 0.5f);
posMin = new Vector3(-0.5f, -0.5f, -0.5f);
}
Vector3 posMax;
Vector3 posMin;

// Vertex positions
byte[] posBytes = subMeshMap["Position"];
// If PositionDomain is not specified, the default is from -0.5 to 0.5
if (subMeshMap.TryGetValue("PositionDomain", out OSD osdpd) && osdpd is OSDMap mappd)
{
posMax = mappd["Max"];
posMin = mappd["Min"];
}
else
{
posMax = new Vector3(0.5f, 0.5f, 0.5f);
posMin = new Vector3(-0.5f, -0.5f, -0.5f);
}

// Normals
byte[] norBytes = null;
if (subMeshMap.ContainsKey("Normal"))
{
norBytes = subMeshMap["Normal"];
}
// Vertex positions
byte[] posBytes = subMeshMap["Position"];

// UV texture map
Vector2 texPosMax = Vector2.Zero;
Vector2 texPosMin = Vector2.Zero;
byte[] texBytes = null;
if (subMeshMap.ContainsKey("TexCoord0"))
{
texBytes = subMeshMap["TexCoord0"];
texPosMax = ((OSDMap)subMeshMap["TexCoord0Domain"])["Max"];
texPosMin = ((OSDMap)subMeshMap["TexCoord0Domain"])["Min"];
}
// Normals
byte[] norBytes = null;
if (subMeshMap.TryGetValue("Normal", out OSD osdNormal))
{
norBytes = osdNormal;
}

// Extract the vertex position data
// If present normals and texture coordinates too
for (int i = 0; i < posBytes.Length; i += 6)
{
ushort uX = Utils.BytesToUInt16(posBytes, i);
ushort uY = Utils.BytesToUInt16(posBytes, i + 2);
ushort uZ = Utils.BytesToUInt16(posBytes, i + 4);
// UV texture map
Vector2 texPosMax = Vector2.Zero;
Vector2 texPosMin = Vector2.Zero;
byte[] texBytes = null;
if (subMeshMap.TryGetValue("TexCoord0", out OSD osdtcord0))
{
texBytes = osdtcord0;
texPosMax = ((OSDMap)subMeshMap["TexCoord0Domain"])["Max"];
texPosMin = ((OSDMap)subMeshMap["TexCoord0Domain"])["Min"];
}

Vertex vx = new Vertex();
// Extract the vertex position data
// If present normals and texture coordinates too
for (int i = 0; i < posBytes.Length; i += 6)
{
ushort uX = Utils.BytesToUInt16(posBytes, i);
ushort uY = Utils.BytesToUInt16(posBytes, i + 2);
ushort uZ = Utils.BytesToUInt16(posBytes, i + 4);

vx.Position = new Vector3(
Utils.UInt16ToFloat(uX, posMin.X, posMax.X),
Utils.UInt16ToFloat(uY, posMin.Y, posMax.Y),
Utils.UInt16ToFloat(uZ, posMin.Z, posMax.Z));
Vertex vx = new Vertex();

if (norBytes != null && norBytes.Length >= i + 4)
{
ushort nX = Utils.BytesToUInt16(norBytes, i);
ushort nY = Utils.BytesToUInt16(norBytes, i + 2);
ushort nZ = Utils.BytesToUInt16(norBytes, i + 4);

vx.Normal = new Vector3(
Utils.UInt16ToFloat(nX, posMin.X, posMax.X),
Utils.UInt16ToFloat(nY, posMin.Y, posMax.Y),
Utils.UInt16ToFloat(nZ, posMin.Z, posMax.Z));
}
vx.Position = new Vector3(
Utils.UInt16ToFloat(uX, posMin.X, posMax.X),
Utils.UInt16ToFloat(uY, posMin.Y, posMax.Y),
Utils.UInt16ToFloat(uZ, posMin.Z, posMax.Z));

var vertexIndexOffset = oface.Vertices.Count * 4;
if (norBytes is not null && norBytes.Length >= i + 4)
{
ushort nX = Utils.BytesToUInt16(norBytes, i);
ushort nY = Utils.BytesToUInt16(norBytes, i + 2);
ushort nZ = Utils.BytesToUInt16(norBytes, i + 4);

vx.Normal = new Vector3(
Utils.UInt16ToFloat(nX, posMin.X, posMax.X),
Utils.UInt16ToFloat(nY, posMin.Y, posMax.Y),
Utils.UInt16ToFloat(nZ, posMin.Z, posMax.Z));
}

if (texBytes != null && texBytes.Length >= vertexIndexOffset + 4)
{
ushort tX = Utils.BytesToUInt16(texBytes, vertexIndexOffset);
ushort tY = Utils.BytesToUInt16(texBytes, vertexIndexOffset + 2);
var vertexIndexOffset = oface.Vertices.Count * 4;

vx.TexCoord = new Vector2(
Utils.UInt16ToFloat(tX, texPosMin.X, texPosMax.X),
Utils.UInt16ToFloat(tY, texPosMin.Y, texPosMax.Y));
}
if (texBytes is not null && texBytes.Length >= vertexIndexOffset + 4)
{
ushort tX = Utils.BytesToUInt16(texBytes, vertexIndexOffset);
ushort tY = Utils.BytesToUInt16(texBytes, vertexIndexOffset + 2);

oface.Vertices.Add(vx);
vx.TexCoord = new Vector2(
Utils.UInt16ToFloat(tX, texPosMin.X, texPosMax.X),
Utils.UInt16ToFloat(tY, texPosMin.Y, texPosMax.Y));
}

byte[] triangleBytes = subMeshMap["TriangleList"];
for (int i = 0; i < triangleBytes.Length; i += 6)
{
ushort v1 = (ushort)(Utils.BytesToUInt16(triangleBytes, i));
oface.Indices.Add(v1);
ushort v2 = (ushort)(Utils.BytesToUInt16(triangleBytes, i + 2));
oface.Indices.Add(v2);
ushort v3 = (ushort)(Utils.BytesToUInt16(triangleBytes, i + 4));
oface.Indices.Add(v3);
}
oface.Vertices.Add(vx);
}

mesh.Faces.Add(oface);
byte[] triangleBytes = subMeshMap["TriangleList"];
for (int i = 0; i < triangleBytes.Length; i += 6)
{
oface.Indices.Add(Utils.BytesToUInt16(triangleBytes, i));
oface.Indices.Add(Utils.BytesToUInt16(triangleBytes, i + 2));
oface.Indices.Add(Utils.BytesToUInt16(triangleBytes, i + 4));
}

mesh.Faces.Add(oface);
}

}
Expand Down
2 changes: 1 addition & 1 deletion OpenMetaverseTypes/Matrix3x3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public static Matrix3x3 CreateFromEulers(float roll, float pitch, float yaw)

public static Matrix3x3 CreateFromQuaternion(Quaternion rot)
{
float x2 = rot.X + rot.X;
float x2 = rot.X + rot.X;
float y2 = rot.Y + rot.Y;
float z2 = rot.Z + rot.Z;

Expand Down
Loading

0 comments on commit 3f705d1

Please sign in to comment.