示例#1
0
        public static Shader LoadShader(string file, bool usePath = false)
        {
            string source = LoadSource(file, usePath);

            if (!(source.Contains("#type vertex") && source.Contains("#type fragment")))
            {
                Log.Error("");
                return(null);
            }

            var sources = source.Split(new string[] { "#type vertex", "#type fragment" }, StringSplitOptions.RemoveEmptyEntries);

            var vertexID   = CompileShader(sources[0], ShaderType.VertexShader);
            var fragmentID = CompileShader(sources[1], ShaderType.FragmentShader);

            int programID = GL.CreateProgram();

            GL.AttachShader(programID, vertexID);
            GL.AttachShader(programID, fragmentID);
            GL.BindAttribLocation(programID, 0, "position");
            GL.BindAttribLocation(programID, 1, "textureCoords");
            GL.LinkProgram(programID);
            GL.ValidateProgram(programID);

            Shader shader = new Shader(programID, vertexID, fragmentID);

            Shaders.Add(shader);

            return(shader);
        }
        /// <summary>
        /// Create Program and load shader
        /// </summary>
        private void GetProgram()
        {
            string vertexShaderSrc = "uniform mat4 uMVPMatrix;   \n" +
                                     "attribute vec4 aPosition;    \n" +
                                     "attribute vec2 a_TextureCoordinates;    \n" +
                                     "varying vec2 v_TextureCoordinates;" +
                                     "void main()                  \n" +
                                     "{                            \n" +
                                     "   gl_Position = uMVPMatrix * aPosition;  \n" +
                                     "   v_TextureCoordinates = a_TextureCoordinates;\n" +
                                     "}                            \n";

            string fragmentShaderSrc = "precision mediump float;             \n" +
                                       "uniform sampler2D u_TextureUnit;             \n" +
                                       "varying vec2 v_TextureCoordinates;           \n" +
                                       "void main()                                  \n" +
                                       "{                                            \n" +
                                       "  gl_FragColor = texture2D(u_TextureUnit, v_TextureCoordinates);\n" +
                                       "}                                            \n";

            program = ShaderHelper.BuildProgram(vertexShaderSrc, fragmentShaderSrc);
            GL.BindAttribLocation(program, 0, "aPosition");
            GL.LinkProgram(program);
            GL.UseProgram(program);
        }
示例#3
0
 public void BindAttributes()
 {
     foreach (KeyValuePair <string, int> attrib in Attributes)
     {
         GL.BindAttribLocation(ProgramID, attrib.Value, attrib.Key);
     }
 }
示例#4
0
        /*
         * public bool LoadFromFile(string vs, string fs)
         * {
         *      string vss = "";
         *      string fss = "";
         *      return LoadFromString(vss, fss);
         * }
         */

        public bool LoadFromString(string vss, string fss)
        {
            program = GL.CreateProgram();

            vs = GL.CreateShader(ShaderType.VertexShader);
            fs = GL.CreateShader(ShaderType.FragmentShader);

            GL.ShaderSource(vs, vss);
            GL.ShaderSource(fs, fss);

            GL.CompileShader(vs);
            GL.CompileShader(fs);

            GL.AttachShader(program, vs);
            GL.AttachShader(program, fs);

            GL.BindAttribLocation(program, 0, "in_position");
            GL.BindAttribLocation(program, 1, "in_color");
            GL.BindFragDataLocation(program, 0, "out_color");
            GL.LinkProgram(program);
            GL.UseProgram(program);

            if (!PrintLog("Vertex Shader", vs) ||
                !PrintLog("Fragment Shader", fs) ||
                !PrintLog("Shader Program", program)
                )
            {
                return(false);
            }
            return(true);
        }
示例#5
0
        public void BindAttribute(string attrName)
        {
            AttributeProperties attr = this.GetAttributeByName(attrName);

            GL.BindAttribLocation(ProgramId, attr.Id, attr.Name);
            //GL.BindAttribLocation(ProgramId, 0, "vPosition")
        }
 public void BindLayouts(int shader)
 {
     foreach (KeyValuePair <int, string> pair in _layout)
     {
         GL.BindAttribLocation(shader, pair.Key, pair.Value);
     }
 }
