feat: vertex colors

This commit is contained in:
2025-11-29 10:07:59 +01:00
parent 0c94bf1df2
commit efbde487cc
5 changed files with 15 additions and 6 deletions

View File

@@ -7,8 +7,10 @@
#include <vector> #include <vector>
struct model { struct model {
std::vector<vec3> verts; std::vector<vec3> verts;
// At 0 vertecie index, at 1 normal index
std::vector<std::tuple<uint16_t, uint16_t>> faces; std::vector<std::tuple<uint16_t, uint16_t>> faces;
std::vector<vec3> normals; std::vector<vec3> normals;
std::vector<vec3> colors;
}; };
#endif #endif

View File

@@ -37,11 +37,12 @@ for index,line in enumerate(content):
faces = content[startFaces:-1] faces = content[startFaces:-1]
break break
colors = ["vec3(" +",".join(vert.split(" ")[4:7]) + ")" for vert in verts]
verts = ["vec3(" +",".join(vert.split(" ")[1:4]) + ")" 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] 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] 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) + "});" out = "#include \"model.hpp\" \n const model testModel({" + ",".join(verts) +"},{" + ",".join(faces) + "},{" + ",".join(normals) + "},{"+ ",".join(colors)+"});"
with open("testModel.hpp", "w") as f: with open("testModel.hpp", "w") as f:
f.write(out) f.write(out)

View File

@@ -11,6 +11,7 @@ struct polygon {
decimal bounding[4]; // min x, max x, min y, max y decimal bounding[4]; // min x, max x, min y, max y
vec3 normals[3]; vec3 normals[3];
vec3 colors[3];
polygon(const vec3 &v1, const vec3 &v2, const vec3 &v3) polygon(const vec3 &v1, const vec3 &v2, const vec3 &v3)
: points{v1, v2, v3}, delta{} {} : points{v1, v2, v3}, delta{} {}
@@ -84,6 +85,11 @@ struct polygon {
return result.normalize(); return result.normalize();
} }
vec3 calcColor(vec3 barycentrics) {
return colors[0] * barycentrics[0] + colors[1] * barycentrics[1] +
colors[2] * barycentrics[2];
}
decimal calcDepth(vec3 barycentrics) { decimal calcDepth(vec3 barycentrics) {
return points[0].z() * barycentrics[0] + return points[0].z() * barycentrics[0] +
points[1].z() * barycentrics[1] + points[1].z() * barycentrics[1] +

View File

@@ -78,17 +78,17 @@ class Renderer {
decimal depth = testP.calcDepth(factors); decimal depth = testP.calcDepth(factors);
if (depth < target->getDepth(x, y)) { if (depth < target->getDepth(x, y)) {
// std::cout << factors << std::endl; // std::cout << factors << std::endl;
vec3 normals = testP.calcNormal(factors); vec3 normal = testP.calcNormal(factors);
vec3 color = testP.calcColor(factors);
decimal lightFac = decimal lightFac =
std::max( std::max(
normals * normal *
(-vec3(1.0, -1.0, 1.0).normalize()), (-vec3(1.0, -1.0, 1.0).normalize()),
decimal(0.0)) + decimal(0.0)) +
decimal(0.5); decimal(0.5);
target->setDepth(x, y, depth); target->setDepth(x, y, depth);
target->set(x, y, target->set(x, y,
(lightFac * vec3(1., 1., 1.)) * (color * decimal(120.0)) * lightFac);
decimal(120.0));
// target->set(x, y, // target->set(x, y,
// (normals + vec3(1.0, 1.0, 1.0)) * // (normals + vec3(1.0, 1.0, 1.0)) *

File diff suppressed because one or more lines are too long