Compare commits
2 Commits
e9e4eed79b
...
e370c1d0c3
| Author | SHA1 | Date | |
|---|---|---|---|
| e370c1d0c3 | |||
| f8ffb9c0b3 |
14
fastmath.hpp
14
fastmath.hpp
@@ -55,6 +55,12 @@ 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;
|
||||
@@ -62,7 +68,6 @@ struct decimal {
|
||||
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);
|
||||
@@ -155,6 +160,13 @@ template <int n, class Dev> struct vec {
|
||||
}
|
||||
return res;
|
||||
}
|
||||
bool isSmall() {
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (abs(v[i].i) > (1 << (HALF_SHIFT - 1)))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
decimal &operator[](const int &i) { return v[i]; }
|
||||
|
||||
decimal len_sq() { return *this * *this; }
|
||||
|
||||
10
main.cpp
10
main.cpp
@@ -2,6 +2,7 @@
|
||||
#include "polygon.hpp"
|
||||
#include "renderer.hpp"
|
||||
#include "rendertarget.hpp"
|
||||
#include "testModel.hpp"
|
||||
#include <QApplication>
|
||||
#include <QImage>
|
||||
#include <QLabel>
|
||||
@@ -74,9 +75,9 @@ int main(int argc, char *argv[]) {
|
||||
renderer.target = ⌖
|
||||
|
||||
polygon poly =
|
||||
polygon(vec3(0.9, 0.9, 0.0), vec3(0.5, 0.1, 0.0), vec3(0.1, 0.9, 0.0));
|
||||
polygon(vec3(0.9, 0.9, 1.0), vec3(0.5, 0.1, 1.0), vec3(0.1, 0.9, 1.0));
|
||||
|
||||
renderer.render(&poly);
|
||||
renderer.render(&testModel);
|
||||
QApplication a(argc, argv);
|
||||
|
||||
uint8_t *pixel = new uint8_t[64 * 64 * 3];
|
||||
@@ -98,9 +99,8 @@ int main(int argc, char *argv[]) {
|
||||
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 * y + x) * 3 + c] = result[c] >> 2; /*
|
||||
target.pixels[(target.width * y * 2 + x * 2) * 3 + c].i >>
|
||||
SHIFT_AMOUNT;*/
|
||||
pixel[(WIDTH * (WIDTH - y - 1) + WIDTH - x - 1) * 3 + c] =
|
||||
result[c] >> 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
12
model.hpp
Normal file
12
model.hpp
Normal file
@@ -0,0 +1,12 @@
|
||||
|
||||
#ifndef MODEL_H
|
||||
#define MODEL_H
|
||||
|
||||
#include "fastmath.hpp"
|
||||
#include <vector>
|
||||
struct model {
|
||||
std::vector<vec3> verts;
|
||||
std::vector<uint16_t> faces;
|
||||
};
|
||||
|
||||
#endif
|
||||
40
parseObj.py
Normal file
40
parseObj.py
Normal file
@@ -0,0 +1,40 @@
|
||||
# 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
|
||||
|
||||
startFaces = 0;
|
||||
faces = []
|
||||
for index,line in enumerate(content):
|
||||
if line[0] == 'f':
|
||||
startFaces = index
|
||||
faces = content[startFaces:-1]
|
||||
break
|
||||
|
||||
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]
|
||||
|
||||
out = "#include \"model.hpp\" \n const model testModel({" + ",".join(verts) +"},{" + ",".join(faces) + "});"
|
||||
|
||||
with open("testModel.hpp", "w") as f:
|
||||
f.write(out)
|
||||
print(out)
|
||||
# Close the file
|
||||
33
polygon.hpp
33
polygon.hpp
@@ -4,25 +4,48 @@
|
||||
#include "fastmath.hpp"
|
||||
struct polygon {
|
||||
|
||||
const vec3 points[3];
|
||||
vec3 points[3];
|
||||
vec3 delta[3];
|
||||
bool skip = false;
|
||||
|
||||
polygon(const vec3 &v1, const vec3 &v2, const vec3 &v3)
|
||||
: points{v1, v2, v3} {}
|
||||
polygon() : points{} {}
|
||||
|
||||
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] = vec3(d.y(), -d.x(), d.z());
|
||||
if (delta[i].isSmall()) {
|
||||
skip = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool contains(const vec3 &p) {
|
||||
if (skip)
|
||||
return false;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
int n = (i + 1) % 3;
|
||||
|
||||
vec3 s = p - points[n];
|
||||
|
||||
if (s * d < decimal(0.0))
|
||||
if (s.isSmall())
|
||||
return false;
|
||||
if (s * delta[i] <= decimal(0.0))
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
34
renderer.hpp
34
renderer.hpp
@@ -3,6 +3,7 @@
|
||||
#define RENDERER_H
|
||||
|
||||
#include "fastmath.hpp"
|
||||
#include "model.hpp"
|
||||
#include "polygon.hpp"
|
||||
#include "rendertarget.hpp"
|
||||
#include <memory.h>
|
||||
@@ -15,20 +16,42 @@ class Renderer {
|
||||
bool clearTarget = true;
|
||||
Renderer() : clearTarget(false) {}
|
||||
|
||||
void render(polygon *p) {
|
||||
decimal widthScale = decimal(1.0) / decimal((float)target->width);
|
||||
decimal heightScale = decimal(1.0) / decimal((float)target->height);
|
||||
void toScreenSpace(polygon *p) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
p->points[i].x() =
|
||||
p->points[i].x() / p->points[i].z() * decimal(8.0) +
|
||||
decimal(8.0);
|
||||
p->points[i].y() =
|
||||
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) {
|
||||
decimal widthScale = decimal(16.0) / decimal((float)target->width);
|
||||
decimal heightScale = decimal(16.0) / decimal((float)target->height);
|
||||
// std::cout << widthScale << std::endl;
|
||||
vec3 pos = vec3();
|
||||
|
||||
if (clearTarget) {
|
||||
memset((wchar_t *)target->pixels, 0,
|
||||
target->height * target->width * sizeof(target[0]));
|
||||
}
|
||||
|
||||
polygon testP;
|
||||
|
||||
for (int f = 0; f < model->faces.size(); f += 3) {
|
||||
for (int p = 0; p < 3; p++) {
|
||||
testP.points[p] = model->verts[model->faces[f + p]];
|
||||
}
|
||||
|
||||
toScreenSpace(&testP);
|
||||
|
||||
testP.calcDelta();
|
||||
vec3 pos = vec3();
|
||||
for (int x = 0; x < target->height; x++) {
|
||||
for (int y = 0; y < target->height; y++) {
|
||||
if (p->contains(pos)) {
|
||||
if (testP.contains(pos)) {
|
||||
target->set(x, y, vec3(0.0, 255.0, 0.0));
|
||||
}
|
||||
pos.y() += heightScale;
|
||||
@@ -37,6 +60,7 @@ class Renderer {
|
||||
pos.x() += widthScale;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#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