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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
//! # Turbine-Scene3D
//!
//! Scene rendering for the Turbine game engine.
//!
//! <video width="320" height="240" controls>
//!  <source src="https://i.imgur.com/M0frz9B.mp4" type="video/mp4">
//! Your browser does not support the video tag.
//! </video>
//!
//! ### Design
//!
//! - Scene object stores all resources used for rendering
//! - Frame graph stores command lists
//!
//! This design allows flexible programming of scenes, without the need for
//! a tree structure to store nodes for scene data.
//! The frame graph can be used to debug the scene.

#![deny(missing_docs)]

extern crate gl;
extern crate piston;
extern crate vecmath;
extern crate opengl_graphics;
extern crate wavefront_obj;
extern crate image;

use std::path::Path;
use std::io;

use vecmath::*;
use opengl_graphics::shader_utils::{
    compile_shader,
    uniform_location
};

/// Stores a scene command.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Command {
    /// Use program.
    UseProgram(Program),
    /// Set model-view-projection transform.
    SetModelViewProjection(Matrix4Uniform),
    /// Set model transform.
    SetModel(Matrix4Uniform),
    /// Set view transform.
    SetView(Matrix4Uniform),
    /// Set texture.
    SetTexture(Texture),
    /// Set f32 uniform.
    SetF32(F32Uniform, f32),
    /// Set 2D vector uniform.
    SetVector2(Vector2Uniform, Vector2<f32>),
    /// Set 3D vector uniform.
    SetVector3(Vector3Uniform, Vector3<f32>),
    /// Set matrx uniform.
    SetMatrix4(Matrix4Uniform, Matrix4<f32>),
    /// Enable cull face.
    EnableCullFace,
    /// Disable cull face.
    DisableCullFace,
    /// Cull front face.
    CullFaceFront,
    /// Cull back face.
    CullFaceBack,
    /// Cull both front and back face.
    CullFaceFrontAndBack,
    /// Draw triangles.
    DrawTriangles(VertexArray, usize),
    /// Draw triangle strip.
    DrawTriangleStrip(VertexArray, usize),
    /// Draw lines.
    DrawLines(VertexArray, usize),
    /// Draw points.
    DrawPoints(VertexArray, usize),
    /// Translate model.
    Translate(Vector3<f32>),
    /// Translate model in global coordinates.
    TranslateGlobal(Vector3<f32>),
    /// Scale model.
    Scale(Vector3<f32>),
    /// Rotate model around x axis with degrees.
    RotateXDeg(f32),
    /// Rotate model around y axis with degrees.
    RotateYDeg(f32),
    /// Rotate model around z axis with degrees.
    RotateZDeg(f32),
    /// Rotate model around axis with degrees.
    RotateAxisDeg(Vector3<f32>, f32),
    /// Push model transform to transform stack.
    PushTransform,
    /// Pop model transform from transform stack.
    PopTransform,
    /// Draw a command list.
    Draw(CommandList),
}

/// Stores how stuff is rendered in a single frame.
#[derive(Debug)]
pub struct FrameGraph {
    command_lists: Vec<Vec<Command>>,
}

impl FrameGraph {
    /// Creates a new frame graph.
    pub fn new() -> FrameGraph {
        FrameGraph {
            command_lists: vec![]
        }
    }

    /// Create command list.
    pub fn command_list(&mut self, commands: Vec<Command>) -> CommandList {
        let id = self.command_lists.len();
        self.command_lists.push(commands);
        CommandList(id)
    }
}

/// References a vertex shader.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct VertexShader(usize);
/// References a fragment shader.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct FragmentShader(usize);
/// References a program.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Program(usize);
/// References 4D matrix uniform.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Matrix4Uniform(usize);
/// References a 2D vector uniform.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Vector2Uniform(usize);
/// References a 3D vector uniform.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Vector3Uniform(usize);
/// References a f32 uniform.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct F32Uniform(usize);
/// References a vertex array object.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct VertexArray(usize);
/// References a color buffer object.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct ColorBuffer(usize, usize);
/// References a 3D vertex buffer object.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct VertexBuffer3(usize, usize);
/// References a 2D vertex buffer object.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct VertexBuffer2(usize, usize);
/// References an UV buffer object.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct UVBuffer(usize, usize);
/// References a normal buffer object.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct NormalBuffer(usize, usize);
/// References a command list object.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct CommandList(usize);
/// References a texture object.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Texture(usize);

