This commit is contained in:
iutbgdin 2025-10-10 15:32:57 +02:00
parent c00ccdaf9c
commit b38a84b51d
18 changed files with 156 additions and 70 deletions

View file

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

View file

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

View file

@ -2,10 +2,12 @@
#include <iostream>
#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);
}

View file

@ -1,28 +1,7 @@
#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;
}
#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<Point*> 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);
}

View file

@ -9,12 +9,7 @@ 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();
uint8_t getZ();
uint64_t getColor();
friend std::ostream & operator<<(std::ostream& os, Plane& p);
private:
std::vector<Point*> pts;

View file

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

6
src/render/camera.cpp Normal file
View file

@ -0,0 +1,6 @@
#include "camera.h"
Camera::Camera()
{
}

20
src/render/camera.h Normal file
View file

@ -0,0 +1,20 @@
#ifndef CAMERA_H
#define CAMERA_H
#include <cstdint>
#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

6
src/render/image.cpp Normal file
View file

@ -0,0 +1,6 @@
#include "image.h"
Image::Image()
{
}

12
src/render/image.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef IMAGE_H
#define IMAGE_H
class Image {
public:
Image();
private:
};
#endif // IMAGE_H

6
src/render/pixel.cpp Normal file
View file

@ -0,0 +1,6 @@
#include "pixel.h"
Pixel::Pixel()
{
}

22
src/render/pixel.h Normal file
View file

@ -0,0 +1,22 @@
#ifndef PIXEL_H
#define PIXEL_H
#include <cstdint>
#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<int*> direction;
};
#endif // PIXEL_H

View file

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

View file

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

40
src/util/util.cpp Normal file
View file

@ -0,0 +1,40 @@
#include <cmath>
#include <cstdint>
#include <algorithm>
#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<hex.length(); i++) {
if (hex.at(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; }

18
src/util/util.h Normal file
View file

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

View file

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

View file

@ -1,2 +1,2 @@
OBJECT
0,0,0:FF00FFFF10
0,0,0:FF00FFFF10AB