// applies texture stage setup to gl.  texturestagenum will be 0 except for blend
 // where it will be either 0 or 1
 // texture coordinates are handled independently (?)
 // either we're using multipass or we're using multitexture.  multipass still uses 2 multitexture units, but not 4, 6, 8, ...
 public void Apply(int texturestagenum, bool UsingMultipass, int mapwidth, int mapheight)
 {
     //Console.WriteLine( "MapTextureStageView for " + maptexturestagemodel + " " + ((GlTexture)splattexture).GlReference +  " Apply" );
     GlTextureCombine texturecombine;
     GraphicsHelperGl g = new GraphicsHelperGl();
     switch (maptexturestagemodel.Operation)
     {
         case MapTextureStageModel.OperationType.NoTexture:
             g.DisableTexture2d();
             g.EnableModulate();
             break;
         case MapTextureStageModel.OperationType.Add:
             splattexture.Apply();
             SetTextureScale(1 / (double)maptexturestagemodel.Tilesize);
             texturecombine = new GlTextureCombine();
             texturecombine.Operation = GlTextureCombine.OperationType.Add;
             texturecombine.Args[0].SetRgbaSource(GlCombineArg.Source.Previous);
             texturecombine.Args[1].SetRgbaSource(GlCombineArg.Source.Texture);
             texturecombine.Apply();
             break;
         case MapTextureStageModel.OperationType.Blend:
             if (UsingMultipass)
             {
                 if (texturestagenum == 0)
                 {
                     blendtexture.Apply();
                     SetTextureScale(1 / (double)mapwidth);
                     texturecombine = new GlTextureCombine();
                     texturecombine.Operation = GlTextureCombine.OperationType.Replace;
                     texturecombine.Args[0].SetAlphaSource(GlCombineArg.Source.Texture, GlCombineArg.Operand.Alpha);
                     texturecombine.Apply();
                 }
                 else
                 {
                     splattexture.Apply();
                     SetTextureScale( 1 / (double)maptexturestagemodel.Tilesize );
                     new GraphicsHelperGl().EnableModulate();
                 }
             }
             else
             {
                 if (texturestagenum == 0)
                 {
                     blendtexture.Apply();
                     SetTextureScale(1 / (double)mapwidth);
                     texturecombine = new GlTextureCombine();
                     texturecombine.Operation = GlTextureCombine.OperationType.Replace;
                     texturecombine.Args[0].SetRgbSource(GlCombineArg.Source.Previous, GlCombineArg.Operand.Rgb);
                     texturecombine.Args[0].SetAlphaSource(GlCombineArg.Source.Texture, GlCombineArg.Operand.Alpha);
                     texturecombine.Apply();
                 }
                 else
                 {
                     splattexture.Apply();
                     SetTextureScale( 1 / (double)maptexturestagemodel.Tilesize );
                     texturecombine = new GlTextureCombine();
                     texturecombine.Operation = GlTextureCombine.OperationType.Interpolate;
                     texturecombine.Args[0].SetRgbaSource(GlCombineArg.Source.Previous);
                     texturecombine.Args[1].SetRgbaSource(GlCombineArg.Source.Texture);
                     texturecombine.Args[2].SetRgbSource(GlCombineArg.Source.Previous, GlCombineArg.Operand.Alpha);
                     texturecombine.Args[2].SetAlphaSource(GlCombineArg.Source.Previous, GlCombineArg.Operand.Alpha);
                     texturecombine.Apply();
                 }
             }
             break;
         case MapTextureStageModel.OperationType.Multiply:
             splattexture.Apply();
             SetTextureScale( 1 / (double)maptexturestagemodel.Tilesize );
             texturecombine = new GlTextureCombine();
             texturecombine.Operation = GlTextureCombine.OperationType.Modulate;
             texturecombine.Args[0].SetRgbaSource(GlCombineArg.Source.Previous);
             texturecombine.Args[1].SetRgbaSource(GlCombineArg.Source.Texture);
             texturecombine.Apply();
             break;
         case MapTextureStageModel.OperationType.Subtract:
             splattexture.Apply();
             SetTextureScale( 1 / (double)maptexturestagemodel.Tilesize );
             texturecombine = new GlTextureCombine();
             texturecombine.Operation = GlTextureCombine.OperationType.Subtract;
             texturecombine.Args[0].SetRgbaSource(GlCombineArg.Source.Previous);
             texturecombine.Args[1].SetRgbaSource(GlCombineArg.Source.Texture);
             texturecombine.Apply();
             break;
         case MapTextureStageModel.OperationType.Replace:
             splattexture.Apply();
             SetTextureScale( 1 / (double)maptexturestagemodel.Tilesize );
             texturecombine = new GlTextureCombine();
             texturecombine.Operation = GlTextureCombine.OperationType.Modulate;
             texturecombine.Args[0].SetRgbaSource(GlCombineArg.Source.Texture);
             texturecombine.Args[1].SetRgbaSource(GlCombineArg.Source.Fragment);
             texturecombine.Apply();
             break;
     }
 }
        // applies texture stage setup to gl.  texturestagenum will be 0 except for blend
        // where it will be either 0 or 1
        // texture coordinates are handled independently (?)
        // either we're using multipass or we're using multitexture.  multipass still uses 2 multitexture units, but not 4, 6, 8, ...
        public void Apply(int texturestagenum, bool UsingMultipass, int mapwidth, int mapheight)
        {
            //Console.WriteLine( "MapTextureStageView for " + maptexturestagemodel + " " + ((GlTexture)splattexture).GlReference +  " Apply" );
            GlTextureCombine texturecombine;
            GraphicsHelperGl g = new GraphicsHelperGl();

            switch (maptexturestagemodel.Operation)
            {
            case MapTextureStageModel.OperationType.NoTexture:
                g.DisableTexture2d();
                g.EnableModulate();
                break;

            case MapTextureStageModel.OperationType.Add:
                splattexture.Apply();
                SetTextureScale(1 / (double)maptexturestagemodel.Tilesize);
                texturecombine           = new GlTextureCombine();
                texturecombine.Operation = GlTextureCombine.OperationType.Add;
                texturecombine.Args[0].SetRgbaSource(GlCombineArg.Source.Previous);
                texturecombine.Args[1].SetRgbaSource(GlCombineArg.Source.Texture);
                texturecombine.Apply();
                break;

            case MapTextureStageModel.OperationType.Blend:
                if (UsingMultipass)
                {
                    if (texturestagenum == 0)
                    {
                        blendtexture.Apply();
                        SetTextureScale(1 / (double)mapwidth);
                        texturecombine           = new GlTextureCombine();
                        texturecombine.Operation = GlTextureCombine.OperationType.Replace;
                        texturecombine.Args[0].SetAlphaSource(GlCombineArg.Source.Texture, GlCombineArg.Operand.Alpha);
                        texturecombine.Apply();
                    }
                    else
                    {
                        splattexture.Apply();
                        SetTextureScale(1 / (double)maptexturestagemodel.Tilesize);
                        new GraphicsHelperGl().EnableModulate();
                    }
                }
                else
                {
                    if (texturestagenum == 0)
                    {
                        blendtexture.Apply();
                        SetTextureScale(1 / (double)mapwidth);
                        texturecombine           = new GlTextureCombine();
                        texturecombine.Operation = GlTextureCombine.OperationType.Replace;
                        texturecombine.Args[0].SetRgbSource(GlCombineArg.Source.Previous, GlCombineArg.Operand.Rgb);
                        texturecombine.Args[0].SetAlphaSource(GlCombineArg.Source.Texture, GlCombineArg.Operand.Alpha);
                        texturecombine.Apply();
                    }
                    else
                    {
                        splattexture.Apply();
                        SetTextureScale(1 / (double)maptexturestagemodel.Tilesize);
                        texturecombine           = new GlTextureCombine();
                        texturecombine.Operation = GlTextureCombine.OperationType.Interpolate;
                        texturecombine.Args[0].SetRgbaSource(GlCombineArg.Source.Previous);
                        texturecombine.Args[1].SetRgbaSource(GlCombineArg.Source.Texture);
                        texturecombine.Args[2].SetRgbSource(GlCombineArg.Source.Previous, GlCombineArg.Operand.Alpha);
                        texturecombine.Args[2].SetAlphaSource(GlCombineArg.Source.Previous, GlCombineArg.Operand.Alpha);
                        texturecombine.Apply();
                    }
                }
                break;

            case MapTextureStageModel.OperationType.Multiply:
                splattexture.Apply();
                SetTextureScale(1 / (double)maptexturestagemodel.Tilesize);
                texturecombine           = new GlTextureCombine();
                texturecombine.Operation = GlTextureCombine.OperationType.Modulate;
                texturecombine.Args[0].SetRgbaSource(GlCombineArg.Source.Previous);
                texturecombine.Args[1].SetRgbaSource(GlCombineArg.Source.Texture);
                texturecombine.Apply();
                break;

            case MapTextureStageModel.OperationType.Subtract:
                splattexture.Apply();
                SetTextureScale(1 / (double)maptexturestagemodel.Tilesize);
                texturecombine           = new GlTextureCombine();
                texturecombine.Operation = GlTextureCombine.OperationType.Subtract;
                texturecombine.Args[0].SetRgbaSource(GlCombineArg.Source.Previous);
                texturecombine.Args[1].SetRgbaSource(GlCombineArg.Source.Texture);
                texturecombine.Apply();
                break;

            case MapTextureStageModel.OperationType.Replace:
                splattexture.Apply();
                SetTextureScale(1 / (double)maptexturestagemodel.Tilesize);
                texturecombine           = new GlTextureCombine();
                texturecombine.Operation = GlTextureCombine.OperationType.Modulate;
                texturecombine.Args[0].SetRgbaSource(GlCombineArg.Source.Texture);
                texturecombine.Args[1].SetRgbaSource(GlCombineArg.Source.Fragment);
                texturecombine.Apply();
                break;
            }
        }