impl ColorBuffer {
    /// Length of color buffer.
    pub fn len(&self) -> usize {self.1}
}

impl VertexBuffer3 {
    /// Length of vertex buffer.
    pub fn len(&self) -> usize {self.1}
}

impl VertexBuffer2 {
    /// Length of vertex buffer.
    pub fn len(&self) -> usize {self.1}
}

/// Stores OBJ mesh data.
pub struct ObjMesh {
    /// Stores vertex coordinates.
    pub vertices: Vec<f32>,
    /// Stores texture coordinates.
    pub uvs: Vec<f32>,
    /// Stores normal coordinates.
    pub normals: Vec<f32>,
}

impl ObjMesh {
    /// Load OBJ file.
    pub fn load<P: AsRef<Path>>(path: P) -> Result<ObjMesh, io::Error> {
        use std::fs::File;
        use std::io::Read;

        let mut obj_file = File::open(path)?;
        let mut data = String::new();
        obj_file.read_to_string(&mut data)?;
        let obj_set = wavefront_obj::obj::parse(data).unwrap();
        let obj = &obj_set.objects[0];
        let temp_vertices = {
            let mut res = vec![];
            for v in &obj.vertices {
                res.push(v.x as f32);
                res.push(v.y as f32);
                res.push(v.z as f32);
            }
            res
        };
        let temp_uvs = {
            let mut res = vec![];
            for uv in &obj.tex_vertices {
                res.push(uv.u as f32);
                res.push(1.0 - uv.v as f32);
            }
            res
        };
        let temp_normals = {
            let mut res = vec![];
            for normal in &obj.normals {
                res.push(normal.x as gl::types::GLfloat);
                res.push(normal.y as gl::types::GLfloat);
                res.push(normal.z as gl::types::GLfloat);
            }
            res
        };
        let mut vertices = vec![];
        let mut uvs = vec![];
        let mut normals = vec![];
        for geom in &obj.geometry {
            for shape in &geom.shapes {
                use wavefront_obj::obj::Primitive;

                if let Primitive::Triangle(
                    (a_v, Some(a_uv), Some(a_n)),
                    (b_v, Some(b_uv), Some(b_n)),
                    (c_v, Some(c_uv), Some(c_n))
                ) = shape.primitive {
                    vertices.push(temp_vertices[a_v * 3 + 0]);
                    vertices.push(temp_vertices[a_v * 3 + 1]);
                    vertices.push(temp_vertices[a_v * 3 + 2]);

                    vertices.push(temp_vertices[b_v * 3 + 0]);
                    vertices.push(temp_vertices[b_v * 3 + 1]);
                    vertices.push(temp_vertices[b_v * 3 + 2]);

                    vertices.push(temp_vertices[c_v * 3 + 0]);
                    vertices.push(temp_vertices[c_v * 3 + 1]);
                    vertices.push(temp_vertices[c_v * 3 + 2]);

                    uvs.push(temp_uvs[a_uv * 2 + 0]);
                    uvs.push(temp_uvs[a_uv * 2 + 1]);

                    uvs.push(temp_uvs[b_uv * 2 + 0]);
                    uvs.push(temp_uvs[b_uv * 2 + 1]);

                    uvs.push(temp_uvs[c_uv * 2 + 0]);
                    uvs.push(temp_uvs[c_uv * 2 + 1]);

                    normals.push(temp_normals[a_n * 3 + 0]);
                    normals.push(temp_normals[a_n * 3 + 1]);
                    normals.push(temp_normals[a_n * 3 + 2]);

                    normals.push(temp_normals[b_n * 3 + 0]);
                    normals.push(temp_normals[b_n * 3 + 1]);
                    normals.push(temp_normals[b_n * 3 + 2]);

                    normals.push(temp_normals[c_n * 3 + 0]);
                    normals.push(temp_normals[c_n * 3 + 1]);
                    normals.push(temp_normals[c_n * 3 + 2]);
                }
            }
        }
        Ok(ObjMesh {
            vertices,
            uvs,
            normals
        })
    }
}

