Hack around brightness and shader viewport size
[clouds.git] / debug.hpp
1 #include <GL/glew.h>
2 #include <cstdio>
3 #include <cstdlib>
4 #include <cstring>
5 #include <glm/glm.hpp>
6
7 inline void dump(glm::mat4 m) {
8   for (int i = 0; i < 4; i++)
9     fprintf(stderr, "%f,%f,%f,%f\n", m[i][0], m[i][1], m[i][2], m[i][3]);
10 }
11
12 inline void dump(glm::vec4 v) {
13   fprintf(stderr, "{%f,%f,%f,%f}\n", v.x, v.y, v.z, v.w);
14 }
15 inline void dump(glm::vec3 v) {
16   fprintf(stderr, "{%f,%f,%f}\n", v.x, v.y, v.z);
17 }
18 inline void dump(glm::vec2 v) { fprintf(stderr, "{%f,%f}\n", v.x, v.y); }
19
20 typedef struct {
21   char head[12];
22   short dx /* Width */, dy /* Height */, head2;
23   char pic[1000 * 1000 * 3];
24 } TGA;
25 char tgahead[12] = {0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
26                     0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
27
28 void saveFBO() {
29   GLint ps[4];
30   glGetIntegerv(GL_VIEWPORT, ps);
31   GLint width = ps[2], height = ps[3];
32   TGA *tga = (TGA *)malloc(sizeof(TGA));
33   memcpy(tga->head, tgahead, 12);
34   tga->dx = width;
35   tga->dy = height;
36   tga->head2 = 0x2018;
37   // TODO: flip -- the image is upside down so far
38   glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE, tga->pic);
39   FILE *cc = fopen("fbo.tga", "wb");
40   fwrite(tga, 1, (18 + 3 * width * height), cc);
41   fclose(cc);
42   free(tga);
43 }
44
45 void saveImage(char *img, short width, short height, const char *file) {
46   TGA *tga = (TGA *)malloc(sizeof(TGA));
47   memcpy(tga->head, tgahead, 12);
48   tga->dx = width;
49   tga->dy = height;
50   tga->head2 = 0x2018;
51   memcpy(tga->pic, img, width * height * 3);
52   // TODO: flip -- the image is upside down so far
53   FILE *cc = fopen(file, "wb");
54   fwrite(tga, 1, (18 + 3 * width * height), cc);
55   fclose(cc);
56   free(tga);
57 }
58
59 void saveGrayscale(float *img, short width, short height, const char *file) {
60   char *bytes = (char*)malloc(sizeof(char) * width * height * 3);
61   for (int i = 0; i < width; i++) {
62     for (int j = 0; j < height; j++) {
63       for (int z = 0; z < 3; z++) {
64         bytes[i * 3 + j * width * 3 + z] = (int)(img[i + j * width] * 255.f * 2);
65       }
66     }
67   }
68   saveImage(bytes, width, height, file);
69   free(bytes);
70 }