//----------------------------------------------------------------------------- // Material.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Text; using RacingGame.Helpers; using RacingGame.Shaders; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace RacingGame.Graphics { /// /// Material class for DirectX materials used for Models. Consists of /// normal DirectX material settings (ambient, diffuse, specular), /// the diffuse texture and optionally of normal map, height map and shader /// parameters. /// public class Material : IDisposable { /// /// Default color values are: /// 0.15f for ambient and 1.0f for diffuse and 1.0f specular. /// public static readonly Color DefaultAmbientColor = new Color(40, 40, 40), DefaultDiffuseColor = new Color(210, 210, 210), DefaultSpecularColor = new Color(255, 255, 255); /// /// Default specular power (24) /// const float DefaultSpecularPower = 24.0f; /// /// Parallax amount for parallax and offset shaders. /// public const float DefaultParallaxAmount = 0.04f; /// /// Colors /// public Color diffuseColor = DefaultDiffuseColor, ambientColor = DefaultAmbientColor, specularColor = DefaultSpecularColor; /// /// Specular power /// public float specularPower = DefaultSpecularPower; /// /// Diffuse texture for the material. Can be null for unused. /// public Texture diffuseTexture = null; /// /// Normal texture in case we use normal mapping. Can be null for unused. /// public Texture normalTexture = null; /// /// Height texture in case we use parallax mapping. Can be null for unused. /// public Texture heightTexture = null; /// /// Detail texture, used for landscape rendering. Can be null for unused. /// public Texture detailTexture = null; /// /// Parallax amount for parallax and offset shaders. /// public float parallaxAmount = DefaultParallaxAmount; /// /// Checks if the diffuse texture has alpha /// public bool HasAlpha { get { if (diffuseTexture != null) return diffuseTexture.HasAlphaPixels; else return false; } } /// /// Create material, just using default values. /// public Material() { } /// /// Create material, just using default color values. /// public Material(string setDiffuseTexture) { diffuseTexture = new Texture(setDiffuseTexture); } /// /// Create material /// public Material(Color setAmbientColor, Color setDiffuseColor, string setDiffuseTexture) { ambientColor = setAmbientColor; diffuseColor = setDiffuseColor; diffuseTexture = new Texture(setDiffuseTexture); // Leave rest to default } /// /// Create material /// public Material(Color setAmbientColor, Color setDiffuseColor, Texture setDiffuseTexture) { ambientColor = setAmbientColor; diffuseColor = setDiffuseColor; diffuseTexture = setDiffuseTexture; // Leave rest to default } /// /// Create material /// public Material(string setDiffuseTexture, string setNormalTexture) { diffuseTexture = new Texture(setDiffuseTexture); normalTexture = new Texture(setNormalTexture); // Leave rest to default } /// /// Create material /// public Material(string setDiffuseTexture, string setNormalTexture, string setHeightTexture) { diffuseTexture = new Texture(setDiffuseTexture); normalTexture = new Texture(setNormalTexture); heightTexture = new Texture(setHeightTexture); // Leave rest to default } /// /// Create material /// public Material(Color setAmbientColor, Color setDiffuseColor, Color setSpecularColor, string setDiffuseTexture, string setNormalTexture, string setHeightTexture, string setDetailTexture) { ambientColor = setAmbientColor; diffuseColor = setDiffuseColor; specularColor = setSpecularColor; diffuseTexture = new Texture(setDiffuseTexture); if (String.IsNullOrEmpty(setNormalTexture) == false) normalTexture = new Texture(setNormalTexture); if (String.IsNullOrEmpty(setHeightTexture) == false) heightTexture = new Texture(setHeightTexture); if (String.IsNullOrEmpty(setDetailTexture) == false) detailTexture = new Texture(setDetailTexture); // Leave rest to default } /// /// Create material /// /// Effect public Material(Effect effect) { if (effect == null) throw new ArgumentNullException("effect"); EffectParameter diffuseTextureParameter = effect.Parameters["diffuseTexture"]; if (diffuseTextureParameter != null) diffuseTexture = new Texture( diffuseTextureParameter.GetValueTexture2D()); EffectParameter normalTextureParameter = effect.Parameters["normalTexture"]; if (normalTextureParameter != null) normalTexture = new Texture( normalTextureParameter.GetValueTexture2D()); EffectParameter diffuseColorParameter = effect.Parameters["diffuseColor"]; if (diffuseColorParameter != null) diffuseColor = new Color(diffuseColorParameter.GetValueVector4()); EffectParameter ambientColorParameter = effect.Parameters["ambientColor"]; if (ambientColorParameter != null) ambientColor = new Color(ambientColorParameter.GetValueVector4()); EffectParameter specularColorParameter = effect.Parameters["specularColor"]; if (specularColorParameter != null) specularColor = new Color(specularColorParameter.GetValueVector4()); EffectParameter specularPowerParameter = effect.Parameters["specularPower"]; if (specularPowerParameter != null) specularPower = specularPowerParameter.GetValueSingle(); } /// /// Dispose /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// /// Dispose /// /// Disposing protected virtual void Dispose(bool disposing) { if (disposing) { if (diffuseTexture != null) diffuseTexture.Dispose(); if (normalTexture != null) normalTexture.Dispose(); if (heightTexture != null) heightTexture.Dispose(); if (detailTexture != null) detailTexture.Dispose(); } } } }