/// Stores scene settings.
#[derive(Clone)]
pub struct SceneSettings {
    clear_depth_buffer: bool,
    clear_enable_depth_test: bool,
}

impl SceneSettings {
    /// Returns new scene settings with default settings.
    pub fn new() -> SceneSettings {
        SceneSettings {
            clear_depth_buffer: true,
            clear_enable_depth_test: true,
        }
    }

    /// Set whether to clear depth buffer on clear.
    pub fn clear_depth_buffer(mut self, val: bool) -> Self {
        self.clear_depth_buffer = val;
        self
    }

    /// Set whether to enable depth test on clear.
    ///
    /// Uses depth test function `LESS` by default.
    pub fn clear_enable_depth_test(mut self, val: bool) -> Self {
        self.clear_enable_depth_test = val;
        self
    }
}

impl Default for SceneSettings {
    fn default() -> Self {SceneSettings::new()}
}

/// Stores scene data.
pub struct Scene {
    /// Scene settings.
    pub settings: SceneSettings,
    /// Projection transform.
    pub projection: Matrix4<f32>,
    /// Camera transform.
    pub camera: Matrix4<f32>,
    /// Model transform.
    pub model: Matrix4<f32>,
    transform_stack: Vec<Matrix4<f32>>,
    shaders: Vec<gl::types::GLuint>,
    programs: Vec<gl::types::GLuint>,
    uniforms: Vec<gl::types::GLuint>,
    vertex_arrays: Vec<gl::types::GLuint>,
    buffers: Vec<gl::types::GLuint>,
    textures: Vec<gl::types::GLuint>,
}

impl Scene {
    /// Create new scene.
    pub fn new(settings: SceneSettings) -> Scene {
        let mat_id = mat4_id();
        Scene {
            settings,
            projection: mat_id,
            camera: mat_id,
            model: mat_id,
            shaders: vec![],
            programs: vec![],
            uniforms: vec![],
            vertex_arrays: vec![],
            buffers: vec![],
            transform_stack: vec![],
            textures: vec![],
        }
    }

    /// Load texture from path.
    pub fn load_texture<P: AsRef<Path>>(&mut self, path: P) -> Result<Texture, image::ImageError> {
        use std::mem::transmute;

        let image = match image::open(path)? {
            image::DynamicImage::ImageRgba8(img) => img,
            x => x.to_rgba()
        };
        let (image_width, image_height) = image.dimensions();
        let mut texture_id = 0;
        unsafe {
            gl::GenTextures(1, &mut texture_id);
            gl::BindTexture(gl::TEXTURE_2D, texture_id);
            gl::TexImage2D(
                gl::TEXTURE_2D,
                0,
                gl::RGBA as i32,
                image_width as i32,
                image_height as i32,
                0,
                gl::RGBA,
                gl::UNSIGNED_BYTE,
                transmute(image.as_ptr())
            );
            gl::TexParameteri(
                gl::TEXTURE_2D,
                gl::TEXTURE_MAG_FILTER,
                gl::LINEAR as i32
            );
            gl::TexParameteri(
                gl::TEXTURE_2D,
                gl::TEXTURE_MIN_FILTER,
                gl::LINEAR_MIPMAP_LINEAR as i32
            );
            gl::GenerateMipmap(gl::TEXTURE_2D);
        }
        let id = self.textures.len();
        self.textures.push(texture_id);
        Ok(Texture(id))
    }

    /// Create vertex shader from source.
    pub fn vertex_shader(
        &mut self,
        vertex_shader_src: &str
    ) -> Result<VertexShader, String> {
        let id = self.shaders.len();
        let vertex_shader = compile_shader(gl::VERTEX_SHADER, vertex_shader_src)
            .unwrap();
        self.shaders.push(vertex_shader);
        Ok(VertexShader(id))
    }

