Esempio n. 1
0
        public static void TransformMesh(Mesh mesh, Matrix transform)
        {
            CustomVertex.PositionNormal[] tmp = new CustomVertex.PositionNormal[mesh.NumberVertices];
            Vector3 pos  = new Vector3();
            Vector3 norm = new Vector3();

            using (VertexBuffer vb = mesh.VertexBuffer)
            {
                GraphicsStream vertexData = vb.Lock(0, 0, LockFlags.None);
                BinaryReader   br         = new BinaryReader(vertexData);
                for (int v = 0; v < mesh.NumberVertices; v++)
                {
                    pos.X  = br.ReadSingle();
                    pos.Y  = br.ReadSingle();
                    pos.Z  = br.ReadSingle();
                    norm.X = br.ReadSingle();
                    norm.Y = br.ReadSingle();
                    norm.Z = br.ReadSingle();
                    pos.TransformCoordinate(transform);
                    norm.TransformNormal(transform);
                    tmp[v] = new CustomVertex.PositionNormal(pos.X, pos.Y, pos.Z, norm.X, norm.Y, norm.Z);
                }
                vb.Unlock();
            }

            mesh.SetVertexBufferData(tmp, LockFlags.None);
        }
Esempio n. 2
0
        public CustomVertex.PositionNormal[] GetRenderData()
        {
            //CustomVertex.PositionNormalColored[] result=new CustomVertex.PositionNormalColored[triangle.Length*3];
            CustomVertex.PositionNormal[] result = new CustomVertex.PositionNormal[triangle.Length * 3];
            for (int i = 0; i <= triangle.High; i++)
            {
                result[i * 3 + 0].Position = pos[triangle[i].p0];
                result[i * 3 + 0].Normal   = normal[triangle[i].n0];
                //result[i * 3 + 0].Color = color[triangle[i].c1];

                result[i * 3 + 1].Position = pos[triangle[i].p1];
                result[i * 3 + 1].Normal   = normal[triangle[i].n1];
                //result[i * 3 + 1].Color = color[triangle[i].c2];

                result[i * 3 + 2].Position = pos[triangle[i].p2];
                result[i * 3 + 2].Normal   = normal[triangle[i].n2];
                //result[i * 3 + 2].Color = color[triangle[i].c3];
            }
            return(result);
        }
Esempio n. 3
0
        public override void Initialize( )
        {
            if (pointBuffer != null)
            {
                pointBuffer.Dispose();
            }

            model = source as PointModel;

            //Create the vertex buffer
            pointBuffer = new AutoVertexBuffer(d3d, typeof(CustomVertex.PositionNormal), 1,
                                               Usage.Dynamic, CustomVertex.PositionNormal.Format, Pool.Default);

            CustomVertex.PositionNormal[] verts = new CustomVertex.PositionNormal[1];

            verts[0].Position = model.Position;
            verts[0].Normal   = Direct3dRender.DefaultVertexNormal;

            pointBuffer.VB.SetData(verts, 0, LockFlags.None);
        }
        /// <summary>
        /// Builds an array of PositionNormal 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 PositionNormal vertices</returns>
        public static CustomVertex.PositionNormal[] BuildPositionNormal(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.PositionNormal[] verts = new CustomVertex.PositionNormal[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;
                    verts[y * nY + x].Normal = new Vector3(0, 0, -1);
                }
            }
            return verts;
        }
