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
/// type definition for a Vector3
pub type Vector3 = (f32, f32, f32);

/// type definition for a Quaternion
pub type Quaternion = (f32, f32, f32, f32);

/// type definition for a matrix4
pub type Matrix4 = [[f32; 4]; 4];

/// struct for defining a Vector for creating meshes
#[derive(Copy, Clone, Serialize, Deserialize, PartialEq)]
pub struct Vertex {
    /// The position of the vertex
    pub position: [f32; 3],
    /// The normal direction of the vertex
    pub normal: [f32; 3],
    /// The uv/texture coordinates
    pub texture: [f32; 2],
}
implement_vertex!(Vertex, position, normal, texture);

/// struct for handling transform data
#[derive(Builder, Copy, Clone, Serialize, Deserialize, PartialEq)]
#[builder(default)]
pub struct Transform {
    /// The position of the transform
    pub pos: Vector3,
    /// The rotation of the transform
    pub rot: Quaternion,
    /// The scale of the transform
    pub scale: Vector3,
    /// Whether the transform is currently active/should be rendered
    pub active: bool,
    /// Whether the transform is frustum culled
    pub cull: bool,
}

impl Default for Transform {
    fn default() -> Self {
        Transform {
            pos: (0f32, 0f32, 0f32),
            rot: (0f32, 0f32, 0f32, 0f32),
            scale: (1f32, 1f32, 1f32),
            active: true,
            cull: true,
        }
    }
}

unsafe impl Send for Transform {}

/// Denotes how the RenderItem acts in the physics engine
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
pub enum PhysicsType {
    /// Item is Collidable but does not move
    Static,
    /// Item is Colliable and moves
    Dynamic,
    /// Item does not use the physics engine
    None,
}

/// Default RenderItem utype
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug, Default)]
pub struct DefaultTag;

/// struct for abstracting items to be sent to render
#[derive(Builder, Clone, Serialize, Deserialize, PartialEq)]
#[builder(default)]
pub struct RenderItem<T: Default> {
    /// The vertices representing this items mesh
    pub vertices: Vec<Vertex>,
    /// The material that will be used for rendering the Item
    pub material: Material,
    /// The instances of this item
    pub instance_transforms: Vec<Transform>,
    /// Whether the item is active/should be rendered
    pub active: bool,
    /// How this item acts in the physics engine
    pub physics_type: PhysicsType,
    /// The name of the RenderItem for lookup
    #[builder(setter(into))]
    pub name: String,
    /// Tag Type for grouping similar items
    pub tag: T,
}

impl<T: Default> Default for RenderItem<T> {
    fn default() -> Self {
        RenderItem {
            vertices: Default::default(),
            material: Default::default(),
            instance_transforms: Default::default(),
            active: true,
            physics_type: PhysicsType::None,
            name: "ri".into(),
            tag: Default::default(),
        }
    }
}

unsafe impl<T: Default> Send for RenderItem<T> {}

/// Struct for containing material information
#[derive(Builder, Clone, Serialize, Deserialize, PartialEq)]
#[builder(default)]
pub struct Material {
    /// The shader that will used to render this item
    #[builder(setter(into))]
    pub shader_name: String,
    /// The texture that will be used
    pub texture_name: Option<String>,
    /// The normal texture that will be used
    pub normal_texture_name: Option<String>,
}

impl Default for Material {
    fn default() -> Self {
        Material {
            shader_name: "dist".into(),
            texture_name: None,
            normal_texture_name: None,
        }
    }
}

/// struct for abstacting text items to be rendered
#[derive(Builder, Clone, Serialize, Deserialize, PartialEq)]
#[builder(default)]
pub struct TextItem {
    /// The text that the item displays
    #[builder(setter(into))]
    pub text: String,
    /// The color the text is displayed in
    pub color: (f32, f32, f32, f32),
    /// The position to display this text
    pub pos: Vector3,
    /// The scale/size the text is displayed at
    pub scale: Vector3,
    /// Whether this item is active/should be rendered
    pub active: bool,
    /// The name of the RenderItem for lookup
    #[builder(setter(into))]
    pub name: String,
}

impl Default for TextItem {
    fn default() -> Self {
        TextItem {
            text: Default::default(),
            color: (0f32, 0f32, 0f32, 1f32),
            pos: (0f32, 0f32, 0f32),
            scale: (1f32, 1f32, 1f32),
            active: true,
            name: "ti".into(),
        }
    }
}

unsafe impl Send for TextItem {}

/// struct for abstracting the camera state
#[derive(Builder, Copy, Clone, Serialize, Deserialize, PartialEq)]
#[builder(default)]
pub struct Camera {
    /// The position of the camera in 3d space
    pub pos: Vector3,
    /// The euler rotation of the camera
    pub euler_rot: Vector3,
}

impl Default for Camera {
    fn default() -> Self {
        Camera {
            pos: (0f32, 0f32, 0f32),
            euler_rot: (0f32, 0f32, 0f32),
        }
    }
}

/// struct for shader attributes
#[derive(Copy, Clone, Serialize, Deserialize, PartialEq)]
pub struct ShaderIn {
    /// The world position to be passed to the shader
    pub world_position: Vector3,
    /// The world rotation to be passed to the shader
    pub world_rotation: Quaternion,
    /// The world scale to be passed to the shader
    pub world_scale: Vector3,
}
implement_vertex!(ShaderIn, world_position, world_rotation, world_scale);