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