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

texture work

This commit is contained in:
Robin Appelman 2023-12-09 20:25:33 +01:00
commit f1ead50800
4 changed files with 112 additions and 14 deletions

View file

@ -1,9 +1,10 @@
use super::Handle;
use crate::data::*;
use itertools::Either;
impl<'a> Handle<'a, Face> {
/// Get the texture of the face
pub fn texture(&self) -> Handle<TextureInfo> {
pub fn texture(&self) -> Handle<'a, TextureInfo> {
self.bsp
.textures_info
.get(self.texture_info as usize)
@ -79,4 +80,14 @@ impl<'a> Handle<'a, Face> {
pub fn displacement(&self) -> Option<Handle<'a, DisplacementInfo>> {
self.bsp.displacement(self.displacement_info as usize)
}
/// Get the vertex positions for the face
///
/// This either calculates the displacement or normal triangulation depending on the face
pub fn vertex_positions(&self) -> impl Iterator<Item = Vector> + 'a {
self.displacement()
.map(|displacement| displacement.triangulated_displaced_vertices())
.map(Either::Left)
.unwrap_or_else(|| Either::Right(self.triangulate().flatten()))
}
}

View file

@ -4,7 +4,10 @@ mod game;
use crate::data::*;
use crate::Bsp;
use ahash::RandomState;
use std::fmt::{Debug, Formatter};
use std::hash::BuildHasher;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
/// A handle represents a data structure in the bsp file and the bsp file containing it.
@ -63,18 +66,6 @@ impl<'a> Handle<'a, Model> {
}
}
impl<'a> Handle<'a, TextureInfo> {
/// Get the texture data references by the texture
pub fn texture(&self) -> Handle<'a, TextureData> {
let texture = self
.bsp
.textures_data
.get(self.data.texture_data_index as usize)
.unwrap();
Handle::new(self.bsp, texture)
}
}
impl Handle<'_, Node> {
/// Get the plane splitting this node
pub fn plane(&self) -> Handle<'_, Plane> {
@ -130,6 +121,25 @@ impl<'a> Handle<'a, TextureInfo> {
pub fn name(&self) -> &'a str {
self.texture_data().name()
}
/// Get a color that is unique but determistic for this texture
pub fn debug_color(&self) -> [u8; 3] {
self.texture_data().debug_color()
}
pub fn u(&self, pos: Vector) -> f32 {
(self.texture_scale[0] * pos.x
+ self.texture_scale[1] * pos.y
+ self.texture_scale[2] * pos.z)
/ self.texture_data().height as f32
}
pub fn v(&self, pos: Vector) -> f32 {
(self.texture_transform[0] * pos.x
+ self.texture_transform[1] * pos.y
+ self.texture_transform[2] * pos.z)
/ self.texture_data().width as f32
}
}
impl<'a> Handle<'a, TextureData> {
@ -142,4 +152,12 @@ impl<'a> Handle<'a, TextureData> {
part
}
}
/// Get a color that is unique but determistic for this texture
pub fn debug_color(&self) -> [u8; 3] {
let mut name_hasher = RandomState::with_seeds(0, 0, 0, 0).build_hasher();
self.name().hash(&mut name_hasher);
let name_hash = name_hasher.finish().to_be_bytes();
[name_hash[0], name_hash[1], name_hash[2]]
}
}