    /// Create fragment shader from source.
    pub fn fragment_shader(
        &mut self,
        fragment_shader_src: &str
    ) -> Result<FragmentShader, String> {
        let id = self.shaders.len();
        let fragment_shader = compile_shader(gl::FRAGMENT_SHADER, fragment_shader_src)?;
        self.shaders.push(fragment_shader);
        Ok(FragmentShader(id))
    }

    /// Create program from vertex and fragment shader.
    pub fn program_from_vertex_fragment(
        &mut self,
        vertex_shader: VertexShader,
        fragment_shader: FragmentShader
    ) -> Program {
        unsafe {
            let id = self.programs.len();
            let program = gl::CreateProgram();
            gl::AttachShader(program, self.shaders[vertex_shader.0]);
            gl::AttachShader(program, self.shaders[fragment_shader.0]);
            gl::LinkProgram(program);
            gl::DetachShader(program, self.shaders[vertex_shader.0]);
            gl::DetachShader(program, self.shaders[fragment_shader.0]);
            self.programs.push(program);
            Program(id)
        }
    }

    /// Create 4D matrix uniform.
    pub fn matrix4_uniform(
        &mut self,
        program: Program,
        name: &str,
    ) -> Result<Matrix4Uniform, String> {
        let id = self.uniforms.len();
        let uniform_location = uniform_location(self.programs[program.0], name)?;
        self.uniforms.push(uniform_location);
        Ok(Matrix4Uniform(id))
    }

    /// Create 2D vector uniform.
    pub fn vector2_uniform(
        &mut self,
        program: Program,
        name: &str
    ) -> Result<Vector2Uniform, String> {
        let id = self.uniforms.len();
        let uniform_location = uniform_location(self.programs[program.0], name)?;
        self.uniforms.push(uniform_location);
        Ok(Vector2Uniform(id))
    }

    /// Create 3D vector uniform.
    pub fn vector3_uniform(
        &mut self,
        program: Program,
        name: &str
    ) -> Result<Vector3Uniform, String> {
        let id = self.uniforms.len();
        let uniform_location = uniform_location(self.programs[program.0], name)?;
        self.uniforms.push(uniform_location);
        Ok(Vector3Uniform(id))
    }

    /// Create f32 uniform.
    pub fn f32_uniform(
        &mut self,
        program: Program,
        name: &str
    ) -> Result<F32Uniform, String> {
        let id = self.uniforms.len();
        let uniform_location = uniform_location(self.programs[program.0], name)?;
        self.uniforms.push(uniform_location);
        Ok(F32Uniform(id))
    }

    /// Create vertex array.
    pub fn vertex_array(&mut self) -> VertexArray {
        unsafe {
            let id = self.vertex_arrays.len();
            let mut vertex_array_id = 0;
            gl::GenVertexArrays(1, &mut vertex_array_id);
            self.vertex_arrays.push(vertex_array_id);
            VertexArray(id)
        }
    }

    /// Create uv buffer.
    pub fn uv_buffer(
        &mut self,
        vertex_array: VertexArray,
        attribute: u32,
        data: &[f32]
    ) -> UVBuffer {
        use std::mem::{size_of, transmute};
        use std::ptr::null;

        unsafe {
            let id = self.buffers.len();
            let mut color_buffer = 0;
            gl::GenBuffers(1, &mut color_buffer);
            gl::BindBuffer(gl::ARRAY_BUFFER, color_buffer);
            gl::BufferData(
                gl::ARRAY_BUFFER,
                (data.len() * size_of::<gl::types::GLfloat>()) as isize,
                transmute(data.as_ptr()),
                gl::STATIC_DRAW
            );

            gl::BindVertexArray(self.vertex_arrays[vertex_array.0]);
            gl::VertexAttribPointer(
                attribute,  // attribute
                2,          // size
                gl::FLOAT,  // type
                gl::FALSE,  // normalized?
                0,          // stride
                null()      // array buffer offset
            );
            gl::EnableVertexAttribArray(attribute);

            self.buffers.push(color_buffer);
            UVBuffer(id, data.len() / 2)
        }
    }

