]> git.mxchange.org Git - simgear.git/blob - simgear/misc/texcoord.cxx
Ed Williams: Added some bulletproofing at the poles.
[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         tmin.setx( (double)( (int)tmin.x() + 1 ) );
124         tmin.sety( (double)( (int)tmin.y() + 1 ) );
125         // cout << "found tmin = " << tmin << endl;
126     } else {
127         // structure is small enough ... we can mod it so we can
128         // properly scale the texture coordinates later.
129         // cout << "MODDING" << endl;
130         double x1 = fmod(tmin.x(), MAX_TEX_COORD);
131         while ( x1 < 0 ) { x1 += MAX_TEX_COORD; }
132
133         double y1 = fmod(tmin.y(), MAX_TEX_COORD);
134         while ( y1 < 0 ) { y1 += MAX_TEX_COORD; }
135
136         double x2 = fmod(tmax.x(), MAX_TEX_COORD);
137         while ( x2 < 0 ) { x2 += MAX_TEX_COORD; }
138
139         double y2 = fmod(tmax.y(), MAX_TEX_COORD);
140         while ( y2 < 0 ) { y2 += MAX_TEX_COORD; }
141         
142         // At this point we know that the object is < 16 wide in
143         // texture coordinate space.  If the modulo of the tmin is >
144         // the mod of the tmax at this point, then we know that the
145         // starting tex coordinate for the tmin > 16 so we can shift
146         // everything down by 16 and get it within the 0-32 range.
147
148         if ( x1 > x2 ) {
149             mod_shift.setx( HALF_MAX_TEX_COORD );
150         } else {
151             mod_shift.setx( 0.0 );
152         }
153
154         if ( y1 > y2 ) {
155             mod_shift.sety( HALF_MAX_TEX_COORD );
156         } else {
157             mod_shift.sety( 0.0 );
158         }
159
160         // cout << "mod_shift = " << mod_shift << endl;
161     }
162
163     // generate tex_list
164     Point3D adjusted_t;
165     point_list tex;
166     tex.clear();
167     for ( i = 0; i < (int)fan.size(); ++i ) {
168         p = geod_nodes[ fan[i] ];
169         t = basic_tex_coord( p, degree_width, degree_height, scale );
170         // cout << "second t = " << t << endl;
171
172         if ( do_shift ) {
173             adjusted_t = t - tmin;
174         } else {
175             adjusted_t.setx( fmod(t.x() + mod_shift.x(), MAX_TEX_COORD) );
176             while ( adjusted_t.x() < 0 ) { 
177                 adjusted_t.setx( adjusted_t.x() + MAX_TEX_COORD );
178             }
179             adjusted_t.sety( fmod(t.y() + mod_shift.y(), MAX_TEX_COORD) );
180             while ( adjusted_t.y() < 0 ) {
181                 adjusted_t.sety( adjusted_t.y() + MAX_TEX_COORD );
182             }
183             // cout << "adjusted_t " << adjusted_t << endl;
184         }
185
186         if ( adjusted_t.x() < FG_EPSILON ) {
187             adjusted_t.setx( 0.0 );
188         }
189         if ( adjusted_t.y() < FG_EPSILON ) {
190             adjusted_t.sety( 0.0 );
191         }
192         adjusted_t.setz( 0.0 );
193         // cout << "adjusted_t = " << adjusted_t << endl;
194         
195         tex.push_back( adjusted_t );
196     }
197
198     return tex;
199 }