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) {
|
friend bool operator>(const decimal &d1, const decimal &d2) {
|
||||||
return d1.i > d2.i;
|
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) {
|
friend bool operator==(const decimal &d1, const decimal &d2) {
|
||||||
return d1.i == d2.i;
|
return d1.i == d2.i;
|
||||||
@@ -62,7 +68,6 @@ struct decimal {
|
|||||||
friend bool operator!=(const decimal &d1, const decimal &d2) {
|
friend bool operator!=(const decimal &d1, const decimal &d2) {
|
||||||
return d1.i != d2.i;
|
return d1.i != d2.i;
|
||||||
}
|
}
|
||||||
|
|
||||||
decimal &operator=(decimal const &in) {
|
decimal &operator=(decimal const &in) {
|
||||||
if (this != &in) {
|
if (this != &in) {
|
||||||
std::destroy_at(this);
|
std::destroy_at(this);
|
||||||
@@ -155,6 +160,13 @@ template <int n, class Dev> struct vec {
|
|||||||
}
|
}
|
||||||
return res;
|
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 &operator[](const int &i) { return v[i]; }
|
||||||
|
|
||||||
decimal len_sq() { return *this * *this; }
|
decimal len_sq() { return *this * *this; }
|
||||||
|
|||||||
10
main.cpp
10
main.cpp
@@ -2,6 +2,7 @@
|
|||||||
#include "polygon.hpp"
|
#include "polygon.hpp"
|
||||||
#include "renderer.hpp"
|
#include "renderer.hpp"
|
||||||
#include "rendertarget.hpp"
|
#include "rendertarget.hpp"
|
||||||
|
#include "testModel.hpp"
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QImage>
|
#include <QImage>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
@@ -74,9 +75,9 @@ int main(int argc, char *argv[]) {
|
|||||||
renderer.target = ⌖
|
renderer.target = ⌖
|
||||||
|
|
||||||
polygon poly =
|
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);
|
QApplication a(argc, argv);
|
||||||
|
|
||||||
uint8_t *pixel = new uint8_t[64 * 64 * 3];
|
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 + (x * 2 + 1)) * 3, result);
|
||||||
addTo((target.width * (y * 2 + 1) + (x * 2 + 1)) * 3, result);
|
addTo((target.width * (y * 2 + 1) + (x * 2 + 1)) * 3, result);
|
||||||
for (int c = 0; c < 3; c++) {
|
for (int c = 0; c < 3; c++) {
|
||||||
pixel[(WIDTH * y + x) * 3 + c] = result[c] >> 2; /*
|
pixel[(WIDTH * (WIDTH - y - 1) + WIDTH - x - 1) * 3 + c] =
|
||||||
target.pixels[(target.width * y * 2 + x * 2) * 3 + c].i >>
|
result[c] >> 2;
|
||||||
SHIFT_AMOUNT;*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
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"
|
#include "fastmath.hpp"
|
||||||
struct polygon {
|
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)
|
polygon(const vec3 &v1, const vec3 &v2, const vec3 &v3)
|
||||||
: points{v1, v2, v3} {}
|
: points{v1, v2, v3} {}
|
||||||
|
polygon() : points{} {}
|
||||||
|
|
||||||
|
void calcDelta() {
|
||||||
|
|
||||||
bool contains(const vec3 &p) {
|
|
||||||
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];
|
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];
|
vec3 s = p - points[n];
|
||||||
|
if (s.isSmall())
|
||||||
if (s * d < decimal(0.0))
|
return false;
|
||||||
|
if (s * delta[i] <= decimal(0.0))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
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
|
#endif
|
||||||
|
|||||||
48
renderer.hpp
48
renderer.hpp
@@ -3,6 +3,7 @@
|
|||||||
#define RENDERER_H
|
#define RENDERER_H
|
||||||
|
|
||||||
#include "fastmath.hpp"
|
#include "fastmath.hpp"
|
||||||
|
#include "model.hpp"
|
||||||
#include "polygon.hpp"
|
#include "polygon.hpp"
|
||||||
#include "rendertarget.hpp"
|
#include "rendertarget.hpp"
|
||||||
#include <memory.h>
|
#include <memory.h>
|
||||||
@@ -15,26 +16,49 @@ class Renderer {
|
|||||||
bool clearTarget = true;
|
bool clearTarget = true;
|
||||||
Renderer() : clearTarget(false) {}
|
Renderer() : clearTarget(false) {}
|
||||||
|
|
||||||
void render(polygon *p) {
|
void toScreenSpace(polygon *p) {
|
||||||
decimal widthScale = decimal(1.0) / decimal((float)target->width);
|
for (int i = 0; i < 3; i++) {
|
||||||
decimal heightScale = decimal(1.0) / decimal((float)target->height);
|
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;
|
// std::cout << widthScale << std::endl;
|
||||||
vec3 pos = vec3();
|
|
||||||
|
|
||||||
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]));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int x = 0; x < target->height; x++) {
|
polygon testP;
|
||||||
for (int y = 0; y < target->height; y++) {
|
|
||||||
if (p->contains(pos)) {
|
for (int f = 0; f < model->faces.size(); f += 3) {
|
||||||
target->set(x, y, vec3(0.0, 255.0, 0.0));
|
for (int p = 0; p < 3; p++) {
|
||||||
}
|
testP.points[p] = model->verts[model->faces[f + p]];
|
||||||
pos.y() += heightScale;
|
}
|
||||||
|
|
||||||
|
toScreenSpace(&testP);
|
||||||
|
|
||||||
|
testP.calcDelta();
|
||||||
|
vec3 pos = vec3();
|
||||||
|
for (int x = 0; x < target->height; x++) {
|
||||||
|
for (int y = 0; y < target->height; y++) {
|
||||||
|
if (testP.contains(pos)) {
|
||||||
|
target->set(x, y, vec3(0.0, 255.0, 0.0));
|
||||||
|
}
|
||||||
|
pos.y() += heightScale;
|
||||||
|
}
|
||||||
|
pos.y() = decimal(0);
|
||||||
|
pos.x() += widthScale;
|
||||||
}
|
}
|
||||||
pos.y() = decimal(0);
|
|
||||||
pos.x() += widthScale;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
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