Compare commits

..

10 Commits

8 changed files with 201 additions and 48 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)};
} }
@@ -79,6 +81,7 @@ struct decimal {
decimal sqrt() { return {((int32_t)sqrtf(i)) << HALF_SHIFT}; } decimal sqrt() { return {((int32_t)sqrtf(i)) << HALF_SHIFT}; }
float to_float() { return TO_FLOAT(i); } float to_float() { return TO_FLOAT(i); }
bool isSmall() { return (abs(i) < (1 << (HALF_SHIFT - 1))); }
}; };
template <int n, class Dev> struct vec { template <int n, class Dev> struct vec {
@@ -162,7 +165,7 @@ template <int n, class Dev> struct vec {
} }
bool isSmall() { bool isSmall() {
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
if (abs(v[i].i) > (1 << (HALF_SHIFT - 1))) if (!v[i].isSmall())
return false; return false;
} }
return true; return true;

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

@@ -3,10 +3,14 @@
#define MODEL_H #define MODEL_H
#include "fastmath.hpp" #include "fastmath.hpp"
#include <tuple>
#include <vector> #include <vector>
struct model { struct model {
std::vector<vec3> verts; std::vector<vec3> verts;
std::vector<uint16_t> faces; // At 0 vertecie index, at 1 normal index
std::vector<std::tuple<uint16_t, uint16_t>> faces;
std::vector<vec3> normals;
std::vector<vec3> colors;
}; };
#endif #endif

View File

@@ -21,6 +21,14 @@ for index,line in enumerate(content):
content = content[endVerts:] content = content[endVerts:]
break break
normals = []
for index,line in enumerate(content):
if "vt" in line:
normals = content[:index]
content = content[index:]
break
startFaces = 0; startFaces = 0;
faces = [] faces = []
for index,line in enumerate(content): for index,line in enumerate(content):
@@ -29,10 +37,12 @@ for index,line in enumerate(content):
faces = content[startFaces:-1] faces = content[startFaces:-1]
break break
colors = ["vec3(" +",".join(vert.split(" ")[4:7]) + ")" for vert in verts]
verts = ["vec3(" +",".join(vert.split(" ")[1:4]) + ")" for vert in verts] verts = ["vec3(" +",".join(vert.split(" ")[1:4]) + ")" for vert in verts]
faces = [ ",".join([str(int((d.split("/")[0])) - 1) for d in face.split(" ")[1:4]]) for face in faces] faces = [ ",".join(["{" + str(int((d.split("/")[0])) - 1) + "," + str(int((d.split("/")[2])) - 1) + "}" for d in face.split(" ")[1:4]]) for face in faces]
normals = ["vec3(" + ",".join(normal.split(" ")[1:4]) + ")" for normal in normals]
out = "#include \"model.hpp\" \n const model testModel({" + ",".join(verts) +"},{" + ",".join(faces) + "});" out = "#include \"model.hpp\" \n const model testModel({" + ",".join(verts) +"},{" + ",".join(faces) + "},{" + ",".join(normals) + "},{"+ ",".join(colors)+"});"
with open("testModel.hpp", "w") as f: with open("testModel.hpp", "w") as f:
f.write(out) f.write(out)

View File

@@ -5,37 +5,68 @@
struct polygon { struct polygon {
vec3 points[3]; vec3 points[3];
vec3 delta[3]; decimal delta[9];
bool skip = false; bool small = false;
decimal baryFactor;
decimal bounding[4]; // min x, max x, min y, max y
vec3 normals[3];
vec3 colors[3];
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; if (delta[i * 3].i == 0 && delta[i * 3 + 1].i == 0)
} small = true;
} }
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();
}
baryFactor =
(points[1].x() - points[0].x()) * (points[2].y() - points[1].y()) -
(points[1].y() - points[0].y()) * (points[2].x() - points[1].x());
if (baryFactor.isSmall()) {
small = true;
} else
baryFactor = decimal(1.0) / baryFactor;
// std::cout << baryFactor << std::endl;
/*if ((bounding[1].i - bounding[0].i < 1 << HALF_SHIFT) &&
(bounding[3].i - bounding[2].i < 1 << HALF_SHIFT))
small = true;*/
} }
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; if (small)
return true;
vec3 s = p - points[n]; vec3 d = p;
if (s.isSmall()) if ((d.x() * delta[i * 3] + d.y() * delta[i * 3 + 1] +
return false; delta[i * 3 + 2]) > decimal(0.2))
if (s * delta[i] <= decimal(0.0))
return false; return false;
} }
return true; return true;
@@ -46,6 +77,37 @@ struct polygon {
} }
return os; return os;
} }
vec3 calcNormal(vec3 barycentrics) {
vec3 result = normals[0] * barycentrics[0] +
normals[1] * barycentrics[1] +
normals[2] * barycentrics[2];
return result.normalize();
}
vec3 calcColor(vec3 barycentrics) {
return colors[0] * barycentrics[0] + colors[1] * barycentrics[1] +
colors[2] * barycentrics[2];
}
decimal calcDepth(vec3 barycentrics) {
return points[0].z() * barycentrics[0] +
points[1].z() * barycentrics[1] +
points[2].z() * barycentrics[2];
}
vec3 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];
return result;
}
}; };
#endif #endif

