]> icculus.org git repositories - divverent/nexuiz.git/blob - misc/brightspot.c
add shell script running brightspot... and it indeed IS broken
[divverent/nexuiz.git] / misc / brightspot.c
1 #include <stdio.h>
2 #include <math.h>
3
4 // USAGE: see brightspot.sh (and in the future brightspot.bat)
5 // It should output the right parameters for the sun direction in q3map2's format.
6 // But probably is broken.
7
8 #define false 0
9 #define true 1
10
11 int flip[6*3] =
12 {
13         false, false,  true, // "rt"
14          true,  true,  true, // "lf"
15         false,  true, false, // "bk"
16          true, false, false, // "ft"
17         false, false,  true, // "up"
18         false, false,  true  // "dn"
19 };
20
21 static const double skyboxvertex3f[6*4*3] =
22 {
23         // skyside[0]
24          16, -16,  16,
25          16, -16, -16,
26          16,  16, -16,
27          16,  16,  16,
28         // skyside[1]
29         -16,  16,  16,
30         -16,  16, -16,
31         -16, -16, -16,
32         -16, -16,  16,
33         // skyside[2]
34          16,  16,  16,
35          16,  16, -16,
36         -16,  16, -16,
37         -16,  16,  16,
38         // skyside[3]
39         -16, -16,  16,
40         -16, -16, -16,
41          16, -16, -16,
42          16, -16,  16,
43         // skyside[4]
44         -16, -16,  16,
45          16, -16,  16,
46          16,  16,  16,
47         -16,  16,  16,
48         // skyside[5]
49          16, -16, -16,
50         -16, -16, -16,
51         -16,  16, -16,
52          16,  16, -16
53 };
54
55 void MapCoord(int pic, int x, int y, double vec[3])
56 {
57         int h;
58         int flipx = flip[3*pic+0];
59         int flipy = flip[3*pic+1];
60         int flipdiag = flip[3*pic+2];
61
62         double a[3] = { skyboxvertex3f[pic*4*3+0*3+0], skyboxvertex3f[pic*4*3+0*3+1], skyboxvertex3f[pic*4*3+0*3+2] };
63         double b[3] = { skyboxvertex3f[pic*4*3+1*3+0], skyboxvertex3f[pic*4*3+1*3+1], skyboxvertex3f[pic*4*3+1*3+2] };
64         double c[3] = { skyboxvertex3f[pic*4*3+2*3+0], skyboxvertex3f[pic*4*3+2*3+1], skyboxvertex3f[pic*4*3+2*3+2] };
65         //double d[3] = { skyboxvertex3f[pic*4*3+3*3+0], skyboxvertex3f[pic*4*3+3*3+1], skyboxvertex3f[pic*4*3+3*3+2] };
66
67         if(flipx)
68         {
69                 x = 511 - x;
70         }
71
72         if(flipy)
73         {
74                 y = 511 - y;
75         }
76
77         if(flipdiag)
78         {
79                 h = x; x = y; y = h;
80         }
81
82         vec[0] = a[0] + (b[0] - a[0]) * (x + 0.5) / 512.0 + (c[0] - b[0]) * (y + 0.5) / 512.0;
83         vec[1] = a[1] + (b[1] - a[1]) * (x + 0.5) / 512.0 + (c[1] - b[1]) * (y + 0.5) / 512.0;
84         vec[2] = a[2] + (b[2] - a[2]) * (x + 0.5) / 512.0 + (c[2] - b[2]) * (y + 0.5) / 512.0;
85 }
86
87 int main(int argc, char **argv)
88 {
89         FILE *f;
90         int i, j, k;
91         unsigned char picture[6][512][512];
92         double brightvec[3];
93         double pitch, yaw;
94
95         if(argc != 2)
96         {
97                 fprintf(stderr, "Usage: %s imagefile.gray\n", *argv);
98                 return 1;
99         }
100
101         f = fopen(argv[1], "rb");
102         if(!f)
103         {
104                 perror("fopen");
105                 return 1;
106         }
107         fread(&picture, sizeof(picture), 1, f);
108         fclose(f);
109
110         brightvec[0] = brightvec[1] = brightvec[2] = 0;
111         for(i = 0; i < 6; ++i)
112                 for(j = 0; j < 512; ++j)
113                         for(k = 0; k < 512; ++k)
114                         {
115                                 double vec[3], f;
116                                 MapCoord(i, j, k, vec);
117                                 f = exp(10 * (picture[i][j][k] - 255)) / sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]);
118                                 brightvec[0] += f * vec[0];
119                                 brightvec[1] += f * vec[1];
120                                 brightvec[2] += f * vec[2];
121                         }
122         
123         pitch = -atan2(brightvec[2], sqrt(brightvec[0]*brightvec[0] + brightvec[1]*brightvec[1]));
124         yaw = atan2(brightvec[1], brightvec[0]);
125
126         printf("%f %f\n", yaw * 180 / M_PI, pitch * 180 / M_PI);
127         return 0;
128 }