Compare commits
18 Commits
a921fb27b1
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 7ab3825598 | |||
| efbde487cc | |||
| 0c94bf1df2 | |||
| 9a8b1fb5c1 | |||
| 7faf830f2e | |||
| 939d1e6b1e | |||
| eec4a0d44b | |||
| 7c55eb3be4 | |||
| 7944026fd3 | |||
| 1de652e88e | |||
| e370c1d0c3 | |||
| f8ffb9c0b3 | |||
| e9e4eed79b | |||
| adafae2d84 | |||
| 949578f0db | |||
| e6a49c4355 | |||
| 036b905cd6 | |||
| a27a11603b |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
build
|
||||
@@ -3,7 +3,12 @@ cmake_minimum_required(VERSION 3.15)
|
||||
project(SDFVisual VERSION 0.1
|
||||
DESCRIPTION "CPU SDF Renderer"
|
||||
LANGUAGES CXX)
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS
|
||||
Widgets Gui
|
||||
)
|
||||
|
||||
set (CMAKE_CXX_STANDARD 20)
|
||||
add_executable(one main.cpp)
|
||||
|
||||
target_link_libraries(one m)
|
||||
target_link_libraries(one m Qt6::Widgets Qt6::Gui)
|
||||
|
||||
211
fastmath.hpp
211
fastmath.hpp
@@ -5,6 +5,7 @@
|
||||
#include <iostream>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <vector>
|
||||
#define SHIFT_AMOUNT 16
|
||||
#define HALF_SHIFT (SHIFT_AMOUNT / 2)
|
||||
|
||||
@@ -18,8 +19,9 @@
|
||||
|
||||
struct decimal {
|
||||
|
||||
const int32_t i;
|
||||
int32_t i;
|
||||
|
||||
decimal() : i(0) {}
|
||||
decimal(float i) : i(TO_INT(i)) {}
|
||||
decimal(double i) : i(TO_INT(i)) {}
|
||||
decimal(int32_t i) : i(i) {}
|
||||
@@ -42,6 +44,8 @@ struct decimal {
|
||||
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) {
|
||||
return {DIV_F(d1.i, d2.i)};
|
||||
}
|
||||
@@ -53,11 +57,19 @@ struct decimal {
|
||||
friend bool operator>(const decimal &d1, const decimal &d2) {
|
||||
return d1.i > d2.i;
|
||||
}
|
||||
friend bool operator<=(const decimal &d1, const decimal &d2) {
|
||||
return d1.i <= d2.i;
|
||||
}
|
||||
friend bool operator>=(const decimal &d1, const decimal &d2) {
|
||||
return d1.i >= d2.i;
|
||||
}
|
||||
|
||||
friend bool operator==(const decimal &d1, const decimal &d2) {
|
||||
return d1.i == d2.i;
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -69,72 +81,167 @@ struct decimal {
|
||||
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))); }
|
||||
};
|
||||
|
||||
struct vec3 {
|
||||
const decimal x;
|
||||
const decimal y;
|
||||
const decimal z;
|
||||
template <int n, class Dev> struct vec {
|
||||
|
||||
vec3(float x, float y, float z)
|
||||
: x(decimal(x)), y(decimal(y)), z(decimal(z)) {}
|
||||
|
||||
vec3(double x, double y, double z)
|
||||
: x(decimal(x)), y(decimal(y)), z(decimal(z)) {}
|
||||
|
||||
vec3(int32_t x, int32_t y, int32_t z)
|
||||
: x(decimal(x)), y(decimal(y)), z(decimal(z)) {}
|
||||
vec3(decimal x, decimal y, decimal z) : x(x), y(y), z(z) {}
|
||||
|
||||
friend vec3 operator+(const vec3 &v1, const vec3 &v2) {
|
||||
return vec3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
|
||||
}
|
||||
|
||||
friend vec3 operator-(const vec3 &v1, const vec3 &v2) {
|
||||
return vec3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
|
||||
}
|
||||
|
||||
vec3 operator-() { return vec3(-x, -y, -z); }
|
||||
|
||||
friend vec3 operator*(const vec3 &v, const decimal &n) {
|
||||
int32_t f = n.i >> HALF_SHIFT;
|
||||
return vec3({(v.x.i >> HALF_SHIFT) * f}, {(v.y.i >> HALF_SHIFT) * f},
|
||||
{(v.z.i >> HALF_SHIFT) * f});
|
||||
}
|
||||
friend vec3 operator*(const decimal &n, const vec3 &v) { return v * n; }
|
||||
|
||||
decimal operator*(const vec3 &v) { return v.x * x + v.y * y + v.z * z; }
|
||||
|
||||
friend bool operator==(const vec3 &v1, const vec3 &v2) {
|
||||
return (v1.x == v2.x) & (v1.y == v2.y) & (v1.z == v2.z);
|
||||
}
|
||||
|
||||
vec3 &operator=(vec3 const &in) {
|
||||
if (this != &in) {
|
||||
std::destroy_at(this);
|
||||
std::construct_at(this, in);
|
||||
vec(decimal newV[n]) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
v[i] = newV[i];
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, const vec3 &v) {
|
||||
return (os << "(" << v.x << ", " << v.y << ", " << v.z << ")"
|
||||
<< std::endl);
|
||||
vec(std::vector<decimal> newV) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
v[i] = newV[i];
|
||||
}
|
||||
}
|
||||
|
||||
vec3 cross(const vec3 &v) {
|
||||
return vec3((y * v.z) - (z * v.y), (z * v.x) - (x * v.z),
|
||||
(x * v.y) - (y * v.x));
|
||||
vec() : v{} {}
|
||||
|
||||
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 = {};
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
newV.v[i] = v1.v[i] - v2.v[i];
|
||||
}
|
||||
return static_cast<Dev>(newV);
|
||||
}
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, const vec<n, Dev> &v) {
|
||||
os << "(" << v.v[0];
|
||||
for (int i = 1; i < n; i++) {
|
||||
os << ", " << v.v[i];
|
||||
}
|
||||
return (os << ")" << std::endl);
|
||||
}
|
||||
|
||||
Dev operator-() {
|
||||
Dev newV = {};
|
||||
for (int i = 0; i < n; i++) {
|
||||
newV.v[i] = -v[i];
|
||||
}
|
||||
return newV;
|
||||
}
|
||||
|
||||
friend Dev operator*(const vec<n, Dev> &v, const decimal &d) {
|
||||
int32_t f = d.i >> HALF_SHIFT;
|
||||
|
||||
Dev newV = {};
|
||||
for (int i = 0; i < n; i++) {
|
||||
newV.v[i] = (v.v[i].i >> HALF_SHIFT) * f;
|
||||
}
|
||||
return newV;
|
||||
}
|
||||
|
||||
friend Dev operator*(const decimal &d, const vec<n, Dev> &v) {
|
||||
return v * d;
|
||||
}
|
||||
|
||||
decimal operator*(const vec<n, Dev> &vec) {
|
||||
decimal res;
|
||||
for (int i = 0; i < n; i++) {
|
||||
res += vec.v[i] * v[i];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
friend bool operator==(const vec<n, Dev> &v1, const vec<n, Dev> &v2) {
|
||||
bool res = true;
|
||||
for (int i = 0; i < n; i++) {
|
||||
res &= v1.v[i] == v2.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(); }
|
||||
|
||||
vec3 normalize() {
|
||||
Dev normalize() {
|
||||
decimal f = decimal(1.0) / this->len();
|
||||
return (*this * f);
|
||||
}
|
||||
};
|
||||
|
||||
protected:
|
||||
decimal v[n];
|
||||
};
|
||||
struct vec2 : public vec<2, vec2> {
|
||||
|
||||
vec2() : vec<2, vec2>() {}
|
||||
|
||||
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(int32_t x, int32_t y) : vec<2, vec2>({decimal(x), decimal(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>() {}
|
||||
|
||||
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)}) {}
|
||||
|
||||
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}) {}
|
||||
|
||||
decimal &x() { return v[0]; }
|
||||
decimal &y() { return v[1]; }
|
||||
decimal &z() { return v[2]; }
|
||||
|
||||
vec3 cross(vec3 &v) {
|
||||
return vec3((y() * v.z()) - (z() * v.y()),
|
||||
(z() * v.x()) - (x() * v.z()),
|
||||
(x() * v.y()) - (y() * v.x()));
|
||||
}
|
||||
};
|
||||
struct vec4 : public vec<4, vec4> {
|
||||
|
||||
vec4() : vec<4, vec4>() {}
|
||||
|
||||
vec4(float x, float y, float z, float 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)}) {}
|
||||
|
||||
vec4(int32_t x, int32_t y, int32_t z, int32_t 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}) {}
|
||||
|
||||
decimal &x() { return v[0]; }
|
||||
decimal &y() { return v[1]; }
|
||||
decimal &z() { return v[2]; }
|
||||
decimal &w() { return v[3]; }
|
||||
};
|
||||
#endif
|
||||
|
||||
75
main.cpp
75
main.cpp
@@ -1,12 +1,20 @@
|
||||
#include "fastmath.hpp"
|
||||
#include "polygon.hpp"
|
||||
#include "renderer.hpp"
|
||||
#include "rendertarget.hpp"
|
||||
#include "testModel.hpp"
|
||||
#include <QApplication>
|
||||
#include <QImage>
|
||||
#include <QLabel>
|
||||
#include <QPixmap>
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define HIGHT 64
|
||||
#define WIDTH 64
|
||||
|
||||
@@ -56,24 +64,63 @@ void drawImage(unsigned short *img) {
|
||||
img[(y.i >> SHIFT_AMOUNT) * RWIDTH + (x.i >> SHIFT_AMOUNT)] =
|
||||
(unsigned short)23; // (((-normal[1]+(1 <<
|
||||
// SHIFT_AMOUNT))*14)>>SHIFT_AMOUNT);
|
||||
} else {
|
||||
img[(y.i >> SHIFT_AMOUNT) * RWIDTH + (x.i >> SHIFT_AMOUNT)] =
|
||||
(unsigned short)0;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("done writing %d \n", *(img + sizeof(unsigned short) * 32 * 32));
|
||||
}
|
||||
|
||||
int main() {
|
||||
unsigned short *img =
|
||||
(unsigned short *)malloc(128 * 128 * sizeof(unsigned short));
|
||||
while (true) {
|
||||
drawImage(img);
|
||||
printf("%s", drawToString(img));
|
||||
return 0;
|
||||
// sleep(0.001);
|
||||
int main(int argc, char *argv[]) {
|
||||
Rendertarget target(128, 128);
|
||||
Renderer renderer;
|
||||
renderer.target = ⌖
|
||||
|
||||
polygon poly =
|
||||
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);
|
||||
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);
|
||||
|
||||
uint8_t *pixel = new uint8_t[64 * 64 * 3];
|
||||
/*for (int i = 0; i < 64 * 64 * 3; i++) {
|
||||
pixel[i] = target.pixels[i].i >> SHIFT_AMOUNT;
|
||||
}*/
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
// printf("%s", test);
|
||||
return 0;
|
||||
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()));
|
||||
widget.show();
|
||||
return a.exec();
|
||||
}
|
||||
|
||||
16
model.hpp
Normal file
16
model.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
#ifndef MODEL_H
|
||||
#define MODEL_H
|
||||
|
||||
#include "fastmath.hpp"
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
struct model {
|
||||
std::vector<vec3> verts;
|
||||
// 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
|
||||
50
parseObj.py
Normal file
50
parseObj.py
Normal file
@@ -0,0 +1,50 @@
|
||||
# Open the file in read mode
|
||||
file = open("test.obj", "r")
|
||||
|
||||
# Read the entire content of the file
|
||||
content = file.read().split("\n")
|
||||
|
||||
file.close()
|
||||
|
||||
startVerts = 0
|
||||
verts = []
|
||||
for index,line in enumerate(content):
|
||||
if line[0] == 'o':
|
||||
startVerts = index + 1
|
||||
content = content[startVerts:]
|
||||
break
|
||||
|
||||
for index,line in enumerate(content):
|
||||
if "vn" in line:
|
||||
endVerts = index
|
||||
verts = content[:endVerts]
|
||||
content = content[endVerts:]
|
||||
break
|
||||
|
||||
|
||||
normals = []
|
||||
for index,line in enumerate(content):
|
||||
if "vt" in line:
|
||||
normals = content[:index]
|
||||
content = content[index:]
|
||||
break
|
||||
|
||||
startFaces = 0;
|
||||
faces = []
|
||||
for index,line in enumerate(content):
|
||||
if line[0] == 'f':
|
||||
startFaces = index
|
||||
faces = content[startFaces:-1]
|
||||
break
|
||||
|
||||
colors = ["vec3(" +",".join(vert.split(" ")[4:7]) + ")" for vert in verts]
|
||||
verts = ["vec3(" +",".join(vert.split(" ")[1:4]) + ")" for vert in verts]
|
||||
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) + "},{" + ",".join(normals) + "},{"+ ",".join(colors)+"});"
|
||||
|
||||
with open("testModel.hpp", "w") as f:
|
||||
f.write(out)
|
||||
print(out)
|
||||
# Close the file
|
||||
100
polygon.hpp
100
polygon.hpp
@@ -2,28 +2,112 @@
|
||||
#define POLYGON_H
|
||||
|
||||
#include "fastmath.hpp"
|
||||
|
||||
struct polygon {
|
||||
|
||||
const vec3 points[3];
|
||||
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];
|
||||
vec3 colors[3];
|
||||
|
||||
polygon(const vec3 &v1, const vec3 &v2, const vec3 &v3)
|
||||
: points{v1, v2, v3} {}
|
||||
: points{v1, v2, v3}, delta{} {}
|
||||
polygon() : points{}, delta{} {}
|
||||
|
||||
void calcDelta() {
|
||||
|
||||
bool contains(const vec3 &p) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
int n = (i + 1) % 3;
|
||||
|
||||
vec3 d = points[n] - points[i];
|
||||
d = vec3(d.y, -d.x, d.z);
|
||||
delta[i * 3] = points[i].y() - points[n].y();
|
||||
delta[i * 3 + 1] = points[n].x() - points[i].x();
|
||||
delta[i * 3 + 2] =
|
||||
points[i].x() * points[n].y() - points[i].y() * points[n].x();
|
||||
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();
|
||||
}
|
||||
|
||||
vec3 s = p - points[n];
|
||||
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 (s * d < decimal(0.0))
|
||||
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;*/
|
||||
}
|
||||
|
||||
const bool contains(const vec3 &p) {
|
||||
// if (skip)
|
||||
// return false;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (small)
|
||||
return true;
|
||||
vec3 d = p;
|
||||
if ((d.x() * delta[i * 3] + d.y() * delta[i * 3 + 1] +
|
||||
delta[i * 3 + 2]) > decimal(0.2))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
friend std::ostream &operator<<(std::ostream &os, const polygon &p) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
os << p.points[i];
|
||||
}
|
||||
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
|
||||
|
||||
119
renderer.hpp
Normal file
119
renderer.hpp
Normal file
@@ -0,0 +1,119 @@
|
||||
|
||||
#ifndef RENDERER_H
|
||||
#define RENDERER_H
|
||||
|
||||
#include "fastmath.hpp"
|
||||
#include "model.hpp"
|
||||
#include "polygon.hpp"
|
||||
#include "rendertarget.hpp"
|
||||
#include <bits/stdc++.h>
|
||||
#include <cstring>
|
||||
#include <memory.h>
|
||||
|
||||
#define SCREEN_SPACE_SIZE 8.0
|
||||
|
||||
class Renderer {
|
||||
|
||||
public:
|
||||
Rendertarget *target;
|
||||
|
||||
bool clearTarget = true;
|
||||
|
||||
void toScreenSpace(vec3 *p) {
|
||||
p->x() = p->x() / p->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) +
|
||||
decimal(SCREEN_SPACE_SIZE);
|
||||
p->z() = p->z();
|
||||
}
|
||||
|
||||
void render(const model *model) {
|
||||
decimal widthScale =
|
||||
decimal(SCREEN_SPACE_SIZE * 2) / decimal((float)target->width);
|
||||
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) {
|
||||
// memset((wchar_t *)target->pixels, 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;
|
||||
|
||||
for (int f = 0; f < model->faces.size(); f += 3) {
|
||||
for (int p = 0; p < 3; 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])];
|
||||
}
|
||||
|
||||
testP.calcDelta();
|
||||
|
||||
int startX = std::max(
|
||||
(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)) {
|
||||
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() = decimal(testP.bounding[2]);
|
||||
pos.x() += widthScale;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
42
rendertarget.hpp
Normal file
42
rendertarget.hpp
Normal file
@@ -0,0 +1,42 @@
|
||||
|
||||
#ifndef RENDERTARGET_H
|
||||
#define RENDERTARGET_H
|
||||
|
||||
#include "fastmath.hpp"
|
||||
class Rendertarget {
|
||||
|
||||
public:
|
||||
const int width;
|
||||
const int height;
|
||||
|
||||
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) {
|
||||
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];
|
||||
}
|
||||
}
|
||||
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
|
||||
2
testModel.hpp
Normal file
2
testModel.hpp
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user