Beispiel #1
0
            public static void Draw(GL_ControlModern control, Pass pass, Vector4 boxColor, Vector4 outlineColor, Vector4 pickingColor)
            {
                if (pass == Pass.OPAQUE)
                {
                    control.CurrentShader = DefaultShaderProgram;

                    GL.ActiveTexture(TextureUnit.Texture0);
                    GL.BindTexture(TextureTarget.Texture2D, Framework.TextureSheet);

                    DefaultShaderProgram.SetVector4("color", boxColor);

                    blockVao.Use(control);
                    GL.DrawArrays(PrimitiveType.Quads, 0, 24);

                    #region outlines
                    GL.LineWidth(1);
                    control.CurrentShader = SolidColorShaderProgram;
                    SolidColorShaderProgram.SetVector4("color", outlineColor);

                    linesVao.Use(control);
                    GL.DrawArrays(PrimitiveType.Lines, 0, 24);
                    GL.LineWidth(2);
                    #endregion
                }
                else
                {
                    control.CurrentShader = SolidColorShaderProgram;
                    SolidColorShaderProgram.SetVector4("color", pickingColor);

                    blockVao.Use(control);
                    GL.DrawArrays(PrimitiveType.Quads, 0, 24);
                }
            }
        public static void Initialize(GL_ControlModern control)
        {
            if (DefaultShaderProgram != null && DefaultShaderProgram.programs.ContainsKey(control))
            {
                return;
            }

            if (DefaultShaderProgram == null)
            {
                var solidColorFrag = new FragmentShader(
                    @"#version 330
                        uniform vec4 color;
                        out vec4 fragColor;

                        void main(){
                            fragColor = color;
                        }");
                var solidColorVert = new VertexShader(
                    @"#version 330
                        layout(location = 0) in vec4 position;
                        uniform mat4 mtxMdl;
                        uniform mat4 mtxCam;
                        void main(){
                            gl_Position = mtxCam*mtxMdl*position;
                        }");

                var defaultFrag = new FragmentShader(
                    @"#version 330
                uniform vec4 color;
                in vec3 viewPosition;
                in vec3 normal;
                void main(){
                    gl_FragColor = color;
                }");
                var defaultVert = new VertexShader(
                    @"#version 330
                layout(location = 0) in vec3 position;
                layout(location = 1) in vec3 vNormal;
                uniform mat4 mtxMdl;
                uniform mat4 mtxCam;
                out vec3 normal;
                out vec3 viewPosition;
                void main(){
                    normal = vNormal;
                    viewPosition = position;
                    gl_Position = mtxCam*mtxMdl*vec4(position, 1);
                }");

                DefaultShaderProgram    = new ShaderProgram(defaultFrag, defaultVert, control);
                SolidColorShaderProgram = new ShaderProgram(solidColorFrag, solidColorVert, control);

                int buffer = GL.GenBuffer();
                sphereVao = new VertexArrayObject(buffer);
                sphereVao.AddAttribute(0, 3, VertexAttribPointerType.Float, false, 24, 0);
                sphereVao.AddAttribute(1, 3, VertexAttribPointerType.Float, false, 24, 12);
                sphereVao.Initialize(control);

                List <float> list = new List <float>();
                Vertices = GetVertices(1, 32);
                for (int i = 0; i < Vertices.Length; i++)
                {
                    list.Add(Vertices[i].Position.X);
                    list.Add(Vertices[i].Position.Y);
                    list.Add(Vertices[i].Position.Z);
                    list.Add(Vertices[i].Normal.X);
                    list.Add(Vertices[i].Normal.Y);
                    list.Add(Vertices[i].Normal.Z);
                }

                float[] data = list.ToArray();
                GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * data.Length, data, BufferUsageHint.StaticDraw);
            }
            else
            {
                sphereVao.Initialize(control);
                DefaultShaderProgram.Link(control);
                SolidColorShaderProgram.Link(control);
            }
        }
        public static void Draw(GL_ControlModern control, Pass pass,
                                Vector4 sphereColor, Vector4 outlineColor, bool xray = false)
        {
            if (DefaultShaderProgram == null)
            {
                return;
            }

            Initialize(control);

            sphereVao.Enable(control);

            if (pass == Pass.PICKING)
            {
                control.CurrentShader = SolidColorShaderProgram;
                control.CurrentShader.SetVector4("color", control.NextPickingColor());

                sphereVao.Use(control);
                GL.DrawArrays(PrimitiveType.TriangleStrip, 0, Vertices.Length);
                return;
            }

            if (pass == Pass.OPAQUE && outlineColor.W != 0)
            {
                GL.Enable(EnableCap.StencilTest);
                GL.Clear(ClearBufferMask.StencilBufferBit);
                GL.ClearStencil(0);
                GL.StencilFunc(StencilFunction.Always, 0x1, 0x1);
                GL.StencilOp(StencilOp.Keep, StencilOp.Replace, StencilOp.Replace);
            }

            if (pass == Pass.OPAQUE && !xray || pass == Pass.TRANSPARENT && xray)
            {
                control.CurrentShader = DefaultShaderProgram;
                DefaultShaderProgram.SetVector4("color", sphereColor);

                sphereVao.Use(control);
                GL.DrawArrays(PrimitiveType.TriangleStrip, 0, Vertices.Length);
            }

            if (pass == Pass.OPAQUE && outlineColor.W != 0)
            {
                GL.ColorMask(false, false, false, false);
                GL.DepthMask(false);

                sphereVao.Use(control);
                GL.DrawArrays(PrimitiveType.TriangleStrip, 0, Vertices.Length);

                GL.ColorMask(true, true, true, true);
                GL.DepthMask(true);
            }

            GL.Disable(EnableCap.Blend);

            if (pass == Pass.OPAQUE && outlineColor.W != 0)
            {
                control.CurrentShader = SolidColorShaderProgram;
                control.CurrentShader.SetVector4("color", new Vector4(outlineColor.Xyz, 1));

                GL.LineWidth(3.0f);
                GL.StencilFunc(StencilFunction.Equal, 0x0, 0x1);
                GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Replace);

                //GL.DepthFunc(DepthFunction.Always);

                GL.PolygonMode(MaterialFace.Front, PolygonMode.Line);
                sphereVao.Use(control);
                GL.DrawArrays(PrimitiveType.TriangleStrip, 0, Vertices.Length);
                GL.PolygonMode(MaterialFace.Front, PolygonMode.Fill);

                //GL.DepthFunc(DepthFunction.Lequal);

                GL.Disable(EnableCap.StencilTest);
                GL.LineWidth(2);
            }
        }
Beispiel #4
0
            public static void Initialize(GL_ControlModern control)
            {
                if (!Initialized)
                {
                    var defaultFrag = new FragmentShader(
                        @"#version 330
                        uniform sampler2D tex;
                        in vec4 fragColor;
                        in vec3 fragPosition;
                        in vec2 uv;
                
                        void main(){
                            gl_FragColor = fragColor*((fragPosition.y+2)/3)*texture(tex, uv, 100);
                        }");
                    var solidColorFrag = new FragmentShader(
                        @"#version 330
                        uniform vec4 color;
                        void main(){
                            gl_FragColor = color;
                        }");
                    var defaultVert = new VertexShader(
                        @"#version 330
                        layout(location = 0) in vec4 position;
                        uniform vec4 color;
                        uniform mat4 mtxMdl;
                        uniform mat4 mtxCam;
                        out vec4 fragColor;
                        out vec3 fragPosition;
                        out vec2 uv;

                        vec2 map(vec2 value, vec2 min1, vec2 max1, vec2 min2, vec2 max2) {
                            return min2 + (value - min1) * (max2 - min2) / (max1 - min1);
                        }

                        void main(){
                            fragPosition = position.xyz;
                            uv = map(fragPosition.xz,vec2(-1.0625,-1.0625),vec2(1.0625,1.0625), vec2(0.5,0.5), vec2(0.75,1.0));
                            gl_Position = mtxCam*mtxMdl*position;
                            fragColor = color;
                        }");
                    var solidColorVert = new VertexShader(
                        @"#version 330
                        layout(location = 0) in vec4 position;
                        uniform mat4 mtxMdl;
                        uniform mat4 mtxCam;
                        void main(){
                            gl_Position = mtxCam*mtxMdl*position;
                        }");
                    DefaultShaderProgram = new ShaderProgram(defaultFrag, defaultVert, control);

                    SolidColorShaderProgram = new ShaderProgram(solidColorFrag, solidColorVert, control);

                    int buffer;

                    #region block
                    buffer = GL.GenBuffer();

                    blockVao = new VertexArrayObject(buffer);
                    blockVao.AddAttribute(0, 3, VertexAttribPointerType.Float, false, sizeof(float) * 3, 0);
                    blockVao.Initialize(control);

                    List <float> list = new List <float>();
                    Face(ref points, ref list, 0, 1, 2, 3);
                    FaceInv(ref points, ref list, 4, 5, 6, 7);
                    FaceInv(ref points, ref list, 0, 1, 4, 5);
                    Face(ref points, ref list, 2, 3, 6, 7);
                    Face(ref points, ref list, 0, 2, 4, 6);
                    FaceInv(ref points, ref list, 1, 3, 5, 7);

                    float[] data = list.ToArray();
                    GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * data.Length, data, BufferUsageHint.StaticDraw);


                    #endregion

                    #region lines
                    buffer = GL.GenBuffer();

                    linesVao = new VertexArrayObject(buffer);
                    linesVao.AddAttribute(0, 3, VertexAttribPointerType.Float, false, sizeof(float) * 3, 0);
                    linesVao.Initialize(control);

                    list = new List <float>();
                    LineFace(ref points, ref list, 0, 1, 2, 3);
                    LineFace(ref points, ref list, 4, 5, 6, 7);
                    Line(ref points, ref list, 0, 4);
                    Line(ref points, ref list, 1, 5);
                    Line(ref points, ref list, 2, 6);
                    Line(ref points, ref list, 3, 7);

                    data = list.ToArray();
                    GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * data.Length, data, BufferUsageHint.StaticDraw);

                    #endregion

                    Initialized = true;
                }
                else
                {
                    DefaultShaderProgram.Link(control);
                    SolidColorShaderProgram.Link(control);
                    blockVao.Initialize(control);
                    linesVao.Initialize(control);
                }
            }
Beispiel #5
0
        public static void Initialize(GL_ControlModern control, float scale)
        {
            if (DefaultShaderProgram != null && DefaultShaderProgram.programs.ContainsKey(control))
            {
                return;
            }

            if (DefaultShaderProgram == null)
            {
                var solidColorFrag = new FragmentShader(
                    @"#version 330
                        uniform vec4 color;
                        out vec4 fragColor;

                        void main(){
                            fragColor = color;
                        }");
                var solidColorVert = new VertexShader(
                    @"#version 330
                        layout(location = 0) in vec4 position;
                        uniform mat4 mtxMdl;
                        uniform mat4 mtxCam;
                        void main(){
                            gl_Position = mtxCam*mtxMdl*position;
                        }");

                var defaultFrag = new FragmentShader(
                    @"#version 330
                uniform vec4 highlight_color;
                uniform sampler2D texture0;

                in vec3 viewPosition;
                in vec3 normal;
                in vec2 f_texcoord0;

                out vec4 fragColor;

                void main(){
                    vec4 color = texture(texture0,f_texcoord0);

                    float hc_a   = highlight_color.w;
                    vec4 colorComb = vec4(color.rgb * (1-hc_a) + highlight_color.rgb * hc_a, color.a);

                    vec3 displayNormal = (normal.xyz * 0.5) + 0.5;
                    float halfLambert = max(displayNormal.y,0.5);

                    vec4 colorOutput = vec4(colorComb.rgb * halfLambert, colorComb.a);
                    fragColor = colorOutput;
                }");
                var defaultVert = new VertexShader(
                    @"#version 330
                layout(location = 0) in vec3 position;
                layout(location = 1) in vec3 vNormal;
                layout(location = 2) in vec2 vTexCoord;

                uniform mat4 mtxMdl;
                uniform mat4 mtxCam;
                out vec3 normal;
                out vec3 viewPosition;
                out vec2 f_texcoord0;

                void main(){
                    normal = vNormal;
                    viewPosition = position;
                    f_texcoord0 = vTexCoord;
                    gl_Position = mtxCam*mtxMdl*vec4(position, 1);
                }");

                DefaultShaderProgram    = new ShaderProgram(defaultFrag, defaultVert, control);
                SolidColorShaderProgram = new ShaderProgram(solidColorFrag, solidColorVert, control);

                int buffer = GL.GenBuffer();
                sphereVao = new VertexArrayObject(buffer);
                sphereVao.AddAttribute(0, 3, VertexAttribPointerType.Float, false, 32, 0);
                sphereVao.AddAttribute(1, 3, VertexAttribPointerType.Float, false, 32, 12);
                sphereVao.AddAttribute(2, 2, VertexAttribPointerType.Float, false, 32, 24);
                sphereVao.Initialize(control);

                List <float> list = new List <float>();
                Vertices = GetVertices(scale);
                for (int i = 0; i < Vertices.Length; i++)
                {
                    list.Add(Vertices[i].Position.X);
                    list.Add(Vertices[i].Position.Y);
                    list.Add(Vertices[i].Position.Z);
                    list.Add(Vertices[i].Normal.X);
                    list.Add(Vertices[i].Normal.Y);
                    list.Add(Vertices[i].Normal.Z);
                    list.Add(Vertices[i].TexCoord.X);
                    list.Add(Vertices[i].TexCoord.Y);
                }

                float[] data = list.ToArray();
                GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * data.Length, data, BufferUsageHint.StaticDraw);
            }
            else
            {
                sphereVao.Initialize(control);
                DefaultShaderProgram.Link(control);
                SolidColorShaderProgram.Link(control);
            }
        }
Beispiel #6
0
        public void Draw(GL_ControlModern control, Pass pass,
                         Vector4 sphereColor, Vector4 outlineColor)
        {
            Initialize(control, Scale);

            if (pass == Pass.TRANSPARENT)
            {
                GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
                GL.Enable(EnableCap.Blend);
            }

            sphereVao.Enable(control);

            if (pass == Pass.PICKING)
            {
                control.CurrentShader = SolidColorShaderProgram;
                control.CurrentShader.SetVector4("color", control.NextPickingColor());

                sphereVao.Use(control);
                GL.DrawArrays(PrimitiveType.Quads, 0, Vertices.Length);
                return;
            }
            else
            {
            }

            if (pass == Pass.OPAQUE && outlineColor.W != 0)
            {
                GL.Enable(EnableCap.StencilTest);
                GL.Clear(ClearBufferMask.StencilBufferBit);
                GL.ClearStencil(0);
                GL.StencilFunc(StencilFunction.Always, 0x1, 0x1);
                GL.StencilOp(StencilOp.Keep, StencilOp.Replace, StencilOp.Replace);
            }

            if (pass == PlanePass)
            {
                control.CurrentShader = DefaultShaderProgram;
                DefaultShaderProgram.SetVector4("highlight_color", sphereColor);
                if (Texture != null)
                {
                    GL.ActiveTexture(TextureUnit.Texture0 + 1);
                    DefaultShaderProgram.SetInt("texture0", 1);
                    if (Texture.RenderableTex == null || !Texture.RenderableTex.GLInitialized)
                    {
                        Texture.LoadOpenGLTexture();
                    }

                    GL.BindTexture(TextureTarget.Texture2D, Texture.RenderableTex.TexID);
                }

                sphereVao.Use(control);
                GL.DrawArrays(PrimitiveType.Quads, 0, Vertices.Length);
            }

            if (pass == Pass.OPAQUE && outlineColor.W != 0)
            {
                GL.ColorMask(false, false, false, false);
                GL.DepthMask(false);

                sphereVao.Use(control);
                GL.DrawArrays(PrimitiveType.Quads, 0, Vertices.Length);

                GL.ColorMask(true, true, true, true);
                GL.DepthMask(true);
            }

            GL.Disable(EnableCap.Blend);

            if (pass == Pass.OPAQUE && outlineColor.W != 0)
            {
                control.CurrentShader = SolidColorShaderProgram;
                control.CurrentShader.SetVector4("color", new Vector4(outlineColor.Xyz, 1));

                GL.LineWidth(3.0f);
                GL.StencilFunc(StencilFunction.Equal, 0x0, 0x1);
                GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Replace);

                //GL.DepthFunc(DepthFunction.Always);

                GL.PolygonMode(MaterialFace.Front, PolygonMode.Line);
                sphereVao.Use(control);
                GL.DrawArrays(PrimitiveType.Quads, 0, Vertices.Length);
                GL.PolygonMode(MaterialFace.Front, PolygonMode.Fill);

                //GL.DepthFunc(DepthFunction.Lequal);

                GL.Disable(EnableCap.StencilTest);
                GL.LineWidth(2);
            }
        }