public static Bounds CalculateBounds(IModel model, BoneTransformManager boneTransformManager) { var minX = float.MaxValue; var minY = float.MaxValue; var minZ = float.MaxValue; var maxX = float.MinValue; var maxY = float.MinValue; var maxZ = float.MinValue; var position = new ModelImpl.PositionImpl(); var anyVertices = false; foreach (var mesh in model.Skin.Meshes) { foreach (var primitive in mesh.Primitives) { foreach (var vertex in primitive.Vertices) { anyVertices = true; boneTransformManager.ProjectVertex(vertex, position); var x = position.X; var y = position.Y; var z = position.Z; minX = MathF.Min(minX, x); maxX = MathF.Max(maxX, x); minY = MathF.Min(minY, y); maxY = MathF.Max(maxY, y); minZ = MathF.Min(minZ, z); maxZ = MathF.Max(maxZ, z); } } } if (!anyVertices) { var boneQueue = new Queue <IBone>(); boneQueue.Enqueue(model.Skeleton.Root); while (boneQueue.Count > 0) { var bone = boneQueue.Dequeue(); var x = 0d; var y = 0d; var z = 0d; boneTransformManager.ProjectVertex(bone, ref x, ref y, ref z); minX = MathF.Min(minX, (float)x); maxX = MathF.Max(maxX, (float)x); minY = MathF.Min(minY, (float)y); maxY = MathF.Max(maxY, (float)y); minZ = MathF.Min(minZ, (float)z); maxZ = MathF.Max(maxZ, (float)z); foreach (var child in bone.Children) { boneQueue.Enqueue(child); } } } return(new Bounds(minX, minY, minZ, maxX, maxY, maxZ)); }
public void UpdateTransforms(BoneTransformManager boneTransformManager) { for (var i = 0; i < this.vertices_.Count; ++i) { var vertex = this.vertices_[i]; boneTransformManager.ProjectVertex( vertex, this.position_, this.normal_, true); var positionOffset = POSITION_SIZE_ * i; this.positionData_[positionOffset + 0] = this.position_.X; this.positionData_[positionOffset + 1] = this.position_.Y; this.positionData_[positionOffset + 2] = this.position_.Z; var normalOffset = NORMAL_SIZE_ * i; this.normalData_[normalOffset + 0] = this.normal_.X; this.normalData_[normalOffset + 1] = this.normal_.Y; this.normalData_[normalOffset + 2] = this.normal_.Z; for (var u = 0; u < this.uvData_.Length; ++u) { var uv = vertex.GetUv(u); var uvOffset = UV_SIZE_ * i; this.uvData_[u][uvOffset + 0] = uv?.U ?? 0; this.uvData_[u][uvOffset + 1] = uv?.V ?? 0; } } GL.BindVertexArray(this.vaoId_); // Position var vertexAttribPosition = 0; GL.BindBuffer(BufferTarget.ArrayBuffer, this.vboIds_[vertexAttribPosition]); GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(sizeof(float) * this.positionData_.Length), this.positionData_, BufferUsageHint.DynamicDraw); GL.VertexAttribPointer( vertexAttribPosition, POSITION_SIZE_, VertexAttribPointerType.Float, false, 0, 0); GL.EnableVertexAttribArray(vertexAttribPosition); // Normal var vertexAttribNormal = 1; GL.BindBuffer(BufferTarget.ArrayBuffer, this.vboIds_[vertexAttribNormal]); GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(sizeof(float) * this.normalData_.Length), this.normalData_, BufferUsageHint.DynamicDraw); GL.VertexAttribPointer( vertexAttribNormal, NORMAL_SIZE_, VertexAttribPointerType.Float, false, 0, 0); GL.EnableVertexAttribArray(vertexAttribNormal); // Uv for (var i = 0; i < this.uvData_.Length; ++i) { var vertexAttribUv = 2 + i; GL.BindBuffer(BufferTarget.ArrayBuffer, this.vboIds_[vertexAttribUv]); GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(sizeof(float) * this.uvData_[i].Length), this.uvData_[i], BufferUsageHint.DynamicDraw); GL.VertexAttribPointer( vertexAttribUv, UV_SIZE_, VertexAttribPointerType.Float, false, 0, 0); GL.EnableVertexAttribArray(vertexAttribUv); } // Make sure the buffers are not changed by outside code GL.BindVertexArray(0); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); }