]> git.mxchange.org Git - simgear.git/blob - simgear/misc/texcoord.cxx
Starting to work on an independent sky implimentation that can be used by
[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     for ( int i = 0; i < (int)fan.size(); ++i ) {
84         p = geod_nodes[ fan[i] ];
85         // cout << "point p = " << p << endl;
86
87         t = basic_tex_coord( p, degree_width, degree_height, scale );
88         // cout << "basic_tex_coord = " << t << endl;
89
90         if ( first ) {
91             tmin = tmax = t;
92             first = false;
93         } else {
94             if ( t.x() < tmin.x() ) {
95                 tmin.setx( t.x() );
96             }
97             if ( t.y() < tmin.y() ) {
98                 tmin.sety( t.y() );
99             }
100             if ( t.x() > tmax.x() ) {
101                 tmax.setx( t.x() );
102             }
103             if ( t.y() > tmax.y() ) {
104                 tmax.sety( t.y() );
105             }
106         }
107     }
108
109     double dx = fabs( tmax.x() - tmin.x() );
110     double dy = fabs( tmax.y() - tmin.y() );
111     // cout << "dx = " << dx << " dy = " << dy << endl;
112
113     bool do_shift = false;
114     Point3D mod_shift;
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;
120         do_shift = true;
121         tmin.setx( (double)( (int)tmin.x() + 1 ) );
122         tmin.sety( (double)( (int)tmin.y() + 1 ) );
123         // cout << "found tmin = " << tmin << endl;
124     } else {
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; }
130
131         double y1 = fmod(tmin.y(), MAX_TEX_COORD);
132         while ( y1 < 0 ) { y1 += MAX_TEX_COORD; }
133
134         double x2 = fmod(tmax.x(), MAX_TEX_COORD);
135         while ( x2 < 0 ) { x2 += MAX_TEX_COORD; }
136
137         double y2 = fmod(tmax.y(), MAX_TEX_COORD);
138         while ( y2 < 0 ) { y2 += MAX_TEX_COORD; }
139         
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.
145
146         if ( x1 > x2 ) {
147             mod_shift.setx( HALF_MAX_TEX_COORD );
148         } else {
149             mod_shift.setx( 0.0 );
150         }
151
152         if ( y1 > y2 ) {
153             mod_shift.sety( HALF_MAX_TEX_COORD );
154         } else {
155             mod_shift.sety( 0.0 );
156         }
157
158         // cout << "mod_shift = " << mod_shift << endl;
159     }
160
161     // generate tex_list
162     Point3D adjusted_t;
163     point_list tex;
164     tex.clear();
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;
169
170         if ( do_shift ) {
171             adjusted_t = t - tmin;
172         } else {
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 );
176             }
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 );
180             }
181             // cout << "adjusted_t " << adjusted_t << endl;
182         }
183
184         if ( adjusted_t.x() < FG_EPSILON ) {
185             adjusted_t.setx( 0.0 );
186         }
187         if ( adjusted_t.y() < FG_EPSILON ) {
188             adjusted_t.sety( 0.0 );
189         }
190         adjusted_t.setz( 0.0 );
191         // cout << "adjusted_t = " << adjusted_t << endl;
192         
193         tex.push_back( adjusted_t );
194     }
195
196     return tex;
197 }