public Effect GetShaderEffect(Q3BSPMaterial material, int lightMapIndex, Matrix worldViewProjection, GameTime gameTime)
        {
            Effect effect = material.Effect;

            if (material.NeedsTime)
            {
                effect.Parameters["gameTime"].SetValue((float)gameTime.TotalGameTime.TotalSeconds);
            }

            effect.Parameters["worldViewProj"].SetValue(worldViewProjection);
            foreach (Q3BSPMaterialStage stage in material.Stages)
            {
                if (stage.IsLightmapStage)
                {
                    stage.Texture = lightMapManager.GetLightMap(lightMapIndex);
                }

                stage.SetEffectParameters(ref effect, gameTime);
            }

            return(effect);
        }
        public bool LoadTextures(Q3BSPTextureData[] textures, GraphicsDevice graphics, ContentManager Content)
        {
            string    texName;
            int       texCount          = textures.Length;
            Texture2D nullTexture       = GenerateNullTexture(graphics, false);
            Texture2D nullShaderTexture = GenerateNullTexture(graphics, true);

            if (shaderPath.StartsWith(@"\"))
            {
                shaderPath = shaderPath.Substring(1);
            }

            diffuseTextures  = new Texture2D[texCount];
            shaderDictionary = new Dictionary <int, Q3BSPMaterial>();
            Dictionary <string, Q3BSPMaterial> completeShaderDictionary = new Dictionary <string, Q3BSPMaterial>();

            #region Load all shaders
            if (shaderPath != string.Empty && Directory.Exists(Content.RootDirectory + @"\" + shaderPath))
            {
                DirectoryInfo  di          = new DirectoryInfo(Content.RootDirectory + @"\" + shaderPath);
                ContentManager tempContent = new ContentManager(Content.ServiceProvider, Content.RootDirectory);

                foreach (FileInfo fi in di.GetFiles())
                {
                    #region If the file is not the output of the Content Pipeline, ignore it.
                    if (fi.Name.Substring(fi.Name.LastIndexOf('.')) != ".xnb")
                    {
                        continue;
                    }
                    #endregion

                    #region Load the file from the disk. If the file is not a Dictionary<string, Q3BSPMaterial>, ignore it.
                    Object loadedFile = tempContent.Load <Object>(shaderPath + fi.Name.Substring(0, fi.Name.LastIndexOf('.')));

                    if (!(loadedFile is Dictionary <string, Q3BSPMaterial>))
                    {
                        continue;
                    }

                    Dictionary <string, Q3BSPMaterial> tempDictionary = Content.Load <Dictionary <string, Q3BSPMaterial> >(shaderPath + fi.Name.Substring(0, fi.Name.LastIndexOf('.')));
                    #endregion

                    foreach (KeyValuePair <string, Q3BSPMaterial> kvp in tempDictionary)
                    {
                        if (completeShaderDictionary.ContainsKey(kvp.Key))
                        {
                            throw new ContentLoadException("Error loading " + fi.Name.Substring(0, fi.Name.LastIndexOf('.')) + ".shader: A shader with the name " + kvp.Key + " already exists.");
                        }
                        completeShaderDictionary.Add(kvp.Key, kvp.Value);
                    }
                }

                tempContent.Dispose();
            }
            #endregion

            for (int i = 0; i < texCount; i++)
            {
                texName = textures[i].Name.Trim();
                Texture2D     thisTexture = null;
                Q3BSPMaterial thisShader;

                if (noShader != texName)
                {
                    // First check to see if a shader with this name exists
                    if (completeShaderDictionary.TryGetValue(texName, out thisShader))
                    {
                        bool brokenShader = false;

                        // Load the texture for each stage, if the file exists, and the null texture if it doesn't.
                        foreach (Q3BSPMaterialStage stage in thisShader.Stages)
                        {
                            // Stage is a special case stage (such as a lightmap stage) and should not be assigned a texture
                            if (stage.IsSpecialCaseStage)
                            {
                                continue;
                            }

                            if (!File.Exists(Content.RootDirectory + '\\' + stage.TextureFilename + ".xnb"))
                            {
                                thisTexture  = nullShaderTexture;
                                brokenShader = true;
                                break;
                            }
                            stage.Texture = Content.Load <Texture2D>(stage.TextureFilename);
                        }

                        if (!brokenShader)
                        {
                            if (thisShader.IsSky == true)
                            {
                                skyMaterial = thisShader;
                            }
                            shaderDictionary.Add(i, thisShader);
                        }
                    }

                    // Next check if this is a static texture
                    else if (File.Exists(Content.RootDirectory + '\\' + texName + ".xnb"))
                    {
                        thisTexture = Content.Load <Texture2D>(texName);
                    }

                    else
                    {
                        thisTexture = nullTexture;
                    }
                }
                diffuseTextures[i] = thisTexture;
            }

            basicEffect = Content.Load <Effect>(shaderPath + "Q3BasicEffect");

            if (null == basicEffect)
            {
                throw (new Exception("Q3BasicEffect failed to load. Ensure 'Q3BasicEffect.fx' is added to the project."));
            }


            return(true);
        }