protected override void OnRender() { Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.LightGray, 1.0f, 0); Device.BeginScene(); Device.SetTransform(TransformState.View, Freelook.View); foreach (RigidBody body in PhysicsContext.World.CollisionObjectArray) { Device.SetTransform(TransformState.World, body.WorldTransform); RenderWithMaterial(body); if (Physics.HasDistanceResult) { Device.Material = ActiveMaterial; Device.SetTransform(TransformState.World, Matrix.Identity); PositionColored[] vertices = new PositionColored[2]; vertices[0] = new PositionColored(Physics.distanceFrom, -1); vertices[1] = new PositionColored(Physics.distanceTo, -1); Device.DrawUserPrimitives(PrimitiveType.LineList, 1, vertices); } } DebugDrawWorld(); Fps.Text = string.Format( "Move using mouse and WASD+shift\n" + "F3 - Toggle debug\n" + "F11 - Toggle fullscreen\n" + "Distance: {0}", Physics.distance.ToString("0.00")); Fps.OnRender(FramesPerSecond); Device.EndScene(); Device.Present(); }
public static void DrawLine(Vector3 from, Vector3 to, System.Drawing.Color color) { var vertices = new PositionColored[2]; vertices[0] = new PositionColored(Vector3.Zero, color.ToArgb()); from = from.SwitchYZ(); to = to.SwitchYZ(); vertices[1] = new PositionColored(to - from, color.ToArgb()); InternalRender(from); Drawing.Direct3DDevice.DrawUserPrimitives(PrimitiveType.LineList, vertices.Length / 2, vertices); }
public static void DrawLine(Vector3 from, Vector3 to, System.Drawing.Color color) { var vertices = new PositionColored[2]; vertices[0] = new PositionColored(Vector3.Zero, color.ToArgb()); from = from.LSSwitchYZ(); to = to.LSSwitchYZ(); vertices[1] = new PositionColored(to - from, color.ToArgb()); InternalRender(from); Drawing.Direct3DDevice.DrawUserPrimitives(PrimitiveType.LineList, vertices.Length / 2, vertices); }
public void RenderSoftBody(SoftBody softBody) { Cull cullMode = device.GetRenderState <Cull>(RenderState.CullMode); device.SetRenderState(RenderState.CullMode, Cull.None); AlignedFaceArray faces = softBody.Faces; int faceCount = faces.Count; if (faceCount > 0) { PositionedNormal[] vectors = new PositionedNormal[faceCount * 6]; int v = 0; int i; for (i = 0; i < faceCount; i++) { NodePtrArray nodes = faces[i].N; Node n0 = nodes[0]; Node n1 = nodes[1]; Node n2 = nodes[2]; n0.GetX(out vectors[v].Position); n0.GetNormal(out vectors[v].Normal); n1.GetX(out vectors[v + 1].Position); n1.GetNormal(out vectors[v + 1].Normal); n2.GetX(out vectors[v + 2].Position); n2.GetNormal(out vectors[v + 2].Normal); v += 3; } device.VertexFormat = VertexFormat.PositionNormal; device.DrawUserPrimitives(PrimitiveType.TriangleList, faces.Count, vectors); } else { AlignedTetraArray tetras = softBody.Tetras; int tetraCount = tetras.Count; if (tetraCount > 0) { PositionedNormal[] vectors = new PositionedNormal[tetraCount * 12]; int v = 0; for (int i = 0; i < tetraCount; i++) { NodePtrArray nodes = tetras[i].Nodes; BulletSharp.Vector3 v0 = nodes[0].X; BulletSharp.Vector3 v1 = nodes[1].X; BulletSharp.Vector3 v2 = nodes[2].X; BulletSharp.Vector3 v3 = nodes[3].X; BulletSharp.Vector3 v10 = v1 - v0; BulletSharp.Vector3 v02 = v0 - v2; BulletSharp.Vector3 normal = BulletSharp.Vector3.Cross(v10, v02); normal.Normalize(); vectors[v].Position = v0; vectors[v].Normal = normal; vectors[v + 1].Position = v1; vectors[v + 1].Normal = normal; vectors[v + 2].Position = v2; vectors[v + 2].Normal = normal; normal = BulletSharp.Vector3.Cross(v10, v3 - v0); normal.Normalize(); vectors[v + 3].Position = v0; vectors[v + 3].Normal = normal; vectors[v + 4].Position = v1; vectors[v + 4].Normal = normal; vectors[v + 5].Position = v3; vectors[v + 5].Normal = normal; normal = BulletSharp.Vector3.Cross(v2 - v1, v3 - v1); normal.Normalize(); vectors[v + 6].Position = v1; vectors[v + 6].Normal = normal; vectors[v + 7].Position = v2; vectors[v + 7].Normal = normal; vectors[v + 8].Position = v3; vectors[v + 8].Normal = normal; normal = BulletSharp.Vector3.Cross(v02, v3 - v2); normal.Normalize(); vectors[v + 9].Position = v2; vectors[v + 9].Normal = normal; vectors[v + 10].Position = v0; vectors[v + 10].Normal = normal; vectors[v + 11].Position = v3; vectors[v + 11].Normal = normal; v += 12; } device.VertexFormat = VertexFormat.PositionNormal; device.DrawUserPrimitives(PrimitiveType.TriangleList, tetraCount * 4, vectors); } else if (softBody.Links.Count > 0) { AlignedLinkArray links = softBody.Links; int linkCount = links.Count; int linkColor = System.Drawing.Color.Black.ToArgb(); device.VertexFormat = VertexFormat.Position | VertexFormat.Diffuse; PositionColored[] linkArray = new PositionColored[linkCount * 2]; for (int i = 0; i < linkCount; i++) { Link link = links[i]; linkArray[i * 2].Position = link.Nodes[0].X; linkArray[i * 2].Color = linkColor; linkArray[i * 2 + 1].Position = link.Nodes[1].X; linkArray[i * 2 + 1].Color = linkColor; } device.DrawUserPrimitives(PrimitiveType.LineList, links.Count, linkArray); } } device.SetRenderState(RenderState.CullMode, cullMode); }
void InitLineBuffer(RenderContext renderContext) { if (renderContext.gl != null) { if (lineBuffers.Count == 0) { int count = linePoints.Count; PositionColoredVertexBuffer lineBuffer = null; PositionColored[] linePointList = null; localCenter = new Vector3d(); if (DepthBuffered) { // compute the local center.. foreach (Vector3d point in linePoints) { localCenter.Add(point); } localCenter.X /= count; localCenter.Y /= count; localCenter.Z /= count; } int countLeft = count; int index = 0; int counter = 0; Vector3d temp; foreach (Vector3d point in linePoints) { if (counter >= 100000 || linePointList == null) { if (lineBuffer != null) { lineBuffer.Unlock(); } int thisCount = Math.Min(100000, countLeft); countLeft -= thisCount; lineBuffer = new PositionColoredVertexBuffer(thisCount); linePointList = lineBuffer.Lock(); // Lock the buffer (which will return our structs) lineBuffers.Add(lineBuffer); lineBufferCounts.Add(thisCount); counter = 0; } if (UseLocalCenters) { temp = Vector3d.SubtractVectors(point, localCenter); linePointList[counter] = new PositionColored(temp, lineColors[index]); } else { linePointList[counter] = new PositionColored(point, lineColors[index]); } index++; counter++; } if (lineBuffer != null) { lineBuffer.Unlock(); } } } }
public void RenderSoftBody(SoftBody softBody) { Cull cullMode = device.GetRenderState<Cull>(RenderState.CullMode); device.SetRenderState(RenderState.CullMode, Cull.None); AlignedFaceArray faces = softBody.Faces; int faceCount = faces.Count; if (faceCount > 0) { PositionedNormal[] vectors = new PositionedNormal[faceCount * 6]; int v = 0; int i; for (i = 0; i < faceCount; i++) { NodePtrArray nodes = faces[i].N; Node n0 = nodes[0]; Node n1 = nodes[1]; Node n2 = nodes[2]; n0.GetX(out vectors[v].Position); n0.GetNormal(out vectors[v].Normal); n1.GetX(out vectors[v + 1].Position); n1.GetNormal(out vectors[v + 1].Normal); n2.GetX(out vectors[v + 2].Position); n2.GetNormal(out vectors[v + 2].Normal); v += 3; } device.VertexFormat = VertexFormat.PositionNormal; device.DrawUserPrimitives(PrimitiveType.TriangleList, faces.Count, vectors); } else { AlignedTetraArray tetras = softBody.Tetras; int tetraCount = tetras.Count; if (tetraCount > 0) { PositionedNormal[] vectors = new PositionedNormal[tetraCount * 12]; int v = 0; for (int i = 0; i < tetraCount; i++) { NodePtrArray nodes = tetras[i].Nodes; BulletSharp.Vector3 v0 = nodes[0].X; BulletSharp.Vector3 v1 = nodes[1].X; BulletSharp.Vector3 v2 = nodes[2].X; BulletSharp.Vector3 v3 = nodes[3].X; BulletSharp.Vector3 v10 = v1 - v0; BulletSharp.Vector3 v02 = v0 - v2; BulletSharp.Vector3 normal = BulletSharp.Vector3.Cross(v10, v02); normal.Normalize(); vectors[v].Position = v0; vectors[v].Normal = normal; vectors[v + 1].Position = v1; vectors[v + 1].Normal = normal; vectors[v + 2].Position = v2; vectors[v + 2].Normal = normal; normal = BulletSharp.Vector3.Cross(v10, v3 - v0); normal.Normalize(); vectors[v + 3].Position = v0; vectors[v + 3].Normal = normal; vectors[v + 4].Position = v1; vectors[v + 4].Normal = normal; vectors[v + 5].Position = v3; vectors[v + 5].Normal = normal; normal = BulletSharp.Vector3.Cross(v2 - v1, v3 - v1); normal.Normalize(); vectors[v + 6].Position = v1; vectors[v + 6].Normal = normal; vectors[v + 7].Position = v2; vectors[v + 7].Normal = normal; vectors[v + 8].Position = v3; vectors[v + 8].Normal = normal; normal = BulletSharp.Vector3.Cross(v02, v3 - v2); normal.Normalize(); vectors[v + 9].Position = v2; vectors[v + 9].Normal = normal; vectors[v + 10].Position = v0; vectors[v + 10].Normal = normal; vectors[v + 11].Position = v3; vectors[v + 11].Normal = normal; v += 12; } device.VertexFormat = VertexFormat.PositionNormal; device.DrawUserPrimitives(PrimitiveType.TriangleList, tetraCount * 4, vectors); } else if (softBody.Links.Count > 0) { AlignedLinkArray links = softBody.Links; int linkCount = links.Count; int linkColor = System.Drawing.Color.Black.ToArgb(); device.VertexFormat = VertexFormat.Position | VertexFormat.Diffuse; PositionColored[] linkArray = new PositionColored[linkCount * 2]; for (int i = 0; i < linkCount; i++) { Link link = links[i]; linkArray[i * 2].Position = link.Nodes[0].X; linkArray[i * 2].Color = linkColor; linkArray[i * 2 + 1].Position = link.Nodes[1].X; linkArray[i * 2 + 1].Color = linkColor; } device.DrawUserPrimitives(PrimitiveType.LineList, links.Count, linkArray); } } device.SetRenderState(RenderState.CullMode, cullMode); }