コード例 #1
0
        public FlatShader(OpenGL gl)
            : base(gl)
        {
            _program = new ShaderProgram();
            var vertex   = new VertexShader();
            var fragment = new FragmentShader();

            vertex.CreateInContext(gl);
            vertex.SetSource(File.ReadAllText("Shaders/Flat.vert.glsl"));
            vertex.Compile();

            fragment.CreateInContext(gl);
            fragment.SetSource(File.ReadAllText("Shaders/Flat.frag.glsl"));
            fragment.Compile();

            _program.CreateInContext(gl);
            _program.AttachShader(vertex);
            _program.AttachShader(fragment);
            _program.Link();

            Debug.WriteLine(_program.InfoLog);
            foreach (var attachedShader in _program.AttachedShaders)
            {
                Debug.WriteLine(attachedShader.InfoLog);
            }
        }
コード例 #2
0
ファイル: OctreeShader.cs プロジェクト: xiaoxiongnpu/OpenCAD
        public OctreeShader(OpenGL gl) : base(gl)
        {
            _program = new ShaderProgram();
            var vertex   = new VertexShader();
            var fragment = new FragmentShader();
            var geo      = new GeometryShader();

            vertex.CreateInContext(gl);
            vertex.SetSource(File.ReadAllText("Shaders/Octree.vert.glsl"));
            vertex.Compile();

            fragment.CreateInContext(gl);
            fragment.SetSource(File.ReadAllText("Shaders/Octree.frag.glsl"));
            fragment.Compile();

            geo.CreateInContext(gl);
            geo.SetSource(File.ReadAllText("Shaders/Octree.geo.glsl"));
            geo.Compile();

            _program.CreateInContext(gl);
            _program.AttachShader(vertex);
            _program.AttachShader(fragment);
            _program.AttachShader(geo);
            _program.Link();

            Debug.WriteLine(_program.InfoLog);
            foreach (var attachedShader in _program.AttachedShaders)
            {
                Debug.WriteLine(attachedShader.InfoLog);
            }

            _modelLocation      = gl.GetUniformLocation(_program.ProgramObject, "Model");
            _viewLocation       = gl.GetUniformLocation(_program.ProgramObject, "View");
            _projectionLocation = gl.GetUniformLocation(_program.ProgramObject, "Projection");
        }
コード例 #3
0
        private void OpenGLControl_OpenGLInitialized(object sender, OpenGLEventArgs args)
        {
            OpenGL gl = args.OpenGL;

            Foo();

            gl.Enable(OpenGL.GL_DEPTH_TEST);

            float[] global_ambient = new float[] { 0.5f, 0.5f, 0.5f, 1.0f };
            float[] light0pos      = new float[] { 0.0f, 5.0f, 10.0f, 1.0f };
            float[] light0ambient  = new float[] { 0.2f, 0.2f, 0.2f, 1.0f };
            float[] light0diffuse  = new float[] { 0.3f, 0.3f, 0.3f, 1.0f };
            float[] light0specular = new float[] { 0.8f, 0.8f, 0.8f, 1.0f };

            float[] lmodel_ambient = new float[] { 0.2f, 0.2f, 0.2f, 1.0f };
            gl.LightModel(OpenGL.GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);

            gl.LightModel(OpenGL.GL_LIGHT_MODEL_AMBIENT, global_ambient);
            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_POSITION, light0pos);
            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_AMBIENT, light0ambient);
            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_DIFFUSE, light0diffuse);
            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_SPECULAR, light0specular);
            gl.Enable(OpenGL.GL_LIGHTING);
            gl.Enable(OpenGL.GL_LIGHT0);

            gl.ShadeModel(OpenGL.GL_SMOOTH);

            //  Create a vertex shader.
            VertexShader vertexShader = new VertexShader();

            vertexShader.CreateInContext(gl);
            vertexShader.SetSource(
                "void main()" + Environment.NewLine +
                "{" + Environment.NewLine +
                "gl_Position = ftransform();" + Environment.NewLine +
                "}" + Environment.NewLine);

            //  Create a fragment shader.
            FragmentShader fragmentShader = new FragmentShader();

            fragmentShader.CreateInContext(gl);
            fragmentShader.SetSource(
                "void main()" + Environment.NewLine +
                "{" + Environment.NewLine +
                "gl_FragColor = vec4(0.4,0.4,0.8,1.0);" + Environment.NewLine +
                "}" + Environment.NewLine);

            //  Compile them both.
            vertexShader.Compile();
            fragmentShader.Compile();

            //  Build a program.
            program.CreateInContext(gl);

            //  Attach the shaders.
            program.AttachShader(vertexShader);
            program.AttachShader(fragmentShader);
            program.Link();
        }