    /// Create color buffer.
    pub fn color_buffer(
        &mut self,
        vertex_array: VertexArray,
        attribute: u32,
        data: &[f32]
    ) -> ColorBuffer {
        use std::mem::{size_of, transmute};
        use std::ptr::null;

        unsafe {
            let id = self.buffers.len();
            let mut color_buffer = 0;
            gl::GenBuffers(1, &mut color_buffer);
            gl::BindBuffer(gl::ARRAY_BUFFER, color_buffer);
            gl::BufferData(
                gl::ARRAY_BUFFER,
                (data.len() * size_of::<gl::types::GLfloat>()) as isize,
                transmute(data.as_ptr()),
                gl::STATIC_DRAW
            );

            gl::BindVertexArray(self.vertex_arrays[vertex_array.0]);
            gl::VertexAttribPointer(
                attribute,  // attribute
                3,          // size
                gl::FLOAT,  // type
                gl::FALSE,  // normalized?
                0,          // stride
                null()      // array buffer offset
            );
            gl::EnableVertexAttribArray(attribute);

            self.buffers.push(color_buffer);
            ColorBuffer(id, data.len() / 3)
        }
    }

    /// Create vertex buffer for 2D coordinates.
    pub fn vertex_buffer2(
        &mut self,
        vertex_array: VertexArray,
        attribute: u32,
        data: &[f32]
    ) -> VertexBuffer2 {
        use std::mem::{size_of, transmute};
        use std::ptr::null;

        unsafe {
            let id = self.buffers.len();
            let mut vertex_buffer = 0;
            gl::GenBuffers(1, &mut vertex_buffer);
            gl::BindBuffer(gl::ARRAY_BUFFER, vertex_buffer);
            gl::BufferData(
                gl::ARRAY_BUFFER,
                (data.len() * size_of::<gl::types::GLfloat>()) as isize,
                transmute(data.as_ptr()),
                gl::STATIC_DRAW
            );

            gl::BindVertexArray(self.vertex_arrays[vertex_array.0]);
            gl::VertexAttribPointer(
                attribute,  // attribute
                2,          // size
                gl::FLOAT,  // type
                gl::FALSE,  // normalized?
                0,          // stride
                null()      // array buffer offset
            );
            gl::EnableVertexAttribArray(attribute);

            self.buffers.push(vertex_buffer);
            VertexBuffer2(id, data.len() / 2)
        }
    }

    /// Create vertex buffer for 3D coordinates.
    pub fn vertex_buffer3(
        &mut self,
        vertex_array: VertexArray,
        attribute: u32,
        data: &[f32]
    ) -> VertexBuffer3 {
        use std::mem::{size_of, transmute};
        use std::ptr::null;

        unsafe {
            let id = self.buffers.len();
            let mut vertex_buffer = 0;
            gl::GenBuffers(1, &mut vertex_buffer);
            gl::BindBuffer(gl::ARRAY_BUFFER, vertex_buffer);
            gl::BufferData(
                gl::ARRAY_BUFFER,
                (data.len() * size_of::<gl::types::GLfloat>()) as isize,
                transmute(data.as_ptr()),
                gl::STATIC_DRAW
            );

            gl::BindVertexArray(self.vertex_arrays[vertex_array.0]);
            gl::VertexAttribPointer(
                attribute,  // attribute
                3,          // size
                gl::FLOAT,  // type
                gl::FALSE,  // normalized?
                0,          // stride
                null()      // array buffer offset
            );
            gl::EnableVertexAttribArray(attribute);

            self.buffers.push(vertex_buffer);
            VertexBuffer3(id, data.len() / 3)
        }
    }

    /// Create normal buffer.
    pub fn normal_buffer(
        &mut self,
        vertex_array: VertexArray,
        attribute: u32,
        data: &[f32]
    ) -> NormalBuffer {
        use std::mem::{size_of, transmute};
        use std::ptr::null;

        unsafe {
            let id = self.buffers.len();
            let mut vertex_buffer = 0;
            gl::GenBuffers(1, &mut vertex_buffer);
            gl::BindBuffer(gl::ARRAY_BUFFER, vertex_buffer);
            gl::BufferData(
                gl::ARRAY_BUFFER,
                (data.len() * size_of::<gl::types::GLfloat>()) as isize,
                transmute(data.as_ptr()),
                gl::STATIC_DRAW
            );

            gl::BindVertexArray(self.vertex_arrays[vertex_array.0]);
            gl::VertexAttribPointer(
                attribute,  // attribute
                3,          // size
                gl::FLOAT,  // type
                gl::FALSE,  // normalized?
                0,          // stride
                null()      // array buffer offset
            );
            gl::EnableVertexAttribArray(attribute);

            self.buffers.push(vertex_buffer);
            NormalBuffer(id, data.len() / 3)
        }
    }

