Exemplo n.º 1
0
        /// <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>
        /// <exception cref="ShaderCompilationException"></exception>
        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)
            {
                string log = this.GetInfoLog();
                throw new ShaderCompilationException(
                          string.Format("Failed to link shader program with ID {0}. Log: {1}", ShaderProgramObject, log),
                          log);
            }
            if (vertexShader.GetCompileStatus() == false)
            {
                string log = vertexShader.GetInfoLog();
                throw new Exception(log);
            }
            if (fragmentShader.GetCompileStatus() == false)
            {
                string log = fragmentShader.GetInfoLog();
                throw new Exception(log);
            }

            GL.DetachShader(ShaderProgramObject, vertexShader.ShaderObject);
            GL.DetachShader(ShaderProgramObject, fragmentShader.ShaderObject);
            vertexShader.Delete();
            fragmentShader.Delete();
        }