示例#7
0
        private protected virtual bool CompileInternal()
        {
            foreach (ShaderPart p in parts)
            {
                if (!p.Compiled)
                {
                    p.Compile();
                }
                GL.AttachShader(this, p);

                foreach (ShaderInputInfo input in p.ShaderInputs)
                {
                    GL.BindAttribLocation(this, input.Location, input.Name);
                }
            }

            GL.LinkProgram(this);
            GL.GetProgram(this, GetProgramParameterName.LinkStatus, out int linkResult);

            foreach (var part in parts)
            {
                GL.DetachShader(this, part);
            }

            return(linkResult == 1);
        }
示例#8
0
        void CreateShaders()
        {
            ShaderProgramHandle = GL.CreateProgram();

            VertexShaderHandle   = GL.CreateShader(ShaderType.VertexShader);
            FragmentShaderHandle = GL.CreateShader(ShaderType.FragmentShader);

            GL.ShaderSource(VertexShaderHandle, VertexShaderSource);
            GL.ShaderSource(FragmentShaderHandle, FragmentShaderSource);

            GL.CompileShader(VertexShaderHandle);
            GL.CompileShader(FragmentShaderHandle);
            Console.WriteLine(GL.GetShaderInfoLog(VertexShaderHandle));
            Console.WriteLine(GL.GetShaderInfoLog(FragmentShaderHandle));

            GL.AttachShader(ShaderProgramHandle, VertexShaderHandle);
            GL.AttachShader(ShaderProgramHandle, FragmentShaderHandle);

            GL.BindAttribLocation(ShaderProgramHandle, 0, "vertex_position");
            GL.BindAttribLocation(ShaderProgramHandle, 1, "vertex_color");
            Console.WriteLine(GL.GetProgramInfoLog(ShaderProgramHandle));
            GL.LinkProgram(ShaderProgramHandle);
            Console.WriteLine(GL.GetProgramInfoLog(ShaderProgramHandle));
            GL.UseProgram(ShaderProgramHandle);
        }
示例#9
0
 internal void BindAttribute(VertexElementUsage usage, string name)
 {
     ThreadingHelper.BlockOnUIThread(() =>
     {
         GL.BindAttribLocation(program, (int)usage, name);
     });
 }
示例#10
0
        protected override void CreateShaderFromFile(string vertShader, string fragShader)
        {
            //Initialize the OpenGL Program
            _programId = GL.CreateProgram();

            int vertShaderId, fragShaderId;

            LoadShader(vertShader, ShaderType.VertexShader, _programId, out vertShaderId);
            LoadShader(fragShader, ShaderType.FragmentShader, _programId, out fragShaderId);

            //Deincriment the reference count on the shaders so that they
            //don't exist until the context is destroyed.
            GL.DeleteShader(vertShaderId);
            GL.DeleteShader(fragShaderId);

            GL.BindAttribLocation(_programId, (int)ShaderAttributeIds.Position, "vertexPos");
            GL.BindAttribLocation(_programId, (int)ShaderAttributeIds.Color, "inColor");
            GL.BindAttribLocation(_programId, (int)ShaderAttributeIds.TexCoord, "vertexUV");

            //Link shaders
            GL.LinkProgram(_programId);

            _uniformMVP = GL.GetUniformLocation(_programId, "modelview");

            if (GL.GetError() != ErrorCode.NoError)
            {
                Console.WriteLine(GL.GetProgramInfoLog(_programId));
            }
        }
