} // Begin #endregion #region Render Model /// <summary> /// Render a model using the simple technique. /// </summary> internal void RenderModelSimple(ref Matrix worldMatrix, Model model, BlinnPhong material, int meshIndex, int meshPart) { try { Resource.CurrentTechnique = blinnPhongSimpleTechnique; // Matrix Matrix worldViewProjection; Matrix.Multiply(ref worldMatrix, ref viewProjectionMatrix, out worldViewProjection); spWorldViewProjMatrix.QuickSetValue(ref worldViewProjection); spWorldMatrix.QuickSetValue(ref worldMatrix); // Diffuse if (material.DiffuseTexture == null) { spDiffuseColor.Value = material.DiffuseColor; spDiffuseTexture.Value = Texture.BlackTexture; } else { spDiffuseColor.Value = Color.Black; spDiffuseTexture.Value = material.DiffuseTexture; } // Specular spSpecularIntensity.Value = material.SpecularIntensity; spSpecularTexture.Value = material.SpecularTexture ?? Texture.WhiteTexture; // Reflection if (material.ReflectionTexture != null) { spReflectionTexture.Value = material.ReflectionTexture; if (material.ReflectionTexture.IsRgbm) { spIsRGBM.Value = true; spMaxRange.Value = material.ReflectionTexture.RgbmMaxRange; } else { spIsRGBM.Value = false; } spReflectionTextured.Value = true; } else { spReflectionTextured.Value = false; } Resource.CurrentTechnique.Passes[0].Apply(); model.RenderMeshPart(meshIndex, meshPart); } catch (Exception e) { throw new InvalidOperationException("Bling Phong Material: Unable to render model.", e); } } // RenderModelSimple
} // Begin #endregion #region Render Model /// <summary> /// Render a model. /// </summary> internal void RenderModel(ref Matrix worldMatrix, Model model, BlinnPhong material, int meshIndex, int meshPart, Vector3 spotLightPosition, Vector3 spotLightDirection, Color spotLightColor, float spotLightIntensity, float spotLightInnerAngle, float spotLightOuterAngle, float spotLightRange, Vector3 pointLightPos1, Color pointLightColor1, float pointLightIntensity1, float pointLightRange1, Vector3 pointLightPos2, Color pointLightColor2, float pointLightIntensity2, float pointLightRange2) { // Set Render States. EngineManager.Device.RasterizerState = material.BothSides ? RasterizerState.CullNone : RasterizerState.CullCounterClockwise; try { spWorldViewProjMatrix.Value = worldMatrix * viewprojectionMatrix; spWorldMatrix.Value = worldMatrix; spWorldITMatrix.Value = Matrix.Transpose(Matrix.Invert(worldMatrix)); spSpecularIntensity.Value = material.SpecularIntensity; spSpecularPower.Value = material.SpecularPower; spDiffuseColor.Value = material.DiffuseColor; spAlphaBlending.Value = material.AlphaBlending; // Diffuse if (material.DiffuseTexture == null) { spDiffuseColor.Value = material.DiffuseColor; spDiffuseTexture.Value = Texture.BlackTexture; } else { spDiffuseColor.Value = Color.Black; spDiffuseTexture.Value = material.DiffuseTexture; } // Reflection if (material.ReflectionTexture != null) { spReflectionTexture.Value = material.ReflectionTexture; if (material.ReflectionTexture.IsRgbm) { spIsRGBM.Value = true; spMaxRange.Value = material.ReflectionTexture.RgbmMaxRange; } else { spIsRGBM.Value = false; } spReflectionTextured.Value = true; } else { spReflectionTextured.Value = false; } // Spot Light // spSpotLightDirection.Value = spotLightDirection; spSpotLightPosition.Value = spotLightPosition; spSpotLightColor.Value = spotLightColor; spSpotLightIntensity.Value = spotLightIntensity; spSpotLightInnerAngle.Value = spotLightInnerAngle * (3.141592f / 180.0f); spSpotLightOuterAngle.Value = spotLightOuterAngle * (3.141592f / 180.0f); spInvSpotLightRadius.Value = 1 / spotLightRange; // Point Light // spPointLightPos1.Value = pointLightPos1; spPointLightColor1.Value = pointLightColor1; spPointLightIntensity1.Value = pointLightIntensity1; spInvPointLightRadius1.Value = 1 / pointLightRange1; spPointLightPos2.Value = pointLightPos2; spPointLightColor2.Value = pointLightColor2; spPointLightIntensity2.Value = pointLightIntensity2; spInvPointLightRadius2.Value = 1 / pointLightRange2; Resource.CurrentTechnique.Passes[0].Apply(); model.RenderMeshPart(meshIndex, meshPart); } catch (Exception e) { throw new InvalidOperationException("Forward Bling Phong Material: Unable to render model.", e); } }
} // RenderModelSkinned /// <summary> /// Render a model using the parallax technique. /// </summary> internal void RenderModelParallax(ref Matrix worldMatrix, Model model, BlinnPhong material, int meshIndex, int meshPart) { try { Resource.CurrentTechnique = blinnPhongWithParrallaxTechnique; // Matrix Matrix worldViewProjection; Matrix.Multiply(ref worldMatrix, ref viewProjectionMatrix, out worldViewProjection); spWorldViewProjMatrix.QuickSetValue(ref worldViewProjection); spWorldMatrix.QuickSetValue(ref worldMatrix); // Diffuse if (material.DiffuseTexture == null) { spDiffuseColor.Value = material.DiffuseColor; spDiffuseTexture.Value = Texture.BlackTexture; } else { spDiffuseColor.Value = Color.Black; spDiffuseTexture.Value = material.DiffuseTexture; } // Specular spSpecularIntensity.Value = material.SpecularIntensity; spSpecularTexture.Value = material.SpecularTexture ?? Texture.WhiteTexture; // Parallax spNormalTexture.Value = material.NormalTexture; spNormalTextureSize.Value = new Vector2(material.NormalTexture.Width, material.NormalTexture.Height); EngineManager.Device.SamplerStates[1] = SamplerState.AnisotropicClamp; spLODThreshold.Value = material.ParallaxLodThreshold; spMinimumNumberSamples.Value = material.ParallaxMinimumNumberSamples; spMaximumNumberSamples.Value = material.ParallaxMaximumNumberSamples; spHeightMapScale.Value = material.ParallaxHeightMapScale; spWorldITMatrix.Value = Matrix.Transpose(Matrix.Invert(worldMatrix)); // Reflection if (material.ReflectionTexture != null) { spReflectionTexture.Value = material.ReflectionTexture; if (material.ReflectionTexture.IsRgbm) { spIsRGBM.Value = true; spMaxRange.Value = material.ReflectionTexture.RgbmMaxRange; } else { spIsRGBM.Value = false; } spReflectionTextured.Value = true; } else { spReflectionTextured.Value = false; } Resource.CurrentTechnique.Passes[0].Apply(); model.RenderMeshPart(meshIndex, meshPart); } catch (Exception e) { throw new InvalidOperationException("Bling Phong Material: Unable to render model.", e); } } // RenderModelParallax
/// <summary> /// Creates the configuration controls of this asset. /// </summary> public static void AddControls(BlinnPhong asset, Window owner) { #region Diffuse GroupBox groupDiffuse = CommonControls.Group("Diffuse", owner); // Diffuse Color var sliderDiffuseColor = CommonControls.SliderColor("Diffuse Color", groupDiffuse, asset.DiffuseColor, asset, "DiffuseColor"); // Diffuse Texture var assetSelectorDiffuseTexture = CommonControls.AssetSelector<Texture>("Diffuse Texture", groupDiffuse, asset, "DiffuseTexture"); groupDiffuse.AdjustHeightFromChildren(); #endregion #region Specular GroupBox groupSpecular = CommonControls.Group("Specular", owner); CheckBox checkBoxSpecularPowerFromTexture = null; // Specular Intensity var sliderSpecularIntensity = CommonControls.SliderNumericFloat("Specular Intensity", groupSpecular, asset.SpecularIntensity, false, true, 0, 2, asset, "SpecularIntensity"); // Specular Power var sliderSpecularPower = CommonControls.SliderNumericFloat("Specular Power", groupSpecular, asset.SpecularPower, true, true, 0, 100, asset, "SpecularPower"); sliderSpecularPower.Draw += delegate { sliderSpecularPower.Enabled = !checkBoxSpecularPowerFromTexture.Enabled || (checkBoxSpecularPowerFromTexture.Enabled && !checkBoxSpecularPowerFromTexture.Checked); }; // Specular Texture var assetSelectorSpecularTexture = CommonControls.AssetSelector<Texture>("Specular Texture", groupSpecular, asset, "SpecularTexture"); assetSelectorSpecularTexture.ItemIndexChanged += delegate { checkBoxSpecularPowerFromTexture.Enabled = asset.SpecularTexture != null; }; // Specular Texture Power Enabled checkBoxSpecularPowerFromTexture = CommonControls.CheckBox("Use specular power from Texture", groupSpecular, asset.SpecularPowerFromTexture, asset, "SpecularPowerFromTexture", "Indicates if the specular power will be read from the texture (the alpha channel of the specular texture) or from the specular power property."); // Reflection Texture var assetSelectorReflectionTexture = CommonControls.AssetSelector<TextureCube>("Reflection Texture", groupSpecular, asset, "ReflectionTexture"); groupSpecular.AdjustHeightFromChildren(); #endregion #region Normals GroupBox groupNormals = CommonControls.Group("Normals", owner); // Normal Texture var assetSelectorNormalTexture = CommonControls.AssetSelector<Texture>("Normal Texture", groupNormals, asset, "NormalTexture"); // Parallax Enabled CheckBox checkBoxParallaxEnabled = CommonControls.CheckBox("Enabled", groupNormals, asset.ParallaxEnabled, asset, "ParallaxEnabled"); // Parallax Threshold var sliderParallaxLodThreshold = CommonControls.SliderNumericInt("Parallax LOD Threshold", groupNormals, asset.ParallaxLodThreshold, false, false, 0, 10, asset, "ParallaxLodThreshold"); // Parallax Minimum Number Samples var sliderParallaxMinimumNumberSamples = CommonControls.SliderNumericInt("Parallax Minimum Number Samples", groupNormals, asset.ParallaxMinimumNumberSamples, false, false, 0, 50, asset, "ParallaxMinimumNumberSamples"); // Parallax Maximum Number Samples var sliderParallaxMaximumNumberSamples = CommonControls.SliderNumericInt("Parallax Maximum Number Samples", groupNormals, asset.ParallaxMaximumNumberSamples, false, false, 0, 500, asset, "ParallaxMaximumNumberSamples"); // Parallax Maximum Number Samples var sliderParallaxHeightMapScale = CommonControls.SliderNumericFloat("Parallax Height Map Scale", groupNormals, asset.ParallaxHeightMapScale, false, false, 0, 1, asset, "ParallaxHeightMapScale"); checkBoxParallaxEnabled.CheckedChanged += delegate { asset.ParallaxEnabled = checkBoxParallaxEnabled.Checked; sliderParallaxLodThreshold.Enabled = asset.ParallaxEnabled; sliderParallaxMinimumNumberSamples.Enabled = asset.ParallaxEnabled; sliderParallaxMaximumNumberSamples.Enabled = asset.ParallaxEnabled; sliderParallaxHeightMapScale.Enabled = asset.ParallaxEnabled; }; assetSelectorNormalTexture.Draw += delegate { if (asset.NormalTexture == null) checkBoxParallaxEnabled.Enabled = false; }; groupNormals.AdjustHeightFromChildren(); #endregion } // AddControls