]> icculus.org git repositories - divverent/nexuiz.git/blob - misc/spherefunc2skybox.c
add a simple and stupid skybox generator (to be expanded later), for modern art skybo...
[divverent/nexuiz.git] / misc / spherefunc2skybox.c
1 #include <stdio.h>
2 #include <err.h>
3 #include <stdint.h>
4 #include <math.h>
5 #include <string.h>
6
7 typedef void (*mapfunc_t) (double x_in, double y_in, double *x_out, double *y_out, double *z_out);
8 typedef void (*colorfunc_t) (double x, double y, double z, double *r, double *g, double *b);
9
10 void color_test(double x, double y, double z, double *r, double *g, double *b)
11 {
12         // put in a nice function here
13         *r = 0.5 + 0.5 * x;
14         *g = 0.5 + 0.5 * y;
15         *b = 0.5 + 0.5 * z;
16 }
17
18 void map_back(double x_in, double y_in, double *x_out, double *y_out, double *z_out)
19 {
20         *x_out = 2 * x_in - 1;
21         *y_out = +1;
22         *z_out = 1 - 2 * y_in;
23 }
24
25 void map_right(double x_in, double y_in, double *x_out, double *y_out, double *z_out)
26 {
27         *x_out = +1;
28         *y_out = 1 - 2 * x_in;
29         *z_out = 1 - 2 * y_in;
30 }
31
32 void map_front(double x_in, double y_in, double *x_out, double *y_out, double *z_out)
33 {
34         *x_out = 1 - 2 * x_in;
35         *y_out = -1;
36         *z_out = 1 - 2 * y_in;
37 }
38
39 void map_left(double x_in, double y_in, double *x_out, double *y_out, double *z_out)
40 {
41         *x_out = -1;
42         *y_out = 2 * x_in - 1;
43         *z_out = 1 - 2 * y_in;
44 }
45
46 void map_up(double x_in, double y_in, double *x_out, double *y_out, double *z_out)
47 {
48         *x_out = 2 * y_in - 1;
49         *y_out = 1 - 2 * x_in;
50         *z_out = +1;
51 }
52
53 void map_down(double x_in, double y_in, double *x_out, double *y_out, double *z_out)
54 {
55         *x_out = 1 - 2 * y_in;
56         *y_out = 1 - 2 * x_in;
57         *z_out = -1;
58 }
59
60 #define WIDTH 512
61 #define HEIGHT 512
62
63 void writepic(colorfunc_t f, mapfunc_t m, const char *fn)
64 {
65         int x, y;
66         uint8_t tga[18];
67
68         FILE *file = fopen(fn, "wb");
69         if(!file)
70                 err(1, "fopen %s", fn);
71
72         memset(tga, 0, sizeof(tga));
73         tga[2] = 2;          // uncompressed type
74         tga[12] = (WIDTH >> 0) & 0xFF;
75         tga[13] = (WIDTH >> 8) & 0xFF;
76         tga[14] = (HEIGHT >> 0) & 0xFF;
77         tga[15] = (HEIGHT >> 8) & 0xFF;
78         tga[16] = 24;        // pixel size
79
80         fwrite(&tga, sizeof(tga), 1, file);
81         for(y = HEIGHT-1; y >= 0; --y)
82                 for(x = 0; x < WIDTH; ++x)
83                 {
84                         uint8_t rgb[3];
85                         double rr, gg, bb;
86                         double xx, yy;
87                         double xxx, yyy, zzz;
88                         double r;
89                         xx = (x + 0.5) / WIDTH;
90                         yy = (y + 0.5) / HEIGHT;
91                         m(xx, yy, &xxx, &yyy, &zzz);
92                         r = sqrt(xxx*xxx + yyy*yyy + zzz*zzz);
93                         xxx /= r;
94                         yyy /= r;
95                         zzz /= r;
96                         f(xxx, yyy, zzz, &rr, &gg, &bb);
97                         rgb[0] = floor(0.5 + rr * 255);
98                         rgb[1] = floor(0.5 + gg * 255);
99                         rgb[2] = floor(0.5 + bb * 255);
100                         fwrite(rgb, sizeof(rgb), 1, file);
101                 }
102         
103         fclose(file);
104 }
105
106 void map_all(const char *fn, colorfunc_t f)
107 {
108         char buf[1024];
109         snprintf(buf, sizeof(buf), "%s_bk.tga", fn); buf[sizeof(buf) - 1] = 0; writepic(f, map_back, buf);
110         snprintf(buf, sizeof(buf), "%s_ft.tga", fn); buf[sizeof(buf) - 1] = 0; writepic(f, map_front, buf);
111         snprintf(buf, sizeof(buf), "%s_rt.tga", fn); buf[sizeof(buf) - 1] = 0; writepic(f, map_right, buf);
112         snprintf(buf, sizeof(buf), "%s_lf.tga", fn); buf[sizeof(buf) - 1] = 0; writepic(f, map_left, buf);
113         snprintf(buf, sizeof(buf), "%s_up.tga", fn); buf[sizeof(buf) - 1] = 0; writepic(f, map_up, buf);
114         snprintf(buf, sizeof(buf), "%s_dn.tga", fn); buf[sizeof(buf) - 1] = 0; writepic(f, map_down, buf);
115 }
116
117 int main(int argc, char **argv)
118 {
119         if(argc != 2)
120                 errx(1, "file name argument missing");
121         map_all(argv[1], color_test);
122         return 0;
123 }