Example #1
0
        public Mesh(Scene _scene)
        {
            this.scene = _scene;
            this.graphicsDevice = _scene.GraphicsDevice;
            this.mySilverlightEffect = _scene.ContentManager.Load<SilverlightEffect>("CustomEffect");

            //init map for mesh
            mapWidth = 128;
            mapHeight = 128;

            //cache effect parameters
            worldViewProjectionParameter = mySilverlightEffect.Parameters["WorldViewProjection"];
            worldParameter = mySilverlightEffect.Parameters["World"];
            lightPositionParameter = mySilverlightEffect.Parameters["LightPosition"];
            this.LightPosition = new Vector3(0, 10, 0);

            //init vertices/indices
            vertices = new VertexPositionNormalTexture[mapWidth * mapHeight];
            indices = new ushort[(mapWidth - 1) * (mapHeight - 1) * 6];
            Canvas _canvas = new Canvas();
            _canvas.Width = _canvas.Height = 128;
            _canvas.Background = new SolidColorBrush(Colors.Blue);
            WriteableBitmap _map = new WriteableBitmap(_canvas, null);
            heightData = LoadHeightDataFromMap(_map);
            texture = new Texture2D(graphicsDevice, _map.PixelWidth, _map.PixelHeight);
            texture.SetData(_map.Pixels);

            //
            SetupVertices();
            SetupIndices();
            CalculateNormals();
        }
Example #2
0
        public Cube(Scene scene, float size)
        {
            this.scene = scene;
            this.graphicsDevice = scene.GraphicsDevice;
            this.mySilverlightEffect = scene.ContentManager.Load<SilverlightEffect>("CustomEffect");

            // Cache effect parameters
            worldViewProjectionParameter = mySilverlightEffect.Parameters["WorldViewProjection"];
            worldParameter = mySilverlightEffect.Parameters["World"];
            lightPositionParameter = mySilverlightEffect.Parameters["LightPosition"];

            // Init static parameters
            this.LightPosition = new Vector3(1, 1, -10);

            // Temporary lists
            List<VertexPositionNormalTexture> vertices = new List<VertexPositionNormalTexture>();
            List<ushort> indices = new List<ushort>();

            vertices.Add(new VertexPositionNormalTexture(new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector2(0, 0)));
            vertices.Add(new VertexPositionNormalTexture(new Vector3(5, 5, 0), new Vector3(1, 0, 0), new Vector2(1.0f, 1.0f)));
            vertices.Add(new VertexPositionNormalTexture(new Vector3(5, 0, 0), new Vector3(1, 0, 0), new Vector2(1, 0)));

            indices.Add((ushort)0);
            indices.Add((ushort)1);
            indices.Add((ushort)2);

            // Create a vertex buffer, and copy our vertex data into it.
            vertexBuffer = new VertexBuffer(graphicsDevice, VertexPositionColorNormal.VertexDeclaration, vertices.Count, BufferUsage.None);

            vertexBuffer.SetData(0, vertices.ToArray(), 0, vertices.Count, VertexPositionColorNormal.Stride);

            // Create an index buffer, and copy our index data into it.
            indexBuffer = new IndexBuffer(graphicsDevice, IndexElementSize.SixteenBits, indices.Count, BufferUsage.None);

            indexBuffer.SetData(0, indices.ToArray(), 0, indices.Count);

            // Statistics
            VerticesCount = vertices.Count;
            FaceCount = indices.Count / 3;
        }
        public Cube(Engine engine, float size)
        {
            this.graphicsDevice = engine.GraphicsDevice;
            this.mySilverlightEffect = engine.Content.Load<SilverlightEffect>("CustomEffect");

            // Cache effect parameters
            worldViewProjectionParameter = mySilverlightEffect.Parameters["WorldViewProjection"];
            //lightDirectionParameter = mySilverlightEffect.Parameters["LightDirection"];

            // Init static parameters
            //this.LightDirection = new Vector3(0, 0, 1);

            // Temporary lists
            List<VertexPositionNormalTexture> vertices = new List<VertexPositionNormalTexture>();
            List<ushort> indices = new List<ushort>();

            // A cube has six faces, each one pointing in a different direction.
            Vector3[] normals =
            {
                new Vector3(0, 0, 1),
                new Vector3(0, 0, -1),
                new Vector3(1, 0, 0),
                new Vector3(-1, 0, 0),
                new Vector3(0, 1, 0),
                new Vector3(0, -1, 0)
            };

            // Create each face in turn.
            foreach (Vector3 normal in normals)
            {
                // Get two vectors perpendicular to the face normal and to each other.
                Vector3 side1 = new Vector3(normal.Y, normal.Z, normal.X);
                Vector3 side2 = Vector3.Cross(normal, side1);

                // Six indices (two triangles) per face.
                indices.Add((ushort)vertices.Count);
                indices.Add((ushort)(vertices.Count + 1));
                indices.Add((ushort)(vertices.Count + 2));

                indices.Add((ushort)vertices.Count);
                indices.Add((ushort)(vertices.Count + 2));
                indices.Add((ushort)(vertices.Count + 3));

                vertices.Add(new VertexPositionNormalTexture((normal - side1 - side2) * size / 2, normal, new Vector2(0, 0)));
                vertices.Add(new VertexPositionNormalTexture((normal - side1 + side2) * size / 2, normal, new Vector2(1, 0)));
                vertices.Add(new VertexPositionNormalTexture((normal + side1 + side2) * size / 2, normal, new Vector2(1, 1)));
                vertices.Add(new VertexPositionNormalTexture((normal + side1 - side2) * size / 2, normal, new Vector2(0, 1)));
            }

            // Create a vertex buffer, and copy our vertex data into it.
            vertexBuffer = new VertexBuffer(graphicsDevice, VertexPositionNormalTexture.VertexDeclaration, vertices.Count, BufferUsage.None);

            vertexBuffer.SetData(0, vertices.ToArray(), 0, vertices.Count, VertexPositionNormalTexture.Stride);

            // Create an index buffer, and copy our index data into it.
            indexBuffer = new IndexBuffer(graphicsDevice, IndexElementSize.SixteenBits, indices.Count, BufferUsage.None);

            indexBuffer.SetData(0, indices.ToArray(), 0, indices.Count);

            // Statistics
            VerticesCount = vertices.Count;
            FaceCount = indices.Count / 3;
        }
 protected ShaderBase(Engine engine, string assetName)
 {
     _effect = engine.Content.Load<SilverlightEffect>(assetName);
 }