-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathquery_texture_texels.rs
167 lines (152 loc) · 4.31 KB
/
query_texture_texels.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
//! This program shows how to render two simple triangles, query the texels from the rendered
//! framebuffer and output them in a texture.
//!
//! This example is requires a file system to run as it will write the output to it.
//!
//! <https://docs.rs/luminance>
use crate::{
shared::{FragSlot, Vertex},
Example, LoopFeedback, PlatformServices,
};
use image::{save_buffer, ColorType};
use luminance::{
backend::{Backend, Error},
context::Context,
dim::{Dim2, Size2},
framebuffer::Framebuffer,
pipeline::PipelineState,
primitive::Triangle,
render_state::RenderState,
shader::{Program, ProgramBuilder},
texture::{Mipmaps, TextureSampling},
vertex_entity::{VertexEntity, VertexEntityBuilder, View},
vertex_storage::{Interleaved, Interleaving},
};
use mint::{Vector2, Vector3};
// 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");
// The vertices. We define two triangles.
const TRI_VERTICES: [Vertex; 6] = [
// first triangle – an RGB one
Vertex {
co: Vector2 { x: 0.5, y: -0.5 },
color: Vector3 {
x: 0.,
y: 1.,
z: 0.,
},
},
Vertex {
co: Vector2 { x: 0.0, y: 0.5 },
color: Vector3 {
x: 0.,
y: 0.,
z: 1.,
},
},
Vertex {
co: Vector2 { x: -0.5, y: -0.5 },
color: Vector3 {
x: 1.,
y: 0.,
z: 0.,
},
},
// second triangle, a purple one, positioned differently
Vertex {
co: Vector2 { x: -0.5, y: 0.5 },
color: Vector3 {
x: 1.,
y: 0.2,
z: 1.,
},
},
Vertex {
co: Vector2 { x: 0.0, y: -0.5 },
color: Vector3 {
x: 0.2,
y: 1.,
z: 1.,
},
},
Vertex {
co: Vector2 { x: 0.5, y: 0.5 },
color: Vector3 {
x: 0.2,
y: 0.2,
z: 1.,
},
},
];
pub struct LocalExample {
program: Program<Vertex, (), Triangle, FragSlot, ()>,
triangles: VertexEntity<Vertex, Triangle, Interleaving>,
framebuffer: Framebuffer<Dim2, FragSlot, ()>,
}
impl Example for LocalExample {
type Err = Error;
const TITLE: &'static str = "Query Texture Texels";
fn bootstrap(
[width, height]: [u32; 2],
_: &mut impl PlatformServices,
ctx: &mut Context<impl Backend>,
) -> Result<Self, Self::Err> {
// we need a program to “shade” our triangles and to tell luminance which is the input vertex
// type, and we’re not interested in the other two type variables for this sample
let program = ctx.new_program(
ProgramBuilder::new()
.add_vertex_stage(VS)
.no_primitive_stage()
.add_shading_stage(FS),
)?;
// create tessellation for direct geometry; that is, tessellation that will render vertices by
// taking one after another in the provided slice
let triangles = ctx.new_vertex_entity(
VertexEntityBuilder::new().add_vertices(Interleaved::new().set_vertices(TRI_VERTICES)),
)?;
// the back buffer, which we will make our render into (we make it mutable so that we can change
// it whenever the window dimensions change)
let framebuffer = ctx.new_framebuffer(
Size2::new(width, height),
Mipmaps::No,
&TextureSampling::default(),
)?;
Ok(Self {
program,
triangles,
framebuffer,
})
}
fn render_frame(
mut self,
_: f32,
_: impl Iterator<Item = crate::InputAction>,
ctx: &mut Context<impl Backend>,
) -> Result<LoopFeedback<Self>, Self::Err> {
let program = &mut self.program;
let triangles = &self.triangles;
// create a new dynamic pipeline that will render to the back buffer and must clear it with
// pitch black prior to do any render to it
ctx.with_framebuffer(&self.framebuffer, &PipelineState::default(), |mut frame| {
frame.with_program(program, |mut frame| {
frame.with_render_state(&RenderState::default(), |mut frame| {
frame.render_vertex_entity(triangles.view(..))
})
})
})?;
// the backbuffer contains our texels
let texels = ctx.read_texture(&self.framebuffer.layers().frag)?;
let size = self.framebuffer.layers().frag.size();
// create a .png file and output it
save_buffer(
"./rendered.png",
&texels[..],
size.width,
size.height,
ColorType::Rgb8,
)
.unwrap();
Ok(LoopFeedback::Exit)
}
}