Browse Source

Fixed Byte Stride padding.
trying to understand Asset minVersion...

Vicente Penades 6 years ago
parent
commit
e673681dc4

+ 7 - 0
src/Shared/_Extensions.cs

@@ -27,6 +27,13 @@ namespace SharpGLTF
             return rest == 0 ? 0 : mult - rest;
             return rest == 0 ? 0 : mult - rest;
         }
         }
 
 
+        internal static int WordPadded(this int length)
+        {
+            var padding = (length & 3);
+
+            return length + (padding == 0 ? 0 : 4 - padding);
+        }
+
         internal static bool _IsReal(this float value)
         internal static bool _IsReal(this float value)
         {
         {
             return !(float.IsNaN(value) | float.IsInfinity(value));
             return !(float.IsNaN(value) | float.IsInfinity(value));

+ 1 - 1
src/SharpGLTF.Core/Memory/MemoryAccessor.cs

@@ -93,7 +93,7 @@ namespace SharpGLTF.Memory
         /// <summary>
         /// <summary>
         /// Gets the number of bytes of the current encoded Item, padded to 4 bytes.
         /// Gets the number of bytes of the current encoded Item, padded to 4 bytes.
         /// </summary>
         /// </summary>
-        public int PaddedByteLength => (this.Dimensions.DimCount() * this.Encoding.ByteLength()).PaddingSize(4);
+        public int PaddedByteLength => (this.Dimensions.DimCount() * this.Encoding.ByteLength()).WordPadded();
 
 
         public Boolean IsValidVertexAttribute
         public Boolean IsValidVertexAttribute
         {
         {

+ 14 - 7
src/SharpGLTF.Core/Schema2/gltf.Accessors.cs

@@ -290,16 +290,23 @@ namespace SharpGLTF.Schema2
 
 
         public void UpdateBounds()
         public void UpdateBounds()
         {
         {
-            var dimensions = this._type.DimCount();
-
-            var bounds = new double[dimensions];
             this._min.Clear();
             this._min.Clear();
-            this._min.AddRange(bounds);
             this._max.Clear();
             this._max.Clear();
-            this._max.AddRange(bounds);
 
 
-            this._min.Fill(double.MaxValue);
-            this._max.Fill(double.MinValue);
+            if (this.Count == 0) return;
+
+            // With the current limitations of the serializer, we can only handle floating point values.
+            if (this.Encoding != EncodingType.FLOAT) return;
+
+            // https://github.com/KhronosGroup/glTF-Validator/issues/79
+
+            var dimensions = this._type.DimCount();
+
+            for (int i = 0; i < dimensions; ++i)
+            {
+                this._min.Add(double.MaxValue);
+                this._max.Add(double.MinValue);
+            }
 
 
             var array = new MultiArray(this.SourceBufferView.Content, this.ByteOffset, this.Count, this.SourceBufferView.ByteStride, dimensions, this.Encoding, false);
             var array = new MultiArray(this.SourceBufferView.Content, this.ByteOffset, this.Count, this.SourceBufferView.ByteStride, dimensions, this.Encoding, false);
 
 

+ 5 - 6
src/SharpGLTF.Core/Schema2/gltf.Asset.cs

@@ -23,8 +23,8 @@ namespace SharpGLTF.Schema2
             {
             {
                 _generator = generator,
                 _generator = generator,
                 _copyright = copyright,
                 _copyright = copyright,
-                _version = MAXVERSION.ToString(),
-                _minVersion = MINVERSION.ToString()
+                _version = MINVERSION.ToString(),
+                _minVersion = null
             };
             };
         }
         }
 
 
@@ -46,13 +46,13 @@ namespace SharpGLTF.Schema2
 
 
         private static readonly Version ZEROVERSION = new Version(0, 0);
         private static readonly Version ZEROVERSION = new Version(0, 0);
         private static readonly Version MINVERSION = new Version(2, 0);
         private static readonly Version MINVERSION = new Version(2, 0);
-        private static readonly Version MAXVERSION = new Version(2, 0);
 
 
         public string Copyright { get => _copyright; set => _copyright = value.AsEmptyNullable(); }
         public string Copyright { get => _copyright; set => _copyright = value.AsEmptyNullable(); }
         public string Generator { get => _generator; set => _generator = value.AsEmptyNullable(); }
         public string Generator { get => _generator; set => _generator = value.AsEmptyNullable(); }
 
 
         public Version Version      => Version.TryParse(   _version, out Version ver) ? ver : ZEROVERSION;
         public Version Version      => Version.TryParse(   _version, out Version ver) ? ver : ZEROVERSION;
-        public Version MinVersion   => Version.TryParse(_minVersion, out Version ver) ? ver : ZEROVERSION;
+
+        public Version MinVersion   => Version.TryParse(_minVersion, out Version ver) ? ver : MINVERSION;
 
 
         #endregion
         #endregion
 
 
@@ -77,8 +77,7 @@ namespace SharpGLTF.Schema2
             var curVer = this.Version;
             var curVer = this.Version;
             var minVer = this.MinVersion;
             var minVer = this.MinVersion;
 
 
-            if (curVer < MINVERSION) result.Add(new EXCEPTION(this, $"invalid version number {this.Version} expected {MINVERSION}"));
-            if (curVer > MAXVERSION) result.Add(new EXCEPTION(this, $"invalid version number {this.Version} expected {MAXVERSION}"));
+            if (curVer < minVer) result.Add(new EXCEPTION(this, $"invalid version number {this.Version} expected {MINVERSION}"));
         }
         }
 
 
         private string _GetExtraInfo(string key)
         private string _GetExtraInfo(string key)

+ 17 - 0
tests/SharpGLTF.Tests/ExtensionsTests.cs

@@ -12,6 +12,23 @@ namespace SharpGLTF
     [TestFixture]
     [TestFixture]
     public class ExtensionsTests
     public class ExtensionsTests
     {
     {
+        internal static int _WordPadded(int length)
+        {
+            var padding = (length & 3);
+
+            return length + (padding == 0 ? 0 : 4 - padding);
+        }
+
+        [Test]
+        public void Padding()
+        {
+            Assert.AreEqual(4, _WordPadded(1));
+            Assert.AreEqual(4, _WordPadded(2));
+            Assert.AreEqual(4, _WordPadded(3));
+            Assert.AreEqual(4, _WordPadded(4));
+            Assert.AreEqual(8, _WordPadded(5));
+        }
+
         [Test]
         [Test]
         public void TestAsNullableExtensions()
         public void TestAsNullableExtensions()
         {
         {