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;
83 for ( int i = 0; i < (int)fan.size(); ++i ) {
84 p = geod_nodes[ fan[i] ];
85 // cout << "point p = " << p << endl;
87 t = basic_tex_coord( p, degree_width, degree_height, scale );
88 // cout << "basic_tex_coord = " << t << endl;
94 if ( t.x() < tmin.x() ) {
97 if ( t.y() < tmin.y() ) {
100 if ( t.x() > tmax.x() ) {
103 if ( t.y() > tmax.y() ) {
109 double dx = fabs( tmax.x() - tmin.x() );
110 double dy = fabs( tmax.y() - tmin.y() );
111 // cout << "dx = " << dx << " dy = " << dy << endl;
113 bool do_shift = false;
115 if ( (dx > HALF_MAX_TEX_COORD) || (dy > HALF_MAX_TEX_COORD) ) {
116 // structure is too big, we'll just have to shift it so that
117 // tmin = (0,0). This messes up subsequent texture scaling,
118 // but is the best we can do.
119 // cout << "SHIFTING" << endl;
121 tmin.setx( (double)( (int)tmin.x() + 1 ) );
122 tmin.sety( (double)( (int)tmin.y() + 1 ) );
123 // cout << "found tmin = " << tmin << endl;
125 // structure is small enough ... we can mod it so we can
126 // properly scale the texture coordinates later.
127 // cout << "MODDING" << endl;
128 double x1 = fmod(tmin.x(), MAX_TEX_COORD);
129 while ( x1 < 0 ) { x1 += MAX_TEX_COORD; }
131 double y1 = fmod(tmin.y(), MAX_TEX_COORD);
132 while ( y1 < 0 ) { y1 += MAX_TEX_COORD; }
134 double x2 = fmod(tmax.x(), MAX_TEX_COORD);
135 while ( x2 < 0 ) { x2 += MAX_TEX_COORD; }
137 double y2 = fmod(tmax.y(), MAX_TEX_COORD);
138 while ( y2 < 0 ) { y2 += MAX_TEX_COORD; }
140 // At this point we know that the object is < 16 wide in
141 // texture coordinate space. If the modulo of the tmin is >
142 // the mod of the tmax at this point, then we know that the
143 // starting tex coordinate for the tmin > 16 so we can shift
144 // everything down by 16 and get it within the 0-32 range.
147 mod_shift.setx( HALF_MAX_TEX_COORD );
149 mod_shift.setx( 0.0 );
153 mod_shift.sety( HALF_MAX_TEX_COORD );
155 mod_shift.sety( 0.0 );
158 // cout << "mod_shift = " << mod_shift << endl;
165 for ( int i = 0; i < (int)fan.size(); ++i ) {
166 p = geod_nodes[ fan[i] ];
167 t = basic_tex_coord( p, degree_width, degree_height, scale );
168 // cout << "second t = " << t << endl;
171 adjusted_t = t - tmin;
173 adjusted_t.setx( fmod(t.x() + mod_shift.x(), MAX_TEX_COORD) );
174 while ( adjusted_t.x() < 0 ) {
175 adjusted_t.setx( adjusted_t.x() + MAX_TEX_COORD );
177 adjusted_t.sety( fmod(t.y() + mod_shift.y(), MAX_TEX_COORD) );
178 while ( adjusted_t.y() < 0 ) {
179 adjusted_t.sety( adjusted_t.y() + MAX_TEX_COORD );
181 // cout << "adjusted_t " << adjusted_t << endl;
184 if ( adjusted_t.x() < FG_EPSILON ) {
185 adjusted_t.setx( 0.0 );
187 if ( adjusted_t.y() < FG_EPSILON ) {
188 adjusted_t.sety( 0.0 );
190 adjusted_t.setz( 0.0 );
191 // cout << "adjusted_t = " << adjusted_t << endl;
193 tex.push_back( adjusted_t );