summaryrefslogtreecommitdiff
path: root/src/shadow.rs
blob: f82796780a5421a27405e61e51caa3419e31054a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
use glow::HasContext;

use crate::context;

pub struct ShadowBuffer {
    pub fbo: glow::Framebuffer,
    pub depth_tex: glow::Texture,
    pub width: i32,
    pub height: i32,
}

impl ShadowBuffer {
    pub fn new(ctx: &context::Context, w: i32, h: i32) -> Self {
        unsafe {
            // generate and bind FBO
            let fbo = ctx.gl.create_framebuffer().expect("failed to create framebuffer");
            ctx.gl.bind_framebuffer(glow::FRAMEBUFFER, Some(fbo));

            // generate and attach depth buffer
            let depth_tex = ctx.gl.create_texture().expect("failed to create texture");
            ctx.gl.bind_texture(glow::TEXTURE_2D, Some(depth_tex));
            ctx.gl.tex_image_2d(
                glow::TEXTURE_2D,
                0,
                glow::DEPTH_COMPONENT as i32,
                w,
                h,
                0,
                glow::DEPTH_COMPONENT,
                glow::FLOAT,
                None,
            );
            ctx.gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_MIN_FILTER, glow::LINEAR as i32);
            ctx.gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_MAG_FILTER, glow::LINEAR as i32);
            ctx.gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_WRAP_S, glow::CLAMP_TO_BORDER as i32);
            ctx.gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_WRAP_T, glow::CLAMP_TO_BORDER as i32);
            ctx.gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_COMPARE_MODE, glow::COMPARE_REF_TO_TEXTURE as i32);
            ctx.gl.tex_parameter_f32_slice(glow::TEXTURE_2D, glow::TEXTURE_BORDER_COLOR, &[1.0, 1.0, 1.0, 1.0]);
            ctx.gl.framebuffer_texture_2d(glow::FRAMEBUFFER, glow::DEPTH_ATTACHMENT, glow::TEXTURE_2D, Some(depth_tex), 0);
            ctx.gl.draw_buffer(glow::NONE);
            ctx.gl.read_buffer(glow::NONE);

            let status = ctx.gl.check_framebuffer_status(glow::FRAMEBUFFER);
            if status != glow::FRAMEBUFFER_COMPLETE {
                panic!("error initializing framebuffer: {}", status);
            }
            Self {
                fbo,
                depth_tex,
                width: w,
                height: h,
            }
        }
    }

    pub fn bind(&self, ctx: &context::Context) {
        unsafe {
            ctx.gl.bind_framebuffer(glow::FRAMEBUFFER, Some(self.fbo));
            ctx.gl.viewport(0, 0, self.width as _, self.height as _);
            ctx.gl.clear(glow::DEPTH_BUFFER_BIT);

        }
    }
}

pub struct ShadowBuffer3D {
    pub fbo: glow::Framebuffer,
    pub depth_cubemap: glow::Texture,
    pub width: i32,
    pub height: i32,
}

impl ShadowBuffer3D {
    pub fn new(ctx: &context::Context, w: i32, h: i32) -> Self {
        unsafe {
            // generate and bind FBO
            let fbo = ctx.gl.create_framebuffer().expect("failed to create framebuffer");
            ctx.gl.bind_framebuffer(glow::FRAMEBUFFER, Some(fbo));

            // generate and attach depth buffer
            let depth_cubemap = ctx.gl.create_texture().expect("failed to create texture");
            ctx.gl.bind_texture(glow::TEXTURE_CUBE_MAP, Some(depth_cubemap));
            for i in 0..6 {
                ctx.gl.tex_image_2d(
                    glow::TEXTURE_CUBE_MAP_POSITIVE_X + i,
                    0,
                    glow::DEPTH_COMPONENT as i32,
                    w,
                    h,
                    0,
                    glow::DEPTH_COMPONENT,
                    glow::FLOAT,
                    None,
                );
                ctx.gl.tex_parameter_i32(glow::TEXTURE_CUBE_MAP, glow::TEXTURE_MIN_FILTER, glow::LINEAR as i32);
                ctx.gl.tex_parameter_i32(glow::TEXTURE_CUBE_MAP, glow::TEXTURE_MAG_FILTER, glow::LINEAR as i32);
                ctx.gl.tex_parameter_i32(glow::TEXTURE_CUBE_MAP, glow::TEXTURE_WRAP_S, glow::CLAMP_TO_EDGE as i32);
                ctx.gl.tex_parameter_i32(glow::TEXTURE_CUBE_MAP, glow::TEXTURE_WRAP_T, glow::CLAMP_TO_EDGE as i32);
                ctx.gl.tex_parameter_i32(glow::TEXTURE_CUBE_MAP, glow::TEXTURE_WRAP_R, glow::CLAMP_TO_EDGE as i32);
            }

            ctx.gl.framebuffer_texture(glow::FRAMEBUFFER, glow::DEPTH_ATTACHMENT, Some(depth_cubemap), 0);
            ctx.gl.draw_buffer(glow::NONE);
            ctx.gl.read_buffer(glow::NONE);

            let status = ctx.gl.check_framebuffer_status(glow::FRAMEBUFFER);
            if status != glow::FRAMEBUFFER_COMPLETE {
                panic!("error initializing framebuffer: {}", status);
            }
            Self {
                fbo,
                depth_cubemap,
                width: w,
                height: h,
            }
        }
    }

    pub fn bind(&self, ctx: &context::Context) {
        unsafe {
            ctx.gl.bind_framebuffer(glow::FRAMEBUFFER, Some(self.fbo));
            ctx.gl.viewport(0, 0, self.width as _, self.height as _);
            ctx.gl.clear(glow::DEPTH_BUFFER_BIT);

        }
    }
}