コード例 #4
0
ファイル: ShaderStore.cs プロジェクト: DarkDestry/Cauldron
        public static void CompileShadersHotReload(OpenGL gl)
        {
            foreach (var name in shaderNames)
            {
                bool vertChanged = ResourceLoader.LoadTextFileIfChanged($"Shaders\\{name}.vert", out var vert);
                bool fragChanged = ResourceLoader.LoadTextFileIfChanged($"Shaders\\{name}.frag", out var frag);

                if (vertChanged || fragChanged)
                {
                    string shaderError = "";

                    ShaderProgram program = new ShaderProgram();

                    //  Create a vertex shader.
                    VertexShader vertexShader = new VertexShader();
                    vertexShader.CreateInContext(gl);
                    vertexShader.SetSource(vert);

                    //  Create a fragment shader.
                    FragmentShader fragmentShader = new FragmentShader();
                    fragmentShader.CreateInContext(gl);
                    fragmentShader.SetSource(frag);

                    //  Compile them both.
                    vertexShader.Compile();
                    fragmentShader.Compile();

                    if ((bool)!vertexShader.CompileStatus)
                    {
                        shaderError += $"{name}.vert: " + vertexShader.InfoLog + "\n\n";
                    }
                    if ((bool)!fragmentShader.CompileStatus)
                    {
                        shaderError += $"{name}.frag: " + fragmentShader.InfoLog;
                    }

                    //  Build a program.
                    program.CreateInContext(gl);

                    //  Attach the shaders.
                    program.AttachShader(vertexShader);
                    program.AttachShader(fragmentShader);
                    program.Link();

                    programs[name]     = program;
                    shaderErrors[name] = shaderError;
                }
            }
        }
コード例 #5
0
ファイル: ShaderStore.cs プロジェクト: DarkDestry/Cauldron
        public static void CompileShaders(OpenGL gl)
        {
            foreach (var name in shaderNames)
            {
                ShaderProgram program     = new ShaderProgram();
                string        shaderError = "";

                //  Create a vertex shader.
                VertexShader vertexShader = new VertexShader();
                vertexShader.CreateInContext(gl);
                vertexShader.SetSource(ResourceLoader.LoadEmbeddedTextFile($"Shaders\\{name}.vert"));

                //  Create a fragment shader.
                FragmentShader fragmentShader = new FragmentShader();
                fragmentShader.CreateInContext(gl);
                fragmentShader.SetSource(ResourceLoader.LoadEmbeddedTextFile($"Shaders\\{name}.frag"));

                //  Compile them both.
                vertexShader.Compile();
                fragmentShader.Compile();

                if ((bool)!vertexShader.CompileStatus)
                {
                    shaderError += "color.vert: " + vertexShader.InfoLog + "\n\n";
                }
                if ((bool)!fragmentShader.CompileStatus)
                {
                    shaderError += "color.frag: " + fragmentShader.InfoLog;
                }

                //  Build a program.
                program.CreateInContext(gl);

                //  Attach the shaders.
                program.AttachShader(vertexShader);
                program.AttachShader(fragmentShader);
                program.Link();

                programs.Add(name, program);
                shaderErrors.Add(name, shaderError);
            }
        }
コード例 #6
0
        public static void CreateBasicShader(OpenGLWindow _window)
        {
            //ThrowIfNull(_window);

            OpenGL context = _window.Context;

            //  Create a vertex shader.
            VertexShader vertexShader = new VertexShader();

            vertexShader.CreateInContext(context);
            vertexShader.SetSource(
                "void main()" + Environment.NewLine +
                "{" + Environment.NewLine +
                "gl_Position = ftransform();" + Environment.NewLine +
                "}" + Environment.NewLine);

            //  Create a fragment shader.
            FragmentShader fragmentShader = new FragmentShader();

            fragmentShader.CreateInContext(context);
            fragmentShader.SetSource(
                "void main()" + Environment.NewLine +
                "{" + Environment.NewLine +
                "gl_FragColor = vec4(0.4,0.4,0.8,1.0);" + Environment.NewLine +
                "}" + Environment.NewLine);

            //  Compile them both.
            vertexShader.Compile();
            fragmentShader.Compile();

            //  Build a program.
            _window.m_shaderProgram.CreateInContext(context);

            //  Attach the shaders.
            _window.m_shaderProgram.AttachShader(vertexShader);
            _window.m_shaderProgram.AttachShader(fragmentShader);
            _window.m_shaderProgram.Link();
        }
