Exemple #1
0
 /// <summary>
 /// Creates a new Material
 /// </summary>
 public Material()
 {
     this.info = new BatchInfo();
 }
Exemple #2
0
 /// <summary>
 /// Creates a new Material based on the specified <see cref="DrawTechnique"/>.
 /// </summary>
 /// <param name="technique">The <see cref="Duality.Resources.DrawTechnique"/> to use.</param>
 public Material(ContentRef <DrawTechnique> technique)
 {
     this.info = new BatchInfo(technique);
 }
Exemple #3
0
 /// <summary>
 /// Creates a new single-texture Material.
 /// </summary>
 /// <param name="technique">The <see cref="Duality.Resources.DrawTechnique"/> to use.</param>
 /// <param name="mainTex">The main <see cref="Duality.Resources.Texture"/> to use.</param>
 public Material(ContentRef <DrawTechnique> technique, ContentRef <Texture> mainTex)
 {
     this.info = new BatchInfo(technique, mainTex);
 }
Exemple #4
0
 /// <summary>
 /// Creates a new Material based on the specified BatchInfo
 /// </summary>
 /// <param name="info"></param>
 public Material(BatchInfo info)
 {
     this.info = new BatchInfo(info);
 }
Exemple #5
0
 /// <summary>
 /// Creates a new color-only Material.
 /// </summary>
 /// <param name="technique">The <see cref="Duality.Resources.DrawTechnique"/> to use.</param>
 /// <param name="mainColor">The <see cref="MainColor"/> to use.</param>
 public Material(ContentRef <DrawTechnique> technique, ColorRgba mainColor)
 {
     this.info = new BatchInfo(technique, mainColor);
 }
Exemple #6
0
 /// <summary>
 /// Creates a new complex Material.
 /// </summary>
 /// <param name="technique">The <see cref="Duality.Resources.DrawTechnique"/> to use.</param>
 /// <param name="mainColor">The <see cref="MainColor"/> to use.</param>
 /// <param name="textures">A set of <see cref="Duality.Resources.Texture">Textures</see> to use.</param>
 /// <param name="uniforms">A set of <see cref="Duality.Resources.ShaderVarInfo">uniform values</see> to use.</param>
 public Material(ContentRef <DrawTechnique> technique, ColorRgba mainColor, Dictionary <string, ContentRef <Texture> > textures = null, Dictionary <string, float[]> uniforms = null)
 {
     this.info = new BatchInfo(technique, mainColor, textures, uniforms);
 }
Exemple #7
0
 /// <summary>
 /// Prepares rendering using this DrawTechnique.
 /// </summary>
 /// <param name="device"></param>
 /// <param name="material"></param>
 protected virtual void PrepareRendering(IDrawDevice device, BatchInfo material)
 {
 }
Exemple #8
0
        /// <summary>
        /// Sets up the appropriate OpenGL rendering state for this DrawTechnique.
        /// </summary>
        /// <param name="lastTechnique">The last DrawTechnique that has been set up. This parameter is optional, but
        /// specifying it will increase performance by reducing redundant state changes.</param>
        /// <param name="textures">A set of <see cref="Duality.Resources.Texture">Textures</see> to use.</param>
        /// <param name="uniforms">A set of <see cref="Duality.Resources.ShaderVarInfo">uniform values</see> to apply.</param>
        public void SetupForRendering(IDrawDevice device, BatchInfo material, DrawTechnique lastTechnique)
        {
            // Prepare Rendering
            if (this.NeedsPreparation)
            {
                // Clone the material, if not done yet due to vertex preprocessing
                if (!this.NeedsPreprocess)
                {
                    material = new BatchInfo(material);
                }
                this.PrepareRendering(device, material);
            }

            // Setup BlendType
            if (lastTechnique == null || this.blendType != lastTechnique.blendType)
            {
                this.SetupBlendType(this.blendType, device.DepthWrite);
            }

            // Bind Shader
            ContentRef <ShaderProgram> selShader = this.SelectShader();

            if (lastTechnique == null || selShader.Res != lastTechnique.shader.Res)
            {
                ShaderProgram.Bind(selShader);
            }

            // Setup shader data
            if (selShader.IsAvailable)
            {
                ShaderVarInfo[] varInfo = selShader.Res.VarInfo;

                // Setup sampler bindings automatically
                int curSamplerIndex = 0;
                if (material.Textures != null)
                {
                    for (int i = 0; i < varInfo.Length; i++)
                    {
                        if (varInfo[i].glVarLoc == -1)
                        {
                            continue;
                        }
                        if (varInfo[i].type != ShaderVarType.Sampler2D)
                        {
                            continue;
                        }

                        // Bind Texture
                        ContentRef <Texture> texRef = material.GetTexture(varInfo[i].name);
                        Texture.Bind(texRef, curSamplerIndex);
                        GL.Uniform1(varInfo[i].glVarLoc, curSamplerIndex);

                        curSamplerIndex++;
                    }
                }
                Texture.ResetBinding(curSamplerIndex);

                // Transfer uniform data from material to actual shader
                if (material.Uniforms != null)
                {
                    for (int i = 0; i < varInfo.Length; i++)
                    {
                        if (varInfo[i].glVarLoc == -1)
                        {
                            continue;
                        }
                        float[] data = material.GetUniform(varInfo[i].name);
                        if (data == null)
                        {
                            continue;
                        }
                        varInfo[i].SetupUniform(data);
                    }
                }
            }
            // Setup fixed function data
            else
            {
                // Fixed function texture binding
                if (material.Textures != null)
                {
                    int samplerIndex = 0;
                    foreach (var pair in material.Textures)
                    {
                        Texture.Bind(pair.Value, samplerIndex);
                        samplerIndex++;
                    }
                    Texture.ResetBinding(samplerIndex);
                }
                else
                {
                    Texture.ResetBinding();
                }
            }
        }
Exemple #9
0
 /// <summary>
 /// Performs a preprocessing operation for incoming vertices. Does nothing by default but may be overloaded, if needed.
 /// </summary>
 /// <typeparam name="T">The incoming vertex type</typeparam>
 /// <param name="device"></param>
 /// <param name="material"><see cref="Duality.Resources.Material"/> information for the current batch.</param>
 /// <param name="vertexMode">The mode of incoming vertex data.</param>
 /// <param name="vertexBuffer">A buffer storing incoming vertex data.</param>
 /// <param name="vertexCount">The number of vertices to preprocess, beginning at the start of the specified buffer.</param>
 public virtual void PreprocessBatch <T>(IDrawDevice device, BatchInfo material, ref VertexMode vertexMode, ref T[] vertexBuffer, ref int vertexCount)
 {
 }