示例#11
0
        private void Compile()
        {
            ShaderProgram = GL.CreateProgram();

            var vertexShader   = LoadShader(ShaderType.VertexShader, vertexShaderCode);
            var fragmentShader = LoadShader(ShaderType.FragmentShader, fragmentShaderCode);

            GL.AttachShader(ShaderProgram, vertexShader);
            GL.AttachShader(ShaderProgram, fragmentShader);
            GL.BindAttribLocation(ShaderProgram, (int)ShaderAttributeEntity.Vertex, "a_vertex");
            GL.BindAttribLocation(ShaderProgram, (int)ShaderAttributeEntity.Color, "a_color");
            GL.LinkProgram(ShaderProgram);

            MatrixHandle    = GL.GetUniformLocation(ShaderProgram, "u_matrix");
            PointSizeHandle = GL.GetUniformLocation(ShaderProgram, "u_pointSize");

            if (vertexShader != 0)
            {
                GL.DetachShader(ShaderProgram, vertexShader);
                GL.DeleteShader(vertexShader);
            }

            if (fragmentShader != 0)
            {
                GL.DetachShader(ShaderProgram, fragmentShader);
                GL.DeleteShader(fragmentShader);
            }

            GL.UseProgram(ShaderProgram);
        }
示例#12
0
        private void PlatformCreateShaders()
        {
            GLDebug.CheckAccess();

            var vertexShaderHandle   = GL.CreateShader(ShaderType.VertexShader);
            var fragmentShaderHandle = GL.CreateShader(ShaderType.FragmentShader);

            GL.ShaderSource(vertexShaderHandle, vertexShaderSource);
            GL.ShaderSource(fragmentShaderHandle, fragmentShaderSource);

            GL.CompileShader(vertexShaderHandle);
            GL.CompileShader(fragmentShaderHandle);

            // Create program
            shaderProgramHandle = GL.CreateProgram();

            GL.AttachShader(shaderProgramHandle, vertexShaderHandle);
            GL.AttachShader(shaderProgramHandle, fragmentShaderHandle);

            GL.BindAttribLocation(shaderProgramHandle, 0, "in_position");
            GL.BindAttribLocation(shaderProgramHandle, 1, "in_color");
            GL.BindAttribLocation(shaderProgramHandle, 2, "in_uv");

            GL.LinkProgram(shaderProgramHandle);

            Debug.WriteLine(GL.GetProgramInfoLog(shaderProgramHandle));
            GL.UseProgram(shaderProgramHandle);

            // Set uniforms
            transformLocation = GL.GetUniformLocation(shaderProgramHandle, "transform");
        }
示例#13
0
        public IShader Load(string vertexShaderName, string fragmentShaderName)
        {
            string vSource = EmbeddedShaderLoader.GetShader <GL20ShaderLoader>(vertexShaderName, "vert");
            string fSource = EmbeddedShaderLoader.GetShader <GL20ShaderLoader>(fragmentShaderName, "frag");

            int vShader = GL.CreateShader(ShaderType.VertexShader);
            int fShader = GL.CreateShader(ShaderType.FragmentShader);

            GL.ShaderSource(vShader, vSource);
            GL.ShaderSource(fShader, fSource);
            // Compile shaders
            GL.CompileShader(vShader);
            GL.CompileShader(fShader);
            Debug.WriteLine(GL.GetShaderInfoLog(vShader));
            Debug.WriteLine(GL.GetShaderInfoLog(fShader));

            int program = GL.CreateProgram();

            // Link and attach shaders to program
            GL.AttachShader(program, vShader);
            GL.AttachShader(program, fShader);

            GL.BindAttribLocation(program, 0, "in_screen_coords");
            GL.BindAttribLocation(program, 1, "in_uv");
            GL.BindAttribLocation(program, 2, "in_color");

            GL.LinkProgram(program);
            Debug.WriteLine(GL.GetProgramInfoLog(program));

            return(new GLShader(program, vShader, fShader));
        }
示例#14
0
 protected override void BindAttributes(int programHandle)
 {
     base.BindAttributes(programHandle);
     GL.BindAttribLocation(programHandle, 1, "vertex_normal");
     GL.BindAttribLocation(programHandle, 2, "left_color");
     GL.BindAttribLocation(programHandle, 3, "right_color");
 }
