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
use crate::camera::*;
use crate::core::*;
use crate::definition::*;
use crate::math::*;
pub struct Skybox<T: TextureValueType> {
program: Program,
vertex_buffer: VertexBuffer,
texture: TextureCubeMap<T>,
}
impl<T: TextureValueType> Skybox<T> {
pub fn new(context: &Context, cpu_texture: &mut CPUTexture<T>) -> Result<Skybox<T>, Error> {
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<T>,
) -> Result<Skybox<T>, Error> {
let program = Program::from_source(
context,
include_str!("shaders/skybox.vert"),
include_str!("shaders/skybox.frag"),
)?;
let vertex_buffer = VertexBuffer::new_with_static_f32(context, &get_positions())?;
Ok(Skybox {
program,
vertex_buffer,
texture,
})
}
pub fn render(&self, viewport: Viewport, camera: &Camera) -> Result<(), Error> {
let render_states = RenderStates {
depth_test: DepthTestType::LessOrEqual,
..Default::default()
};
self.program.use_texture_cube(&self.texture, "texture0")?;
self.program
.use_uniform_block(camera.uniform_buffer(), "Camera");
self.program
.use_attribute_vec3(&self.vertex_buffer, "position")?;
self.program
.draw_arrays(render_states, CullType::Front, viewport, 36);
Ok(())
}
pub fn get_texture(&self) -> &TextureCubeMap<T> {
&self.texture
}
}
fn get_positions() -> Vec<f32> {
vec![
1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0,
-1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0,
-1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0,
1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
-1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0,
-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, -1.0,
]
}