public PostProcessingExample(int width, int height)
 {
     fbo = new FBO();
     textureForRendering = Texture.Create(width, height);
     shaderPostProcess = PixelShader.Create(Encoding.UTF8.GetString(Resources.Swirl));
     shaderSource = PixelShader.Create(Encoding.UTF8.GetString(Resources.PatternCircle));
 }
Ejemplo n.º 2
0
 public bool CheckForShaderChange()
 {
     //test if we even have file -> no files nothing to be done
     if (null == shaderWatcherVertex || null == shaderWatcherFragment) return false;
     //test if any file is dirty
     if (!shaderWatcherVertex.Dirty && !shaderWatcherFragment.Dirty) return false;
     try
     {
         shader = ShaderLoader.FromFiles(shaderWatcherVertex.FullPath, shaderWatcherFragment.FullPath);
         shaderWatcherVertex.Dirty = false;
         shaderWatcherFragment.Dirty = false;
         form.Clear();
         return true;
     }
     catch (IOException e)
     {
         LastException = new ShaderException("ERROR", e.Message, string.Empty, string.Empty);
         form.Show(LastException);
     }
     catch (ShaderException e)
     {
         LastException = e;
         form.Show(e);
     }
     return false;
 }
Ejemplo n.º 3
0
 private void LoadShader()
 {
     string sVertexShader = @"
         #version 430 core
         out vec3 pos;
         void main() {
             const vec3 vertices[4] = vec3[4](vec3(-0.9, -0.8, 0.5),
                             vec3( 0.9, -0.9, 0.5),
                             vec3( 0.9,  0.8, 0.5),
                             vec3(-0.9,  0.9, 0.5));
             pos = vertices[gl_VertexID];
             gl_Position = vec4(pos, 1.0);
         }";
     string sFragmentShd = @"
     #version 430 core
     in vec3 pos;
     out vec4 color;
     void main() {
         color = vec4(pos + 1.0, 1.0);
     }";
     //read shader from file
     //string fileName = @"..\..\..\GLSL pixel shader\Hello world.glsl";
     //try
     //{
     //	using (StreamReader sr = new StreamReader(fileName))
     //	{
     //		sFragmentShd = sr.ReadToEnd();
     //		sr.Dispose();
     //	}
     //}
     //catch { };
     shader = ShaderLoader.FromStrings(sVertexShader, sFragmentShd);
 }
Ejemplo n.º 4
0
 private static VAO CreateMesh(Shader shader)
 {
     Mesh mesh = Meshes.CreateSphere(0.03f, 2);
     var vao = new VAO();
     vao.SetAttribute(shader.GetAttributeLocation("position"), mesh.positions.ToArray(), VertexAttribPointerType.Float, 3);
     vao.SetAttribute(shader.GetAttributeLocation("normal"), mesh.normals.ToArray(), VertexAttribPointerType.Float, 3);
     vao.SetID(mesh.ids.ToArray(), PrimitiveType.Triangles);
     return vao;
 }
Ejemplo n.º 5
0
 public VisualPlane()
 {
     var sVertex = Encoding.UTF8.GetString(Resourcen.plane_vert);
     var sFragment = Encoding.UTF8.GetString(Resourcen.plane_frag);
     shdPlane = ShaderLoader.FromStrings(sVertex, sFragment);
     var mesh = Meshes.CreatePlane(2, 2, 1, 1);
     plane.SetAttribute(shdPlane.GetAttributeLocation("position"), mesh.positions.ToArray(), VertexAttribPointerType.Float, 3);
     plane.SetID(mesh.ids.ToArray(), PrimitiveType.Triangles);
 }
Ejemplo n.º 6
0
 private static VAO CreateMesh(Shader shader)
 {
     Mesh mesh = Obj2Mesh.FromObj(Resourcen.suzanne);
     var vao = new VAO();
     vao.SetAttribute(shader.GetAttributeLocation("position"), mesh.positions.ToArray(), VertexAttribPointerType.Float, 3);
     vao.SetAttribute(shader.GetAttributeLocation("normal"), mesh.normals.ToArray(), VertexAttribPointerType.Float, 3);
     vao.SetID(mesh.ids.ToArray(), PrimitiveType.Triangles);
     return vao;
 }
