예제 #1
0
        /// <summary>
        /// Constructs the font object using the desired font family. If the desired family
        /// is not supported, the fallback family defined in the Settings file is used.
        /// </summary>
        public Font(Renderer renderer, string familyName, int height)
        {
            this.renderer = renderer;
            this.familyName = familyName;
            this.height = height + 5;

            // Attempt to create the Windows font object
            try
            {
                windowsFont = new System.Drawing.Font(familyName, this.height,
                    System.Drawing.FontStyle.Regular);
            }
            catch
            {
                // Attempt to create the font using the "fallback" font family
                // defined in the Settings file
                Log.Write("The desired font family was not available. Falling back on the default " +
                    "family defined in the Settings file.");
                windowsFont = new System.Drawing.Font(Dynamic2DLighting.Properties.Settings.Default.FallbackFontFamily,
                    this.height, System.Drawing.FontStyle.Regular);
            }

            d3dFont = new Direct3D.Font(renderer.Device, windowsFont);
            textSprite = new Direct3D.Sprite(renderer.Device);

            renderer.AddGraphicsObject(this);
        }
예제 #2
0
        public Effect(Renderer renderer, string filename)
        {
            //if (!File.Exists(filename))
            //    throw new FileNotFoundException(filename);

            if (renderer == null)
                throw new ArgumentNullException("renderer",
                    "Can't create an Effect without a valid renderer.");

            string compilationErrors = "";

            try
            {
                effect = Direct3D.Effect.FromFile(renderer.Device, filename, null, null,
                    null, ShaderFlags.None, null, out compilationErrors);

            }
            catch
            {
                Log.Write("Unable to create effect " + filename + ". The compilation errors were: \n\n" +
                    compilationErrors);
            }

            renderer.AddGraphicsObject(this);
        }
예제 #3
0
 /// <summary>
 /// Initializes a new instance of GeometryNode.
 /// </summary>
 public GeometryNode(Renderer renderer, SceneGraph sceneGraph, Matrix localTransform,
     IRenderable renderObject)
     : base(renderer, sceneGraph)
 {
     this.localTransform = localTransform;
     this.renderObject = renderObject;
 }
예제 #4
0
        public Light(Renderer renderer, float range, float intensity)
        {
            if (renderer == null)
            {
                Log.Write("'renderer' is null.");
                throw new ArgumentNullException("renderer", "Can't create a Light with a null " +
                    "renderer reference.");
            }

            if (intensity < 0.0f || intensity > 1.0f)
            {
                Log.Write("'intensity' is out of range.");
                throw new ArgumentOutOfRangeException("intensity", intensity,
                    "'intensity' is outside of the range [0,1].");
            }

            this.renderer = renderer;
            this.range = range;
            this.intensity = intensity;

            // Allocate memory for the attenuation map
            attenuationMap = new Texture(renderer, renderer.FullscreenSize.Width,
                renderer.FullscreenSize.Height, true);

            CreateAttenuationCircle();
            CreateAttenuationMap();
        }
예제 #5
0
        public Material(Renderer renderer, Direct3D.Material material,
            string diffuseMapFilename)
        {
            if (renderer == null)
                throw new ArgumentNullException("renderer",
                    "Cannot create a material with a null renderer reference.");

            this.renderer = renderer;
            d3dMaterial = material;
            diffuseMap = new Texture(renderer, diffuseMapFilename);
        }
예제 #6
0
        /// <summary>
        /// Builds the material using default parameters.
        /// </summary>
        public Material(Renderer renderer)
        {
            if (renderer == null)
                throw new ArgumentNullException("renderer",
                    "Cannot create a material with a null renderer reference.");

            this.renderer = renderer;

            d3dMaterial = new Direct3D.Material();
            d3dMaterial.Ambient = DefaultAmbientColor;
            d3dMaterial.Diffuse = DefaultDiffuseColor;
            d3dMaterial.Specular = DefaultSpecularColor;
            d3dMaterial.SpecularSharpness = DefaultShininess;
        }
예제 #7
0
        public ConvexHull(Renderer renderer, Mesh polygonGeometry)
        {
            if (renderer == null)
                throw new ArgumentNullException("renderer", "Can't create a ConvexHull with a null renderer.");

            if (polygonGeometry == null)
                throw new ArgumentNullException("polygonGeometry", "Can't create a ConvexHull without a valid " +
                    "mesh.");

            this.renderer = renderer;
            this.polygonGeometry = polygonGeometry;

            effect = GlobalResourceCache.CreateEffectFromFile(renderer,
                "Effect Files\\Dynamic2DLightingEffect.fx");
        }
