1 /**************************************************************************
2 * sky.c -- model sky with an upside down "bowl"
4 * Written by Curtis Olson, started December 1997.
6 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * (Log is kept at end of this file)
24 **************************************************************************/
39 #include "../XGL/xgl.h"
43 #include "../Time/fg_time.h"
45 #include "../Aircraft/aircraft.h"
46 #include "../Flight/flight.h"
47 #include "../Include/constants.h"
48 #include "../Main/views.h"
49 #include "../Math/fg_random.h"
51 #include "../Include/general.h"
54 /* in meters of course */
55 #define CENTER_ELEV 25000.0
56 #define INNER_RADIUS 50000.0
57 #define INNER_ELEV 20000.0
58 #define MIDDLE_RADIUS 70000.0
59 #define MIDDLE_ELEV 8000.0
60 #define OUTER_RADIUS 80000.0
61 #define OUTER_ELEV 0.0
64 static float sky_inner[12][3];
65 static float sky_middle[12][3];
66 static float sky_outer[12][3];
68 /* Calculate the sky structure verticies */
73 printf("Generating the sky dome vertices.\n");
75 for ( i = 0; i < 12; i++ ) {
76 theta = (i * 30.0) * DEG_TO_RAD;
78 sky_inner[i][0] = cos(theta) * INNER_RADIUS;
79 sky_inner[i][1] = sin(theta) * INNER_RADIUS;
80 sky_inner[i][2] = INNER_ELEV;
82 printf(" %.2f %.2f\n", cos(theta) * INNER_RADIUS,
83 sin(theta) * INNER_RADIUS);
85 sky_middle[i][0] = cos((double)theta) * MIDDLE_RADIUS;
86 sky_middle[i][1] = sin((double)theta) * MIDDLE_RADIUS;
87 sky_middle[i][2] = MIDDLE_ELEV;
89 sky_outer[i][0] = cos((double)theta) * OUTER_RADIUS;
90 sky_outer[i][1] = sin((double)theta) * OUTER_RADIUS;
91 sky_outer[i][2] = OUTER_ELEV;
102 float inner_color[4], middle_color[4], diff, east_dot, dot, angle;
105 f = ¤t_aircraft.flight;
106 l = &cur_light_params;
109 printf("Rendering the sky.\n");
112 l->fog_color[0] = 1.0;
113 l->fog_color[1] = 0.0;
114 l->fog_color[2] = 0.0;
115 l->fog_color[3] = 1.0;
118 /* calculate transition colors between sky and fog */
119 for ( i = 0; i < 3; i++ ) {
120 diff = l->sky_color[i] - l->fog_color[i];
121 inner_color[i] = l->sky_color[i] - diff * 0.3;
122 middle_color[i] = l->sky_color[i] - diff * 1.0;
124 inner_color[3] = middle_color[3] = l->sky_color[3];
128 /* calculate the angle between v->surface_to_sun and
129 * v->surface_east. We do this so we can sort out the acos()
130 * ambiguity. I wish I could think of a more efficient way ... :-( */
131 east_dot = MAT3_DOT_PRODUCT(v->surface_to_sun, v->surface_east);
132 printf(" East dot product = %.2f\n", east_dot);
134 /* calculate the angle between v->surface_to_sun and
135 * v->surface_south. this is how much we have to rotate the sky
136 * for it to align with the sun */
137 dot = MAT3_DOT_PRODUCT(v->surface_to_sun, v->surface_south);
138 printf(" Dot product = %.2f\n", dot);
139 if ( east_dot >= 0 ) {
144 printf(" Sky needs to rotate = %.3f rads = %.1f degrees.\n",
145 angle, angle * RAD_TO_DEG);
147 /* Translate to view position */
148 xglTranslatef( v->cur_zero_elev.x, v->cur_zero_elev.y, v->cur_zero_elev.z );
149 /* printf(" Translated to %.2f %.2f %.2f\n",
150 v->cur_zero_elev.x, v->cur_zero_elev.y, v->cur_zero_elev.z ); */
152 /* Rotate to proper orientation */
153 printf(" lon = %.2f lat = %.2f\n", FG_Longitude * RAD_TO_DEG,
154 FG_Latitude * RAD_TO_DEG);
155 xglRotatef( FG_Longitude * RAD_TO_DEG, 0.0, 0.0, 1.0 );
156 xglRotatef( 90.0 - FG_Latitude * RAD_TO_DEG, 0.0, 1.0, 0.0 );
157 xglRotatef( angle * RAD_TO_DEG, 0.0, 0.0, 1.0 );
159 /* Draw inner/center section of sky*/
160 xglBegin( GL_TRIANGLE_FAN );
161 xglColor4fv(l->sky_color);
162 xglVertex3f(0.0, 0.0, CENTER_ELEV);
163 xglColor4fv( inner_color );
164 for ( i = 0; i < 12; i++ ) {
165 xglVertex3fv( sky_inner[i] );
167 xglVertex3fv( sky_inner[0] );
170 /* Draw the middle ring */
171 xglBegin( GL_TRIANGLE_STRIP );
172 for ( i = 0; i < 12; i++ ) {
173 xglColor4fv( middle_color );
174 xglVertex3fv( sky_middle[i] );
175 xglColor4fv( inner_color );
176 xglVertex3fv( sky_inner[i] );
178 xglColor4fv( middle_color );
179 xglColor4f(1.0, 0.0, 0.0, 1.0);
180 xglVertex3fv( sky_middle[0] );
181 xglColor4fv( inner_color );
182 xglColor4f(1.0, 0.0, 0.0, 1.0);
183 xglVertex3fv( sky_inner[0] );
186 /* Draw the outer ring */
187 xglBegin( GL_TRIANGLE_STRIP );
188 for ( i = 0; i < 12; i++ ) {
189 xglColor4fv( l->fog_color );
190 xglVertex3fv( sky_outer[i] );
191 xglColor4fv( middle_color );
192 xglVertex3fv( sky_middle[i] );
194 xglColor4fv( l->fog_color );
195 xglVertex3fv( sky_outer[0] );
196 xglColor4fv( middle_color );
197 xglVertex3fv( sky_middle[0] );
205 /* Revision 1.6 1997/12/22 04:14:34 curt
206 /* Aligned sky with sun so dusk/dawn effects can be correct relative to the sun.
208 * Revision 1.5 1997/12/19 23:34:59 curt
209 * Lot's of tweaking with sky rendering and lighting.
211 * Revision 1.4 1997/12/19 16:45:02 curt
212 * Working on scene rendering order and options.
214 * Revision 1.3 1997/12/18 23:32:36 curt
215 * First stab at sky dome actually starting to look reasonable. :-)
217 * Revision 1.2 1997/12/18 04:07:03 curt
218 * Worked on properly translating and positioning the sky dome.
220 * Revision 1.1 1997/12/17 23:14:30 curt
222 * Begin work on rendering the sky. (Rather than just using a clear screen.)