aboutsummaryrefslogtreecommitdiff
path: root/stockton-levels/src/q3/vertices.rs
blob: 97cb28f70ae97c6a6ed70d116b9b3128c9e3ca2d (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
/*
 * Copyright (C) Oscar Shrimpton 2020
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

use std::convert::TryInto;

use super::Q3BspFile;
use crate::coords::CoordSystem;
use crate::helpers::{slice_to_u32, slice_to_vec3};
use crate::traits::vertices::*;
use crate::types::{ParseError, Result, Rgba};

/// The size of one vertex
const VERTEX_SIZE: usize = (4 * 3) + (2 * 2 * 4) + (4 * 3) + 4;

/// Parse a Vertices data from the data in a BSP file.
pub fn verts_from_data(data: &[u8]) -> Result<Box<[Vertex]>> {
    if data.len() % VERTEX_SIZE != 0 {
        return Err(ParseError::Invalid);
    }
    let length = data.len() / VERTEX_SIZE;

    let mut vertices = Vec::with_capacity(length as usize);
    for n in 0..length {
        let offset = n * VERTEX_SIZE;
        let vertex = &data[offset..offset + VERTEX_SIZE];

        vertices.push(Vertex {
            position: slice_to_vec3(&vertex[0..12]),
            tex: TexCoord::from_bytes(&vertex[12..28].try_into().unwrap()),
            normal: slice_to_vec3(&vertex[28..40]),
            color: Rgba::from_slice(&vertex[40..44]),
        })
    }

    Ok(vertices.into_boxed_slice())
}

/// Parse the given data as a list of MeshVerts.
pub fn meshverts_from_data(data: &[u8]) -> Result<Box<[MeshVert]>> {
    if data.len() % 4 != 0 {
        return Err(ParseError::Invalid);
    }
    let length = data.len() / 4;

    let mut meshverts = Vec::with_capacity(length as usize);
    for n in 0..length {
        meshverts.push(slice_to_u32(&data[n * 4..(n + 1) * 4]))
    }

    Ok(meshverts.into_boxed_slice())
}

impl<T: CoordSystem> HasVertices<T> for Q3BspFile<T> {
    type VerticesIter<'a> = std::slice::Iter<'a, Vertex>;

    fn vertices_iter(&self) -> Self::VerticesIter<'_> {
        self.vertices.iter()
    }

    fn get_vertex(&self, index: u32) -> &Vertex {
        &self.vertices[index as usize]
    }
}

impl<T: CoordSystem> HasMeshVerts<T> for Q3BspFile<T> {
    type MeshVertsIter<'a> = std::slice::Iter<'a, MeshVert>;

    fn meshverts_iter(&self) -> Self::MeshVertsIter<'_> {
        self.meshverts.iter()
    }

    fn get_meshvert<'a>(&self, index: u32) -> MeshVert {
        self.meshverts[index as usize]
    }
}