From 8383fc1edfe3db35901f6b95519e09d10193c607 Mon Sep 17 00:00:00 2001 From: joachim Date: Thu, 9 Oct 2025 21:33:37 +0200 Subject: [PATCH] load map/obj --- .gitignore | 3 ++ CMakeLists.txt | 19 ++++++++++++ src/main.cpp | 10 ++++++- src/object/3dObject.cpp | 9 ++++++ src/object/3dObject.h | 9 ++++++ src/object/map.cpp | 59 ++++++++++++++++++++++++++++++++++++++ src/object/map.h | 13 +++++++++ src/object/mapObject.cpp | 0 src/object/objObject.cpp | 13 --------- src/object/object.cpp | 51 ++++++++++++++++++++++++++++++++ src/object/object.h | 16 +++++++++++ src/object/plane.cpp | 56 ++++++++++++++++++++++++++++++++++++ src/object/plane.h | 20 +++++++++++++ src/object/planeObject.cpp | 12 -------- src/object/point.cpp | 23 +++++++++++++++ src/object/point.h | 16 +++++++++++ src/object/pointObject.cpp | 15 ---------- test/assets/carte.map | 4 +-- 18 files changed, 305 insertions(+), 43 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 src/object/3dObject.cpp create mode 100644 src/object/3dObject.h create mode 100644 src/object/map.cpp create mode 100644 src/object/map.h delete mode 100644 src/object/mapObject.cpp delete mode 100644 src/object/objObject.cpp create mode 100644 src/object/object.cpp create mode 100644 src/object/object.h create mode 100644 src/object/plane.cpp create mode 100644 src/object/plane.h delete mode 100644 src/object/planeObject.cpp create mode 100644 src/object/point.cpp create mode 100644 src/object/point.h delete mode 100644 src/object/pointObject.cpp diff --git a/.gitignore b/.gitignore index 365da43..c974910 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,6 @@ *.out *.app +build/* +test/3d-renderer +*.user diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..319634b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.16) + +project(3d-renderer LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +add_executable(3d-renderer src/main.cpp + src/object/3dObject.cpp src/object/3dObject.h + src/object/map.cpp src/object/map.h + src/object/object.cpp src/object/object.h + src/object/plane.cpp src/object/plane.h + src/object/point.cpp src/object/point.h) + +include(GNUInstallDirs) +install(TARGETS 3d-renderer + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} +) diff --git a/src/main.cpp b/src/main.cpp index f4403b1..54e2a05 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,11 @@ -int main { +#include "object/map.h" + +int main() { + + Map m; + m.load("assets/carte.map", ""); + + std::cout << m << std::endl; + return 0; } diff --git a/src/object/3dObject.cpp b/src/object/3dObject.cpp new file mode 100644 index 0000000..0694394 --- /dev/null +++ b/src/object/3dObject.cpp @@ -0,0 +1,9 @@ +#include "3dObject.h" + +std::ostream & operator<<(std::ostream& os, object3D& m){ + return m.print(os); +} + +std::ostream& object3D::print(std::ostream& os) { + return os; +} diff --git a/src/object/3dObject.h b/src/object/3dObject.h new file mode 100644 index 0000000..480a914 --- /dev/null +++ b/src/object/3dObject.h @@ -0,0 +1,9 @@ +#include + +class object3D { + public: + object3D() {} + virtual void load(std::string file, std::string txtpos) {} + virtual std::ostream& print(std::ostream& os); + friend std::ostream & operator<<(std::ostream& os, object3D& m); +}; diff --git a/src/object/map.cpp b/src/object/map.cpp new file mode 100644 index 0000000..3af3601 --- /dev/null +++ b/src/object/map.cpp @@ -0,0 +1,59 @@ +#include +#include + +#include "map.h" + +void Map::load(std::string file, std::string txtpos) { + + std::string line; + + std::ifstream ifile; + ifile.open (file); + + bool first = true; + std::string pos; + + if (ifile.is_open()) { + while ( getline (ifile,line) ) { + if (first) { + if (line != "MAP") { + std::cerr << "File " << file << " does not have MAP signature (" << line << ")." << std::endl; + return; + } + first = false; + } else if (line == "" ) { + return; + } else { + pos = line.substr(0, line.find(":")); + line.erase(0, line.find(":") + 1); + if (line.substr(line.length() - 3, line.length()) == "obj") { + objs.push_back(new Object()); + } else if (line.substr(line.length() - 3, line.length()) == "map") { + objs.push_back(new Map()); + } else { + std::cerr << "Object " << line << " have unknown extension (" << line.substr(line.length() - 2, line.length()) << ")." << std::endl; + } + (*objs.rbegin())->load(line, pos); + } + } + ifile.close(); + } else { + std::cerr << "Could not open file " << file << std::endl; + return; + } + if (txtpos != "") { + this->pts = new Point(txtpos); + } +} + +std::ostream & operator<<(std::ostream& os, Map& m){ + return m.print(os); +} + +std::ostream& Map::print(std::ostream& os) { + os << "- Map " << this << std::endl; + for (object3D* i: this->getObject()) { + os << (*i); + } + return os; +} diff --git a/src/object/map.h b/src/object/map.h new file mode 100644 index 0000000..c931e81 --- /dev/null +++ b/src/object/map.h @@ -0,0 +1,13 @@ +#include "object.h" + +class Map: public object3D { + public: + Map() {} + std::vector getObject() { return this->objs; } + void load(std::string file, std::string txtpos); + virtual std::ostream& print(std::ostream& os); + friend std::ostream & operator<<(std::ostream& os, Map& p); + private: + std::vector objs; + Point* pts; +}; diff --git a/src/object/mapObject.cpp b/src/object/mapObject.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/object/objObject.cpp b/src/object/objObject.cpp deleted file mode 100644 index ea9cf0e..0000000 --- a/src/object/objObject.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "planeObject.cpp" - -class Object { - public: - Object(std::string txt); - Plane* getSides(); - char* getName(); - bool isContained(); - private: - Plane* sides; - char* name; - float* radius; -} diff --git a/src/object/object.cpp b/src/object/object.cpp new file mode 100644 index 0000000..d31b875 --- /dev/null +++ b/src/object/object.cpp @@ -0,0 +1,51 @@ +#include +#include + +#include "object.h" + +void Object::load(std::string file, std::string txtpos) { + + std::string line; + + std::ifstream ifile; + ifile.open (file); + + bool first = true; + + if (ifile.is_open()) { + while ( getline (ifile,line) ) { + if (first) { + if (line != "OBJECT") { + std::cerr << "File " << file << " does not contain OBJECT signature (" << line << ")." << std::endl; + return; + } + first = false; + } else if (line == "" ) { + return; + } else { + this->plns.push_back(new Plane(line)); + } + } + ifile.close(); + } else { + std::cerr << "Could not open file " << file << std::endl; + return; + } + + this->pts = new Point(txtpos); +} + +std::vector Object::getSides() { return this->plns; } +bool Object::isContained() { return true; }; + +std::ostream & operator<<(std::ostream& os, Object& o){ + return o.print(os); +} + +std::ostream& Object::print(std::ostream& os) { + os << " - Object " << this << std::endl; + for (Plane* i: this->getSides()) { + os << (*i); + } + return os; +} diff --git a/src/object/object.h b/src/object/object.h new file mode 100644 index 0000000..32d1a56 --- /dev/null +++ b/src/object/object.h @@ -0,0 +1,16 @@ +#include "plane.h" +#include "3dObject.h" + +class Object: public object3D { + public: + Object() {} + std::vector getSides(); + void load(std::string file, std::string txtpos); + bool isContained(); + std::ostream& print(std::ostream& os); + friend std::ostream & operator<<(std::ostream& os, Object& o); + private: + std::vector plns; + Point* pts; + float* radius; +}; diff --git a/src/object/plane.cpp b/src/object/plane.cpp new file mode 100644 index 0000000..2210d3a --- /dev/null +++ b/src/object/plane.cpp @@ -0,0 +1,56 @@ +#include +#include +#include + +#include "plane.h" + +uint64_t st64(std::string hex) { + std::reverse(hex.begin(), hex.end()); + uint64_t res = 0; + + for (int i = 0; i= 'A' && hex.at(i) <= 'Z') { + res += (hex.at(i) - 'A' + 10) * pow(16, i); + } else if (hex.at(i) >= 'a' && hex.at(i) <= 'z') { + res += (hex.at(i) - 'a' + 10) * pow(16, i); + } else if (hex.at(i) >= '0' && hex.at(i) <= '9') { + res += (hex.at(i) - '0') * pow(16, i); + } else { + std::cerr << "Color value seem not to be hexadecimal, returning 0 (" << hex << ")." << std::endl; + return 0; + } + } + + return res; +} + +Plane::Plane(std::string& txt) { + size_t pos = 0; + uint8_t it = 0; + + this->color = new uint64_t(st64(txt.substr(txt.find(":") + 1, txt.length()))); + txt.erase(txt.find(":") + 1, txt.length()); + + while ((pos = txt.find(";")) != std::string::npos) { + std::string tmp = txt.substr(0, pos); + this->pts.push_back(new Point(tmp)); + txt.erase(0, pos + 1); + it++; + } + this->pts.push_back(new Point(txt)); +} + +std::vector Plane::getPoints() { return pts; } +uint8_t Plane::getR() { return (*this->color) >> 32; } +uint8_t Plane::getG() { return ((*this->color) << 32) >> 56; } +uint8_t Plane::getB() { return ((*this->color) << 40) >> 56; } +uint8_t Plane::getA() { return ((*this->color) << 48) >> 56; } +uint8_t Plane::getL() { return ((*this->color) << 56) >> 56; } + +std::ostream & operator<<(std::ostream& os, Plane& p){ + os << " - Plan " << &p << "\n - R: " << (int)p.getR() << "\n - G: "<< (int)p.getG() << "\n - B: "<< (int)p.getB() << "\n - A: "<< (int)p.getA() << "\n - L: "<< (int)p.getL() << std::endl; + for (Point* i: p.getPoints()) { + os << (*i); + } + return os; +} diff --git a/src/object/plane.h b/src/object/plane.h new file mode 100644 index 0000000..99eaa68 --- /dev/null +++ b/src/object/plane.h @@ -0,0 +1,20 @@ +#include +#include +#include + +#include "point.h" + +class Plane { + public: + Plane(std::string& txt); + std::vector getPoints(); + uint8_t getR(); + uint8_t getG(); + uint8_t getB(); + uint8_t getA(); + uint8_t getL(); + friend std::ostream & operator<<(std::ostream& os, Plane& p); + private: + std::vector pts; + uint64_t* color; +}; diff --git a/src/object/planeObject.cpp b/src/object/planeObject.cpp deleted file mode 100644 index 58c0bd2..0000000 --- a/src/object/planeObject.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include -#include - -class Plane { - public: - Plane(std::string& text); - float* getPoints(); - uint64_t* getColor(); - private: - float* points; - uint64_t* color; -} diff --git a/src/object/point.cpp b/src/object/point.cpp new file mode 100644 index 0000000..1d11ea8 --- /dev/null +++ b/src/object/point.cpp @@ -0,0 +1,23 @@ +#include "point.h" +#include + +Point::Point(std::string &txt) { + size_t pos = 0; + + while ((pos = txt.find(",")) != std::string::npos) { + this->xyz.push_back(new float(std::stof(txt.substr(0, pos)))); + txt.erase(0, pos + 1); + } + this->xyz.push_back(new float(std::stof(txt))); +} + +uint16_t Point::translate3D() { return 0; }; + +float Point::getX() { return (*this->xyz.at(0)); } +float Point::getY() { return (*this->xyz.at(1)); } +float Point::getZ() { return (*this->xyz.at(2)); } + +std::ostream & operator<<(std::ostream& os, Point& p){ + os << " - Point " << &p << "\n - X: " << p.getX() << "\n - Y: "<< p.getY() << "\n - Z: "<< p.getZ() << std::endl; + return os; +} diff --git a/src/object/point.h b/src/object/point.h new file mode 100644 index 0000000..fe5be9a --- /dev/null +++ b/src/object/point.h @@ -0,0 +1,16 @@ +#include +#include +#include +#include + +class Point { + public: + Point(std::string& txt); + uint16_t translate3D(); + float getX(); + float getY(); + float getZ(); + friend std::ostream & operator<<(std::ostream& os, Point& p); + private: + std::vector xyz; +}; diff --git a/src/object/pointObject.cpp b/src/object/pointObject.cpp deleted file mode 100644 index ce482be..0000000 --- a/src/object/pointObject.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include - -class Point { - public: - Point(std::string& txt); - uint16_t 3dTranslate(); - float getX(); - float getY(); - float getZ(); - private: - float x; - float y; - float z; -} diff --git a/test/assets/carte.map b/test/assets/carte.map index 86b5887..e08f172 100644 --- a/test/assets/carte.map +++ b/test/assets/carte.map @@ -1,3 +1,3 @@ MAP -0,0,0:cube -0.5,0.5,0.5:light +0,0,0:assets/cube.obj +0.5,0.5,0.5:assets/light.obj