From b84e2ee2c582c4ed5e6305370208cb98f3e90670 Mon Sep 17 00:00:00 2001 From: Redacted Date: Fri, 3 Jan 2025 22:16:27 -0500 Subject: [PATCH] Just pushing what I have. --- assets/models/cube.amo | 46 +++++++++ include/JGL/types/VertexArray.h | 1 + main.cpp | 6 +- src/internals/include/AMO.h | 9 ++ src/internals/include/WavefrontOBJ.h | 2 + src/internals/src/WavefrontOBJ.cpp | 141 ++++++++++++++++++++++++++- src/renderer/J2D.cpp | 2 +- src/types/VertexArray.cpp | 5 + 8 files changed, 204 insertions(+), 8 deletions(-) create mode 100644 assets/models/cube.amo create mode 100644 src/internals/include/AMO.h diff --git a/assets/models/cube.amo b/assets/models/cube.amo new file mode 100644 index 0000000..ae25511 --- /dev/null +++ b/assets/models/cube.amo @@ -0,0 +1,46 @@ +ao Cube 1 +v 8 +1.000000 1.000000 1.000000 +1.000000 1.000000 -1.000000 +1.000000 -1.000000 1.000000 +1.000000 -1.000000 -1.000000 +-1.000000 1.000000 1.000000 +-1.000000 1.000000 -1.000000 +-1.000000 -1.000000 1.000000 +-1.000000 -1.000000 -1.000000 +vt 14 +0.625000 0.500000 +0.875000 0.500000 +0.875000 0.750000 +0.625000 0.750000 +0.375000 0.750000 +0.625000 1.000000 +0.375000 1.000000 +0.375000 0.000000 +0.625000 0.000000 +0.625000 0.250000 +0.375000 0.250000 +0.125000 0.500000 +0.375000 0.500000 +0.125000 0.750000 +vn 6 +0.000000 0.000000 1.000000 +0.000000 -1.000000 0.000000 +-1.000000 0.000000 0.000000 +0.000000 0.000000 -1.000000 +1.000000 0.000000 0.000000 +0.000000 1.000000 0.000000 +f 12 +2 0 0 0 1 0 4 2 0 +7 3 1 3 4 1 2 3 1 +5 5 2 7 6 2 6 7 2 +7 8 3 5 9 3 1 10 3 +3 11 4 1 12 4 0 4 4 +1 13 5 5 12 5 4 0 5 +2 0 0 4 1 0 6 2 0 +7 3 1 2 4 1 6 3 1 +5 5 2 6 6 2 4 7 2 +7 8 3 1 9 3 3 10 3 +3 11 4 0 12 4 2 4 4 +1 13 5 4 12 5 0 0 5 +end diff --git a/include/JGL/types/VertexArray.h b/include/JGL/types/VertexArray.h index 9ac9790..fdc2e3b 100644 --- a/include/JGL/types/VertexArray.h +++ b/include/JGL/types/VertexArray.h @@ -93,6 +93,7 @@ public: const std::vector& vertex_normals = {}, const std::vector& texture_coordinates = {}); static VertexArray LoadWavefrontOBJ(const std::string& file_text); + static VertexArray LoadAMO(const std::string& file_text); }; diff --git a/main.cpp b/main.cpp index d19cf0c..46d189c 100644 --- a/main.cpp +++ b/main.cpp @@ -317,7 +317,7 @@ int main(int argc, char** argv) { window->SetResizable(true); window->SetVsyncEnabled(true); - std::ifstream file("assets/models/cube.obj"); + std::ifstream file("assets/models/cube.amo"); if (!file.is_open()) return -1; @@ -325,9 +325,9 @@ int main(int argc, char** argv) { buffer << file.rdbuf(); std::string file_text = buffer.str(); file.close(); - //std::cout << "File contents:\n" << file_text << std::endl; + std::cout << file_text << std::endl; - auto result = VertexArray::LoadWavefrontOBJ(file_text); + auto result = VertexArray::LoadAMO(file_text); ReWindow::Logger::Error.EnableConsole(false); ReWindow::Logger::Warning.EnableConsole(false); diff --git a/src/internals/include/AMO.h b/src/internals/include/AMO.h new file mode 100644 index 0000000..a707d74 --- /dev/null +++ b/src/internals/include/AMO.h @@ -0,0 +1,9 @@ +#pragma once +#include + +namespace JGL { + class VertexArray; + /* + VertexArray LoadAMO(const std::string& file_text); + */ +} \ No newline at end of file diff --git a/src/internals/include/WavefrontOBJ.h b/src/internals/include/WavefrontOBJ.h index fea87ed..1097921 100644 --- a/src/internals/include/WavefrontOBJ.h +++ b/src/internals/include/WavefrontOBJ.h @@ -3,5 +3,7 @@ namespace JGL { class VertexArray; + // Currently requires "Triangulate Mesh" ticked in Blender. + // TODO use the number of slashes in the first "face-line" to detect quads. VertexArray LoadWavefrontOBJ(const std::string& file_text); } \ No newline at end of file diff --git a/src/internals/src/WavefrontOBJ.cpp b/src/internals/src/WavefrontOBJ.cpp index e74cf6c..28d540f 100644 --- a/src/internals/src/WavefrontOBJ.cpp +++ b/src/internals/src/WavefrontOBJ.cpp @@ -1,4 +1,5 @@ #include "../include/WavefrontOBJ.h" +#include "../include/AMO.h" #include #include @@ -61,7 +62,19 @@ std::pair ParseVector3(const std::string& file_text, con return {Vector3(x_result.first, y_result.first, z_result.first), z_result.second + 1}; } -std::pair, unsigned long> ParseFaceData(const std::string& file_text, const unsigned long& offset) { +std::pair ParseVector4(const std::string& file_text, const unsigned long& offset) { + auto x_result = ParseNumber(file_text, offset); + auto y_result = ParseNumber(file_text, x_result.second + 1); + auto z_result = ParseNumber(file_text, y_result.second + 1); + auto w_result = ParseNumber(file_text, z_result.second + 1); + + if (x_result.second == offset || y_result.second == x_result.second || z_result.second == y_result.second || w_result.second == z_result.second) + return {Vector4(0, 0, 0, 0), offset}; + + return {Vector4(x_result.first, y_result.first, z_result.first, w_result.first), w_result.second + 1}; +} + +std::pair, unsigned long> ParseWavefrontFaceData(const std::string& file_text, const unsigned long& offset) { unsigned long new_offset = offset; std::array face_data; @@ -111,7 +124,6 @@ VertexArray JGL::LoadWavefrontOBJ(const std::string &file_text) { std::vector temp_vertices; unsigned long offset = 0; - while (offset < file_text.size()) { char c = file_text[offset]; @@ -161,7 +173,7 @@ VertexArray JGL::LoadWavefrontOBJ(const std::string &file_text) { else if (c == 'f' && offset + 1 < file_text.size() && file_text[offset + 1] == ' ') { offset += 2; - auto faceline_result = ParseFaceData(file_text, offset); + auto faceline_result = ParseWavefrontFaceData(file_text, offset); if (faceline_result.second != offset) { faceline_data.push_back(faceline_result.first); offset = faceline_result.second; @@ -200,4 +212,125 @@ VertexArray JGL::LoadWavefrontOBJ(const std::string &file_text) { final_indices.data(), final_indices.size(), final_normals.data(), final_normals.size(), final_uvs.data(), final_uvs.size()); -} \ No newline at end of file +} + +/* +VertexArray JGL::LoadAMO(const std::string& file_text) { + std::vector temp_vertices; + std::vector temp_normals; + std::vector temp_uvs; + std::vector> faceline_data; + std::string name; + + unsigned long offset = 0; + while (offset < file_text.size()) { + char c = file_text[offset]; + + // If we've dropped on a newline character somehow then just skip passed it. + if (c == '\n' || c == '\r') { + offset++; continue; + } + + // File name. + else if (offset + 2 < file_text.size() && c == 'a' && file_text[offset + 1] == 'o' && file_text[offset + 2] == ' ') { + offset += 3; + while (offset < file_text.size()) { + if (file_text[offset] != '\n' && file_text[offset] != '\r') + name.push_back(file_text[offset]), offset++; + else + break; + } + } + + // Vertices + else if (offset + 1 < file_text.size() && c == 'v' && file_text[offset + 1] == ' ') { + offset += 2; auto parsed_number = ParseNumber(file_text, offset); + + if (parsed_number.second == offset) + Logger::Fatal("We couldn't interpret the Vertex count at: " + std::to_string(offset)); + + unsigned long vertex_count = parsed_number.first; + offset = parsed_number.second; + + for (unsigned long i = 0; i < vertex_count; i++) { + // Skip by newlines. + while (file_text[offset] == '\n' || file_text[offset] == '\r') + offset++; + + auto parsed_vector3 = ParseVector3(file_text, offset); + if (parsed_vector3.second == offset) + Logger::Fatal("We couldn't interpret the Vertex at: " + std::to_string(offset)); + + temp_vertices.push_back(parsed_vector3.first); + offset = parsed_vector3.second; + } + } + + // UVs + else if (offset + 2 < file_text.size() && c == 'v' && file_text[offset + 1] == 't' && file_text[offset + 2] == ' ') { + offset += 3; auto parsed_number = ParseNumber(file_text, offset); + + if (parsed_number.second == offset) + Logger::Fatal("We couldn't interpret the UV count at: " + std::to_string(offset)); + + unsigned long uv_count = parsed_number.first; + offset = parsed_number.second; + + for (unsigned long i = 0; i < uv_count; i++) { + // Skip by newlines. + while (file_text[offset] == '\n' || file_text[offset] == '\r') + offset++; + + auto parsed_vector2 = ParseVector2(file_text, offset); + if (parsed_vector2.second == offset) + Logger::Fatal("We couldn't interpret the UV at: " + std::to_string(offset)); + + temp_uvs.push_back(parsed_vector2.first); + offset = parsed_vector2.second; + } + } + + // Normals + else if (offset + 2 < file_text.size() && c == 'v' && file_text[offset + 1] == 'n' && file_text[offset + 2] == ' ') { + offset += 3; auto parsed_number = ParseNumber(file_text, offset); + + if (parsed_number.second == offset) + Logger::Fatal("We couldn't interpret the Normal count at: " + std::to_string(offset)); + + unsigned long uv_count = parsed_number.first; + offset = parsed_number.second; + + for (unsigned long i = 0; i < uv_count; i++) { + // Skip by newlines. + while (file_text[offset] == '\n' || file_text[offset] == '\r') + offset++; + + auto parsed_vector3 = ParseVector3(file_text, offset); + if (parsed_vector3.second == offset) + Logger::Fatal("We couldn't interpret the Normal at: " + std::to_string(offset)); + + temp_normals.push_back(parsed_vector3.first); + offset = parsed_vector3.second; + } + } + + // Face Lines + else if (offset + 1 < file_text.size() && c == 'f' && file_text[offset + 1] == ' ') { + offset += 2; auto parsed_number = ParseNumber(file_text, offset); + + if (parsed_number.second == offset) + Logger::Fatal("We couldn't interpret the Face Line count at: " + std::to_string(offset)); + + unsigned long faceline_count = parsed_number.first; + for (unsigned long i = 0; i < faceline_count; i++) { + // Skip by newlines. + while (file_text[offset] == '\n' || file_text[offset] == '\r') + offset++; + + + } + } + + } +} + */ \ No newline at end of file diff --git a/src/renderer/J2D.cpp b/src/renderer/J2D.cpp index b8ea5ec..f705b4f 100644 --- a/src/renderer/J2D.cpp +++ b/src/renderer/J2D.cpp @@ -116,7 +116,7 @@ void JGL::J2D::End() { if (!OpenGLState::wasBlendEnabled) glDisable(GL_BLEND); - //Select whatever texture mapper was selected before. + // Select whatever texture mapper was selected before. glActiveTexture(GL_TEXTURE0 + OpenGLState::activeTextureUnit); glClientActiveTexture(GL_TEXTURE0 + OpenGLState::activeTextureUnit); diff --git a/src/types/VertexArray.cpp b/src/types/VertexArray.cpp index a2d99ac..26f07c4 100644 --- a/src/types/VertexArray.cpp +++ b/src/types/VertexArray.cpp @@ -1,6 +1,7 @@ #include #include #include "../internals/include/WavefrontOBJ.h" +#include "../internals/include/AMO.h" using namespace JGL; @@ -182,3 +183,7 @@ bool VertexArray::Static() { VertexArray VertexArray::LoadWavefrontOBJ(const std::string& file_text) { return JGL::LoadWavefrontOBJ(file_text); } + +VertexArray VertexArray::LoadAMO(const std::string& file_text) { + //return JGL::LoadAMO(file_text); +}