예제 #8
0
        /// <summary>
        /// Creates an effect from a file. If the effect has already been created, the cached version
        /// is returned.
        /// </summary>
        public static Effect CreateEffectFromFile(Renderer renderer, string filename)
        {
            // Search cache first
            foreach (string cachedFilename in effectCache.Keys)
            {
                if (StringHelper.CaseInsensitiveCompare(cachedFilename, filename))
                    return effectCache[cachedFilename] as Effect;
            }

            Effect newEffect = new Effect(renderer, filename);

            effectCache.Add(filename, newEffect);

            return newEffect;
        }
예제 #9
0
        /// <summary>
        /// Creates the texture from a file.
        /// </summary>
        /// <param name="renderer">The renderer.</param>
        /// <param name="filename">The name of the texture file to load.</param>
        public Texture(Renderer renderer, string filename)
        {
            if (renderer == null)
                throw new ArgumentNullException("renderer",
                    "Unable to create texture without a valid renderer reference.");

            if (String.IsNullOrEmpty(filename))
                throw new ArgumentNullException("filename",
                    "Unable to create texture without valid filename.");

            // Add default extension if none is given
            if (Dynamic2DLighting.Properties.Settings.Default.DefaultTextureExtension.Length > 0 &&
                StringHelper.GetExtension(filename).Length == 0)
                filename = filename + "." + Dynamic2DLighting.Properties.Settings.Default.DefaultTextureExtension;

            this.renderer = renderer;
            texFilename = filename;

            // Try to load the texture
            try
            {
                if (File.Exists(filename) == false)
                    throw new FileNotFoundException(filename);

                ImageInformation imageInfo = TextureLoader.ImageInformationFromFile(filename);
                size = new Size(imageInfo.Width, imageInfo.Height);

                if (size.Width == 0 || size.Height == 0)
                    throw new InvalidOperationException(
                        "Image size=" + size + " is invalid, unable to create texture.");

                hasAlpha = imageInfo.Format == Format.Dxt5 ||
                    imageInfo.Format == Format.Dxt3 ||
                    imageInfo.Format.ToString().StartsWith("A");

                d3dTexture = TextureLoader.FromFile(this.renderer.Device, filename);

                loaded = true;

                renderer.AddGraphicsObject(this);
            }
            catch (Exception ex)
            {
                loaded = false;
                Log.Write("Failed to load texture " + filename +
                    ", will use empty texture! Error: " + ex.ToString());
            }
        }
예제 #10
0
        /// <summary>
        /// Initializes a new instance of SceneGraphNode.
        /// </summary>
        public SceneGraphNode(Renderer renderer, SceneGraph sceneGraph)
        {
            if (renderer == null)
            {
                Log.Write("Cannot create a SceneGraphNode with a null Renderer reference.");
                throw new ArgumentNullException("renderer",
                    "Cannot create a SceneGraphNode with a null Renderer reference.");
            }

            if (sceneGraph == null)
            {
                Log.Write("Cannot create a SceneGraphNode with a null SceneGraph reference.");
                throw new ArgumentNullException("sceneGraph",
                    "Cannot create a SceneGraphNode with a null SceneGraph reference.");
            }

            this.renderer = renderer;
            this.sceneGraph = sceneGraph;
        }
예제 #11
0
        /// <summary>
        /// Creates a font. If the font has already been created, the cached version is returned.
        /// </summary>
        public static Font CreateFont(Renderer renderer, string familyName, int height)
        {
            // Search cache first
            foreach (CachedFont cf in fontCache.Keys)
            {
                if (StringHelper.CaseInsensitiveCompare(cf.FamilyName, familyName) &&
                    cf.Height == height)
                    return fontCache[cf] as Font;
            }

            Font newFont = new Font(renderer, familyName, height);
            CachedFont cachedFont = new CachedFont();
            cachedFont.FamilyName = familyName;
            cachedFont.Height = height;

            fontCache.Add(cachedFont, newFont);

            return newFont;
        }