Esempio n. 5
0
		///<summary>Converts the VertexNormals list into a vertex buffer so the verticies can be used to render DirectX triangles.</summary>
		public void PrepareForDirectX(Device deviceRef){
			CleanupDirectX();
			CustomVertex.PositionNormal[] verts=new CustomVertex.PositionNormal[VertexNormals.Count];
			for(int i=0;i<VertexNormals.Count;i++){
				verts[i].X=VertexNormals[i].Vertex.X;
				verts[i].Y=VertexNormals[i].Vertex.Y;
				verts[i].Z=VertexNormals[i].Vertex.Z;
				verts[i].Nx=VertexNormals[i].Normal.X;
				verts[i].Ny=VertexNormals[i].Normal.Y;
				verts[i].Nz=VertexNormals[i].Normal.Z;
			}
			vb=new VertexBuffer(typeof(CustomVertex.PositionNormal),CustomVertex.PositionNormal.StrideSize*verts.Length,
				deviceRef,Usage.WriteOnly,CustomVertex.PositionNormal.Format,Pool.Managed);
			vb.SetData(verts,0,LockFlags.None);
			for(int g=0;g<Groups.Count;g++) {
				Groups[g].PrepareForDirectX(deviceRef);
			}
		}
Esempio n. 6
0
        public static void UpdateBox(Vector3 start, Vector3 end, float height)
        {
            CustomVertex.PositionNormal[] tmp = new CustomVertex.PositionNormal[boxMesh.NumberVertices];
            Vector3 pos  = new Vector3();
            Vector3 norm = new Vector3();

            using (VertexBuffer vb = boxMesh.VertexBuffer)
            {
                GraphicsStream vertexData = vb.Lock(0, 0, LockFlags.None);
                BinaryReader   br         = new BinaryReader(vertexData);
                for (int v = 0; v < boxMesh.NumberVertices; v++)
                {
                    pos.X  = br.ReadSingle();
                    pos.Y  = br.ReadSingle();
                    pos.Z  = br.ReadSingle();
                    norm.X = br.ReadSingle();
                    norm.Y = br.ReadSingle();
                    norm.Z = br.ReadSingle();
                    tmp[v] = new CustomVertex.PositionNormal(pos.X, pos.Y, pos.Z, norm.X, norm.Y, norm.Z);
                }
                vb.Unlock();
            }

            tmp[0].X = start.X;
            tmp[0].Y = start.Y;
            tmp[0].Z = start.Z;

            tmp[1].X = start.X;
            tmp[1].Y = start.Y;
            tmp[1].Z = start.Z + height;

            tmp[2].X = start.X;
            tmp[2].Y = end.Y;
            tmp[2].Z = start.Z + height;

            tmp[3].X = start.X;
            tmp[3].Y = end.Y;
            tmp[3].Z = start.Z;

            tmp[4].X = start.X;
            tmp[4].Y = end.Y;
            tmp[4].Z = start.Z;

            tmp[5].X = start.X;
            tmp[5].Y = end.Y;
            tmp[5].Z = start.Z + height;

            tmp[6].X = end.X;
            tmp[6].Y = end.Y;
            tmp[6].Z = start.Z + height;

            tmp[7].X = end.X;
            tmp[7].Y = end.Y;
            tmp[7].Z = start.Z;

            tmp[8].X = end.X;
            tmp[8].Y = end.Y;
            tmp[8].Z = start.Z;

            tmp[9].X = end.X;
            tmp[9].Y = end.Y;
            tmp[9].Z = start.Z + height;

            tmp[10].X = end.X;
            tmp[10].Y = start.Y;
            tmp[10].Z = start.Z + height;

            tmp[11].X = end.X;
            tmp[11].Y = start.Y;
            tmp[11].Z = start.Z;

            tmp[12].X = start.X;
            tmp[12].Y = start.Y;
            tmp[12].Z = end.Z + height;

            tmp[13].X = start.X;
            tmp[13].Y = start.Y;
            tmp[13].Z = start.Z;

            tmp[14].X = end.X;
            tmp[14].Y = start.Y;
            tmp[14].Z = start.Z;

            tmp[15].X = end.X;
            tmp[15].Y = start.Y;
            tmp[15].Z = start.Z + height;

            tmp[16].X = start.X;
            tmp[16].Y = start.Y;
            tmp[16].Z = start.Z + height;

            tmp[17].X = end.X;
            tmp[17].Y = start.Y;
            tmp[17].Z = start.Z + height;

            tmp[18].X = end.X;
            tmp[18].Y = end.Y;
            tmp[18].Z = start.Z + height;

            tmp[19].X = start.X;
            tmp[19].Y = end.Y;
            tmp[19].Z = start.Z + height;

            tmp[20].X = start.X;
            tmp[20].Y = start.Y;
            tmp[20].Z = start.Z;

            tmp[21].X = start.X;
            tmp[21].Y = end.Y;
            tmp[21].Z = start.Z;

            tmp[22].X = end.X;
            tmp[22].Y = end.Y;
            tmp[22].Z = start.Z;

            tmp[23].X = end.X;
            tmp[23].Y = start.Y;
            tmp[23].Z = start.Z;

            boxMesh.SetVertexBufferData(tmp, LockFlags.None);
        }
