]> git.mxchange.org Git - simgear.git/blob - simgear/misc/texcoord.cxx
Still tweaking output messages.
[simgear.git] / simgear / misc / texcoord.cxx
1 // texcoord.hxx -- routine(s) to handle texture coordinate generation
2 //
3 // Written by Curtis Olson, started March 1999.
4 //
5 // Copyright (C) 1999  Curtis L. Olson  - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #include "texcoord.hxx"
25
26
27 #define FG_STANDARD_TEXTURE_DIMENSION 1000.0 // meters
28 #define MAX_TEX_COORD 8.0
29 #define HALF_MAX_TEX_COORD ( MAX_TEX_COORD / 2.0 )
30
31
32 // return the basic unshifted/unmoded texture coordinate for a lat/lon
33 inline Point3D basic_tex_coord( const Point3D& p, 
34                                 double degree_width, double degree_height,
35                                 double scale )
36 {
37     return Point3D( p.x() * ( degree_width * scale / 
38                               FG_STANDARD_TEXTURE_DIMENSION ),
39                     p.y() * ( degree_width * scale /
40                               FG_STANDARD_TEXTURE_DIMENSION ),
41                     0.0 );
42 }
43
44
45 // traverse the specified fan/strip/list of vertices and attempt to
46 // calculate "none stretching" texture coordinates
47 point_list calc_tex_coords( const FGBucket& b, const point_list& geod_nodes,
48                             const int_list& fan, double scale )
49 {
50     // cout << "calculating texture coordinates for a specific fan of size = "
51     //      << fan.size() << endl;
52
53     // calculate perimeter based on center of this degree (not center
54     // of bucket)
55     double clat = (int)b.get_center_lat();
56     if ( clat > 0 ) {
57         clat = (int)clat + 0.5;
58     } else {
59         clat = (int)clat - 0.5;
60     }
61
62     double clat_rad = clat * DEG_TO_RAD;
63     double cos_lat = cos( clat_rad );
64     double local_radius = cos_lat * EQUATORIAL_RADIUS_M;
65     double local_perimeter = 2.0 * local_radius * FG_PI;
66     double degree_width = local_perimeter / 360.0;
67
68     // cout << "clat = " << clat << endl;
69     // cout << "clat (radians) = " << clat_rad << endl;
70     // cout << "cos(lat) = " << cos_lat << endl;
71     // cout << "local_radius = " << local_radius << endl;
72     // cout << "local_perimeter = " << local_perimeter << endl;
73     // cout << "degree_width = " << degree_width << endl;
74
75     double perimeter = 2.0 * EQUATORIAL_RADIUS_M * FG_PI;
76     double degree_height = perimeter / 360.0;
77     // cout << "degree_height = " << degree_height << endl;
78
79     // find min/max of fan
80     Point3D tmin, tmax, p, t;
81     bool first = true;
82
83     int i;
84
85     for ( i = 0; i < (int)fan.size(); ++i ) {
86         p = geod_nodes[ fan[i] ];
87         // cout << "point p = " << p << endl;
88
89         t = basic_tex_coord( p, degree_width, degree_height, scale );
90         // cout << "basic_tex_coord = " << t << endl;
91
92         if ( first ) {
93             tmin = tmax = t;
94             first = false;
95         } else {
96             if ( t.x() < tmin.x() ) {
97                 tmin.setx( t.x() );
98             }
99             if ( t.y() < tmin.y() ) {
100                 tmin.sety( t.y() );
101             }
102             if ( t.x() > tmax.x() ) {
103                 tmax.setx( t.x() );
104             }
105             if ( t.y() > tmax.y() ) {
106                 tmax.sety( t.y() );
107             }
108         }
109     }
110
111     double dx = fabs( tmax.x() - tmin.x() );
112     double dy = fabs( tmax.y() - tmin.y() );
113     // cout << "dx = " << dx << " dy = " << dy << endl;
114
115     bool do_shift = false;
116     // Point3D mod_shift;
117     if ( (dx > HALF_MAX_TEX_COORD) || (dy > HALF_MAX_TEX_COORD) ) {
118         // structure is too big, we'll just have to shift it so that
119         // tmin = (0,0).  This messes up subsequent texture scaling,
120         // but is the best we can do.
121         // cout << "SHIFTING" << endl;
122         do_shift = true;
123         if ( tmin.x() < 0 ) {
124             tmin.setx( (double)( (int)tmin.x() - 1 ) );
125         } else {
126             tmin.setx( (int)tmin.x() );
127         }
128         if ( tmin.y() < 0 ) {
129             tmin.sety( (double)( (int)tmin.y() - 1 ) );
130         } else {
131             tmin.sety( (int)tmin.y() );
132         }
133         // cout << "found tmin = " << tmin << endl;
134     } else {
135         if ( tmin.x() < 0 ) {
136             tmin.setx( ( (int)(tmin.x() / HALF_MAX_TEX_COORD) - 1 )
137                        * HALF_MAX_TEX_COORD );
138         } else {
139             tmin.setx( ( (int)(tmin.x() / HALF_MAX_TEX_COORD) )
140                        * HALF_MAX_TEX_COORD );
141         }
142         if ( tmin.y() < 0 ) {
143             tmin.sety( ( (int)(tmin.y() / HALF_MAX_TEX_COORD) - 1 )
144                        * HALF_MAX_TEX_COORD );
145         } else {
146             tmin.sety( ( (int)(tmin.y() / HALF_MAX_TEX_COORD) )
147                        * HALF_MAX_TEX_COORD );
148         }
149 #if 0
150         // structure is small enough ... we can mod it so we can
151         // properly scale the texture coordinates later.
152         // cout << "MODDING" << endl;
153         double x1 = fmod(tmin.x(), MAX_TEX_COORD);
154         while ( x1 < 0 ) { x1 += MAX_TEX_COORD; }
155
156         double y1 = fmod(tmin.y(), MAX_TEX_COORD);
157         while ( y1 < 0 ) { y1 += MAX_TEX_COORD; }
158
159         double x2 = fmod(tmax.x(), MAX_TEX_COORD);
160         while ( x2 < 0 ) { x2 += MAX_TEX_COORD; }
161
162         double y2 = fmod(tmax.y(), MAX_TEX_COORD);
163         while ( y2 < 0 ) { y2 += MAX_TEX_COORD; }
164         
165         // At this point we know that the object is < 16 wide in
166         // texture coordinate space.  If the modulo of the tmin is >
167         // the mod of the tmax at this point, then we know that the
168         // starting tex coordinate for the tmax > 16 so we can shift
169         // everything down by 16 and get it within the 0-32 range.
170
171         if ( x1 > x2 ) {
172             mod_shift.setx( HALF_MAX_TEX_COORD );
173         } else {
174             mod_shift.setx( 0.0 );
175         }
176
177         if ( y1 > y2 ) {
178             mod_shift.sety( HALF_MAX_TEX_COORD );
179         } else {
180             mod_shift.sety( 0.0 );
181         }
182 #endif
183         // cout << "mod_shift = " << mod_shift << endl;
184     }
185
186     // generate tex_list
187     Point3D adjusted_t;
188     point_list tex;
189     tex.clear();
190     for ( i = 0; i < (int)fan.size(); ++i ) {
191         p = geod_nodes[ fan[i] ];
192         t = basic_tex_coord( p, degree_width, degree_height, scale );
193         // cout << "second t = " << t << endl;
194
195         // if ( do_shift ) {
196         adjusted_t = t - tmin;
197 #if 0
198         } else {
199             adjusted_t.setx( fmod(t.x() + mod_shift.x(), MAX_TEX_COORD) );
200             while ( adjusted_t.x() < 0 ) { 
201                 adjusted_t.setx( adjusted_t.x() + MAX_TEX_COORD );
202             }
203             adjusted_t.sety( fmod(t.y() + mod_shift.y(), MAX_TEX_COORD) );
204             while ( adjusted_t.y() < 0 ) {
205                 adjusted_t.sety( adjusted_t.y() + MAX_TEX_COORD );
206             }
207             // cout << "adjusted_t " << adjusted_t << endl;
208         }
209 #endif
210         if ( adjusted_t.x() < FG_EPSILON ) {
211             adjusted_t.setx( 0.0 );
212         }
213         if ( adjusted_t.y() < FG_EPSILON ) {
214             adjusted_t.sety( 0.0 );
215         }
216         adjusted_t.setz( 0.0 );
217         // cout << "adjusted_t = " << adjusted_t << endl;
218         
219         tex.push_back( adjusted_t );
220     }
221
222     return tex;
223 }