예제 #12
0
        public ConvexHull(Renderer renderer, Mesh polygonGeometry, Vector2 position,
            float rotation, float size)
        {
            if (renderer == null)
                throw new ArgumentNullException("renderer", "Can't create a ConvexHull with a null renderer.");

            if (polygonGeometry == null)
                throw new ArgumentNullException("polygonGeometry", "Can't create a ConvexHull without a valid " +
                    "mesh.");

            this.renderer = renderer;
            this.polygonGeometry = polygonGeometry;
            this.position = position;
            this.rotation = rotation;
            this.size = size;

            effect = GlobalResourceCache.CreateEffectFromFile(renderer,
                "Effect Files\\Dynamic2DLightingEffect.fx");

            CalculateWorldMatrix();
        }
        /// <summary>
        /// Initializes the BloomPostProcessor.
        /// </summary>
        public BloomPostProcessor(Renderer renderer)
        {
            if (renderer == null)
                throw new ArgumentNullException("renderer", "Can't create the BloomPostProcessor with a "
                    + "null Renderer reference.");

            this.renderer = renderer;

            brightPassTex = new Texture(renderer, renderer.FullscreenSize.Width / 2,
                renderer.FullscreenSize.Height / 2, true);
            blurHorizontalTex = new Texture(renderer, renderer.FullscreenSize.Width / 2,
                renderer.FullscreenSize.Height / 2, true);
            blurVerticalTex = new Texture(renderer, renderer.FullscreenSize.Width / 2,
                renderer.FullscreenSize.Height / 2, true);
            finalBloomImage = new Texture(renderer, renderer.FullscreenSize.Width,
                renderer.FullscreenSize.Height, true);

            fullscreenQuad = Mesh.Rectangle(renderer, Color.Black, renderer.FullscreenSize.Width,
                renderer.FullscreenSize.Height);

            bloomEffect = GlobalResourceCache.CreateEffectFromFile(renderer,
                "Effect Files\\Bloom.fx");
        }
예제 #14
0
        /// <summary>
        /// Prepares the form so that it behaves better with Direct3D; initializes
        /// the renderer; and finally hooks up appropriate event handlers for handling
        /// the Direct3D device.
        /// </summary>
        private void SetupForDirect3D(bool windowed, int desiredWidth, int desiredHeight,
            string windowTitle)
        {
            ClientSize = new Size(desiredWidth, desiredHeight);
            this.Text = windowTitle;

            try
            {
                renderer = new Renderer(windowed, this, desiredWidth, desiredHeight);
            }
            catch
            {
                throw new DirectXException("Unable to initialize Direct3D.");
            }

            this.Resize += new EventHandler(OnResize);
            renderer.Device.DeviceResizing += new System.ComponentModel.CancelEventHandler(OnDeviceResizing);
            renderer.WindowedChanged += new EventHandler(OnWindowedChanged);

            OnWindowedChanged(this, null);
        }
예제 #15
0
        public static Material FromFile(Renderer renderer, string xmlFilename)
        {
            if (renderer == null)
                throw new ArgumentNullException("renderer",
                    "Cannot create a material with a null renderer reference.");

            if (String.IsNullOrEmpty(xmlFilename))
                throw new ArgumentNullException("xmlFilename",
                    "Cannot load a material without a valid filename.");

            if (!File.Exists(xmlFilename))
                throw new FileNotFoundException(xmlFilename);

            Material material = new Material(renderer);
            XmlTextReader reader = new XmlTextReader(xmlFilename);

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.LocalName == "Ambient")
                    {
                        int a = int.Parse(reader.GetAttribute(0));
                        int r = int.Parse(reader.GetAttribute(1));
                        int g = int.Parse(reader.GetAttribute(2));
                        int b = int.Parse(reader.GetAttribute(3));

                        material.Ambient = Color.FromArgb(a, r, g, b);
                    }
                    else if (reader.LocalName == "Diffuse")
                    {
                        int a = int.Parse(reader.GetAttribute(0));
                        int r = int.Parse(reader.GetAttribute(1));
                        int g = int.Parse(reader.GetAttribute(2));
                        int b = int.Parse(reader.GetAttribute(3));

                        material.Diffuse = Color.FromArgb(a, r, g, b);
                    }
                    else if (reader.LocalName == "Specular")
                    {
                        int a = int.Parse(reader.GetAttribute(0));
                        int r = int.Parse(reader.GetAttribute(1));
                        int g = int.Parse(reader.GetAttribute(2));
                        int b = int.Parse(reader.GetAttribute(3));

                        material.Specular = Color.FromArgb(a, r, g, b);
                    }
                    else if (reader.LocalName == "Shininess")
                    {
                        float shininess = float.Parse(reader.ReadString());
                        material.Shininess = shininess;
                    }
                    else if (reader.LocalName == "DiffuseMap")
                    {
                        string filename = reader.ReadString();
                        material.DiffuseMap = GlobalResourceCache.CreateTextureFromFile(renderer,
                            filename);
                    }
                    else if (reader.LocalName == "NormalMap")
                    {
                        string filename = reader.ReadString();
                        material.NormalMap = GlobalResourceCache.CreateTextureFromFile(renderer,
                            filename);
                    }
                    else if (reader.LocalName == "HeightMap")
                    {
                        string filename = reader.ReadString();
                        material.HeightMap = GlobalResourceCache.CreateTextureFromFile(renderer,
                            filename);
                    }
                }
            }

            return material;
        }
