<commit_before>using System;
using System.Drawing;
using System.Reactive.Disposables;
using OpenTK.Graphics.OpenGL;

namespace SolidworksAddinFramework.OpenGl
{
    public static class ModernOpenGl
    {
        private static float[] GetMaterial(MaterialFace materialFace, MaterialParameter materialParameter)
        {
            var @params = new float[4];
            GL.GetMaterial(materialFace, materialParameter, @params);
            return @params;
        }


        public static IDisposable SetColor(Color value, ShadingModel shadingModel)
        {

            GL.ShadeModel(shadingModel);
            if (shadingModel == ShadingModel.Smooth)
            {
                GL.Enable(EnableCap.ColorMaterial);
                GL.ColorMaterial(MaterialFace.FrontAndBack, ColorMaterialParameter.AmbientAndDiffuse);

                GL.Enable(EnableCap.ColorMaterial);
                GL.ColorMaterial(MaterialFace.FrontAndBack, ColorMaterialParameter.AmbientAndDiffuse);
                /*
                GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Specular, SpecularColor);
                GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Emission, EmissionColor);
                GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Shininess, Shinyness);
                */
                GL.Enable(EnableCap.Lighting);

            }
            else
            {
                GL.Disable(EnableCap.ColorMaterial);
                GL.Disable(EnableCap.Lighting);
            }

            if (value.A<255)
            {
                GL.DepthMask(false);
                GL.Enable(EnableCap.Blend);
                GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);


            }

            var color = new[] { value.R / 255f, value.G / 255f, value.B / 255f, value.A / 255f };
            GL.Color4(color);

            return Disposable.Create(() =>
            {
                if(value.A<255)
                {
                    GL.Disable(EnableCap.Blend);
                    GL.DepthMask(true);
                }
            });
        }

        public static IDisposable SetLineWidth(float value)
        {
            var currentLineWidth = GL.GetFloat(GetPName.LineWidth);
            GL.LineWidth(value);
            return Disposable.Create(() => GL.LineWidth(currentLineWidth));
        }

        public static IDisposable Begin(PrimitiveType mode)
        {
            GL.Begin(mode);
            return Disposable.Create(GL.End);
        }
    }
}<commit_msg>Add nested support to GLBegin<commit_after>using System;
using System.Diagnostics;
using System.Drawing;
using System.Reactive.Disposables;
using OpenTK.Graphics.OpenGL;

namespace SolidworksAddinFramework.OpenGl
{
    public static class ModernOpenGl
    {
        private static float[] GetMaterial(MaterialFace materialFace, MaterialParameter materialParameter)
        {
            var @params = new float[4];
            GL.GetMaterial(materialFace, materialParameter, @params);
            return @params;
        }


        public static IDisposable SetColor(Color value, ShadingModel shadingModel)
        {

            GL.ShadeModel(shadingModel);
            if (shadingModel == ShadingModel.Smooth)
            {
                GL.Enable(EnableCap.ColorMaterial);
                GL.ColorMaterial(MaterialFace.FrontAndBack, ColorMaterialParameter.AmbientAndDiffuse);

                GL.Enable(EnableCap.ColorMaterial);
                GL.ColorMaterial(MaterialFace.FrontAndBack, ColorMaterialParameter.AmbientAndDiffuse);
                /*
                GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Specular, SpecularColor);
                GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Emission, EmissionColor);
                GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Shininess, Shinyness);
                */
                GL.Enable(EnableCap.Lighting);

            }
            else
            {
                GL.Disable(EnableCap.ColorMaterial);
                GL.Disable(EnableCap.Lighting);
            }

            if (value.A<255)
            {
                GL.DepthMask(false);
                GL.Enable(EnableCap.Blend);
                GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);


            }

            var color = new[] { value.R / 255f, value.G / 255f, value.B / 255f, value.A / 255f };
            GL.Color4(color);

            return Disposable.Create(() =>
            {
                if(value.A<255)
                {
                    GL.Disable(EnableCap.Blend);
                    GL.DepthMask(true);
                }
            });
        }

        public static IDisposable SetLineWidth(float value)
        {
            var currentLineWidth = GL.GetFloat(GetPName.LineWidth);
            GL.LineWidth(value);
            return Disposable.Create(() => GL.LineWidth(currentLineWidth));
        }


        public static int BeginCount = 0;
        public static PrimitiveType? BeginType = null; 

        public static IDisposable Begin(PrimitiveType mode)
        {
            BeginCount++;
            if(BeginCount==1)
            {
                GL.Begin(mode);
                BeginType = mode;
            }else 
                Debug.Assert(BeginType != null && mode == BeginType.Value);

            return Disposable.Create(() =>
            {
                BeginCount --;
                if (BeginCount == 0)
                {
                    GL.End();
                    BeginType = null;
                }
            });
        }
    }
}
{'fraction_non_alphanumeric': 0.08943376256835421, 'fraction_numerical': 0.007585112012700653, 'mean_word_length': 1.6470588235294117, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3861', 'n_tokens_mistral': 1540, 'n_tokens_neox': 1405, 'n_words': 238}