Compare commits

...

3 Commits

5 changed files with 117 additions and 52 deletions

5
.clangd Normal file
View File

@@ -0,0 +1,5 @@
If:
PathMatch: .*\.hpp
CompileFlags:
Add: [-std=c++20]

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
build

View File

@@ -3,7 +3,12 @@ cmake_minimum_required(VERSION 3.15)
project(SDFVisual VERSION 0.1
DESCRIPTION "CPU SDF Renderer"
LANGUAGES CXX)
find_package(Qt6 REQUIRED COMPONENTS
Widgets Gui
)
set (CMAKE_CXX_STANDARD 20)
add_executable(one main.cpp)
target_link_libraries(one m)
target_link_libraries(one m Qt6::Widgets Qt6::Gui)

View File

@@ -3,8 +3,10 @@
#include <cmath>
#include <iostream>
#include <sstream>
#include <stdint.h>
#include <stdlib.h>
#include <vector>
#define SHIFT_AMOUNT 16
#define HALF_SHIFT (SHIFT_AMOUNT / 2)
@@ -20,6 +22,7 @@ struct decimal {
const int32_t i;
decimal() : i(0) {}
decimal(float i) : i(TO_INT(i)) {}
decimal(double i) : i(TO_INT(i)) {}
decimal(int32_t i) : i(i) {}
@@ -71,70 +74,122 @@ struct decimal {
float to_float() { return TO_FLOAT(i); }
};
struct vec3 {
const decimal x;
const decimal y;
const decimal z;
template <int n, class Dev> struct vec {
vec3(float x, float y, float z)
: x(decimal(x)), y(decimal(y)), z(decimal(z)) {}
vec3(double x, double y, double z)
: x(decimal(x)), y(decimal(y)), z(decimal(z)) {}
vec3(int32_t x, int32_t y, int32_t z)
: x(decimal(x)), y(decimal(y)), z(decimal(z)) {}
vec3(decimal x, decimal y, decimal z) : x(x), y(y), z(z) {}
friend vec3 operator+(const vec3 &v1, const vec3 &v2) {
return vec3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
}
friend vec3 operator-(const vec3 &v1, const vec3 &v2) {
return vec3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
}
vec3 operator-() { return vec3(-x, -y, -z); }
friend vec3 operator*(const vec3 &v, const decimal &n) {
int32_t f = n.i >> HALF_SHIFT;
return vec3({(v.x.i >> HALF_SHIFT) * f}, {(v.y.i >> HALF_SHIFT) * f},
{(v.z.i >> HALF_SHIFT) * f});
}
friend vec3 operator*(const decimal &n, const vec3 &v) { return v * n; }
decimal operator*(const vec3 &v) { return v.x * x + v.y * y + v.z * z; }
friend bool operator==(const vec3 &v1, const vec3 &v2) {
return (v1.x == v2.x) & (v1.y == v2.y) & (v1.z == v2.z);
}
vec3 &operator=(vec3 const &in) {
if (this != &in) {
std::destroy_at(this);
std::construct_at(this, in);
vec(decimal newV[n]) {
for (int i = 0; i < n; i++) {
v[i] = newV[i];
}
return *this;
}
friend std::ostream &operator<<(std::ostream &os, const vec3 &v) {
return (os << "(" << v.x << ", " << v.y << ", " << v.z << ")"
<< std::endl);
vec(std::vector<decimal> newV) {
for (int i = 0; i < n; i++) {
v[i] = newV[i];
}
}
vec3 cross(const vec3 &v) {
return vec3((y * v.z) - (z * v.y), (z * v.x) - (x * v.z),
(x * v.y) - (y * v.x));
vec() : v{} {}
friend Dev operator+(const vec<n, Dev> &v1, const vec<n, Dev> &v2) {
Dev newV = {};
for (int i = 0; i < n; i++) {
newV.v[i] = v1.v[i] + v2.v[i];
}
return newV;
}
friend Dev operator-(const vec<n, Dev> &v1, const vec<n, Dev> &v2) {
Dev newV = {};
for (int i = 0; i < n; i++) {
newV.v[i] = v1.v[i] - v2.v[i];
}
return static_cast<Dev>(newV);
}
friend std::ostream &operator<<(std::ostream &os, const vec<n, Dev> &v) {
os << "(" << v.v[0];
for (int i = 1; i < n; i++) {
os << ", " << v.v[i];
}
return (os << ")" << std::endl);
}
Dev operator-() {
Dev newV = {};
for (int i = 0; i < n; i++) {
newV.v[i] = -v[i];
}
return newV;
}
friend Dev operator*(const vec<n, Dev> &v, const decimal &d) {
int32_t f = d.i >> HALF_SHIFT;
Dev newV = {};
for (int i = 0; i < n; i++) {
newV.v[i] = (v.v[i].i >> HALF_SHIFT) * f;
}
return newV;
}
friend Dev operator*(const decimal &d, const vec<n, Dev> &v) {
return v * d;
}
decimal operator*(const vec<n, Dev> &vec) {
decimal res;
for (int i = 0; i < n; i++) {
res += vec.v[i] * v[i];
}
return res;
}
friend bool operator==(const vec<n, Dev> &v1, const vec<n, Dev> &v2) {
bool res = true;
for (int i = 0; i < n; i++) {
res &= v1.v[i] == v2.v[i];
}
return res;
}
decimal len_sq() { return *this * *this; }
decimal len() { return this->len_sq().sqrt(); }
vec3 normalize() {
Dev normalize() {
decimal f = decimal(1.0) / this->len();
return (*this * f);
}
protected:
decimal v[n];
};
struct vec3 : public vec<3, vec3> {
vec3() : vec<3, vec3>() {}
vec3(float x, float y, float z)
: vec<3, vec3>({decimal(x), decimal(y), decimal(z)}) {}
vec3(double x, double y, double z)
: vec<3, vec3>({decimal(x), decimal(y), decimal(z)}) {}
vec3(int32_t x, int32_t y, int32_t z)
: vec<3, vec3>({decimal(x), decimal(y), decimal(z)}) {}
vec3(decimal x, decimal y, decimal z) : vec<3, vec3>({x, y, z}) {}
decimal x() const { return v[0]; }
decimal y() const { return v[1]; }
decimal z() const { return v[2]; }
vec3 cross(const vec3 &v) {
return vec3((y() * v.z()) - (z() * v.y()),
(z() * v.x()) - (x() * v.z()),
(x() * v.y()) - (y() * v.x()));
}
};
#endif

View File

@@ -2,7 +2,6 @@
#define POLYGON_H
#include "fastmath.hpp"
struct polygon {
const vec3 points[3];
@@ -15,7 +14,7 @@ struct polygon {
int n = (i + 1) % 3;
vec3 d = points[n] - points[i];
d = vec3(d.y, -d.x, d.z);
d = vec3(d.y(), -d.x(), d.z());
vec3 s = p - points[n];