534 lines
9.9 KiB
D
534 lines
9.9 KiB
D
module dlib.packer;
|
|
|
|
import includes;
|
|
import dlib.util;
|
|
import dlib.aliases;
|
|
import dlib.assets;
|
|
|
|
import std.stdio;
|
|
import std.string;
|
|
import std.file;
|
|
import std.path;
|
|
import std.traits;
|
|
import std.algorithm.comparison;
|
|
import core.memory;
|
|
import std.conv;
|
|
import std.array;
|
|
|
|
AssetType[string] Lookup = [
|
|
".obj": AT.ModelObj,
|
|
".png": AT.Texture,
|
|
".jpg": AT.Texture,
|
|
".spv": AT.Shader,
|
|
];
|
|
|
|
struct Texture
|
|
{
|
|
string name;
|
|
u8[] data;
|
|
u32 w;
|
|
u32 h;
|
|
u32 ch;
|
|
}
|
|
|
|
struct MeshGroup
|
|
{
|
|
u32 start;
|
|
u32 length;
|
|
}
|
|
|
|
struct MeshIdx
|
|
{
|
|
union
|
|
{
|
|
struct
|
|
{
|
|
u32 v, n, uv;
|
|
};
|
|
u32[3] arr;
|
|
};
|
|
}
|
|
|
|
struct Model
|
|
{
|
|
Vertex[] v;
|
|
u32[] idx;
|
|
MaterialData[] mats;
|
|
MeshGroup[] meshes;
|
|
}
|
|
|
|
u64 g_asset_count = 0;
|
|
string[] g_file_names = [];
|
|
Texture[] g_model_textures = [];
|
|
|
|
/**************************************************
|
|
****** UPDATE FILE_VERSION AFTER CHANGES !! ******
|
|
**************************************************/
|
|
|
|
void main(string[] argv)
|
|
{
|
|
Log("running");
|
|
if(isDir("build"))
|
|
{
|
|
chdir("build");
|
|
}
|
|
|
|
bool pack = false;
|
|
bool font = false;
|
|
string font_file;
|
|
|
|
for(u64 i = 0; i < argv.length; i += 1)
|
|
{
|
|
if(argv[i] == "-pack")
|
|
{
|
|
pack = true;
|
|
}
|
|
}
|
|
|
|
if(pack)
|
|
{
|
|
PackFile();
|
|
TestFile();
|
|
}
|
|
}
|
|
|
|
void
|
|
PackFile()
|
|
{
|
|
File ap = File("./assets.sgp", "wb");
|
|
scope(exit)
|
|
{
|
|
ap.flush();
|
|
ap.close();
|
|
chdir("../build");
|
|
}
|
|
|
|
chdir("../assets");
|
|
|
|
foreach(string file; dirEntries(".", SpanMode.depth))
|
|
{
|
|
if (isDir(file)) continue;
|
|
|
|
g_file_names ~= file;
|
|
g_asset_count += 1;
|
|
}
|
|
|
|
FileHeader h = InitHeader(g_asset_count);
|
|
ap.rawWrite([h]);
|
|
|
|
u64 offset = FileHeader.sizeof + (AssetInfo.sizeof * g_asset_count);
|
|
AssetInfo[] asset_info;
|
|
foreach(file; g_file_names)
|
|
{
|
|
AssetType type = AT.None;
|
|
foreach(extension, t; Lookup)
|
|
{
|
|
if (file.endsWith(extension))
|
|
{
|
|
type = t;
|
|
break;
|
|
}
|
|
}
|
|
|
|
assert(type != AT.None, "Asset Type is none, offending file " ~ file);
|
|
|
|
auto f = File(file, "rb");
|
|
|
|
u64 length = cast(u64)f.size();
|
|
|
|
string base_name = chompPrefix(file, "./");
|
|
AssetInfo info = {
|
|
hash: Hash(base_name),
|
|
offset: offset,
|
|
length: length,
|
|
type: type,
|
|
};
|
|
|
|
auto data = f.rawRead(new u8[length]);
|
|
|
|
assert(length == data.length, "rawRead failure: data length returned doesn't match");
|
|
|
|
ap.seek(offset);
|
|
ap.rawWrite(data);
|
|
|
|
offset += length;
|
|
|
|
asset_info ~= info;
|
|
|
|
f.close();
|
|
}
|
|
|
|
ap.seek(FileHeader.sizeof);
|
|
ap.rawWrite(asset_info);
|
|
}
|
|
|
|
TexMeta
|
|
GetTexMeta(u8[] data)
|
|
{
|
|
int ch = 4;
|
|
int width, height, has_ch;
|
|
auto img = stbi_load_from_memory(data.ptr, cast(int)data.length, &width, &height, &has_ch, ch);
|
|
|
|
assert(img != null, "stbi_load_from_image failure: image is NULL");
|
|
assert(width > 0 && height > 0 && has_ch > 0, "stbi_load_from_image failure: dimensions are invalid");
|
|
|
|
stbi_image_free(img);
|
|
|
|
return TexMeta(w: width, h: height, ch: has_ch);
|
|
}
|
|
|
|
void
|
|
TestFile()
|
|
{
|
|
File ap = File("assets.sgp", "rb");
|
|
scope(exit)
|
|
{
|
|
ap.flush();
|
|
ap.close();
|
|
chdir("../build");
|
|
}
|
|
|
|
FileHeader file_header = ap.rawRead(new FileHeader[1])[0];
|
|
FileHeader test_header = InitHeader(g_asset_count);
|
|
assert(file_header == test_header, "TestFile failure: Header is incorrect");
|
|
|
|
AssetInfo[] file_info = ap.rawRead(new AssetInfo[g_asset_count]);
|
|
assert(file_info.length == file_header.asset_count, "TestFile failure: Incorrect AssetInfo length returned");
|
|
|
|
chdir("../assets");
|
|
|
|
u64 asset_index = 0;
|
|
foreach(i, file; g_file_names)
|
|
{
|
|
scope(exit) asset_index += 1;
|
|
|
|
AssetInfo* info = file_info.ptr + asset_index;
|
|
File asset = File(file, "rb");
|
|
|
|
u8[] data = asset.rawRead(new u8[asset.size()]);
|
|
assert(data.length == info.length, "TestFile failure: File length read is incorrect");
|
|
|
|
string base_name = chompPrefix(file, "./");
|
|
assert(Hash(base_name) == info.hash, "TestFile failure: File hash is incorrect");
|
|
|
|
ap.seek(info.offset);
|
|
u8[] pack_data = ap.rawRead(new u8[info.length]);
|
|
assert(equal!((a, b) => a == b)(data[], pack_data[]), "TestFile failure: Asset data does not match file data");
|
|
}
|
|
}
|
|
|
|
static u32
|
|
MagicValue(string str)
|
|
{
|
|
assert(str.length == 4, "Magic value must 4 characters");
|
|
return cast(u32)(cast(u32)(str[0] << 24) | cast(u32)(str[1] << 16) | cast(u32)(str[2] << 8) | cast(u32)(str[3] << 0));
|
|
}
|
|
|
|
static FileHeader
|
|
InitHeader(u64 asset_count)
|
|
{
|
|
FileHeader header = {
|
|
magic: MagicValue("steg"),
|
|
file_version: FILE_VERSION,
|
|
asset_count: asset_count,
|
|
asset_info_offset: FileHeader.sizeof,
|
|
};
|
|
|
|
return header;
|
|
}
|
|
|
|
static ModelHeader
|
|
InitModelHeader()
|
|
{
|
|
ModelHeader header = {
|
|
magic: MagicValue("stgm"),
|
|
model_version: MODEL_VERSION,
|
|
};
|
|
|
|
return header;
|
|
}
|
|
|
|
string[][]
|
|
TokenizeLines(u8[] data)
|
|
{
|
|
string[][] tokens = [];
|
|
string[] line_tokens = [];
|
|
u64 start = -1;
|
|
for(u64 i = 0; i < data.length; i += 1)
|
|
{
|
|
if(i64(start) != -1 && CheckWhiteSpace(data[i]))
|
|
{
|
|
line_tokens ~= ConvString(data[start .. i]);
|
|
start = -1;
|
|
}
|
|
|
|
if(data[i] == '\n')
|
|
{
|
|
tokens ~= line_tokens;
|
|
line_tokens = [];
|
|
|
|
continue;
|
|
}
|
|
|
|
if(i64(start) == -1 && !CheckWhiteSpace(data[i]))
|
|
{
|
|
start = i;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
return tokens;
|
|
}
|
|
|
|
u8[]
|
|
OpenFile(string file_name)
|
|
{
|
|
File f = File(file_name, "rb");
|
|
u8[] data = new u8[f.size()];
|
|
f.rawRead(data);
|
|
f.close();
|
|
|
|
return data;
|
|
}
|
|
|
|
Model
|
|
ConvertObj(string file_name)
|
|
{
|
|
import std.array;
|
|
|
|
u8[] data = OpenFile(file_name);
|
|
|
|
Model model;
|
|
|
|
u64 vcount, uvcount, ncount, fcount, gcount, mcount;
|
|
|
|
string[][] tokens = TokenizeLines(data);
|
|
|
|
for(u64 i = 0; i < tokens.length; i += 1)
|
|
{
|
|
if(tokens[i].length == 0) continue;
|
|
|
|
switch(tokens[i][0])
|
|
{
|
|
case "v": vcount += 1; break;
|
|
case "vt": uvcount += 1; break;
|
|
case "vn": ncount += 1; break;
|
|
case "f": fcount += 1; break;
|
|
case "g": gcount += 1; break;
|
|
case "usemtl": mcount += 1; break;
|
|
default: break;
|
|
}
|
|
}
|
|
|
|
Vec3[] positions = new Vec3[vcount];
|
|
Vec3[] normals = new Vec3[ncount];
|
|
Vec2[] uvs = new Vec2[uvcount];
|
|
MeshGroup[] groups = new MeshGroup[gcount];
|
|
Vertex[] vtx = new Vertex[fcount];
|
|
MeshIdx[][] idx = [];
|
|
MaterialData[] mtls = [];
|
|
|
|
vcount = 0;
|
|
ncount = 0;
|
|
uvcount = 0;
|
|
|
|
MeshIdx[] part_idx = [];
|
|
for(u64 i = 0; i < tokens.length; i += 1)
|
|
{
|
|
if(tokens[i][0] == "#") continue;
|
|
|
|
if(tokens[i][0] == "v")
|
|
{
|
|
if(tokens[i].length < 4) assert(false, "OBJ file error, not enough points for case [v]");
|
|
|
|
positions[vcount++] = Vec3(ToF32(tokens[i][1]), ToF32(tokens[i][2]), ToF32(tokens[i][3]));
|
|
continue;
|
|
}
|
|
|
|
if(tokens[i][0] == "vn")
|
|
{
|
|
if(tokens[i].length < 4) assert(false, "OBJ file error, not enough points for case [vn]");
|
|
|
|
normals[ncount++] = Vec3(ToF32(tokens[i][1]), ToF32(tokens[i][2]), ToF32(tokens[i][3]));
|
|
continue;
|
|
}
|
|
|
|
if(tokens[i][0] == "vt")
|
|
{
|
|
if(tokens[i].length < 3) assert(false, "OBJ file error, not enough points for case [vt]");
|
|
|
|
uvs[uvcount++] = Vec2(ToF32(tokens[i][1]), ToF32(tokens[i][2]));
|
|
continue;
|
|
}
|
|
|
|
if(tokens[i][0] == "f")
|
|
{
|
|
u32 sep_count = StrCharCount(tokens[i][1], '/');
|
|
|
|
if(tokens[i].length == 4)
|
|
{
|
|
for(u64 j = 1; j < tokens[i].length; j += 1)
|
|
{
|
|
string[] parts = tokens[i][j].split('/');
|
|
|
|
if(sep_count == 0)
|
|
{
|
|
part_idx ~= MeshIdx(v: to!u32(parts[0]));
|
|
}
|
|
if(sep_count == 1)
|
|
{
|
|
part_idx ~= MeshIdx(v: to!u32(parts[0]), uv: to!u32(parts[1]));
|
|
}
|
|
if(sep_count == 2)
|
|
{
|
|
MeshIdx mesh_idx;
|
|
foreach(ipart, part; parts)
|
|
{
|
|
if(part == "") continue;
|
|
mesh_idx.arr[ipart] = to!u32(part);
|
|
}
|
|
|
|
part_idx ~= mesh_idx;
|
|
}
|
|
}
|
|
}
|
|
else assert(false, "Only triangles or quads supported for mesh face");
|
|
|
|
continue;
|
|
}
|
|
|
|
if(tokens[i][0] == "g" && part_idx.length > 0)
|
|
{
|
|
idx ~= part_idx;
|
|
part_idx = [];
|
|
continue;
|
|
}
|
|
|
|
if(tokens[i][0] == "mtllib")
|
|
{
|
|
u8[] mtl_data = OpenFile(GetFilePath(file_name) ~ tokens[i][1]);
|
|
|
|
MaterialData* mtl = null;
|
|
string[][] mtl_tokens = TokenizeLines(mtl_data);
|
|
for(u64 j = 0; j < mtl_tokens.length; j += 1)
|
|
{
|
|
if(mtl_tokens[j].length == 0)
|
|
{
|
|
if(mtl)
|
|
{
|
|
mtls ~= *mtl;
|
|
mtl = null;
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
if(mtl_tokens[j][0] == "newmtl")
|
|
{
|
|
mtl = new MaterialData;
|
|
mtl.name = mtl_tokens[j][1];
|
|
continue;
|
|
}
|
|
|
|
if(!mtl) continue;
|
|
|
|
switch(mtl_tokens[j][0])
|
|
{
|
|
case "Ka", "Kd", "Ks", "Tf", "Ke":
|
|
{
|
|
mtl.colors[GetMatColor(mtl_tokens[j][0])] = Vec3(ToF32(mtl_tokens[j][1]), ToF32(mtl_tokens[j][2]), ToF32(mtl_tokens[j][3]));
|
|
} break;
|
|
case "Ns", "d", "Tr", "Ni", "Pr", "Pm", "Pc", "Pcr", "aniso", "Ps":
|
|
{
|
|
f32 v = ToF32(mtl_tokens[j][1]);
|
|
if(mtl_tokens[j][1] == "Tr")
|
|
{
|
|
v = 1.0 - v;
|
|
}
|
|
|
|
mtl.props[GetMatFloat(mtl_tokens[j][0])] = v;
|
|
} break;
|
|
case "map_Ka", "map_Kd", "map_Ks", "map_Ns", "map_d", "map_bump", "anisor",
|
|
"bump", "map_Pr", "map_Pm", "map_Ke", "map_Ps", "norm", "disp", "decal":
|
|
{
|
|
mtl.maps[GetMatMap(mtl_tokens[j][0])] = mtl_tokens[j][1];
|
|
} break;
|
|
case "illum":
|
|
{
|
|
mtl.illum = cast(IllumModel)(to!(u32)(mtl_tokens[j][1]));
|
|
} break;
|
|
default: break;
|
|
}
|
|
}
|
|
|
|
if(mtl)
|
|
{
|
|
mtls ~= *mtl;
|
|
mtl = null;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
assert(mtls[mtls.length-1].name == "sp_zid_vani");
|
|
|
|
return model;
|
|
}
|
|
|
|
string
|
|
GetFilePath(string file_name)
|
|
{
|
|
string result = file_name;
|
|
|
|
for(u64 i = file_name.length-1; i64(i) >= 0; i -= 1)
|
|
{
|
|
version(Windows)
|
|
{
|
|
char ch = '\\';
|
|
}
|
|
else
|
|
{
|
|
char ch = '/';
|
|
}
|
|
|
|
if(file_name[i] == ch)
|
|
{
|
|
result = file_name[0 .. i+1];
|
|
break;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
pragma(inline) f32
|
|
ToF32(string str)
|
|
{
|
|
return to!(f32)(str);
|
|
}
|
|
|
|
pragma(inline) string
|
|
ConvString(u8[] bytes)
|
|
{
|
|
return (cast(immutable(char)*)bytes.ptr)[0 .. bytes.length];
|
|
}
|
|
|
|
pragma(inline) bool
|
|
CheckWhiteSpace(u8 ch)
|
|
{
|
|
return ch == ' ' ||
|
|
ch == '\t'||
|
|
ch == '\n'||
|
|
ch == 0x0D||
|
|
ch == 0x0A||
|
|
ch == 0x0B||
|
|
ch == 0x0C;
|
|
}
|
|
|
|
unittest
|
|
{
|
|
{ // Obj test
|
|
Model model = ConvertObj("./test/sponza.obj");
|
|
}
|
|
}
|