Ejemplo n.º 7
0
 public PingPongExample(int width, int height)
 {
     fbo = new FBO();
     textureA = Texture.Create(width, height);
     textureB = Texture.Create(width, height);
     active = textureA;
     shaderCopy = PixelShader.Create(PixelShader.Copy);
     shaderGameOfLife = PixelShader.Create(Encoding.UTF8.GetString(Resources.GameOfLife));
 }
Ejemplo n.º 8
0
        public MainVisual()
        {
            var sVertex = Encoding.UTF8.GetString(Resourcen.vertex);
            var sFragment = Encoding.UTF8.GetString(Resourcen.fragment);
            shader = ShaderLoader.FromStrings(sVertex, sFragment);

            InitVBOs();
            timeSource.Start();
        }
Ejemplo n.º 9
0
        public VisualContext()
        {
            GL.Disable(EnableCap.DepthTest);
            GL.ClearColor(1, 0, 0, 0);

            surface = new FBO();
            textureSurface = Texture.Create(1, 1);

            shaderCopyToScreen = InitShaderCopyToScreen();
            shaderDefault = InitShaderDefault();
        }
Ejemplo n.º 10
0
        public MainVisual()
        {
            var sVertex = Encoding.UTF8.GetString(Resourcen.vertex);
            var sFragment = Encoding.UTF8.GetString(Resourcen.fragment);
            shader = ShaderLoader.FromStrings(sVertex, sFragment);

            geometry = CreateMesh(shader);

            CreatePerInstanceAttributes(geometry, shader);

            GL.Enable(EnableCap.DepthTest);
        }
