diff --git a/CMakeLists.txt b/CMakeLists.txt index c7ee768..d0a3bed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,10 @@ add_executable(3d-renderer src/main.cpp src/object/object.cpp src/object/object.h src/object/plane.cpp src/object/plane.h src/object/point.cpp src/object/point.h - src/util/math.h src/util/math.cpp) + src/util/util.h src/util/util.cpp + src/render/image.h src/render/image.cpp + src/render/pixel.h src/render/pixel.cpp + src/render/camera.h src/render/camera.cpp) include(GNUInstallDirs) install(TARGETS 3d-renderer diff --git a/src/object/map.cpp b/src/object/map.cpp index d412ab2..c623b97 100644 --- a/src/object/map.cpp +++ b/src/object/map.cpp @@ -5,6 +5,8 @@ void Map::load(std::string file, std::string txtpos) { + this->pts = new Point(txtpos); + std::string line; std::ifstream ifile; @@ -31,7 +33,7 @@ void Map::load(std::string file, std::string txtpos) { if (fname == "obj") { objs.push_back(new Object()); } else if (fname == "map") { - if (fname == file) { + if (line == file) { std::cerr << "Recursive map import detected! Stopping import for " << file << "." << std::endl; return; } @@ -48,8 +50,6 @@ void Map::load(std::string file, std::string txtpos) { std::cerr << "Could not open file " << file << std::endl; return; } - - this->pts = new Point(txtpos); } std::ostream & operator<<(std::ostream& os, Map& m){ @@ -57,7 +57,7 @@ std::ostream & operator<<(std::ostream& os, Map& m){ } std::ostream& Map::print(std::ostream& os) { - os << "- Map " << this << std::endl; + os << "# Map " << this << "\n - *Position*:\n" << (*this->pts); for (object3D* i: this->getObject()) { os << (*i); } diff --git a/src/object/object.cpp b/src/object/object.cpp index 3c0fc66..1dddada 100644 --- a/src/object/object.cpp +++ b/src/object/object.cpp @@ -2,10 +2,12 @@ #include #include "object.h" -#include "../util/math.h" +#include "../util/util.h" void Object::load(std::string file, std::string txtpos) { + this->pts = new Point(txtpos); + std::string line; std::ifstream ifile; @@ -22,7 +24,7 @@ void Object::load(std::string file, std::string txtpos) { } first = false; } else if (line == "" ) { - return; + break; } else { this->plns.push_back(new Plane(line)); } @@ -33,7 +35,6 @@ void Object::load(std::string file, std::string txtpos) { return; } - this->pts = new Point(txtpos); this->diameter = new float(this->diameterCalc()); } @@ -45,7 +46,7 @@ std::ostream & operator<<(std::ostream& os, Object& o){ } std::ostream& Object::print(std::ostream& os) { - os << " - Object " << this << "\n - Position:\n" << (*this->pts) << "\n - Diameter:\n - " << (*this->diameter) << std::endl; + os << "## Object " << this << "\n - *Position*:\n" << (*this->pts) << " - *Diameter*: " << (*this->diameter) << std::endl; for (Plane* i: this->getSides()) { os << (*i); } diff --git a/src/object/plane.cpp b/src/object/plane.cpp index 4da116e..f50fe7c 100644 --- a/src/object/plane.cpp +++ b/src/object/plane.cpp @@ -1,28 +1,7 @@ -#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; -} +#include "../util/util.h" Plane::Plane(std::string& txt) { size_t pos = 0; @@ -42,16 +21,12 @@ Plane::Plane(std::string& txt) { this->pts.push_back(new Point(tmp)); } +uint64_t Plane::getColor() { return (*this->color); } + std::vector Plane::getPoints() { return pts; } -uint8_t Plane::getR() { return (*this->color) >> 40; } -uint8_t Plane::getG() { return ((*this->color) << 8) >> 40; } -uint8_t Plane::getB() { return ((*this->color) << 16) >> 40; } -uint8_t Plane::getA() { return ((*this->color) << 24) >> 40; } -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){ - 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 << "### Plane " << &p << "\n - *R*: " << (int)getR(p.getColor()) << ", *G*: "<< (int)getG(p.getColor()) << ", *B*: "<< (int)getB(p.getColor()) << ", *A*: "<< (int)getA(p.getColor()) << ", *L*: "<< (int)getL(p.getColor()) << ", *Z*: "<< (int)getZ(p.getColor()) << std::endl; for (Point* i: p.getPoints()) { os << (*i); } diff --git a/src/object/plane.h b/src/object/plane.h index 0d5ab47..6c4f6c8 100644 --- a/src/object/plane.h +++ b/src/object/plane.h @@ -9,12 +9,7 @@ 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(); - uint8_t getZ(); + uint64_t getColor(); friend std::ostream & operator<<(std::ostream& os, Plane& p); private: std::vector pts; diff --git a/src/object/point.cpp b/src/object/point.cpp index b4a62c8..9f50ff1 100644 --- a/src/object/point.cpp +++ b/src/object/point.cpp @@ -9,13 +9,13 @@ Point::Point(std::string& txt) { this->xyz.push_back(new float(std::stof(txt.substr(0, pos)))); txt.erase(0, pos + 1); } - std::cout << txt << std::endl; this->xyz.push_back(new float(std::stof(txt))); } if (this->xyz.size() != 3) { this->xyz = {new float(0), new float(0), new float(0)}; } + } uint16_t Point::translate3D() { return 0; } @@ -25,6 +25,6 @@ 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; + os << " - *Point " << &p << "*: *X*: " << p.getX() << ", *Y*: "<< p.getY() << ", *Z*: "<< p.getZ() << std::endl; return os; } diff --git a/src/render/camera.cpp b/src/render/camera.cpp new file mode 100644 index 0000000..2ce55cb --- /dev/null +++ b/src/render/camera.cpp @@ -0,0 +1,6 @@ +#include "camera.h" + +Camera::Camera() +{ + +} diff --git a/src/render/camera.h b/src/render/camera.h new file mode 100644 index 0000000..410e3a0 --- /dev/null +++ b/src/render/camera.h @@ -0,0 +1,20 @@ +#ifndef CAMERA_H +#define CAMERA_H + +#include + +#include "../util/util.h" + +class Camera { + public: + Camera(); + uint16_t translateOnScreenX(Point& p); + uint16_t translateOnScreenY(Point& p); + private: + float focal_ecran; + uint16_t size_x; + uint16_t size_y; + Point* position; +}; + +#endif // CAMERA_H diff --git a/src/render/image.cpp b/src/render/image.cpp new file mode 100644 index 0000000..d564a79 --- /dev/null +++ b/src/render/image.cpp @@ -0,0 +1,6 @@ +#include "image.h" + +Image::Image() +{ + +} diff --git a/src/render/image.h b/src/render/image.h new file mode 100644 index 0000000..0fad47b --- /dev/null +++ b/src/render/image.h @@ -0,0 +1,12 @@ +#ifndef IMAGE_H +#define IMAGE_H + + +class Image { + public: + Image(); + private: + +}; + +#endif // IMAGE_H diff --git a/src/render/pixel.cpp b/src/render/pixel.cpp new file mode 100644 index 0000000..d738807 --- /dev/null +++ b/src/render/pixel.cpp @@ -0,0 +1,6 @@ +#include "pixel.h" + +Pixel::Pixel() +{ + +} diff --git a/src/render/pixel.h b/src/render/pixel.h new file mode 100644 index 0000000..d67429d --- /dev/null +++ b/src/render/pixel.h @@ -0,0 +1,22 @@ +#ifndef PIXEL_H +#define PIXEL_H + +#include + +#include "../util/util.h" +#include "../object/plane.h" + +class Pixel { + public: + Pixel(); + void colorMix(uint64_t color, uint8_t amount); + void setPosition(Point* p); + void setColor(uint64_t* c); + void setDirection(Plane& p); + private: + uint64_t* color; + Point* position; + std::vector direction; +}; + +#endif // PIXEL_H diff --git a/src/util/math.cpp b/src/util/math.cpp deleted file mode 100644 index 6b1c37d..0000000 --- a/src/util/math.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include - -#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) - ); -} diff --git a/src/util/math.h b/src/util/math.h deleted file mode 100644 index c611aa8..0000000 --- a/src/util/math.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef MATH_H -#define MATH_H - -#include "../object/point.h" - -float distance(Point& a, Point& b); - -#endif // MATH_H diff --git a/src/util/util.cpp b/src/util/util.cpp new file mode 100644 index 0000000..47bc631 --- /dev/null +++ b/src/util/util.cpp @@ -0,0 +1,40 @@ +#include +#include +#include + +#include "math.h" +#include "../object/point.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) + ); +} + +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) <= 'F') { + res += (hex.at(i) - 'A' + 10) * pow(16, i); + } else if (hex.at(i) >= 'a' && hex.at(i) <= 'f') { + 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; +} + +uint8_t getR(uint64_t color) { return (color << 16) >> 56; } +uint8_t getG(uint64_t color) { return (color << 24) >> 56; } +uint8_t getB(uint64_t color) { return (color << 32) >> 56; } +uint8_t getA(uint64_t color) { return (color << 40) >> 56; } +uint8_t getL(uint64_t color) { return (color << 48) >> 56; } +uint8_t getZ(uint64_t color) { return (color << 56) >> 56; } diff --git a/src/util/util.h b/src/util/util.h new file mode 100644 index 0000000..ed39b01 --- /dev/null +++ b/src/util/util.h @@ -0,0 +1,18 @@ +#pragma once +#ifndef MATH_H +#define MATH_H + +#include "../object/point.h" + +float distance(Point& a, Point& b); + +uint64_t st64(std::string hex); + +uint8_t getR(uint64_t color); +uint8_t getG(uint64_t color); +uint8_t getB(uint64_t color); +uint8_t getA(uint64_t color); +uint8_t getL(uint64_t color); +uint8_t getZ(uint64_t color); + +#endif // MATH_H diff --git a/test/assets/cube.obj b/test/assets/cube.obj index f46ed47..b23f2c2 100644 --- a/test/assets/cube.obj +++ b/test/assets/cube.obj @@ -1,7 +1,7 @@ OBJECT -0,0,1;0,1,1;1,0,1;1,1,1:FF00FF0000 -0,0,1;0,1,1;0,0,0;0,1,0:00FFFF0000 -0,0,0;0,0,1;1,0,1;1,0,0:FF00000000 -0,1,0;0,1,1;1,1,1;1,1,0:0000FF0000 -1,0,1;1,1,1;1,1,0;1,0,0:00FF000000 +0,0,1;0,1,1;1,0,1;1,1,1:FF00FF0000AB +0,0,1;0,1,1;0,0,0;0,1,0:00FFFF0000AB +0,0,0;0,0,1;1,0,1;1,0,0:FF00000000AB +0,1,0;0,1,1;1,1,1;1,1,0:0000FF0000AB +1,0,1;1,1,1;1,1,0;1,0,0:00FF000000AB diff --git a/test/assets/light.obj b/test/assets/light.obj index 9dbeb33..8b782df 100644 --- a/test/assets/light.obj +++ b/test/assets/light.obj @@ -1,2 +1,2 @@ OBJECT -0,0,0:FF00FFFF10 +0,0,0:FF00FFFF10AB