protected override void OnLoad(EventArgs e) { this.shaderMan = new ShaderManager(); var shaderLoader = ShaderFileLoader.CreateDefault("shaders"); // load all shaders var shaders = shaderLoader.Load("").ToList(); this.shaderMan.Add(shaders); var layerShader = this.shaderMan.MakeShaderProgram() .WithVertexShader("copy").WithFragmentShader("copy") .As("layer"); var copyShader = this.shaderMan.MakeShaderProgram() .WithVertexShader("post").WithFragmentShader("copy") .As("copypost"); var wispShader = this.shaderMan.MakeShaderProgram() .WithVertexShader("wisp").WithFragmentShader("wisp") .As("wisp"); var rayShader = this.shaderMan.MakeShaderProgram() .WithVertexShader("crepuscularrays") .WithGeometryShader("crepuscularrays") .WithFragmentShader("crepuscularrays") .As("crepuscularrays"); this.renderTexture = new Texture(1, 1); this.renderTexture.SetParameters(TextureMinFilter.Linear, TextureMagFilter.Linear, TextureWrapMode.ClampToBorder, TextureWrapMode.ClampToBorder); this.renderTarget = new RenderTarget(this.renderTexture); var renderTextureUniform = new TextureUniform("diffuseTexture", this.renderTexture); this.copyToScreen = new PostProcessSurface(); this.copyToScreen.AddSettings(renderTextureUniform, new ColorUniform("color", Color.White)); copyShader.UseOnSurface(this.copyToScreen); this.raySurface = new VertexSurface<CrepuscularRayVertex>(PrimitiveType.Points); this.raySurface.AddSettings(renderTextureUniform); rayShader.UseOnSurface(this.raySurface); var rayGeo = new CrepuscularRayGeometry(this.raySurface); const int layerCount = 4; const float maxBrightness = 0.03f; const float brightnessStep = maxBrightness / layerCount; this.layers = Enumerable.Range(0, layerCount).Reverse() .Select(i => new Layer(layerShader, wispShader, rayGeo, "layer" + i, GameMath.Sqrt(brightnessStep * i), i * i * 5)) .ToList(); InputManager.Initialize(this.Mouse); }
public Layer(ISurfaceShader shader, ISurfaceShader wispShader, CrepuscularRayGeometry rayGeo, string filename, float brightness, int wisps) { this.rayGeo = rayGeo; var texture = new Texture("gfx/" + filename + ".png", true); this.surface = new PostProcessSurface(); this.surface.AddSettings( new TextureUniform("diffuseTexture", texture), new ColorUniform("color", Color.GrayScale((byte)(255 * brightness))) ); shader.UseOnSurface(this.surface); this.wispSurface = new IndexedSurface<UVColorVertexData>(); wispShader.UseOnSurface(this.wispSurface); this.wispGeo = new Sprite2DGeometry(this.wispSurface) { Color = Color.White.WithAlpha() }; this.wisps = Enumerable.Range(0, wisps) .Select(i => new Wisp()).ToList(); }
/// <summary> /// Initializes a new instance of the <see cref="TextureUniform"/> class. /// </summary> /// <param name="name">The name of this uniform.</param> /// <param name="texture">The initial <see cref="Texture"/> value of this uniform.</param> /// <param name="target">The initial <see cref="TextureUnit"/> used by this uniform.</param> public TextureUniform(string name, Texture texture, TextureUnit target = TextureUnit.Texture0) { this.name = name; this.Target = target; this.Texture = texture; }
/// <summary> /// Attaches a <see cref="Texture"/> to the specified <see cref="FramebufferAttachment"/>, so it can be rendered to. /// </summary> /// <param name="attachment">The attachment.</param> /// <param name="texture">The texture.</param> /// <param name="target">Texture target of the attachment.</param> public void Attach(FramebufferAttachment attachment, Texture texture, TextureTarget target = TextureTarget.Texture2D) { GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, this.handle); GL.FramebufferTexture2D(FramebufferTarget.DrawFramebuffer, attachment, target, texture, 0); GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, 0); }
/// <summary> /// Initializes a new instance of the <see cref="RenderTarget"/> class assigning a texture to its default color attachment. /// </summary> /// <param name="texture">The texture to attach.</param> public RenderTarget(Texture texture) : this() { this.Attach(FramebufferAttachment.ColorAttachment0, texture); }