# core/fullscreenpass (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/core/fullscreenpass/

Fullscreen pass. (one triangle covering the whole screen + a fragment shader)
- A port of the original core/fullscreenpass.js. The basic unit of screen-wide effects such as backgrounds / post-processing. (used by ScreenEffect)
- Builds the triangle from gl_VertexID without a vertex buffer. Draws with an empty vertex array bound. (both GL 3.3 core and WebGL2 only need a vertex array bound)
- The shader is GLSL ES 3.00 like the original. On desktop, ShaderProgram replaces only the header with #version 330 core before compiling.
- Differences from the original: it does not hold the GL context but takes it in each function. Creation failure returns Err instead of an exception. GL resources are deleted with destroy.

## FullscreenPass (struct)

Fullscreen pass. (paints a single triangle that covers the whole screen with a fragment shader)
- The fragment shader can receive fragmentCoordinate (clip coordinates, -1 to 1) and fragmentTextureCoordinate (texture coordinates, 0 to 1) as in.
- Order of use: use_program, upload uniforms, draw. When finished, destroy deletes the GL resources.

Example: Darkening the screen edges

```rust
use vanilla::{FullscreenPass, Graphic};

const VIGNETTE_FRAGMENTSHADER_SOURCE: &str = "#version 300 es
precision highp float;
in vec2 fragmentTextureCoordinate;
uniform float strength;
out vec4 outputColor;
void main() {
	float centerDistance = length(fragmentTextureCoordinate - 0.5);
	outputColor = vec4(0.0, 0.0, 0.0, clamp(centerDistance * strength, 0.0, 1.0));
}
";

fn create_vignette(graphic: &Graphic) -> Result<FullscreenPass, String> {
	FullscreenPass::new(graphic.get_gl(), VIGNETTE_FRAGMENTSHADER_SOURCE)
}

fn draw_vignette(graphic: &mut Graphic, fullscreen_pass: &FullscreenPass) {
	graphic.run_external_pass(false, |gl| {
		fullscreen_pass.use_program(gl);
		unsafe {
			gl.uniform_1_f32(fullscreen_pass.get_uniform_location(gl, "strength").as_ref(), 1.2);
		}
		fullscreen_pass.draw(gl);
	});
}

fn destroy_vignette(graphic: &Graphic, fullscreen_pass: FullscreenPass) {
	fullscreen_pass.destroy(graphic.get_gl());
}
```

- `pub fn new(gl: &GlContext, fragment_shader_source: &str) -> Result<FullscreenPass, String>` (constructor, since 0.3.2): Constructor. (an error holding the log if the fragment shader compile / link fails)
  - `gl` &GlContext: GL context. (get_gl of Graphic)
  - `fragment_shader_source` &str: Fragment shader source. (GLSL ES 3.00, leading and trailing whitespace is trimmed)
  - returns Result<FullscreenPass, String>: The created pass. Err holding the error text if shader compile or link, or vertex array creation fails.
- `pub fn use_program(&self, gl: &GlContext)` (method, since 0.3.2): Starts using the pass. (binds the program and an empty vertex array; the original useProgram)
  - `gl` &GlContext: GL context that created the pass.
- `pub fn draw(&self, gl: &GlContext)` (method, since 0.3.2): Output. (called after use_program and setting uniforms)
  - `gl` &GlContext: GL context that created the pass. (draws one triangle with 3 vertices)
- `pub fn get_uniform_location(&self, gl: &GlContext, uniform_name: &str) -> Option<GlUniformLocation>` (method, since 0.3.2): Returns the uniform location. (cached)
  - `gl` &GlContext: GL context that created the pass.
  - `uniform_name` &str: Uniform name. (exactly as named in the shader source)
  - returns Option<GlUniformLocation>: Uniform location. None if there is no active uniform with that name.
- `pub fn get_shader_program(&self) -> &ShaderProgram` (method, since 0.3.2): Returns the shader program.
  - returns &ShaderProgram: Shader program of the pass.
- `pub fn get_vertex_array(&self) -> GlVertexArray` (method, since 0.3.2): Returns the vertex array.
  - returns GlVertexArray: Empty vertex array bound when drawing.
- `pub fn destroy(&self, gl: &GlContext)` (method, since 0.3.2): Destroy. (deletes the GL program and vertex array)
  - `gl` &GlContext: GL context that created the pass.