    /// Use program.
    pub fn use_program(&self, program: Program) {
        unsafe {gl::UseProgram(self.programs[program.0])}
    }

    /// Set model-view-projection transform uniform.
    pub fn set_model_view_projection(&self, matrix_id: Matrix4Uniform) {
        use vecmath::col_mat4_mul as mul;
        unsafe {
            let mvp = mul(mul(self.projection, self.camera), self.model);
            gl::UniformMatrix4fv(self.uniforms[matrix_id.0] as i32, 1, gl::FALSE, &mvp[0][0])
        }
    }

    /// Set view transform uniform.
    pub fn set_view(&self, matrix_id: Matrix4Uniform) {
        unsafe {
            gl::UniformMatrix4fv(self.uniforms[matrix_id.0] as i32, 1, gl::FALSE, &self.camera[0][0])
        }
    }

    /// Set model transform uniform.
    pub fn set_model(&self, matrix_id: Matrix4Uniform) {
        unsafe {
            gl::UniformMatrix4fv(self.uniforms[matrix_id.0] as i32, 1, gl::FALSE, &self.model[0][0])
        }
    }

    /// Set matrix uniform.
    pub fn set_matrix4(&self, matrix_id: Matrix4Uniform, val: Matrix4<f32>) {
        unsafe {
            gl::UniformMatrix4fv(self.uniforms[matrix_id.0] as i32, 1, gl::FALSE, &val[0][0])
        }
    }

    /// Set 2D vector uniform.
    pub fn set_vector2(&self, v_id: Vector2Uniform, v: Vector2<f32>) {
        unsafe {
            gl::Uniform2f(self.uniforms[v_id.0] as i32, v[0], v[1]);
        }
    }

    /// Set 3D vector uniform.
    pub fn set_vector3(&self, v_id: Vector3Uniform, v: Vector3<f32>) {
        unsafe {
            gl::Uniform3f(self.uniforms[v_id.0] as i32, v[0], v[1], v[2]);
        }
    }

    /// Set f32 uniform.
    pub fn set_f32(&self, f_id: F32Uniform, v: f32) {
        unsafe {
            gl::Uniform1f(self.uniforms[f_id.0] as i32, v);
        }
    }

    /// Set texture.
    pub fn set_texture(&self, texture_id: Texture) {
        unsafe {gl::BindTexture(gl::TEXTURE_2D, self.textures[texture_id.0])};
    }

    /// Translate model.
    pub fn translate(&mut self, v: Vector3<f32>) {
        let mat = [
            [1.0, 0.0, 0.0, 0.0],
            [0.0, 1.0, 0.0, 0.0],
            [0.0, 0.0, 1.0, 0.0],
            [v[0], v[1], v[2], 1.0]
        ];
        self.model = col_mat4_mul(self.model, mat);
    }

    /// Translate model in global coordinates.
    pub fn translate_global(&mut self, v: Vector3<f32>) {
        for i in 0..3 {
            self.model[3][i] += v[i];
        }
    }

    /// Scale model.
    pub fn scale(&mut self, v: Vector3<f32>) {
        let mat = [
            [v[0], 0.0, 0.0, 0.0],
            [0.0, v[1], 0.0, 0.0],
            [0.0, 0.0, v[2], 0.0],
            [0.0, 0.0, 0.0, 1.0]
        ];
        self.model = col_mat4_mul(self.model, mat);
    }