コード例 #7
0
ファイル: GlContext.cs プロジェクト: Emartronics/pfsled
        public void swapShader(OpenGL gl, string Vertex_shader_textbox, string Fragment_shader_textbox)
        {
            //  Create a vertex shader.
            vertexShader = new VertexShader();
            vertexShader.CreateInContext(gl);
            vertexShader.SetSource(Vertex_shader_textbox);

            //  Create a fragment shader.
            fragmentShader = new FragmentShader();
            fragmentShader.CreateInContext(gl);
            fragmentShader.SetSource(Fragment_shader_textbox);

            //  Compile them both.
            vertexShader.Compile();
            fragmentShader.Compile();

            //  Build a program.
            m_shaderProgram.CreateInContext(gl);

            //  Attach the shaders.
            m_shaderProgram.AttachShader(vertexShader);
            m_shaderProgram.AttachShader(fragmentShader);
            m_shaderProgram.Link();
        }
コード例 #8
0
ファイル: GlContext.cs プロジェクト: Emartronics/pfsled
        public void OpenGLInit(object sender, OpenGLEventArgs args, string Vertex_shader_textbox, string Fragment_shader_textbox)
        {
            //CREATE THE VAO
            args.OpenGL.GenVertexArrays(1, vao);
            args.OpenGL.BindVertexArray(vao[0]);
            //CREATE THE VBO AND POPULATE IT WITH THE QUAD VERTICIES
            args.OpenGL.GenBuffers(1, vbo);
            args.OpenGL.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vbo[0]);
            args.OpenGL.BufferData(OpenGL.GL_ARRAY_BUFFER, g_vertex_buffer_data.Length * sizeof(float), GCHandle.Alloc(g_vertex_buffer_data, GCHandleType.Pinned).AddrOfPinnedObject(), OpenGL.GL_STATIC_DRAW);
            args.OpenGL.VertexAttribPointer(0, 3, OpenGL.GL_FLOAT, false, 0, new IntPtr(0));



            //Tell opengl what attribute arrays we need
            args.OpenGL.EnableVertexAttribArray(0);
            args.OpenGL.EnableVertexAttribArray(1);
            args.OpenGL.EnableVertexAttribArray(2);
            args.OpenGL.EnableVertexAttribArray(3);

            //This is the position attribute pointer  (layout(location = 0) in vec2 vertexPosition;)
            args.OpenGL.VertexAttribPointer(1, 2, OpenGL.GL_FLOAT, false, 0, new IntPtr(0));
            //This is the color attribute pointer layout(location = 1) in vec4 vertexColor;
            args.OpenGL.VertexAttribPointer(2, 4, OpenGL.GL_UNSIGNED_BYTE, false, 0, new IntPtr(2));
            //This is the UV attribute pointer layout(location = 2) in vec2 vertexUV;
            args.OpenGL.VertexAttribPointer(3, 2, OpenGL.GL_FLOAT, false, 0, new IntPtr(0));

            //UNBIND WHEN FINISHED
            args.OpenGL.BindVertexArray(0);
            args.OpenGL.BindBuffer(OpenGL.GL_ARRAY_BUFFER, 0);


            //SET SOME GL CONSTANTS
            args.OpenGL.Enable(OpenGL.GL_DEPTH_TEST);
            args.OpenGL.Enable(OpenGL.GL_BLEND);
            args.OpenGL.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
            args.OpenGL.ShadeModel(OpenGL.GL_SMOOTH);



            //  Create a vertex shader.
            vertexShader = new VertexShader();
            vertexShader.CreateInContext(args.OpenGL);
            vertexShader.SetSource(Vertex_shader_textbox);

            //  Create a fragment shader.
            fragmentShader = new FragmentShader();
            fragmentShader.CreateInContext(args.OpenGL);
            fragmentShader.SetSource(Fragment_shader_textbox);

            //  Compile them both.
            vertexShader.Compile();
            fragmentShader.Compile();



            //  Build a program.
            m_shaderProgram.CreateInContext(args.OpenGL);

            //  Attach the shaders.
            m_shaderProgram.AttachShader(vertexShader);
            m_shaderProgram.AttachShader(fragmentShader);
            m_shaderProgram.Link();


            //SET UP TEXTURE 1

            texture.Create(args.OpenGL, Environment.CurrentDirectory + @"\1.png");
            texture.Bind(args.OpenGL);
        }