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
use crate::math::*;
use crate::definition::*;
use crate::core::*;
use crate::camera::*;
use crate::object::*;
use crate::light::*;
use crate::phong::*;
pub struct PhongForwardMesh {
context: Context,
pub name: String,
mesh: Mesh,
pub material: PhongMaterial
}
impl PhongForwardMesh
{
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_with_ambient(&self, render_states: RenderStates, viewport: Viewport, transformation: &Mat4, camera: &Camera, ambient_light: &AmbientLight) -> Result<(), Error>
{
let program = match self.material.color_source {
ColorSource::Color(_) => {
unsafe {
if PROGRAM_COLOR_AMBIENT.is_none()
{
PROGRAM_COLOR_AMBIENT = Some(MeshProgram::new(&self.context, include_str!("shaders/colored_forward_ambient.frag"))?);
}
PROGRAM_COLOR_AMBIENT.as_ref().unwrap()
}
},
ColorSource::Texture(_) => {
unsafe {
if PROGRAM_TEXTURE_AMBIENT.is_none()
{
PROGRAM_TEXTURE_AMBIENT = Some(MeshProgram::new(&self.context,include_str!("shaders/textured_forward_ambient.frag"))?);
}
PROGRAM_TEXTURE_AMBIENT.as_ref().unwrap()
}
}
};
program.use_uniform_vec3("ambientColor", &(ambient_light.color * ambient_light.intensity))?;
match self.material.color_source {
ColorSource::Color(ref color) => {
program.use_uniform_vec4("surfaceColor", color)?;
},
ColorSource::Texture(ref texture) => {
program.use_texture(texture.as_ref(),"tex")?;
}
}
self.mesh.render(program, render_states, viewport,transformation, camera)
}
pub fn render_with_ambient_and_directional(&self, render_states: RenderStates, viewport: Viewport, transformation: &Mat4, camera: &Camera, ambient_light: &AmbientLight, directional_light: &DirectionalLight) -> Result<(), Error>
{
let program = match self.material.color_source {
ColorSource::Color(_) => {
unsafe {
if PROGRAM_COLOR_AMBIENT_DIRECTIONAL.is_none()
{
PROGRAM_COLOR_AMBIENT_DIRECTIONAL = Some(MeshProgram::new(&self.context, &format!("{}\n{}",
&include_str!("shaders/light_shared.frag"),
&include_str!("shaders/colored_forward_ambient_directional.frag")))?);
}
PROGRAM_COLOR_AMBIENT_DIRECTIONAL.as_ref().unwrap()
}
},
ColorSource::Texture(_) => {
unsafe {
if PROGRAM_TEXTURE_AMBIENT_DIRECTIONAL.is_none()
{
PROGRAM_TEXTURE_AMBIENT_DIRECTIONAL = Some(MeshProgram::new(&self.context, &format!("{}\n{}",
include_str!("shaders/light_shared.frag"),
include_str!("shaders/textured_forward_ambient_directional.frag")))?)
}
PROGRAM_TEXTURE_AMBIENT_DIRECTIONAL.as_ref().unwrap()
}
}
};
program.use_uniform_vec3("ambientColor", &(ambient_light.color * ambient_light.intensity))?;
program.use_uniform_vec3("eyePosition", &camera.position())?;
program.use_texture(directional_light.shadow_map(), "shadowMap")?;
program.use_uniform_block(directional_light.buffer(), "DirectionalLightUniform");
self.material.bind(program)?;
self.mesh.render(program, render_states, viewport, transformation, camera)
}
}
impl std::ops::Deref for PhongForwardMesh {
type Target = Mesh;
fn deref(&self) -> &Mesh {
&self.mesh
}
}
impl Drop for PhongForwardMesh {
fn drop(&mut self) {
unsafe {
MESH_COUNT -= 1;
if MESH_COUNT == 0 {
PROGRAM_COLOR_AMBIENT = None;
PROGRAM_COLOR_AMBIENT_DIRECTIONAL = None;
PROGRAM_TEXTURE_AMBIENT = None;
PROGRAM_TEXTURE_AMBIENT_DIRECTIONAL = None;
}
}
}
}
static mut PROGRAM_COLOR_AMBIENT: Option<MeshProgram> = None;
static mut PROGRAM_COLOR_AMBIENT_DIRECTIONAL: Option<MeshProgram> = None;
static mut PROGRAM_TEXTURE_AMBIENT: Option<MeshProgram> = None;
static mut PROGRAM_TEXTURE_AMBIENT_DIRECTIONAL: Option<MeshProgram> = None;
static mut MESH_COUNT: u32 = 0;