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 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.
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.
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.
25 #include "texcoord.hxx"
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 )
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,
38 return Point3D( p.x() * ( degree_width * scale /
39 FG_STANDARD_TEXTURE_DIMENSION ),
40 p.y() * ( degree_width * scale /
41 FG_STANDARD_TEXTURE_DIMENSION ),
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 )
51 // cout << "calculating texture coordinates for a specific fan of size = "
52 // << fan.size() << endl;
54 // calculate perimeter based on center of this degree (not center
56 double clat = (int)b.get_center_lat();
58 clat = (int)clat + 0.5;
60 clat = (int)clat - 0.5;
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;
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;
76 double perimeter = 2.0 * EQUATORIAL_RADIUS_M * FG_PI;
77 double degree_height = perimeter / 360.0;
78 // cout << "degree_height = " << degree_height << endl;
80 // find min/max of fan
81 Point3D tmin, tmax, p, t;
86 for ( i = 0; i < (int)fan.size(); ++i ) {
87 p = geod_nodes[ fan[i] ];
88 // cout << "point p = " << p << endl;
90 t = basic_tex_coord( p, degree_width, degree_height, scale );
91 // cout << "basic_tex_coord = " << t << endl;
97 if ( t.x() < tmin.x() ) {
100 if ( t.y() < tmin.y() ) {
103 if ( t.x() > tmax.x() ) {
106 if ( t.y() > tmax.y() ) {
112 double dx = fabs( tmax.x() - tmin.x() );
113 double dy = fabs( tmax.y() - tmin.y() );
114 // cout << "dx = " << dx << " dy = " << dy << endl;
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;
124 if ( tmin.x() < 0 ) {
125 tmin.setx( (double)( (int)tmin.x() - 1 ) );
127 tmin.setx( (int)tmin.x() );
129 if ( tmin.y() < 0 ) {
130 tmin.sety( (double)( (int)tmin.y() - 1 ) );
132 tmin.sety( (int)tmin.y() );
134 // cout << "found tmin = " << tmin << endl;
136 if ( tmin.x() < 0 ) {
137 tmin.setx( ( (int)(tmin.x() / HALF_MAX_TEX_COORD) - 1 )
138 * HALF_MAX_TEX_COORD );
140 tmin.setx( ( (int)(tmin.x() / HALF_MAX_TEX_COORD) )
141 * HALF_MAX_TEX_COORD );
143 if ( tmin.y() < 0 ) {
144 tmin.sety( ( (int)(tmin.y() / HALF_MAX_TEX_COORD) - 1 )
145 * HALF_MAX_TEX_COORD );
147 tmin.sety( ( (int)(tmin.y() / HALF_MAX_TEX_COORD) )
148 * HALF_MAX_TEX_COORD );
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; }
157 double y1 = fmod(tmin.y(), MAX_TEX_COORD);
158 while ( y1 < 0 ) { y1 += MAX_TEX_COORD; }
160 double x2 = fmod(tmax.x(), MAX_TEX_COORD);
161 while ( x2 < 0 ) { x2 += MAX_TEX_COORD; }
163 double y2 = fmod(tmax.y(), MAX_TEX_COORD);
164 while ( y2 < 0 ) { y2 += MAX_TEX_COORD; }
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.
173 mod_shift.setx( HALF_MAX_TEX_COORD );
175 mod_shift.setx( 0.0 );
179 mod_shift.sety( HALF_MAX_TEX_COORD );
181 mod_shift.sety( 0.0 );
184 // cout << "mod_shift = " << mod_shift << endl;
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;
197 adjusted_t = t - tmin;
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 );
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 );
208 // cout << "adjusted_t " << adjusted_t << endl;
211 if ( adjusted_t.x() < FG_EPSILON ) {
212 adjusted_t.setx( 0.0 );
214 if ( adjusted_t.y() < FG_EPSILON ) {
215 adjusted_t.sety( 0.0 );
217 adjusted_t.setz( 0.0 );
218 // cout << "adjusted_t = " << adjusted_t << endl;
220 tex.push_back( adjusted_t );