feat: barycentric interpolation for normals
This commit is contained in:
40
polygon.hpp
40
polygon.hpp
@@ -7,8 +7,10 @@ struct polygon {
|
||||
vec3 points[3];
|
||||
decimal delta[9];
|
||||
bool small = false;
|
||||
decimal baryFactor;
|
||||
|
||||
decimal bounding[4]; // min x, max x, min y, max y
|
||||
vec3 normals[3];
|
||||
|
||||
polygon(const vec3 &v1, const vec3 &v2, const vec3 &v3)
|
||||
: points{v1, v2, v3}, delta{} {}
|
||||
@@ -40,6 +42,16 @@ struct polygon {
|
||||
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;*/
|
||||
@@ -53,7 +65,7 @@ struct polygon {
|
||||
return true;
|
||||
vec3 d = p;
|
||||
if ((d.x() * delta[i * 3] + d.y() * delta[i * 3 + 1] +
|
||||
delta[i * 3 + 2]) > decimal(0))
|
||||
delta[i * 3 + 2]) > decimal(0.2))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -64,6 +76,32 @@ struct polygon {
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
vec3 calcNormal(vec3 barycentrics) {
|
||||
vec3 result = normals[0] * barycentrics[0] +
|
||||
normals[1] * barycentrics[1] +
|
||||
normals[2] * barycentrics[2];
|
||||
return result.normalize();
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
40
renderer.hpp
40
renderer.hpp
@@ -6,6 +6,7 @@
|
||||
#include "model.hpp"
|
||||
#include "polygon.hpp"
|
||||
#include "rendertarget.hpp"
|
||||
#include <bits/stdc++.h>
|
||||
#include <cstring>
|
||||
#include <memory.h>
|
||||
|
||||
@@ -17,14 +18,13 @@ class Renderer {
|
||||
Rendertarget *target;
|
||||
|
||||
bool clearTarget = true;
|
||||
Renderer() : clearTarget(false) {}
|
||||
|
||||
void toScreenSpace(vec3 *p) {
|
||||
p->x() = p->x() / p->z() * decimal(SCREEN_SPACE_SIZE) +
|
||||
decimal(SCREEN_SPACE_SIZE);
|
||||
p->y() = p->y() / p->z() * decimal(SCREEN_SPACE_SIZE) +
|
||||
decimal(SCREEN_SPACE_SIZE);
|
||||
p->z() = decimal(1.0);
|
||||
p->z() = p->z();
|
||||
}
|
||||
|
||||
void render(const model *model) {
|
||||
@@ -38,9 +38,11 @@ class Renderer {
|
||||
decimal invHeightScale =
|
||||
decimal((float)(target->height / SCREEN_SPACE_SIZE / 2));
|
||||
|
||||
// TODO clear target with memset
|
||||
if (clearTarget) {
|
||||
memset((wchar_t *)target->pixels, 0,
|
||||
target->height * target->width * sizeof(target[0]));
|
||||
// memset((wchar_t *)target->pixels, 0,
|
||||
// target->height * target->width * sizeof(target[0]));
|
||||
target->clearDepth();
|
||||
}
|
||||
|
||||
vec3 verts[model->verts.size()] = {};
|
||||
@@ -55,7 +57,9 @@ class Renderer {
|
||||
|
||||
for (int f = 0; f < model->faces.size(); f += 3) {
|
||||
for (int p = 0; p < 3; p++) {
|
||||
testP.points[p] = verts[model->faces[f + p]];
|
||||
testP.points[p] = verts[std::get<0>(model->faces[f + p])];
|
||||
testP.normals[p] =
|
||||
model->normals[std::get<1>(model->faces[f + p])];
|
||||
}
|
||||
|
||||
testP.calcDelta();
|
||||
@@ -69,7 +73,31 @@ class Renderer {
|
||||
for (int x = startX; x < endX; x++) {
|
||||
for (int y = startY; y < endY; y++) {
|
||||
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 normals = testP.calcNormal(factors);
|
||||
decimal lightFac =
|
||||
std::max(
|
||||
normals *
|
||||
(-vec3(1.0, -1.0, 1.0).normalize()),
|
||||
decimal(0.0)) +
|
||||
decimal(0.5);
|
||||
target->setDepth(x, y, depth);
|
||||
target->set(x, y,
|
||||
(lightFac * vec3(1., 1., 1.)) *
|
||||
decimal(120.0));
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ class Rendertarget {
|
||||
|
||||
Rendertarget(int width, int height) : width(width), height(height) {
|
||||
pixels = new decimal[width * height * 3];
|
||||
depth = new decimal[width * height];
|
||||
}
|
||||
|
||||
void set(int x, int y, vec3 val) {
|
||||
@@ -25,8 +26,17 @@ class Rendertarget {
|
||||
(*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 *depth;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user