public static ComposedShader BuildDefaultShader(List <ShaderPart> additionalShaderParts = null)
        {
            var defaultsh = new ComposedShader();

            defaultsh.IsBuiltIn = true; // specifies this is a built in system shader
            defaultsh.language  = "GLSL";

            if (additionalShaderParts != null)
            {
                // FORWARD DECLARATION of additional shader parts
                foreach (ShaderPart part in additionalShaderParts)
                {
                    defaultsh.ShaderParts.Add(part);
                }
            }

            defaultsh.ShaderParts.Add(new ShaderPart()
            {
                ShaderSource = DefaultShader.vertexShaderSource,
                Type         = shaderPartTypeValues.VERTEX
            });

            defaultsh.ShaderParts.Add(new ShaderPart()
            {
                ShaderSource = DefaultShader.fragmentShaderSource,
                Type         = shaderPartTypeValues.FRAGMENT
            });

            return(defaultsh);
        }
        public static ComposedShader ApplyShader(string vertexShaderSource, string fragmentShaderSource)
        {
            ComposedShader shader = new ComposedShader();

            shader.IsBuiltIn = true; // specifies this is a built in system shader
            shader.language  = "GLSL";

            if (!string.IsNullOrEmpty(vertexShaderSource))
            {
                shader.ShaderParts.Add(new ShaderPart()
                {
                    ShaderSource = vertexShaderSource,
                    Type         = shaderPartTypeValues.VERTEX
                });
            }

            if (!string.IsNullOrEmpty(fragmentShaderSource))
            {
                shader.ShaderParts.Add(new ShaderPart()
                {
                    ShaderSource = fragmentShaderSource,
                    Type         = shaderPartTypeValues.FRAGMENT
                });
            }

            return(shader);
        }
        /// <summary>
        /// Create a copy of a Shader program using same source but linked in another new program instance.
        /// </summary>
        /// <param name="copyTarget">
        /// The shader to derive a new instance from.
        /// </param>
        /// <param name="link">
        /// If the shader copy is to be linked
        /// </param>
        /// <returns></returns>
        public static ComposedShader CreateNewInstance(ComposedShader copyTarget, bool link = true)
        {
            ComposedShader derived;
            ShaderPart     partClone;

            derived           = new ComposedShader();
            derived.language  = copyTarget.language;
            derived.IsBuiltIn = copyTarget.IsBuiltIn;

            foreach (ShaderPart part in copyTarget.ShaderParts)
            {
                partClone = new ShaderPart();
                partClone.ShaderSource = part.ShaderSource;
                partClone.Type         = part.Type;

                derived.ShaderParts.Add(partClone);
            }

            if (link)
            {
                derived.Link();
            }

            return(derived);
        }
Beispiel #4
0
 /// <summary>
 /// Precondition: Apply Buffer Pointers right before GL.DrawArrays or GL.DrawElements,
 /// requires default pointers to be bound in the shader after linking.
 /// </summary>
 public static void ApplyBufferPointers(ComposedShader shader)
 {
     // Set pointers to shader vertex attributes
     shader.SetPointer("position", VertexAttribType.Position);     // vertex position
     shader.SetPointer("normal", VertexAttribType.Normal);         // vertex normal
     shader.SetPointer("color", VertexAttribType.Color);           // vertex color
     shader.SetPointer("texcoord", VertexAttribType.TextureCoord); // vertex texCoordinate
 }
        public static ComposedShader ApplyShader(string vertexShaderSource, string fragmentShaderSource,
                                                 string tessControlSource = "", string tessEvalSource = "", string geometryShaderSource = "")
        {
            ComposedShader shader = new ComposedShader();

            shader.IsBuiltIn = true; // specifies this is a built in system shader
            shader.language  = "GLSL";

            if (!string.IsNullOrEmpty(vertexShaderSource))
            {
                shader.ShaderParts.Add(new ShaderPart()
                {
                    ShaderSource = vertexShaderSource.Trim(),
                    Type         = shaderPartTypeValues.VERTEX
                });
            }

            if (!string.IsNullOrEmpty(tessControlSource))
            {
                shader.ShaderParts.Add(new ShaderPart()
                {
                    ShaderSource = tessControlSource.Trim(),
                    Type         = shaderPartTypeValues.TESS_CONTROL
                });
            }

            if (!string.IsNullOrEmpty(tessEvalSource))
            {
                shader.ShaderParts.Add(new ShaderPart()
                {
                    ShaderSource = tessEvalSource.Trim(),
                    Type         = shaderPartTypeValues.TESS_EVAL
                });
            }


            if (!string.IsNullOrEmpty(geometryShaderSource))
            {
                shader.ShaderParts.Add(new ShaderPart()
                {
                    ShaderSource = geometryShaderSource.Trim(),
                    Type         = shaderPartTypeValues.GEOMETRY
                });
            }

            if (!string.IsNullOrEmpty(fragmentShaderSource))
            {
                shader.ShaderParts.Add(new ShaderPart()
                {
                    ShaderSource = fragmentShaderSource.Trim(),
                    Type         = shaderPartTypeValues.FRAGMENT
                });
            }


            return(shader);
        }
