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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
use std::any::Any;
use std::cell::RefCell;
use std::rc::Rc;
use std::path::Path;
use gl::types::*;
use na::{Point3, Point2, Vector3, Isometry3};
use resource::{Texture, TextureManager, Material, Mesh};
use camera::Camera;
use light::Light;
#[path = "../error.rs"]
mod error;
pub struct ObjectData {
material: Rc<RefCell<Box<Material + 'static>>>,
texture: Rc<Texture>,
color: Point3<f32>,
wlines: f32,
wpoints: f32,
draw_surface: bool,
cull: bool,
user_data: Box<Any + 'static>
}
impl ObjectData {
#[inline]
pub fn texture(&self) -> &Rc<Texture> {
&self.texture
}
#[inline]
pub fn color(&self) -> &Point3<f32> {
&self.color
}
#[inline]
pub fn lines_width(&self) -> f32 {
self.wlines
}
#[inline]
pub fn points_size(&self) -> f32 {
self.wpoints
}
#[inline]
pub fn surface_rendering_active(&self) -> bool {
self.draw_surface
}
#[inline]
pub fn backface_culling_enabled(&self) -> bool {
self.cull
}
#[inline]
pub fn user_data(&self) -> &Any {
&*self.user_data
}
}
pub struct Object {
data: ObjectData,
mesh: Rc<RefCell<Mesh>>
}
impl Object {
#[doc(hidden)]
pub fn new(mesh: Rc<RefCell<Mesh>>,
r: f32,
g: f32,
b: f32,
texture: Rc<Texture>,
material: Rc<RefCell<Box<Material + 'static>>>) -> Object {
let user_data = ();
let data = ObjectData {
color: Point3::new(r, g, b),
texture: texture,
wlines: 0.0,
wpoints: 0.0,
draw_surface: true,
cull: true,
material: material,
user_data: Box::new(user_data)
};
Object {
data: data,
mesh: mesh
}
}
#[doc(hidden)]
pub fn render(&self,
transform: &Isometry3<f32>,
scale: &Vector3<f32>,
pass: usize,
camera: &mut Camera,
light: &Light) {
self.data.material.borrow_mut().render(
pass,
transform,
scale,
camera,
light,
&self.data,
&mut *self.mesh.borrow_mut());
}
#[inline]
pub fn data(&self) -> &ObjectData {
&self.data
}
#[inline]
pub fn data_mut(&mut self) -> &mut ObjectData {
&mut self.data
}
#[inline]
pub fn enable_backface_culling(&mut self, active: bool) {
self.data.cull = active;
}
#[inline]
pub fn set_user_data(&mut self, user_data: Box<Any + 'static>) {
self.data.user_data = user_data;
}
#[inline]
pub fn material(&self) -> Rc<RefCell<Box<Material + 'static>>> {
self.data.material.clone()
}
#[inline]
pub fn set_material(&mut self, material: Rc<RefCell<Box<Material + 'static>>>) {
self.data.material = material;
}
#[inline]
pub fn set_lines_width(&mut self, width: f32) {
self.data.wlines = width
}
#[inline]
pub fn lines_width(&self) -> f32 {
self.data.wlines
}
#[inline]
pub fn set_points_size(&mut self, size: f32) {
self.data.wpoints = size
}
#[inline]
pub fn points_size(&self) -> f32 {
self.data.wpoints
}
#[inline]
pub fn set_surface_rendering_activation(&mut self, active: bool) {
self.data.draw_surface = active
}
#[inline]
pub fn surface_rendering_activation(&self) -> bool {
self.data.draw_surface
}
#[inline]
pub fn mesh(&self) -> &Rc<RefCell<Mesh>> {
&self.mesh
}
#[inline(always)]
pub fn modify_vertices<F: FnMut(&mut Vec<Point3<GLfloat>>)>(&mut self, f: &mut F) {
let bmesh = self.mesh.borrow_mut();
let _ = bmesh.coords().write().unwrap().data_mut().as_mut().map(|coords| f(coords));
}
#[inline(always)]
pub fn read_vertices<F: FnMut(&[Point3<GLfloat>])>(&self, f: &mut F) {
let bmesh = self.mesh.borrow();
let _ = bmesh.coords().read().unwrap().data().as_ref().map(|coords| f(&coords[..]));
}
#[inline]
pub fn recompute_normals(&mut self) {
self.mesh.borrow_mut().recompute_normals();
}
#[inline(always)]
pub fn modify_normals<F: FnMut(&mut Vec<Vector3<GLfloat>>)>(&mut self, f: &mut F) {
let bmesh = self.mesh.borrow_mut();
let _ = bmesh.normals().write().unwrap().data_mut().as_mut().map(|normals| f(normals));
}
#[inline(always)]
pub fn read_normals<F: FnMut(&[Vector3<GLfloat>])>(&self, f: &mut F) {
let bmesh = self.mesh.borrow();
let _ = bmesh.normals().read().unwrap().data().as_ref().map(|normals| f(&normals[..]));
}
#[inline(always)]
pub fn modify_faces<F: FnMut(&mut Vec<Point3<GLuint>>)>(&mut self, f: &mut F) {
let bmesh = self.mesh.borrow_mut();
let _ = bmesh.faces().write().unwrap().data_mut().as_mut().map(|faces| f(faces));
}
#[inline(always)]
pub fn read_faces<F: FnMut(&[Point3<GLuint>])>(&self, f: &mut F) {
let bmesh = self.mesh.borrow();
let _ = bmesh.faces().read().unwrap().data().as_ref().map(|faces| f(&faces[..]));
}
#[inline(always)]
pub fn modify_uvs<F: FnMut(&mut Vec<Point2<GLfloat>>)>(&mut self, f: &mut F) {
let bmesh = self.mesh.borrow_mut();
let _ = bmesh.uvs().write().unwrap().data_mut().as_mut().map(|uvs| f(uvs));
}
#[inline(always)]
pub fn read_uvs<F: FnMut(&[Point2<GLfloat>])>(&self, f: &mut F) {
let bmesh = self.mesh.borrow();
let _ = bmesh.uvs().read().unwrap().data().as_ref().map(|uvs| f(&uvs[..]));
}
#[inline]
pub fn set_color(&mut self, r: f32, g: f32, b: f32) {
self.data.color.x = r;
self.data.color.y = g;
self.data.color.z = b;
}
#[inline]
pub fn set_texture_from_file(&mut self, path: &Path, name: &str) {
let texture = TextureManager::get_global_manager(|tm| tm.add(path, name));
self.set_texture(texture)
}
#[inline]
pub fn set_texture_with_name(&mut self, name: &str) {
let texture = TextureManager::get_global_manager(|tm| tm.get(name).unwrap_or_else(
|| panic!("Invalid attempt to use the unregistered texture: {}", name)));
self.set_texture(texture)
}
#[inline]
pub fn set_texture(&mut self, texture: Rc<Texture>) {
self.data.texture = texture
}
}