Example #1
0
        public static void AddModel(Search3DBase<FacetRef> search, KModel model)
        {
            for (int facetIndex = 0; facetIndex < model.Facets.Count; facetIndex++)
            {
                var facet = model.Facets[facetIndex];

                search.AddTriangle(new FacetRef() { Model = model, faId = facetIndex },
                    model.Vertices[facet.Ve0].Pt, model.Vertices[facet.Ve1].Pt, model.Vertices[facet.Ve2].Pt);
            }
        }
Example #2
0
        public DrawableModel(KModel model, Texture2D texture, GraphicsDevice graphicsDevice, bool smoothShading)
        {
            _graphicsDevice = graphicsDevice;
            _texture = texture;

            // Create vertex buffer
            var vertexArray = ModelRendering.ToNonIndexedWithNormals(model, texture, smoothShading);
            _buffer = new VertexBuffer(graphicsDevice, VertexPositionNormalTexture.VertexDeclaration, vertexArray.Length, BufferUsage.WriteOnly);
            /*var vertexArray = ModelRendering.ToNonIndexed(model, texture);
            _buffer = new VertexBuffer(graphicsDevice, VertexPositionTexture.VertexDeclaration, vertexArray.Length, BufferUsage.WriteOnly);*/
            _buffer.SetData(vertexArray);
        }
Example #3
0
        public static VertexPositionTexture[] ToNonIndexed(KModel model, Texture2D texture)
        {
            var result = new VertexPositionTexture[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;
                arrayIndex++;

                result[arrayIndex].Position = model.Vertices[facet.Ve1].Pt;
                result[arrayIndex].TextureCoordinate = facet.Tex1;
                arrayIndex++;

                result[arrayIndex].Position = model.Vertices[facet.Ve2].Pt;
                result[arrayIndex].TextureCoordinate = facet.Tex2;
                arrayIndex++;
            }

            // 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;
                for (int i = 0; i < result.Length; i++)
                {
                    result[i].TextureCoordinate.X *= scaleX;
                    result[i].TextureCoordinate.Y *= scaleY;
                }
            }

            return result;
        }
Example #4
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;
        }