示例#15
0
        private int GenVertexArray(VBO vbo)
        {
            Console.WriteLine(@"In GenVertexArray");
            GL.GenVertexArrays(1, out int vaoHandle);
            GL.BindVertexArray(vaoHandle);
            switch (vbo.VertexFormat)
            {
            case InterleavedArrayFormat.T2fN3fV3f:
            {
                var stride = 32;
                GL.EnableVertexAttribArray(0);
                GL.BindBuffer(BufferTarget.ArrayBuffer, vbo.VboID);
                GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, stride, 20);
                GL.BindAttribLocation(Handle, 0, "vertexPosition_modelspace");

                GL.EnableVertexAttribArray(1);
                GL.BindBuffer(BufferTarget.ArrayBuffer, vbo.VboID);
                GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, true, stride, 8);
                GL.BindAttribLocation(Handle, 1, "vertexNormal_modelspace");

                GL.EnableVertexAttribArray(2);
                GL.BindBuffer(BufferTarget.ArrayBuffer, vbo.VboID);
                GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, stride, 0);
                GL.BindAttribLocation(Handle, 2, "in_texture");
                GL.BindVertexArray(0);
            }
            break;

            default:
                throw new Exception(@"unrecognized InterleavedArrayFormat");
            }
            return(vaoHandle);
        }
示例#16
0
        protected void Load2()
        {
            string vShaderStr = "uniform mat4 u_mvpMatrix;              \n" +
                                "attribute vec4 a_position;                  \n" +
                                "varying vec4 vColor;                \n" +
                                "void main()                                 \n" +
                                "{                                           \n" +
                                "   vColor = a_position;                \n" +
                                "   gl_Position = u_mvpMatrix * a_position;  \n" +
                                "}                                           \n";
            string fShaderStr = "precision mediump float;                            \n" +
                                "varying vec4 vColor;                \n" +
                                "void main()                                         \n" +
                                "{                                                   \n" +
                                "  gl_FragColor = vColor;        \n" +
                                "}                                                   \n";

            programObject2 = ShaderHelper.BuildProgram(vShaderStr, fShaderStr);
            GL.BindAttribLocation(programObject2, 0, "a_position");
            GL.LinkProgram(programObject2);
            vertices2 = new float[]
            {
                -0.5f, 0.5f, 0.5f, 1.0f,
                0.5f, 0.5f, 0.5f, 1.0f,
                -0.5f, 0.5f, -0.5f, 1.0f,
                0.5f, 0.5f, -0.5f, 1.0f,
                -0.5f, -0.5f, -0.5f, 1.0f,
                0.5f, -0.5f, -0.5f, 1.0f,
                0.5f, -0.5f, 0.5f, 1.0f,
                -0.5f, -0.5f, 0.5f, 1.0f
            };
        }
示例#17
0
        public Shader(string vertexCode, string fragmentCode)
        {
            VertexID   = LoadShader(vertexCode, ShaderType.VertexShader);
            FragmentID = LoadShader(fragmentCode, ShaderType.FragmentShader);

            ProgramID = GL.CreateProgram();
            GL.AttachShader(ProgramID, VertexID);
            GL.AttachShader(ProgramID, FragmentID);


            // Binds the matrix to the vertex shader sort of
            GL.BindAttribLocation(ProgramID, 0, "mvp");

            GL.LinkProgram(ProgramID);

            // Check if linked
            GL.GetProgram(ProgramID, GetProgramParameterName.LinkStatus, out int linked);
            if (linked < 1)
            {
                GL.GetProgramInfoLog(ProgramID, out string info);
                Console.WriteLine($"Failed to link shader program :\n{info}");
            }

            MVPID = GL.GetUniformLocation(ProgramID, "mvp");

            GL.DetachShader(ProgramID, VertexID);
            GL.DetachShader(ProgramID, FragmentID);
            GL.DeleteShader(VertexID);
            GL.DeleteShader(FragmentID);
        }
