-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathrenderer_uniform_std140.rs
273 lines (231 loc) · 7.05 KB
/
renderer_uniform_std140.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
use notan::math::{vec3, Mat4, Vec3};
use notan::prelude::*;
// language=glsl
const LIGHT_VERTEX_SHADER: ShaderSource = notan::vertex_shader! {
r#"
#version 450
layout (location = 0) in vec3 a_pos;
layout (location = 1) in vec3 a_normal;
layout (location = 0) out vec3 v_pos;
layout (location = 1) out vec3 v_normal;
layout(set = 0, binding = 0) uniform Transform {
mat4 model;
mat4 view;
mat4 projection;
};
void main()
{
v_pos = vec3(model * vec4(a_pos, 1.0));
v_normal = a_normal;
gl_Position = projection * view * vec4(v_pos, 1.0);
}
"#
};
// language=glsl
const LIGHT_FRAGMENT_SHADER: ShaderSource = notan::fragment_shader! {
r#"
#version 450
layout(location = 0) in vec3 v_normal;
layout(location = 1) in vec3 v_pos;
layout(location = 0) out vec4 color;
layout(set = 0, binding = 1) uniform Light {
vec3 lightPos;
vec3 lightColor;
vec3 objectColor;
};
void main()
{
// ambient
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * lightColor;
// diffuse
vec3 norm = normalize(v_normal);
vec3 lightDir = normalize(lightPos - v_pos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
vec3 result = (ambient + diffuse) * objectColor;
color = vec4(result, 1.0);
}
"#
};
/*
Marking this struct with the `uniform` macro allows
to use the struct as a buffer data using the right
layout (std140) automatically, though this struct
uses 3 Mat4 which is already using the right layout
*/
#[uniform]
#[derive(Copy, Clone)]
struct Transform {
model: Mat4,
view: Mat4,
projection: Mat4,
}
impl Default for Transform {
fn default() -> Self {
Self {
model: Mat4::IDENTITY,
view: Mat4::IDENTITY,
projection: Mat4::perspective_rh_gl(20.0_f32.to_radians(), 800.0 / 600.0, 0.1, 100.0),
}
}
}
impl Transform {
fn with_view(time: f32) -> Transform {
let radius = 10.0;
let cam_x = time.sin() * radius;
let cam_z = time.cos() * radius;
Transform {
view: Mat4::look_at_rh(
vec3(cam_x, cam_z * 0.5, cam_z),
vec3(0.0, 0.0, 0.0),
vec3(0.0, 1.0, 0.0),
),
..Default::default()
}
}
}
/*
This struct doesn't fit the sdt140 layout
because a Vec3 uses 12 bytes instead of 16,
using `uniform` macro will adds the necessary
padding making it works as it is
*/
#[uniform]
#[derive(Copy, Clone)]
struct Light {
light_pos: Vec3,
light_color: Vec3,
object_color: Vec3,
}
impl Default for Light {
fn default() -> Self {
Self {
light_pos: vec3(1.2, 1.0, 2.0),
light_color: vec3(1.0, 1.0, 1.0),
object_color: vec3(1.0, 0.5, 0.31),
}
}
}
impl Light {
fn with_time(time: f32) -> Self {
Self {
light_pos: vec3(1.2 + time.sin() * 2.0, 1.0 + time.cos() * 2.0, 2.0),
..Default::default()
}
}
}
#[derive(AppState)]
struct State {
pipeline: Pipeline,
vbo: Buffer,
transform_ubo: Buffer,
light_ubo: Buffer,
}
#[notan_main]
fn main() -> Result<(), String> {
notan::init_with(setup).draw(draw).build()
}
fn setup(gfx: &mut Graphics) -> State {
// Declare the vertex attributes
let vertex_info = VertexInfo::new()
.attr(0, VertexFormat::Float32x3) // positions
.attr(1, VertexFormat::Float32x3); // normals
// Enable depth test
let depth_test = DepthStencil {
write: true,
compare: CompareMode::Less,
};
// build the pipeline
let pipeline = gfx
.create_pipeline()
.from(&LIGHT_VERTEX_SHADER, &LIGHT_FRAGMENT_SHADER)
.with_vertex_info(&vertex_info)
.with_depth_stencil(depth_test)
.build()
.unwrap();
// define vertex data
#[rustfmt::skip]
let vertices = [
// pos // normals
-0.5, -0.5, -0.5, 0.0, 0.0, -1.0,
0.5, -0.5, -0.5, 0.0, 0.0, -1.0,
0.5, 0.5, -0.5, 0.0, 0.0, -1.0,
0.5, 0.5, -0.5, 0.0, 0.0, -1.0,
-0.5, 0.5, -0.5, 0.0, 0.0, -1.0,
-0.5, -0.5, -0.5, 0.0, 0.0, -1.0,
-0.5, -0.5, 0.5, 0.0, 0.0, 1.0,
0.5, -0.5, 0.5, 0.0, 0.0, 1.0,
0.5, 0.5, 0.5, 0.0, 0.0, 1.0,
0.5, 0.5, 0.5, 0.0, 0.0, 1.0,
-0.5, 0.5, 0.5, 0.0, 0.0, 1.0,
-0.5, -0.5, 0.5, 0.0, 0.0, 1.0,
-0.5, 0.5, 0.5, -1.0, 0.0, 0.0,
-0.5, 0.5, -0.5, -1.0, 0.0, 0.0,
-0.5, -0.5, -0.5, -1.0, 0.0, 0.0,
-0.5, -0.5, -0.5, -1.0, 0.0, 0.0,
-0.5, -0.5, 0.5, -1.0, 0.0, 0.0,
-0.5, 0.5, 0.5, -1.0, 0.0, 0.0,
0.5, 0.5, 0.5, 1.0, 0.0, 0.0,
0.5, 0.5, -0.5, 1.0, 0.0, 0.0,
0.5, -0.5, -0.5, 1.0, 0.0, 0.0,
0.5, -0.5, -0.5, 1.0, 0.0, 0.0,
0.5, -0.5, 0.5, 1.0, 0.0, 0.0,
0.5, 0.5, 0.5, 1.0, 0.0, 0.0,
-0.5, -0.5, -0.5, 0.0, -1.0, 0.0,
0.5, -0.5, -0.5, 0.0, -1.0, 0.0,
0.5, -0.5, 0.5, 0.0, -1.0, 0.0,
0.5, -0.5, 0.5, 0.0, -1.0, 0.0,
-0.5, -0.5, 0.5, 0.0, -1.0, 0.0,
-0.5, -0.5, -0.5, 0.0, -1.0, 0.0,
-0.5, 0.5, -0.5, 0.0, 1.0, 0.0,
0.5, 0.5, -0.5, 0.0, 1.0, 0.0,
0.5, 0.5, 0.5, 0.0, 1.0, 0.0,
0.5, 0.5, 0.5, 0.0, 1.0, 0.0,
-0.5, 0.5, 0.5, 0.0, 1.0, 0.0,
-0.5, 0.5, -0.5, 0.0, 1.0, 0.0,
];
// create the vertex buffer object
let vbo = gfx
.create_vertex_buffer()
.with_data(&vertices)
.with_info(&vertex_info)
.build()
.unwrap();
let transform = Transform::with_view(0.0);
// create the uniform buffer object
let transform_ubo = gfx
.create_uniform_buffer(0, "Transform")
.with_data(&transform) // upload the transform to the gpu directly
.build()
.unwrap();
let light = Light::with_time(0.0);
let light_ubo = gfx
.create_uniform_buffer(1, "Light")
.with_data(&light) // upload the light object to thr gpu directly
.build()
.unwrap();
State {
pipeline,
vbo,
transform_ubo,
light_ubo,
}
}
fn draw(app: &mut App, gfx: &mut Graphics, state: &mut State) {
let time = app.timer.elapsed_f32();
gfx.set_buffer_data(&state.transform_ubo, &Transform::with_view(time));
gfx.set_buffer_data(&state.light_ubo, &Light::with_time(time));
let mut renderer = gfx.create_renderer();
let clear = ClearOptions {
color: Some(Color::from_rgb(0.1, 0.2, 0.3)),
depth: Some(1.0),
stencil: None,
};
renderer.begin(Some(clear));
renderer.set_pipeline(&state.pipeline);
renderer.bind_buffers(&[&state.vbo, &state.transform_ubo, &state.light_ubo]);
renderer.draw(0, 36);
renderer.end();
gfx.render(&renderer);
}