Added asset loading and OBJ parsing, including indexing
This commit is contained in:
parent
086f8a1670
commit
9fcd8240e4
Binary file not shown.
After Width: | Height: | Size: 28 KiB |
@ -109,7 +109,7 @@
|
|||||||
<AdditionalDependencies>nf.res;%(AdditionalDependencies)</AdditionalDependencies>
|
<AdditionalDependencies>nf.res;%(AdditionalDependencies)</AdditionalDependencies>
|
||||||
</Link>
|
</Link>
|
||||||
<PostBuildEvent>
|
<PostBuildEvent>
|
||||||
<Command>cd "$(SolutionDir)NFPackCreator\bin\AssetBuild" && "$(SolutionDir)NFPackCreator\bin\Win32$(Configuration)\NFPackCreator.exe" && del "$(OutDir)assets\" /Q /S && copy "$(SolutionDir)NFPackCreator\bin\AssetBuild\*.nfpack" "$(OutDir)assets\"</Command>
|
<Command>cd "$(SolutionDir)NFPackCreator\bin\AssetBuild" && "$(SolutionDir)NFPackCreator\bin\Win32$(Configuration)\NFPackCreator.exe" && del "$(OutDir)assets\" /Q /S && move "$(SolutionDir)NFPackCreator\bin\AssetBuild\*.nfpack" "$(OutDir)assets\"</Command>
|
||||||
</PostBuildEvent>
|
</PostBuildEvent>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
@ -135,7 +135,7 @@
|
|||||||
<AdditionalDependencies>nf.res;%(AdditionalDependencies)</AdditionalDependencies>
|
<AdditionalDependencies>nf.res;%(AdditionalDependencies)</AdditionalDependencies>
|
||||||
</Link>
|
</Link>
|
||||||
<PostBuildEvent>
|
<PostBuildEvent>
|
||||||
<Command>cd "$(SolutionDir)NFPackCreator\bin\AssetBuild" && "$(SolutionDir)NFPackCreator\bin\Win32$(Configuration)\NFPackCreator.exe" && del "$(OutDir)assets\" /Q /S && copy "$(SolutionDir)NFPackCreator\bin\AssetBuild\*.nfpack" "$(OutDir)assets\"</Command>
|
<Command>cd "$(SolutionDir)NFPackCreator\bin\AssetBuild" && "$(SolutionDir)NFPackCreator\bin\Win32$(Configuration)\NFPackCreator.exe" && del "$(OutDir)assets\" /Q /S && move "$(SolutionDir)NFPackCreator\bin\AssetBuild\*.nfpack" "$(OutDir)assets\"</Command>
|
||||||
</PostBuildEvent>
|
</PostBuildEvent>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||||
@ -157,7 +157,7 @@
|
|||||||
<AdditionalDependencies>nf.res;%(AdditionalDependencies)</AdditionalDependencies>
|
<AdditionalDependencies>nf.res;%(AdditionalDependencies)</AdditionalDependencies>
|
||||||
</Link>
|
</Link>
|
||||||
<PostBuildEvent>
|
<PostBuildEvent>
|
||||||
<Command>cd "$(SolutionDir)NFPackCreator\bin\AssetBuild" && "$(SolutionDir)NFPackCreator\bin\Win32$(Configuration)\NFPackCreator.exe" && del "$(OutDir)assets\" /Q /S && copy "$(SolutionDir)NFPackCreator\bin\AssetBuild\*.nfpack" "$(OutDir)assets\"</Command>
|
<Command>cd "$(SolutionDir)NFPackCreator\bin\AssetBuild" && "$(SolutionDir)NFPackCreator\bin\Win32$(Configuration)\NFPackCreator.exe" && del "$(OutDir)assets\" /Q /S && move "$(SolutionDir)NFPackCreator\bin\AssetBuild\*.nfpack" "$(OutDir)assets\"</Command>
|
||||||
</PostBuildEvent>
|
</PostBuildEvent>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||||
@ -183,7 +183,7 @@
|
|||||||
<AdditionalDependencies>nf.res;%(AdditionalDependencies)</AdditionalDependencies>
|
<AdditionalDependencies>nf.res;%(AdditionalDependencies)</AdditionalDependencies>
|
||||||
</Link>
|
</Link>
|
||||||
<PostBuildEvent>
|
<PostBuildEvent>
|
||||||
<Command>cd "$(SolutionDir)NFPackCreator\bin\AssetBuild" && "$(SolutionDir)NFPackCreator\bin\Win32$(Configuration)\NFPackCreator.exe" && del "$(OutDir)assets\" /Q /S && copy "$(SolutionDir)NFPackCreator\bin\AssetBuild\*.nfpack" "$(OutDir)assets\"</Command>
|
<Command>cd "$(SolutionDir)NFPackCreator\bin\AssetBuild" && "$(SolutionDir)NFPackCreator\bin\Win32$(Configuration)\NFPackCreator.exe" && del "$(OutDir)assets\" /Q /S && move "$(SolutionDir)NFPackCreator\bin\AssetBuild\*.nfpack" "$(OutDir)assets\"</Command>
|
||||||
</PostBuildEvent>
|
</PostBuildEvent>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@ -203,7 +203,7 @@
|
|||||||
</ImportGroup>
|
</ImportGroup>
|
||||||
<Target Name="AfterClean">
|
<Target Name="AfterClean">
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<_delete Include="$(OutDir)**\*"/>
|
<_delete Include="$(OutDir)**\*" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Delete Files="@(_delete)" />
|
<Delete Files="@(_delete)" />
|
||||||
</Target>
|
</Target>
|
||||||
|
@ -6,7 +6,6 @@
|
|||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||||
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
|
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
|
||||||
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
|
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
|
||||||
<LocalDebuggerCommandArguments>-h</LocalDebuggerCommandArguments>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
|
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
|
||||||
|
@ -33,7 +33,7 @@ void Success(const std::string& in) {
|
|||||||
|
|
||||||
std::string readFile(const std::string& filename) {
|
std::string readFile(const std::string& filename) {
|
||||||
std::ifstream in;
|
std::ifstream in;
|
||||||
in.open(filename);
|
in.open(filename, std::ios::binary);
|
||||||
if (!in)
|
if (!in)
|
||||||
Error("File \"" + (std::string)filename + (std::string)"\" could not be read!");
|
Error("File \"" + (std::string)filename + (std::string)"\" could not be read!");
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
@ -49,13 +49,15 @@ std::string readFile(const std::string& filename) {
|
|||||||
|
|
||||||
void writeFile(const std::string& filename, const std::string& in, bool encrypted) {
|
void writeFile(const std::string& filename, const std::string& in, bool encrypted) {
|
||||||
std::ofstream out;
|
std::ofstream out;
|
||||||
out.open(filename);
|
out.open(filename, std::ios::binary);
|
||||||
if (!out)
|
if (!out)
|
||||||
Error("File \"" + (std::string)filename + (std::string)"\" could not be written!");
|
Error("File \"" + (std::string)filename + (std::string)"\" could not be written!");
|
||||||
std::string write(in);
|
std::string write(in);
|
||||||
if (encrypted) {
|
if (encrypted) {
|
||||||
for (unsigned int i = 0; i < write.size(); i++)
|
for (unsigned int i = 0; i < write.size(); i++) {
|
||||||
write[i] = write[i] + 100;
|
char temp = write[i] + 100;
|
||||||
|
write[i] = temp;
|
||||||
|
}
|
||||||
write.insert(0, "NFEF");
|
write.insert(0, "NFEF");
|
||||||
}
|
}
|
||||||
out << write;
|
out << write;
|
||||||
@ -82,7 +84,7 @@ int main(int argc, char* argv[]) {
|
|||||||
continue;
|
continue;
|
||||||
std::string filename = currDir.path().filename().string().append(".nfpack");
|
std::string filename = currDir.path().filename().string().append(".nfpack");
|
||||||
Log("Creating pack \"" + filename + (std::string)"\"");
|
Log("Creating pack \"" + filename + (std::string)"\"");
|
||||||
std::ifstream in;
|
std::string currFileContents;
|
||||||
std::stringstream out;
|
std::stringstream out;
|
||||||
unsigned int fileCount = 0;
|
unsigned int fileCount = 0;
|
||||||
for (const auto& curr : std::filesystem::recursive_directory_iterator(currDir)) {
|
for (const auto& curr : std::filesystem::recursive_directory_iterator(currDir)) {
|
||||||
@ -93,15 +95,12 @@ int main(int argc, char* argv[]) {
|
|||||||
Error("File \"" + relative.string() + (std::string)"\" is not of supported type!");
|
Error("File \"" + relative.string() + (std::string)"\" is not of supported type!");
|
||||||
Log("Current file: " + relative.string());
|
Log("Current file: " + relative.string());
|
||||||
|
|
||||||
in.open(curr.path(), std::ios::binary);
|
currFileContents = readFile(curr.path().string());
|
||||||
if (!in)
|
|
||||||
Error("Couldn't open \"" + relative.string() + (std::string)"\"!");
|
|
||||||
if (out.rdbuf()->in_avail() != 0)
|
if (out.rdbuf()->in_avail() != 0)
|
||||||
out << "\n";
|
out << "\n";
|
||||||
out << "#NFASSET " + curr.path().filename().string();
|
out << "#NFASSET " + curr.path().filename().string();
|
||||||
out << "\n";
|
out << "\n";
|
||||||
out << in.rdbuf();
|
out << currFileContents;
|
||||||
in.close();
|
|
||||||
fileCount++;
|
fileCount++;
|
||||||
}
|
}
|
||||||
if (fileCount == 0) {
|
if (fileCount == 0) {
|
||||||
@ -114,6 +113,8 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
if (dirCount > 0)
|
if (dirCount > 0)
|
||||||
Log("Wrote " + std::to_string(dirCount) + (std::string)" asset pack(s).");
|
Log("Wrote " + std::to_string(dirCount) + (std::string)" asset pack(s).");
|
||||||
|
else
|
||||||
|
Log("No directories found!");
|
||||||
Log("Done");
|
Log("Done");
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(2));
|
std::this_thread::sleep_for(std::chrono::seconds(2));
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -144,7 +144,7 @@ namespace nf {
|
|||||||
RegisterClass(&wclass);
|
RegisterClass(&wclass);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
Error("Cannot run two NF applications at once!");
|
Error("Cannot run multiple NF applications concurrently!");
|
||||||
}
|
}
|
||||||
|
|
||||||
RECT Application::getWindowRect() const {
|
RECT Application::getWindowRect() const {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#include "Assets.h"
|
#include "Assets.h"
|
||||||
|
|
||||||
|
#include "Model.h"
|
||||||
#include "Utility.h"
|
#include "Utility.h"
|
||||||
|
|
||||||
namespace nf {
|
namespace nf {
|
||||||
@ -7,14 +8,12 @@ namespace nf {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AGeometry::~AGeometry() {
|
AModel::~AModel() {
|
||||||
delete[] m_vertexBufferData;
|
delete[] data;
|
||||||
delete[] m_indexBufferData;
|
|
||||||
delete[] m_textureCoordinatesBufferData;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ATexture::~ATexture() {
|
ATexture::~ATexture() {
|
||||||
delete[] m_data;
|
delete[] data;
|
||||||
}
|
}
|
||||||
|
|
||||||
AssetPack::AssetPack() {
|
AssetPack::AssetPack() {
|
||||||
@ -22,8 +21,59 @@ namespace nf {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void AssetPack::load(const char* packName) {
|
void AssetPack::load(const char* packName) {
|
||||||
std::string path = "assets/" + (std::string)packName + ".nfpack";
|
std::string path = "assets/" + (std::string)packName;
|
||||||
std::string contents = readFile(path);
|
std::string packContents = readFile(path);
|
||||||
|
while (packContents.size()) {
|
||||||
|
unsigned int startingPos = packContents.find_first_of("#NFASSET ") + 9;
|
||||||
|
packContents = packContents.substr(9);
|
||||||
|
unsigned int endAssetNamePos = packContents.find_first_of('\n');
|
||||||
|
std::string assetName = packContents.substr(0, endAssetNamePos);
|
||||||
|
packContents = packContents.substr(endAssetNamePos + 1);
|
||||||
|
unsigned int extensionPos = assetName.find_first_of('.');
|
||||||
|
std::string extension = assetName.substr(extensionPos + 1);
|
||||||
|
std::string assetContents;
|
||||||
|
unsigned int nextAssetPos = packContents.find("#NFASSET ");
|
||||||
|
if (nextAssetPos != std::string::npos) {
|
||||||
|
assetContents = packContents.substr(0, nextAssetPos - 1);
|
||||||
|
packContents = packContents.substr(nextAssetPos);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
assetContents = packContents;
|
||||||
|
packContents = "";
|
||||||
|
}
|
||||||
|
size_t assetSize = assetContents.size();
|
||||||
|
|
||||||
|
if (extension == "obj") {
|
||||||
|
AModel* geometry = new AModel;
|
||||||
|
geometry->data = new char[assetSize + 1];
|
||||||
|
std::memcpy(geometry->data, &assetContents[0], assetSize);
|
||||||
|
geometry->data[assetSize] = '\0';
|
||||||
|
geometry->alreadyLoaded = false;
|
||||||
|
geometry->loadedModel = nullptr;
|
||||||
|
m_assets[assetName] = geometry;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (extension == "png") {
|
||||||
|
ATexture* texture = new ATexture;
|
||||||
|
texture->data = new unsigned char[assetSize];
|
||||||
|
std::memcpy(texture->data, &assetContents[0], assetSize);
|
||||||
|
texture->size = assetSize;
|
||||||
|
m_assets[assetName] = texture;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Error("Invalid asset extention in pack \"" + (std::string)packName + (std::string)"\"!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Asset* AssetPack::operator[](const char* in) {
|
||||||
|
if (m_assets.find(in) == m_assets.end())
|
||||||
|
Error("Could not find asset \"" + (std::string)in + (std::string)"\" in asset pack!");
|
||||||
|
return m_assets[in];
|
||||||
|
}
|
||||||
|
Asset* AssetPack::operator[](std::string& in) {
|
||||||
|
if (m_assets.find(in) == m_assets.end())
|
||||||
|
Error("Could not find asset \"" + in + (std::string)"\" in asset pack!");
|
||||||
|
return m_assets[in];
|
||||||
}
|
}
|
||||||
|
|
||||||
AssetPack::~AssetPack() {
|
AssetPack::~AssetPack() {
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
#include "Entity.h"
|
#include "Entity.h"
|
||||||
|
|
||||||
|
#include<vector>
|
||||||
|
|
||||||
|
#include "Utility.h"
|
||||||
|
|
||||||
namespace nf {
|
namespace nf {
|
||||||
Entity::Entity() :
|
Entity::Entity() :
|
||||||
m_model(nullptr),
|
m_model(nullptr),
|
||||||
@ -10,25 +14,40 @@ namespace nf {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Entity::create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinatesBufferData, size_t textureCoordinatesBufferSize, const char* textureName) {
|
void Entity::create(Asset* modelAsset, Asset* textureAsset) {
|
||||||
|
AModel& model = *(AModel*)modelAsset;
|
||||||
|
if (model.alreadyLoaded) {
|
||||||
|
m_model = model.loadedModel;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!textureAsset)
|
||||||
|
Error("No texture given to Entity create function on new model load!");
|
||||||
|
ATexture& texture = *(ATexture*)textureAsset;
|
||||||
|
std::string obj = model.data;
|
||||||
m_model = new Model;
|
m_model = new Model;
|
||||||
m_model->create(vertexBufferData, vertexBufferSize, indexBufferData, indexBufferCount, textureCoordinatesBufferData, textureCoordinatesBufferSize, textureName);
|
std::vector<float> vb;
|
||||||
//TODO: Replace this with getting the information from the AGeometry and ATexture structs
|
std::vector<unsigned int> ib;
|
||||||
|
size_t ibCount = 0;
|
||||||
|
std::vector<float> tc;
|
||||||
|
parseOBJ(obj, vb, ib, ibCount, tc);
|
||||||
|
m_model->create(&vb[0], vb.size() * sizeof(float), &ib[0], ibCount, &tc[0], tc.size() * sizeof(float), texture.data, texture.size);
|
||||||
|
model.alreadyLoaded = true;
|
||||||
|
model.loadedModel = m_model;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Entity::setPosition(float x, float y, float z) {
|
void Entity::setPosition(double x, double y, double z) {
|
||||||
m_position = { x, y, z };
|
m_position = { x, y, z };
|
||||||
}
|
}
|
||||||
|
|
||||||
void Entity::setRotation(float x, float y, float z) {
|
void Entity::setRotation(double x, double y, double z) {
|
||||||
m_rotation = { x, y, z };
|
m_rotation = { x, y, z };
|
||||||
}
|
}
|
||||||
|
|
||||||
void Entity::setScale(float x) {
|
void Entity::setScale(double x) {
|
||||||
m_rotation = { x, x, x };
|
m_rotation = { x, x, x };
|
||||||
}
|
}
|
||||||
|
|
||||||
void Entity::setScale(float x, float y, float z) {
|
void Entity::setScale(double x, double y, double z) {
|
||||||
m_scale = { x, y, z };
|
m_scale = { x, y, z };
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,9 +64,9 @@ namespace nf {
|
|||||||
void Entity::setModelMatrix(Shader* shader) {
|
void Entity::setModelMatrix(Shader* shader) {
|
||||||
glm::mat4 model(1.0f);
|
glm::mat4 model(1.0f);
|
||||||
model = glm::translate(model, glm::vec3(m_position.x, m_position.y, m_position.z));
|
model = glm::translate(model, glm::vec3(m_position.x, m_position.y, m_position.z));
|
||||||
model = glm::rotate(model, glm::radians(m_rotation.x), glm::vec3(1.0, 0.0, 0.0));
|
model = glm::rotate(model, glm::radians((float)m_rotation.x), glm::vec3(1.0, 0.0, 0.0));
|
||||||
model = glm::rotate(model, glm::radians(m_rotation.y), glm::vec3(0.0, 1.0, 0.0));
|
model = glm::rotate(model, glm::radians((float)m_rotation.y), glm::vec3(0.0, 1.0, 0.0));
|
||||||
model = glm::rotate(model, glm::radians(m_rotation.z), glm::vec3(0.0, 0.0, 1.0));
|
model = glm::rotate(model, glm::radians((float)m_rotation.z), glm::vec3(0.0, 0.0, 1.0));
|
||||||
model = glm::scale(model, glm::vec3(m_scale.x, m_scale.y, m_scale.z));
|
model = glm::scale(model, glm::vec3(m_scale.x, m_scale.y, m_scale.z));
|
||||||
shader->setUniform("model", model);
|
shader->setUniform("model", model);
|
||||||
}
|
}
|
||||||
|
@ -6,18 +6,17 @@ namespace nf {
|
|||||||
Model::Model() {
|
Model::Model() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Model::create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinatesBufferData, size_t textureCoordinatesBufferSize, const char* textureName) {
|
void Model::create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinatesBufferData, size_t textureCoordinatesBufferSize, const unsigned char* textureData, size_t textureSize) {
|
||||||
m_vao = new VertexArray;
|
m_vao = new VertexArray;
|
||||||
m_vao->addBuffer(vertexBufferData, vertexBufferSize);
|
m_vao->addBuffer(vertexBufferData, vertexBufferSize);
|
||||||
m_vao->push<float>(2);
|
m_vao->push<float>(3);
|
||||||
//TODO: Change this to 3
|
|
||||||
m_vao->finishBufferLayout();
|
m_vao->finishBufferLayout();
|
||||||
if (textureCoordinatesBufferData && textureCoordinatesBufferSize && textureName) {
|
if (textureCoordinatesBufferData && textureCoordinatesBufferSize && textureData) {
|
||||||
m_vao->addBuffer(textureCoordinatesBufferData, textureCoordinatesBufferSize);
|
m_vao->addBuffer(textureCoordinatesBufferData, textureCoordinatesBufferSize);
|
||||||
m_vao->push<float>(2);
|
m_vao->push<float>(2);
|
||||||
m_vao->finishBufferLayout();
|
m_vao->finishBufferLayout();
|
||||||
m_texture = new Texture;
|
m_texture = new Texture;
|
||||||
m_texture->create(textureName);
|
m_texture->create(textureData, textureSize);
|
||||||
}
|
}
|
||||||
m_ib = new IndexBuffer(indexBufferData, indexBufferCount);
|
m_ib = new IndexBuffer(indexBufferData, indexBufferCount);
|
||||||
}
|
}
|
||||||
|
@ -11,18 +11,17 @@ namespace nf {
|
|||||||
glGenTextures(1, &m_id);
|
glGenTextures(1, &m_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Texture::create(const char* textureName) {
|
void Texture::create(const unsigned char* textureData, size_t textureSize) {
|
||||||
int width, height, nChannels;
|
int width, height, nChannels;
|
||||||
stbi_set_flip_vertically_on_load(true);
|
stbi_set_flip_vertically_on_load(true);
|
||||||
unsigned char* texture = stbi_load(textureName, &width, &height, &nChannels, 0);
|
unsigned char* texture = stbi_load_from_memory(textureData, textureSize, &width, &height, &nChannels, 0);
|
||||||
//TODO: Load from memory
|
|
||||||
if (!texture)
|
if (!texture)
|
||||||
Error("Texture \"" + (std::string)textureName + "\" failed to load from memory!");
|
Error("Texture failed to load from memory!");
|
||||||
glBindTexture(GL_TEXTURE_2D, m_id);
|
glBindTexture(GL_TEXTURE_2D, m_id);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
|
||||||
//glGenerateMipmap(GL_TEXTURE_2D);
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
stbi_image_free(texture);
|
stbi_image_free(texture);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
|
|
||||||
@ -28,24 +29,22 @@ namespace nf {
|
|||||||
std::chrono::duration<float> time = getCurrentTime();
|
std::chrono::duration<float> time = getCurrentTime();
|
||||||
std::printf("[%.4f] Debug: %.4f\n", time.count(), in);
|
std::printf("[%.4f] Debug: %.4f\n", time.count(), in);
|
||||||
}
|
}
|
||||||
|
//TODO: Test every Error in release mode
|
||||||
void Debug::ErrorImp(const char* in, const char* filename, int line) {
|
void Debug::ErrorImp(const char* in, const char* filename, int line) {
|
||||||
std::chrono::duration<float> time = getCurrentTime();
|
std::chrono::duration<float> time = getCurrentTime();
|
||||||
HANDLE cmd = GetStdHandle(STD_OUTPUT_HANDLE);
|
static HANDLE cmd = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
SetConsoleTextAttribute(cmd, FOREGROUND_RED);
|
SetConsoleTextAttribute(cmd, FOREGROUND_RED);
|
||||||
std::printf("[%.4f] Error (%s, %i): %s\n", time.count(), filename, line, in);
|
std::printf("[%.4f] Error (%s, %i): %s\n", time.count(), filename, line, in);
|
||||||
SetConsoleTextAttribute(cmd, 7);
|
SetConsoleTextAttribute(cmd, 7);
|
||||||
FindClose(cmd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Debug::ErrorImp(const std::string& in, const char* filename, int line) {
|
void Debug::ErrorImp(const std::string& in, const char* filename, int line) {
|
||||||
std::chrono::duration<float> time = getCurrentTime();
|
std::chrono::duration<float> time = getCurrentTime();
|
||||||
HANDLE cmd = GetStdHandle(STD_OUTPUT_HANDLE);
|
static HANDLE cmd = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
SetConsoleTextAttribute(cmd, FOREGROUND_RED);
|
SetConsoleTextAttribute(cmd, FOREGROUND_RED);
|
||||||
std::printf("[%.4f] Error (%s, %i): ", time.count(), filename, line);
|
std::printf("[%.4f] Error (%s, %i): ", time.count(), filename, line);
|
||||||
std::cout << in << "\n";
|
std::cout << in << "\n";
|
||||||
SetConsoleTextAttribute(cmd, 7);
|
SetConsoleTextAttribute(cmd, 7);
|
||||||
FindClose(cmd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::chrono::duration<float> Debug::getCurrentTime() {
|
std::chrono::duration<float> Debug::getCurrentTime() {
|
||||||
@ -96,7 +95,7 @@ namespace nf {
|
|||||||
CreateDirectory(folders.c_str(), NULL);
|
CreateDirectory(folders.c_str(), NULL);
|
||||||
}
|
}
|
||||||
std::ofstream out;
|
std::ofstream out;
|
||||||
out.open(filename);
|
out.open(filename, std::ios::binary);
|
||||||
if (!out)
|
if (!out)
|
||||||
Error("File \"" + (std::string)filename + (std::string)"\" could not be written!");
|
Error("File \"" + (std::string)filename + (std::string)"\" could not be written!");
|
||||||
std::string write(in);
|
std::string write(in);
|
||||||
@ -118,13 +117,108 @@ namespace nf {
|
|||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << in.rdbuf();
|
ss << in.rdbuf();
|
||||||
std::string read(ss.str());
|
std::string read(ss.str());
|
||||||
if (read.size() > 4 && read.substr(0, 4) == "NFEF" ){
|
if (read.size() > 4 && read.substr(0, 4) == "NFEF") {
|
||||||
read = read.substr(4);
|
read = read.substr(4);
|
||||||
for (unsigned int i = 0; i < read.size(); i++)
|
for (unsigned int i = 0; i < read.size(); i++)
|
||||||
read[i] = read[i] - 100;
|
read[i] = read[i] - 100;
|
||||||
}
|
}
|
||||||
return read;
|
return read;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void parseOBJ(std::string& in, std::vector<float>& vbOut, std::vector<unsigned int>& ibOut, size_t& ibCountOut, std::vector<float>& tcOut) {
|
||||||
|
std::string file = in;
|
||||||
|
std::vector<float> vbRaw, tcRaw;
|
||||||
|
std::vector<unsigned int> vertexIndices, uvIndices, normalIndices;
|
||||||
|
std::vector<float> tempVertices;
|
||||||
|
std::vector<float> tempTC;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
char line[500];
|
||||||
|
int remove = 0;
|
||||||
|
int result = sscanf_s(file.c_str(), "\n%s", line, (unsigned)_countof(line));
|
||||||
|
if (result == EOF)
|
||||||
|
break;
|
||||||
|
if (std::strcmp(line, "v") == 0) {
|
||||||
|
float x = 0.0, y = 0.0, z = 0.0;
|
||||||
|
sscanf_s(file.c_str(), "\nv %f %f %f\n", &x, &y, &z);
|
||||||
|
remove = 28;
|
||||||
|
tempVertices.push_back(x);
|
||||||
|
tempVertices.push_back(y);
|
||||||
|
tempVertices.push_back(z);
|
||||||
|
}
|
||||||
|
else if (std::strcmp(line, "vt") == 0) {
|
||||||
|
float u = 0.0, v = 0.0;
|
||||||
|
sscanf_s(file.c_str(), "\nvt %f %f\n", &u, &v);
|
||||||
|
remove = 18;
|
||||||
|
tempTC.push_back(u);
|
||||||
|
tempTC.push_back(v);
|
||||||
|
}
|
||||||
|
else if (std::strcmp(line, "f") == 0) {
|
||||||
|
unsigned int vertexIndex[3], uvIndex[3];
|
||||||
|
sscanf_s(file.c_str(), "\nf %d/%d %d/%d %d/%d\n", &vertexIndex[0], &uvIndex[0], &vertexIndex[1], &uvIndex[1], &vertexIndex[2], &uvIndex[2]);
|
||||||
|
remove = 12;
|
||||||
|
vertexIndices.push_back(vertexIndex[0]);
|
||||||
|
vertexIndices.push_back(vertexIndex[1]);
|
||||||
|
vertexIndices.push_back(vertexIndex[2]);
|
||||||
|
uvIndices.push_back(uvIndex[0]);
|
||||||
|
uvIndices.push_back(uvIndex[1]);
|
||||||
|
uvIndices.push_back(uvIndex[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int pos = file.find(line) + strlen(line) + remove;
|
||||||
|
file = file.substr(pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < vertexIndices.size(); i++) {
|
||||||
|
unsigned int vertexIndex = vertexIndices[i];
|
||||||
|
unsigned int uvIndex = uvIndices[i];
|
||||||
|
float vertexX = tempVertices[(vertexIndex - 1) * 3];
|
||||||
|
float vertexY = tempVertices[(vertexIndex - 1) * 3 + 1];
|
||||||
|
float vertexZ = tempVertices[(vertexIndex - 1) * 3 + 2];
|
||||||
|
float vertexU = tempTC[(uvIndex - 1) * 2];
|
||||||
|
float vertexV = tempTC[(uvIndex - 1) * 2 + 1];
|
||||||
|
vbRaw.push_back(vertexX);
|
||||||
|
vbRaw.push_back(vertexY);
|
||||||
|
vbRaw.push_back(vertexZ);
|
||||||
|
tcRaw.push_back(vertexU);
|
||||||
|
tcRaw.push_back(vertexV);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Vertex {
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
float z;
|
||||||
|
|
||||||
|
float u;
|
||||||
|
float v;
|
||||||
|
|
||||||
|
bool operator<(const Vertex other) const {
|
||||||
|
return std::memcmp((void*)this, (void*)&other, sizeof(Vertex)) > 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
std::map<Vertex, unsigned int> vertexMap;
|
||||||
|
for (unsigned int i = 0; i * 3 < vbRaw.size(); i++) {
|
||||||
|
Vertex curr = { vbRaw[(i * 3)], vbRaw[(i * 3) + 1], vbRaw[(i * 3) + 2], tcRaw[(i * 2)], tcRaw[(i * 2) + 1] };
|
||||||
|
bool found = false;
|
||||||
|
found = vertexMap.find(curr) != vertexMap.end();
|
||||||
|
if (found) {
|
||||||
|
unsigned int index = vertexMap[curr];
|
||||||
|
ibOut.push_back(index);
|
||||||
|
ibCountOut++;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
vbOut.push_back(curr.x);
|
||||||
|
vbOut.push_back(curr.y);
|
||||||
|
vbOut.push_back(curr.z);
|
||||||
|
tcOut.push_back(curr.u);
|
||||||
|
tcOut.push_back(curr.v);
|
||||||
|
unsigned int index = (vbOut.size() / 3) - 1;
|
||||||
|
ibOut.push_back(index);
|
||||||
|
vertexMap[curr] = index;
|
||||||
|
ibCountOut++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Nvidia Optimius support
|
//Nvidia Optimius support
|
||||||
|
@ -1,21 +1,25 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace nf {
|
namespace nf {
|
||||||
|
class Model;
|
||||||
|
|
||||||
struct Asset {
|
struct Asset {
|
||||||
virtual ~Asset();
|
virtual ~Asset();
|
||||||
};
|
};
|
||||||
|
|
||||||
struct AGeometry : Asset {
|
struct AModel : Asset {
|
||||||
const float* m_vertexBufferData;
|
char* data;
|
||||||
const unsigned int* m_indexBufferData;
|
bool alreadyLoaded;
|
||||||
const float* m_textureCoordinatesBufferData;
|
Model* loadedModel;
|
||||||
~AGeometry() override;
|
|
||||||
|
~AModel() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ATexture : Asset {
|
struct ATexture : Asset {
|
||||||
const void* m_data;
|
unsigned char* data;
|
||||||
|
size_t size;
|
||||||
|
|
||||||
~ATexture() override;
|
~ATexture() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -24,6 +28,8 @@ namespace nf {
|
|||||||
AssetPack();
|
AssetPack();
|
||||||
|
|
||||||
void load(const char* packName);
|
void load(const char* packName);
|
||||||
|
Asset* operator[](const char* in);
|
||||||
|
Asset* operator[](std::string& in);
|
||||||
|
|
||||||
~AssetPack();
|
~AssetPack();
|
||||||
private:
|
private:
|
||||||
|
@ -1,22 +1,23 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Model.h"
|
#include "Model.h"
|
||||||
|
#include "Assets.h"
|
||||||
|
|
||||||
namespace nf {
|
namespace nf {
|
||||||
class Entity {
|
class Entity {
|
||||||
struct Vec3 {
|
struct Vec3 {
|
||||||
Vec3(float x1) : x(x1), y(x1), z(x1) {}
|
Vec3(double x1) : x(x1), y(x1), z(x1) {}
|
||||||
Vec3(float x1, float y1, float z1) : x(x1), y(y1), z(z1) {}
|
Vec3(double x1, double y1, double z1) : x(x1), y(y1), z(z1) {}
|
||||||
float x, y, z;
|
double x, y, z;
|
||||||
};
|
};
|
||||||
public:
|
public:
|
||||||
Entity();
|
Entity();
|
||||||
|
|
||||||
void create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinatesBufferData = nullptr, size_t textureCoordinatesBufferSize = 0, const char* textureName = nullptr);
|
void create(Asset* modelAsset, Asset* textureAsset = nullptr);
|
||||||
//TODO: Do this using loaded assets somehow
|
|
||||||
void setPosition(float x, float y, float z);
|
void setPosition(double x, double y, double z);
|
||||||
void setRotation(float x, float y, float z);
|
void setRotation(double x, double y, double z);
|
||||||
void setScale(float x);
|
void setScale(double x);
|
||||||
void setScale(float x, float y, float z);
|
void setScale(double x, double y, double z);
|
||||||
|
|
||||||
void bind(Shader* shader);
|
void bind(Shader* shader);
|
||||||
Model* getModel() const;
|
Model* getModel() const;
|
||||||
|
@ -12,7 +12,7 @@ namespace nf {
|
|||||||
public:
|
public:
|
||||||
Model();
|
Model();
|
||||||
|
|
||||||
void create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinates = nullptr, size_t textureCoordinatesBufferSize = 0, const char* textureName = nullptr);
|
void create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinates, size_t textureCoordinatesBufferSize, const unsigned char* textureData, size_t textureSize);
|
||||||
void bind() override;
|
void bind() override;
|
||||||
|
|
||||||
~Model();
|
~Model();
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
#include "IntroGamestate.h"
|
#include "IntroGamestate.h"
|
||||||
|
#include "Assets.h"
|
||||||
|
|
||||||
namespace nf {
|
namespace nf {
|
||||||
class Drawable;
|
class Drawable;
|
||||||
@ -15,19 +16,19 @@ namespace nf {
|
|||||||
|
|
||||||
class Entity {
|
class Entity {
|
||||||
struct Vec3 {
|
struct Vec3 {
|
||||||
Vec3(float x1) : x(x1), y(x1), z(x1) {}
|
Vec3(double x1) : x(x1), y(x1), z(x1) {}
|
||||||
Vec3(float x1, float y1, float z1) : x(x1), y(y1), z(z1) {}
|
Vec3(double x1, double y1, double z1) : x(x1), y(y1), z(z1) {}
|
||||||
float x, y, z;
|
double x, y, z;
|
||||||
};
|
};
|
||||||
public:
|
public:
|
||||||
Entity();
|
Entity();
|
||||||
|
|
||||||
void create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinatesBufferData = nullptr, size_t textureCoordinatesBufferSize = 0, const char* textureName = nullptr);
|
void create(Asset* modelAsset, Asset* textureAsset = nullptr);
|
||||||
//TODO: Do this using loaded assets somehow
|
|
||||||
void setPosition(float x, float y, float z);
|
void setPosition(double x, double y, double z);
|
||||||
void setRotation(float x, float y, float z);
|
void setRotation(double x, double y, double z);
|
||||||
void setScale(float x);
|
void setScale(double x);
|
||||||
void setScale(float x, float y, float z);
|
void setScale(double x, double y, double z);
|
||||||
|
|
||||||
void bind(Shader* shader);
|
void bind(Shader* shader);
|
||||||
Model* getModel() const;
|
Model* getModel() const;
|
||||||
@ -131,6 +132,5 @@ namespace nf {
|
|||||||
Renderer* m_renderer;
|
Renderer* m_renderer;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#include "Assets.h"
|
|
||||||
#include "Input.h"
|
#include "Input.h"
|
||||||
#include "Utility.h"
|
#include "Utility.h"
|
@ -5,7 +5,7 @@ namespace nf {
|
|||||||
public:
|
public:
|
||||||
Texture();
|
Texture();
|
||||||
|
|
||||||
void create(const char* textureName);
|
void create(const unsigned char* textureData, size_t textureSize);
|
||||||
void bind();
|
void bind();
|
||||||
|
|
||||||
~Texture();
|
~Texture();
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
|
|
||||||
namespace nf {
|
namespace nf {
|
||||||
@ -49,4 +50,5 @@ std::exit(-1);}
|
|||||||
void writeFile(const std::string& filename, const std::string& in, bool encrypted = false);
|
void writeFile(const std::string& filename, const std::string& in, bool encrypted = false);
|
||||||
std::string readFile(const std::string& filename);
|
std::string readFile(const std::string& filename);
|
||||||
|
|
||||||
|
void parseOBJ(std::string& in, std::vector<float>& vbOut, std::vector<unsigned int>& ibOut, size_t& ibCountOut, std::vector<float>& tcOut);
|
||||||
}
|
}
|
Reference in New Issue
Block a user