Ejemplo n.º 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");
        }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 3
0
        public void ImportFile(string srcFile, string targetName, string targetDir)
        {
            string ext = Path.GetExtension(srcFile).ToLower();

            string[] output = this.GetOutputFiles(srcFile, targetName, targetDir);
            if (ext == ".vert")
            {
                VertexShader res = new VertexShader();
                res.LoadSource(srcFile);
                res.Compile();
                res.Save(output[0]);
            }
            else if (ext == ".frag")
            {
                FragmentShader res = new FragmentShader();
                res.LoadSource(srcFile);
                res.Compile();
                res.Save(output[0]);
            }
            else
            {
                GeometryShader res = new GeometryShader();
                res.LoadSource(srcFile);
                res.Compile();
                res.Save(output[0]);
            }
        }
Ejemplo n.º 4
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();
        }
Ejemplo n.º 5
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;
                }
            }
        }
Ejemplo n.º 6
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);
            }
        }
Ejemplo n.º 7
0
        public void ImportFile(string srcFile, string targetName, string targetDir)
        {
            string ext = Path.GetExtension(srcFile);

            string[] output = this.GetOutputFiles(srcFile, targetName, targetDir);
            if (string.Equals(ext, SourceFileExtVertex, StringComparison.InvariantCultureIgnoreCase))
            {
                VertexShader res = new VertexShader();
                res.LoadSource(srcFile);
                res.Compile();
                res.Save(output[0]);
            }
            else
            {
                FragmentShader res = new FragmentShader();
                res.LoadSource(srcFile);
                res.Compile();
                res.Save(output[0]);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Initializes the renderer.
        /// </summary>
        public void Initialize()
        {
            _renderContext.Initialize();
            _colorShader = new ShaderProgram();
            var vshader = new VertexShader();

            vshader.Compile(SimpleVertexShader.SourceCode);
            var fshader = new FragmentShader();

            fshader.Compile(SimpleFragmentShader.SourceCode);
            _colorShader.Link(vshader, fshader);
            OpenGLInterops.Enable(OpenGLInterops.GL_BLEND);
            OpenGLInterops.AlphaBlend();
            SetTransform(Matrix2x3.Identity);
            OpenGLColor clearColor = OpenGLHelper.ConvertColor(_graphicsDevice.ClearColor);

            OpenGLInterops.ClearColor(clearColor);
            _windowSize = _window.Size;
            OpenGLInterops.Viewport(0, 0, (int)_windowSize.X, (int)_windowSize.Y);
            _sourceVao = new VertexArray();
            _sourceVao.Bind();
            _colorShader.Bind();
            _sourceEbo = new IndexBuffer();

            var elements = new ushort[]
            {
                0, 1, 2,
                2, 3, 0
            };

            _sourceEbo.Bind();
            _sourceEbo.SetData(elements);

            _sourceVbo = new VertexBuffer();
            _sourceVbo.Bind();

            _graphicsDevice.ClearColorChanged += GraphicsDeviceClearColorChanged;
            _window.ScreenSizeChanged         += WindowScreenSizeChanged;
        }
Ejemplo n.º 9
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();
        }
Ejemplo n.º 10
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();
        }
Ejemplo n.º 11
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);
        }