1
0
Fork 0
mirror of https://codeberg.org/icewind/vbsp.git synced 2026-06-03 10:44:07 +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

67
Cargo.lock generated
View file

@ -2,6 +2,19 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "ahash"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91429305e9f0a25f6205c5b8e0d2db09e0708a7a6df0f42212bb56c32c8ac97a"
dependencies = [
"cfg-if",
"getrandom",
"once_cell",
"version_check",
"zerocopy",
]
[[package]]
name = "approx"
version = "0.4.0"
@ -153,6 +166,17 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "835a3dc7d1ec9e75e2b5fb4ba75396837112d2060b03f7d43bc1897c7f7211da"
[[package]]
name = "getrandom"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe9006bed769170c11f845cf00c7c1e9092aeb3f268e007c3e760ac68008070f"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "hashbrown"
version = "0.14.2"
@ -169,6 +193,21 @@ dependencies = [
"hashbrown",
]
[[package]]
name = "itertools"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0"
dependencies = [
"either",
]
[[package]]
name = "libc"
version = "0.2.150"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c"
[[package]]
name = "lzma"
version = "0.2.2"
@ -420,11 +459,13 @@ checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
name = "vbsp"
version = "0.2.0"
dependencies = [
"ahash",
"arrayvec",
"binrw",
"bitflags",
"bv",
"cgmath",
"itertools",
"lzma-rs",
"main_error",
"num_enum",
@ -452,6 +493,12 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "winnow"
version = "0.5.19"
@ -461,6 +508,26 @@ dependencies = [
"memchr",
]
[[package]]
name = "zerocopy"
version = "0.7.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "306dca4455518f1f31635ec308b6b3e4eb1b11758cefafc782827d0aa7acb5c7"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.7.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be912bf68235a88fbefd1b73415cb218405958d1655b2ece9035a19920bdf6ba"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.39",
]
[[package]]
name = "zip-lzma"
version = "0.6.3"

View file

@ -23,6 +23,8 @@ num_enum = "0.7.1"
vbsp-derive = { path = "derive", version = "0.1.0", features = ["__vbsp_as_self"], optional = true }
cgmath = "0.18.0"
zip = { package = "zip-lzma", version = "0.6.3", default-features = false, features = ["lzma"] }
itertools = "0.12.0"
ahash = "0.8.6"
[dev-dependencies]
obj = "0.10"

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]]
}
}