Struct 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 and fxaa.

Fields§

§texture: Option<DepthTexture2d>

Implementations§

Source§

impl ResizableDepthTexture2d

Source

pub fn resize(&mut self, display: &Display, new_size: (u32, u32))

Source

pub fn texture(&self) -> &DepthTexture2d

borrows the texture or panics. to handle failed borrows use self.texture.as_ref() instead

Examples found in repository?
examples/simple-fxaa.rs (line 128)
13fn main() {
14    use Action::*;
15    let event_loop = EventLoop::new().unwrap();
16    event_loop.set_control_flow(ControlFlow::Poll);
17
18    let mut colour = ResizableTexture2d::default();
19    let mut depth = ResizableDepthTexture2d::default();
20
21    let input = { use base_input_codes::*; input_map!(
22        (Left,    ArrowLeft,  KeyA, LeftStickLeft ),
23        (Right,   ArrowRight, KeyD, LeftStickRight),
24        (Forward, ArrowUp,    KeyW, LeftStickUp   ),
25        (Back,    ArrowDown,  KeyS, LeftStickDown ),
26        (LookRight, MouseMoveRight, RightStickRight),
27        (LookLeft,  MouseMoveLeft,  RightStickLeft ),
28        (LookUp,    MouseMoveUp,    RightStickUp   ),
29        (LookDown,  MouseMoveDown,  RightStickDown ),
30        (FXAA,      KeyF,       GamepadInput::North)
31    ) };
32    struct Graphics {
33        screen_indices: IndexBuffer<u32>,
34        screen_vertices: VertexBuffer<Vertex>,
35        screen_uvs: VertexBuffer<TextureCoords>,
36
37        teapot_indices: IndexBuffer<u16>,
38        teapot_vertices: VertexBuffer<Vertex>,
39        teapot_uvs: VertexBuffer<TextureCoords>,
40        teapot_normals: VertexBuffer<Normal>,
41
42        fxaa: Program, normal: Program, program: Program
43    }
44    let graphics: Rc<RefCell<Option<Graphics>>> = Rc::default();
45    let graphics_setup = graphics.clone();
46
47    let draw_parameters = DrawParameters {
48        backface_culling: draw_parameters::BackfaceCullingMode::CullClockwise,
49        ..params::alias_3d()
50    };
51    let mut fxaa_on = true;
52    
53    let mut pos = vec3(0.0, 0.0, -30.0);
54    let mut rot = vec2(0.0, 0.0);
55    
56    let mut frame_start = Instant::now();
57
58    thin_engine::builder(input).with_setup(|display, window, _| {
59        window.set_title("FXAA Test");
60        let _ = window.set_cursor_grab(CursorGrabMode::Confined);
61        let _ = window.set_cursor_grab(CursorGrabMode::Locked);
62        window.set_cursor_visible(false);
63
64        let (screen_indices, screen_vertices, screen_uvs) = mesh!(
65            display, &screen::INDICES, &screen::VERTICES, &screen::UVS
66        );
67        let (teapot_indices, teapot_vertices, teapot_uvs, teapot_normals) = mesh!(
68            display, &teapot::INDICES, &teapot::VERTICES, &[] as &[TextureCoords; 0], &teapot::NORMALS
69        );
70
71        let program = Program::from_source(
72            display, shaders::VERTEX,
73            "#version 140
74            out vec4 colour;
75            in vec3 v_normal;
76            uniform vec3 light;
77            uniform mat4 camera;
78            uniform vec3 ambient;
79            uniform vec3 albedo;
80            uniform float shine;
81            void main() {
82                vec3 camera_dir = inverse(mat3(camera)) * vec3(0, 0, -1);
83                vec3 half_dir = normalize(camera_dir + light);
84                float specular = pow(max(dot(half_dir, v_normal), 0.0), shine);
85                float light_level = max(dot(light, v_normal), 0.0);
86                colour = vec4(albedo * light_level + ambient + vec3(specular), 1.0);
87            }", None
88        ).unwrap();
89        let fxaa = shaders::fxaa_shader(display).unwrap();
90        let normal = Program::from_source(
91            display, shaders::SCREEN_VERTEX, 
92            "#version 140
93            in vec2 uv;
94            uniform sampler2D tex;
95            out vec4 colour;
96            void main() {
97                colour = texture(tex, uv);
98            }", None
99        ).unwrap();
100        graphics_setup.replace(Some(Graphics {
101            screen_indices, screen_vertices, screen_uvs,
102            teapot_indices, teapot_vertices, teapot_uvs, teapot_normals,
103            program, normal, fxaa
104        }));
105    }).with_update(|input, display, _, _, _| {
106        let graphics = graphics.borrow();
107        let Graphics {
108            screen_indices, screen_vertices, screen_uvs,
109            teapot_indices, teapot_vertices, teapot_uvs, teapot_normals,
110            program, normal, fxaa
111        } = graphics.as_ref().unwrap();
112        let teapot_mesh = (teapot_vertices, teapot_normals, teapot_uvs);
113        let screen_mesh = (screen_vertices, screen_uvs);
114
115        let delta_time = frame_start.elapsed().as_secs_f32();
116        frame_start = Instant::now();
117
118        // using a small resolution to better show the effect of fxaa.
119        let size = (380, 216);
120        display.resize(size);
121        depth.resize_to_display(&display);
122        colour.resize_to_display(&display);
123
124        // press f to toggle FXAA
125        if input.pressed(FXAA) { fxaa_on = !fxaa_on }
126
127        let colour = colour.texture();
128        let depth = depth.texture();
129        let mut frame = SimpleFrameBuffer::with_depth_buffer(
130            display, colour, depth
131        ).unwrap();
132
133        let view = Mat4::view_matrix_3d(size, 1.0, 1024.0, 0.1);
134
135        // set camera rotation
136        let look_move = input.dir(LookLeft, LookRight, LookUp, LookDown);
137        rot += look_move.scale(delta_time * 20.0);
138        rot.y = rot.y.clamp(-PI / 2.0, PI / 2.0);
139        let rx = Quat::from_y_rot(rot.x);
140        let ry = Quat::from_x_rot(rot.y);
141        let rot = rx * ry;
142
143        // move player based on view
144        let dir = input.dir_max_len_1(Right, Left, Forward, Back);
145        let move_dir = vec3(dir.x, 0.0, dir.y).scale(5.0*delta_time);
146        pos += move_dir.transform(&Mat3::from_rot(rx));
147
148        frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
149        // draw teapot
150        frame.draw(
151            teapot_mesh, teapot_indices,
152            program, &uniform! {
153                view: view,
154                model: Mat4::from_scale(Vec3::splat(0.1)),
155                camera: Mat4::from_inverse_transform(pos, Vec3::ONE, rot),
156                light:   vec3(0.1, 0.25, -1.0).normalise(),
157                albedo:  vec3(0.5, 0.1,   0.4),
158                ambient: vec3(0.0, 0.05,  0.1),
159                shine: 50.0f32,
160            },
161            &draw_parameters,
162        ).unwrap();
163
164        let mut frame = display.draw();
165        frame.draw(
166            screen_mesh, screen_indices, if fxaa_on { fxaa } else { normal },
167            &shaders::fxaa_uniforms(colour), &DrawParameters::default()
168        ).unwrap();
169        frame.finish().unwrap();
170    }).build(event_loop).unwrap();
171}
Source