Ejemplo n.º 11
0
        public MainVisual()
        {
            camera.FarClip = 500;
            camera.Distance = 30;
            var sVertex = Encoding.UTF8.GetString(Resourcen.vertex);
            var sFragment = Encoding.UTF8.GetString(Resourcen.fragment);
            shader = ShaderLoader.FromStrings(sVertex, sFragment);
            geometry = CreateMesh(shader);

            GL.Enable(EnableCap.DepthTest);
            GL.Enable(EnableCap.CullFace);
            timeSource.Start();
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Compiles and links vertex and fragment shaders from strings.
 /// </summary>
 /// <param name="sVertexShd_">The s vertex SHD_.</param>
 /// <param name="sFragmentShd_">The s fragment SHD_.</param>
 /// <returns>a new instance</returns>
 public static Shader FromStrings(string sVertexShd_, string sFragmentShd_)
 {
     Shader shd = new Shader();
     try
     {
         shd.Compile(sVertexShd_, ShaderType.VertexShader);
         shd.Compile(sFragmentShd_, ShaderType.FragmentShader);
         shd.Link();
     }
     catch(Exception e)
     {
         shd.Dispose();
         throw e;
     }
     return shd;
 }
Ejemplo n.º 13
0
        private static void CreatePerInstanceAttributes(VAO vao, Shader shader)
        {
            //per instance attributes
            var rnd = new Random(12);
            Func<float> Rnd01 = () => (float)rnd.NextDouble();
            Func<float> RndCoord = () => (Rnd01() - 0.5f) * 2.0f;
            var instancePositions = new Vector3[particelCount];
            for (int i = 0; i < particelCount; ++i)
            {
                instancePositions[i] = new Vector3(RndCoord(), RndCoord(), RndCoord());
            }
            vao.SetAttribute(shader.GetAttributeLocation("instancePosition"), instancePositions, VertexAttribPointerType.Float, 3, true);

            //todo: students: add per instance attribute speed here
            //var locInstSpeed = shader.GetAttributeLocation("instanceSpeed");
        }
Ejemplo n.º 14
0
 public void SetShader(string fragmentShaderText)
 {
     string sVertexShader = @"
         #version 130
         out vec2 uv;
         void main() {
             const vec2 vertices[4] = vec2[4](vec2(-1.0, -1.0),
                             vec2( 1.0, -1.0),
                             vec2( 1.0,  1.0),
                             vec2(-1.0,  1.0));
             vec2 pos = vertices[gl_VertexID];
             uv = pos * 0.5 + 0.5;
             gl_Position = vec4(pos, 1.0, 1.0);
         }";
     shader = ShaderLoader.FromStrings(sVertexShader, fragmentShaderText);
 }
Ejemplo n.º 15
0
        public Visual()
        {
            // Vertex positions
            float[] positions =
            {
                1.0f, -1.0f,
                1.0f, 1.0f,
                -1.0f, -1.0f,
                -1.0f, 1.0f
            };
            // Reserve a name for the buffer object.
            bufferQuad = GL.GenBuffer();
            // Bind it to the GL_ARRAY_BUFFER target.
            GL.BindBuffer(BufferTarget.ArrayBuffer, bufferQuad);
            // Allocate space for it (sizeof(positions)
            GL.BufferData(BufferTarget.ArrayBuffer
                , (IntPtr) (sizeof(float) * positions.Length), positions, BufferUsageHint.StaticDraw);

            GL.BindVertexArray(bufferQuad);
            GL.EnableClientState(ArrayCap.VertexArray);
            GL.VertexPointer(2, VertexPointerType.Float, 0, 0);
            GL.BindVertexArray(0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

            GL.Disable(EnableCap.DepthTest);
            GL.ClearColor(1, 0, 0, 0);

            surface = new FBO();
            textureSurface = Texture.Create(1, 1);

            string sVertexShader = @"
                varying vec2 uv;
                void main() {
                    gl_Position = gl_Vertex;
                    uv = gl_Vertex.xy * 0.5f + 0.5f;
                }";
            string sFragmentShd = @"
            varying vec2 uv;
            uniform sampler2D tex;
            void main() {
                gl_FragColor = texture(tex, uv);
            }";
            shaderCopyToScreen = Shader.LoadFromStrings(sVertexShader, sFragmentShd);
            shader = new Shader();
            texture1 = new Texture();
            texture2 = new Texture();
        }
Ejemplo n.º 16
0
        public MainVisual()
        {
            var sVertex = Encoding.UTF8.GetString(Resourcen.vertex);
            var sFragment = Encoding.UTF8.GetString(Resourcen.fragment);
            shader = ShaderLoader.FromStrings(sVertex, sFragment);

            //load geometry
            Mesh mesh = Obj2Mesh.FromObj(Resourcen.suzanne);
            geometry.SetAttribute(shader.GetAttributeLocation("position"), mesh.positions.ToArray(), VertexAttribPointerType.Float, 3);
            geometry.SetAttribute(shader.GetAttributeLocation("normal"), mesh.normals.ToArray(), VertexAttribPointerType.Float, 3);
            //geometry.SetAttribute(shader.GetAttributeLocation("uv"), mesh.uvs.ToArray(), VertexAttribPointerType.Float, 2);
            geometry.SetID(mesh.ids.ToArray(), PrimitiveType.Triangles);

            GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line); //draw wireframe

            //GL.Enable(EnableCap.DepthTest);
            //texDiffuse = TextureLoader.FromFile(pathMedia + "capsule0.jpg");
        }
Ejemplo n.º 17
0
 public ShaderFileDebugger(string vertexFile, string fragmentFile, byte[] vertexShader = null, byte [] fragmentShader = null)
 {
     if (File.Exists(vertexFile) && File.Exists(fragmentFile))
     {
         shaderWatcherVertex = new FileWatcher(vertexFile);
         shaderWatcherFragment = new FileWatcher(fragmentFile);
         CheckForShaderChange();
         while(null != LastException)
         {
             form.Hide();
             FormShaderExceptionFacade.ShowModal(LastException);
             CheckForShaderChange();
         }
     }
     else
     {
         var sVertex = Encoding.UTF8.GetString(vertexShader);
         var sFragment = Encoding.UTF8.GetString(fragmentShader);
         shader = ShaderLoader.FromStrings(sVertex, sFragment);
     }
 }
Ejemplo n.º 18
0
        public void LoadFragmentShader(string fileName)
        {
            string sVertexShader = @"
                #version 130
                uniform vec2 iResolution;
                varying vec2 uv;
                out vec2 fragCoord;
                void main() {
                    gl_Position = gl_Vertex;
                    uv = gl_Vertex.xy * 0.5f + 0.5f;
                    fragCoord = uv * iResolution;
                }";

            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException("Could not find fragment shader file '" + fileName + "'");
            }
            try
            {
                StreamReader sr = new StreamReader(fileName);
                string sFragmentShd = sr.ReadToEnd();
                sr.Dispose();
                shader = Shader.LoadFromStrings(sVertexShader, sFragmentShd);
            }
            catch(VertexShaderCompileException e)
            {
                throw new ShaderLoadException("Vertex shader compilation failed!" + Environment.NewLine + e.Message);
            }
            catch(FragmentShaderCompileException e)
            {
                throw new ShaderLoadException("Fragment shader compilation failed!" + Environment.NewLine + e.Message);
            }
            catch (ShaderLinkException e)
            {
                throw new ShaderLoadException("Shader link failed!" + Environment.NewLine + e.Message);
            }
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Reads the contents of a file into a string
        /// </summary>
        /// <param name="shaderFile">path to the shader file</param>
        /// <returns>string with contents of shaderFile</returns>
        public static string ShaderStringFromFileWithIncludes(string shaderFile, bool compileInclude)
        {
            string sShader = null;
            if (!File.Exists(shaderFile))
            {
                throw new FileNotFoundException("Could not find shader file '" + shaderFile + "'");
            }
            sShader = File.ReadAllText(shaderFile);

            //handle includes
            string sCurrentPath = Path.GetDirectoryName(shaderFile) + Path.DirectorySeparatorChar; // get path to current shader
            string sName = Path.GetFileName(shaderFile);
            //split into lines
            var lines = sShader.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
            var pattern = @"\s*#include\s+" + '"' + "(.+)" + '"';
            int lineNr = 1;
            foreach (var line in lines)
            {
                // Search for include pattern (e.g. #include raycast.glsl) (nested not supported)
                foreach (Match match in Regex.Matches(line, pattern, RegexOptions.Singleline))
                {
                    string sFullMatch = match.Value;
                    string sIncludeFileName = match.Groups[1].ToString(); // get the filename to include
                    string sIncludePath = sCurrentPath + sIncludeFileName; // build path to file

                    if (!File.Exists(sIncludePath))
                    {
                        throw new FileNotFoundException("Could not find include-file '" + sIncludeFileName + "' for shader '" + shaderFile + "'.");
                    }
                    string sIncludeShd = File.ReadAllText(sIncludePath); // read include as string
                    if (compileInclude)
                    {
                        using (var shader = new Shader())
                        {
                            try
                            {
                                shader.Compile(sIncludeShd, ShaderType.FragmentShader); //test compile include shader
                            }
                            catch (ShaderException e)
                            {
                                throw new ShaderException(e.Type,
                                    "include compile '" + sIncludePath + "'",
                                    e.Log, sIncludeShd);
                            }
                        }
                    }
                    sIncludeShd += Environment.NewLine + "#line " + lineNr.ToString() + Environment.NewLine;
                    sShader = sShader.Replace(sFullMatch, sIncludeShd); // replace #include with actual include
             			}
                ++lineNr;
            }
            return sShader;
        }
Ejemplo n.º 20
0
 public bool SetShader(string shaderFileName)
 {
     if (!shaders.TryGetValue(shaderFileName, out shaderCurrent))
     {
         shaderCurrent = shaderDefault;
     }
     Debug.Assert(null != shaderCurrent);
     if (!shaderCurrent.IsLinked)
     {
         shaderCurrent = shaderDefault;
     }
     shaderCurrent.Begin();
     return shaderCurrent != shaderDefault;
 }