View File

@@ -6,57 +6,110 @@
#include "model.hpp" #include "model.hpp"
#include "polygon.hpp" #include "polygon.hpp"
#include "rendertarget.hpp" #include "rendertarget.hpp"
#include <bits/stdc++.h>
#include <cstring>
#include <memory.h> #include <memory.h>
#define SCREEN_SPACE_SIZE 8.0
class Renderer { class Renderer {
public: public:
Rendertarget *target; Rendertarget *target;
bool clearTarget = true; bool clearTarget = true;
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(2.0) * 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(2.0) * decimal(SCREEN_SPACE_SIZE) +
decimal(8.0); decimal(SCREEN_SPACE_SIZE);
p->points[i].y() = p->z() = p->z();
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));
// TODO clear target with memset
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]));
target->clearDepth();
}
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[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])];
} }
toScreenSpace(&testP);
testP.calcDelta(); testP.calcDelta();
vec3 pos = vec3();
for (int x = 0; x < target->height; x++) { int startX = std::max(
for (int y = 0; y < target->height; y++) { (testP.bounding[0] * invWidthScale).i >> SHIFT_AMOUNT, 0);
int endX =
std::min((testP.bounding[1] * invWidthScale).i >> SHIFT_AMOUNT,
target->width - 1);
int startY = std::max(
(testP.bounding[2] * invHeightScale).i >> SHIFT_AMOUNT, 0);
int endY =
std::min((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.contains(pos)) {
target->set(x, y, vec3(0.0, 255.0, 0.0)); 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)) +
decimal(0.5);
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)) *
// decimal(120.0));
// target->set(x, y, factors * decimal(200.0));
// if (!factors.isSmall())
// target->set(x, y, vec3(0., 255.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;
} }
} }

View File

@@ -11,6 +11,7 @@ class Rendertarget {
Rendertarget(int width, int height) : width(width), height(height) { Rendertarget(int width, int height) : width(width), height(height) {
pixels = new decimal[width * height * 3]; pixels = new decimal[width * height * 3];
depth = new decimal[width * height];
} }
void set(int x, int y, vec3 val) { void set(int x, int y, vec3 val) {
@@ -25,8 +26,17 @@ class Rendertarget {
(*val)[i] = pixels[start + i]; (*val)[i] = pixels[start + i];
} }
} }
decimal getDepth(int x, int y) { return depth[width * y + x]; }
void setDepth(int x, int y, decimal val) { depth[width * y + x] = val; }
void clearDepth() {
for (int i = 0; i < width * height; i++) {
depth[i].i = std::numeric_limits<int32_t>::max();
}
}
decimal *pixels; decimal *pixels;
decimal *depth;
}; };
#endif #endif

File diff suppressed because one or more lines are too long