Esempio n. 7
0
        void fillPosNorm(ref OBJGroup group)
        {
            List<CustomVertex.PositionNormal> verts = new List<CustomVertex.PositionNormal>();
              List<int> indicies = new List<int>();
              foreach (OBJFace f in group.faces)
              {
            int numVerts = f.vertIndices.Count;
            for (int i = 0; i < numVerts; i++)
            {
              bool foundMatch = false;
              for (int j = 0; j < verts.Count; j++)
              {
            // Look for identical verts
            if (verts[j].Position == positions[f.vertIndices[i]]
              && verts[j].Normal == normals[f.vertNormals[i]])
            {
              indicies.Add(j);
              foundMatch = true;
              break;
            }
              }
              if (foundMatch)
            continue;

              CustomVertex.PositionNormal vert = new CustomVertex.PositionNormal(positions[f.vertIndices[i]], normals[f.vertNormals[i]]);
              verts.Add(vert);
              indicies.Add(verts.Count - 1);
            }
              }

              group.numVerts = verts.Count;
              group.numTris = indicies.Count / 3;
              group.vertexFormat = CustomVertex.PositionNormal.Format;
              group.vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionNormal), verts.Count, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionNormal.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);

              /*using (StreamWriter fs = new StreamWriter("output.txt",false))
              {
            for(int k=0; k<verts.Count; k++)
            {
              fs.WriteLine("V"+k+" Pos: ("+
            verts[k].Position.X+
            ", "+verts[k].Position.Y+
            ", "+verts[k].Position.Z+
            ") Norm: ("+
            verts[k].Normal.X+
            ", "+verts[k].Normal.Y+
            ", "+verts[k].Normal.Z+")");
            }
              }*/

              /*using (StreamWriter fs = new StreamWriter("indiciesOut.txt", false))
              {
            for (int k = 0; k < indicies.Count; k++)
            {
              fs.WriteLine(indicies[k]);
            }
              }*/
        }
