Compare commits
2 Commits
0c94bf1df2
...
7ab3825598
| Author | SHA1 | Date | |
|---|---|---|---|
| 7ab3825598 | |||
| efbde487cc |
@@ -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] +
|
||||
|
||||
28
renderer.hpp
28
renderer.hpp
@@ -20,9 +20,9 @@ class Renderer {
|
||||
bool clearTarget = true;
|
||||
|
||||
void toScreenSpace(vec3 *p) {
|
||||
p->x() = p->x() / p->z() * decimal(SCREEN_SPACE_SIZE) +
|
||||
p->x() = p->x() / p->z() * decimal(2.0) * decimal(SCREEN_SPACE_SIZE) +
|
||||
decimal(SCREEN_SPACE_SIZE);
|
||||
p->y() = p->y() / p->z() * decimal(SCREEN_SPACE_SIZE) +
|
||||
p->y() = p->y() / p->z() * decimal(2.0) * decimal(SCREEN_SPACE_SIZE) +
|
||||
decimal(SCREEN_SPACE_SIZE);
|
||||
p->z() = p->z();
|
||||
}
|
||||
@@ -58,16 +58,24 @@ class Renderer {
|
||||
for (int f = 0; f < model->faces.size(); f += 3) {
|
||||
for (int p = 0; p < 3; p++) {
|
||||
testP.points[p] = verts[std::get<0>(model->faces[f + p])];
|
||||
testP.colors[p] =
|
||||
model->colors[std::get<0>(model->faces[f + p])];
|
||||
testP.normals[p] =
|
||||
model->normals[std::get<1>(model->faces[f + p])];
|
||||
}
|
||||
|
||||
testP.calcDelta();
|
||||
|
||||
int startX = (testP.bounding[0] * invWidthScale).i >> SHIFT_AMOUNT;
|
||||
int endX = (testP.bounding[1] * invWidthScale).i >> SHIFT_AMOUNT;
|
||||
int startY = (testP.bounding[2] * invHeightScale).i >> SHIFT_AMOUNT;
|
||||
int endY = (testP.bounding[3] * invHeightScale).i >> SHIFT_AMOUNT;
|
||||
int startX = std::max(
|
||||
(testP.bounding[0] * invWidthScale).i >> SHIFT_AMOUNT, 0);
|
||||
int endX =
|
||||
std::min((testP.bounding[1] * invWidthScale).i >> SHIFT_AMOUNT,
|
||||
target->width - 1);
|
||||
int startY = std::max(
|
||||
(testP.bounding[2] * invHeightScale).i >> SHIFT_AMOUNT, 0);
|
||||
int endY =
|
||||
std::min((testP.bounding[3] * invHeightScale).i >> SHIFT_AMOUNT,
|
||||
target->height - 1);
|
||||
|
||||
vec3 pos = vec3(testP.bounding[0], testP.bounding[2], 0.0);
|
||||
for (int x = startX; x < endX; x++) {
|
||||
@@ -78,17 +86,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