1 // texcoord.hxx -- routine(s) to handle texture coordinate generation
3 // Written by Curtis Olson, started March 1999.
5 // Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
24 #include "texcoord.hxx"
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 )
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,
37 return Point3D( p.x() * ( degree_width * scale /
38 FG_STANDARD_TEXTURE_DIMENSION ),
39 p.y() * ( degree_width * scale /
40 FG_STANDARD_TEXTURE_DIMENSION ),
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 )
50 // cout << "calculating texture coordinates for a specific fan of size = "
51 // << fan.size() << endl;
53 // calculate perimeter based on center of this degree (not center
55 double clat = (int)b.get_center_lat();
57 clat = (int)clat + 0.5;
59 clat = (int)clat - 0.5;
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;
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;
75 double perimeter = 2.0 * EQUATORIAL_RADIUS_M * FG_PI;
76 double degree_height = perimeter / 360.0;
77 // cout << "degree_height = " << degree_height << endl;
79 // find min/max of fan
80 Point3D tmin, tmax, p, t;
85 for ( i = 0; i < (int)fan.size(); ++i ) {
86 p = geod_nodes[ fan[i] ];
87 // cout << "point p = " << p << endl;
89 t = basic_tex_coord( p, degree_width, degree_height, scale );
90 // cout << "basic_tex_coord = " << t << endl;
96 if ( t.x() < tmin.x() ) {
99 if ( t.y() < tmin.y() ) {
102 if ( t.x() > tmax.x() ) {
105 if ( t.y() > tmax.y() ) {
111 double dx = fabs( tmax.x() - tmin.x() );
112 double dy = fabs( tmax.y() - tmin.y() );
113 // cout << "dx = " << dx << " dy = " << dy << endl;
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;
123 if ( tmin.x() < 0 ) {
124 tmin.setx( (double)( (int)tmin.x() - 1 ) );
126 tmin.setx( (int)tmin.x() );
128 if ( tmin.y() < 0 ) {
129 tmin.sety( (double)( (int)tmin.y() - 1 ) );
131 tmin.sety( (int)tmin.y() );
133 // cout << "found tmin = " << tmin << endl;
135 if ( tmin.x() < 0 ) {
136 tmin.setx( ( (int)(tmin.x() / HALF_MAX_TEX_COORD) - 1 )
137 * HALF_MAX_TEX_COORD );
139 tmin.setx( ( (int)(tmin.x() / HALF_MAX_TEX_COORD) )
140 * HALF_MAX_TEX_COORD );
142 if ( tmin.y() < 0 ) {
143 tmin.sety( ( (int)(tmin.y() / HALF_MAX_TEX_COORD) - 1 )
144 * HALF_MAX_TEX_COORD );
146 tmin.sety( ( (int)(tmin.y() / HALF_MAX_TEX_COORD) )
147 * HALF_MAX_TEX_COORD );
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; }
156 double y1 = fmod(tmin.y(), MAX_TEX_COORD);
157 while ( y1 < 0 ) { y1 += MAX_TEX_COORD; }
159 double x2 = fmod(tmax.x(), MAX_TEX_COORD);
160 while ( x2 < 0 ) { x2 += MAX_TEX_COORD; }
162 double y2 = fmod(tmax.y(), MAX_TEX_COORD);
163 while ( y2 < 0 ) { y2 += MAX_TEX_COORD; }
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.
172 mod_shift.setx( HALF_MAX_TEX_COORD );
174 mod_shift.setx( 0.0 );
178 mod_shift.sety( HALF_MAX_TEX_COORD );
180 mod_shift.sety( 0.0 );
183 // cout << "mod_shift = " << mod_shift << endl;
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;
196 adjusted_t = t - tmin;
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 );
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 );
207 // cout << "adjusted_t " << adjusted_t << endl;
210 if ( adjusted_t.x() < FG_EPSILON ) {
211 adjusted_t.setx( 0.0 );
213 if ( adjusted_t.y() < FG_EPSILON ) {
214 adjusted_t.sety( 0.0 );
216 adjusted_t.setz( 0.0 );
217 // cout << "adjusted_t = " << adjusted_t << endl;
219 tex.push_back( adjusted_t );