Example #1
0
        public static VertexPositionNormalTexture[] ToNonIndexedWithNormals(KModel model, Texture2D texture, bool smoothShading)
        {
            // Calc normals
            Vector3[] normals;
            if (smoothShading)
                normals = model.CalcVertexNormals();
            else
                normals = model.CalcFacetNormals();

            // Build vertex array for rendering
            var result = new VertexPositionNormalTexture[3 * model.Facets.Count];

            var arrayIndex = 0;
            for (int facetIndex = 0; facetIndex < model.Facets.Count; facetIndex++)
            {
                var facet = model.Facets[facetIndex];

                result[arrayIndex].Position = model.Vertices[facet.Ve0].Pt;
                result[arrayIndex].TextureCoordinate = facet.Tex0;
                result[arrayIndex].Normal = smoothShading ? normals[facet.Ve0] : normals[facetIndex];
                arrayIndex++;

                result[arrayIndex].Position = model.Vertices[facet.Ve1].Pt;
                result[arrayIndex].TextureCoordinate = facet.Tex1;
                result[arrayIndex].Normal = smoothShading ? normals[facet.Ve1] : normals[facetIndex];
                arrayIndex++;

                result[arrayIndex].Position = model.Vertices[facet.Ve2].Pt;
                result[arrayIndex].TextureCoordinate = facet.Tex2;
                result[arrayIndex].Normal = smoothShading ? normals[facet.Ve2] : normals[facetIndex];
                arrayIndex++;
            }

            /*for (int i = 0; i < result.Length; i++)
            {
                result[i].Normal = result[i].Position.Normalized();
            }*/

            // If texture coordinates are in pixels, then scale them all to [0,1] x [0,1]
            if (model.TexCoordsAreInPixels)
            {
                var scaleX = 1f / texture.Bounds.Width;
                var scaleY = 1f / texture.Bounds.Height;
                var bounds = texture.Bounds;
                for (int i = 0; i < result.Length; i++)
                {
                    result[i].TextureCoordinate.X *= scaleX;
                    result[i].TextureCoordinate.Y *= scaleY;
                }
            }

            return result;
        }