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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use std::ptr;
use gl;
use gl::types::*;
use na::{Point2, Point3, Vector3, Matrix3, Matrix4, Isometry3};
use resource::Material;
use scene::ObjectData;
use light::Light;
use camera::Camera;
use resource::{Mesh, Shader, ShaderAttribute, ShaderUniform};
#[path = "../error.rs"]
mod error;
pub struct ObjectMaterial {
shader: Shader,
pos: ShaderAttribute<Point3<f32>>,
normal: ShaderAttribute<Vector3<f32>>,
tex_coord: ShaderAttribute<Point2<f32>>,
light: ShaderUniform<Point3<f32>>,
color: ShaderUniform<Point3<f32>>,
transform: ShaderUniform<Matrix4<f32>>,
scale: ShaderUniform<Matrix3<f32>>,
ntransform: ShaderUniform<Matrix3<f32>>,
view: ShaderUniform<Matrix4<f32>>
}
impl ObjectMaterial {
pub fn new() -> ObjectMaterial {
let mut shader = Shader::new_from_str(OBJECT_VERTEX_SRC, OBJECT_FRAGMENT_SRC);
shader.use_program();
ObjectMaterial {
pos: shader.get_attrib("position").unwrap(),
normal: shader.get_attrib("normal").unwrap(),
tex_coord: shader.get_attrib("tex_coord_v").unwrap(),
light: shader.get_uniform("light_position").unwrap(),
color: shader.get_uniform("color").unwrap(),
transform: shader.get_uniform("transform").unwrap(),
scale: shader.get_uniform("scale").unwrap(),
ntransform: shader.get_uniform("ntransform").unwrap(),
view: shader.get_uniform("view").unwrap(),
shader: shader
}
}
fn activate(&mut self) {
self.shader.use_program();
self.pos.enable();
self.normal.enable();
self.tex_coord.enable();
}
fn deactivate(&mut self) {
self.pos.disable();
self.normal.disable();
self.tex_coord.disable();
}
}
impl Material for ObjectMaterial {
fn render(&mut self,
pass: usize,
transform: &Isometry3<f32>,
scale: &Vector3<f32>,
camera: &mut Camera,
light: &Light,
data: &ObjectData,
mesh: &mut Mesh) {
self.activate();
camera.upload(pass, &mut self.view);
let pos = match *light {
Light::Absolute(ref p) => p.clone(),
Light::StickToCamera => camera.eye()
};
self.light.upload(&pos);
let formated_transform = transform.to_homogeneous();
let formated_ntransform = transform.rotation.to_rotation_matrix().unwrap();
let formated_scale = Matrix3::from_diagonal(&Vector3::new(scale.x, scale.y, scale.z));
unsafe {
self.transform.upload(&formated_transform);
self.ntransform.upload(&formated_ntransform);
self.scale.upload(&formated_scale);
self.color.upload(data.color());
mesh.bind(&mut self.pos, &mut self.normal, &mut self.tex_coord);
verify!(gl::ActiveTexture(gl::TEXTURE0));
verify!(gl::BindTexture(gl::TEXTURE_2D, data.texture().id()));
if data.surface_rendering_active() {
if data.backface_culling_enabled() {
verify!(gl::Enable(gl::CULL_FACE));
}
else {
verify!(gl::Disable(gl::CULL_FACE));
}
verify!(gl::PolygonMode(gl::FRONT_AND_BACK, gl::FILL));
verify!(gl::DrawElements(
gl::TRIANGLES,
mesh.num_pts() as GLint,
gl::UNSIGNED_INT,
ptr::null()));
}
if data.lines_width() != 0.0 {
verify!(gl::Disable(gl::CULL_FACE));
verify!(gl::PolygonMode(gl::FRONT_AND_BACK, gl::LINE));
gl::LineWidth(data.lines_width());
verify!(gl::DrawElements(
gl::TRIANGLES,
mesh.num_pts() as GLint,
gl::UNSIGNED_INT,
ptr::null()));
gl::LineWidth(1.0);
}
if data.points_size() != 0.0 {
verify!(gl::Disable(gl::CULL_FACE));
verify!(gl::PolygonMode(gl::FRONT_AND_BACK, gl::POINT));
gl::PointSize(data.points_size());
verify!(gl::DrawElements(
gl::TRIANGLES,
mesh.num_pts() as GLint,
gl::UNSIGNED_INT,
ptr::null()));
gl::PointSize(1.0);
}
}
mesh.unbind();
self.deactivate();
}
}
pub static OBJECT_VERTEX_SRC: &'static str = A_VERY_LONG_STRING;
pub static OBJECT_FRAGMENT_SRC: &'static str = ANOTHER_VERY_LONG_STRING;
const A_VERY_LONG_STRING: &'static str =
"#version 120
attribute vec3 position;
attribute vec3 normal;
attribute vec3 color;
attribute vec2 tex_coord_v;
varying vec3 ws_normal;
varying vec3 ws_position;
varying vec2 tex_coord;
uniform mat4 view;
uniform mat4 transform;
uniform mat3 scale;
uniform mat3 ntransform;
void main() {
mat4 scale4 = mat4(scale);
vec4 pos4 = transform * scale4 * vec4(position, 1.0);
tex_coord = tex_coord_v;
ws_position = pos4.xyz;
gl_Position = view * pos4;
ws_normal = normalize(ntransform * scale * normal);
}";
const ANOTHER_VERY_LONG_STRING: &'static str =
"#version 120
uniform vec3 color;
uniform vec3 light_position;
uniform sampler2D tex;
varying vec2 tex_coord;
varying vec3 ws_normal;
varying vec3 ws_position;
void main() {
vec3 L = normalize(light_position - ws_position);
vec3 E = normalize(-ws_position);
//calculate Ambient Term:
vec4 Iamb = vec4(color, 1.0);
//calculate Diffuse Term:
vec4 Idiff1 = vec4(1.0, 1.0, 1.0, 1.0) * max(dot(ws_normal,L), 0.0);
Idiff1 = clamp(Idiff1, 0.0, 1.0);
// double sided lighting:
vec4 Idiff2 = vec4(1.0, 1.0, 1.0, 1.0) * max(dot(-ws_normal,L), 0.0);
Idiff2 = clamp(Idiff2, 0.0, 1.0);
vec4 tex_color = texture2D(tex, tex_coord);
gl_FragColor = tex_color * (Iamb + (Idiff1 + Idiff2) / 2) / 2;
}";