load map/obj

This commit is contained in:
Joachim Rey 2025-10-09 21:33:37 +02:00
parent 6b454d46ad
commit 8383fc1edf
18 changed files with 305 additions and 43 deletions

3
.gitignore vendored
View file

@ -31,3 +31,6 @@
*.out *.out
*.app *.app
build/*
test/3d-renderer
*.user

19
CMakeLists.txt Normal file
View file

@ -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}
)

View file

@ -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; return 0;
} }

9
src/object/3dObject.cpp Normal file
View file

@ -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;
}

9
src/object/3dObject.h Normal file
View file

@ -0,0 +1,9 @@
#include <string>
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);
};

59
src/object/map.cpp Normal file
View file

@ -0,0 +1,59 @@
#include <fstream>
#include <iostream>
#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;
}

13
src/object/map.h Normal file
View file

@ -0,0 +1,13 @@
#include "object.h"
class Map: public object3D {
public:
Map() {}
std::vector<object3D*> 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<object3D*> objs;
Point* pts;
};

View file

@ -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;
}

51
src/object/object.cpp Normal file
View file

@ -0,0 +1,51 @@
#include <fstream>
#include <iostream>
#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<Plane*> 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;
}

16
src/object/object.h Normal file
View file

@ -0,0 +1,16 @@
#include "plane.h"
#include "3dObject.h"
class Object: public object3D {
public:
Object() {}
std::vector<Plane*> 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<Plane*> plns;
Point* pts;
float* radius;
};

56
src/object/plane.cpp Normal file
View file

@ -0,0 +1,56 @@
#include <algorithm>
#include <cmath>
#include <iostream>
#include "plane.h"
uint64_t st64(std::string hex) {
std::reverse(hex.begin(), hex.end());
uint64_t res = 0;
for (int i = 0; i<hex.length(); i++) {
if (hex.at(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<Point*> 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;
}

20
src/object/plane.h Normal file
View file

@ -0,0 +1,20 @@
#include <cstdint>
#include <string>
#include <vector>
#include "point.h"
class Plane {
public:
Plane(std::string& txt);
std::vector<Point*> 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<Point*> pts;
uint64_t* color;
};

View file

@ -1,12 +0,0 @@
#include <cstdint>
#include <string>
class Plane {
public:
Plane(std::string& text);
float* getPoints();
uint64_t* getColor();
private:
float* points;
uint64_t* color;
}

23
src/object/point.cpp Normal file
View file

@ -0,0 +1,23 @@
#include "point.h"
#include <string>
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;
}

16
src/object/point.h Normal file
View file

@ -0,0 +1,16 @@
#include <cstdint>
#include <string>
#include <vector>
#include <iostream>
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<float*> xyz;
};

View file

@ -1,15 +0,0 @@
#include <cstdint>
#include <string>
class Point {
public:
Point(std::string& txt);
uint16_t 3dTranslate();
float getX();
float getY();
float getZ();
private:
float x;
float y;
float z;
}

View file

@ -1,3 +1,3 @@
MAP MAP
0,0,0:cube 0,0,0:assets/cube.obj
0.5,0.5,0.5:light 0.5,0.5,0.5:assets/light.obj