feat: added the ability to render whole model

This commit is contained in:
2025-11-13 13:47:59 +01:00
parent f8ffb9c0b3
commit e370c1d0c3
6 changed files with 123 additions and 22 deletions

View File

@@ -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 = &target; renderer.target = &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
View 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
View 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

View File

@@ -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

View File

@@ -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,20 +16,42 @@ 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]));
} }
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 x = 0; x < target->height; x++) {
for (int y = 0; y < target->height; y++) { 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)); target->set(x, y, vec3(0.0, 255.0, 0.0));
} }
pos.y() += heightScale; pos.y() += heightScale;
@@ -37,6 +60,7 @@ class Renderer {
pos.x() += widthScale; pos.x() += widthScale;
} }
} }
}
}; };
#endif #endif

2
testModel.hpp Normal file

File diff suppressed because one or more lines are too long