Compare commits

...

3 Commits

Author SHA1 Message Date
7c55eb3be4 feat: time tracking added 2025-11-19 08:53:36 +01:00
7944026fd3 feat: improved performance and fixed bugs 2025-11-19 08:53:06 +01:00
1de652e88e feat: added decimal operator 2025-11-19 08:50:54 +01:00
4 changed files with 81 additions and 38 deletions

View File

@@ -44,6 +44,8 @@ struct decimal {
return {MUL_F(d1.i, d2.i)}; return {MUL_F(d1.i, d2.i)};
} }
decimal &operator*=(const decimal &d) { return (*this) = {MUL_F(i, d.i)}; }
friend decimal operator/(const decimal &d1, const decimal &d2) { friend decimal operator/(const decimal &d1, const decimal &d2) {
return {DIV_F(d1.i, d2.i)}; return {DIV_F(d1.i, d2.i)};
} }

View File

@@ -7,6 +7,7 @@
#include <QImage> #include <QImage>
#include <QLabel> #include <QLabel>
#include <QPixmap> #include <QPixmap>
#include <chrono>
#include <functional> #include <functional>
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
@@ -77,7 +78,17 @@ int main(int argc, char *argv[]) {
polygon poly = polygon poly =
polygon(vec3(0.9, 0.9, 1.0), vec3(0.5, 0.1, 1.0), vec3(0.1, 0.9, 1.0)); polygon(vec3(0.9, 0.9, 1.0), vec3(0.5, 0.1, 1.0), vec3(0.1, 0.9, 1.0));
std::chrono::steady_clock::time_point begin =
std::chrono::steady_clock::now();
renderer.render(&testModel); renderer.render(&testModel);
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;
QApplication a(argc, argv); QApplication a(argc, argv);
uint8_t *pixel = new uint8_t[64 * 64 * 3]; uint8_t *pixel = new uint8_t[64 * 64 * 3];

View File

@@ -5,37 +5,50 @@
struct polygon { struct polygon {
vec3 points[3]; vec3 points[3];
vec3 delta[3]; decimal delta[9];
bool skip = false; bool skip = false;
decimal bounding[4]; // min x, max x, min y, max y
polygon(const vec3 &v1, const vec3 &v2, const vec3 &v3) polygon(const vec3 &v1, const vec3 &v2, const vec3 &v3)
: points{v1, v2, v3} {} : points{v1, v2, v3}, delta{} {}
polygon() : points{} {} polygon() : points{}, delta{} {}
void calcDelta() { void calcDelta() {
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
int n = (i + 1) % 3; int n = (i + 1) % 3;
vec3 d = points[n] - points[i]; delta[i * 3] = points[i].y() - points[n].y();
delta[i] = vec3(d.y(), -d.x(), d.z()); delta[i * 3 + 1] = points[n].x() - points[i].x();
if (delta[i].isSmall()) { delta[i * 3 + 2] =
skip = true; points[i].x() * points[n].y() - points[i].y() * points[n].x();
return; }
} bounding[0] = points[0].x();
bounding[1] = points[0].x();
bounding[2] = points[0].y();
bounding[3] = points[0].y();
for (int i = 1; i < 3; i++) {
if (bounding[0] > points[i].x())
bounding[0] = points[i].x();
if (bounding[1] < points[i].x())
bounding[1] = points[i].x();
if (bounding[2] > points[i].y())
bounding[2] = points[i].y();
if (bounding[3] < points[i].y())
bounding[3] = points[i].y();
} }
} }
bool contains(const vec3 &p) { const bool contains(const vec3 &p) {
if (skip) // if (skip)
return false; // return false;
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
int n = (i + 1) % 3; vec3 d = p;
if ((((d.x().i >> SHIFT_AMOUNT) * delta[i * 3].i +
vec3 s = p - points[n]; (d.y().i >> SHIFT_AMOUNT) * delta[i * 3 + 1].i +
if (s.isSmall()) delta[i * 3 + 2].i) >>
return false; SHIFT_AMOUNT) > 0)
if (s * delta[i] <= decimal(0.0))
return false; return false;
} }
return true; return true;

View File

@@ -6,8 +6,11 @@
#include "model.hpp" #include "model.hpp"
#include "polygon.hpp" #include "polygon.hpp"
#include "rendertarget.hpp" #include "rendertarget.hpp"
#include <cstring>
#include <memory.h> #include <memory.h>
#define SCREEN_SPACE_SIZE 8.0
class Renderer { class Renderer {
public: public:
@@ -16,47 +19,61 @@ class Renderer {
bool clearTarget = true; bool clearTarget = true;
Renderer() : clearTarget(false) {} Renderer() : clearTarget(false) {}
void toScreenSpace(polygon *p) { void toScreenSpace(vec3 *p) {
for (int i = 0; i < 3; i++) { p->x() = p->x() / p->z() * decimal(SCREEN_SPACE_SIZE) +
p->points[i].x() = decimal(SCREEN_SPACE_SIZE);
p->points[i].x() / p->points[i].z() * decimal(8.0) + p->y() = p->y() / p->z() * decimal(SCREEN_SPACE_SIZE) +
decimal(8.0); decimal(SCREEN_SPACE_SIZE);
p->points[i].y() = p->z() = decimal(1.0);
p->points[i].y() / p->points[i].z() * decimal(8.0) +
decimal(8.0);
p->points[i].z() = decimal(1.0);
}
} }
void render(const model *model) { void render(const model *model) {
decimal widthScale = decimal(16.0) / decimal((float)target->width); decimal widthScale =
decimal heightScale = decimal(16.0) / decimal((float)target->height); decimal(SCREEN_SPACE_SIZE * 2) / decimal((float)target->width);
// std::cout << widthScale << std::endl; decimal heightScale =
decimal(SCREEN_SPACE_SIZE * 2) / decimal((float)target->height);
decimal invWidthScale =
decimal((float)(target->width / SCREEN_SPACE_SIZE / 2));
decimal invHeightScale =
decimal((float)(target->height / SCREEN_SPACE_SIZE / 2));
if (clearTarget) { if (clearTarget) {
memset((wchar_t *)target->pixels, 0, memset((wchar_t *)target->pixels, 0,
target->height * target->width * sizeof(target[0])); target->height * target->width * sizeof(target[0]));
} }
vec3 verts[model->verts.size()] = {};
std::copy(model->verts.begin(), model->verts.end(), verts);
for (int i = 0; i < model->verts.size(); i++) {
toScreenSpace(verts + i);
}
polygon testP; polygon testP;
for (int f = 0; f < model->faces.size(); f += 3) { for (int f = 0; f < model->faces.size(); f += 3) {
for (int p = 0; p < 3; p++) { for (int p = 0; p < 3; p++) {
testP.points[p] = model->verts[model->faces[f + p]]; testP.points[p] = verts[model->faces[f + p]];
} }
toScreenSpace(&testP);
testP.calcDelta(); testP.calcDelta();
vec3 pos = vec3();
for (int x = 0; x < target->height; x++) { int startX = (testP.bounding[0] * invWidthScale).i >> SHIFT_AMOUNT;
for (int y = 0; y < target->height; y++) { int endX = (testP.bounding[1] * invWidthScale).i >> SHIFT_AMOUNT;
int startY = (testP.bounding[2] * invHeightScale).i >> SHIFT_AMOUNT;
int endY = (testP.bounding[3] * invHeightScale).i >> SHIFT_AMOUNT;
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.contains(pos)) {
target->set(x, y, vec3(0.0, 255.0, 0.0)); target->set(x, y, vec3(0.0, 255.0, 0.0));
} }
pos.y() += heightScale; pos.y() += heightScale;
} }
pos.y() = decimal(0); pos.y() = decimal(testP.bounding[2]);
pos.x() += widthScale; pos.x() += widthScale;
} }
} }