//draw all the instances //Note this class must be drawn after all the instances, to keep drawing in //the correct order. Otherwise instance culling may appear a frame delayed. public void Draw(DrawState state) { //get the instancing shader and bind it var shader = state.GetShader<Shader.Tutorial16>(); using (state + shader) { //in this case, Xen.Ex.Geometry.Sphere implements IDrawBatch - allowing drawing a batch easily //otherwise, a call to: // // vertices.DrawInstances(...) // //can be made (internally, the Sphere class does exactly this) //Note that all prebuilt shaders in Xen supports hardware instancing, and could be used in place of the custom shader //Get a dynamic instance buffer from the DrawState. //Pass in the matrix array to fill the buffer with the matrices //(Alternatively, get an empty instance buffer and write each matrix one at a time) var buffer = state.GetDynamicInstanceBuffer(this.instanceMatrices, this.instanceCount); //draw the dynamic instances geometry.DrawBatch(state, buffer); //reset the counter for the next frame instanceCount = 0; } }
public void Draw(DrawState state) { //bind the background filling shader using (state.Shader.Push(state.GetShader<Shaders.BackgroundFill>())) { geometry.Draw(state); } }
private void SetupShadowShader(DrawState state) { var shadowCamera = this.shadowMapTarget.Camera; //compute the view*projection matrix for the shadow map camera... Matrix view, projection, viewProjection; shadowCamera.GetViewMatrix(out view); shadowCamera.GetProjectionMatrix(out projection, this.shadowMapTarget.Size); Matrix.Multiply(ref view, ref projection, out viewProjection); //and the view direction Vector3 viewDirection; shadowCamera.GetCameraViewDirection(out viewDirection); //set the matrix and other constants in the shadow mapping shader instances var shader = state.GetShader<Shader.ShadowShader>(); //non-blending shader shader.LightColour = lightColour; shader.ShadowMap = this.shadowMapTarget.GetTexture(); shader.SetShadowMapProjection(ref viewProjection); shader.SetShadowViewDirection(ref viewDirection); }
public void Draw(DrawState state) { //switch rendering mode based on the TutorialRenderMode flag switch (state.DrawFlags.GetFlag<TutorialRenderMode>()) { case TutorialRenderMode.DepthOutput: //bind the depth output shader state.Shader.Push(state.GetShader<Xen.Ex.Shaders.NonLinearDepthOut>()); break; case TutorialRenderMode.DrawShadow: //bind the shadow rendering shader Shader.ShadowShader shader = state.GetShader<Shader.ShadowShader>(); shader.TextureMap = material.Textures.TextureMap; shader.TextureSampler = material.Textures.TextureMapSampler; state.Shader.Push(shader); break; default: //no flag known specified state.Shader.Push(material); break; } //draw the ground vertices.Draw(state, indices, PrimitiveType.TriangleList); state.Shader.Pop(); }
//return the shader to use: public IShader BeginGeometry(DrawState state, GeometryData geometry) { //query the draw flag, switch (state.DrawFlags.GetFlag<TutorialRenderMode>()) { case TutorialRenderMode.DrawShadow: { //return the shader that draws the shadow var shader = state.GetShader<Shader.ShadowShader>(); shader.TextureMap = geometry.MaterialData.Texture; return shader; } //return the shader that outpus depth case TutorialRenderMode.DepthOutput: return state.GetShader<Xen.Ex.Shaders.NonLinearDepthOut>(); default: //no flag is set, or it is in it's default state return null; //do not change the shader } }
//return the shader to use: public IShader BeginGeometry(DrawState state, GeometryData geometry) { //query the draw flag, switch (state.DrawFlags.GetFlag<RenderMode>()) { case RenderMode.DrawShadow: { //return the shader shader Shaders.ShadowShader shader = state.GetShader<Shaders.ShadowShader>(); shader.TextureMap = geometry.MaterialData.Texture; return shader; } case RenderMode.DepthOutput: return state.GetShader<Xen.Ex.Shaders.NonLinearDepthOut>(); default: return null; //no change } }
private void DrawSphere(DrawState state) { //draw the geometry with a solid colour shader if (geometry.CullTest(state)) { var shader = state.GetShader<Xen.Ex.Shaders.FillSolidColour>(); shader.FillColour = lightColour.ToVector4(); using (state.Shader.Push(shader)) geometry.Draw(state); } }
public void Draw(DrawState state) { //scale the mesh Matrix scaleMatrix; Matrix.CreateScale(this.scale, out scaleMatrix); Xen.Ex.Shaders.FillVertexColour shader = state.GetShader<Xen.Ex.Shaders.FillVertexColour>(); using (state.RenderState.Push()) using (state.Shader.Push(shader)) using (state.WorldMatrix.PushMultiply(ref worldMatrix)) { state.WorldMatrix.Multiply(ref scaleMatrix); //setup blending state.RenderState.CurrentRasterState.CullMode = CullMode.None; state.RenderState.CurrentBlendState = AlphaBlendState.AdditiveSaturate; state.RenderState.CurrentDepthState.DepthWriteEnabled = false; //draw vertices.Draw(state, indices, PrimitiveType.TriangleList); } //this is a hack :-) //flicker the scale //every so often, target a new scale if (random.Next(100) > 75) scaleTarget = (float)random.NextDouble() * 0.4f + 0.6f; //interpolate to the scale target this.scale = this.scale * 0.75f + this.scaleTarget * 0.25f; }
//draw all the instances public void Draw(DrawState state) { //get the instancing shader and bind it var shader = state.GetShader<Shader.Tutorial16>(); using (state + shader) { //draw everything in one go! this.geometry.DrawBatch(state, this.staticInstanceBuffer); } }
//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. var boneAnimationTransforms = model.GetAnimationController().GetTransformedBones(state); //Get a simple shader from Xen.Ex that fills a solid colour var shader = state.GetShader<Xen.Ex.Shaders.FillSolidColour>(); //set the fill colour shader.FillColour = Color.White.ToVector4(); using (state + shader) using (state.RenderState.Push()) { //disable back face culling state.RenderState.CurrentRasterState.CullMode = CullMode.None; //set to wireframe state.RenderState.CurrentRasterState.FillMode = FillMode.WireFrame; //loop through all the geometry data in the model.. //(note, the sample model has only 1 geometry instance) var modelSkeleton = model.ModelData.Skeleton; int boxIndex = 0; foreach (var meshData in model.ModelData.Meshes) { foreach (var 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) var 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]; //push the transform using (state.WorldMatrix.PushMultiply(ref boneTransform)) { //draw the box if (boundingBoxes[boxIndex].CullTest(state)) boundingBoxes[boxIndex].Draw(state); boxIndex++; } } } } } }
private void DrawSphere(DrawState state) { //draw the geometry with a solid colour shader if (geometry.CullTest(state)) { var shader = state.GetShader<Xen.Ex.Shaders.FillSolidColour>(); shader.FillColour = lightColour.ToVector4(); using (state.Shader.Push(shader)) { //state.RenderState.CurrentRasterState.FillMode = FillMode.WireFrame; geometry.Draw(state); //state.RenderState.CurrentRasterState.FillMode = FillMode.Solid; } } }
private void DrawSphere(DrawState state) { //draw the geometry with a solid colour shader if (geometry.CullTest(state)) { var shader = state.GetShader<Xen.Ex.Shaders.FillSolidColour>(); shader.FillColour = lightColour.ToVector4(); using (state + shader) // this add operator is a shortcut for 'state.Shader.Push(IShader)' { geometry.Draw(state); } } }