Compare commits

..

3 Commits

Author SHA1 Message Date
a1d8d374c5 feat: started on matrix calc 2025-12-20 21:08:20 +01:00
09bc4d9326 fix: reduced ram usage 2025-12-20 21:08:01 +01:00
beacb9a985 feat: added lighting 2025-12-20 21:07:16 +01:00
5 changed files with 161 additions and 21 deletions

View File

@@ -1,8 +1,8 @@
#ifndef FASTMATH_H
#define FASTMATH_H
#include <cmath>
#include <iostream>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <vector>
@@ -108,6 +108,14 @@ template <int n, class Dev> struct vec {
}
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 newV;
}
friend Dev operator-(const vec<n, Dev> &v1, const vec<n, Dev> &v2) {
Dev newV = {};
@@ -143,6 +151,20 @@ template <int n, class Dev> struct vec {
}
return newV;
}
static Dev max(const vec<n, Dev> &v1, const vec<n, Dev> &v2) {
Dev newV = {};
for (int i = 0; i < n; i++) {
newV.v[i] = std::max(v1.v[i], v2.v[i]);
}
return newV;
}
static Dev min(const vec<n, Dev> &v1, const vec<n, Dev> &v2) {
Dev newV = {};
for (int i = 0; i < n; i++) {
newV.v[i] = std::min(v1.v[i], v2.v[i]);
}
return newV;
}
friend Dev operator*(const decimal &d, const vec<n, Dev> &v) {
return v * d;
@@ -244,4 +266,108 @@ struct vec4 : public vec<4, vec4> {
decimal &z() { return v[2]; }
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];
// };
#endif

View File

@@ -98,7 +98,7 @@ int main(int argc, char *argv[]) {
std::function<void(int, uint32_t *)> addTo = [&target](int start,
uint32_t *arr) {
for (int c = 0; c < 3; c++) {
arr[c] += target.pixels[start + c].i >> SHIFT_AMOUNT;
arr[c] += target.pixels[start + c];
}
};

View File

@@ -58,6 +58,14 @@ struct polygon {
small = true;*/
}
vec3 avgNormal() {
vec3 result;
for (int i = 0; i++; i < 3) {
result += normals[i];
}
return result * decimal(0.3333);
}
const bool contains(const vec3 &p) {
// if (skip)
// return false;
@@ -82,6 +90,9 @@ struct polygon {
vec3 result = normals[0] * barycentrics[0] +
normals[1] * barycentrics[1] +
normals[2] * barycentrics[2];
if (result.isSmall())
return normals[0];
else
return result.normalize();
}
@@ -97,8 +108,8 @@ struct polygon {
}
vec3 calcBarycentric(vec3 s) {
if (small)
return vec3(decimal(0.333), decimal(0.333), decimal(0.333));
// 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());
@@ -106,7 +117,9 @@ struct polygon {
(points[0].x() - s.x()) * (points[2].y() - s.y());
result = result * baryFactor;
result[2] = decimal(1.0) - result[1] - result[0];
return result;
// return result;
return vec3::max(vec3::min(result, vec3(1.0, 1.0, 1.0)),
vec3(0.0, 0.0, 0.0));
}
};

View File

@@ -18,6 +18,7 @@ class Renderer {
Rendertarget *target;
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) +
@@ -56,6 +57,7 @@ class Renderer {
polygon testP;
for (int f = 0; f < model->faces.size(); f += 3) {
testP.small = false;
for (int p = 0; p < 3; p++) {
testP.points[p] = verts[std::get<0>(model->faces[f + p])];
testP.colors[p] =
@@ -63,6 +65,8 @@ class Renderer {
testP.normals[p] =
model->normals[std::get<1>(model->faces[f + p])];
}
if ((testP.avgNormal() * vec3(0.0, 0.0, 1.0)) > decimal(0.))
continue;
testP.calcDelta();
@@ -81,20 +85,19 @@ class Renderer {
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);
factors = factors.normalize();
decimal depth = testP.calcDepth(factors);
if (depth < target->getDepth(x, y)) {
// std::cout << factors << std::endl;
vec3 normal = testP.calcNormal(factors);
vec3 color = testP.calcColor(factors);
decimal lightFac =
std::max(
normal *
(-vec3(1.0, -1.0, 1.0).normalize()),
decimal(0.0)) +
std::max(normal * (-sunDir), decimal(0.0)) +
decimal(0.5);
target->setDepth(x, y, depth);
// decimal reflection = target->setDepth(x, y,
// depth);
target->set(x, y,
(color * decimal(120.0)) * lightFac);

View File

@@ -10,22 +10,20 @@ class Rendertarget {
const int height;
Rendertarget(int width, int height) : width(width), height(height) {
pixels = new decimal[width * height * 3];
pixels = new uint8_t[width * height * 3];
depth = new decimal[width * height];
for (int i = 0; i < width * height * 3; i++) {
pixels[i] = 0;
}
}
void set(int x, int y, vec3 val) {
int start = (width * y + x) * 3;
for (int i = 0; i < 3; i++) {
pixels[start + i] = val[i];
}
}
void get(int x, int y, vec3 *val) {
int start = (width * y + x) * 3;
for (int i = 0; i < 3; i++) {
(*val)[i] = pixels[start + i];
pixels[start + i] = (uint8_t)(val[i].i >> SHIFT_AMOUNT);
}
}
decimal getDepth(int x, int y) { return depth[width * y + x]; }
void setDepth(int x, int y, decimal val) { depth[width * y + x] = val; }
@@ -35,7 +33,7 @@ class Rendertarget {
}
}
decimal *pixels;
uint8_t *pixels;
decimal *depth;
};