Beispiel #6
0
        public void Load()
        {
            Vertex v;
            List<Vertex> geometry = new List<Vertex>();

            v = new Vertex()
            {
                Position = new Vector3(0f, 0f, 0) + Position,
                TexCoord = new Vector2(0f, 1f)
            };
            geometry.Add(v);

            v = new Vertex()
            {
                Position = new Vector3(this.Height, 0f, 0) + Position,
                TexCoord = new Vector2(1f, 1f)
            };
            geometry.Add(v);

            v = new Vertex()
            {
                Position = new Vector3(this.Height, this.Height, 0) + Position,
                TexCoord = new Vector2(1f, 0f)
            };
            geometry.Add(v);

            v = new Vertex()
            {
                Position = new Vector3(0f, this.Height, 0) + Position,
                TexCoord = new Vector2(0f, 0f)
            };

            geometry.Add(v);



            //Bitmap bmpCross = ImageTexture.CreateBitmap(System.Drawing.Color.Black, 100, 100);
            //GraphicsUnit gunit = GraphicsUnit.Pixel;
            //var bounds = bmpCross.GetBounds(ref gunit);
            //this._image = ImageTexture.CreateTextureFromImage(bmpCross, bounds);


            //var @default = ShaderCompiler.BuildDefaultShader();
            //@default.Link();
            //@default.Use();

            var @default = ShaderCompiler.ApplyShader(PerlinNoiseShader.vertexShaderSource, PerlinNoiseShader.fragmentShaderSource);
            @default.Link();
            @default.Use();

            CurrentShader = @default;

            Buffering.BufferShaderGeometry(geometry, out vbo, out NumVerticies);
        }
        public void Load()
        {
            Vertex        v;
            List <Vertex> geometry = new List <Vertex>();

            v = new Vertex()
            {
                Position = new Vector3(0f, 0f, 0) + Position,
                TexCoord = new Vector2(0f, 1f)
            };
            geometry.Add(v);

            v = new Vertex()
            {
                Position = new Vector3(1.0f, 0f, 0) + Position,
                TexCoord = new Vector2(1f, 1f)
            };
            geometry.Add(v);

            v = new Vertex()
            {
                Position = new Vector3(1.0f, this.Height, 0) + Position,
                TexCoord = new Vector2(1f, 0f)
            };
            geometry.Add(v);

            v = new Vertex()
            {
                Position = new Vector3(0f, this.Height, 0) + Position,
                TexCoord = new Vector2(0f, 0f)
            };

            geometry.Add(v);


            Bitmap       bmpCross = Properties.Resources.crosshair;
            GraphicsUnit gunit    = GraphicsUnit.Pixel;
            var          bounds   = bmpCross.GetBounds(ref gunit);

            this._crosshair = ImageTexture.CreateTextureFromImage(bmpCross, bounds);

            var @default = ShaderCompiler.ApplyShader(CrosshairShader.vertexShaderSource, CrosshairShader.fragmentShaderSource);

            @default.Link();
            @default.Use();

            CurrentShader = @default;

            Buffering.BufferShaderGeometry(geometry, out vbo, out NumVerticies);
        }
Beispiel #8
0
        public static bool BufferMaterial(ComposedShader shader, ShaderMaterial material, string name, out int uboMaterial)
        {
            int uboIndex; // Index to use for the buffer binding (All good things start at 0 )

            //int uniformBlockLocation;

            uboIndex = 0;

            //uniformBlockLocation = GL.GetUniformBlockIndex(shader.ShaderHandle, name);

            //if(UniformBlockLocation > -1)
            //{
            //    GL.UniformBlockBinding(shader.ShaderHandle, uniformBlockLocation, uboIndex);
            //}


            uboMaterial = GL.GenBuffer();

            GL.BindBuffer(BufferTarget.UniformBuffer, uboMaterial);

            // Allocate Memory Request
            GL.BufferData(BufferTarget.UniformBuffer,
                          (IntPtr)(ShaderMaterial.Size), (IntPtr)(null), BufferUsageHint.DynamicDraw);

            // Bind the created Uniform Buffer to the Buffer Index
            GL.BindBufferRange(BufferRangeTarget.UniformBuffer,
                               uboIndex,
                               uboMaterial,
                               (IntPtr)0, ShaderMaterial.Size
                               );


            GL.BufferSubData <ShaderMaterial>(BufferTarget.UniformBuffer, (IntPtr)0, ShaderMaterial.Size, ref material);
            GL.BindBuffer(BufferTarget.UniformBuffer, 0);

            return(true);
        }
Beispiel #9
0
        public static bool BufferMaterials(ComposedShader shader, List <ShaderMaterial> materials, string name)
        {
            int uboMaterial;
            int uboIndex; // Index to use for the buffer binding. All binding indicies start from 0

            ShaderMaterial[] _materials;
            ShaderMaterial[] src;

            _materials = new ShaderMaterial[16]; // finely-sharpened imposes a limit of 16 of materials per object
            src        = materials.ToArray();
            Array.Copy(src, 0, _materials, 0, src.Length);

            uboIndex    = 0;
            uboMaterial = GL.GenBuffer();

            GL.BindBuffer(BufferTarget.UniformBuffer, uboMaterial);

            // Allocate Memory Request
            GL.BufferData <ShaderMaterial>(BufferTarget.UniformBuffer,
                                           (IntPtr)(_materials.Length * ShaderMaterial.Size),
                                           _materials, BufferUsageHint.StaticDraw);


            // Bind the created Uniform Buffer to the Buffer Index
            GL.BindBufferRange(BufferRangeTarget.UniformBuffer,
                               uboIndex,
                               uboMaterial,
                               (IntPtr)0,
                               _materials.Length * ShaderMaterial.Size
                               );


            // Cleanup
            GL.BindBuffer(BufferTarget.UniformBuffer, 0);

            return(true);
        }