protected override void Initialise() { Resource.EnableResourceTracking(); Camera3D camera = new Camera3D(); camera.LookAt(Vector3.Zero, new Vector3(0, 0, 5), Vector3.UnitY); //create the draw target. drawToScreen = new DrawTargetScreen(this, camera); drawToScreen.ClearBuffer.ClearColour = Color.CornflowerBlue; //create a shader to display the geometry (this is the same as tutorial 02) Vector3 lightDirection = new Vector3(1.0f, 0.5f, 0.5f); MaterialShader material = new MaterialShader(new MaterialLightCollection()); material.UsePerPixelSpecular = true; material.Lights.AmbientLightColour = Color.CornflowerBlue.ToVector3() * 0.5f; //set the ambient material.Lights.AddDirectionalLight(true, lightDirection, Color.Gray); //add the first of two light sources material.Lights.AddDirectionalLight(true, -lightDirection, Color.DarkSlateBlue); material.SpecularColour = Color.LightYellow.ToVector3(); //give the material a nice sheen //create a simpler shader to display the wireframe (and also used for the bounding cube) Xen.Ex.Shaders.FillSolidColour simpleShader = new Xen.Ex.Shaders.FillSolidColour(); simpleShader.FillColour = Vector4.One * 0.01f; Vector3 sphereSize = new Vector3(0.5f, 0.5f, 0.5f); //create the complex sphere, this will have ~100k triangles. //pass in a shader for wireframe rendering sphere = new GeometryDrawer(new Xen.Ex.Geometry.Sphere(sphereSize, 200), material, simpleShader); //create the bounding cube sphereBoundingBox = new GeometryDrawer(new Xen.Ex.Geometry.Cube(sphereSize), simpleShader, null); //create the occluding cube, and position it close to the camera cube = new GeometryDrawer(new Xen.Ex.Geometry.Cube(Vector3.One), material, null); cube.position = new Vector3(0, 0, 2.75f); //add the cube first (so it can draw first, potentially occluding the sphere) //if the cube was added second, it would have no effect, as it would draw after the sphere drawToScreen.Add(cube); //create the predicate, passing in the sphere and bounding box Xen.Ex.Scene.DrawPredicate predicate = new Xen.Ex.Scene.DrawPredicate(sphere, sphereBoundingBox); //add the DrawPredicate (the DrawPredicate draws it's children) drawToScreen.Add(predicate); //statistic overlay statOverlay = new Xen.Ex.Graphics2D.Statistics.DrawStatisticsDisplay(this.UpdateManager); drawToScreen.Add(statOverlay); }
/// <summary> /// End the modifier (This method is called by the DrawTarget) /// </summary> /// <param name="state"></param> public void End(DrawState state) { if (enabledBuffer) { state.PopPostCuller(); } if (cubes.Count > 0 || spheres.Count > 0) { state.PushCamera(camera); state.PushRenderState(); state.RenderState.DepthColourCull.DepthWriteEnabled = false; state.RenderState.AlphaBlend = AlphaBlendState.Alpha; Xen.Ex.Shaders.FillSolidColour shader = state.GetShader <Xen.Ex.Shaders.FillSolidColour>(); shader.FillColour = new Vector4(1, 1, 1, 0.25f); shader.Bind(state); GenCubeVS(state); GenSphereVS(state); Matrix mat; for (int i = 0; i < cubes.Count; i++) { mat = cubes[i]; state.PushWorldMatrix(ref mat); cubeVS.Draw(state, null, PrimitiveType.LineList); state.PopWorldMatrix(); } mat = Matrix.Identity; Vector4 v; for (int i = 0; i < spheres.Count; i++) { v = spheres[i]; mat.M11 = v.W; mat.M22 = v.W; mat.M33 = v.W; mat.M41 = v.X; mat.M42 = v.Y; mat.M43 = v.Z; state.PushWorldMatrix(ref mat); sphereVS.Draw(state, null, PrimitiveType.LineList); state.PopWorldMatrix(); } state.PopRenderState(); state.PopCamera(); } }
public void Draw(DrawState state) { Xen.Ex.Shaders.FillSolidColour shader = state.GetShader <Xen.Ex.Shaders.FillSolidColour>(); shader.FillColour = colour; shader.Bind(state); vertices.Draw(state, null, PrimitiveType.TriangleStrip); }
private void DrawSphere(DrawState state) { //draw the geometry with a solid colour shader if (geometry.CullTest(state)) { Xen.Ex.Shaders.FillSolidColour shader = state.GetShader <Xen.Ex.Shaders.FillSolidColour>(); shader.FillColour = lightColour.ToVector4(); shader.Bind(state); geometry.Draw(state); } }
//NEW CODE private void DrawBoundingBoxes(DrawState state) { //First, get the animated bone transforms of the model. //These transforms are in 'bone-space', not in world space. ReadOnlyArrayCollection <Transform> boneAnimationTransforms = model.GetAnimationController().GetTransformedBones(state); //Get a simple shader from Xen.Ex that fills a solid colour Xen.Ex.Shaders.FillSolidColour shader = state.GetShader <Xen.Ex.Shaders.FillSolidColour>(); shader.FillColour = Color.White.ToVector4(); shader.Bind(state); //push the render state... state.PushRenderState(); //disable back face culling state.RenderState.DepthColourCull.CullMode = CullMode.None; //set to wireframe state.RenderState.DepthColourCull.FillMode = FillMode.WireFrame; //loop through all the geometry data in the model.. //(note, the sample model has only 1 geometry instance) Xen.Ex.Graphics.Content.SkeletonData modelSkeleton = model.ModelData.Skeleton; Matrix matrix; int boxIndex = 0; foreach (Xen.Ex.Graphics.Content.MeshData meshData in model.ModelData.Meshes) { foreach (Xen.Ex.Graphics.Content.GeometryData geometry in meshData.Geometry) { //now loop through all bones used by this geometry for (int geometryBone = 0; geometryBone < geometry.BoneIndices.Length; geometryBone++) { //index of the bone (a piece of geometry may not use all the bones in the model) int boneIndex = geometry.BoneIndices[geometryBone]; //get the base transform of the bone (the transform when not animated) Transform boneTransform = modelSkeleton.BoneWorldTransforms[boneIndex]; //multiply the transform with the animation bone-local transform //it would be better to use Transform.Multiply() here to save data copying on the xbox boneTransform *= boneAnimationTransforms[boneIndex]; //Get the transform as a matrix boneTransform.GetMatrix(out matrix); //push the matrix state.PushWorldMatrix(ref matrix); //draw the box if (boundingBoxes[boxIndex].CullTest(state)) { boundingBoxes[boxIndex].Draw(state); } boxIndex++; //pop the world matrix state.PopWorldMatrix(); } } } //pop the render state state.PopRenderState(); }
protected override void Initialise() { Camera3D camera = new Camera3D(); camera.LookAt(Vector3.Zero, new Vector3(0, 0, 5), Vector3.UnitY); //create the draw target. drawToScreen = new DrawTargetScreen(camera); drawToScreen.ClearBuffer.ClearColour = Color.CornflowerBlue; //create a shader to display the geometry (this is the same as tutorial 02) var lightDirection = new Vector3(1.0f, 0.5f, 0.5f); var material = new MaterialShader(); material.SpecularColour = Color.LightYellow.ToVector3(); //give the material a nice sheen var lights = new MaterialLightCollection(); lights.AmbientLightColour = Color.CornflowerBlue.ToVector3() * 0.5f; //set the ambient lights.CreateDirectionalLight(lightDirection, Color.Gray); //add the first of two light sources lights.CreateDirectionalLight(-lightDirection, Color.DarkSlateBlue); material.LightCollection = lights; //create a simpler shader to display the wireframe (and also used for the bounding cube) var simpleShader = new Xen.Ex.Shaders.FillSolidColour(); simpleShader.FillColour = Vector4.One * 0.01f; var sphereSize = new Vector3(0.5f, 0.5f, 0.5f); //create the complex sphere, this will have ~100k triangles. //pass in a shader for wireframe rendering sphere = new GeometryDrawer(new Xen.Ex.Geometry.Sphere(sphereSize, 200), material, simpleShader); //create the bounding cube sphereBoundingBox = new GeometryDrawer(new Xen.Ex.Geometry.Cube(sphereSize), simpleShader, null); //create the occluding cube, and position it close to the camera cube = new GeometryDrawer(new Xen.Ex.Geometry.Cube(Vector3.One), material, null); cube.position = new Vector3(0, 0, 2.75f); //add the cube first (so it can draw first, potentially occluding the sphere) //if the cube was added second, it would have no effect, as it would draw after the sphere drawToScreen.Add(cube); //create the predicate, passing in the sphere and bounding box var predicate = new Xen.Ex.Scene.DrawPredicate(sphere, sphereBoundingBox); //add the DrawPredicate (the DrawPredicate draws it's children) drawToScreen.Add(predicate); //statistic overlay statOverlay = new Xen.Ex.Graphics2D.Statistics.DrawStatisticsDisplay(this.UpdateManager); drawToScreen.Add(statOverlay); }