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
use crate::math::*;
use crate::definition::*;
use crate::core::*;
use crate::camera::*;
use crate::object::*;
use crate::phong::*;
pub struct PhongDeferredMesh {
context: Context,
pub name: String,
mesh: Mesh,
pub material: PhongMaterial
}
impl PhongDeferredMesh {
pub fn new(context: &Context, cpu_mesh: &CPUMesh, material: &PhongMaterial) -> Result<Self, Error>
{
let mesh = Mesh::new(context, cpu_mesh)?;
unsafe {MESH_COUNT += 1;}
Ok(Self {
context: context.clone(),
name: cpu_mesh.name.clone(),
mesh,
material: material.clone()
})
}
pub fn render_geometry(&self, render_states: RenderStates, viewport: Viewport, transformation: &Mat4, camera: &Camera) -> Result<(), Error>
{
let program = match self.material.color_source {
ColorSource::Color(_) => {
unsafe {
if PROGRAM_COLOR.is_none()
{
PROGRAM_COLOR = Some(MeshProgram::new(&self.context, &format!("{}\n{}",
include_str!("shaders/deferred_objects_shared.frag"),
include_str!("shaders/colored_deferred.frag")))?);
}
PROGRAM_COLOR.as_ref().unwrap()
}
},
ColorSource::Texture(_) => {
unsafe {
if PROGRAM_TEXTURE.is_none()
{
PROGRAM_TEXTURE = Some(MeshProgram::new(&self.context, &format!("{}\n{}",
include_str!("shaders/deferred_objects_shared.frag"),
include_str!("shaders/textured_deferred.frag")))?);
}
PROGRAM_TEXTURE.as_ref().unwrap()
}
}
};
self.material.bind(program)?;
self.mesh.render(program, render_states, viewport, transformation, camera)
}
}
impl std::ops::Deref for PhongDeferredMesh {
type Target = Mesh;
fn deref(&self) -> &Mesh {
&self.mesh
}
}
impl Drop for PhongDeferredMesh {
fn drop(&mut self) {
unsafe {
MESH_COUNT -= 1;
if MESH_COUNT == 0 {
PROGRAM_COLOR = None;
PROGRAM_TEXTURE = None;
}
}
}
}
static mut PROGRAM_COLOR: Option<MeshProgram> = None;
static mut PROGRAM_TEXTURE: Option<MeshProgram> = None;
static mut MESH_COUNT: u32 = 0;