Finished reworking asset builder; Models are now cooked when built

This commit is contained in:
grayson committed 2021-12-14 03:04:56 -06:00
1 parent cc0316ff80
commit 7a93befa14
20 files changed
+291 -475

No files matched your search

+2 -2
View File
@@ -62,6 +62,7 @@
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<AdditionalIncludeDirectories>$(ProjectDir)src\include;$(ProjectDir)dep\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@@ -79,6 +80,7 @@
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<AdditionalIncludeDirectories>$(ProjectDir)src\include;$(ProjectDir)dep\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@@ -91,13 +93,11 @@
<ItemGroup>
<ClCompile Include="src\main.cpp" />
<ClCompile Include="src\Models.cpp" />
<ClCompile Include="src\Textures.cpp" />
<ClCompile Include="src\Utility.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="resource.h" />
<ClInclude Include="src\include\Models.h" />
<ClInclude Include="src\include\Textures.h" />
<ClInclude Include="src\include\Utility.h" />
</ItemGroup>
<ItemGroup>
@@ -18,9 +18,6 @@
<ClCompile Include="src\main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Textures.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Models.cpp">
<Filter>Source Files</Filter>
</ClCompile>
@@ -32,9 +29,6 @@
<ClInclude Include="src\include\Utility.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\include\Textures.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\include\Models.h">
<Filter>Header Files</Filter>
</ClInclude>
File diff suppressed because it is too large. Load diff
+58 -22
View File
@@ -4,6 +4,7 @@
#include <sstream>
#include <filesystem>
#include <map>
#include <set>
void cookModel(std::string& OBJin, std::string& MTLin, std::string& out) {
std::vector<char> obj(&OBJin[0], &OBJin[0] + OBJin.size());
@@ -156,8 +157,18 @@ void cookModel(std::string& OBJin, std::string& MTLin, std::string& out) {
int matCount = 0;
std::string modelHeader;
int numMats = (int)mats.size();
modelHeader.append((char*)&numMats, sizeof(numMats));
std::vector<std::string> textures = getNeededTextures(MTLin);
std::string textureHeader;
if (textures.empty())
textureHeader = "none\n";
else
for (const auto& curr : textures)
textureHeader.append(curr + (std::string)"\n");
modelHeader.append(textureHeader);
modelHeader.append("/ENDTEXTURES\n");
modelHeader.append(std::to_string(mats.size()) + (std::string)"\n");
for (auto& m : mats) {
std::string curr = m.first;
@@ -192,13 +203,25 @@ void cookModel(std::string& OBJin, std::string& MTLin, std::string& out) {
TempMaterial& curr2 = *m.second;
//Serialize this
//curr2.diffuseColor.x, curr2.diffuseColor.y, curr2.diffuseColor.z, curr2.shininess
std::string textureIndices;
if (!curr2.diffuseTextureName.empty())
textureIndices.append(std::to_string((std::find(textures.begin(), textures.end(), curr2.diffuseTextureName) - textures.begin()) + 1) + (std::string)" ");
else
textureIndices.append(std::to_string(0) + (std::string)" ");
if (!curr2.specularTextureName.empty())
textureIndices.append(std::to_string((std::find(textures.begin(), textures.end(), curr2.specularTextureName) - textures.begin()) + 1) + (std::string)" ");
else
textureIndices.append(std::to_string(0) + (std::string)" ");
if (!curr2.normalTextureName.empty())
textureIndices.append(std::to_string((std::find(textures.begin(), textures.end(), curr2.normalTextureName) - textures.begin()) + 1) + (std::string)" ");
else
textureIndices.append(std::to_string(0) + (std::string)" ");
modelHeader.append((char*)&curr2.diffuseColor.x, sizeof(curr2.diffuseColor.x));
modelHeader.append((char*)&curr2.diffuseColor.y, sizeof(curr2.diffuseColor.y));
modelHeader.append((char*)&curr2.diffuseColor.z, sizeof(curr2.diffuseColor.z));
modelHeader.append((char*)&curr2.shininess, sizeof(curr2.shininess));
modelHeader.append(textureIndices);
modelHeader.append(std::to_string(curr2.diffuseColor.x) + (std::string)" ");
modelHeader.append(std::to_string(curr2.diffuseColor.y) + (std::string)" ");
modelHeader.append(std::to_string(curr2.diffuseColor.z) + (std::string)" ");
modelHeader.append(std::to_string(curr2.shininess) + (std::string)"\n");
size_t offset = vboPositions.size() / 3;
vboPositions.insert(vboPositions.end(), curr2.outVB.begin(), curr2.outVB.end());
@@ -212,20 +235,23 @@ void cookModel(std::string& OBJin, std::string& MTLin, std::string& out) {
delete m.second;
matCount++;
}
modelHeader.append("/ENDMATERIALS\n");
size_t posSize = vboPositions.size() * sizeof(float);
size_t tcSize = vboTexCoords.size() * sizeof(float);
size_t normSize = vboNormals.size() * sizeof(float);
size_t tanSize = vboTangents.size() * sizeof(float);
size_t matIndicesSize = vboMaterialIndices.size() * sizeof(int);
size_t indicesSize = vboIndices.size() * sizeof(unsigned int);
unsigned int posSize = (unsigned int)vboPositions.size() * sizeof(float);
unsigned int tcSize = (unsigned int)vboTexCoords.size() * sizeof(float);
unsigned int normSize = (unsigned int)vboNormals.size() * sizeof(float);
unsigned int tanSize = (unsigned int)vboTangents.size() * sizeof(float);
unsigned int matIndicesSize = (unsigned int)vboMaterialIndices.size() * sizeof(int);
unsigned int indicesSize = (unsigned int)vboIndices.size() * sizeof(unsigned int);
modelHeader.append((char*)&posSize, sizeof(posSize));
modelHeader.append((char*)&tcSize, sizeof(tcSize));
modelHeader.append((char*)&normSize, sizeof(normSize));
modelHeader.append((char*)&tanSize, sizeof(tanSize));
modelHeader.append((char*)&matIndicesSize, sizeof(matIndicesSize));
modelHeader.append((char*)&indicesSize, sizeof(indicesSize));
modelHeader.append(std::to_string(posSize) + (std::string)" ");
modelHeader.append(std::to_string(tcSize) + (std::string)" ");
modelHeader.append(std::to_string(normSize) + (std::string)" ");
modelHeader.append(std::to_string(tanSize) + (std::string)" ");
modelHeader.append(std::to_string(matIndicesSize) + (std::string)" ");
modelHeader.append(std::to_string(vboIndices.size()) + (std::string)"\n");
modelHeader.append("/ENDHEADER\n");
out.append(modelHeader);
out.append((char*)vboPositions.data(), posSize);
@@ -278,7 +304,8 @@ void parseMaterials(std::unordered_map<std::string, TempMaterial*>& mats, std::v
}
}
void getNeededImages(std::string mtl, std::set<std::string>& set) {
std::vector<std::string> getNeededTextures(std::string mtl) {
std::set<std::string> tex;
while (mtl.size()) {
size_t pos = mtl.find("map_");
if (pos == std::string::npos)
@@ -292,6 +319,15 @@ void getNeededImages(std::string mtl, std::set<std::string>& set) {
size_t pos2 = temp.find_last_of("/\\");
if (pos2 != std::string::npos)
temp = temp.substr(pos2 + 1);
set.insert(temp);
tex.insert(temp);
}
std::vector<std::string> out;
if (tex.empty())
return out;
else
for (const auto& curr : tex)
out.push_back(curr);
return out;
}
-18
View File
@@ -1,18 +0,0 @@
#include "Textures.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
void cookTexture(const std::string& in, std::string& out) {
stbi_set_flip_vertically_on_load(true);
int x, y, channels;
unsigned char* texture = stbi_load_from_memory((unsigned char*)&in[0], (int)in.size(), &x, &y, &channels, 0);
out.append((char*)&x, sizeof(x));
out.append((char*)&y, sizeof(y));
out.append((char*)&channels, sizeof(channels));
out.append((char*)texture, x * y * channels);
stbi_image_free(texture);
}
+3 -2
View File
@@ -56,8 +56,6 @@ void writePack(const std::string& filename, std::string& in) {
if (!out)
Error("Pack \"" + filename + (std::string)"\" could not be written!");
in.insert(0, "NFASSETPACK");
Log("Encrypting...");
for (unsigned int i = 0; i < in.size(); i++)
in[i] += 100;
@@ -80,5 +78,8 @@ std::string getNewLine(std::stringstream& stringstream) {
if (out[out.size() - 1] == '\r')
out.erase(out.size() - 1);
if (out[0] == ' ')
out = out.substr(1);
return out;
}
+1 -3
View File
@@ -1,6 +1,4 @@
#pragma once
#include <string>
#include <set>
#include <unordered_map>
#include <vector>
@@ -30,4 +28,4 @@ struct TempMaterial {
void cookModel(std::string& in, std::string& MTLin, std::string& out);
void parseMaterials(std::unordered_map<std::string, TempMaterial*>& mats, std::vector<char>& mtl);
void getNeededImages(std::string mtl, std::set<std::string>& set);
std::vector<std::string> getNeededTextures(std::string mtl);
-4
View File
@@ -1,4 +0,0 @@
#pragma once
#include <string>
void cookTexture(const std::string& in, std::string& out);
+30 -27
View File
@@ -1,25 +1,29 @@
#include <iostream>
#include <thread>
#include <filesystem>
#include <set>
#include <unordered_map>
#include <Windows.h>
#include <compressapi.h>
#include "Utility.h"
#include "Models.h"
#include "Textures.h"
[[noreturn]]
void printHelp() {
std::cout << "Nothin' Fancy Asset Builder\n\nThis tool creates .nfpack files for the NF engine to "
"load at runtime. A pack gets created for each directory in the working directory if there "
"are only compatible files inside. Subdirectories are not ignored.\n\nFor more information, "
"refer to the Nothin' Fancy manual";
std::exit(0);
}
int main(int argc, char* argv[]) {
Log("Starting up");
if (argc > 1 && ((std::string)argv[1] == "-h" || (std::string)argv[1] == "/?"))
printHelp();
if (argc > 1)
if ((std::string)argv[1] == "-h") {
Log("Nothin' Fancy Asset Builder\nThis tool creates .nfpack files for the NF engine to load "
"at runtime.\nA pack gets created for each directory in the working directory if there "
"are only compatible files inside.\nSubdirectories are not ignored.");
return 0;
}
Log("Starting up");
initCompressor();
@@ -35,7 +39,8 @@ int main(int argc, char* argv[]) {
std::string currPackFilename = currDir.path().filename().string().append(".nfpack");
Log("Building pack " + currPackFilename);
std::unordered_map<std::string, size_t> packFiles;
std::vector<std::string> packFilenames;
std::vector<unsigned int> packFileSizes;
std::string currFileExtension;
std::string currFileIn;
std::string currPackOut;
@@ -47,8 +52,8 @@ int main(int argc, char* argv[]) {
std::filesystem::path relative = std::filesystem::relative(curr, currDir);
std::string currFileName = relative.filename().string();
for (auto& file : packFiles)
if (currFileName == file.first)
for (auto& file : packFilenames)
if (currFileName == file)
Error("Duplicate asset \"" + currFileName + (std::string)"\" in pack!");
currFileExtension = relative.extension().string().substr(1);
@@ -77,10 +82,11 @@ int main(int argc, char* argv[]) {
Log("Cooking model...");
cookModel(currFileIn, mtl, currFileOut);
}
else if (currFileExtension == "png" || currFileExtension == "jpg")
cookTexture(currFileIn, currFileOut);
else
currFileOut = currFileIn;
packFiles[currFileName] = currFileOut.size();
packFilenames.push_back(currFileName);
packFileSizes.push_back((unsigned int)currFileOut.size());
currPackOut += currFileOut;
@@ -94,19 +100,16 @@ int main(int argc, char* argv[]) {
else
Log("Finished gathering files");
//Write pack header with asset names and sizes
std::string header;
for (auto& currFile : packFiles) {
header += currFile.first;
header += ':';
header.append((char*)&currFile.second, sizeof(size_t));
header += ':';
header += std::to_string(fileCount) + (std::string)"\n";
for (unsigned int i = 0; i < packFilenames.size(); i++) {
header += packFilenames[i];
header += '\n';
header += std::to_string(packFileSizes[i]);
header += '\n';
}
header.erase(header.size() - 1);
header.append("/NFENDOFPACKHEADER");
int headerTableSize = (int)header.size();
header.insert(0, (char*)&headerTableSize, sizeof(headerTableSize));
header.insert(0, (char*)&fileCount, sizeof(fileCount));
header.append("/ENDHEADER");
currPackOut.insert(0, header);
writePack(currPackFilename, currPackOut);