X-Git-Url: https://git.lukelau.me/?p=clouds.git;a=blobdiff_plain;f=debug.hpp;h=daa756a2df82d4bb0549bfac628a3ca0b1cba8a7;hp=debe65e6eab44875f8ed97e02b8f837ae6a8aeaf;hb=HEAD;hpb=69b74f1d5e67236a5396b75628a6e1be0e501631 diff --git a/debug.hpp b/debug.hpp index debe65e..daa756a 100644 --- a/debug.hpp +++ b/debug.hpp @@ -4,6 +4,8 @@ #include #include +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + inline void dump(glm::mat4 m) { for (int i = 0; i < 4; i++) fprintf(stderr, "%f,%f,%f,%f\n", m[i][0], m[i][1], m[i][2], m[i][3]); @@ -20,22 +22,51 @@ inline void dump(glm::vec2 v) { fprintf(stderr, "{%f,%f}\n", v.x, v.y); } typedef struct { char head[12]; short dx /* Width */, dy /* Height */, head2; - char pic[600 * 400][3]; + char pic[1000 * 1000 * 3]; } TGA; char tgahead[12] = {0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; void saveFBO() { - float width = 600, height = 400; + GLint ps[4]; + glGetIntegerv(GL_VIEWPORT, ps); + GLint width = ps[2], height = ps[3]; TGA *tga = (TGA *)malloc(sizeof(TGA)); memcpy(tga->head, tgahead, 12); tga->dx = width; tga->dy = height; tga->head2 = 0x2018; // TODO: flip -- the image is upside down so far - glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE, tga->pic[0]); + glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE, tga->pic); FILE *cc = fopen("fbo.tga", "wb"); fwrite(tga, 1, (18 + 3 * width * height), cc); fclose(cc); free(tga); } + +void saveImage(char *img, short width, short height, const char *file) { + TGA *tga = (TGA *)malloc(sizeof(TGA)); + memcpy(tga->head, tgahead, 12); + tga->dx = width; + tga->dy = height; + tga->head2 = 0x2018; + memcpy(tga->pic, img, width * height * 3); + // TODO: flip -- the image is upside down so far + FILE *cc = fopen(file, "wb"); + fwrite(tga, 1, (18 + 3 * width * height), cc); + fclose(cc); + free(tga); +} + +void saveGrayscale(float *img, short width, short height, const char *file) { + char *bytes = (char*)malloc(sizeof(char) * width * height * 3); + for (int i = 0; i < width; i++) { + for (int j = 0; j < height; j++) { + for (int z = 0; z < 3; z++) { + bytes[i * 3 + j * width * 3 + z] = (int)(img[i + j * width] * 255.f * 2); + } + } + } + saveImage(bytes, width, height, file); + free(bytes); +}