    /// Rotate model around x axis with degrees.
    pub fn rotate_x_deg(&mut self, deg: f32) {
        let angle = deg * 0.017453292519943295;
        let cos = angle.cos();
        let sin = angle.sin();
        let mat = [
            [1.0, 0.0, 0.0, 0.0],
            [0.0, cos, sin, 0.0],
            [0.0, -sin, cos, 0.0],
            [0.0, 0.0, 0.0, 1.0]
        ];
        self.model = col_mat4_mul(self.model, mat);
    }

    /// Rotate model around y axis with degrees.
    pub fn rotate_y_deg(&mut self, deg: f32) {
        let angle = deg * 0.017453292519943295;
        let cos = angle.cos();
        let sin = angle.sin();
        let mat = [
            [cos, 0.0, sin, 0.0],
            [0.0, 1.0, 0.0, 0.0],
            [-sin, 0.0, cos, 0.0],
            [0.0, 0.0, 0.0, 1.0]
        ];
        self.model = col_mat4_mul(self.model, mat);
    }

    /// Rotate model around z axis with degrees.
    pub fn rotate_z_deg(&mut self, deg: f32) {
        let angle = deg * 0.017453292519943295;
        let cos = angle.cos();
        let sin = angle.sin();
        let mat = [
            [cos, sin, 0.0, 0.0],
            [-sin, cos, 0.0, 0.0],
            [0.0, 0.0, 1.0, 0.0],
            [0.0, 0.0, 0.0, 1.0]
        ];
        self.model = col_mat4_mul(self.model, mat);
    }

    /// Rotate model around axis with degrees.
    pub fn rotate_axis_deg(&mut self, axis: Vector3<f32>, deg: f32) {
        let angle = deg * 0.017453292519943295;
        let cos = angle.cos();
        let sin = angle.sin();
        let mat = [
            [
                cos + axis[0]*axis[0]*(1.0-cos),
                axis[0]*axis[1]*(1.0-cos) + axis[2]*sin,
                axis[0]*axis[2]*(1.0-cos) - axis[1]*sin,
                0.0,
            ],
            [
                axis[0]*axis[1]*(1.0-cos) - axis[2]*sin,
                cos + axis[1]*axis[1]*(1.0-cos),
                axis[1]*axis[2]*(1.0-cos) + axis[0]*sin,
                0.0,
            ],
            [
                axis[0]*axis[2]*(1.0-cos) + axis[1]*sin,
                axis[1]*axis[2]*(1.0-cos) - axis[0]*sin,
                cos + axis[2]*axis[2]*(1.0-cos),
                0.0
            ],
            [0.0, 0.0, 0.0, 1.0]
        ];
        self.model = col_mat4_mul(self.model, mat);
    }

    /// Push model transfrom to transform stack.
    pub fn push_transform(&mut self) {
        self.transform_stack.push(self.model);
    }

    /// Pop model transform from transform stack.
    pub fn pop_transform(&mut self) {
        if let Some(mat) = self.transform_stack.pop() {
            self.model = mat;
        }
    }

    /// Clear background with color.
    pub fn clear(&self, bg_color: [f32; 4]) {
        unsafe {
            gl::ClearColor(bg_color[0], bg_color[1], bg_color[2], bg_color[3]);
            if self.settings.clear_depth_buffer {
                gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
            } else {
                gl::Clear(gl::COLOR_BUFFER_BIT);
            }
            if self.settings.clear_enable_depth_test {
                gl::Enable(gl::DEPTH_TEST);
                gl::DepthFunc(gl::LESS);
            }
        }
    }

    /// Enable cull face.
    pub fn enable_cull_face(&self) {
        unsafe {
            gl::Enable(gl::CULL_FACE);
        }
    }

    /// Disable cull face.
    pub fn disable_cull_face(&self) {
        unsafe {
            gl::Disable(gl::CULL_FACE);
        }
    }

    /// Cull front face.
    pub fn cull_face_front(&self) {
        unsafe {
            gl::CullFace(gl::FRONT);
        }
    }

    /// Cull back face.
    pub fn cull_face_back(&self) {
        unsafe {
            gl::CullFace(gl::BACK);
        }
    }

    /// Cull both front and back face.
    pub fn cull_face_front_and_back(&self) {
        unsafe {
            gl::CullFace(gl::FRONT_AND_BACK);
        }
    }

