summaryrefslogtreecommitdiff
path: root/engine/src/object.rs
blob: dffd2404e6758ee7d14fde16409ba47b75146087 (plain)
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
use crate::opengl::shader::{
    Error as ShaderError,
    Kind as ShaderKind,
    Program as ShaderProgram,
    Shader,
};
use crate::renderer::Renderable;
use crate::transform::Transform;
use crate::vector::Vec3;
use crate::vertex::Vertex;

#[derive(Debug)]
pub struct Object
{
    id: Id,
    renderable: Renderable,
    transform: Transform,
}

impl Object
{
    /// Returns a new `Object`.
    ///
    /// # Errors
    /// Will return `Err` if shader creation fails or if shader program linking fails.
    pub fn new(
        id: Id,
        vertices: &[Vertex],
        indices: Option<&[u32]>,
    ) -> Result<Self, Error>
    {
        let vertex_shader = Shader::new(ShaderKind::Vertex);

        vertex_shader
            .set_source(include_str!("../vertex.glsl"))
            .map_err(Error::CreateVertexShaderFailed)?;

        vertex_shader
            .compile()
            .map_err(Error::CreateVertexShaderFailed)?;

        let fragment_shader = Shader::new(ShaderKind::Fragment);

        fragment_shader
            .set_source(include_str!("../fragment.glsl"))
            .map_err(Error::CreateFragmentShaderFailed)?;

        fragment_shader
            .compile()
            .map_err(Error::CreateFragmentShaderFailed)?;

        let shader_program = ShaderProgram::new();

        shader_program.attach(&vertex_shader);
        shader_program.attach(&fragment_shader);

        shader_program
            .link()
            .map_err(Error::LinkShaderProgramFailed)?;

        let renderable = Renderable::new(shader_program, vertices, indices);

        let transform = Transform::new();

        Ok(Self { id, renderable, transform })
    }

    /// Returns the object ID.
    #[must_use]
    pub fn id(&self) -> Id
    {
        self.id
    }

    #[must_use]
    pub fn position(&self) -> &Vec3<f32>
    {
        self.transform.translation()
    }

    pub fn translate(&mut self, translation: Vec3<f32>)
    {
        self.transform.set_translation(translation);
    }

    pub fn scale(&mut self, scaling: Vec3<f32>)
    {
        self.transform.set_scaling(scaling);
    }

    pub(crate) fn renderable(&self) -> &Renderable
    {
        &self.renderable
    }

    pub(crate) fn transform(&self) -> &Transform
    {
        &self.transform
    }
}

/// Object error
#[derive(Debug, thiserror::Error)]
pub enum Error
{
    #[error("Failed to create vertex shader")]
    CreateVertexShaderFailed(#[source] ShaderError),

    #[error("Failed to create fragment shader")]
    CreateFragmentShaderFailed(#[source] ShaderError),

    #[error("Failed to link shader program")]
    LinkShaderProgramFailed(#[source] ShaderError),
}

/// Object ID.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Id
{
    id: u16,
}

impl Id
{
    /// Returns a new object ID.
    #[must_use]
    pub fn new(id: u16) -> Self
    {
        Self { id }
    }
}