public void InitializeModel(ArcMode mode) { m_IntersectPoint[0] = new CustomVertex.PositionOnly(); m_IntersectPoint[1] = new CustomVertex.PositionOnly(); m_Mode = mode; //Set up rotation handles m_Verts = new CustomVertex.PositionOnly[CircleVertCount + 1]; float dc = 2f * (float)Math.PI / (CircleVertCount - 2); float arc = 0; float a, b; m_Verts[CircleVertCount] = new CustomVertex.PositionOnly(0, 0, 0); for (int i = 0; i < CircleVertCount; i++) { b = (float)(RadiusScale * Math.Cos(arc)); a = (float)(RadiusScale * Math.Sin(arc)); switch (m_Mode) { case ArcMode.Pitch: m_Verts[i] = new CustomVertex.PositionOnly(0, b, a); break; case ArcMode.Roll: m_Verts[i] = new CustomVertex.PositionOnly(a, 0, b); break; case ArcMode.Yaw: m_Verts[i] = new CustomVertex.PositionOnly(b, a, 0); break; } arc += dc; } for (short i = 0; i < CircleVertCount - 1; i++) { m_Arc[i] = i; m_Pie[i * 3] = (short)CircleVertCount; m_Pie[i * 3 + 1] = i; m_Pie[i * 3 + 2] = (short)(i + 1); } m_VertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionOnly), m_Verts.Length, MdxRender.Device, Usage.WriteOnly, CustomVertex.PositionOnly.Format, Microsoft.DirectX.Direct3D.Pool.Default); m_VertexBuffer.Created += new EventHandler(this.OnVertexBufferCreate); OnVertexBufferCreate(m_VertexBuffer, null); m_ArcIndexBuffer = new IndexBuffer(typeof(short), m_Arc.Length, MdxRender.Device, Usage.WriteOnly, Microsoft.DirectX.Direct3D.Pool.Default); m_ArcIndexBuffer.Created += new EventHandler(this.OnArcIndexBufferCreate); OnArcIndexBufferCreate(m_ArcIndexBuffer, null); m_PieIndexBuffer = new IndexBuffer(typeof(short), m_Pie.Length, MdxRender.Device, Usage.WriteOnly, Microsoft.DirectX.Direct3D.Pool.Default); m_PieIndexBuffer.Created += new EventHandler(this.OnPieIndexBufferCreate); OnPieIndexBufferCreate(m_PieIndexBuffer, null); }
public static void EnableExtendedAxes(bool bLongX, bool bLongY, bool bLongZ) { if (bLongX) { m_XAxis[0] = new CustomVertex.PositionOnly(-100, 0, 0); m_XAxis[1] = new CustomVertex.PositionOnly(100, 0, 0); } else { m_XAxis[0] = new CustomVertex.PositionOnly(0.35f * Scale, 0, 0); m_XAxis[1] = new CustomVertex.PositionOnly(Scale, 0, 0); } if (bLongY) { m_YAxis[0] = new CustomVertex.PositionOnly(0, -100, 0); m_YAxis[1] = new CustomVertex.PositionOnly(0, 100, 0); } else { m_YAxis[0] = new CustomVertex.PositionOnly(0, 0.35f * Scale, 0); m_YAxis[1] = new CustomVertex.PositionOnly(0, Scale, 0); } if (bLongZ) { m_ZAxis[0] = new CustomVertex.PositionOnly(0, 0, -100); m_ZAxis[1] = new CustomVertex.PositionOnly(0, 0, 100); } else { m_ZAxis[0] = new CustomVertex.PositionOnly(0, 0, 0.35f * Scale); m_ZAxis[1] = new CustomVertex.PositionOnly(0, 0, Scale); } }
public void DrawNodes() { float node_offset = 0.0f; if (nodeSphere == null) { nodeSphere = Mesh.Sphere(MdxRender.Dev, 0.01f, 6, 6); } if (nodeLocations == null) { nodeLocations = new CustomVertex.PositionOnly[2 * m_Model.m_Nodes.Length]; for (int i = 0; i < 2 * m_Model.m_Nodes.Length; i++) { nodeLocations[i] = new CustomVertex.PositionOnly(); } } Microsoft.DirectX.Matrix mat = new Microsoft.DirectX.Matrix(); MdxRender.SM.m_TextureManager.ActivateDefaultTexture(); MdxRender.Dev.RenderState.ZBufferFunction = Compare.Always; int nc = 0; for (int i = 0; i < m_Model.m_Nodes.Length; i++) { mat = Microsoft.DirectX.Matrix.Identity; mat.Translate(m_Model.NodeTransforms[i].m_FinalNode[0] + node_offset, m_Model.NodeTransforms[i].m_FinalNode[1], m_Model.NodeTransforms[i].m_FinalNode[2]); MdxRender.Dev.Transform.World = mat; nodeSphere.DrawSubset(0); if (m_AniNodes[i].ParentNode != -1) { nodeLocations[2 * nc].X = m_Model.NodeTransforms[i].m_FinalNode[0] + node_offset; nodeLocations[2 * nc].Y = m_Model.NodeTransforms[i].m_FinalNode[1]; nodeLocations[2 * nc].Z = m_Model.NodeTransforms[i].m_FinalNode[2]; nodeLocations[2 * nc + 1].X = m_Model.NodeTransforms[m_AniNodes[i].ParentNode].m_FinalNode[0] + node_offset; nodeLocations[2 * nc + 1].Y = m_Model.NodeTransforms[m_AniNodes[i].ParentNode].m_FinalNode[1]; nodeLocations[2 * nc + 1].Z = m_Model.NodeTransforms[m_AniNodes[i].ParentNode].m_FinalNode[2]; nc++; } } MdxRender.Dev.Transform.World = Microsoft.DirectX.Matrix.Identity; MdxRender.Dev.DrawUserPrimitives(PrimitiveType.LineList, nc, nodeLocations); MdxRender.Dev.RenderState.ZBufferFunction = Compare.LessEqual; }
public Laser() { vertexBuffer = new CustomVertex.PositionOnly[6]; vertexBuffer[0] = new CustomVertex.PositionOnly(-0.1f, 0, 0); vertexBuffer[1] = new CustomVertex.PositionOnly(0.1f, 0, 0); vertexBuffer[2] = new CustomVertex.PositionOnly(-0.1f, -1, 0); vertexBuffer[3] = new CustomVertex.PositionOnly(0.1f, -1, 0); vertexBuffer[4] = new CustomVertex.PositionOnly(-0.1f, -1, 0); vertexBuffer[5] = new CustomVertex.PositionOnly(0.1f, 0, 0); }
private void DebugRenderCutPlane() { float x, y, z; float a = 0, b = 0, c = 0; int angle_start = 0, angle_stop = 0; CustomVertex.PositionOnly[] cross_line = new CustomVertex.PositionOnly[2]; a = -MdxRender.Camera.LookVector.X; b = -MdxRender.Camera.LookVector.Y; c = -MdxRender.Camera.LookVector.Z; //Calculate cut plane intersect points switch (m_Mode) { case ArcMode.Pitch: z = (float)Math.Sqrt((RadiusScale * RadiusScale) / ((c * c) / (b * b) + 1.0)); y = -(c / b) * z; x = 0; cross_line[0] = new CustomVertex.PositionOnly(x, y, z); cross_line[1] = new CustomVertex.PositionOnly(-x, -y, -z); angle_start = (int)((GetQuadrantAngle(y, z) / (Math.PI * 2)) * CircleVertCount - 1); angle_stop = (int)((GetQuadrantAngle(-y, -z) / (Math.PI * 2)) * CircleVertCount - 1); break; case ArcMode.Roll: z = (float)Math.Sqrt((RadiusScale * RadiusScale) / ((c * c) / (a * a) + 1.0)); x = -(c / a) * z; y = 0; cross_line[0] = new CustomVertex.PositionOnly(x, y, z); cross_line[1] = new CustomVertex.PositionOnly(-x, -y, -z); angle_start = (int)((GetQuadrantAngle(x, z) / (Math.PI * 2)) * CircleVertCount - 1); angle_stop = (int)((GetQuadrantAngle(-x, -z) / (Math.PI * 2)) * CircleVertCount - 1); break; case ArcMode.Yaw: y = (float)Math.Sqrt((RadiusScale * RadiusScale) / ((b * b) / (a * a) + 1.0)); x = -(b / a) * y; z = 0; cross_line[0] = new CustomVertex.PositionOnly(x, y, z); cross_line[1] = new CustomVertex.PositionOnly(-x, -y, -z); angle_start = (int)((GetQuadrantAngle(y, x) / (Math.PI * 2)) * CircleVertCount - 1); angle_stop = (int)((GetQuadrantAngle(-y, -x) / (Math.PI * 2)) * CircleVertCount - 1); break; } MdxRender.Device.DrawUserPrimitives(PrimitiveType.LineList, 1, cross_line); // MdxRender.Dev.VertexFormat = CustomVertex.PositionOnly.Format; // MdxRender.Dev.Indices = m_ArcIndexBuffer; // MdxRender.Dev.SetStreamSource(0, m_VertexBuffer, 0); // if(angle_stop > angle_start) // MdxRender.Dev.DrawIndexedPrimitives(PrimitiveType.LineStrip, 0, 0, m_Verts.Length, angle_start*2, angle_stop-angle_start); }
static public void Init(TGCVector3 centro, int nivelTeselado, float escalaXZ, string ShadersDir) { //Inicializar efecto effect = TGCShaders.Instance.LoadEffect(ShadersDir + "agua.fx"); effect.Technique = "RenderScene"; effect.SetValue("time", 0); //Inicializar vertex buffer int nivelTeseladoX = nivelTeselado; int nivelTeseladoZ = nivelTeselado; totalVertices = 6 * nivelTeseladoX * nivelTeseladoZ; vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionOnly), totalVertices, D3DDevice.Instance.Device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionOnly.Format, Pool.Default); //Inicializar y llenar data para meterla en el vertexBuffer int dataIndex = 0; CustomVertex.PositionOnly[] data = new CustomVertex.PositionOnly[totalVertices]; centro.X = centro.X * escalaXZ - nivelTeseladoX / 2 * escalaXZ; centro.Z = centro.Z * escalaXZ - nivelTeseladoZ / 2 * escalaXZ; for (var i = 0; i < nivelTeseladoX; i++) { for (var j = 0; j < nivelTeseladoZ; j++) { //Vertices var v1 = new TGCVector3(centro.X + i * escalaXZ, centro.Y, centro.Z + j * escalaXZ); var v2 = new TGCVector3(centro.X + i * escalaXZ, centro.Y, centro.Z + (j + 1) * escalaXZ); var v3 = new TGCVector3(centro.X + (i + 1) * escalaXZ, centro.Y, centro.Z + j * escalaXZ); var v4 = new TGCVector3(centro.X + (i + 1) * escalaXZ, centro.Y, centro.Z + (j + 1) * escalaXZ); //Cargar triangulo 1 data[dataIndex] = new CustomVertex.PositionOnly(v1); data[dataIndex + 1] = new CustomVertex.PositionOnly(v2); data[dataIndex + 2] = new CustomVertex.PositionOnly(v4); //Cargar triangulo 2 data[dataIndex + 3] = new CustomVertex.PositionOnly(v1); data[dataIndex + 4] = new CustomVertex.PositionOnly(v4); data[dataIndex + 5] = new CustomVertex.PositionOnly(v3); dataIndex += 6; } } //Meter en vertex buffer vertexBuffer.SetData(data, 0, LockFlags.None); }
private void OnVertexBufferCreate(object sender, EventArgs e) { VertexBuffer buffer = (VertexBuffer)sender; CustomVertex.PositionOnly[] verts = new CustomVertex.PositionOnly[6]; verts[0].Position = new Vector3(0.0f, 1.0f, 1.0f); verts[1].Position = new Vector3(-1.0f, -1.0f, 1.0f); verts[2].Position = new Vector3(1.0f, -1.0f, 1.0f); verts[3].Position = verts[0].Position; verts[4].Position = verts[2].Position; verts[5].Position = new Vector3(2.0f, 1.0f, 1.0f); buffer.SetData(verts, 0, LockFlags.None); }
static public void Initialize() { boxMesh = Mesh.Box(MdxRender.Device, 1, 1, 1); cursor = Mesh.Box(MdxRender.Device, 0.5f, 0.5f, 2); xLine[0] = new CustomVertex.PositionOnly(); xLine[1] = new CustomVertex.PositionOnly(); yLine[0] = new CustomVertex.PositionOnly(); yLine[1] = new CustomVertex.PositionOnly(); zLine[0] = new CustomVertex.PositionOnly(); zLine[1] = new CustomVertex.PositionOnly(); aqua = new Material(); aqua.Ambient = Color.Aqua; aqua.Diffuse = Color.Aqua; white = new Material(); white.Ambient = Color.White; white.Diffuse = Color.White; UpdatePrimitives(); }
/// <summary> /// Builds an array of PositionOnly vertices that form a /// nX by nY tesselation of a quad given by it's 2 corners /// </summary> /// <param name="bottomLeft">Bottom left corner of the quad</param> /// <param name="topRight">Top right corner of the quad</param> /// <param name="nX">amount of X tesselation</param> /// <param name="nY">amount of Y tesselation</param> /// <returns>An array of PositionOnly vertices</returns> public static CustomVertex.PositionOnly[] BuildPositionOnly(PointF bottomLeft, PointF topRight, int nX, int nY) { if (nX < 2 || nY < 2) { throw new Exception("Cannot tesselate with nX or nY below 2"); } float deltaX = (topRight.X - bottomLeft.X) / (float)(nX - 1); float deltaY = (topRight.Y - bottomLeft.Y) / (float)(nY - 1); float fx, fy; CustomVertex.PositionOnly[] verts = new CustomVertex.PositionOnly[nX * nY]; for (int y = 0; y < nY; y++) { if (y == nY - 1) fy = topRight.Y; else fy = bottomLeft.Y + (float)y * deltaY; for (int x = 0; x < nX; x++) { if (x == nX - 1) fx = topRight.X; else fx = bottomLeft.X + (float)x * deltaX; verts[y * nY + x].X = fx; verts[y * nY + x].Y = fy; verts[y * nY + x].Z = 0; } } return verts; }
void fillPos(ref OBJGroup group) { List<CustomVertex.PositionOnly> verts = new List<CustomVertex.PositionOnly>(); List<int> indicies = new List<int>(); foreach (OBJFace f in group.faces) { int numVerts = f.vertIndices.Count; for (int i = numVerts - 1; i > -1; i--) { CustomVertex.PositionOnly vert = new CustomVertex.PositionOnly(positions[f.vertIndices[i]]); verts.Add(vert); indicies.Add(indicies.Count); } } group.numVerts = verts.Count; group.numTris = indicies.Count / 3; group.vertexFormat = CustomVertex.PositionOnly.Format; group.vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionOnly), verts.Count, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionOnly.Format, Pool.Default); group.vertexBuffer.SetData(verts.ToArray(), 0, LockFlags.None); group.indexBuffer = new IndexBuffer(typeof(int), indicies.Count * sizeof(int), device, Usage.WriteOnly, Pool.Default); group.indexBuffer.SetData(indicies.ToArray(), 0, LockFlags.None); }
public static void InitializeHandleModel() { m_SelectorColor = new Material(); m_SelectorColor.Ambient = Color.DarkGray; m_SelectorColor.Diffuse = Color.DarkGray; m_GreenY = new Material(); m_GreenY.Ambient = Color.Green; m_GreenY.Diffuse = Color.Green; m_Gray = new Material(); m_Gray.Ambient = Color.Gray; m_Gray.Diffuse = Color.Gray; m_BlueX = new Material(); m_BlueX.Ambient = Color.Blue; m_BlueX.Diffuse = Color.Blue; m_RedZ = new Material(); m_RedZ.Ambient = Color.Red; m_RedZ.Diffuse = Color.Red; Color transparent_yellow = Color.Yellow; m_Yellow = new Material(); m_Yellow.Ambient = Color.Yellow; m_Yellow.Diffuse = Color.Yellow; m_XAxis[0] = new CustomVertex.PositionOnly(0.35f * Scale, 0, 0); m_XAxis[1] = new CustomVertex.PositionOnly(Scale, 0, 0); m_XAxisY[0] = new CustomVertex.PositionOnly(0.5f * Scale, 0, 0); m_XAxisY[1] = new CustomVertex.PositionOnly(0.5f * Scale, 0.5f * Scale, 0); m_XAxisZ[0] = new CustomVertex.PositionOnly(0.5f * Scale, 0, 0); m_XAxisZ[1] = new CustomVertex.PositionOnly(0.5f * Scale, 0, 0.5f * Scale); m_YAxis[0] = new CustomVertex.PositionOnly(0, 0.35f * Scale, 0); m_YAxis[1] = new CustomVertex.PositionOnly(0, Scale, 0); m_YAxisX[0] = new CustomVertex.PositionOnly(0, 0.5f * Scale, 0); m_YAxisX[1] = new CustomVertex.PositionOnly(0.5f * Scale, 0.5f * Scale, 0); m_YAxisZ[0] = new CustomVertex.PositionOnly(0, 0.5f * Scale, 0); m_YAxisZ[1] = new CustomVertex.PositionOnly(0, 0.5f * Scale, 0.5f * Scale); m_ZAxis[0] = new CustomVertex.PositionOnly(0, 0, 0.35f * Scale); m_ZAxis[1] = new CustomVertex.PositionOnly(0, 0, Scale); m_ZAxisX[0] = new CustomVertex.PositionOnly(0, 0, 0.5f * Scale); m_ZAxisX[1] = new CustomVertex.PositionOnly(0.5f * Scale, 0, 0.5f * Scale); m_ZAxisY[0] = new CustomVertex.PositionOnly(0, 0, 0.5f * Scale); m_ZAxisY[1] = new CustomVertex.PositionOnly(0, 0.5f * Scale, 0.5f * Scale); m_Selector = Mesh.Sphere(MdxRender.Device, 0.05f * Scale, 8, 4); Matrix rotate = new Matrix(); Matrix translate = new Matrix(); Matrix scale = new Matrix(); Matrix overall_transform = new Matrix(); //x-axis rotate.RotateY((float)Math.PI / 2); translate.Translate(1 * Scale, 0, 0); overall_transform = Matrix.Multiply(rotate, translate); m_XAxisTip = Mesh.Cylinder(MdxRender.Device, 0.05f * Scale, 0f, 0.3f * Scale, 6, 1); TransformMesh(m_XAxisTip, overall_transform); //y-axis rotate.RotateX((float)Math.PI / -2); translate.Translate(0, 1 * Scale, 0); overall_transform = Matrix.Multiply(rotate, translate); m_YAxisTip = Mesh.Cylinder(MdxRender.Device, 0.05f * Scale, 0f, 0.3f * Scale, 6, 1); TransformMesh(m_YAxisTip, overall_transform); //z-axis rotate.RotateZ((float)Math.PI / -2); translate.Translate(0, 0, 1 * Scale); overall_transform = Matrix.Multiply(rotate, translate); m_ZAxisTip = Mesh.Cylinder(MdxRender.Device, 0.05f * Scale, 0f, 0.3f * Scale, 6, 1); TransformMesh(m_ZAxisTip, overall_transform); m_XYPlane = Mesh.Box(MdxRender.Device, 0.5f * Scale, 0.5f * Scale, 0.001f * Scale); translate.Translate(0.25f * Scale, 0.25f * Scale, 0); TransformMesh(m_XYPlane, translate); m_XZPlane = Mesh.Box(MdxRender.Device, 0.5f * Scale, 0.001f * Scale, 0.5f * Scale); translate.Translate(0.25f * Scale, 0, 0.25f * Scale); TransformMesh(m_XZPlane, translate); m_YZPlane = Mesh.Box(MdxRender.Device, 0.001f * Scale, 0.5f * Scale, 0.5f * Scale); translate.Translate(0, 0.25f * Scale, 0.25f * Scale); TransformMesh(m_YZPlane, translate); m_PitchHandle.InitializeModel(ArcMode.Pitch); m_RollHandle.InitializeModel(ArcMode.Roll); m_YawHandle.InitializeModel(ArcMode.Yaw); EnableExtendedAxes(true, true, true); }
/// <summary> /// Finds the face of a mesh that has been intersected with a ray /// </summary> /// <param name="iiIntInfo">IntersectInformation object from the intersection of a ray and face</param> /// <returns>Vector3[] of vertices that make up a triangle face</returns> public static Vector3[] FindIntersectionFace(Microsoft.DirectX.Direct3D.Mesh mshMesh, IntersectInformation iiInfo) { if(mshMesh != null) { //vertices that make up the face CustomVertex.PositionOnly[] vertsFace = new CustomVertex.PositionOnly[3]; // get the index buffer and lock it GraphicsStream buffer = mshMesh.LockIndexBuffer(LockFlags.ReadOnly); // move to the position of the face index buffer.Position = iiInfo.FaceIndex * System.Runtime.InteropServices.Marshal.SizeOf(new IndexEntry()); // 6 = sizeof(IndexEntry) // read the index entry from the buffer IndexEntry entry = (IndexEntry)buffer.Read(typeof(IndexEntry)); // we've got the info we need from the index buffer... unlock it mshMesh.UnlockIndexBuffer(); buffer.Close(); //now that we have the vertex locations, get each vertex from the vertex buffer // get the vertex buffer and lock it buffer = mshMesh.LockVertexBuffer(LockFlags.ReadOnly); // move to the first vertex position if(buffer.Length > entry.v1 * mshMesh.NumberBytesPerVertex) { buffer.Position = entry.v1 * mshMesh.NumberBytesPerVertex; // grab first (left) vertex of triangle vertsFace[0] = (CustomVertex.PositionOnly)buffer.Read(typeof(CustomVertex.PositionOnly)); } if(buffer.Length > entry.v2 * mshMesh.NumberBytesPerVertex) { // move to the second vertex position buffer.Position = entry.v2 * mshMesh.NumberBytesPerVertex; // grab second (middle) vertex of triangle vertsFace[1] = (CustomVertex.PositionOnly)buffer.Read(typeof(CustomVertex.PositionOnly)); } if(buffer.Length > entry.v3 * mshMesh.NumberBytesPerVertex) { // move to the third vertex of triangle buffer.Position = entry.v3 * mshMesh.NumberBytesPerVertex; // grab last (right) vertex of triangle vertsFace[2] = (CustomVertex.PositionOnly)buffer.Read(typeof(CustomVertex.PositionOnly)); } //unlock the buffer mshMesh.UnlockVertexBuffer(); buffer.Close(); // Vector representation instead of CustomVertex Vector3[] v3Verts = new Vector3[vertsFace.Length]; v3Verts[0] = vertsFace[0].Position; v3Verts[1] = vertsFace[1].Position; v3Verts[2] = vertsFace[2].Position; return v3Verts; } else return null; }