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
//! Vertex Array Object
pub mod buffer;
pub mod color_vao;
pub mod config;
pub mod renderer;
pub mod texture_vao;
pub mod vertex;

use std::mem;
use std::os::raw::c_void;

use crate::gl;
use crate::gl::types::{GLenum, GLfloat, GLint, GLsizei, GLsizeiptr};
use crate::gl::Gl;
use crate::shader::UniformVariables;

pub use {
    buffer::VaoBuffer,
    color_vao::VaoBuilder3DGeometryOutline,
    config::{VaoConfig, VaoConfigBuilder},
    renderer::{
        Color3DRenderer, Color3DRenderingInfo, Phong3DRenderer, Phong3DRenderingInfo,
        PhongRenderingInfo, Renderer,
    },
    texture_vao::builder::{CuboidTextures, VaoBuilder3DGeometry},
    vertex::{VertexType, VertexWithColor, VertexWithNormUv},
};

/// OpenGLのVertex Array ObjectとVertex Buffer Objectに対応する構造体
#[derive(Debug)]
pub struct Vao<'a> {
    gl: Gl,
    vao: u32,
    vbo: u32,
    vertex_num: i32,
    config: &'a VaoConfig,
}

impl<'a> Vao<'a> {
    /// 代わりに[`self::vao_buffer::VaoBuffer`]を使うことを推奨
    pub fn new(
        gl: Gl,
        size: GLsizeiptr,
        data: *const c_void,
        usage: GLenum,
        num_attributes: usize,
        attribute_types: &'static [GLenum],
        attribute_sizes: &'static [GLint],
        stride: GLsizei,
        vertex_num: i32,
        config: &'a VaoConfig,
    ) -> Vao<'a> {
        debug_assert_eq!(num_attributes, attribute_types.len());
        debug_assert_eq!(num_attributes, attribute_sizes.len());

        let mut vao = 0;
        let mut vbo = 0;

        unsafe {
            // create vertex array object and vertex buffer object
            gl.GenVertexArrays(1, &mut vao);
            gl.GenBuffers(1, &mut vbo);

            // bind buffer
            gl.BindVertexArray(vao);
            gl.BindBuffer(gl::ARRAY_BUFFER, vbo);
            gl.BufferData(gl::ARRAY_BUFFER, size, data, usage);

            let mut offset = 0;
            for i in 0..num_attributes {
                gl.EnableVertexAttribArray(i as u32);
                gl.VertexAttribPointer(
                    i as u32,
                    attribute_sizes[i],
                    attribute_types[i],
                    gl::FALSE,
                    stride,
                    (offset * mem::size_of::<GLfloat>()) as *const c_void,
                );
                offset += attribute_sizes[i] as usize;
            }

            // unbind
            gl.BindBuffer(gl::ARRAY_BUFFER, 0);
            gl.BindVertexArray(0);
        }

        Vao {
            gl,
            vao,
            vbo,
            vertex_num,
            config,
        }
    }

    /// Use [`crate::Renderer`] instead
    pub fn draw(&self, uniforms: &UniformVariables, draw_mode: GLenum) {
        unsafe {
            if self.config.depth_test {
                self.gl.Enable(gl::DEPTH_TEST);
            } else {
                self.gl.Disable(gl::DEPTH_TEST);
            }

            if self.config.blend {
                self.gl.Enable(gl::BLEND);
                self.gl.BlendFunc(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA);
            } else {
                self.gl.Disable(gl::BLEND);
            }

            if self.config.wireframe {
                self.gl.PolygonMode(gl::FRONT_AND_BACK, gl::LINE);
            } else {
                self.gl.PolygonMode(gl::FRONT_AND_BACK, gl::FILL);
            }

            if self.config.culling {
                self.gl.Enable(gl::CULL_FACE);
            } else {
                self.gl.Disable(gl::CULL_FACE);
            }

            self.gl.BindVertexArray(self.vao);
            self.gl.DrawArrays(draw_mode, 0, self.vertex_num);
            self.gl.BindVertexArray(0);
            self.gl.BindTexture(gl::TEXTURE_2D, 0);
        }
    }

    /// ポリゴンを描画する
    /// Use [`crate::Renderer`] instead
    pub fn draw_triangles(&self, uniforms: &UniformVariables) {
        self.draw(uniforms, gl::TRIANGLES);
    }
}

impl<'a> Drop for Vao<'a> {
    fn drop(&mut self) {
        unsafe {
            if self.vbo > 0 {
                self.gl.DeleteBuffers(1, &self.vbo as _);
            }
            if self.vao > 0 {
                self.gl.DeleteVertexArrays(1, &self.vao as _);
            }
        }
    }
}