예제 #16
0
        public Material(Renderer renderer, Color ambientColor, Color diffuseColor,
            string diffuseMapFilename)
        {
            if (renderer == null)
                throw new ArgumentNullException("renderer",
                    "Cannot create a material with a null renderer reference.");

            this.renderer = renderer;

            d3dMaterial = new Direct3D.Material();
            d3dMaterial.Ambient = ambientColor;
            d3dMaterial.Diffuse = diffuseColor;
            d3dMaterial.Specular = DefaultSpecularColor;
            d3dMaterial.SpecularSharpness = DefaultShininess;

            diffuseMap = new Texture(renderer, diffuseMapFilename);
        }
예제 #17
0
        /// <summary>
        /// Builds a rectangular mesh, centered around the origin.
        /// </summary>
        public static Mesh Rectangle(Renderer renderer, Color color, float width, float height,
            float textureMapTiling)
        {
            Mesh rectMesh = new Mesh(renderer, 4, 2);

            rectMesh.AddVertex(0, new Vector3(-width / 2, height / 2, 1.0f), color,
                new Vector2(0.0f, 0.0f));
            rectMesh.AddVertex(1, new Vector3(width / 2, height / 2, 1.0f), color,
                new Vector2(textureMapTiling, 0.0f));
            rectMesh.AddVertex(2, new Vector3(width / 2, -height / 2, 1.0f), color,
                new Vector2(textureMapTiling, textureMapTiling));
            rectMesh.AddVertex(3, new Vector3(-width / 2, -height / 2, 1.0f), color,
                new Vector2(0.0f, textureMapTiling));

            rectMesh.AddTriangle(0, 0, 1, 2);
            rectMesh.AddTriangle(1, 0, 2, 3);

            return rectMesh;
        }
예제 #18
0
 /// <summary>
 /// Builds a rectangular mesh, centered around the origin.
 /// </summary>
 public static Mesh Rectangle(Renderer renderer, Color color, float width, float height)
 {
     return Mesh.Rectangle(renderer, color, width, height, 1.0f);
 }
예제 #19
0
        /// <summary>
        /// Builds a circular mesh centered around the origin.
        /// </summary>
        public static Mesh Circle(Renderer renderer, Color centerColor, Color rimColor, float radius,
            int numSubdivisions)
        {
            Mesh circleMesh = new Mesh(renderer, numSubdivisions, numSubdivisions - 2);

            float angleStep = (2 * (float)Math.PI) / numSubdivisions;
            for (int i = 0; i < numSubdivisions; ++i)
            {
                circleMesh.AddVertex(i, new Vector3(radius * (float)Math.Cos(angleStep * i),
                    radius * (float)Math.Sin(angleStep * i), 1.0f), rimColor, new Vector2());
            }

            for (int i = 2, count = 0; i < numSubdivisions - 1; ++i, ++count)
            {
                circleMesh.AddTriangle(count, 0, i, i - 1);
            }

            circleMesh.AddTriangle(numSubdivisions - 3, 0, numSubdivisions - 2, numSubdivisions - 1);

            return circleMesh;
        }
