1
0
Fork 0
mirror of https://codeberg.org/icewind/vbsp.git synced 2026-06-03 18:54:05 +02:00

node and leaf structures

This commit is contained in:
Robin Appelman 2020-06-26 18:39:23 +02:00
commit 130c2efc09
2 changed files with 53 additions and 37 deletions

View file

@ -7,6 +7,7 @@ use bv::BitVec;
use parse_display::Display;
use std::fmt;
use std::io::{Error, ErrorKind};
use std::iter::once;
use std::mem::size_of;
use std::ops::Index;
@ -222,6 +223,12 @@ pub struct Vector {
z: f32,
}
impl Vector {
pub fn iter(&self) -> impl Iterator<Item = f32> {
once(self.x).chain(once(self.y)).chain(once(self.z))
}
}
#[derive(Debug, Clone, BinRead)]
pub struct TextureInfo {
pub texture_scale: [f32; 4],
@ -253,24 +260,34 @@ pub struct Plane {
#[derive(Debug, Clone, BinRead)]
pub struct Node {
pub plane: u32,
pub plane_index: i32,
pub children: [i32; 2],
pub mins: [i32; 3],
pub maxs: [i32; 3],
pub mins: [i16; 3],
pub maxs: [i16; 3],
pub first_face: u16,
pub face_cound: u16,
pub area: i16,
pub padding: i16,
}
static_assertions::const_assert_eq!(size_of::<Node>(), 32);
#[derive(Debug, Clone, BinRead)]
pub struct Leaf {
pub cluster: i32,
pub area: u32,
pub mins: [i32; 3],
pub maxs: [i32; 3],
pub leaf_face: u32,
pub num_leaf_faces: u32,
pub leaf_brush: u32,
pub num_leaf_brushes: u32,
pub contents: i32,
pub cluster: i16,
pub area_and_flags: i16, // first 9 bits is area, last 7 bits is flags
pub mins: [i16; 3],
pub maxs: [i16; 3],
pub first_leaf_face: u16,
pub leaf_face_count: u16,
pub first_leaf_brush: u16,
pub leaf_brush_count: u16,
pub leaf_watter_data_id: i16,
}
static_assertions::const_assert_eq!(size_of::<Leaf>(), 32);
#[derive(Debug, Clone, BinRead)]
pub struct LeafBrush {
pub brush: u32,

View file

@ -107,7 +107,7 @@ impl Leaves {
// to explicitly specify the type.
pub fn clusters<'this>(
&'this self,
) -> GroupBy<i32, impl Iterator<Item = &'this Leaf>, impl FnMut(&&'this Leaf) -> i32> {
) -> GroupBy<i16, impl Iterator<Item = &'this Leaf>, impl FnMut(&&'this Leaf) -> i16> {
self.leaves.iter().group_by(|leaf: &&Leaf| leaf.cluster)
}
}
@ -284,28 +284,27 @@ impl Bsp {
self.models.iter().map(move |m| Handle::new(self, m))
}
pub fn leaf_at(&self, point: [f32; 3]) -> Option<Handle<'_, Leaf>> {
pub fn leaf_at(&self, point: Vector) -> Option<Handle<'_, Leaf>> {
let mut current = self.root_node()?;
None
// loop {
// let plane = current.plane()?;
// let dot: f32 = point
// .iter()
// .zip(plane.normal.iter())
// .map(|(a, b)| a * b)
// .sum();
//
// let [front, back] = current.children;
//
// let next = if dot < plane.dist { back } else { front };
//
// if next < 0 {
// return self.leaf((!next) as usize);
// } else {
// current = self.node(next as usize)?;
// }
// }
loop {
let plane = current.plane()?;
let dot: f32 = point
.iter()
.zip(plane.normal.iter())
.map(|(a, b)| a * b)
.sum();
let [front, back] = current.children;
let next = if dot < plane.dist { back } else { front };
if next < 0 {
return self.leaf((!next) as usize);
} else {
current = self.node(next as usize)?;
}
}
}
}
@ -365,7 +364,7 @@ impl<'a> Handle<'a, Face> {
impl Handle<'_, Node> {
pub fn plane(&self) -> Option<Handle<'_, Plane>> {
self.bsp.plane(self.plane as _)
self.bsp.plane(self.plane_index as _)
}
}
@ -398,8 +397,8 @@ impl<'a> Handle<'a, Leaf> {
}
pub fn faces(&self) -> impl Iterator<Item = Handle<'a, Face>> {
let start = self.leaf_face as usize;
let end = start + self.num_leaf_faces as usize;
let start = self.first_leaf_face as usize;
let end = start + self.leaf_face_count as usize;
let bsp = self.bsp;
bsp.leaf_faces[start..end]
.iter()