Draw the sun, debug the colour and variable 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[600 * 400 * 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   float width = 600, height = 400;
30   TGA *tga = (TGA *)malloc(sizeof(TGA));
31   memcpy(tga->head, tgahead, 12);
32   tga->dx = width;
33   tga->dy = height;
34   tga->head2 = 0x2018;
35   // TODO: flip -- the image is upside down so far
36   glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE, tga->pic);
37   FILE *cc = fopen("fbo.tga", "wb");
38   fwrite(tga, 1, (18 + 3 * width * height), cc);
39   fclose(cc);
40   free(tga);
41 }
42
43 void saveImage(char *img, short width, short height, const char *file) {
44   TGA *tga = (TGA *)malloc(sizeof(TGA));
45   memcpy(tga->head, tgahead, 12);
46   tga->dx = width;
47   tga->dy = height;
48   tga->head2 = 0x2018;
49   memcpy(tga->pic, img, width * height * 3);
50   // TODO: flip -- the image is upside down so far
51   FILE *cc = fopen(file, "wb");
52   fwrite(tga, 1, (18 + 3 * width * height), cc);
53   fclose(cc);
54   free(tga);
55 }
56
57 void saveGrayscale(float *img, short width, short height, const char *file) {
58   char *bytes = (char*)malloc(sizeof(char) * width * height * 3);
59   for (int i = 0; i < width; i++) {
60     for (int j = 0; j < height; j++) {
61       for (int z = 0; z < 3; z++) {
62         bytes[i * 3 + j * width * 3 + z] = (int)(img[i + j * width] * 255.f * 2);
63       }
64     }
65   }
66   saveImage(bytes, width, height, file);
67   free(bytes);
68 }