summaryrefslogtreecommitdiff
path: root/tools/asset_packer.cc
diff options
context:
space:
mode:
Diffstat (limited to 'tools/asset_packer.cc')
-rw-r--r--tools/asset_packer.cc70
1 files changed, 45 insertions, 25 deletions
diff --git a/tools/asset_packer.cc b/tools/asset_packer.cc
index 42dfa7a..0d26cf6 100644
--- a/tools/asset_packer.cc
+++ b/tools/asset_packer.cc
@@ -2,16 +2,16 @@
// It implements the asset packer tool for demoscene resource management.
// Converts external files into embedded C++ byte arrays and look-up records.
-#include <cstdio> // for simplicity, use fprintf() for output generation
+#include <algorithm> // For std::count
+#include <cmath>
+#include <cstdio> // for simplicity, use fprintf() for output generation
+#include <cstring> // For std::memcpy
#include <fstream>
#include <map>
-#include <algorithm> // For std::count
-#include <cstring> // For std::memcpy
#include <regex> // For std::regex
#include <stdexcept> // For std::stof exceptions
#include <string>
#include <vector>
-#include <cmath>
#define STB_IMAGE_IMPLEMENTATION
#define STBI_NO_LINEAR // Don't apply gamma correction, we want raw bytes
@@ -63,16 +63,29 @@ struct AssetBuildInfo {
struct Vec3 {
float x, y, z;
- Vec3 operator+(const Vec3& o) const { return {x + o.x, y + o.y, z + o.z}; }
- Vec3 operator+=(const Vec3& o) { x += o.x; y += o.y; z += o.z; return *this; }
- Vec3 operator-(const Vec3& o) const { return {x - o.x, y - o.y, z - o.z}; }
- Vec3 operator*(float s) const { return {x * s, y * s, z * s}; }
+ Vec3 operator+(const Vec3& o) const {
+ return {x + o.x, y + o.y, z + o.z};
+ }
+ Vec3 operator+=(const Vec3& o) {
+ x += o.x;
+ y += o.y;
+ z += o.z;
+ return *this;
+ }
+ Vec3 operator-(const Vec3& o) const {
+ return {x - o.x, y - o.y, z - o.z};
+ }
+ Vec3 operator*(float s) const {
+ return {x * s, y * s, z * s};
+ }
static Vec3 cross(const Vec3& a, const Vec3& b) {
- return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x};
+ return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z,
+ a.x * b.y - a.y * b.x};
}
Vec3 normalize() const {
float len = std::sqrt(x * x + y * y + z * z);
- if (len > 1e-6f) return {x / len, y / len, z / len};
+ if (len > 1e-6f)
+ return {x / len, y / len, z / len};
return {0, 0, 0};
}
};
@@ -359,26 +372,29 @@ int main(int argc, char* argv[]) {
int idx0 = face.v[0] - 1;
int idx1 = face.v[1] - 1;
int idx2 = face.v[2] - 1;
-
+
if (idx0 >= 0 && idx1 >= 0 && idx2 >= 0) {
- Vec3 p0 = {v_pos[idx0 * 3], v_pos[idx0 * 3 + 1], v_pos[idx0 * 3 + 2]};
- Vec3 p1 = {v_pos[idx1 * 3], v_pos[idx1 * 3 + 1], v_pos[idx1 * 3 + 2]};
- Vec3 p2 = {v_pos[idx2 * 3], v_pos[idx2 * 3 + 1], v_pos[idx2 * 3 + 2]};
-
+ Vec3 p0 = {v_pos[idx0 * 3], v_pos[idx0 * 3 + 1],
+ v_pos[idx0 * 3 + 2]};
+ Vec3 p1 = {v_pos[idx1 * 3], v_pos[idx1 * 3 + 1],
+ v_pos[idx1 * 3 + 2]};
+ Vec3 p2 = {v_pos[idx2 * 3], v_pos[idx2 * 3 + 1],
+ v_pos[idx2 * 3 + 2]};
+
Vec3 normal = Vec3::cross(p1 - p0, p2 - p0).normalize();
temp_normals[idx0] += normal;
temp_normals[idx1] += normal;
temp_normals[idx2] += normal;
}
}
-
+
for (const auto& n : temp_normals) {
Vec3 normalized = n.normalize();
v_norm.push_back(normalized.x);
v_norm.push_back(normalized.y);
v_norm.push_back(normalized.z);
}
-
+
// Assign generated normals to faces
for (auto& face : raw_faces) {
face.vn[0] = face.v[0];
@@ -392,12 +408,13 @@ int main(int argc, char* argv[]) {
for (int i = 0; i < 3; ++i) {
// Reconstruct key string for uniqueness check
char key_buf[128];
- std::snprintf(key_buf, sizeof(key_buf), "%d/%d/%d", face.v[i], face.vt[i], face.vn[i]);
+ std::snprintf(key_buf, sizeof(key_buf), "%d/%d/%d", face.v[i],
+ face.vt[i], face.vn[i]);
std::string key = key_buf;
if (vertex_map.find(key) == vertex_map.end()) {
vertex_map[key] = (uint32_t)final_vertices.size();
-
+
Vertex v = {};
if (face.v[i] > 0) {
v.p[0] = v_pos[(face.v[i] - 1) * 3];
@@ -419,11 +436,12 @@ int main(int argc, char* argv[]) {
}
}
- // Format: [num_vertices(u32)][Vertex * num_vertices][num_indices(u32)][uint32_t
+ // Format: [num_vertices(u32)][Vertex *
+ // num_vertices][num_indices(u32)][uint32_t
// * num_indices]
- buffer.resize(sizeof(uint32_t) + final_vertices.size() * sizeof(Vertex) +
- sizeof(uint32_t) +
- final_indices.size() * sizeof(uint32_t));
+ buffer.resize(
+ sizeof(uint32_t) + final_vertices.size() * sizeof(Vertex) +
+ sizeof(uint32_t) + final_indices.size() * sizeof(uint32_t));
uint8_t* out_ptr = buffer.data();
*reinterpret_cast<uint32_t*>(out_ptr) = (uint32_t)final_vertices.size();
out_ptr += sizeof(uint32_t);
@@ -500,9 +518,11 @@ int main(int argc, char* argv[]) {
fprintf(assets_data_cc_file, " return %zu;\n", asset_build_infos.size());
fprintf(assets_data_cc_file, "}\n\n");
- fprintf(assets_data_cc_file, "AssetId GetAssetIdByName(const char* name) {\n");
+ fprintf(assets_data_cc_file,
+ "AssetId GetAssetIdByName(const char* name) {\n");
for (const auto& info : asset_build_infos) {
- fprintf(assets_data_cc_file, " if (std::strcmp(name, \"%s\") == 0) return AssetId::ASSET_%s;\n",
+ fprintf(assets_data_cc_file,
+ " if (std::strcmp(name, \"%s\") == 0) return AssetId::ASSET_%s;\n",
info.name.c_str(), info.name.c_str());
}
fprintf(assets_data_cc_file, " return AssetId::ASSET_LAST_ID;\n");