Skip to content

Commit

Permalink
convert: palette generation and materials in objects
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Dec 14, 2023
1 parent 11c335a commit 8302320
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
12 changes: 12 additions & 0 deletions bin/convert/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ fn main() {
let level_data = layers.export();
level_data.save_vmp(&dst_path);
}
("pal", "mtl") => {
println!("\tConverting object palette to MTL...");
let palette_raw = fs_read(src_path).expect("Unable to open palette");
let palette: Vec<u8> = vangers::render::object::COLOR_TABLE
.iter()
.flat_map(|&range| {
let texel = range[0] as usize + (128 >> range[1]) as usize;
palette_raw[texel * 3 - 3..texel * 3].iter().cloned()
})
.collect();
model_obj::save_palette(dst_path, &palette).unwrap();
}
("pal", "png") => {
println!("Converting palette to PNG...");
let data = fs_read(&src_path).unwrap();
Expand Down
27 changes: 24 additions & 3 deletions bin/convert/model_obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ use obj::{IndexTuple, Obj};
use std::path::Path;
use std::{
fs,
io::{Result as IoResult, Write},
io::{Result as IoResult, Write as _},
path::PathBuf,
};

type RefModel = Model<Mesh<String>, Mesh<String>>;
type RefAnimatedMesh = AnimatedMesh<String>;
type DrawAnimatedMesh = AnimatedMesh<Geometry<DrawTriangle>>;

const MAT_NAME: &'static str = "object.mtl";

pub fn export_m3d(full: FullModel, model_path: &Path) {
const BODY_PATH: &str = "body.obj";
const SHAPE_PATH: &str = "body-shape.obj";
Expand Down Expand Up @@ -191,8 +193,26 @@ fn flatten_pos(poly: &[IndexTuple], positions: &[[f32; 3]]) -> [i8; 3] {
]
}

pub fn save_palette(path: PathBuf, palette: &[u8]) -> IoResult<()> {
if path.file_name().unwrap() != MAT_NAME {
log::warn!("Saved material is different from the expected {}", MAT_NAME);
}
let mut dest = fs::File::create(&path)?;
for (color_id, color) in palette.chunks(3).enumerate() {
writeln!(dest, "newmtl {:?}", map_color_id(color_id as u32))?;
writeln!(
dest,
"\tKd {} {} {}",
color[0] as f32 / 255.0,
color[1] as f32 / 255.0,
color[2] as f32 / 255.0,
)?;
}
Ok(())
}

pub fn save_draw_geometry(geom: &Geometry<DrawTriangle>, path: PathBuf) -> IoResult<()> {
let mut dest = fs::File::create(&path).unwrap();
let mut dest = fs::File::create(&path)?;
for p in geom.positions.iter() {
writeln!(dest, "v {} {} {}", p[0], p[1], p[2])?;
}
Expand All @@ -208,6 +228,7 @@ pub fn save_draw_geometry(geom: &Geometry<DrawTriangle>, path: PathBuf) -> IoRes
}
writeln!(dest)?;

writeln!(dest, "mtllib {}", MAT_NAME)?;
let mut mask = 0u32;
for p in &geom.polygons {
mask |= 1 << p.material[0];
Expand All @@ -216,7 +237,7 @@ pub fn save_draw_geometry(geom: &Geometry<DrawTriangle>, path: PathBuf) -> IoRes
if mask & (1 << color_id) == 0 {
continue;
}
writeln!(dest, "g {:?}", map_color_id(color_id))?;
writeln!(dest, "usemtl {:?}", map_color_id(color_id))?;
for p in &geom.polygons {
if p.material[0] != color_id {
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/render/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use m3d::NUM_COLOR_IDS;

use std::{mem, slice};

const COLOR_TABLE: [[u8; 2]; NUM_COLOR_IDS as usize] = [
pub const COLOR_TABLE: [[u8; 2]; NUM_COLOR_IDS as usize] = [
[0, 0], // reserved
[128, 3], // body
[176, 4], // window
Expand Down

0 comments on commit 8302320

Please sign in to comment.