Esempio n. 8
0
        /// <summary>
        /// Create a new TerrainPatch.
        /// Rows and columns specify the number of vertices in the given directions.
        /// Scale indicates the size of the patch.
        /// </summary>
        /// <param name="rows">Number of rows to create.</param>
        /// <param name="columns">Number of columns to create.</param>
        /// <param name="xScale">Distance from the first column to the last.</param>
        /// <param name="yScale">Distance from the first row to the last.</param>
        public void CreatePatch(int rows, int columns, float xScale, float yScale)
        {
            // Error check
            if (rows < 2)
            {
                throw new Exception("Rows must be greater than 1");
            }

            if (columns < 2)
            {
                throw new Exception("Columns must be greater than 1");
            }

            if (xScale <= 0.0f)
            {
                xScale = 1.0f;
            }

            if (yScale <= 0.0f)
            {
                yScale = 1.0f;
            }

            // Dispose existing data
            Dispose();

            // Set new data
            _rows    = rows;
            _columns = columns;
            _height  = yScale;
            _width   = xScale;

            _vertices         = new CustomVertex.PositionNormal[rows * columns];
            _indices          = new short[NumFaces() * 3];
            _changedVertices  = new bool[rows * columns];
            _selectedVertices = new bool[rows * columns];
            ResetChangedVertices();
            ResetSelectedVertices();

            _numVertices = rows * columns;
            _numIndices  = NumFaces() * 3;

            // Calculate the distance between vertices along xy-plane
            float changeX = xScale / (columns - 1);
            float changeY = yScale / (rows - 1);

            if (changeX < changeY)
            {
                _nearestVertices = changeX;
            }
            else
            {
                _nearestVertices = changeY;
            }

            // Specify starting vertex position
            int changeIndex = 0;
            int curRow      = 0;

            // Calculate vertex positions
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    _vertices[i * columns + j]          = new CustomVertex.PositionNormal();
                    _vertices[i * columns + j].Position =
                        new Vector3(j * changeX, 0.0f, i * changeY);
                    _vertices[i * columns + j].Normal = new Vector3(0f, 1.0f, 0f);
                }
            }

            // Calculate indices list
            for (int count = 0; count < _numIndices; count += 6)
            {
                changeIndex = (count / 6) + curRow;

                if ((( int )(changeIndex + 1) / columns) != curRow)
                {
                    changeIndex++;
                    curRow++;
                }

                _indices[count]     = ( short )(changeIndex);
                _indices[count + 1] = ( short )(changeIndex + columns);
                _indices[count + 2] = ( short )(changeIndex + columns + 1);
                _indices[count + 3] = ( short )(changeIndex + columns + 1);
                _indices[count + 4] = ( short )(changeIndex + 1);
                _indices[count + 5] = ( short )(changeIndex);
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Create a new TerrainPatch.
        /// Rows and columns specify the number of vertices in the given directions.
        /// Scale indicates the size of the patch.
        /// </summary>
        /// <param name="rows">Number of rows to create.</param>
        /// <param name="columns">Number of columns to create.</param>
        /// <param name="xScale">Distance from the first column to the last.</param>
        /// <param name="yScale">Distance from the first row to the last.</param>
        public void CreatePatch( int rows, int columns, float xScale, float yScale )
        {
            // Error check
            if ( rows < 2 )
                throw new Exception( "Rows must be greater than 1" );

            if ( columns < 2 )
                throw new Exception( "Columns must be greater than 1" );

            if ( xScale <= 0.0f )
                xScale = 1.0f;

            if ( yScale <= 0.0f )
                yScale = 1.0f;

            // Dispose existing data
            Dispose();

            // Set new data
            _rows = rows;
            _columns = columns;
            _height = yScale;
            _width = xScale;

            _vertices = new CustomVertex.PositionNormal[rows * columns];
            _indices = new short[NumFaces() * 3];
            _changedVertices = new bool[rows * columns];
            _selectedVertices = new bool[rows * columns];
            ResetChangedVertices();
            ResetSelectedVertices();

            _numVertices = rows * columns;
            _numIndices = NumFaces() * 3;

            // Calculate the distance between vertices along xy-plane
            float changeX = xScale / ( columns - 1 );
            float changeY = yScale / ( rows - 1 );

            if ( changeX < changeY )
                _nearestVertices = changeX;
            else
                _nearestVertices = changeY;

            // Specify starting vertex position
            int changeIndex = 0;
            int curRow = 0;

            // Calculate vertex positions
            for ( int i = 0; i < rows; i++ )
            {
                for ( int j = 0; j < columns; j++ )
                {
                    _vertices[i * columns + j] = new CustomVertex.PositionNormal();
                    _vertices[i * columns + j].Position =
                        new Vector3( j * changeX, 0.0f, i * changeY );
                    _vertices[i * columns + j].Normal = new Vector3( 0f, 1.0f, 0f );
                }
            }

            // Calculate indices list
            for ( int count = 0; count < _numIndices; count += 6 )
            {
                changeIndex = ( count / 6 ) + curRow;

                if ( ( ( int ) ( changeIndex + 1 ) / columns ) != curRow )
                {
                    changeIndex++;
                    curRow++;
                }

                _indices[count]		= ( short ) ( changeIndex );
                _indices[count + 1]	= ( short ) ( changeIndex + columns );
                _indices[count + 2]	= ( short ) ( changeIndex + columns + 1 );
                _indices[count + 3]	= ( short ) ( changeIndex + columns + 1 );
                _indices[count + 4]	= ( short ) ( changeIndex + 1 );
                _indices[count + 5]	= ( short ) ( changeIndex );
            }
        }
        public static Mesh CreateConeLimit(Microsoft.DirectX.Direct3D.Device d3dDevice, float fltHalfAngle, float fltHeight, int iSides)
        {
            Mesh mesh;
            AttributeRange ar = new AttributeRange();

            float fltRotAmnt = Geometry.DegreeToRadian(360.0f/iSides);

            //create indices
            short[] aryIndices = new short[iSides * 3];

            //create vertices
            CustomVertex.PositionNormal[] aryVerts = new CustomVertex.PositionNormal[iSides + 1];

            //create mesh with desired vertex format and desired size
            mesh = new Mesh(aryIndices.Length/3, aryVerts.Length, MeshFlags.SystemMemory, CustomVertex.PositionNormal.Format, d3dDevice);

            //caclulate the bottom radius vertices
            Vector3 v3Current = new Vector3(0, 0,0);
            aryVerts[0].Position = v3Current;

            v3Current.Z = fltHeight;
            v3Current.TransformCoordinate(Matrix.RotationX(fltHalfAngle));

            aryVerts[1].Position = v3Current;

            for(int i=2; i<iSides + 1; i++)
            {
                v3Current.TransformCoordinate(Matrix.RotationZ(fltRotAmnt));
                aryVerts[i].Position = v3Current;
            }

            //calculate the indices
            int j =0;
            for(int i=0; i<aryIndices.Length; i+=3)
            {
                //get first triangle
                aryIndices[i] = (short)(0);
                aryIndices[i+1] = (short)(j + 2);
                aryIndices[i+2] = (short)(j + 1);

                if(i == aryIndices.Length - 3)
                {
                    aryIndices[i] =	(short)(0);
                    aryIndices[i+1] = (short)(1);
                    aryIndices[i+2] = (short)(j + 1);

                    /*	TODO:  Remove when done
                    Debug.WriteLine("\nj = " + j.ToString());
                    Debug.WriteLine(aryIndices[i]);
                    Debug.WriteLine(aryIndices[i+1]);
                    Debug.WriteLine(aryIndices[i+2]);
                    Debug.WriteLine(aryIndices[i+3]);
                    Debug.WriteLine(aryIndices[i+4]);
                    Debug.WriteLine(aryIndices[i+5]);
                    */
                }
                j++;
            }

            //			aryIndices[0] = 0;
            //			aryIndices[1] = 2;
            //			aryIndices[2] = 1;
            //			aryIndices[3] = 0;
            //			aryIndices[4] = 3;
            //			aryIndices[5] = 2;
            //			aryIndices[6] = 0;
            //			aryIndices[7] = 4;
            //			aryIndices[8] = 3;
            //			aryIndices[9] = 0;
            //			aryIndices[10] = 1;
            //			aryIndices[11] = 4;

            ar.AttributeId = 0;
            ar.FaceStart = 0;
            ar.FaceCount = aryIndices.Length/3;
            ar.VertexStart = 0;
            ar.VertexCount = aryVerts.Length;

            //set the mesh
            mesh.VertexBuffer.SetData(aryVerts, 0, LockFlags.None);
            mesh.IndexBuffer.SetData(aryIndices, 0, LockFlags.None);
            mesh.SetAttributeTable(new AttributeRange[]{ar});

            mesh.ComputeNormals();

            return (mesh);
        }