public void Draw(DrawState state) { //switch rendering mode based on the TutorialRenderMode flag switch (state.GetDrawFlag <TutorialRenderMode>()) { case TutorialRenderMode.DepthOutput: //bind the depth output shader state.GetShader <Xen.Ex.Shaders.NonLinearDepthOutRg>().Bind(state); break; case TutorialRenderMode.DrawShadow: //bind the shadow rendering shader Shader.ShadowShader shader = state.GetShader <Shader.ShadowShader>(); shader.TextureMap = material.TextureMap; shader.TextureSampler = material.TextureMapSampler; shader.Bind(state); break; default: //no flag known specified material.Bind(state); break; } //draw the ground vertices.Draw(state, null, PrimitiveType.TriangleFan); }
//draw the ground plane.. public void Draw(DrawState state) { //first bind the material shader material.Bind(state); //then draw the vertices vertices.Draw(state, null, PrimitiveType.TriangleFan); }
public Handle(MaterialShader material) { var bindings = material.Bind(); foreach (var handle in bindings) { if (handle.DefaultState) { DefaultBindings.Add(handle); continue; } ProcessState(handle); } }
//draw the sphere private void DrawGeometry(DrawState state) { //animate the material alpha.. (in a sin wave btween 0 and 1) shader.Alpha = (float)Math.Sin(state.TotalTimeSeconds * 2) * 0.5f + 0.5f; //push the world matrix, multiplying by the current matrix if there is one state.PushWorldMatrixMultiply(ref this.worldMatrix); //cull test the sphere if (sphereGeometry.CullTest(state)) { //bind the shader shader.Bind(state); //draw the sphere geometry sphereGeometry.Draw(state); } //always pop the matrix afterwards state.PopWorldMatrix(); }
/// <summary> /// Draw the model. This class automatically assigns shaders when drawing /// </summary> /// <param name="state"></param> public void Draw(DrawState state) { if (modelData == null) { throw new InvalidOperationException("ModelData is null"); } if (controller != null) { controller.WaitForAsyncAnimation(state, state.FrameIndex, true); if (controller.IsDisposed) { controller = null; } } if (controller != null && hierarchy == null) { hierarchy = new MaterialAnimationTransformHierarchy(modelData.skeleton); } if (hierarchy != null) { hierarchy.UpdateTransformHierarchy(controller.transformedBones); } ModelInstanceShaderProvider shaderProvider = this.shaderProvider; MaterialLightCollection lights = this.lights; ShaderProviderFlag providerFlag; MaterialLightCollection.LightCollectionFlag lightsFlag; state.GetDrawFlag(out providerFlag); if (providerFlag.OverrideShaderProvider) { shaderProvider = providerFlag.ShaderProvider; } state.GetDrawFlag(out lightsFlag); if (lightsFlag.OverrideLightCollection) { lights = lightsFlag.LightCollection; } if (shaderProvider != null) { if (controller != null) { shaderProvider.BeginDraw(state, controller.transformedBones, hierarchy.GetMatrixData()); } else { shaderProvider.BeginDraw(state); } } Vector3 boundsMin, boundsMax; ContainmentType cullModel = ContainmentType.Contains; //if there is just one geometry object, then the ICullable.CullTest() call will have been suficient. bool skipCullTest = this.modelData != null && this.modelData.meshes.Length == 1 && this.modelData.meshes[0].geometry.Length == 1; if (!skipCullTest) { if (controller != null) { cullModel = state.Culler.IntersectBox(ref controller.boundsMin, ref controller.boundsMax); } else { cullModel = state.Culler.IntersectBox(ref modelData.staticBounds.minimum, ref modelData.staticBounds.maximum); } } if (cullModel != ContainmentType.Disjoint) { for (int m = 0; m < modelData.meshes.Length; m++) { MeshData mesh = modelData.meshes[m]; if (shaderProvider != null) { shaderProvider.BeginMesh(state, mesh); } ContainmentType cullMesh = cullModel; if (cullModel == ContainmentType.Intersects && modelData.meshes.Length > 1) { if (controller != null) { controller.ComputeMeshBounds(m, out boundsMin, out boundsMax); cullMesh = state.Culler.IntersectBox(ref boundsMin, ref boundsMax); } else { cullMesh = state.Culler.IntersectBox(ref mesh.staticBounds.minimum, ref mesh.staticBounds.maximum); } } if (cullMesh != ContainmentType.Disjoint) { for (int g = 0; g < mesh.geometry.Length; g++) { GeometryData geom = mesh.geometry[g]; MaterialShader shader = geom.MaterialShader; if (shaderProvider != null && shaderProvider.BeginGeometryShaderOverride(state, geom, lights)) { shader = null; } bool cullTest = true; if (cullMesh == ContainmentType.Intersects && mesh.geometry.Length > 1) { if (controller != null) { controller.ComputeGeometryBounds(m, g, out boundsMin, out boundsMax); cullTest = state.Culler.TestBox(ref boundsMin, ref boundsMax); } else { cullTest = state.Culler.TestBox(ref geom.staticBounds.minimum, ref geom.staticBounds.maximum); } } if (cullTest) { if (shader != null) { shader.AnimationTransforms = hierarchy; shader.Lights = lights; shader.Bind(state); } geom.Vertices.Draw(state, geom.Indices, PrimitiveType.TriangleList); } if (shaderProvider != null) { shaderProvider.EndGeometry(state, geom); } } } if (shaderProvider != null) { shaderProvider.EndMesh(state, mesh); } } } if (shaderProvider != null) { shaderProvider.EndDraw(state); } }
/// <summary> /// Draw all the model batch instances /// </summary> /// <param name="state"></param> public void Draw(DrawState state) { if (modelData == null) { throw new InvalidOperationException("ModelData is null"); } if (geometry == null) { SetupGeometry(); } int geometryIndex = 0; BatchModelShaderProvider shaderProvider = this.shaderProvider; MaterialLightCollection lights = this.lights; ShaderProviderFlag providerFlag; MaterialLightCollection.LightCollectionFlag lightsFlag; state.GetDrawFlag(out providerFlag); if (providerFlag.OverrideShaderProvider) { shaderProvider = providerFlag.ShaderProvider; } state.GetDrawFlag(out lightsFlag); if (lightsFlag.OverrideLightCollection) { lights = lightsFlag.LightCollection; } if (shaderProvider != null) { shaderProvider.BeginDraw(state); } //loop through the model data for (int m = 0; m < modelData.meshes.Length; m++) { MeshData mesh = modelData.meshes[m]; if (shaderProvider != null) { shaderProvider.BeginMesh(state, mesh); } for (int g = 0; g < mesh.geometry.Length; g++) { GeometryData geom = mesh.geometry[g]; GeometrySet set = this.geometry[geometryIndex]; if (set.count > 0) { bool instancing = state.SupportsHardwareInstancing && set.count > 2; if (shaderProvider == null || !shaderProvider.BeginGeometryShaderOverride(state, geom, lights, instancing)) { MaterialShader shader = geom.MaterialShader; shader.AnimationTransforms = null; shader.UseHardwareInstancing = instancing; shader.Lights = lights; shader.Bind(state); } //draw the geometry if (instancing) { state.DrawBatch(geom.Vertices, geom.Indices, PrimitiveType.TriangleList, null, set.instances, set.count); } else { for (int i = 0; i < set.count; i++) { state.PushWorldMatrixMultiply(ref set.instances[i]); geom.Vertices.Draw(state, geom.Indices, PrimitiveType.TriangleList); state.PopWorldMatrix(); } } if (shaderProvider != null) { shaderProvider.EndGeometry(state, geom); } } set.count = 0; geometryIndex++; } if (shaderProvider != null) { shaderProvider.EndMesh(state, mesh); } } if (shaderProvider != null) { shaderProvider.EndDraw(state); } drawCount = 0; }