This commit is contained in:
iutbgdin 2025-10-10 10:21:36 +02:00
parent 8383fc1edf
commit 6ad7555ad7
12 changed files with 81 additions and 24 deletions

View file

@ -10,7 +10,8 @@ add_executable(3d-renderer src/main.cpp
src/object/map.cpp src/object/map.h src/object/map.cpp src/object/map.h
src/object/object.cpp src/object/object.h src/object/object.cpp src/object/object.h
src/object/plane.cpp src/object/plane.h src/object/plane.cpp src/object/plane.h
src/object/point.cpp src/object/point.h) src/object/point.cpp src/object/point.h
src/util/math.h src/util/math.cpp)
include(GNUInstallDirs) include(GNUInstallDirs)
install(TARGETS 3d-renderer install(TARGETS 3d-renderer

View file

@ -1,3 +1,4 @@
#pragma once
#include <string> #include <string>
class object3D { class object3D {

View file

@ -26,12 +26,19 @@ void Map::load(std::string file, std::string txtpos) {
} else { } else {
pos = line.substr(0, line.find(":")); pos = line.substr(0, line.find(":"));
line.erase(0, line.find(":") + 1); line.erase(0, line.find(":") + 1);
if (line.substr(line.length() - 3, line.length()) == "obj") { std::string fname = line.substr(line.length() - 3, line.length());
if (fname == "obj") {
objs.push_back(new Object()); objs.push_back(new Object());
} else if (line.substr(line.length() - 3, line.length()) == "map") { } else if (fname == "map") {
if (fname == file) {
std::cerr << "Recursive map import detected! Stopping import for " << file << "." << std::endl;
return;
}
objs.push_back(new Map()); objs.push_back(new Map());
} else { } else {
std::cerr << "Object " << line << " have unknown extension (" << line.substr(line.length() - 2, line.length()) << ")." << std::endl; std::cerr << "Object " << line << " have unknown extension (" << fname << "). Import stopped for " << file << "." << std::endl;
return;
} }
(*objs.rbegin())->load(line, pos); (*objs.rbegin())->load(line, pos);
} }
@ -41,10 +48,9 @@ void Map::load(std::string file, std::string txtpos) {
std::cerr << "Could not open file " << file << std::endl; std::cerr << "Could not open file " << file << std::endl;
return; return;
} }
if (txtpos != "") {
this->pts = new Point(txtpos); this->pts = new Point(txtpos);
} }
}
std::ostream & operator<<(std::ostream& os, Map& m){ std::ostream & operator<<(std::ostream& os, Map& m){
return m.print(os); return m.print(os);

View file

@ -1,3 +1,4 @@
#pragma once
#include "object.h" #include "object.h"
class Map: public object3D { class Map: public object3D {

View file

@ -2,6 +2,7 @@
#include <iostream> #include <iostream>
#include "object.h" #include "object.h"
#include "../util/math.h"
void Object::load(std::string file, std::string txtpos) { void Object::load(std::string file, std::string txtpos) {
@ -33,19 +34,33 @@ void Object::load(std::string file, std::string txtpos) {
} }
this->pts = new Point(txtpos); this->pts = new Point(txtpos);
this->diameter = new float(this->diameterCalc());
} }
std::vector<Plane*> Object::getSides() { return this->plns; } std::vector<Plane*> Object::getSides() { return this->plns; }
bool Object::isContained() { return true; }; bool Object::isContained() { return true; }
std::ostream & operator<<(std::ostream& os, Object& o){ std::ostream & operator<<(std::ostream& os, Object& o){
return o.print(os); return o.print(os);
} }
std::ostream& Object::print(std::ostream& os) { std::ostream& Object::print(std::ostream& os) {
os << " - Object " << this << std::endl; os << " - Object " << this << "\n - Position:\n" << (*this->pts) << "\n - Diameter:\n - " << (*this->diameter) << std::endl;
for (Plane* i: this->getSides()) { for (Plane* i: this->getSides()) {
os << (*i); os << (*i);
} }
return os; return os;
} }
float Object::diameterCalc() {
float max_dist = 0;
for (Plane* p: this->plns) {
for (Point* i: p->getPoints()) {
float tmp = distance((*this->pts), (*i));
if ( tmp > max_dist) {
max_dist = tmp;
}
}
}
return max_dist;
}

View file

@ -1,3 +1,4 @@
#pragma once
#include "plane.h" #include "plane.h"
#include "3dObject.h" #include "3dObject.h"
@ -10,7 +11,8 @@ class Object: public object3D {
std::ostream& print(std::ostream& os); std::ostream& print(std::ostream& os);
friend std::ostream & operator<<(std::ostream& os, Object& o); friend std::ostream & operator<<(std::ostream& os, Object& o);
private: private:
float diameterCalc();
std::vector<Plane*> plns; std::vector<Plane*> plns;
Point* pts; Point* pts;
float* radius; float* diameter;
}; };

View file

@ -31,21 +31,24 @@ Plane::Plane(std::string& txt) {
this->color = new uint64_t(st64(txt.substr(txt.find(":") + 1, txt.length()))); this->color = new uint64_t(st64(txt.substr(txt.find(":") + 1, txt.length())));
txt.erase(txt.find(":") + 1, txt.length()); txt.erase(txt.find(":") + 1, txt.length());
std::string tmp = "";
while ((pos = txt.find(";")) != std::string::npos) { while ((pos = txt.find(";")) != std::string::npos) {
std::string tmp = txt.substr(0, pos); std::string tmp = txt.substr(0, pos);
this->pts.push_back(new Point(tmp)); this->pts.push_back(new Point(tmp));
txt.erase(0, pos + 1); txt.erase(0, pos + 1);
it++; it++;
} }
this->pts.push_back(new Point(txt)); tmp = txt.substr(0, txt.length - 1);
this->pts.push_back(new Point(tmp));
} }
std::vector<Point*> Plane::getPoints() { return pts; } std::vector<Point*> Plane::getPoints() { return pts; }
uint8_t Plane::getR() { return (*this->color) >> 32; } uint8_t Plane::getR() { return (*this->color) >> 40; }
uint8_t Plane::getG() { return ((*this->color) << 32) >> 56; } uint8_t Plane::getG() { return ((*this->color) << 8) >> 40; }
uint8_t Plane::getB() { return ((*this->color) << 40) >> 56; } uint8_t Plane::getB() { return ((*this->color) << 16) >> 40; }
uint8_t Plane::getA() { return ((*this->color) << 48) >> 56; } uint8_t Plane::getA() { return ((*this->color) << 24) >> 40; }
uint8_t Plane::getL() { return ((*this->color) << 56) >> 56; } uint8_t Plane::getL() { return ((*this->color) << 32) >> 40; }
uint8_t Plane::getZ() { return ((*this->color) << 40) >> 40; }
std::ostream & operator<<(std::ostream& os, Plane& p){ 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; 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;

View file

@ -1,3 +1,4 @@
#pragma once
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include <vector> #include <vector>
@ -13,6 +14,7 @@ class Plane {
uint8_t getB(); uint8_t getB();
uint8_t getA(); uint8_t getA();
uint8_t getL(); uint8_t getL();
uint8_t getZ();
friend std::ostream & operator<<(std::ostream& os, Plane& p); friend std::ostream & operator<<(std::ostream& os, Plane& p);
private: private:
std::vector<Point*> pts; std::vector<Point*> pts;

View file

@ -2,16 +2,23 @@
#include <string> #include <string>
Point::Point(std::string& txt) { Point::Point(std::string& txt) {
if (txt != "") {
size_t pos = 0; size_t pos = 0;
while ((pos = txt.find(",")) != std::string::npos) { while ((pos = txt.find(",")) != std::string::npos) {
this->xyz.push_back(new float(std::stof(txt.substr(0, pos)))); this->xyz.push_back(new float(std::stof(txt.substr(0, pos))));
txt.erase(0, pos + 1); txt.erase(0, pos + 1);
} }
std::cout << txt << std::endl;
this->xyz.push_back(new float(std::stof(txt))); this->xyz.push_back(new float(std::stof(txt)));
} }
uint16_t Point::translate3D() { return 0; }; if (this->xyz.size() != 3) {
this->xyz = {new float(0), new float(0), new float(0)};
}
}
uint16_t Point::translate3D() { return 0; }
float Point::getX() { return (*this->xyz.at(0)); } float Point::getX() { return (*this->xyz.at(0)); }
float Point::getY() { return (*this->xyz.at(1)); } float Point::getY() { return (*this->xyz.at(1)); }

View file

@ -1,3 +1,4 @@
#pragma once
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include <vector> #include <vector>

10
src/util/math.cpp Normal file
View file

@ -0,0 +1,10 @@
#include <cmath>
#include "math.h"
float distance(Point& a, Point& b) {
return sqrt( pow(a.getX() - b.getX(), 2) +
pow(a.getY() - b.getY(), 2) +
pow(a.getZ() - b.getZ(), 2)
);
}

8
src/util/math.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef MATH_H
#define MATH_H
#include "../object/point.h"
float distance(Point& a, Point& b);
#endif // MATH_H