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