-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathrenderer_stencil.rs
173 lines (142 loc) · 3.9 KB
/
renderer_stencil.rs
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use notan::prelude::*;
const VERT: ShaderSource = notan::vertex_shader! {
r#"
#version 450
layout(location = 0) in vec2 a_pos;
layout(location = 1) in vec3 a_color;
layout(location = 0) out vec3 v_color;
void main() {
v_color = a_color;
gl_Position = vec4(a_pos - 0.5, 0.0, 1.0);
}
"#
};
const FRAG: ShaderSource = notan::fragment_shader! {
r#"
#version 450
precision mediump float;
layout(location = 0) in vec3 v_color;
layout(location = 0) out vec4 color;
void main() {
color = vec4(v_color, 1.0);
}
"#
};
#[derive(AppState)]
struct State {
clear_options: ClearOptions,
mask_pipeline: Pipeline,
pipeline: Pipeline,
mask_vbo: Buffer,
vbo: Buffer,
}
#[notan_main]
fn main() -> Result<(), String> {
notan::init_with(setup).draw(draw).build()
}
fn setup(gfx: &mut Graphics) -> State {
let clear_options = ClearOptions {
color: Some(Color::new(0.1, 0.2, 0.3, 1.0)),
depth: None,
stencil: Some(0),
};
let vertex_info = VertexInfo::new()
.attr(0, VertexFormat::Float32x2)
.attr(1, VertexFormat::Float32x3);
let stencil_maks_opts = StencilOptions {
stencil_fail: StencilAction::Keep,
depth_fail: StencilAction::Keep,
pass: StencilAction::Replace,
compare: CompareMode::Always,
read_mask: 0xff,
write_mask: 0xff,
reference: 1,
};
let mask_pipeline = gfx
.create_pipeline()
.from(&VERT, &FRAG)
.with_vertex_info(&vertex_info)
.with_stencil(stencil_maks_opts)
.build()
.unwrap();
let stencil_opts = StencilOptions {
stencil_fail: StencilAction::Keep,
depth_fail: StencilAction::Keep,
pass: StencilAction::Keep,
compare: CompareMode::Equal,
read_mask: 0xff,
write_mask: 0x00,
reference: 1,
};
let pipeline = gfx
.create_pipeline()
.from(&VERT, &FRAG)
.with_vertex_info(&vertex_info)
.with_stencil(stencil_opts)
.build()
.unwrap();
// masking vertices
#[rustfmt::skip]
let mask_vertices = [
0.5, 1.35, 1.0, 1.0, 1.0,
0.25, 0.85, 1.0, 1.0, 1.0,
0.75, 0.85, 1.0, 1.0, 1.0,
0.75, 0.85, 1.0, 1.0, 1.0,
0.5, 0.35, 1.0, 1.0, 1.0,
1.0, 0.35, 1.0, 1.0, 1.0,
0.25, 0.85, 1.0, 1.0, 1.0,
0.0, 0.35, 1.0, 1.0, 1.0,
0.5, 0.35, 1.0, 1.0, 1.0,
0.5, 0.35, 1.0, 1.0, 1.0,
0.25, -0.15, 1.0, 1.0, 1.0,
0.75, -0.15, 1.0, 1.0, 1.0,
1.0, 0.35, 1.0, 1.0, 1.0,
0.75, -0.15, 1.0, 1.0, 1.0,
1.25, -0.15, 1.0, 1.0, 1.0,
0.0, 0.35, 1.0, 1.0, 1.0,
-0.25, -0.15, 1.0, 1.0, 1.0,
0.25, -0.15, 1.0, 1.0, 1.0,
];
let mask_vbo = gfx
.create_vertex_buffer()
.with_info(&vertex_info)
.with_data(&mask_vertices)
.build()
.unwrap();
// triangle
#[rustfmt::skip]
let vertices = [
0.5, 1.0, 1.0, 0.2, 0.3,
0.0, 0.0, 0.1, 1.0, 0.3,
1.0, 0.0, 0.1, 0.2, 1.0,
];
let vbo = gfx
.create_vertex_buffer()
.with_info(&vertex_info)
.with_data(&vertices)
.build()
.unwrap();
State {
clear_options,
mask_pipeline: mask_pipeline,
pipeline: pipeline,
mask_vbo: mask_vbo,
vbo: vbo,
}
}
fn draw(gfx: &mut Graphics, state: &mut State) {
let mut renderer = gfx.create_renderer();
renderer.begin(Some(state.clear_options));
// Render the mask
renderer.set_pipeline(&state.mask_pipeline);
renderer.bind_buffer(&state.mask_vbo);
renderer.draw(0, 18);
renderer.end();
// render the triangle
renderer.begin(None);
renderer.set_pipeline(&state.pipeline);
renderer.bind_buffer(&state.vbo);
renderer.draw(0, 3);
renderer.end();
gfx.render(&renderer);
}