-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from 17cupsofcoffee/custom_shaders
Custom shaders
- Loading branch information
Showing
9 changed files
with
254 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#version 130 | ||
|
||
in vec2 v_uv; | ||
in vec4 v_color; | ||
|
||
uniform sampler2D u_texture; | ||
uniform float u_red; | ||
uniform float u_green; | ||
uniform float u_blue; | ||
|
||
out vec4 o_color; | ||
|
||
void main() { | ||
o_color = v_color * texture(u_texture, v_uv) * vec4(u_red, u_green, u_blue, 1.0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use tetra::graphics::{self, Color, DrawParams, Font, Shader, Text, Texture, Vec2}; | ||
use tetra::{Context, ContextBuilder, State}; | ||
|
||
struct GameState { | ||
texture: Texture, | ||
shader: Shader, | ||
text: Text, | ||
|
||
timer: f32, | ||
red: f32, | ||
green: f32, | ||
blue: f32, | ||
} | ||
|
||
impl GameState { | ||
fn new(ctx: &mut Context) -> tetra::Result<GameState> { | ||
Ok(GameState { | ||
texture: Texture::new(ctx, "./examples/resources/player.png")?, | ||
shader: Shader::fragment(ctx, "./examples/resources/disco.frag")?, | ||
text: Text::new("", Font::default(), 32.0), | ||
|
||
timer: 0.0, | ||
red: 0.0, | ||
green: 0.0, | ||
blue: 0.0, | ||
}) | ||
} | ||
} | ||
|
||
impl State for GameState { | ||
fn update(&mut self, _ctx: &mut Context) -> tetra::Result { | ||
self.timer += 1.0; | ||
|
||
self.red = ((self.timer / 10.0).sin() + 1.0) / 2.0; | ||
self.green = ((self.timer / 100.0).sin() + 1.0) / 2.0; | ||
self.blue = ((self.timer / 1000.0).sin() + 1.0) / 2.0; | ||
|
||
self.text.set_content(format!( | ||
"Red: {:.2}\nGreen: {:.2}\nBlue: {:.2}", | ||
self.red, self.green, self.blue | ||
)); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn draw(&mut self, ctx: &mut Context, _dt: f64) -> tetra::Result { | ||
graphics::clear(ctx, Color::rgb(0.392, 0.584, 0.929)); | ||
|
||
graphics::set_shader(ctx, &self.shader); | ||
|
||
self.shader.set_uniform(ctx, "u_red", self.red); | ||
|
||
self.shader.set_uniform(ctx, "u_green", self.green); | ||
|
||
self.shader.set_uniform(ctx, "u_blue", self.blue); | ||
|
||
graphics::draw( | ||
ctx, | ||
&self.texture, | ||
DrawParams::new() | ||
.position(Vec2::new(640.0, 360.0)) | ||
.origin(Vec2::new(8.0, 8.0)) | ||
.scale(Vec2::new(24.0, 24.0)), | ||
); | ||
|
||
graphics::reset_shader(ctx); | ||
|
||
graphics::draw(ctx, &self.text, Vec2::new(16.0, 16.0)); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
fn main() -> tetra::Result { | ||
ContextBuilder::new("Custom Shaders", 1280, 720) | ||
.maximized(true) | ||
.resizable(true) | ||
.quit_on_escape(true) | ||
.build()? | ||
.run_with(GameState::new) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.