feat: modelmatrix

This commit is contained in:
2025-12-27 23:51:34 +01:00
parent e13458d793
commit 51989119ba
3 changed files with 26 additions and 11 deletions

View File

@@ -80,7 +80,8 @@ int main(int argc, char *argv[]) {
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::time_point begin =
std::chrono::steady_clock::now(); std::chrono::steady_clock::now();
renderer.render(&testModel); renderer.render(&testModel, mat4::translation(vec3(0.0f, -1.0f, 5.0f)) *
mat4::rotateOnY(-1.5707963267948966f));
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::time_point end =
std::chrono::steady_clock::now(); std::chrono::steady_clock::now();

View File

@@ -20,15 +20,16 @@ class Renderer {
bool clearTarget = true; bool clearTarget = true;
vec3 sunDir = vec3(1.0, -1.0, 1.0).normalize(); vec3 sunDir = vec3(1.0, -1.0, 1.0).normalize();
void toScreenSpace(vec3 *p) { void toScreenSpace(vec3 *np, mat4 matrix) {
p->x() = p->x() / p->z() * decimal(2.0) * decimal(SCREEN_SPACE_SIZE) + vec4 tp = (matrix * vec4(*np, decimal(1.0f)));
tp.x() = tp.x() / tp.z() * decimal(2.0) * decimal(SCREEN_SPACE_SIZE) +
decimal(SCREEN_SPACE_SIZE); decimal(SCREEN_SPACE_SIZE);
p->y() = p->y() / p->z() * decimal(2.0) * decimal(SCREEN_SPACE_SIZE) + tp.y() = tp.y() / tp.z() * decimal(2.0) * decimal(SCREEN_SPACE_SIZE) +
decimal(SCREEN_SPACE_SIZE); decimal(SCREEN_SPACE_SIZE);
p->z() = p->z(); *np = vec3(tp.x(), tp.y(), tp.z());
} }
void render(const model *model) { void render(const model *model, const mat4 matrix) {
decimal widthScale = decimal widthScale =
decimal(SCREEN_SPACE_SIZE * 2) / decimal((float)target->width); decimal(SCREEN_SPACE_SIZE * 2) / decimal((float)target->width);
decimal heightScale = decimal heightScale =
@@ -44,6 +45,7 @@ class Renderer {
// 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]));
target->clearDepth(); target->clearDepth();
target->clearTarget();
} }
vec3 verts[model->verts.size()] = {}; vec3 verts[model->verts.size()] = {};
@@ -51,7 +53,13 @@ class Renderer {
std::copy(model->verts.begin(), model->verts.end(), verts); std::copy(model->verts.begin(), model->verts.end(), verts);
for (int i = 0; i < model->verts.size(); i++) { for (int i = 0; i < model->verts.size(); i++) {
toScreenSpace(verts + i); toScreenSpace(verts + i, matrix);
}
vec3 normals[model->normals.size()] = {};
mat3 normalMatrix = matrix.cutTo<mat3>();
for (int i = 0; i < model->normals.size(); i++) {
normals[i] = normalMatrix * model->normals[i];
} }
polygon testP; polygon testP;
@@ -62,8 +70,7 @@ class Renderer {
testP.points[p] = verts[std::get<0>(model->faces[f + p])]; testP.points[p] = verts[std::get<0>(model->faces[f + p])];
testP.colors[p] = testP.colors[p] =
model->colors[std::get<0>(model->faces[f + p])]; model->colors[std::get<0>(model->faces[f + p])];
testP.normals[p] = testP.normals[p] = normals[std::get<1>(model->faces[f + p])];
model->normals[std::get<1>(model->faces[f + p])];
} }
if ((testP.avgNormal() * vec3(0.0, 0.0, 1.0)) > decimal(0.)) if ((testP.avgNormal() * vec3(0.0, 0.0, 1.0)) > decimal(0.))
continue; continue;
@@ -86,8 +93,6 @@ class Renderer {
for (int y = startY; y < endY; y++) { for (int y = startY; y < endY; y++) {
if (testP.depContains(pos)) { if (testP.depContains(pos)) {
if (testP.small)
continue;
testP.calcBarycentric(pos); testP.calcBarycentric(pos);
decimal depth = testP.calcDepth(); decimal depth = testP.calcDepth();
@@ -108,6 +113,10 @@ class Renderer {
// target->set(x, y, // target->set(x, y,
// (normal + vec3(1.0, 1.0, 1.0)) * // (normal + vec3(1.0, 1.0, 1.0)) *
// decimal(120.0)); // decimal(120.0));
// target->set(
// x, y,
// (testP.avgNormal() + vec3(1.0, 1.0, 1.0)) *
// decimal(120.0));
// target->set(x, y, // target->set(x, y,
// testP.barycentrics * decimal(200.0)); // testP.barycentrics * decimal(200.0));
// if (!factors.isSmall()) // if (!factors.isSmall())

View File

@@ -32,6 +32,11 @@ class Rendertarget {
depth[i].i = std::numeric_limits<int32_t>::max(); depth[i].i = std::numeric_limits<int32_t>::max();
} }
} }
void clearTarget() {
for (int i = 0; i < width * height * 3; i++) {
pixels[i] = 0;
}
}
uint8_t *pixels; uint8_t *pixels;
decimal *depth; decimal *depth;