1
0
Fork 0
mirror of https://codeberg.org/icewind/vbsp.git synced 2026-06-03 10:44:07 +02:00

more object convert work

This commit is contained in:
Robin Appelman 2020-06-28 15:04:23 +02:00
commit bdaa8fe6bc
3 changed files with 56 additions and 44 deletions

View file

@ -1,6 +1,5 @@
use main_error::MainError; use main_error::MainError;
use obj::{Group, IndexTuple, Obj, ObjData, Object, SimplePolygon}; use obj::{Group, IndexTuple, Obj, ObjData, Object, SimplePolygon};
use vbsp::TextureFlags;
fn main() -> Result<(), MainError> { fn main() -> Result<(), MainError> {
let mut args = std::env::args(); let mut args = std::env::args();
@ -8,58 +7,40 @@ fn main() -> Result<(), MainError> {
let data = std::fs::read(args.next().expect("No demo file provided"))?; let data = std::fs::read(args.next().expect("No demo file provided"))?;
let bsp = vbsp::Bsp::read(&data)?; let bsp = vbsp::Bsp::read(&data)?;
let exclude_faces = TextureFlags::LIGHT let vertices: Vec<_> = bsp
| TextureFlags::SKY2D
| TextureFlags::SKY
| TextureFlags::WARP
| TextureFlags::TRANS
| TextureFlags::TRIGGER
| TextureFlags::HINT
| TextureFlags::SKIP
| TextureFlags::NODRAW
| TextureFlags::HITBOX;
let vertices = bsp
.vertices .vertices
.iter() .iter()
.map(|vertex| <[f32; 3]>::from(&vertex.position)) .map(|vertex| <[f32; 3]>::from(vertex.position))
.collect(); .collect();
let objects = bsp let world_model = bsp.models().next().unwrap();
.models()
.next() // only do "worldspawn" for now let world_polygons = world_model
.into_iter() .faces()
.map(|model| Group { .filter(|face| face.is_visible())
.map(|face| {
face.vertex_indexes()
.map(|vertex_index| IndexTuple(vertex_index as usize, None, None))
.collect()
})
.map(SimplePolygon)
.collect();
let world_object = Object {
name: "".to_string(),
groups: vec![Group {
name: "".to_string(), name: "".to_string(),
index: 0, index: 0,
material: None, material: None,
polys: model polys: world_polygons,
.faces() }],
.filter(|face| { };
face.texture()
.map(|texture| !texture.flags.intersects(exclude_faces))
.unwrap_or_default()
})
.map(|face| {
SimplePolygon(
face.vertex_indexes()
.map(|vertex| IndexTuple(vertex as usize, None, None))
.collect(),
)
})
.collect(),
})
.map(|group| Object {
name: "".to_string(),
groups: vec![group],
})
.collect();
let obj_data = ObjData { let obj_data = ObjData {
position: vertices, position: vertices,
texture: Vec::new(), texture: Vec::new(),
normal: Vec::new(), normal: Vec::new(),
objects, objects: vec![world_object],
material_libs: Vec::new(), material_libs: Vec::new(),
}; };

View file

@ -9,7 +9,7 @@ use std::fmt;
use std::io::{Error, ErrorKind}; use std::io::{Error, ErrorKind};
use std::iter::once; use std::iter::once;
use std::mem::size_of; use std::mem::size_of;
use std::ops::Index; use std::ops::{Add, Index};
#[derive(Clone)] #[derive(Clone)]
pub struct Directories { pub struct Directories {
@ -216,7 +216,7 @@ impl BinRead for Name {
} }
} }
#[derive(Debug, Clone, BinRead)] #[derive(Debug, Clone, Copy, BinRead)]
pub struct Vector { pub struct Vector {
pub x: f32, pub x: f32,
pub y: f32, pub y: f32,
@ -229,6 +229,18 @@ impl Vector {
} }
} }
impl Add<Vector> for Vector {
type Output = Vector;
fn add(self, rhs: Vector) -> Self::Output {
Vector {
x: self.x + rhs.x,
y: self.y + rhs.y,
z: self.z + rhs.z,
}
}
}
impl From<Vector> for [f32; 3] { impl From<Vector> for [f32; 3] {
fn from(vector: Vector) -> Self { fn from(vector: Vector) -> Self {
[vector.x, vector.y, vector.z] [vector.x, vector.y, vector.z]

View file

@ -1,5 +1,5 @@
mod bspfile; mod bspfile;
mod data; pub mod data;
mod reader; mod reader;
use crate::bspfile::LumpType; use crate::bspfile::LumpType;
@ -371,6 +371,25 @@ impl<'a> Handle<'a, Face> {
EdgeDirection::LastToFirst => edge.end_index, EdgeDirection::LastToFirst => edge.end_index,
}) })
} }
pub fn is_visible(&self) -> bool {
self.texture()
.map(|texture| {
!texture.flags.intersects(
TextureFlags::LIGHT
| TextureFlags::SKY2D
| TextureFlags::SKY
| TextureFlags::WARP
| TextureFlags::TRANS
| TextureFlags::TRIGGER
| TextureFlags::HINT
| TextureFlags::SKIP
| TextureFlags::NODRAW
| TextureFlags::HITBOX,
)
})
.unwrap_or_default()
}
} }
impl Handle<'_, Node> { impl Handle<'_, Node> {