示例#18
0
        protected override void BindVertexAttributes()
        {
            base.BindVertexAttributes();

            GL.BindAttribLocation(pgmId, 2, "in_normal");
            GL.BindAttribLocation(pgmId, 3, "in_model");
        }
示例#19
0
        /// <summary>
        /// Creates a camera and sets up shaders for use in the viewport.
        /// </summary>
        private void SetUpViewport()
        {
            _programID = GL.CreateProgram();

            Cam = new Camera();

            int vertShaderId, fragShaderId;

            LoadShader("vs.glsl", ShaderType.VertexShader, _programID, out vertShaderId);
            LoadShader("fs.glsl", ShaderType.FragmentShader, _programID, out fragShaderId);

            GL.DeleteShader(vertShaderId);
            GL.DeleteShader(fragShaderId);

            GL.BindAttribLocation(_programID, (int)ShaderAttributeIds.Position, "vertexPos");
            GL.LinkProgram(_programID);

            _uniformMVP   = GL.GetUniformLocation(_programID, "modelview");
            _uniformColor = GL.GetUniformLocation(_programID, "col");

            if (GL.GetError() != ErrorCode.NoError)
            {
                Console.WriteLine(GL.GetProgramInfoLog(_programID));
            }
        }
示例#20
0
        public void CreateShader()
        {
            vertexShaderHandle   = GL.CreateShader(ShaderType.VertexShader);
            fragmentShaderHandle = GL.CreateShader(ShaderType.FragmentShader);

            GL.ShaderSource(vertexShaderHandle, vertexShaderSource);
            GL.ShaderSource(fragmentShaderHandle, fragmentShaderSource);

            GL.CompileShader(vertexShaderHandle);
            GL.CompileShader(fragmentShaderHandle);

            Debug.WriteLine(GL.GetShaderInfoLog(vertexShaderHandle));
            Debug.WriteLine(GL.GetShaderInfoLog(fragmentShaderHandle));


            // Create program
            shaderProgramHandle = GL.CreateProgram();

            GL.AttachShader(shaderProgramHandle, vertexShaderHandle);
            GL.AttachShader(shaderProgramHandle, fragmentShaderHandle);

            GL.BindAttribLocation(shaderProgramHandle, 0, "in_position");
            GL.BindAttribLocation(shaderProgramHandle, 1, "in_normal");

            GL.LinkProgram(shaderProgramHandle);

            Debug.WriteLine(GL.GetProgramInfoLog(shaderProgramHandle));
        }
示例#21
0
        public void Load()
        {
            _squareVertices = new[] {
                //Front Face
                -Size, -Size, Size,
                Size, -Size, Size,
                Size, Size, Size,
                -Size, Size, Size,
                //Back Face
                -Size, -Size, -Size,
                -Size, Size, -Size,
                Size, Size, -Size,
                Size, -Size, -Size,
                //Top Face
                -Size, Size, -Size,
                -Size, Size, Size,
                Size, Size, Size,
                Size, Size, -Size,
                //Bottom Face
                -Size, -Size, -Size,
                Size, -Size, -Size,
                Size, -Size, Size,
                -Size, -Size, Size,
                //Right Face
                Size, -Size, -Size,
                Size, Size, -Size,
                Size, Size, Size,
                Size, -Size, Size,
                //Left Face
                -Size, -Size, -Size,
                -Size, -Size, Size,
                -Size, Size, Size,
                -Size, Size, -Size
            };
            _shader = new ShaderProgram();
            using (var sr = new StreamReader("Shaders/Voxel.Vert"))
            {
                _shader.AddShader(ShaderType.VertexShader, sr.ReadToEnd());
            }
            using (var sr = new StreamReader("Shaders/VoxelGreen.Frag"))
            {
                _shader.AddShader(ShaderType.FragmentShader, sr.ReadToEnd());
            }
            GL.BindAttribLocation(_shader.Program, 0, "position");
            GL.BindAttribLocation(_shader.Program, 1, "instance_position");
            GL.BindAttribLocation(_shader.Program, 2, "instance_rotation");

            _shader.Link();
            GL.GenVertexArrays(1, out _squareVao);
            GL.GenBuffers(1, out _squareVbo);
            GL.BindVertexArray(_squareVao);
            GL.BindBuffer(BufferTarget.ArrayBuffer, _squareVbo);
            GL.EnableVertexAttribArray(0);
            GL.EnableVertexAttribArray(1);
            GL.EnableVertexAttribArray(2);
            GL.Arb.VertexAttribDivisor(1, 1);
            GL.Arb.VertexAttribDivisor(2, 1);

            Loaded = true;
        }
