OpenGlobe之ShaderProgramGL3x

发布时间 2023-10-30 14:04:31作者: 2086nmj

该类有几个成员变量:

        private readonly ShaderObjectGL3x _vertexShader;
        private readonly ShaderObjectGL3x _geometryShader;
        private readonly ShaderObjectGL3x _fragmentShader;
        private readonly ShaderProgramNameGL3x _program;
        private readonly FragmentOutputsGL3x _fragmentOutputs;
        private readonly ShaderVertexAttributeCollection _vertexAttributes;
        private readonly IList<ICleanable> _dirtyUniforms;
        private readonly UniformCollection _uniforms;
        private readonly UniformBlockCollection _uniformBlocks;

构造函数:

public ShaderProgramGL3x(string vertexShaderSource,string geometryShaderSource,string fragmentShaderSource)
{
            _vertexShader = new ShaderObjectGL3x(ShaderType.VertexShader, vertexShaderSource);
            if (geometryShaderSource.Length > 0)
            {
                _geometryShader = new ShaderObjectGL3x(ShaderType.GeometryShaderExt, geometryShaderSource);
            }
            _fragmentShader = new ShaderObjectGL3x(ShaderType.FragmentShader, fragmentShaderSource);

            _program = new ShaderProgramNameGL3x();
            int programHandle = _program.Value;

            GL.AttachShader(programHandle, _vertexShader.Handle);
            if (geometryShaderSource.Length > 0)
            {
                GL.AttachShader(programHandle, _geometryShader.Handle);
            }
            GL.AttachShader(programHandle, _fragmentShader.Handle);

            GL.LinkProgram(programHandle);

            int linkStatus;
            GL.GetProgram(programHandle, ProgramParameter.LinkStatus, out linkStatus);

            if (linkStatus == 0)
            {
                throw new CouldNotCreateVideoCardResourceException("Could not link shader program.  Link Log:  \n\n" + ProgramInfoLog);
            }

            _fragmentOutputs = new FragmentOutputsGL3x(_program);
            _vertexAttributes = FindVertexAttributes(_program);
            _dirtyUniforms = new List<ICleanable>();
            _uniforms = FindUniforms(_program);
            _uniformBlocks = FindUniformBlocks(_program);

            InitializeAutomaticUniforms(_uniforms);
}

传入两个参数:vertexShaderSource和fragmentShaderSource,利用ShaderObjectGL3x对它们初始化(编译)。然后将两个shader的句柄handle链接到programHandle。