pub fn new(size: (u32, u32), display: &Display) -> Self

Source

pub fn resize_to_display(&mut self, display: &Display)

Examples found in repository?
examples/simple-fxaa.rs (line 121)
13fn main() {
14    use Action::*;
15    let event_loop = EventLoop::new().unwrap();
16    event_loop.set_control_flow(ControlFlow::Poll);
17
18    let mut colour = ResizableTexture2d::default();
19    let mut depth = ResizableDepthTexture2d::default();
20
21    let input = { use base_input_codes::*; input_map!(
22        (Left,    ArrowLeft,  KeyA, LeftStickLeft ),
23        (Right,   ArrowRight, KeyD, LeftStickRight),
24        (Forward, ArrowUp,    KeyW, LeftStickUp   ),
25        (Back,    ArrowDown,  KeyS, LeftStickDown ),
26        (LookRight, MouseMoveRight, RightStickRight),
27        (LookLeft,  MouseMoveLeft,  RightStickLeft ),
28        (LookUp,    MouseMoveUp,    RightStickUp   ),
29        (LookDown,  MouseMoveDown,  RightStickDown ),
30        (FXAA,      KeyF,       GamepadInput::North)
31    ) };
32    struct Graphics {
33        screen_indices: IndexBuffer<u32>,
34        screen_vertices: VertexBuffer<Vertex>,
35        screen_uvs: VertexBuffer<TextureCoords>,
36
37        teapot_indices: IndexBuffer<u16>,
38        teapot_vertices: VertexBuffer<Vertex>,
39        teapot_uvs: VertexBuffer<TextureCoords>,
40        teapot_normals: VertexBuffer<Normal>,
41
42        fxaa: Program, normal: Program, program: Program
43    }
44    let graphics: Rc<RefCell<Option<Graphics>>> = Rc::default();
45    let graphics_setup = graphics.clone();
46
47    let draw_parameters = DrawParameters {
48        backface_culling: draw_parameters::BackfaceCullingMode::CullClockwise,
49        ..params::alias_3d()
50    };
51    let mut fxaa_on = true;
52    
53    let mut pos = vec3(0.0, 0.0, -30.0);
54    let mut rot = vec2(0.0, 0.0);
55    
56    let mut frame_start = Instant::now();
57
58    thin_engine::builder(input).with_setup(|display, window, _| {
59        window.set_title("FXAA Test");
60        let _ = window.set_cursor_grab(CursorGrabMode::Confined);
61        let _ = window.set_cursor_grab(CursorGrabMode::Locked);
62        window.set_cursor_visible(false);
63
64        let (screen_indices, screen_vertices, screen_uvs) = mesh!(
65            display, &screen::INDICES, &screen::VERTICES, &screen::UVS
66        );
67        let (teapot_indices, teapot_vertices, teapot_uvs, teapot_normals) = mesh!(
68            display, &teapot::INDICES, &teapot::VERTICES, &[] as &[TextureCoords; 0], &teapot::NORMALS
69        );
70
71        let program = Program::from_source(
72            display, shaders::VERTEX,
73            "#version 140
74            out vec4 colour;
75            in vec3 v_normal;
76            uniform vec3 light;
77            uniform mat4 camera;
78            uniform vec3 ambient;
79            uniform vec3 albedo;
80            uniform float shine;
81            void main() {
82                vec3 camera_dir = inverse(mat3(camera)) * vec3(0, 0, -1);
83                vec3 half_dir = normalize(camera_dir + light);
84                float specular = pow(max(dot(half_dir, v_normal), 0.0), shine);
85                float light_level = max(dot(light, v_normal), 0.0);
86                colour = vec4(albedo * light_level + ambient + vec3(specular), 1.0);
87            }", None
88        ).unwrap();
89        let fxaa = shaders::fxaa_shader(display).unwrap();
90        let normal = Program::from_source(
91            display, shaders::SCREEN_VERTEX, 
92            "#version 140
93            in vec2 uv;
94            uniform sampler2D tex;
95            out vec4 colour;
96            void main() {
97                colour = texture(tex, uv);
98            }", None
99        ).unwrap();
100        graphics_setup.replace(Some(Graphics {
101            screen_indices, screen_vertices, screen_uvs,
102            teapot_indices, teapot_vertices, teapot_uvs, teapot_normals,
103            program, normal, fxaa
104        }));
105    }).with_update(|input, display, _, _, _| {
106        let graphics = graphics.borrow();
107        let Graphics {
108            screen_indices, screen_vertices, screen_uvs,
109            teapot_indices, teapot_vertices, teapot_uvs, teapot_normals,
110            program, normal, fxaa
111        } = graphics.as_ref().unwrap();
112        let teapot_mesh = (teapot_vertices, teapot_normals, teapot_uvs);
113        let screen_mesh = (screen_vertices, screen_uvs);
114
115        let delta_time = frame_start.elapsed().as_secs_f32();
116        frame_start = Instant::now();
117
118        // using a small resolution to better show the effect of fxaa.
119        let size = (380, 216);
120        display.resize(size);
121        depth.resize_to_display(&display);
122        colour.resize_to_display(&display);
123
124        // press f to toggle FXAA
125        if input.pressed(FXAA) { fxaa_on = !fxaa_on }
126
127        let colour = colour.texture();
128        let depth = depth.texture();
129        let mut frame = SimpleFrameBuffer::with_depth_buffer(
130            display, colour, depth
131        ).unwrap();
132
133        let view = Mat4::view_matrix_3d(size, 1.0, 1024.0, 0.1);
134
135        // set camera rotation
136        let look_move = input.dir(LookLeft, LookRight, LookUp, LookDown);
137        rot += look_move.scale(delta_time * 20.0);
138        rot.y = rot.y.clamp(-PI / 2.0, PI / 2.0);
139        let rx = Quat::from_y_rot(rot.x);
140        let ry = Quat::from_x_rot(rot.y);
141        let rot = rx * ry;
142
143        // move player based on view
144        let dir = input.dir_max_len_1(Right, Left, Forward, Back);
145        let move_dir = vec3(dir.x, 0.0, dir.y).scale(5.0*delta_time);
146        pos += move_dir.transform(&Mat3::from_rot(rx));
147
148        frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
149        // draw teapot
150        frame.draw(
151            teapot_mesh, teapot_indices,
152            program, &uniform! {
153                view: view,
154                model: Mat4::from_scale(Vec3::splat(0.1)),
155                camera: Mat4::from_inverse_transform(pos, Vec3::ONE, rot),
156                light:   vec3(0.1, 0.25, -1.0).normalise(),
157                albedo:  vec3(0.5, 0.1,   0.4),
158                ambient: vec3(0.0, 0.05,  0.1),
159                shine: 50.0f32,
160            },
161            &draw_parameters,
162        ).unwrap();
163
164        let mut frame = display.draw();
165        frame.draw(
166            screen_mesh, screen_indices, if fxaa_on { fxaa } else { normal },
167            &shaders::fxaa_uniforms(colour), &DrawParameters::default()
168        ).unwrap();
169        frame.finish().unwrap();
170    }).build(event_loop).unwrap();
171}

Trait Implementations§

Source§

impl Default for ResizableDepthTexture2d

Source§

fn default() -> ResizableDepthTexture2d

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

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>

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)

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)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more