]> git.mxchange.org Git - simgear.git/blob - Scenery/sky.c
Lot's of tweaking with sky rendering and lighting.
[simgear.git] / Scenery / sky.c
1 /**************************************************************************
2  * sky.c -- model sky with an upside down "bowl"
3  *
4  * Written by Curtis Olson, started December 1997.
5  *
6  * Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
7  *
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.
12  *
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.
17  *
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.
21  *
22  * $Id$
23  * (Log is kept at end of this file)
24  **************************************************************************/
25
26
27 #ifdef WIN32
28 #  include <windows.h>
29 #endif
30
31 #include <math.h>
32 /*
33 #include <stdio.h>
34 #include <string.h>
35 #include <time.h>
36 */
37
38 #include <GL/glut.h>
39 #include "../XGL/xgl.h"
40
41 #include "sky.h"
42
43 #include "../Time/fg_time.h"
44
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"
50 /*
51 #include "../Include/general.h"
52 */
53
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
62
63
64 static float sky_inner[12][3];
65 static float sky_middle[12][3];
66 static float sky_outer[12][3];
67
68 /* Calculate the sky structure verticies */
69 void fgSkyInit() {
70     float theta;
71     int i;
72
73     printf("Generating the sky dome vertices.\n");
74
75     for ( i = 0; i < 12; i++ ) {
76         theta = (i * 30.0) * DEG_TO_RAD;
77         
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;
81         
82         printf(" %.2f %.2f\n", cos(theta) * INNER_RADIUS, 
83                sin(theta) * INNER_RADIUS);
84
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;
88             
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;
92             
93     }
94 }
95
96
97 /* Draw the Sky */
98 void fgSkyRender() {
99     struct fgFLIGHT *f;
100     struct fgLIGHT *l;
101     struct fgVIEW *v;
102     float inner_color[4], middle_color[4], diff;
103     int i;
104
105     f = &current_aircraft.flight;
106     l = &cur_light_params;
107     v = &current_view;
108
109     printf("Rendering the sky.\n");
110
111     /*
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;
116     */
117
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;
123     }
124     inner_color[3] = middle_color[3] = l->sky_color[3];
125
126     xglPushMatrix();
127
128     /* Translate to view position */
129     xglTranslatef( v->cur_zero_elev.x, v->cur_zero_elev.y, v->cur_zero_elev.z );
130     /* printf("  Translated to %.2f %.2f %.2f\n", 
131            v->cur_zero_elev.x, v->cur_zero_elev.y, v->cur_zero_elev.z ); */
132
133     /* Rotate to proper orientation */
134     printf("  lon = %.2f  lat = %.2f\n", FG_Longitude * RAD_TO_DEG,
135            FG_Latitude * RAD_TO_DEG);
136     xglRotatef( FG_Longitude * RAD_TO_DEG, 0.0, 0.0, 1.0 );
137     xglRotatef( 90.0 - FG_Latitude * RAD_TO_DEG, 0.0, 1.0, 0.0 );
138
139     /* Draw inner/center section of sky*/
140     xglBegin( GL_TRIANGLE_FAN );
141     xglColor4fv(l->sky_color);
142     xglVertex3f(0.0, 0.0, CENTER_ELEV);
143     xglColor4fv( inner_color );
144     for ( i = 0; i < 12; i++ ) {
145         xglVertex3fv( sky_inner[i] );
146     }
147     xglVertex3fv( sky_inner[0] );
148     xglEnd();
149
150     /* Draw the middle ring */
151     xglBegin( GL_TRIANGLE_STRIP );
152     for ( i = 0; i < 12; i++ ) {
153         xglColor4fv( middle_color );
154         xglVertex3fv( sky_middle[i] );
155         xglColor4fv( inner_color );
156         xglVertex3fv( sky_inner[i] );
157     }
158     xglColor4fv( middle_color );
159     xglVertex3fv( sky_middle[0] );
160     xglColor4fv( inner_color );
161     xglVertex3fv( sky_inner[0] );
162     xglEnd();
163
164     /* Draw the outer ring */
165     xglBegin( GL_TRIANGLE_STRIP );
166     for ( i = 0; i < 12; i++ ) {
167         xglColor4fv( l->fog_color );
168         xglVertex3fv( sky_outer[i] );
169         xglColor4fv( middle_color );
170         xglVertex3fv( sky_middle[i] );
171     }
172     xglColor4fv( l->fog_color );
173     xglVertex3fv( sky_outer[0] );
174     xglColor4fv( middle_color );
175     xglVertex3fv( sky_middle[0] );
176     xglEnd();
177
178     xglPopMatrix();
179 }
180
181
182 /* $Log$
183 /* Revision 1.5  1997/12/19 23:34:59  curt
184 /* Lot's of tweaking with sky rendering and lighting.
185 /*
186  * Revision 1.4  1997/12/19 16:45:02  curt
187  * Working on scene rendering order and options.
188  *
189  * Revision 1.3  1997/12/18 23:32:36  curt
190  * First stab at sky dome actually starting to look reasonable. :-)
191  *
192  * Revision 1.2  1997/12/18 04:07:03  curt
193  * Worked on properly translating and positioning the sky dome.
194  *
195  * Revision 1.1  1997/12/17 23:14:30  curt
196  * Initial revision.
197  * Begin work on rendering the sky. (Rather than just using a clear screen.)
198  *
199  */