示例#22
0
        public void Bind(int shaderProgram)
        {
            GL.GenVertexArrays(1, out modelBuffer);
            GL.BindVertexArray(modelBuffer);

            GL.EnableVertexAttribArray(0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, positionBuffer);
            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0);
            GL.BindAttribLocation(shaderProgram, 0, "in_position");

            GL.EnableVertexAttribArray(1);
            GL.BindBuffer(BufferTarget.ArrayBuffer, normalBuffer);
            GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0);
            GL.BindAttribLocation(shaderProgram, 1, "in_normal");

            if (textures != null)
            {
                GL.EnableVertexAttribArray(2);
                GL.BindBuffer(BufferTarget.ArrayBuffer, textureBuffer);
                GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, true, Vector2.SizeInBytes, 0);
                GL.BindAttribLocation(shaderProgram, 2, "in_texture");
            }

            if (colors != null)
            {
                GL.EnableVertexAttribArray(3);
                GL.BindBuffer(BufferTarget.ArrayBuffer, colorBuffer);
                GL.VertexAttribPointer(3, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0);
                GL.BindAttribLocation(shaderProgram, 3, "in_color");
            }

            GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBuffer);
            GL.BindVertexArray(0);
        }
示例#23
0
        public void Load()
        {
            string vSource = m_vShaderSource;
            string fSource = m_fShaderSource;

            int vShader = GL.CreateShader(ShaderType.VertexShader);
            int fShader = GL.CreateShader(ShaderType.FragmentShader);

            GL.ShaderSource(vShader, vSource);
            GL.ShaderSource(fShader, fSource);
            // Compile shaders
            GL.CompileShader(vShader);
            GL.CompileShader(fShader);
            Debug.WriteLine(GL.GetShaderInfoLog(vShader));
            Debug.WriteLine(GL.GetShaderInfoLog(fShader));

            int program = GL.CreateProgram();

            // Link and attach shaders to program
            GL.AttachShader(program, vShader);
            GL.AttachShader(program, fShader);

            GL.BindAttribLocation(program, 0, "in_screen_coords");
            GL.BindAttribLocation(program, 1, "in_uv");
            GL.BindAttribLocation(program, 2, "in_color");

            GL.LinkProgram(program);
            Debug.WriteLine(GL.GetProgramInfoLog(program));

            m_Program        = program;
            m_VertexShader   = vShader;
            m_FragmentShader = fShader;

            m_Uniforms = new UniformDictionary(m_Program);
        }
        /// <summary>
        /// Creates the shader program.
        /// </summary>
        /// <param name="vertexShaderSource">The vertex shader source.</param>
        /// <param name="fragmentShaderSource">The fragment shader source.</param>
        /// <param name="attributeLocations">The attribute locations. This is an optional array of
        /// uint attribute locations to their names.</param>
        public void Create(string vertexShaderSource, string fragmentShaderSource,
                           Dictionary <uint, string> attributeLocations)
        {
            //  Create the shaders.
            vertexShader.Create(GL.GL_VERTEX_SHADER, vertexShaderSource);
            fragmentShader.Create(GL.GL_FRAGMENT_SHADER, fragmentShaderSource);

            //  Create the program, attach the shaders.
            shaderProgramObject = GL.CreateProgram();
            GL.AttachShader(shaderProgramObject, vertexShader.ShaderObject);
            GL.AttachShader(shaderProgramObject, fragmentShader.ShaderObject);

            //  Before we link, bind any vertex attribute locations.
            if (attributeLocations != null)
            {
                foreach (var vertexAttributeLocation in attributeLocations)
                {
                    GL.BindAttribLocation(shaderProgramObject, vertexAttributeLocation.Key, vertexAttributeLocation.Value);
                }
            }

            //  Now we can link the program.
            GL.LinkProgram(shaderProgramObject);

            //  Now that we've compiled and linked the shader, check it's link status. If it's not linked properly, we're
            //  going to throw an exception.
            if (GetLinkStatus() == false)
            {
                throw new Exception();
            }
        }
