Compare commits

..

10 Commits

6 changed files with 335 additions and 241 deletions

View File

@@ -12,3 +12,4 @@ set (CMAKE_CXX_STANDARD 20)
add_executable(one main.cpp)
target_link_libraries(one m Qt6::Widgets Qt6::Gui)
# target_link_libraries(one m)

View File

@@ -1,12 +1,11 @@
#ifndef FASTMATH_H
#define FASTMATH_H
#include <math.h>
#include <iostream>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <vector>
#include <iostream>
#define SHIFT_AMOUNT 16
#define HALF_SHIFT (SHIFT_AMOUNT / 2)
@@ -21,85 +20,75 @@
struct decimal {
int32_t i;
constexpr decimal() = default;
// constexpr decimal() : i(0) {}
constexpr decimal(float i) : i(TO_INT(i)) {}
constexpr decimal(double i) : i(TO_INT(i)) {}
constexpr decimal(int32_t i) : i(i) {}
decimal() : i(0) {}
decimal(float i) : i(TO_INT(i)) {}
decimal(double i) : i(TO_INT(i)) {}
decimal(int32_t i) : i(i) {}
friend std::ostream& operator<<(std::ostream& os, const decimal &d) {
inline friend std::ostream &operator<<(std::ostream &os, const decimal &d) {
return (os << TO_FLOAT(d.i));
}
friend decimal operator+(const decimal &d1, const decimal &d2) {
inline friend decimal operator+(const decimal &d1, const decimal &d2) {
return {d1.i + d2.i};
}
decimal &operator+=(const decimal &d) { return (*this) = {i + d.i}; }
inline decimal &operator+=(const decimal &d) { return (*this) = {i + d.i}; }
friend decimal operator-(const decimal &d1, const decimal &d2) {
inline friend decimal operator-(const decimal &d1, const decimal &d2) {
return {d1.i - d2.i};
}
friend decimal operator-(const decimal &d) { return {-d.i}; }
inline friend decimal operator-(const decimal &d) { return {-d.i}; }
friend decimal operator*(const decimal &d1, const decimal &d2) {
inline friend decimal operator*(const decimal &d1, const decimal &d2) {
return {MUL_F(d1.i, d2.i)};
}
decimal &operator*=(const decimal &d) { return (*this) = {MUL_F(i, d.i)}; }
inline decimal &operator*=(const decimal &d) {
return (*this) = {MUL_F(i, d.i)};
}
friend decimal operator/(const decimal &d1, const decimal &d2) {
inline friend decimal operator/(const decimal &d1, const decimal &d2) {
return {DIV_F(d1.i, d2.i)};
}
friend bool operator<(const decimal &d1, const decimal &d2) {
inline friend bool operator<(const decimal &d1, const decimal &d2) {
return d1.i < d2.i;
}
friend bool operator>(const decimal &d1, const decimal &d2) {
inline friend bool operator>(const decimal &d1, const decimal &d2) {
return d1.i > d2.i;
}
friend bool operator<=(const decimal &d1, const decimal &d2) {
inline friend bool operator<=(const decimal &d1, const decimal &d2) {
return d1.i <= d2.i;
}
friend bool operator>=(const decimal &d1, const decimal &d2) {
inline friend bool operator>=(const decimal &d1, const decimal &d2) {
return d1.i >= d2.i;
}
friend bool operator==(const decimal &d1, const decimal &d2) {
inline friend bool operator==(const decimal &d1, const decimal &d2) {
return d1.i == d2.i;
}
friend bool operator!=(const decimal &d1, const decimal &d2) {
inline friend bool operator!=(const decimal &d1, const decimal &d2) {
return d1.i != d2.i;
}
decimal &operator=(decimal const &in) {
if (this != &in) {
std::destroy_at(this);
std::construct_at(this, in);
}
return *this;
}
decimal sqrt() { return {((int32_t)sqrtf(i)) << HALF_SHIFT}; }
inline decimal sqrt() { return {((int32_t)sqrtf(i)) << HALF_SHIFT}; }
float to_float() { return TO_FLOAT(i); }
bool isSmall() { return (abs(i) < (1 << (HALF_SHIFT - 1))); }
inline float to_float() { return TO_FLOAT(i); }
inline bool isSmall() { return (abs(i) < (1 << (HALF_SHIFT - 1))); }
};
template <int n, class Dev> struct vec {
vec(decimal newV[n]) {
for (int i = 0; i < n; i++) {
v[i] = newV[i];
}
}
decimal v[n];
vec(std::vector<decimal> newV) {
for (int i = 0; i < n; i++) {
v[i] = newV[i];
}
}
constexpr vec() noexcept = default;
vec() : v{} {}
template <class... Args>
constexpr vec(Args... args) noexcept : v{static_cast<decimal>(args)...} {
static_assert(sizeof...(Args) == n, "Wrong number of elements for vec");
}
friend Dev operator+(const vec<n, Dev> &v1, const vec<n, Dev> &v2) {
Dev newV = {};
@@ -124,7 +113,7 @@ template <int n, class Dev> struct vec {
for (int i = 0; i < n; i++) {
newV.v[i] = v1.v[i] - v2.v[i];
}
return static_cast<Dev>(newV);
return newV;
}
friend std::ostream &operator<<(std::ostream &os, const vec<n, Dev> &v) {
@@ -171,10 +160,10 @@ template <int n, class Dev> struct vec {
return v * d;
}
decimal operator*(const vec<n, Dev> &vec) {
decimal res;
friend decimal operator*(const vec<n, Dev> &v1, const vec<n, Dev> &v2) {
decimal res = decimal(0.0f);
for (int i = 0; i < n; i++) {
res += vec.v[i] * v[i];
res += v1.v[i] * v2.v[i];
}
return res;
}
@@ -204,44 +193,47 @@ template <int n, class Dev> struct vec {
return (*this * f);
}
protected:
decimal v[n];
constexpr static Dev zero() {
Dev newV = {};
for (int i = 0; i < n; i++) {
newV[i] = decimal(0);
}
return newV;
}
};
struct vec2 : public vec<2, vec2> {
vec2() : vec<2, vec2>() {}
vec2(float x, float y) : vec<2, vec2>(decimal(x), decimal(y)) {}
vec2(float x, float y) : vec<2, vec2>({decimal(x), decimal(y)}) {}
vec2(double x, double y) : vec<2, vec2>(decimal(x), decimal(y)) {}
vec2(double x, double y) : vec<2, vec2>({decimal(x), decimal(y)}) {}
vec2(int32_t x, int32_t y) : vec<2, vec2>(decimal(x), decimal(y)) {}
vec2(int32_t x, int32_t y) : vec<2, vec2>({decimal(x), decimal(y)}) {}
vec2(decimal x, decimal y) : vec<2, vec2>({x, y}) {}
vec2(decimal x, decimal y) : vec<2, vec2>(x, y) {}
decimal &x() { return v[0]; }
decimal &y() { return v[1]; }
};
struct vec3 : public vec<3, vec3> {
vec3() : vec<3, vec3>() {}
constexpr vec3() : vec<3, vec3>() {}
vec3(float x, float y, float z)
: vec<3, vec3>({decimal(x), decimal(y), decimal(z)}) {}
constexpr 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)}) {}
constexpr 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)}) {}
constexpr 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}) {}
constexpr vec3(decimal x, decimal y, decimal z) : vec<3, vec3>(x, y, z) {}
decimal &x() { return v[0]; }
decimal &y() { return v[1]; }
decimal &z() { return v[2]; }
inline decimal &x() { return v[0]; }
inline decimal &y() { return v[1]; }
inline decimal &z() { return v[2]; }
vec3 cross(vec3 &v) {
inline vec3 cross(vec3 &v) {
return vec3((y() * v.z()) - (z() * v.y()),
(z() * v.x()) - (x() * v.z()),
(x() * v.y()) - (y() * v.x()));
@@ -249,18 +241,18 @@ struct vec3 : public vec<3, vec3> {
};
struct vec4 : public vec<4, vec4> {
vec4() : vec<4, vec4>() {}
constexpr vec4() : vec<4, vec4>() {}
vec4(float x, float y, float z, float w)
: vec<4, vec4>({decimal(x), decimal(y), decimal(z), decimal(w)}) {}
: vec<4, vec4>(decimal(x), decimal(y), decimal(z), decimal(w)) {}
vec4(double x, double y, double z, double w)
: vec<4, vec4>({decimal(x), decimal(y), decimal(z), decimal(w)}) {}
: vec<4, vec4>(decimal(x), decimal(y), decimal(z), decimal(w)) {}
vec4(int32_t x, int32_t y, int32_t z, int32_t w)
: vec<4, vec4>({decimal(x), decimal(y), decimal(z), decimal(w)}) {}
: vec<4, vec4>(decimal(x), decimal(y), decimal(z), decimal(w)) {}
vec4(decimal x, decimal y, decimal z) : vec<4, vec4>({x, y, z}) {}
vec4(vec3 v, decimal w) : vec<4, vec4>(v.x(), v.y(), v.z(), w) {}
decimal &x() { return v[0]; }
decimal &y() { return v[1]; }
@@ -268,107 +260,152 @@ struct vec4 : public vec<4, vec4> {
decimal &w() { return v[3]; }
};
// template <int n, class Dev> struct mat {
//
// mat(decimal newM[n * n]) {
// for (int i = 0; i < n * n; i++) {
// m[i] = newM[i];
// }
// }
//
// mat(std::vector<decimal> newM) {
// for (int i = 0; i < n * n; i++) {
// m[i] = newM[i];
// }
// }
//
// mat() : m{} {}
//
// friend Dev operator+(const mat<n, Dev> &m1, const mat<n, Dev> &m2) {
// Dev newM = {};
//
// for (int i = 0; i < n * n; i++) {
// newM.v[i] = m1.m[i] + m2.m[i];
// }
// return newM;
// }
// friend Dev operator+=(const mat<n, Dev> &m1, const mat<n, Dev> &m2) {
// Dev newM = {};
//
// for (int i = 0; i < n * n; i++) {
// newM.m[i] = m1.m[i] + m2.m[i];
// }
// return newM;
// }
//
// friend Dev operator-(const mat<n, Dev> &m1, const mat<n, Dev> &m2) {
// Dev newM = {};
//
// for (int i = 0; i < n * n; i++) {
// newM.m[i] = m1.m[i] - m2.m[i];
// }
// return static_cast<Dev>(newM);
// }
//
// friend std::ostream &operator<<(std::ostream &os, const mat<n, Dev> &m) {
// os << "(" << m.m[0];
// for (int i = 1; i < n * n; i++) {
// os << ", " << m.m[i];
// }
// return (os << ")" << std::endl);
// }
//
// friend Dev operator*(const mat<n, Dev> &m, const decimal &d) {
// int32_t f = d.i >> HALF_SHIFT;
//
// Dev newM = {};
// for (int i = 0; i < n * n; i++) {
// newM.m[i] = (m.m[i].i >> HALF_SHIFT) * f;
// }
// return newM;
// }
//
// friend Dev operator*(const decimal &d, const mat<n, Dev> &v) {
// return v * d;
// }
//
// Dev operator*(const mat<n, Dev> &mat) {
// Dev newM = {};
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < n; j++) {
// newM.m += mat.v[i * n] * m[i];
// }
// }
// return res;
// }
//
// friend bool operator==(const mat<n, Dev> &v1, const mat<n, Dev> &m2) {
// bool res = true;
// for (int i = 0; i < n; i++) {
// res &= v1.v[i] == m2.v[i];
// }
// return res;
// }
// bool isSmall() {
// for (int i = 0; i < n; i++) {
// if (!v[i].isSmall())
// return false;
// }
// return true;
// }
// decimal &operator[](const int &i) { return v[i]; }
//
// decimal len_sq() { return *this * *this; }
//
// decimal len() { return this->len_sq().sqrt(); }
//
// Dev normalize() {
// decimal f = decimal(1.0) / this->len();
// return (*this * f);
// }
//
// protected:
// decimal m[n * n];
// };
template <int n, class Dev> struct mat {
decimal m[n * n];
static const int size = n;
friend Dev operator+(const mat<n, Dev> &m1, const mat<n, Dev> &m2) {
Dev newM = {};
for (int i = 0; i < n * n; i++) {
newM.v[i] = m1.m[i] + m2.m[i];
}
return newM;
}
friend Dev operator+=(const mat<n, Dev> &m1, const mat<n, Dev> &m2) {
Dev newM = {};
for (int i = 0; i < n * n; i++) {
newM.m[i] = m1.m[i] + m2.m[i];
}
return newM;
}
friend Dev operator-(const mat<n, Dev> &m1, const mat<n, Dev> &m2) {
Dev newM = {};
for (int i = 0; i < n * n; i++) {
newM.m[i] = m1.m[i] - m2.m[i];
}
return newM;
}
friend std::ostream &operator<<(std::ostream &os, const mat<n, Dev> &m) {
for (int i = 0; i < n; i++) {
os << "|" << m.m[i * n];
for (int j = 1; j < n; j++) {
os << ", " << m.m[i * n + j];
}
os << "|" << "\n";
}
return (os << std::endl);
}
template <class Dev1>
friend Dev1 operator*(const mat<n, Dev> &mat, const vec<n, Dev1> &v) {
Dev1 newV = vec<n, Dev1>::zero();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
newV[i] += mat.m[i * n + j] * v.v[j];
}
}
return newV;
}
decimal &operator[](const int &i) { return m[i]; }
friend Dev operator*(const mat<n, Dev> &m1, const mat<n, Dev> &m2) {
Dev newM = mat<n, Dev>::zero();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
newM[i * n + j] += m1.m[i * n + k] * m2.m[k * n + j];
}
}
}
return newM;
}
constexpr static Dev identity() {
Dev newM = {};
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j)
newM.m[i * n + i] = decimal(1.0f);
else
newM.m[i * n + j] = decimal(0.0f);
}
}
return newM;
}
constexpr static Dev zero() {
Dev newM = {};
for (int i = 0; i < n * n; i++) {
newM[i] = decimal(0);
}
return newM;
}
inline void set(int x, int y, decimal v) { m[y * n + x] = v; }
inline decimal get(int x, int y) { return m[y * n + x]; }
friend bool operator==(const mat<n, Dev> &m1, const mat<n, Dev> &m2) {
bool res = true;
for (int i = 0; i < n * n; i++) {
res &= m1.m[i] == m2.m[i];
}
return res;
}
bool isSmall() {
for (int i = 0; i < n; i++) {
if (!m[i].isSmall())
return false;
}
return true;
}
template <class Dev1> Dev1 cutTo() const {
static_assert(Dev1::size < n, "Can only convert to smaller matrix");
Dev1 newM = mat<Dev1::size, Dev1>::zero();
for (int i = 0; i < Dev1::size; i++) {
for (int j = 0; j < Dev1::size; j++) {
newM.m[Dev1::size * i + j] = m[n * i + j];
}
}
return newM;
}
};
template <int n> struct matN : public mat<n, matN<n>> {};
struct mat3 : public mat<3, mat3> {};
struct mat4 : public mat<4, mat4> {
static mat4 translation(const vec3 &v) {
mat4 newM = mat4::identity();
for (int i = 0; i < 3; i++) {
newM[4 * i + 3] = v.v[i];
}
return newM;
}
static mat4 rotateOnX(float a) {
mat4 newM = mat4::identity();
newM.m[1 * 4 + 1] = cos(a), newM.m[2 * 4 + 2] = cos(a);
newM.m[1 * 4 + 2] = -sin(a), newM.m[2 * 4 + 1] = sin(a);
return newM;
}
static mat4 rotateOnY(float a) {
mat4 newM = mat4::identity();
newM.m[0 * 4 + 0] = cos(a), newM.m[2 * 4 + 2] = cos(a);
newM.m[0 * 4 + 2] = sin(a), newM.m[2 * 4 + 0] = -sin(a);
return newM;
}
static mat4 rotateOnZ(float a) {
mat4 newM = mat4::identity();
newM.m[0 * 4 + 0] = cos(a), newM.m[1 * 4 + 1] = cos(a);
newM.m[1 * 4 + 0] = sin(a), newM.m[0 * 4 + 1] = -sin(a);
return newM;
}
};
#endif

View File

@@ -6,7 +6,9 @@
#include <QApplication>
#include <QImage>
#include <QLabel>
#include <QObject>
#include <QPixmap>
#include <QTimer>
#include <chrono>
#include <functional>
#include <math.h>
@@ -80,7 +82,8 @@ int main(int argc, char *argv[]) {
std::chrono::steady_clock::time_point begin =
std::chrono::steady_clock::now();
renderer.render(&testModel);
renderer.render(&testModel, mat4::translation(vec3(0.0f, -1.0f, 5.0f)) *
mat4::rotateOnY(-1.5707963267948966f));
std::chrono::steady_clock::time_point end =
std::chrono::steady_clock::now();
@@ -89,7 +92,6 @@ int main(int argc, char *argv[]) {
begin)
.count()
<< "[ms]" << std::endl;
QApplication a(argc, argv);
uint8_t *pixel = new uint8_t[64 * 64 * 3];
/*for (int i = 0; i < 64 * 64 * 3; i++) {
@@ -102,25 +104,53 @@ int main(int argc, char *argv[]) {
}
};
for (int x = 0; x < 64; x++) {
for (int y = 0; y < 64; y++) {
uint32_t result[3] = {};
addTo((target.width * y * 2 + x * 2) * 3, result);
addTo((target.width * (y * 2 + 1) + x * 2) * 3, result);
addTo((target.width * y * 2 + (x * 2 + 1)) * 3, result);
addTo((target.width * (y * 2 + 1) + (x * 2 + 1)) * 3, result);
for (int c = 0; c < 3; c++) {
pixel[(WIDTH * (WIDTH - y - 1) + WIDTH - x - 1) * 3 + c] =
result[c] >> 2;
}
}
}
QApplication a(argc, argv);
QWidget widget;
widget.setAutoFillBackground(true);
widget.setGeometry(0, 0, 500, 500);
QLabel display(&widget);
QImage img((unsigned char *)pixel, 64, 64, QImage::Format_RGB888);
display.setPixmap(QPixmap::fromImage(img).scaled(widget.size()));
// display.setPixmap(QPixmap::fromImage(img).scaled(widget.size()));
float rot = 0.f;
std::function<void()> renderLoop = [&addTo, &target, &pixel, &renderer,
&display, &widget, &img, &rot]() {
std::chrono::steady_clock::time_point begin =
std::chrono::steady_clock::now();
renderer.render(&testModel, mat4::translation(vec3(0.0f, -1.0f, 5.0f)) *
mat4::rotateOnY(rot));
std::chrono::steady_clock::time_point end =
std::chrono::steady_clock::now();
std::cout << "Time difference = "
<< std::chrono::duration_cast<std::chrono::milliseconds>(
end - begin)
.count()
<< "[ms]" << std::endl;
for (int x = 0; x < 64; x++) {
for (int y = 0; y < 64; y++) {
uint32_t result[3] = {};
addTo((target.width * y * 2 + x * 2) * 3, result);
addTo((target.width * (y * 2 + 1) + x * 2) * 3, result);
addTo((target.width * y * 2 + (x * 2 + 1)) * 3, result);
addTo((target.width * (y * 2 + 1) + (x * 2 + 1)) * 3, result);
for (int c = 0; c < 3; c++) {
pixel[(WIDTH * (WIDTH - y - 1) + WIDTH - x - 1) * 3 + c] =
result[c] >> 2;
}
}
}
// QImage img((unsigned char *)pixel, 64, 64, QImage::Format_RGB888);
display.setPixmap(QPixmap::fromImage(img).scaled(widget.size() * 1));
rot += 0.1f;
};
renderLoop();
widget.show();
QTimer timer;
timer.setInterval(20);
timer.start();
QObject::connect(&timer, &QTimer::timeout, &widget, renderLoop);
return a.exec();
}

View File

@@ -13,6 +13,8 @@ struct polygon {
decimal bounding[4]; // min x, max x, min y, max y
vec3 normals[3];
vec3 colors[3];
vec3 barycentrics;
vec3 boundingBarycentrics;
polygon(const vec3 &v1, const vec3 &v2, const vec3 &v3)
: points{v1, v2, v3}, delta{} {}
@@ -61,13 +63,13 @@ struct polygon {
vec3 avgNormal() {
vec3 result;
for (int i = 0; i < 3;i++) {
for (int i = 0; i < 3; i++) {
result += normals[i];
}
return result * decimal(0.3333);
}
const bool contains(const vec3 &p) {
const bool depContains(const vec3 &p) {
// if (skip)
// return false;
for (int i = 0; i < 3; i++) {
@@ -80,6 +82,14 @@ struct polygon {
}
return true;
}
const bool contains(const vec3 &p) {
if (small)
return true;
else
return (barycentrics[0] >= decimal(-0.01)) &&
(barycentrics[1] >= decimal(-0.01)) &&
(barycentrics[2] >= decimal(-0.01));
}
friend std::ostream &operator<<(std::ostream &os, const polygon &p) {
for (int i = 0; i < 3; i++) {
os << p.points[i];
@@ -87,40 +97,37 @@ struct polygon {
return os;
}
vec3 calcNormal(vec3 barycentrics) {
vec3 result = normals[0] * barycentrics[0] +
normals[1] * barycentrics[1] +
normals[2] * barycentrics[2];
if (result.isSmall())
return normals[0];
else
return result.normalize();
vec3 calcNormal() {
return normals[0] * boundingBarycentrics[0] +
normals[1] * boundingBarycentrics[1] +
normals[2] * boundingBarycentrics[2];
}
vec3 calcColor(vec3 barycentrics) {
return colors[0] * barycentrics[0] + colors[1] * barycentrics[1] +
colors[2] * barycentrics[2];
vec3 calcColor() {
return colors[0] * boundingBarycentrics[0] +
colors[1] * boundingBarycentrics[1] +
colors[2] * boundingBarycentrics[2];
}
decimal calcDepth(vec3 barycentrics) {
return points[0].z() * barycentrics[0] +
points[1].z() * barycentrics[1] +
points[2].z() * barycentrics[2];
decimal calcDepth() {
return points[0].z() * boundingBarycentrics[0] +
points[1].z() * boundingBarycentrics[1] +
points[2].z() * boundingBarycentrics[2];
}
vec3 calcBarycentric(vec3 s) {
void calcBarycentric(vec3 s) {
// if (small)
// return vec3(decimal(0.333), decimal(0.333), decimal(0.333));
vec3 result;
result[0] = (points[1].x() - s.x()) * (points[2].y() - s.y()) -
(points[2].x() - s.x()) * (points[1].y() - s.y());
result[1] = (points[2].x() - s.x()) * (points[0].y() - s.y()) -
(points[0].x() - s.x()) * (points[2].y() - s.y());
result = result * baryFactor;
result[2] = decimal(1.0) - result[1] - result[0];
barycentrics[0] = (points[1].x() - s.x()) * (points[2].y() - s.y()) -
(points[2].x() - s.x()) * (points[1].y() - s.y());
barycentrics[1] = (points[2].x() - s.x()) * (points[0].y() - s.y()) -
(points[0].x() - s.x()) * (points[2].y() - s.y());
barycentrics = barycentrics * baryFactor;
barycentrics[2] = decimal(1.0) - barycentrics[1] - barycentrics[0];
// return result;
return vec3::max(vec3::min(result, vec3(1.0, 1.0, 1.0)),
vec3(0.0, 0.0, 0.0));
boundingBarycentrics = vec3::max(
vec3::min(barycentrics, vec3(1.0, 1.0, 1.0)), vec3(0.0, 0.0, 0.0));
}
};

View File

@@ -20,15 +20,16 @@ class Renderer {
bool clearTarget = true;
vec3 sunDir = vec3(1.0, -1.0, 1.0).normalize();
void toScreenSpace(vec3 *p) {
p->x() = p->x() / p->z() * decimal(2.0) * decimal(SCREEN_SPACE_SIZE) +
void toScreenSpace(vec3 *np, mat4 matrix) {
vec4 tp = (matrix * vec4(*np, decimal(1.0f)));
tp.x() = tp.x() / tp.z() * decimal(2.0) * decimal(SCREEN_SPACE_SIZE) +
decimal(SCREEN_SPACE_SIZE);
p->y() = p->y() / p->z() * decimal(2.0) * decimal(SCREEN_SPACE_SIZE) +
tp.y() = tp.y() / tp.z() * decimal(2.0) * decimal(SCREEN_SPACE_SIZE) +
decimal(SCREEN_SPACE_SIZE);
p->z() = p->z();
*np = vec3(tp.x(), tp.y(), tp.z());
}
void render(const model *model) {
void render(const model *model, const mat4 matrix) {
decimal widthScale =
decimal(SCREEN_SPACE_SIZE * 2) / decimal((float)target->width);
decimal heightScale =
@@ -44,6 +45,7 @@ class Renderer {
// memset((wchar_t *)target->pixels, 0,
// target->height * target->width * sizeof(target[0]));
target->clearDepth();
target->clearTarget();
}
vec3 verts[model->verts.size()] = {};
@@ -51,7 +53,13 @@ class Renderer {
std::copy(model->verts.begin(), model->verts.end(), verts);
for (int i = 0; i < model->verts.size(); i++) {
toScreenSpace(verts + i);
toScreenSpace(verts + i, matrix);
}
vec3 normals[model->normals.size()] = {};
mat3 normalMatrix = matrix.cutTo<mat3>();
for (int i = 0; i < model->normals.size(); i++) {
normals[i] = normalMatrix * model->normals[i];
}
polygon testP;
@@ -62,8 +70,7 @@ class Renderer {
testP.points[p] = verts[std::get<0>(model->faces[f + p])];
testP.colors[p] =
model->colors[std::get<0>(model->faces[f + p])];
testP.normals[p] =
model->normals[std::get<1>(model->faces[f + p])];
testP.normals[p] = normals[std::get<1>(model->faces[f + p])];
}
if ((testP.avgNormal() * vec3(0.0, 0.0, 1.0)) > decimal(0.))
continue;
@@ -72,39 +79,46 @@ class Renderer {
int startX = std::max<int>(
(testP.bounding[0] * invWidthScale).i >> SHIFT_AMOUNT, 0);
int endX =
std::min<int>((testP.bounding[1] * invWidthScale).i >> SHIFT_AMOUNT,(uint32_t)
target->width - 1);
int endX = std::min<int>((testP.bounding[1] * invWidthScale).i >>
SHIFT_AMOUNT,
(uint32_t)target->width - 1);
int startY = std::max<int>(
(testP.bounding[2] * invHeightScale).i >> SHIFT_AMOUNT, 0);
int endY =
std::min<int>((testP.bounding[3] * invHeightScale).i >> SHIFT_AMOUNT,
target->height - 1);
int endY = std::min<int>((testP.bounding[3] * invHeightScale).i >>
SHIFT_AMOUNT,
target->height - 1);
vec3 pos = vec3(testP.bounding[0], testP.bounding[2], 0.0);
for (int x = startX; x < endX; x++) {
for (int y = startY; y < endY; y++) {
if (testP.contains(pos)) {
if (testP.small)
continue;
vec3 factors = testP.calcBarycentric(pos);
decimal depth = testP.calcDepth(factors);
if (testP.depContains(pos)) {
testP.calcBarycentric(pos);
decimal depth = testP.calcDepth();
if (depth < target->getDepth(x, y)) {
// std::cout << factors << std::endl;
vec3 normal = testP.calcNormal(factors);
vec3 color = testP.calcColor(factors);
vec3 normal = testP.calcNormal();
vec3 color = testP.calcColor();
decimal lightFac =
std::max(normal * (-sunDir), decimal(0.0)) +
decimal(0.5);
// decimal reflection = target->setDepth(x, y,
// depth);
;
target->setDepth(x, y, depth);
target->set(x, y,
(color * decimal(120.0)) * lightFac);
// target->set(x, y,
// (normals + vec3(1.0, 1.0, 1.0)) *
// vec3(lightFac * decimal(200.0), 0, 0));
// target->set(x, y,
// (normal + vec3(1.0, 1.0, 1.0)) *
// decimal(120.0));
// target->set(x, y, factors * decimal(200.0));
// target->set(
// x, y,
// (testP.avgNormal() + vec3(1.0, 1.0, 1.0)) *
// decimal(120.0));
// target->set(x, y,
// testP.barycentrics * decimal(200.0));
// if (!factors.isSmall())
// target->set(x, y, vec3(0., 255.0, 0.));
}

View File

@@ -32,6 +32,11 @@ class Rendertarget {
depth[i].i = std::numeric_limits<int32_t>::max();
}
}
void clearTarget() {
for (int i = 0; i < width * height * 3; i++) {
pixels[i] = 0;
}
}
uint8_t *pixels;
decimal *depth;