Пример #1
0
        public ModelShader()
        {
            ShaderBuilder vert = new ShaderBuilder( ShaderType.VertexShader, false );
            vert.AddUniform( ShaderVarType.Mat4, "view_matrix" );
            vert.AddUniform( ShaderVarType.Vec3, "model_pos" );
            vert.AddUniform( ShaderVarType.Vec4, "model_rot" );
            vert.AddUniform( ShaderVarType.Vec4, "colour" );
            vert.AddUniform( ShaderVarType.Float, "fog_density" );
            vert.AddAttribute( ShaderVarType.Vec3, "in_position" );
            vert.AddAttribute( ShaderVarType.Vec2, "in_texcoord" );
            vert.AddAttribute( ShaderVarType.Vec4, "in_colour" );
            vert.AddVarying( ShaderVarType.Vec2, "var_texcoord" );
            vert.AddVarying( ShaderVarType.Vec4, "var_colour" );
            vert.AddVarying( ShaderVarType.Float, "var_fogfactor" );
            vert.Logic = @"
                vec3 qtransform( vec4 q, vec3 v )
                {
                    return v + 2.0 * cross( cross( v, q.xyz ) + q.w * v, q.xyz );
                }

                void main( void )
                {
                    gl_Position = view_matrix * vec4( qtransform( model_rot, in_position ) + model_pos, 1 );
                    var_texcoord = in_texcoord;
                    var_colour = colour * in_colour;

                    const float LOG2 = 1.442695;
                    float dist = length( gl_Position );
                    var_fogfactor = exp2( - fog_density * fog_density * dist * dist * LOG2 );
                    var_fogfactor = clamp( var_fogfactor, 0.0, 1.0 );
                }
            ";

            ShaderBuilder frag = new ShaderBuilder( ShaderType.FragmentShader, false );
            frag.AddUniform( ShaderVarType.Sampler2D, "tex_diffuse" );
            frag.AddUniform( ShaderVarType.Sampler2D, "tex_mask" );
            frag.AddUniform( ShaderVarType.Bool, "flag_mask" );
            frag.AddUniform( ShaderVarType.Vec3, "fog_colour" );
            frag.AddVarying( ShaderVarType.Vec2, "var_texcoord" );
            frag.AddVarying( ShaderVarType.Vec4, "var_colour" );
            frag.AddVarying( ShaderVarType.Float, "var_fogfactor" );
            frag.Logic = @"
                void main( void )
                {
                    if( var_colour.a == 0.0 || var_fogfactor == 1.0 )
                        discard;
                    vec4 clr = texture2D( tex_diffuse, var_texcoord );
                    if( clr.a < 0.5 )
                        discard;
                    if( flag_mask && texture2D( tex_mask, var_texcoord ).a < 0.5 )
                        discard;
                    out_frag_colour = vec4( mix( fog_colour, clr.rgb * var_colour.rgb, var_fogfactor ), clr.a * var_colour.a );
                }
            ";

            VertexSource = vert.Generate( GL3 );
            FragmentSource = frag.Generate( GL3 );

            BeginMode = BeginMode.TriangleStrip;

            myColour = Color4.White;
            myAlphaMask = false;

            myBackfaceCulling = false;

            Create();
        }
Пример #2
0
        public ModelShader()
        {
            ShaderBuilder vert = new ShaderBuilder(ShaderType.VertexShader, false);

            vert.AddUniform(ShaderVarType.Mat4, "view_matrix");
            vert.AddUniform(ShaderVarType.Vec3, "model_pos");
            vert.AddUniform(ShaderVarType.Vec4, "model_rot");
            vert.AddUniform(ShaderVarType.Vec4, "colour");
            vert.AddUniform(ShaderVarType.Float, "fog_density");
            vert.AddAttribute(ShaderVarType.Vec3, "in_position");
            vert.AddAttribute(ShaderVarType.Vec2, "in_texcoord");
            vert.AddAttribute(ShaderVarType.Vec4, "in_colour");
            vert.AddVarying(ShaderVarType.Vec2, "var_texcoord");
            vert.AddVarying(ShaderVarType.Vec4, "var_colour");
            vert.AddVarying(ShaderVarType.Float, "var_fogfactor");
            vert.Logic = @"
                vec3 qtransform( vec4 q, vec3 v )
                { 
	                return v + 2.0 * cross( cross( v, q.xyz ) + q.w * v, q.xyz );
	            }

                void main( void )
                {
                    gl_Position = view_matrix * vec4( qtransform( model_rot, in_position ) + model_pos, 1 );
                    var_texcoord = in_texcoord;
                    var_colour = colour * in_colour;

                    const float LOG2 = 1.442695;
                    float dist = length( gl_Position );
                    var_fogfactor = exp2( - fog_density * fog_density * dist * dist * LOG2 );
                    var_fogfactor = clamp( var_fogfactor, 0.0, 1.0 );
                }
            ";

            ShaderBuilder frag = new ShaderBuilder(ShaderType.FragmentShader, false);

            frag.AddUniform(ShaderVarType.Sampler2D, "tex_diffuse");
            frag.AddUniform(ShaderVarType.Sampler2D, "tex_mask");
            frag.AddUniform(ShaderVarType.Bool, "flag_mask");
            frag.AddUniform(ShaderVarType.Vec3, "fog_colour");
            frag.AddVarying(ShaderVarType.Vec2, "var_texcoord");
            frag.AddVarying(ShaderVarType.Vec4, "var_colour");
            frag.AddVarying(ShaderVarType.Float, "var_fogfactor");
            frag.Logic = @"
                void main( void )
                {
                    if( var_colour.a == 0.0 || var_fogfactor == 1.0 )
                        discard;
                    vec4 clr = texture2D( tex_diffuse, var_texcoord );
                    if( clr.a < 0.5 )
                        discard;
                    if( flag_mask && texture2D( tex_mask, var_texcoord ).a < 0.5 )
                        discard;
                    out_frag_colour = vec4( mix( fog_colour, clr.rgb * var_colour.rgb, var_fogfactor ), clr.a * var_colour.a );
                }
            ";

            VertexSource   = vert.Generate(GL3);
            FragmentSource = frag.Generate(GL3);

            BeginMode = BeginMode.TriangleStrip;

            myColour    = Color4.White;
            myAlphaMask = false;

            myBackfaceCulling = false;

            Create();
        }