示例#25
0
        protected override void createVAOs()
        {
            if (material == null)
            {
                Log.WriteLine(Log.LOG_ERROR, "can not create VAOs without material set for model '" + Name + "'");
                return;
            }

            GL.GenVertexArrays(1, out vaoHandle);
            GL.BindVertexArray(vaoHandle);

            GL.EnableVertexAttribArray(0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, positionVboHandle);
            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0);
            GL.BindAttribLocation(material.ProgramHandle, 0, "in_position");

            GL.EnableVertexAttribArray(1);
            GL.BindBuffer(BufferTarget.ArrayBuffer, normalVboHandle);
            GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0);
            GL.BindAttribLocation(material.ProgramHandle, 1, "in_normal");

            GL.BindBuffer(BufferTarget.ElementArrayBuffer, eboHandle);

            GL.BindVertexArray(0);
        }
示例#26
0
        /// <summary>
        /// Creates the shader program.
        /// </summary>
        /// <param name="gl">The gl.</param>
        /// <param name="vertexShaderSource">The vertex shader source.</param>
        /// <param name="fragmentShaderSource">The fragment shader source.</param>
        /// <param name="attributeLocations">The attribute locations. This is an optional array of
        /// uint attribute locations to their names.</param>
        /// <exception cref="ShaderCompilationException"></exception>
        public void Create(GL gl, string vertexShaderSource, string fragmentShaderSource,
                           Dictionary <uint, string> attributeLocations)
        {
            //  Create the shaders.
            vertexShader.Create(gl, (uint)GLEnum.VertexShader, vertexShaderSource);
            fragmentShader.Create(gl, (uint)GLEnum.FragmentShader, fragmentShaderSource);

            //  Create the program, attach the shaders.
            shaderProgramObject = gl.CreateProgram();
            gl.AttachShader(shaderProgramObject, vertexShader.ShaderObject);
            gl.AttachShader(shaderProgramObject, fragmentShader.ShaderObject);

            //  Before we link, bind any vertex attribute locations.
            if (attributeLocations != null)
            {
                foreach (var vertexAttributeLocation in attributeLocations)
                {
                    gl.BindAttribLocation(shaderProgramObject, vertexAttributeLocation.Key, vertexAttributeLocation.Value);
                }
            }

            //  Now we can link the program.
            gl.LinkProgram(shaderProgramObject);

            //  Now that we've compiled and linked the shader, check it's link status. If it's not linked properly, we're
            //  going to throw an exception.
            if (GetLinkStatus(gl) == false)
            {
                throw new ShaderCompilationException(string.Format("Failed to link shader program with ID {0}.", shaderProgramObject), GetInfoLog(gl));
            }
        }
示例#27
0
 private void bindAttributes(string[] inVariables)
 {
     for (int i = 0; i < inVariables.Length; i++)
     {
         GL.BindAttribLocation(programID, i, inVariables[i]);
     }
 }
