diff --git a/main.cpp b/main.cpp index 186dfe6..109aa5e 100644 --- a/main.cpp +++ b/main.cpp @@ -1,12 +1,18 @@ #include "fastmath.hpp" #include "polygon.hpp" +#include "renderer.hpp" +#include "rendertarget.hpp" +#include +#include +#include +#include +#include #include #include #include #include #include #include - #define HIGHT 64 #define WIDTH 64 @@ -56,24 +62,54 @@ void drawImage(unsigned short *img) { img[(y.i >> SHIFT_AMOUNT) * RWIDTH + (x.i >> SHIFT_AMOUNT)] = (unsigned short)23; // (((-normal[1]+(1 << // SHIFT_AMOUNT))*14)>>SHIFT_AMOUNT); - } else { - img[(y.i >> SHIFT_AMOUNT) * RWIDTH + (x.i >> SHIFT_AMOUNT)] = - (unsigned short)0; } } } printf("done writing %d \n", *(img + sizeof(unsigned short) * 32 * 32)); } -int main() { - unsigned short *img = - (unsigned short *)malloc(128 * 128 * sizeof(unsigned short)); - while (true) { - drawImage(img); - printf("%s", drawToString(img)); - return 0; - // sleep(0.001); +int main(int argc, char *argv[]) { + Rendertarget target(128, 128); + Renderer renderer; + renderer.target = ⌖ + + polygon poly = + polygon(vec3(0.9, 0.9, 0.0), vec3(0.5, 0.1, 0.0), vec3(0.1, 0.9, 0.0)); + + renderer.render(&poly); + 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 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 * y + x) * 3 + c] = result[c] >> 2; /* + target.pixels[(target.width * y * 2 + x * 2) * 3 + c].i >> + SHIFT_AMOUNT;*/ + } + } } - // printf("%s", test); - return 0; + 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(); } diff --git a/renderer.hpp b/renderer.hpp new file mode 100644 index 0000000..37cd9c1 --- /dev/null +++ b/renderer.hpp @@ -0,0 +1,42 @@ + +#ifndef RENDERER_H +#define RENDERER_H + +#include "fastmath.hpp" +#include "polygon.hpp" +#include "rendertarget.hpp" +#include + +class Renderer { + + public: + Rendertarget *target; + + bool clearTarget = true; + Renderer() : clearTarget(false) {} + + void render(polygon *p) { + decimal widthScale = decimal(1.0) / decimal((float)target->width); + decimal heightScale = decimal(1.0) / decimal((float)target->height); + // std::cout << widthScale << std::endl; + vec3 pos = vec3(); + + if (clearTarget) { + memset((wchar_t *)target->pixels, 0, + target->height * target->width * sizeof(target[0])); + } + + for (int x = 0; x < target->height; x++) { + for (int y = 0; y < target->height; y++) { + if (p->contains(pos)) { + target->set(x, y, vec3(0.0, 255.0, 0.0)); + } + pos.y() += heightScale; + } + pos.y() = decimal(0); + pos.x() += widthScale; + } + } +}; + +#endif diff --git a/rendertarget.hpp b/rendertarget.hpp new file mode 100644 index 0000000..63518b7 --- /dev/null +++ b/rendertarget.hpp @@ -0,0 +1,32 @@ + +#ifndef RENDERTARGET_H +#define RENDERTARGET_H + +#include "fastmath.hpp" +class Rendertarget { + + public: + const int width; + const int height; + + Rendertarget(int width, int height) : width(width), height(height) { + pixels = new decimal[width * height * 3]; + } + + void set(int x, int y, vec3 val) { + int start = (width * y + x) * 3; + for (int i = 0; i < 3; i++) { + pixels[start + i] = val[i]; + } + } + void get(int x, int y, vec3 *val) { + int start = (width * y + x) * 3; + for (int i = 0; i < 3; i++) { + (*val)[i] = pixels[start + i]; + } + } + + decimal *pixels; +}; + +#endif