예제 #1
0
        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");
        }
예제 #2
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);
            }
        }
예제 #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
        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
        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
        // ! Инициализация шейдеров
        private void initShader()
        { //! Исходный код шейдеров
            string vsSource =
                "attribute vec2 coord;\n " +
                "void main() {\n" +
                " gl_Position = vec4(coord, 0.0, 1.0);\n" +
                "}\n";
            string fsSource =
                "uniform vec4 color;\n" +
                "void main() {\n" +
                " gl_FragColor = color;\n" +
                "}\n";
            // ! Переменные для хранения идентификаторов шейдеров
            //Shader vShader, fShader; // ! Создаем вершинный шейдер

            OpenGL gl = AnT.OpenGL;

            VertexShader vShader = new VertexShader();

            // ! Передаем исходный код
            vShader.SetSource(vsSource);
            // ! Компилируем шейдер
            vShader.Compile();
        }
예제 #8
0
        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();
        }
예제 #9
0
        /// <summary>
        /// Sets the full shader source.
        /// </summary>
        /// <param name="source">The source.</param>
        public void SetFullShaderSource(string source)
        {
            //  To compile a full shader, we must have a file like this:
            //  -- Vertex
            //  <a vertex shader>
            //  -- Fragment
            //  <a fragment shader>

            //  Prepare the source code.
            string vertexShader = string.Empty;
            string fragmentShader = string.Empty;

            //  Keep track of what we're building, 0 is nothing
            //  1 is the vertex shader, 2 is the fragment shader.
            int code = 0;

            //  Go through the source line by line.
            var lines = source.Split('\n');
            foreach (var line in lines)
            {
                //  If the line is a vertex shader, switch to vertex mode.
                if (line.StartsWith("--") && line.Contains("Vertex"))
                {
                    code = 1;
                }
                else if (line.StartsWith("--") && line.Contains("Fragment"))
                {
                    code = 2;
                }
                else
                {
                    //  Add the line to the appropriate shader.
                    switch (code)
                    {
                        case 1:
                            vertexShader += line + Environment.NewLine;
                            break;
                        case 2:
                            fragmentShader += line + Environment.NewLine;
                            break;
                    }
                }
            }

            //  If we have a vertex shader, build it.
            if (string.IsNullOrEmpty(vertexShader) == false)
            {
                VertexShader shader = new VertexShader();
                shader.CreateInContext(CurrentOpenGLContext);
                shader.SetSource(vertexShader);
                AttachShader(shader);
            }
            if (string.IsNullOrEmpty(fragmentShader) == false)
            {
                FragmentShader shader = new FragmentShader();
                shader.CreateInContext(CurrentOpenGLContext);
                shader.SetSource(fragmentShader);
                AttachShader(shader);
            }
        }
예제 #10
0
        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);
        }