示例#28
0
        public ShaderProgram(string vertexShaderPath, string fragmentShaderPath)
        {
            ProgramId = GL.CreateProgram();

            LoadShader(vertexShaderPath, ShaderType.VertexShader, ProgramId);
            LoadShader(fragmentShaderPath, ShaderType.FragmentShader, ProgramId);

            GL.BindAttribLocation(ProgramId, (int)ShaderAttributeIds.Position, "vertexPos");
            GL.BindAttribLocation(ProgramId, (int)ShaderAttributeIds.Color0, "color0");
            GL.BindAttribLocation(ProgramId, (int)ShaderAttributeIds.Tex0, "texCoord0");

            GL.LinkProgram(ProgramId);

            if (GL.GetError() != ErrorCode.NoError)
            {
                Console.WriteLine("[EditorCore.Rendering] Error linking shader. Result: {0}", GL.GetProgramInfoLog(ProgramId));
            }

            UniformMVP   = GL.GetUniformLocation(ProgramId, "modelview");
            UniformColor = GL.GetUniformLocation(ProgramId, "inColor");

            if (GL.GetError() != ErrorCode.NoError)
            {
                Console.WriteLine("[EditorCore.Rendering] Failed to get modelview uniform for shader. Result: {0}", GL.GetProgramInfoLog(ProgramId));
            }
        }
示例#29
0
        /// <summary>
        /// Links a vertex and fragment shader together to create a shader program.
        /// </summary>
        /// <param name="vertexShader">Specifies the vertex shader.</param>
        /// <param name="fragmentShader">Specifies the fragment shader.</param>
        public ShaderProgram(Shader vertexShader, Shader fragmentShader)
        {
            this.VertexShader    = vertexShader;
            this.FragmentShader  = fragmentShader;
            this.ProgramId       = GL.CreateProgram();
            this.DisposeChildren = false;

            GL.BindAttribLocation(ProgramId, VertexPosition, "vertexPosition");
            GL.BindAttribLocation(ProgramId, VertexColor, "vertexColor");
            GL.BindAttribLocation(ProgramId, VertexSize, "vertexSize");
            GL.BindAttribLocation(ProgramId, TextureCoords, "textureCoord");

            GL.AttachShader(ProgramId, vertexShader.ShaderID);
            GL.AttachShader(ProgramId, fragmentShader.ShaderID);

            GL.LinkProgram(ProgramId);

            int status;

            GL.GetProgram(ProgramId, GetProgramParameterName.LinkStatus, out status);
            if (status == 0)
            {
                Logging.Error("Error Linking Program: {0}", ProgramLog);
            }

            GetParams();
        }
示例#30
0
        private void LoadBuffers()
        {
            var positionAttributeLocation = GL.GetAttribLocation(programId, "a_verts");
            var texCoordsAttribLocation   = GL.GetAttribLocation(programId, "a_texCoords");

            GL.BindAttribLocation(programId, positionAttributeLocation, "a_verts");
            GL.BindAttribLocation(programId, texCoordsAttribLocation, "a_texCoords");

            vertexArrayId = GL.GenVertexArray();
            GL.BindVertexArray(vertexArrayId);

            var positionBuffer = GL.GenBuffer();

            GL.BindBuffer(BufferTarget.ArrayBuffer, positionBuffer);
            GL.BufferData <Vector3>(BufferTarget.ArrayBuffer, (IntPtr)(cubeVertices.Count() * Vector3.SizeInBytes),
                                    cubeVertices, BufferUsageHint.StaticDraw);
            GL.VertexAttribPointer(positionAttributeLocation, 3, VertexAttribPointerType.Float, false, 0, 0);

            var textureBuffer = GL.GenBuffer();

            GL.BindBuffer(BufferTarget.ArrayBuffer, textureBuffer);
            GL.BufferData <Vector2>(BufferTarget.ArrayBuffer, (IntPtr)(texCoords.Count() * Vector2.SizeInBytes), texCoords, BufferUsageHint.StaticDraw);
            GL.VertexAttribPointer(texCoordsAttribLocation, 2, VertexAttribPointerType.Float, false, 0, 0);

            var indexBuffer = GL.GenBuffer();

            GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBuffer);
            GL.BufferData <int>(BufferTarget.ElementArrayBuffer, (IntPtr)(indices.Length * sizeof(int)), indices, BufferUsageHint.StaticDraw);

            GL.EnableVertexAttribArray(positionAttributeLocation);
            GL.EnableVertexAttribArray(texCoordsAttribLocation);
        }