feat: vertex colors
This commit is contained in:
@@ -7,8 +7,10 @@
|
||||
#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
|
||||
|
||||
@@ -37,11 +37,12 @@ for index,line in enumerate(content):
|
||||
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) + "});"
|
||||
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)
|
||||
|
||||
@@ -11,6 +11,7 @@ struct polygon {
|
||||
|
||||
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}, delta{} {}
|
||||
@@ -84,6 +85,11 @@ struct polygon {
|
||||
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] +
|
||||
|
||||
@@ -78,17 +78,17 @@ class Renderer {
|
||||
decimal depth = testP.calcDepth(factors);
|
||||
if (depth < target->getDepth(x, y)) {
|
||||
// std::cout << factors << std::endl;
|
||||
vec3 normals = testP.calcNormal(factors);
|
||||
vec3 normal = testP.calcNormal(factors);
|
||||
vec3 color = testP.calcColor(factors);
|
||||
decimal lightFac =
|
||||
std::max(
|
||||
normals *
|
||||
normal *
|
||||
(-vec3(1.0, -1.0, 1.0).normalize()),
|
||||
decimal(0.0)) +
|
||||
decimal(0.5);
|
||||
target->setDepth(x, y, depth);
|
||||
target->set(x, y,
|
||||
(lightFac * vec3(1., 1., 1.)) *
|
||||
decimal(120.0));
|
||||
(color * decimal(120.0)) * lightFac);
|
||||
|
||||
// target->set(x, y,
|
||||
// (normals + vec3(1.0, 1.0, 1.0)) *
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user