예제 #20
0
        /// <summary>
        /// Creates the mesh and allocates all the memory it will need.
        /// </summary>
        public Mesh(Renderer renderer, int numVertices, int numTriangles)
        {
            if (renderer == null)
                throw new ArgumentNullException("renderer", "Can't create mesh with an invalid renderer.");

            if (numVertices <= 0)
                throw new ArgumentOutOfRangeException("numVertices", numVertices, "Can't create mesh with zero or fewer " +
                    "vertices.");

            if (numTriangles <= 0)
                throw new ArgumentOutOfRangeException("numTriangles", numTriangles, "Can't create mesh with zero or fewer " +
                    "triangles.");

            this.renderer = renderer;
            this.numVertices = numVertices;
            this.numTriangles = numTriangles;

            vertices = new CustomVertex.PositionColoredTextured[numVertices];
            if (vertices == null)
                throw new OutOfMemoryException("Unable to allocate vertex array for Mesh.");

            triangles = new Triangle[numTriangles];
            if (triangles == null)
                throw new OutOfMemoryException("Unable to allocate triangle array for Mesh.");

            vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColoredTextured), numVertices,
                renderer.Device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColoredTextured.Format,
                Pool.Default);

            if (vertexBuffer == null)
                throw new DirectXException("Unable to create vertex buffer for Mesh.");

            indexBuffer = new IndexBuffer(typeof(short), 3 * numTriangles, renderer.Device,
                Usage.WriteOnly, Pool.Default);

            if (indexBuffer == null)
                throw new Direct3DXException("Unable to create index buffer for Mesh.");

            renderer.AddGraphicsObject(this);
        }
예제 #21
0
        /// <summary>
        /// Initializes a new instance of SceneGraph.
        /// </summary>
        public SceneGraph(Renderer renderer)
        {
            if (renderer == null)
            {
                Log.Write("Can't create a SceneGraph with a null Renderer reference.");
                throw new ArgumentNullException("renderer",
                    "Can't create a SceneGraph with a null Renderer reference.");
            }

            this.renderer = renderer;

            root = new SceneGraphNode(this.renderer, this);
        }
예제 #22
0
        /// <summary>
        /// Creates a material from a file. If the material has already been created, the cached version
        /// is returned.
        /// </summary>
        public static Material CreateMaterialFromFile(Renderer renderer, string filename)
        {
            // Search cache first
            foreach (string cachedFilename in materialCache.Keys)
            {
                if (StringHelper.CaseInsensitiveCompare(cachedFilename, filename))
                    return materialCache[cachedFilename] as Material;
            }

            Material newMat = Material.FromFile(renderer, filename);

            materialCache.Add(filename, newMat);

            return newMat;
        }
예제 #23
0
        /// <summary>
        /// Create a texture from a file. If the texture has already been created, the cached
        /// version is returned.
        /// </summary>
        public static Texture CreateTextureFromFile(Renderer renderer, string filename)
        {
            // Search cache first
            foreach (string cachedFilename in textureCache.Keys)
            {
                if (StringHelper.CaseInsensitiveCompare(cachedFilename, filename))
                    return textureCache[cachedFilename] as Texture;
            }

            Texture newTex = new Texture(renderer, filename);

            textureCache.Add(filename, newTex);

            return newTex;
        }
예제 #24
0
        /// <summary>
        /// Creates the texture as a render target.
        /// </summary>
        public Texture(Renderer renderer, int width, int height, bool alpha)
        {
            if (renderer == null)
                throw new ArgumentNullException("renderer",
                    "Unable to create texture without a valid renderer reference.");

            this.renderer = renderer;

            try
            {
                d3dTexture = new Microsoft.DirectX.Direct3D.Texture(renderer.Device,
                    width, height, 1, Usage.RenderTarget, alpha ? Format.A8R8G8B8 : Format.X8R8G8B8,
                    Pool.Default);

                d3dSurface = d3dTexture.GetSurfaceLevel(0);

                this.size = new Size(width, height);
                this.hasAlpha = alpha;

                loaded = true;

                renderer.AddGraphicsObject(this);
            }
            catch (Exception ex)
            {
                loaded = false;
                Log.Write("Failed to create texture as render target, will use empty texture!" +
                    " Error: " + ex.ToString());
            }
        }
예제 #25
0
 /// <summary>
 /// Initializes a new instance of GeometryNode.
 /// </summary>
 public GeometryNode(Renderer renderer, SceneGraph sceneGraph)
     : base(renderer, sceneGraph)
 {
 }