Ejemplo n.º 1
0
        private void CreateRandomLight(int id, Node parent)
        {
            Color diffuse = new Color((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());

            Sphere lightSphere = new Sphere("LightSphere " + id.ToString(), 5, 5, .5f);

            lightSphere.SceneHints.LightCombineHint = LightCombineHint.Off;
            lightSphere.SetSolidColor(diffuse);
            lightSphere.Material = ContentManager.Load <Material>("BasicVertColor.tem");


            PointLight pl = new PointLight();

            pl.Attenuate = true;
            pl.Constant  = .1f;
            pl.Linear    = .001f;
            pl.Quadratic = 0.001f;
            pl.IsEnabled = true;
            pl.Diffuse   = diffuse;
            pl.Ambient   = Color.Black;// new Color(.1f, .1f, .1f);

            RootNode.AddLight(pl);

            lightSphere.AddController(new LightSpatialController(pl, random));
            parent.AddChild(lightSphere);
        }
Ejemplo n.º 2
0
        protected override void LoadContent()
        {
            ClearColor = Color.Black;
            shadowMap  = new RenderTarget2D(1024, 1024, false, SurfaceFormat.Single, DepthFormat.Depth24Stencil8);
            blurRT     = new RenderTarget2D(1024, 1024, false, SurfaceFormat.Single, DepthFormat.None);
            blurEffect = ContentManager.Load <Effect>("Shaders//GaussianBlur.fx");

            ModelLoaderParameters mlp = new ModelLoaderParameters();

            mlp.NormalGeneration   = NormalGeneration.Smooth;
            mlp.SwapWindingOrder   = true;
            mlp.PreferLitMaterials = true;

            rs      = new RasterizerState();
            rs.Cull = CullMode.Front;
            rs.BindRenderState();

            // mesh = new Sphere("Sphere", 50, 50, 20);
            mesh = ContentManager.Load <Mesh>("Models//statue.tebo");
            mesh.ComputeTangentBasis();
            Material mat = ContentManager.Load <Material>("LitBasicTexture.tem").Clone();

            mat.SetParameter("DiffuseMap", ContentManager.Load <Texture2D>("Textures//statue_diff.dds"));
            mat.SetParameter("NormalMap", ContentManager.Load <Texture2D>("Textures//statue_norm.dds"));
            mat.SetParameter("MatSpecular", new Vector3(.3f, .3f, .3f));
            mesh.SetMaterial(mat);

            Quad q = new Quad("Floor", 800, 800);

            q.Rotation    = Quaternion.FromAngleAxis(MathHelper.ToRadians(-90), Vector3.UnitX);
            q.Translation = new Vector3(0, -70, 0);
            q.Material    = ContentManager.Load <Material>("Materials//LitBasicTextureShadow.tem").Clone();
            q.Material.SetParameter("DiffuseMap", ContentManager.Load <Texture2D>("Textures//statue_diff.dds"));
            receiverMat = q.Material;
            receiverMat.SetParameter("MatSpecular", new Vector3(.3f, .3f, .3f));
            // mesh.SetMaterial(receiverMat);

            InputLayer.RegisterTrigger(new InputTrigger(new KeyPressedCondition(Keys.Enter, false), new InputAction(delegate(GameTime time) {
                receive = !receive;
                if (receive)
                {
                    mesh.SetMaterial(receiverMat);
                }
                else
                {
                    mesh.SetMaterial(mat);
                }
            })));
            InputLayer.RegisterTrigger(new InputTrigger(new KeyPressedCondition(Keys.C, false), new InputAction(delegate(GameTime time) {
                cullFront = !cullFront;
            })));

            DataBuffer <Vector2> texCoords = q.MeshData.TextureCoordinates;

            texCoords.Position = 0;
            texCoords.Set(new Vector2(0, 0));
            texCoords.Set(new Vector2(4, 0));
            texCoords.Set(new Vector2(4, 4));
            texCoords.Set(new Vector2(0, 4));
            q.MeshData.UpdateVertexData <Vector2>(VertexSemantic.TextureCoordinate, texCoords);
            RootNode.AddChild(q);

            sl = new SpotLight(new Vector3(100, 200, 10), new Vector3(-100, -200, -10), 5, 15);
            RootNode.RemoveAllLights();
            RootNode.AddLight(sl);
            RootNode.AddChild(mesh);


            shadowMat = new Material("ShadowMat");
            shadowMat.LoadEffect(ContentManager.Load <Effect>("Shaders//ShadowMapEffect.fx"));
            shadowMat.SetEngineParameter("LightWVP", Tesla.Core.EngineValue.WorldViewProjection);
            lightCam = new Camera();

            batch             = new SpriteBatch();
            dss               = new DepthStencilState();
            dss.DepthFunction = ComparisonFunction.LessEqual;
            dss.BindRenderState();

            float texOffsets = 0.5f + (0.5f / (float)shadowMap.Width);

            texMat = new Matrix(0.5f, 0.0f, 0.0f, 0.0f,
                                0.0f, -0.5f, 0.0f, 0.0f,
                                0.0f, 0.0f, 1.0f, 0.0f,
                                texOffsets, texOffsets, 0.0f, 1.0f);
            rtnode             = new Node("Node");
            rtnode.Translation = sl.Position;
            Node parent = new Node("Parent");

            parent.AddChild(rtnode);
            RootNode.AddChild(parent);
            parent.AddController(new Tesla.Scene.Extension.RotateController(Vector3.UnitY, 25));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Please see the SceneGraph.rtf description for scene graph organization.
        /// </summary>
        protected override void LoadContent()
        {
            ClearColor   = Color.CornflowerBlue;
            Window.Title = "Scene Graph Sample";

            //Create the teapot sub-tree, we're also creating a small box showing the position of
            //the node in world space
            Node teapotParent       = new Node("TeapotParent");
            Box  teapotParentVisual = new Box("TeapotParentVisual", 1f, 1f, 1f);

            teapotParentVisual.Material = ContentManager.Load <Material>("LitBasicColor.tem").Clone();
            teapotParent.AddChild(teapotParentVisual);

            //Set a rotate controller onto the teapot parent, so it'll rotate every frame
            teapotParent.AddController(new RotateController(Vector3.UnitY, 25));

            //Create the teapot mesh
            Teapot teapot = new Teapot("Teapot");

            //Load up a default lit color material
            teapot.Material = ContentManager.Load <Material>("LitBasicColor.tem").Clone();
            //Create another rotate controller that will rotate the teapot about its X-Axis
            //45 degrees per second. This shows how we can -easily- concatenate transforms
            //using the hierarchy of the scene graph.
            teapot.AddController(new RotateController(Vector3.UnitX, 45f));

            //Set a red color to the material.
            teapot.Material.SetParameter("MatDiffuse", Color.Crimson.ToVector3());
            teapot.Scale = new Vector3(5f, 5f, 5f);

            //Position and add to parent
            teapot.Translation = new Vector3(-50, 0, 0);
            teapotParent.AddChild(teapot);

            //Add the teapot sub tree to the root.
            RootNode.AddChild(teapotParent);

            //Add a white light to the root node
            RootNode.RemoveAllLights();
            PointLight pl = new PointLight();

            pl.Attenuate = false;
            pl.Position  = new Vector3(-100, 50, 100);
            RootNode.AddLight(pl);

            //Create the box
            Box box = new Box("Box", 10, 10, 10);

            box.Translation = new Vector3(50, -50, 0);
            box.Material    = ContentManager.Load <Material>("LitBasicColor.tem").Clone();
            box.Material.SetParameter("MatDiffuse", Color.Gray.ToVector3());

            //Set the box to only use lights that are attached to it
            box.SceneHints.LightCombineHint = LightCombineHint.Local;

            //Create a green point light
            PointLight pl2 = new PointLight();

            pl2.Attenuate = false;
            pl2.Diffuse   = Color.Green;
            pl2.Position  = new Vector3(100, 50, 100);
            box.AddLight(pl2);

            //Add the box to the root
            RootNode.AddChild(box);

            //Set a bounding box to be used as the volume for each node/mesh in the scene graph.
            RootNode.SetModelBound(new BoundingBox());

            //Create some input handling to show how you can apply
            //a scaling value to a parent node (this case the scene Root),
            //and see it be applied to all children.
            InputLayer.RegisterTrigger(new InputTrigger(new KeyPressedCondition(Keys.J, false), new InputAction(delegate(GameTime time) {
                if (RootNode.Controllers.Count > 0)
                {
                    RootNode.RemoveAllControllers();
                    RootNode.SetScale(1.0f);
                    scaleOn = false;
                }
                else
                {
                    RootNode.AddController(new ScaleController(1.0f, .1f, 2.0f));
                    scaleOn = true;
                }
            })));

            batch = new SpriteBatch();
            font  = ContentManager.Load <SpriteFont>("Fonts//comicsans.fnt");
        }
Ejemplo n.º 4
0
        protected override void LoadContent()
        {
            Window.Title = "Different Lights Sample";
            Type sp = typeof(Spatial);
            Type me = typeof(Mesh);

            bool ishash  = sp.GetHashCode() == sp.GetHashCode();
            bool ishash2 = sp.GetHashCode() == me.GetHashCode();

            bool iseq = sp.Equals(sp);


            pl          = new PointLight();
            pl.Specular = Color.DarkGray; //Cut down on the specular so we don't get blinded

            sl = new SpotLight();
            //Spot light angles - inner angle is where the light is most intense. The intensity smoothly
            //fades from this angle to the outer angle, so we don't have an abrupt cutoff. Essentially spot lights
            //are similar to point lights, except for this restricting cone.
            sl.OuterAngle = 45;
            sl.InnerAngle = 20;
            sl.Direction  = new Vector3(-.577f, -.577f, -.577f);

            //Simple directional light - simulates a light source infinitely away like the Sun.
            dl           = new DirectionalLight();
            dl.Direction = new Vector3(-.577f, -.577f, -.577f);

            activeLight = pl;

            //Create an inverted cube
            Box b = new Box("InsideOut", Vector3.Zero, 100, 100, 100, true);

            b.Material         = ContentManager.Load <Material>("LitBasicColor.tem");
            b.RenderBucketType = RenderBucketType.Skip;

            //These are object material properties (built-in shaders also include Emissive, shininess factor, and an alpha factor)
            b.Material.SetParameter("MatDiffuse", Color.Crimson.ToVector3());
            b.Material.SetParameter("MatAmbient", new Color(.4f, .2f, .2f).ToVector3());
            b.Material.SetParameter("MatSpecular", new Color(.3f, .3f, .3f).ToVector3());
            b.SetModelBound(new BoundingBox());
            RootNode.AddChild(b);

            RootNode.RemoveAllLights();
            RootNode.AddLight(activeLight);
            text = "Active Light: Point";
            //Input response to set the active light
            InputLayer.RegisterTrigger(new InputTrigger(new KeyPressedCondition(Keys.D1, false), new InputAction(
                                                            delegate(GameTime time) {
                activeLight = pl;
                RootNode.RemoveAllLights();
                RootNode.AddLight(activeLight);
                text = "Active Light: Point";
            }
                                                            )));
            InputLayer.RegisterTrigger(new InputTrigger(new KeyPressedCondition(Keys.D2, false), new InputAction(
                                                            delegate(GameTime time) {
                activeLight = sl;
                RootNode.RemoveAllLights();
                RootNode.AddLight(activeLight);
                text = "Active Light: Spot";
            }
                                                            )));
            InputLayer.RegisterTrigger(new InputTrigger(new KeyPressedCondition(Keys.D3, false), new InputAction(
                                                            delegate(GameTime time) {
                activeLight = dl;
                RootNode.RemoveAllLights();
                RootNode.AddLight(activeLight);
                text = "Active Light: Directional";
            }
                                                            )));

            //Set up some text rendering
            batch = new SpriteBatch();
            font  = ContentManager.Load <SpriteFont>("Fonts//comicsans.fnt");
        }