Struct thin_engine::ResizableDepthTexture2D
source · pub struct ResizableDepthTexture2D {
pub texture: Option<DepthTexture2d>,
/* private fields */
}
Expand description
resizable depth texture. use with gliums SimpleFrameBuffer::WithDepthTexture()
to create a texture you can draw on! usefull for things like fog.
Fields§
§texture: Option<DepthTexture2d>
Implementations§
source§impl ResizableDepthTexture2D
impl ResizableDepthTexture2D
pub fn resize(&mut self, display: &Display, new_size: (u32, u32))
sourcepub fn texture(&self) -> &DepthTexture2d
pub fn texture(&self) -> &DepthTexture2d
borrows the texture or panics. to handle failed borrows use self.texture.as_ref()
instead
pub fn new(size: (u32, u32), display: &Display) -> Self
sourcepub fn resize_to_display(&mut self, display: &Display)
pub fn resize_to_display(&mut self, display: &Display)
Examples found in repository?
examples/simple-fxaa.rs (line 91)
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
fn main() {
use Action::*;
let (event_loop, window, display) = thin_engine::set_up().unwrap();
window.set_title("FXAA Test");
let _ = window.set_cursor_grab(CursorGrabMode::Locked);
window.set_cursor_visible(false);
let mut colour = ResizableTexture2D::default();
let mut depth = ResizableDepthTexture2D::default();
let mut input = InputMap::new([
(vec![Input::keycode(KeyCode::ArrowLeft), Input::keycode(KeyCode::KeyA)], Left),
(vec![Input::keycode(KeyCode::ArrowRight), Input::keycode(KeyCode::KeyD)], Right),
(vec![Input::keycode(KeyCode::ArrowUp), Input::keycode(KeyCode::KeyW)], Forward),
(vec![Input::keycode(KeyCode::ArrowDown), Input::keycode(KeyCode::KeyS)], Back),
(vec![Input::keycode(KeyCode::KeyF)], FXAA),
(vec![Input::keycode(KeyCode::Space)], Jump)
]);
let (screen_indices, verts, uvs) = mesh!(
&display, &screen::INDICES, &screen::VERTICES, &screen::UVS
);
let screen_mesh = (&verts, &uvs);
let (indices, verts, norms) = mesh!(
&display, &teapot::INDICES, &teapot::VERTICES, &teapot::NORMALS
);
let teapot_mesh = (&verts, &norms);
let draw_parameters = DrawParameters {
backface_culling: draw_parameters::BackfaceCullingMode::CullClockwise,
..params::alias_3d()
};
let mut fxaa_on = true;
let program = Program::from_source(
&display, shaders::VERTEX,
"#version 140
out vec4 colour;
in vec3 v_normal;
uniform vec3 light;
uniform mat4 camera;
uniform vec3 ambient;
uniform vec3 albedo;
uniform float shine;
void main() {
vec3 camera_dir = inverse(mat3(camera)) * vec3(0, 0, -1);
vec3 half_dir = normalize(camera_dir + light);
float specular = pow(max(dot(half_dir, v_normal), 0.0), shine);
float light_level = max(dot(light, v_normal), 0.0);
colour = vec4(albedo * light_level + ambient + vec3(specular), 1.0);
}", None
).unwrap();
let fxaa = shaders::fxaa_shader(&display).unwrap();
let normal = Program::from_source(
&display, shaders::SCREEN_VERTEX,
"#version 140
in vec2 uv;
uniform sampler2D tex;
out vec4 colour;
void main() {
colour = texture(tex, uv);
}", None
).unwrap();
let mut pos = vec3(0.0, 0.0, -30.0);
let mut rot = vec2(0.0, 0.0);
const DELTA: f32 = 0.016;
thin_engine::run(event_loop, &mut input, |input| {
// using a small resolution to show the effect.
// `let size = window.inner_size().into();`
// can be used isntead to set resolution to window size
let size = (380, 216);
display.resize(size);
depth.resize_to_display(&display);
colour.resize_to_display(&display);
//press f to toggle FXAA
if input.pressed(FXAA) { fxaa_on = !fxaa_on }
let colour = colour.texture.as_ref().unwrap();
let depth = depth.texture.as_ref().unwrap();
let mut frame = SimpleFrameBuffer::with_depth_buffer(
&display, colour, depth
).unwrap();
let view = Mat4::view_matrix_3d(size, 1.0, 1024.0, 0.1);
//set camera rotation
rot += input.mouse_move.scale(DELTA * 2.0);
rot.y = rot.y.clamp(-PI / 2.0, PI / 2.0);
let rx = Quaternion::from_y_rotation(rot.x);
let ry = Quaternion::from_x_rotation(rot.y);
let rot = rx * ry;
//move player based on view
let x = input.axis(Right, Left);
let y = input.axis(Forward, Back);
let move_dir = vec3(x, 0.0, y).normalise().scale(5.0*DELTA);
pos += move_dir.transform(&Mat3::from_rot(rx));
frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
//draw teapot
frame.draw(
teapot_mesh, &indices,
&program, &uniform! {
view: view,
model: Mat4::from_scale(Vec3::splat(0.1)),
camera: Mat4::from_inverse_transform(pos, Vec3::ONE, rot),
light: vec3(0.1, 0.25, -1.0).normalise(),
albedo: vec3(0.5, 0.1, 0.4),
ambient: vec3(0.0, 0.05, 0.1),
shine: 10.0f32,
},
&draw_parameters,
).unwrap();
let mut frame = display.draw();
frame.draw(
screen_mesh, &screen_indices, if fxaa_on { &fxaa } else { &normal },
&shaders::fxaa_uniforms(colour), &DrawParameters::default()
).unwrap();
frame.finish().unwrap();
thread::sleep(Duration::from_millis(16));
}).unwrap();
}
Trait Implementations§
source§impl Default for ResizableDepthTexture2D
impl Default for ResizableDepthTexture2D
source§fn default() -> ResizableDepthTexture2D
fn default() -> ResizableDepthTexture2D
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl !Freeze for ResizableDepthTexture2D
impl !RefUnwindSafe for ResizableDepthTexture2D
impl !Send for ResizableDepthTexture2D
impl !Sync for ResizableDepthTexture2D
impl Unpin for ResizableDepthTexture2D
impl !UnwindSafe for ResizableDepthTexture2D
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Convert
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Convert
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.