Files
SoftwareRenderer/main.cpp

116 lines
3.8 KiB
C++

#include "fastmath.hpp"
#include "polygon.hpp"
#include "renderer.hpp"
#include "rendertarget.hpp"
#include "testModel.hpp"
#include <QApplication>
#include <QImage>
#include <QLabel>
#include <QPixmap>
#include <functional>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define HIGHT 64
#define WIDTH 64
#define FAA_FAC 2
#define RHIGHT ((int)(HIGHT * FAA_FAC))
#define RWIDTH ((int)(WIDTH * FAA_FAC))
char *drawToString(unsigned short *img) {
char *textImg = (char *)malloc(200000 * sizeof(char));
// strcat(textImg,"\e[1;1H\e[2J"); --clear
for (int y = 0; y < HIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
// printf("%d,%d\n",x,y);
char buff[20];
unsigned short val = 0;
int my = y * FAA_FAC;
int mx = x * FAA_FAC;
val += img[my * RWIDTH + mx];
val += img[my * RWIDTH + mx + 1];
val += img[(my + 1) * RWIDTH + mx];
val += img[(my + 1) * RWIDTH + mx + 1];
sprintf(buff, "\033[38;5;%dm██\033[0m", 232 + val / 4);
strcat(textImg, buff);
}
strcat(textImg, "\n");
}
return textImg;
}
void drawImage(unsigned short *img) {
decimal heightPerPix = decimal(1.0) / decimal((float)(HIGHT * FAA_FAC));
decimal widthPerPix = decimal(1.0) / decimal((float)(WIDTH * FAA_FAC));
polygon poly = polygon(vec3(0.9, 0.9, 0.0) * decimal(128.0),
vec3(0.5, 0.1, 0.0) * decimal(128.0),
vec3(0.1, 0.9, 0.0) * decimal(128.0));
// printf("\n hpp: %f, wpp: %f
// \n",TO_FLOAT(heightPerPix),TO_FLOAT(widthPerPix)); calcViewPos(t);
for (decimal y = 0; y < decimal((float)(RHIGHT)); y += decimal(1.0)) {
for (decimal x = 0; x < decimal((float)(RWIDTH)); x += decimal(1.0)) {
vec3 p = vec3(x, y, 0.0);
if (poly.contains(p)) {
img[(y.i >> SHIFT_AMOUNT) * RWIDTH + (x.i >> SHIFT_AMOUNT)] =
(unsigned short)23; // (((-normal[1]+(1 <<
// SHIFT_AMOUNT))*14)>>SHIFT_AMOUNT);
}
}
}
printf("done writing %d \n", *(img + sizeof(unsigned short) * 32 * 32));
}
int main(int argc, char *argv[]) {
Rendertarget target(128, 128);
Renderer renderer;
renderer.target = &target;
polygon poly =
polygon(vec3(0.9, 0.9, 1.0), vec3(0.5, 0.1, 1.0), vec3(0.1, 0.9, 1.0));
renderer.render(&testModel);
QApplication a(argc, argv);
uint8_t *pixel = new uint8_t[64 * 64 * 3];
/*for (int i = 0; i < 64 * 64 * 3; i++) {
pixel[i] = target.pixels[i].i >> SHIFT_AMOUNT;
}*/
std::function<void(int, uint32_t *)> addTo = [&target](int start,
uint32_t *arr) {
for (int c = 0; c < 3; c++) {
arr[c] += target.pixels[start + c].i >> SHIFT_AMOUNT;
}
};
for (int x = 0; x < 64; x++) {
for (int y = 0; y < 64; y++) {
uint32_t result[3] = {};
addTo((target.width * y * 2 + x * 2) * 3, result);
addTo((target.width * (y * 2 + 1) + x * 2) * 3, result);
addTo((target.width * y * 2 + (x * 2 + 1)) * 3, result);
addTo((target.width * (y * 2 + 1) + (x * 2 + 1)) * 3, result);
for (int c = 0; c < 3; c++) {
pixel[(WIDTH * (WIDTH - y - 1) + WIDTH - x - 1) * 3 + c] =
result[c] >> 2;
}
}
}
QWidget widget;
widget.setAutoFillBackground(true);
widget.setGeometry(0, 0, 500, 500);
QLabel display(&widget);
QImage img((unsigned char *)pixel, 64, 64, QImage::Format_RGB888);
display.setPixmap(QPixmap::fromImage(img).scaled(widget.size()));
widget.show();
return a.exec();
}