-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathhello_world.rs
190 lines (170 loc) · 5.42 KB
/
hello_world.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
//! This program shows how to render two simple triangles and is the hello world of luminance.
//!
//! <https://docs.rs/luminance>
use crate::{Example, InputAction, LoopFeedback, PlatformServices};
use luminance::{
backend::{Backend, Error},
context::Context,
dim::{Dim2, Size2},
framebuffer::{Back, Framebuffer},
namespace,
pipeline::PipelineState,
pixel::RGB32F,
primitive::Triangle,
render_state::RenderState,
shader::{Program, ProgramBuilder},
vertex_entity::{VertexEntity, VertexEntityBuilder, View},
vertex_storage::{Interleaved, Interleaving},
RenderSlots, Vertex,
};
// We get the shader at compile time from local files
const VS: &'static str = include_str!("simple-vs.glsl");
const FS: &'static str = include_str!("simple-fs.glsl");
// Vertex namespace.
//
// A namespace is tag-like type that is used to spawn named indices, allowing to uniquely identify various piece of
// protocol information, such as positions, normals, colors, etc. Theoretically, namespaces and named indices can be
// used for anything and everything.
namespace! {
VertexNamespace = { "pos", "rgb" }
}
// Our vertex type.
//
// We derive the Vertex trait automatically and map the type to the namespace, so that a mapping can be done between the
// namespace names and the vertex fields.
//
// Also, currently, we need to use #[repr(C))] to ensure Rust is not going to move struct’s fields around.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Vertex)]
#[vertex(namespace = "VertexNamespace")]
struct Vertex {
pos: mint::Vector2<f32>,
// Here, we can use the special normalized = <bool> construct to state whether we want integral
// vertex attributes to be available as normalized floats in the shaders, when fetching them from
// the vertex buffers. If you set it to "false" or ignore it, you will get non-normalized integer
// values (i.e. value ranging from 0 to 255 for u8, for instance).
#[vertex(normalized = "true")]
rgb: mint::Vector3<u8>,
}
impl Vertex {
const fn new(pos: mint::Vector2<f32>, rgb: mint::Vector3<u8>) -> Self {
Self { pos, rgb }
}
}
// The vertices. We define two triangles.
const TRI_VERTICES: [Vertex; 6] = [
// First triangle – an RGB one.
Vertex::new(
mint::Vector2 { x: 0.5, y: -0.5 },
mint::Vector3 { x: 0, y: 255, z: 0 },
),
Vertex::new(
mint::Vector2 { x: 0.0, y: 0.5 },
mint::Vector3 { x: 0, y: 0, z: 255 },
),
Vertex::new(
mint::Vector2 { x: -0.5, y: -0.5 },
mint::Vector3 { x: 255, y: 0, z: 0 },
),
// Second triangle, a purple one, positioned differently.
Vertex::new(
mint::Vector2 { x: -0.5, y: 0.5 },
mint::Vector3 {
x: 255,
y: 51,
z: 255,
},
),
Vertex::new(
mint::Vector2 { x: 0.0, y: -0.5 },
mint::Vector3 {
x: 51,
y: 255,
z: 255,
},
),
Vertex::new(
mint::Vector2 { x: 0.5, y: 0.5 },
mint::Vector3 {
x: 51,
y: 51,
z: 255,
},
),
];
// Another namespace for render slots (see below).
namespace! {
RenderSlotNamespace = { "frag" }
}
// Render slots.
//
// A render slot represents the channels the end stage of a shader program is going to end up writing to. In our case,
// since we are only interested in rendering the color of each pixel, we will just have one single channel for the
// color.
#[derive(Clone, Copy, Debug, PartialEq, RenderSlots)]
#[slot(namespace = "RenderSlotNamespace")]
pub struct Slots {
frag: RGB32F,
}
/// Local example; this will be picked by the example runner.
pub struct LocalExample {
back_buffer: Framebuffer<Dim2, Back<Slots>, Back<()>>,
// the program will render by mapping our Vertex type as triangles to the color slot, containing a single color
program: Program<Vertex, (), Triangle, Slots, ()>,
triangles: VertexEntity<Vertex, Triangle, Interleaving>,
}
impl Example for LocalExample {
type Err = Error;
const TITLE: &'static str = "Hello, world!";
fn bootstrap(
[width, height]: [u32; 2],
_platform: &mut impl PlatformServices,
context: &mut Context<impl Backend>,
) -> Result<Self, Self::Err> {
// We need a program to “shade” our triangles
let program = context
.new_program(
ProgramBuilder::new()
.add_vertex_stage(VS)
.no_primitive_stage()
.add_shading_stage(FS),
)
.unwrap();
let triangles = context.new_vertex_entity(
VertexEntityBuilder::new().add_vertices(Interleaved::new().set_vertices(TRI_VERTICES)),
)?;
let back_buffer = context.back_buffer(Size2::new(width, height))?;
Ok(Self {
back_buffer,
program,
triangles,
})
}
fn render_frame(
self,
_time_ms: f32,
actions: impl Iterator<Item = InputAction>,
context: &mut Context<impl Backend>,
) -> Result<LoopFeedback<Self>, Self::Err> {
for action in actions {
match action {
InputAction::Quit => return Ok(LoopFeedback::Exit),
_ => (),
}
}
context.with_framebuffer(
&self.back_buffer,
&PipelineState::default(),
|mut with_framebuffer| {
with_framebuffer.with_program(&self.program, |mut with_program| {
with_program.with_render_state(&RenderState::default(), |mut with_render_state| {
with_render_state.render_vertex_entity(self.triangles.view(..))
})
})
},
)?;
// Finally, swap the backbuffer with the frontbuffer in order to render our triangles onto your
// screen.
Ok(LoopFeedback::Continue(self))
}
}