    /// Draws triangles.
    pub fn draw_triangles(&self, vertex_array: VertexArray, len: usize) {
        unsafe {
            gl::BindVertexArray(self.vertex_arrays[vertex_array.0]);
            gl::DrawArrays(gl::TRIANGLES, 0, len as i32);
        }
    }

    /// Draws triangle strip.
    pub fn draw_triangle_strip(&self, vertex_array: VertexArray, len: usize) {
        unsafe {
            gl::BindVertexArray(self.vertex_arrays[vertex_array.0]);
            gl::DrawArrays(gl::TRIANGLE_STRIP, 0, len as i32);
        }
    }

    /// Draws points.
    pub fn draw_points(&self, vertex_array: VertexArray, len: usize) {
        unsafe {
            gl::BindVertexArray(self.vertex_arrays[vertex_array.0]);
            gl::DrawArrays(gl::POINTS, 0, len as i32);
        }
    }

    /// Draws lines.
    pub fn draw_lines(&self, vertex_array: VertexArray, len: usize) {
        unsafe {
            gl::BindVertexArray(self.vertex_arrays[vertex_array.0]);
            gl::DrawArrays(gl::LINES, 0, len as i32);
        }
    }

    /// Executes commands in command list.
    pub fn submit(&mut self, commands: &[Command], frame_graph: &FrameGraph) {
        use Command::*;

        for command in commands {
            match *command {
                UseProgram(program) => self.use_program(program),
                SetModelViewProjection(mvp) => self.set_model_view_projection(mvp),
                SetModel(model) => self.set_model(model),
                SetView(view) => self.set_view(view),
                SetTexture(texture) => self.set_texture(texture),
                SetF32(uni, val) => self.set_f32(uni, val),
                SetVector2(uni, val) => self.set_vector2(uni, val),
                SetVector3(uni, val) => self.set_vector3(uni, val),
                SetMatrix4(uni, val) => self.set_matrix4(uni, val),
                EnableCullFace => self.enable_cull_face(),
                DisableCullFace => self.disable_cull_face(),
                CullFaceFront => self.cull_face_front(),
                CullFaceBack => self.cull_face_back(),
                CullFaceFrontAndBack => self.cull_face_front_and_back(),
                DrawTriangles(vertex_array, len) => self.draw_triangles(vertex_array, len),
                DrawTriangleStrip(vertex_array, len) => self.draw_triangle_strip(vertex_array, len),
                DrawLines(vertex_array, len) => self.draw_lines(vertex_array, len),
                DrawPoints(vertex_array, len) => self.draw_points(vertex_array, len),
                Translate(v) => self.translate(v),
                TranslateGlobal(v) => self.translate_global(v),
                Scale(v) => self.scale(v),
                RotateXDeg(deg) => self.rotate_x_deg(deg),
                RotateYDeg(deg) => self.rotate_y_deg(deg),
                RotateZDeg(deg) => self.rotate_z_deg(deg),
                RotateAxisDeg(axis, deg) => self.rotate_axis_deg(axis, deg),
                PushTransform => self.push_transform(),
                PopTransform => self.pop_transform(),
                Draw(command_list) => self.draw(command_list, frame_graph),
            }
        }
    }

    /// Draws a command list from frame graph.
    pub fn draw(&mut self, command_list: CommandList, frame_graph: &FrameGraph) {
        self.submit(&frame_graph.command_lists[command_list.0], frame_graph)
    }
}

impl Drop for Scene {
    fn drop(&mut self) {
        unsafe {
            for &shader in &self.shaders {
                gl::DeleteShader(shader);
            }
            for &program in &self.programs {
                gl::DeleteProgram(program);
            }
            if self.buffers.len() > 0 {
                gl::DeleteBuffers(self.buffers.len() as i32, &self.buffers[0]);
            }
            if self.textures.len() > 0 {
                gl::DeleteTextures(self.textures.len() as i32, &self.textures[0]);
            }
            if self.vertex_arrays.len() > 0 {
                gl::DeleteVertexArrays(self.vertex_arrays.len() as i32, &self.vertex_arrays[0]);
            }
        }
    }
}


#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}