Files
SoftwareRenderer/renderer.hpp

67 lines
1.8 KiB
C++

#ifndef RENDERER_H
#define RENDERER_H
#include "fastmath.hpp"
#include "model.hpp"
#include "polygon.hpp"
#include "rendertarget.hpp"
#include <memory.h>
class Renderer {
public:
Rendertarget *target;
bool clearTarget = true;
Renderer() : clearTarget(false) {}
void toScreenSpace(polygon *p) {
for (int i = 0; i < 3; i++) {
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;
if (clearTarget) {
memset((wchar_t *)target->pixels, 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 y = 0; y < target->height; y++) {
if (testP.contains(pos)) {
target->set(x, y, vec3(0.0, 255.0, 0.0));
}
pos.y() += heightScale;
}
pos.y() = decimal(0);
pos.x() += widthScale;
}
}
}
};
#endif