public void TestVertexPosition() { Assert.That(VertexPosition.VertexDeclaration.VertexStride, Is.EqualTo(12)); var vertexElements = VertexPosition.VertexDeclaration.GetVertexElements(); Assert.That(vertexElements, Has.Length.EqualTo(1)); Assert.That(vertexElements[0].Offset, Is.EqualTo(0)); Assert.That(vertexElements[0].UsageIndex, Is.EqualTo(0)); Assert.That(vertexElements[0].VertexElementFormat, Is.EqualTo(VertexElementFormat.Vector3)); Assert.That(vertexElements[0].VertexElementUsage, Is.EqualTo(VertexElementUsage.Position)); var vertex1 = new VertexPosition(Vector3.One); var vertex2 = new VertexPosition(Vector3.One); var vertex3 = new VertexPosition(Vector3.Zero); Assert.That(vertex1 == vertex2, Is.True); Assert.That(vertex1 != vertex2, Is.False); Assert.That(vertex1 == vertex3, Is.False); Assert.That(vertex1 != vertex3, Is.True); Assert.That(vertex1.Equals(vertex2), Is.True); Assert.That(vertex1.Equals(vertex3), Is.False); }
public static ModelBoundingBoxBuffer CalcBoundingBoxes(Model scene) { ModelBoundingBoxBuffer ret = new ModelBoundingBoxBuffer(); ret.Model = scene; foreach (ModelMesh mesh in scene.Meshes) { ModelMeshBoundingBoxBuffer meshBox = new ModelMeshBoundingBoxBuffer(); meshBox.Mesh = mesh; ret.MeshList.Add(meshBox); for (int j = 0; j < mesh.MeshParts.Count; j++) { ModelMeshPart part = mesh.MeshParts[j]; Vector3 min = Vector3.One * float.MaxValue; Vector3 max = Vector3.One * float.MinValue; VertexPosition[] vertices = new VertexPosition[part.NumVertices]; //mesh.VertexBuffer.GetData<VertexPosition>(part.BaseVertex * part.VertexStride, vertices, 0, part.NumVertices, part.VertexStride); //mesh.VertexBuffer.GetData<VertexPosition>(part.BaseVertex * part.VertexStride, vertices, 0, part.NumVertices, part.VertexStride); part.VertexBuffer.GetData<VertexPosition>(vertices); for (int x = 0; x < part.NumVertices; x++) { min = Vector3.Min(min, vertices[x].Position); max = Vector3.Max(max, vertices[x].Position); } meshBox.BoundingBoxes.Add(new BoundingBox(min, max)); } } return ret; }
public Line2D(Vector3 p1, Vector3 p2) { vertexBuffer = new VertexBuffer(GraphicsDeviceHolder.Device, sizeof(float) * 6, BufferUsage.None); var data = new VertexPosition[] { new VertexPosition(p1), new VertexPosition(p2) }; vertexBuffer.SetData<VertexPosition>(data); }
public static bool SelectPolyFromModel(Model scene, Matrix sceneTransform, Ray pickRay, out int meshIndex, out int meshPartIndex, out int polyIndex) { meshIndex = -1; polyIndex = -1; meshPartIndex = -1; float fClosestPoly = float.MaxValue; Matrix[] transforms = new Matrix[scene.Bones.Count]; scene.CopyAbsoluteBoneTransformsTo(transforms); Ray meshSpaceRay = new Ray(); for (int i = 0; i < scene.Meshes.Count; i++) { ModelMesh mesh = scene.Meshes[i]; Matrix inverseSceneTransform = Matrix.Invert(transforms[mesh.ParentBone.Index] * sceneTransform); Vector3 rayPosition = Vector3.Transform(pickRay.Position, inverseSceneTransform); Vector3 rayDirection = Vector3.TransformNormal(pickRay.Direction, inverseSceneTransform); meshSpaceRay.Position = rayPosition; meshSpaceRay.Direction = rayDirection; if (mesh.BoundingSphere.Intersects(meshSpaceRay).HasValue == false) { // continue; } List<collidingPart> collidingParts = new List<collidingPart>(); if (!CollisionUtility.Contains(scene)) { CollisionUtility.ModelBoundingInfo.Add(CalcBoundingBoxes(scene)); } else { foreach (ModelBoundingBoxBuffer mbb in modelBoundingInfo) { for (int j = 0; j < mbb.MeshList[i].BoundingBoxes.Count; j++) { float? f = meshSpaceRay.Intersects(mbb.MeshList[i].BoundingBoxes[j]); if (f.HasValue) { collidingPart cp; cp.MeshIndex = i; cp.MeshPartIndex = j; cp.Distance = f.Value; collidingParts.Add(cp); } } } collidingParts.Sort(); } if (collidingParts.Count == 0) { continue; } if (mesh.MeshParts[0].IndexBuffer.IndexElementSize == IndexElementSize.SixteenBits) { for (int j = 0; j < collidingParts.Count; j++) { collidingPart prt = collidingParts[j]; ModelMeshPart part = mesh.MeshParts[prt.MeshPartIndex]; #if DEBUG_RENDER foreach (ModelBoundingBoxBuffer mbb in modelBoundingInfo) { for (int x = 0; x < mbb.MeshList[i].BoundingBoxes.Count; x++) { if (x == prt.MeshPartIndex) { VectorRenderer.SetColor(Color.Red); VectorRenderer.SetWorldMatrix(transforms[mesh.ParentBone.Index] * sceneTransform); VectorRenderer.Draw(mbb.MeshList[i].BoundingBoxes[prt.MeshPartIndex]); } } } #endif short[] indices = new short[part.PrimitiveCount * 3]; //mesh.IndexBuffer.GetData<short>(part.StartIndex * sizeof(short), indices, 0, part.PrimitiveCount * 3); part.IndexBuffer.GetData<short>(part.StartIndex * sizeof(short), indices, 0, part.PrimitiveCount * 3); VertexPosition[] vertices = new VertexPosition[part.NumVertices]; //mesh.VertexBuffer.GetData<VertexPosition>(part.BaseVertex * part.VertexStride, vertices, 0, part.NumVertices, part.VertexStride); part.VertexBuffer.GetData<CollisionUtility.VertexPosition>(vertices, part.StartIndex, part.NumVertices); for (int x = 0; x < part.PrimitiveCount; x++) { float fDist; if (Intersection.RayTriangleIntersect( meshSpaceRay, vertices[indices[x * 3 + 0]].Position, vertices[indices[x * 3 + 1]].Position, vertices[indices[x * 3 + 2]].Position, out fDist)) { if (fDist < fClosestPoly) { meshIndex = i; meshPartIndex = prt.MeshPartIndex; polyIndex = x; fClosestPoly = fDist; } } } } } else if (mesh.MeshParts[0].IndexBuffer.IndexElementSize == IndexElementSize.ThirtyTwoBits) { for (int j = 0; j < collidingParts.Count; j++) { collidingPart prt = collidingParts[j]; ModelMeshPart part = mesh.MeshParts[prt.MeshPartIndex]; #if DEBUG_RENDER foreach (ModelBoundingBoxBuffer mbb in modelBoundingInfo) { for (int x = 0; x < mbb.MeshList[i].BoundingBoxes.Count; x++) { if (x == prt.MeshPartIndex) { VectorRenderer.SetColor(Color.Red); VectorRenderer.SetWorldMatrix(sceneTransform * transforms[mesh.ParentBone.Index]); VectorRenderer.Draw(mbb.MeshList[i].BoundingBoxes[prt.MeshPartIndex]); } } } #endif int[] indices = new int[part.PrimitiveCount * 3]; //mesh.IndexBuffer.GetData<int>(part.StartIndex * sizeof(int), indices, 0, part.PrimitiveCount * 3); part.IndexBuffer.GetData<int>(part.StartIndex * sizeof(int), indices, 0, part.PrimitiveCount * 3); VertexPosition[] vertices = new VertexPosition[part.NumVertices]; //mesh.VertexBuffer.GetData<VertexPosition>(part.BaseVertex * part.VertexStride, vertices, 0, part.NumVertices, part.VertexStride); part.VertexBuffer.GetData<CollisionUtility.VertexPosition>(vertices, part.StartIndex, part.NumVertices); for (int x = 0; x < part.PrimitiveCount; x++) { float fDist; if (Intersection.RayTriangleIntersect( meshSpaceRay, vertices[indices[x * 3 + 0]].Position, vertices[indices[x * 3 + 1]].Position, vertices[indices[x * 3 + 2]].Position, out fDist)) { if (fDist < fClosestPoly) { meshIndex = i; meshPartIndex = prt.MeshPartIndex; polyIndex = x; fClosestPoly = fDist; } } } } } } return meshIndex != -1 && polyIndex != -1 && meshPartIndex != -1; }
VertexPosition[] generateVerts( Vector2[] points ) { // we need to make tris from the points. all points will be shared with the center (0,0) var verts = new VertexPosition[points.Length + 1]; // the first point is the center so we start at 1 for( var i = 1; i <= points.Length; i++ ) { verts[i].Position.X = points[i - 1].X; verts[i].Position.Y = points[i - 1].Y; } return verts; }