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
use crate::core::*;
pub struct Skybox {
program: Program,
vertex_buffer: VertexBuffer,
texture: TextureCubeMap,
}
impl Skybox {
pub fn new<T: TextureDataType>(
context: &Context,
cpu_texture: &mut CPUTexture<T>,
) -> ThreeDResult<Skybox> {
cpu_texture.wrap_t = Wrapping::ClampToEdge;
cpu_texture.wrap_s = Wrapping::ClampToEdge;
cpu_texture.wrap_r = Wrapping::ClampToEdge;
cpu_texture.mip_map_filter = None;
let texture = TextureCubeMap::new(&context, cpu_texture)?;
Self::new_with_texture(context, texture)
}
pub fn new_with_texture(context: &Context, texture: TextureCubeMap) -> ThreeDResult<Skybox> {
let program = Program::from_source(
context,
include_str!("shaders/skybox.vert"),
&format!(
"{}{}",
include_str!("../../core/shared.frag"),
include_str!("shaders/skybox.frag")
),
)?;
let vertex_buffer = VertexBuffer::new_with_static(context, &CPUMesh::cube().positions)?;
Ok(Skybox {
program,
vertex_buffer,
texture,
})
}
pub fn texture(&self) -> &TextureCubeMap {
&self.texture
}
pub fn render(&self, camera: &Camera) -> ThreeDResult<()> {
let render_states = RenderStates {
depth_test: DepthTest::LessOrEqual,
cull: Cull::Front,
..Default::default()
};
self.program.use_texture_cube("texture0", &self.texture)?;
self.program
.use_uniform_block("Camera", camera.uniform_buffer());
self.program
.use_attribute_vec3("position", &self.vertex_buffer)?;
self.program
.draw_arrays(render_states, camera.viewport(), 36);
Ok(())
}
}