src/backdrop.cpp

Go to the documentation of this file.
00001 #include "backdrop.h"
00002 
00003 BACKDROP::BACKDROP()
00004 {
00005         //TEXTURE_PATH = settings.GetDataDir() + "/tex";
00006         error_log.open("logs/backdrop.log");
00007         initdone = false;
00008         sky_tex_img = NULL;
00009 }
00010 
00011 extern bool verbose_output;
00012 BACKDROP::~BACKDROP()
00013 {
00014         if (verbose_output)
00015                 cout << "backdrop deinit" << endl;
00016         
00017         error_log.close();
00018 }
00019 
00020 void BACKDROP::DeInit()
00021 {
00022         if (initdone)
00023         {
00024                 //glDeleteTextures(1, &sun_texture);
00025                 //glDeleteTextures(1, &rain_texture);
00026                 //glDeleteTextures(1, &cloud_texture);
00027                 //glDeleteTextures(1, &moon_texture);
00028                 //glDeleteTextures(1, &stars_texture);
00029                 //glDeleteTextures(1, &sun_texture_ref);
00030                 //glDeleteTextures(1, &sky_texture);
00031                 
00032                 sun_texture.Unload();           
00033                 rain_texture.Unload();
00034                 cloud_texture.Unload();
00035                 moon_texture.Unload();
00036                 stars_texture.Unload();
00037                 sun_texture_ref.Unload();
00038                 sky_texture.Unload();
00039                 
00040                 if (sky_tex_img)
00041                 {
00042                         SDL_FreeSurface( sky_tex_img );
00043                 }
00044                 
00045                 initdone = false;
00046         }
00047 }
00048 
00049 void BACKDROP::Init()
00050 {
00051         fogdensity = 0.0005f;
00052         
00053         //*** Load Texture ***
00054         string filepath = "weather/star.png";
00055         sun_texture.Load(filepath, false);
00056         
00057         /*SDL_Surface *TextureImage[1];                                 // Create Storage Space For The Texture
00058         
00059         int format = GL_RGB;
00060 
00061         // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
00062         if ((TextureImage[0]=IMG_Load(buffer)))
00063         {
00064                 //SDL_SetAlpha(TextureImage[0], 0, 0);
00065                 
00066                 glGenTextures(1, &sun_texture);                                 // Create Texture
00067                 
00068                 // Create MipMapped Texture
00069                 glBindTexture(GL_TEXTURE_2D, sun_texture);
00070                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
00071                 
00072                 {
00073                         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
00074                         glTexImage2D( GL_TEXTURE_2D, 0, format, TextureImage[0]->w, TextureImage[0]->h, 0, format, GL_UNSIGNED_BYTE, TextureImage[0]->pixels );
00075                 }
00076         }
00077         else
00078         {
00079                 //quit, bitmap not found
00080                 error_log << "Could not find texture: " << buffer << "\n";
00081         }
00082 
00083         if (TextureImage[0])                                                            // If Texture Exists
00084         {
00085                 // Free up any memory we may have used
00086         SDL_FreeSurface( TextureImage[0] );
00087         }*/
00088         
00089         
00090         filepath = "weather/star_reflection.png";
00091         //strcpy(buffer, filepath.c_str());
00092         //sun_texture_ref = utility.TexLoad(filepath, GL_RGB, false);
00093         sun_texture_ref.Load(filepath, false);
00094         /*format = GL_RGBA;
00095 
00096         // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
00097         if ((TextureImage[0]=IMG_Load(buffer)))
00098         {
00099                 //SDL_SetAlpha(TextureImage[0], 0, 0);
00100                 
00101                 glGenTextures(1, &sun_texture_ref);                                     // Create Texture
00102                 
00103                 // Create MipMapped Texture
00104                 glBindTexture(GL_TEXTURE_2D, sun_texture_ref);
00105                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
00106                 
00107                 {
00108                         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
00109                         glTexImage2D( GL_TEXTURE_2D, 0, format, TextureImage[0]->w, TextureImage[0]->h, 0, format, GL_UNSIGNED_BYTE, TextureImage[0]->pixels );
00110                 }
00111         }
00112         else
00113         {
00114                 //quit, bitmap not found
00115                 error_log << "Could not find texture: " << buffer << "\n";
00116         }
00117         if (TextureImage[0])                                                            // If Texture Exists
00118         {
00119                 // Free up any memory we may have used
00120         SDL_FreeSurface( TextureImage[0] );
00121         }*/
00122         
00123         string tex_size;
00124         settings.Get( "display.texture_size", tex_size );
00125         filepath = settings.GetFullDataPath("textures/" + tex_size + "/weather/clearSky-joe.png");
00126         //strcpy(buffer, filepath.c_str());
00127 
00128         //sky_texture = utility.TexLoad(filepath, GL_RGB, false);
00129         sky_texture.Load(filepath, false);
00130         sky_tex_img = IMG_Load(filepath.c_str());
00131         
00132         
00133         filepath = "weather/raindrop.tga";
00134         //strcpy(buffer, filepath.c_str());
00135         rain_texture.Load(filepath, false);
00136         /*format = GL_RGBA;
00137         // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
00138         if ((TextureImage[0]=IMG_Load(buffer)))
00139         {
00140                 //SDL_SetAlpha(TextureImage[0], 0, 0);
00141                 
00142                 glGenTextures(1, &rain_texture);                                        // Create Texture
00143                 
00144                 // Create Texture
00145                 glBindTexture(GL_TEXTURE_2D, rain_texture);
00146                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
00147 
00148                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
00149                 glTexImage2D( GL_TEXTURE_2D, 0, format, TextureImage[0]->w, TextureImage[0]->h, 0, format, GL_UNSIGNED_BYTE, TextureImage[0]->pixels );
00150         }
00151         else
00152         {
00153                 error_log << "Could not find texture: " << buffer << "\n";
00154         }*/
00155         //cout << "boo" << endl;
00156         
00157         cloud_texture.Load("weather/clouds.png", true);
00158         
00159         
00160         //check for volumetric fog
00161         /*char extensions[ 16384 ];
00162         int i;
00163         int num_ext = 2;
00164     string extnames[num_ext];
00165         bool hasext[num_ext];
00166         bool extsufficient = true;
00167         extnames[0]     = "GL_ARB_fog_coord";
00168         extnames[1]     = "GL_ARB_multitexture";
00169         //extnames[2]   = "GL_NV_register_combiners";
00170         //extnames[3]   = "GL_NV_texture_shader";
00171     char *buf;
00172         
00173         for (i = 0; i < num_ext; i++)
00174                 hasext[i] = false;
00175 
00176     sprintf( extensions, "%s", (char *)glGetString( GL_ARBENSIONS ) );
00177         //cout << extensions << endl;
00178     buf = strtok( extensions, " " );
00179     while( buf != NULL )
00180     {
00181                 for (i = 0; i < num_ext; i++)
00182                 {
00183                         if( !strcmp( extnames[i].c_str(), buf ) )
00184                         {
00185                                 hasext[i] = true;
00186                         }
00187                 }
00188                 
00189                 buf = strtok( NULL, " " );
00190     }
00191         
00192         for (i = 0; i < num_ext; i++)
00193         {
00194                 if (!hasext[i])
00195                         extsufficient = false;
00196         }
00197 
00198         if (!extsufficient)
00199         {
00200         error_log << "You are missing required extensions.  Expect a crash.\n" << endl;
00201                 error_log << "You need at least a Geforce 3 for these extensions.\n" << endl;
00202                 for (i = 0; i < num_ext; i++)
00203                 {
00204                         error_log << extnames[i] << ": ";
00205                         if (hasext[i])
00206                                 error_log << "YES" << endl;
00207                         else
00208                                 error_log << "NO" << endl;
00209                 }
00210         }*/
00211 
00212         //set up fog
00213         float fogcolor[4] = {  0.53f, 0.74f, 0.91f, 0.0f  };
00214         glFogfv(GL_FOG_COLOR, fogcolor);
00215         glFogf(GL_FOG_START, 100);
00216         glFogf(GL_FOG_END, 1000);
00217         glFogf(GL_FOG_DENSITY, 0.0005f);
00218         //glFogi(GL_FOG_MODE, GL_LINEAR);
00219         glFogi(GL_FOG_MODE, GL_EXP);
00220         glEnable(GL_FOG);
00221 
00222         
00223         //set up fog
00224         /*                                                                                      // Enable Fog
00225         glFogi(GL_FOG_MODE, GL_LINEAR);                                                                         // Fog Fade Is Linear
00226         float fogcolor[4];
00227         fogcolor[0] = 0.09f;
00228         fogcolor[1] = 0.25f;
00229         fogcolor[2] = 0.38f;
00230         fogcolor[3] = 0.2f;
00231         int i;
00232         for (i = 0; i < 4; i++)
00233                 fogcolor[i] *= 0.5f;
00234         glFogfv(GL_FOG_COLOR, fogcolor);
00235         glFogf(GL_FOG_START,  1.0f);                                                                            // Set The Fog Start
00236         glFogf(GL_FOG_END,    0.0f);                                                                            // Set The Fog End
00237         glHint(GL_FOG_HINT, GL_NICEST);                                                                         // Per-Pixel Fog Calculation
00238         glFogi(GL_FOG_COORDINATE_SOURCE_ARB, GL_FOG_COORDINATE_ARB);*/
00239         
00240         sunpos_lng = 0.0f;
00241         sunpos_lat = 0.7f;
00242         
00243         moonpos_lng = 0.0f;
00244         moonpos_lat = 0.2f;
00245         
00246         moon_texture.Load("weather/moon.png", false);
00247         stars_texture.Load("weather/stars.png", false);
00248 }
00249 
00250 void BACKDROP::Draw(VERTEX tersize)
00251 {
00252         //setup gl flags
00253         //glEnable(GL_BLEND);
00254         glDisable(GL_BLEND);
00255         glDisable( GL_TEXTURE_2D );
00256         glDisable( GL_LIGHTING);
00257         //glDisable(GL_FOG);
00258         //glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00259         glDisable(GL_DEPTH_TEST);
00260         /*glBegin(GL_QUADS);
00261         
00262         //setup custom vars
00263         float watercol[4];
00264         watercol[0] = 0.0f;
00265         watercol[1] = 0.0f;
00266         watercol[2] = 0.0f;
00267         watercol[3] = 1.0f;
00268         glColor4fv(watercol);
00269         //float yheight = 3.9f;
00270         float yheightb = 0.1f;
00271         float xw2 = 5000;
00272         float yw2 = 5000;
00273         float xw1 = tersize.x;
00274         float yw1 = tersize.z;
00275         
00276         //draw caustics
00277         float yheightf = 0.0f;
00278         float counter = 0.0001f;
00279         float yheight;
00280         float ystart = 0.0f;
00281 
00282         yheightf = ystart;
00283         yheight = yheightf*tersize.y;
00284         glColor4fv(watercol);
00285         //glNormal3f(0,1,0);
00286         glVertex3f(-xw2,yheight,-yw2);
00287         glVertex3f(-xw2,yheight,yw2);
00288         glVertex3f(xw2,yheight,yw2);
00289         glVertex3f(xw2,yheight,-yw2);
00290         
00291         glEnd();*/
00292 
00293         glEnable(GL_FOG);
00294         glEnable(GL_DEPTH_TEST);
00295         glDisable(GL_BLEND);
00296         glColor4f(1.0f,1.0f,1.0f,1.0f);
00297         glEnable( GL_TEXTURE_2D );
00298         glEnable( GL_LIGHTING);
00299 }
00300 
00301 void BACKDROP::DrawSky(double daytime, float transparency)
00302 {
00303         daytime_cache = daytime;
00304         sunpos_lat = daytime*3.141593f*2.0f;
00305         
00306         moonpos_lat = (daytime+0.4)*3.141593f*2.0f;
00307         
00308         //setup gl flags
00309         glPushAttrib(GL_ALL_ATTRIB_BITS);
00310         glEnable(GL_BLEND);
00311         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00312         glEnable( GL_TEXTURE_2D );
00313         glDisable(GL_LIGHTING);
00314         glDisable(GL_DEPTH_TEST);
00315         glDisable(GL_FOG);
00316         
00317         QUATERNION cor, qx, qy, qz, qt;
00318         double temp_radius, temp_long, temp_lat;
00319         GLdouble temp_matrix[16];
00320         temp_radius = sqrt(cam.position.x*cam.position.x+cam.position.z*cam.position.z+(cam.position.y+EARTH_RADIUS)*(cam.position.y+EARTH_RADIUS));
00321         temp_long = atan2((double)(cam.position.y+EARTH_RADIUS),(double)cam.position.x);
00322         temp_lat = acos(cam.position.z/temp_radius);
00323         temp_lat -= 1.570796f;
00324         temp_long -= 1.570796f;
00325         qz.SetAxisAngle(-temp_long, 0, 0, 1);
00326         qx.SetAxisAngle(temp_lat, 1, 0, 0);
00327         qt = qx*qz;
00328         qt.GetMat(temp_matrix);
00329         
00330         DrawStars(daytime, transparency, temp_matrix);
00331         DrawMoon(daytime, transparency, temp_matrix);
00332         DrawSkyGradient(daytime, transparency, temp_matrix);
00333         /*glColorMask(1,1,1,1);
00334         glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); 
00335         glClear(GL_COLOR_BUFFER_BIT);*/
00336         DrawSun(daytime, transparency, temp_matrix);
00337         DrawClouds(daytime, transparency, temp_matrix);
00338         
00339         glPopAttrib();
00340 }
00341 
00342 void BACKDROP::DrawSkyGradient(double daytime, float transparency, GLdouble *temp_matrix)
00343 {
00344         //setup gl flags
00345         glPushAttrib(GL_ALL_ATTRIB_BITS);
00346         glEnable(GL_BLEND);
00347         //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00348         glBlendFunc(GL_SRC_ALPHA, GL_DST_COLOR);
00349         glEnable( GL_TEXTURE_2D );
00350         glDisable(GL_LIGHTING);
00351         glDisable(GL_DEPTH_TEST);
00352         glDisable(GL_FOG);
00353         
00354         //variables to help with hemisphere generation
00355         int lng,lat;
00356         int num_longitudes = 12;
00357         int num_lattitudes = 12;
00358         float hem_radius = 10.0f;
00359         //float safety_factor = 0.15f;
00360         float safety_factor = 0.0f;
00361         float hem_height = hem_radius;
00362         float pi = 3.141593f;
00363         
00364         int i;
00365         
00366         glPushMatrix();
00367         glMultMatrixd(temp_matrix);
00368         
00369         float colhoriz[3], colzenith[3];
00370         colhoriz[0] = .53f;
00371         colhoriz[1] = .74f;
00372         colhoriz[2] = .91f;
00373         
00374         /*colzenith[0] = 0.0f;
00375         colzenith[1] = 100.0f/255.0f;
00376         colzenith[2] = 200.0f/255.0f;*/
00377         colzenith[0] = 0.0f;
00378         colzenith[1] = 50.0f/255.0f;
00379         colzenith[2] = 100.0f/255.0f;
00380         
00381         VERTEX * oldset;
00382         VERTEX * curset;
00383         oldset = new VERTEX [num_longitudes+1];
00384         curset = new VERTEX [num_longitudes+1];
00385         VERTEX oldrot;
00386         
00387         QUATERNION qrot, qtemp;
00388         
00389         sky_texture.Activate();
00390         
00391         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00392         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
00393         
00394         glBegin(GL_QUADS);
00395 
00396         int num_repeats = 0;
00397         int old_repeats = 0;
00398         
00399         glColor4f(1.0f,1.0f,1.0f,transparency);
00400 
00401         int pass;
00402         for (pass = 0; pass < 2; pass++)
00403         {
00404                 for (lat = 0; lat <= num_lattitudes; lat++)
00405                 {
00406                         qrot.SetAxisAngle((pi/2.0f)*((float)lat/(float)num_lattitudes), 0, 0, 1);
00407                         VERTEX rotvec;
00408                         rotvec.x = hem_radius;
00409                         rotvec = qrot.RotateVec(rotvec);
00410                         
00411                         for (lng = 0; lng <= num_longitudes; lng++)
00412                         {
00413                                 VERTEX currot;
00414                                 float curcol[4];
00415                                 
00416                                 qtemp.SetAxisAngle(2.0f*pi*((float)lng/(float)num_longitudes), 0, 1, 0);
00417                                 currot = qtemp.RotateVec(rotvec);
00418                                 //cout << currot.x << "," << currot.y << "," << currot.z << endl;
00419                                 
00420                                 if (lat > 0 && lng > 0)
00421                                 {
00422                                         //glColor4f(0.53f, 0.74f, 0.91f, 1.0f);
00423                                         float rheight;
00424         
00425                                         rheight = 1.0f-currot.y/hem_height;
00426                                         //rheight = (rheight-1)*(rheight-1)*(rheight-1)+1;
00427         
00428                                         for (i = 0; i < 3; i++)
00429                                                 curcol[i] = colhoriz[i]*(1.0f-rheight)+colzenith[i]*rheight;
00430                                         //glColor4fv(curcol);
00431                                         glTexCoord2f(daytime,rheight);
00432                                         
00433                                         glVertex3f(oldrot.x, oldrot.y-safety_factor, oldrot.z);
00434                                         glVertex3f(currot.x, currot.y-safety_factor, currot.z);
00435         
00436                                         rheight = 1.0f-oldset[lng].y/hem_height;
00437                                         //rheight = (rheight-1)*(rheight-1)*(rheight-1)+1;
00438                                         
00439                                         for (i = 0; i < 3; i++)
00440                                                 curcol[i] = colhoriz[i]*(1.0f-rheight)+colzenith[i]*(rheight);
00441                                         //glColor4fv(curcol);
00442                                         glTexCoord2f(daytime,rheight);  
00443                                         glVertex3f(oldset[lng].x, oldset[lng].y-safety_factor, oldset[lng].z);
00444                                         glVertex3f(oldset[lng-1].x, oldset[lng-1].y-safety_factor, oldset[lng-1].z);
00445                                 }
00446                                 
00447                                 oldrot = currot;
00448                                 curset[lng] = currot;
00449                         }
00450                         
00451                         VERTEX * tptr = curset;
00452                         curset = oldset;
00453                         oldset = tptr;
00454                         old_repeats = num_repeats;
00455                 }
00456                 
00457                 hem_radius = -hem_radius;
00458                 hem_height = hem_radius;
00459         }
00460         
00461         delete [] curset;
00462         delete [] oldset;
00463         
00464         glEnd();
00465         glPopMatrix();
00466         glPopAttrib();
00467 }
00468 
00469 void BACKDROP::DrawSun(double daytime, float transparency, GLdouble *temp_matrix)
00470 {
00471         int i;
00472         float hem_radius = 10.0f;
00473         float hem_height = hem_radius;
00474         
00475         //okay, now plop on the SUN
00476         glPushAttrib(GL_ALL_ATTRIB_BITS);
00477         glEnable(GL_TEXTURE_2D);
00478         glEnable(GL_BLEND);
00479         glClear(GL_DEPTH_BUFFER_BIT);
00480         float sunsize = 2.0f;
00481         /*float reflection_elongation_y = 1.0f; //how much the sun is stretched by in
00482                                                                                 // the reflection rendering
00483         float reflection_elongation_x = 1.0f;*/
00484         float sunsize_x = sunsize;
00485         float sunsize_y = sunsize;
00486         
00487         VERTEX sunquad[4];
00488         for (i = 0; i < 4; i++) //generate the sun quad
00489         {
00490                 if (i == 0 || i == 3)
00491                 {
00492                         sunquad[i].y = -sunsize_y;
00493                 }
00494                 else
00495                 {
00496                         sunquad[i].y = sunsize_y; //only elongate in -y direction
00497                 }
00498                 if (i < 2)
00499                         sunquad[i].x = -sunsize_x;
00500                 else
00501                         sunquad[i].x = sunsize_x;
00502         }
00503         QUATERNION sunquat, sunquat2;
00504         //float sunheight = atan2(LightPosition[1],LightPosition[2]); //sun angular height in radians
00505         float transform_matrix[16];
00506         glPushMatrix();
00507         glMultMatrixd(temp_matrix);
00508         
00509         //float sundir = atan2(LightPosition[0],LightPosition[2]);
00510         sunquat.SetAxisAngle(sunpos_lng+3.141593/2.0, 0, 0, 1);
00511         sunquat.GetMat(transform_matrix); //crank it around the sky
00512         glMultMatrixf(transform_matrix);
00513         
00514         sunquat2.SetAxisAngle(sunpos_lat, 0, 1, 0);
00515         sunquat2.GetMat(transform_matrix); //crank it up into the sky
00516         glMultMatrixf(transform_matrix);
00517         
00518         glTranslatef(hem_height,0,0); //put the sun at an arm's length
00519 
00520         //compensate
00521         sunquat2.ReturnConjugate().GetMat(transform_matrix);
00522         glMultMatrixf(transform_matrix);
00523         
00524         //if (reflection)
00525         //      sunquat.GetMat(transform_matrix);
00526         //else  
00527                 sunquat.ReturnConjugate().GetMat(transform_matrix);
00528         glMultMatrixf(transform_matrix);
00529 
00530         cam.dir.ReturnConjugate().GetMat(transform_matrix); //set the sun's orientation to the camera's direction
00531         glMultMatrixf(transform_matrix);
00532         
00533         glColor4f(1.0f, 1.0f, 1.0f, transparency);
00534         //glBindTexture(GL_TEXTURE_2D, sun_texture_ref);
00535         sun_texture_ref.Activate();
00536         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00537         //then draw the thing
00538         glBegin(GL_QUADS);
00539         for (i = 0; i < 4; i++)
00540         {
00541                 switch(i)
00542                 {
00543                         case 0:
00544                                 glTexCoord2f(0,0);
00545                                 break;
00546                         case 1:
00547                                 glTexCoord2f(1,0);
00548                                 break;
00549                         case 2:
00550                                 glTexCoord2f(1,1);
00551                                 break;
00552                         case 3:
00553                                 glTexCoord2f(0,1);
00554                                 break;
00555                 }
00556                 
00557                 glVertex3f(sunquad[i].x, sunquad[i].y, sunquad[i].z);
00558         }
00559         glEnd();
00560         glPopMatrix();
00561         
00562         glPopAttrib();
00563 }
00564 
00565 void BACKDROP::DrawClouds(double daytime, float transparency, GLdouble *temp_matrix)
00566 {
00567         //draw the cloud layer
00568         cloudthresh = weathersystem.GetCloudCover();
00569         glPushAttrib(GL_ALL_ATTRIB_BITS);
00570         glPushMatrix();
00571         glEnable(GL_TEXTURE_2D);
00572         glEnable(GL_BLEND);
00573         
00574         glEnable(GL_FOG);
00575         float oldfog = fogdensity;
00576         SetFogStrength(fogdensity*300.0f);
00577         RefreshFog();
00578 
00579         glClear(GL_DEPTH_BUFFER_BIT);
00580         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00581         //glBlendFunc( GL_SRC_ALPHA, GL_ONE );
00582         float origalpha = transparency;
00583         float fadealpha = 1.0f;
00584         float cloudcolor = sin(sunpos_lat);
00585         float cloudscale = 20000.0f;//this should be the same as the variable
00586                                                            // of the same name in terrain.drawsub() for the
00587                                                            // the clouds to line up.
00588         //the higher the number, the higher the clouds seem.  don't set above 10.
00589         // in fact, keep it at 1.0, always.
00590         float cloudrep = 1.0f;
00591         float cloudoffset_x, cloudoffset_z;
00592         cloudoffset_x = windx;//10.0f;
00593         //0.1 is the glscale y factor
00594         cloudoffset_z = windz;//10.0f;
00595         cloudcolor += 0.4f;
00596         if (cloudcolor < 0)
00597                 cloudcolor *= 10.0f; //to simulate sun disappearing behind horizon
00598         if (cloudcolor < 0.1f)
00599                 cloudcolor = 0.1f;
00600         if (cloudcolor > 1.0f)
00601                 cloudcolor = 1.0f;
00602         glColor4f(cloudcolor, cloudcolor, cloudcolor, origalpha);
00603         glTranslatef(0,-0.4f,0);
00604         
00605         /*temp_radius = sqrt(cam.position.x*cam.position.x+cam.position.z*cam.position.z+(cam.position.y+EARTH_RADIUS)*(cam.position.y+EARTH_RADIUS));
00606         temp_long = atan2((double)(cam.position.y+EARTH_RADIUS),(double)cam.position.x);
00607         temp_lat = acos(cam.position.z/temp_radius);
00608         temp_lat -= 1.570796f;
00609         temp_long -= 1.570796f;
00610         qz.SetAxisAngle(-temp_long, 0, 0, 1);
00611         qx.SetAxisAngle(temp_lat, 1, 0, 0);
00612         qt = qx*qz;
00613         qt.GetMat(temp_matrix);*/
00614         glMultMatrixd(temp_matrix);
00615         
00616         glScalef(1.0,0.1f,1.0f);
00617         //glTranslatef(0,-15.0f,0);
00618         //glScalef(10.0,10.0f,10.0f);
00619         int num_repeats = 0;
00620         int old_repeats = 0;
00621         int num_longitudes = 12;
00622         int num_lattitudes = 12;
00623         //glBindTexture(GL_TEXTURE_2D, cloud_texture);
00624         cloud_texture.Activate();
00625         
00626         VERTEX * oldset = new VERTEX [num_longitudes+1];
00627         VERTEX * curset = new VERTEX [num_longitudes+1];
00628         float hem_height = 10.0f;
00629         float hem_radius = 10.0f;
00630         int pass, lat, lng;
00631         QUATERNION qrot, qtemp;
00632         VERTEX oldrot;
00633         float safety_factor = 0.0f;
00634         float pi = 3.141593f;
00635         glBegin(GL_QUADS);
00636         for (pass = 0; pass < 2; pass++)
00637         {
00638                 for (lat = 0; lat <= num_lattitudes; lat++)
00639                 {
00640                         if (pass == 0)
00641                                 qrot.SetAxisAngle((pi/2.0f)*((float)lat/(float)num_lattitudes), 0, 1, 0);
00642                         else
00643                                 qrot.SetAxisAngle((pi/2.0f)*((float)lat/(float)num_lattitudes), 0, -1, 0);
00644                         VERTEX rotvec;
00645                         rotvec.z = hem_radius;
00646                         rotvec = qrot.RotateVec(rotvec);
00647                         
00648                         for (lng = 0; lng <= num_longitudes; lng++)
00649                         {
00650                                 VERTEX currot;
00651                                 float curcol[4];
00652                                 
00653                                 curcol[4] = 1.0f;
00654                                 
00655                                 //if (pass == 0)
00656                                         qtemp.SetAxisAngle(2.0f*pi*((float)lng/(float)num_longitudes), 1, 0, 0);
00657                                 //else
00658                                 //      qtemp.SetAxisAngle(2.0f*pi*((float)lng/(float)num_longitudes), 0, 0, -1);
00659                                 currot = qtemp.RotateVec(rotvec);
00660 
00661                                 if (lat > 0 && lng > 0)
00662                                 {
00663                                         float rheight;
00664 
00665                                         float vtile = 1.0f;
00666                                         //float utile = 4.0f;
00667                                         
00668                                         float texu, texv, cx, cv;
00669                                         
00670                                         rheight = 1.0f-vtile*currot.x/hem_height;
00671                                         
00672                                         float fadeterm = 25.0f;
00673                                         float alphafactor;
00674                                         alphafactor = oldrot.x*oldrot.x+oldrot.z*oldrot.z;
00675                                         alphafactor /= (hem_radius*hem_radius-fadeterm);
00676                                         if (alphafactor > 1)
00677                                                 alphafactor = 1.0f;
00678                                         alphafactor = 1.0f - alphafactor;
00679                                         glColor4f(cloudcolor, cloudcolor, cloudcolor, 
00680                                                 alphafactor*origalpha+(1.0f-alphafactor)*fadealpha);
00681                                         
00682                                         //texu = utile*(lng-1)/(float)num_longitudes+cloudoffset_x;
00683                                         //texv = rheight+cloudoffset_z;
00684                                         cx = oldrot.x;
00685                                         cv = oldrot.z;
00686                                         texu = (cx/hem_height+cloudoffset_x)*cloudrep+(-cam.position.x/cloudscale)/cloudrep;
00687                                         texv = (cv/hem_height+cloudoffset_z)*cloudrep+(-cam.position.z/cloudscale)/cloudrep;
00688                                         glTexCoord2f(texu, texv);
00689                                         glVertex3f(oldrot.x, oldrot.y-safety_factor, oldrot.z);
00690                                         
00691                                         alphafactor = currot.x*currot.x+currot.z*currot.z;
00692                                         alphafactor /= (hem_radius*hem_radius-fadeterm);
00693                                         if (alphafactor > 1)
00694                                                 alphafactor = 1.0f;
00695                                         alphafactor = 1.0f - alphafactor;
00696                                         glColor4f(cloudcolor, cloudcolor, cloudcolor, 
00697                                                 alphafactor*origalpha+(1.0f-alphafactor)*fadealpha);
00698                                         
00699                                         //texu = utile*(lng)/(float)num_longitudes+cloudoffset_x;
00700                                         //texv = rheight+cloudoffset_z;
00701                                         cx = currot.x;
00702                                         cv = currot.z;
00703                                         texu = (cx/hem_height+cloudoffset_x)*cloudrep+(-cam.position.x/cloudscale)/cloudrep;
00704                                         texv = (cv/hem_height+cloudoffset_z)*cloudrep+(-cam.position.z/cloudscale)/cloudrep;
00705                                         glTexCoord2f(texu, texv);
00706                                         glVertex3f(currot.x, currot.y-safety_factor, currot.z);
00707         
00708                                         rheight = 1.0f-vtile*oldset[lng].x/hem_height;
00709 
00710                                         alphafactor = oldset[lng].x*oldset[lng].x+oldset[lng].z*oldset[lng].z;
00711                                         alphafactor /= (hem_radius*hem_radius-fadeterm);
00712                                         if (alphafactor > 1)
00713                                                 alphafactor = 1.0f;
00714                                         alphafactor = 1.0f - alphafactor;
00715                                         glColor4f(cloudcolor, cloudcolor, cloudcolor, 
00716                                                 alphafactor*origalpha+(1.0f-alphafactor)*fadealpha);
00717                                         
00718                                         //texu = utile*(lng)/(float)num_longitudes+cloudoffset_x;
00719                                         //texv = rheight+cloudoffset_z;
00720                                         cx = oldset[lng].x;
00721                                         cv = oldset[lng].z;
00722                                         texu = (cx/hem_height+cloudoffset_x)*cloudrep+(-cam.position.x/cloudscale)/cloudrep;
00723                                         texv = (cv/hem_height+cloudoffset_z)*cloudrep+(-cam.position.z/cloudscale)/cloudrep;
00724                                         glTexCoord2f(texu, texv);
00725                                         glVertex3f(oldset[lng].x, oldset[lng].y-safety_factor, oldset[lng].z);
00726                                         
00727                                         alphafactor = oldset[lng-1].x*oldset[lng-1].x+oldset[lng-1].z*oldset[lng-1].z;
00728                                         alphafactor /= (hem_radius*hem_radius-fadeterm);
00729                                         if (alphafactor > 1)
00730                                                 alphafactor = 1.0f;
00731                                         alphafactor = 1.0f - alphafactor;
00732                                         glColor4f(cloudcolor, cloudcolor, cloudcolor, 
00733                                                 alphafactor*origalpha+(1.0f-alphafactor)*fadealpha);
00734                                         
00735                                         //texu = utile*(lng-1)/(float)num_longitudes+cloudoffset_x;
00736                                         //texv = rheight+cloudoffset_z;
00737                                         cx = oldset[lng-1].x;
00738                                         cv = oldset[lng-1].z;
00739                                         texu = (cx/hem_height+cloudoffset_x)*cloudrep+(-cam.position.x/cloudscale)/cloudrep;
00740                                         texv = (cv/hem_height+cloudoffset_z)*cloudrep+(-cam.position.z/cloudscale)/cloudrep;
00741                                         glTexCoord2f(texu, texv);
00742                                         glVertex3f(oldset[lng-1].x, oldset[lng-1].y-safety_factor, oldset[lng-1].z);
00743                                 }
00744                                 
00745                                 oldrot = currot;
00746                                 curset[lng] = currot;
00747                         }
00748                         
00749                         VERTEX * tptr = curset;
00750                         curset = oldset;
00751                         oldset = tptr;
00752                         old_repeats = num_repeats;
00753                 }
00754 
00755                 //hem_height = -hem_height;
00756                 //hem_radius = -hem_radius;
00757         }
00758         delete [] curset;
00759         delete [] oldset;
00760         glEnd();
00761         
00762         /*GLUquadricObj *quadratic;
00763         quadratic=gluNewQuadric();                      // Create A Pointer To The Quadric Object ( NEW )
00764         gluQuadricNormals(quadratic, GLU_SMOOTH);       // Create Smooth Normals ( NEW )
00765         gluQuadricTexture(quadratic, GL_TRUE);          // Create Texture Coords ( NEW )
00766         gluSphere(quadratic,10.0f,32,32);
00767         gluDeleteQuadric(quadratic);*/
00768         
00769         SetFogStrength(oldfog);
00770         RefreshFog();
00771         glPopAttrib();
00772         glPopMatrix();
00773 }
00774 
00775 void BACKDROP::DrawStars(double daytime, float transparency, GLdouble *temp_matrix)
00776 {
00777         //draw the star layer
00778         glPushAttrib(GL_ALL_ATTRIB_BITS);
00779         glPushMatrix();
00780         glEnable(GL_TEXTURE_2D);
00781         glEnable(GL_BLEND);
00782         
00783         glClear(GL_DEPTH_BUFFER_BIT);
00784         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00785         //glBlendFunc( GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR );
00786         //glBlendFunc( GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA );
00787         //glBlendFunc( GL_SRC_ALPHA, GL_ONE );
00788         //float cloudscale = 20000.0f;//this should be the same as the variable
00789                                                            // of the same name in terrain.drawsub() for the
00790                                                            // the clouds to line up.
00791         //the higher the number, the higher the clouds seem.  don't set above 10.
00792         // in fact, keep it at 1.0, always.
00793         //float cloudrep = 40.0f;
00794         //float cloudtile = 10.0f;
00795         //cloudtile = 2.0f;
00796         //cloudscale = 1000.0f;
00797         float fogcolor[4];
00798         GetSkyColor(fogcolor);
00799         glColor4f(1.0,1.0,1.0,1.0-fogcolor[3]);
00800         glTranslatef(0,-0.4f,0);
00801         
00802         /*temp_radius = sqrt(cam.position.x*cam.position.x+cam.position.z*cam.position.z+(cam.position.y+EARTH_RADIUS)*(cam.position.y+EARTH_RADIUS));
00803         temp_long = atan2((double)(cam.position.y+EARTH_RADIUS),(double)cam.position.x);
00804         temp_lat = acos(cam.position.z/temp_radius);
00805         temp_lat -= 1.570796f;
00806         temp_long -= 1.570796f;
00807         qz.SetAxisAngle(-temp_long, 0, 0, 1);
00808         qx.SetAxisAngle(temp_lat, 1, 0, 0);
00809         qt = qx*qz;
00810         qt.GetMat(temp_matrix);*/
00811         glMultMatrixd(temp_matrix);
00812         
00813         //glScalef(1.0,0.1f,1.0f);
00814         glScalef(1.0,1.0f,1.0f);
00815         
00816         //glBindTexture(GL_TEXTURE_2D, stars_texture);
00817         stars_texture.Activate();
00818         glMatrixMode(GL_TEXTURE);
00819         glLoadIdentity();
00820         glScalef(10.0f,10.0f,10.0f);
00821         
00822         glMatrixMode(GL_MODELVIEW);
00823         glRotatef(90,1,0,0);
00824         GLUquadricObj *quadratic;
00825         quadratic=gluNewQuadric();
00826         gluQuadricTexture(quadratic, GL_TRUE);
00827         gluSphere(quadratic,1.0f,32,32);
00828         gluDeleteQuadric(quadratic);
00829         
00830         glMatrixMode(GL_TEXTURE);
00831         glLoadIdentity();
00832         glMatrixMode(GL_MODELVIEW);
00833         
00834         //glTranslatef(0,-15.0f,0);
00835         //glScalef(10.0,10.0f,10.0f);
00836         /*int num_repeats = 0;
00837         int old_repeats = 0;
00838         int num_longitudes = 12;
00839         int num_lattitudes = 12;
00840         glBindTexture(GL_TEXTURE_2D, stars_texture);
00841         float cloudoffset_x, cloudoffset_z;
00842         cloudoffset_x = cloudoffset_z = 0.0f;
00843         VERTEX * oldset = new VERTEX [num_longitudes+1];
00844         VERTEX * curset = new VERTEX [num_longitudes+1];
00845         float hem_height = 10.0f;
00846         float hem_radius = 10.0f;
00847         int pass, lat, lng;
00848         QUATERNION qrot, qtemp;
00849         VERTEX oldrot;
00850         float safety_factor = 0.0f;
00851         float pi = 3.141593f;
00852         glBegin(GL_QUADS);
00853         for (pass = 0; pass < 2; pass++)
00854         {
00855                 for (lat = 0; lat <= num_lattitudes; lat++)
00856                 {
00857                         if (pass == 0)
00858                                 qrot.SetAxisAngle((pi/2.0f)*((float)lat/(float)num_lattitudes), 0, 1, 0);
00859                         else
00860                                 qrot.SetAxisAngle((pi/2.0f)*((float)lat/(float)num_lattitudes), 0, -1, 0);
00861                         VERTEX rotvec;
00862                         rotvec.z = hem_radius;
00863                         rotvec = qrot.RotateVec(rotvec);
00864                         
00865                         for (lng = 0; lng <= num_longitudes; lng++)
00866                         {
00867                                 VERTEX currot;
00868                                 float curcol[4];
00869                                 
00870                                 curcol[4] = 1.0f;
00871                                 
00872                                 //if (pass == 0)
00873                                         qtemp.SetAxisAngle(2.0f*pi*((float)lng/(float)num_longitudes), 1, 0, 0);
00874                                 //else
00875                                 //      qtemp.SetAxisAngle(2.0f*pi*((float)lng/(float)num_longitudes), 0, 0, -1);
00876                                 currot = qtemp.RotateVec(rotvec);
00877 
00878                                 if (lat > 0 && lng > 0)
00879                                 {
00880                                         float rheight;
00881 
00882                                         float vtile = 1.0f;
00883                                         //float vtile = 0.5f;
00884                                         //float utile = 4.0f;
00885                                         
00886                                         float texu, texv, cx, cv;
00887                                         
00888                                         rheight = 1.0f-vtile*currot.x/hem_height;
00889                                         
00890                                         float fadeterm = 25.0f;
00891                                         float alphafactor;
00892                                         alphafactor = oldrot.x*oldrot.x+oldrot.z*oldrot.z;
00893                                         alphafactor /= (hem_radius*hem_radius-fadeterm);
00894                                         if (alphafactor > 1)
00895                                                 alphafactor = 1.0f;
00896                                         alphafactor = 1.0f - alphafactor;
00897                                         //glColor4f(cloudcolor, cloudcolor, cloudcolor, 
00898                                                 //alphafactor*origalpha+(1.0f-alphafactor)*fadealpha);
00899                                         
00900                                         //texu = utile*(lng-1)/(float)num_longitudes+cloudoffset_x;
00901                                         //texv = rheight+cloudoffset_z;
00902                                         cx = oldrot.x;
00903                                         cv = oldrot.z;
00904                                         texu = (cx/hem_height+cloudoffset_x)*cloudtile+(-cam.position.x/cloudscale)/cloudtile;
00905                                         texv = (cv/hem_height+cloudoffset_z)*cloudtile+(-cam.position.z/cloudscale)/cloudtile;
00906                                         glTexCoord2f(texu, texv);
00907                                         glVertex3f(oldrot.x, oldrot.y-safety_factor, oldrot.z);
00908                                         
00909                                         alphafactor = currot.x*currot.x+currot.z*currot.z;
00910                                         alphafactor /= (hem_radius*hem_radius-fadeterm);
00911                                         if (alphafactor > 1)
00912                                                 alphafactor = 1.0f;
00913                                         alphafactor = 1.0f - alphafactor;
00914                                         //glColor4f(cloudcolor, cloudcolor, cloudcolor, 
00915                                                 //alphafactor*origalpha+(1.0f-alphafactor)*fadealpha);
00916                                         
00917                                         //texu = utile*(lng)/(float)num_longitudes+cloudoffset_x;
00918                                         //texv = rheight+cloudoffset_z;
00919                                         cx = currot.x;
00920                                         cv = currot.z;
00921                                         texu = (cx/hem_height+cloudoffset_x)*cloudtile+(-cam.position.x/cloudscale)/cloudtile;
00922                                         texv = (cv/hem_height+cloudoffset_z)*cloudtile+(-cam.position.z/cloudscale)/cloudtile;
00923                                         glTexCoord2f(texu, texv);
00924                                         glVertex3f(currot.x, currot.y-safety_factor, currot.z);
00925         
00926                                         rheight = 1.0f-vtile*oldset[lng].x/hem_height;
00927 
00928                                         alphafactor = oldset[lng].x*oldset[lng].x+oldset[lng].z*oldset[lng].z;
00929                                         alphafactor /= (hem_radius*hem_radius-fadeterm);
00930                                         if (alphafactor > 1)
00931                                                 alphafactor = 1.0f;
00932                                         alphafactor = 1.0f - alphafactor;
00933                                         //glColor4f(cloudcolor, cloudcolor, cloudcolor, 
00934                                                 //alphafactor*origalpha+(1.0f-alphafactor)*fadealpha);
00935                                         
00936                                         //texu = utile*(lng)/(float)num_longitudes+cloudoffset_x;
00937                                         //texv = rheight+cloudoffset_z;
00938                                         cx = oldset[lng].x;
00939                                         cv = oldset[lng].z;
00940                                         texu = (cx/hem_height+cloudoffset_x)*cloudtile+(-cam.position.x/cloudscale)/cloudtile;
00941                                         texv = (cv/hem_height+cloudoffset_z)*cloudtile+(-cam.position.z/cloudscale)/cloudtile;
00942                                         glTexCoord2f(texu, texv);
00943                                         glVertex3f(oldset[lng].x, oldset[lng].y-safety_factor, oldset[lng].z);
00944                                         
00945                                         alphafactor = oldset[lng-1].x*oldset[lng-1].x+oldset[lng-1].z*oldset[lng-1].z;
00946                                         alphafactor /= (hem_radius*hem_radius-fadeterm);
00947                                         if (alphafactor > 1)
00948                                                 alphafactor = 1.0f;
00949                                         alphafactor = 1.0f - alphafactor;
00950                                         //glColor4f(cloudcolor, cloudcolor, cloudcolor, 
00951                                                 //alphafactor*origalpha+(1.0f-alphafactor)*fadealpha);
00952                                         
00953                                         //texu = utile*(lng-1)/(float)num_longitudes+cloudoffset_x;
00954                                         //texv = rheight+cloudoffset_z;
00955                                         cx = oldset[lng-1].x;
00956                                         cv = oldset[lng-1].z;
00957                                         texu = (cx/hem_height+cloudoffset_x)*cloudtile+(-cam.position.x/cloudscale)/cloudtile;
00958                                         texv = (cv/hem_height+cloudoffset_z)*cloudtile+(-cam.position.z/cloudscale)/cloudtile;
00959                                         glTexCoord2f(texu, texv);
00960                                         glVertex3f(oldset[lng-1].x, oldset[lng-1].y-safety_factor, oldset[lng-1].z);
00961                                 }
00962                                 
00963                                 oldrot = currot;
00964                                 curset[lng] = currot;
00965                         }
00966                         
00967                         VERTEX * tptr = curset;
00968                         curset = oldset;
00969                         oldset = tptr;
00970                         old_repeats = num_repeats;
00971                 }
00972 
00973                 //hem_height = -hem_height;
00974                 //hem_radius = -hem_radius;
00975         }
00976         delete [] curset;
00977         delete [] oldset;
00978         glEnd();*/
00979         
00980         /*GLUquadricObj *quadratic;
00981         quadratic=gluNewQuadric();                      // Create A Pointer To The Quadric Object ( NEW )
00982         gluQuadricNormals(quadratic, GLU_SMOOTH);       // Create Smooth Normals ( NEW )
00983         gluQuadricTexture(quadratic, GL_TRUE);          // Create Texture Coords ( NEW )
00984         gluSphere(quadratic,10.0f,32,32);
00985         gluDeleteQuadric(quadratic);*/
00986         
00987         glPopAttrib();
00988         glPopMatrix();
00989 }
00990 
00991 void BACKDROP::RefreshFog()
00992 {
00993         //set up fog
00994         float fogcolor[4];
00995         GetHorizonColor(fogcolor);
00996         fogcolor[3] = 0.0f;
00997         
00998         //desaturate fog based on cloud cover
00999         float colmean = (fogcolor[0] + fogcolor[1] + fogcolor[2])/3.0f;
01000         float cloudiness;
01001         if (cloudthresh >= 0)
01002                 cloudiness = 0.0f;
01003         else
01004                 cloudiness = -(cloudthresh/100.0f); //decrease 50.0f to make the fog
01005                                                                                         //desaturate faster
01006         
01007         //clamp
01008         if (cloudiness > 1.0f)
01009                 cloudiness = 1.0f;
01010         if (cloudiness < 0.0f)
01011                 cloudiness = 0.0f;
01012         
01013         int i;
01014         for (i = 0; i < 3; i++)
01015         {
01016                 fogcolor[i] = fogcolor[i]*(1.0f-cloudiness) + colmean*cloudiness;
01017         }
01018         
01019         glFogfv(GL_FOG_COLOR, fogcolor);
01020         glFogf(GL_FOG_DENSITY, fogdensity);
01021         glFogi(GL_FOG_MODE, GL_EXP);
01022         glEnable(GL_FOG);
01023 }
01024 
01025 struct pix24
01026 {
01027         unsigned char r;
01028         unsigned char g;
01029         unsigned char b;
01030 };
01031 
01032 struct pix32
01033 {
01034         unsigned char r;
01035         unsigned char g;
01036         unsigned char b;
01037         unsigned char a;
01038 };
01039 
01040 void BACKDROP::GetHorizonColor(float * color3)
01041 {
01042         /*color3[0] = 0.53f;
01043         color3[1] = 0.74f;
01044         color3[2] = 0.91f;*/
01045         
01046         struct pix32 * pix;
01047                 
01048         pix = (struct pix32 *) sky_tex_img->pixels;
01049         
01050         float dx;
01051         int x;
01052         int sky_tex_size = 16;
01053         dx = (daytime_cache*sky_tex_size);
01054         x = (int)dx;
01055         dx = dx - x;
01056         x %= sky_tex_size;
01057         int idx1 = x + 15*sky_tex_size;
01058         x++;
01059         x %= sky_tex_size;
01060         int idx2 = x + 15*sky_tex_size;
01061         
01062         color3[0] = (1.0f-dx)*pix[idx1].r/255.0f+dx*pix[idx2].r/255.0f;
01063         color3[1] = (1.0f-dx)*pix[idx1].g/255.0f+dx*pix[idx2].g/255.0f;
01064         color3[2] = (1.0f-dx)*pix[idx1].b/255.0f+dx*pix[idx2].b/255.0f;
01065         color3[3] = (1.0f-dx)*pix[idx1].a/255.0f+dx*pix[idx2].a/255.0f;
01066 }
01067 
01068 void BACKDROP::GetSkyColor(float * color3)
01069 {
01070         /*color3[0] = 0.53f;
01071         color3[1] = 0.74f;
01072         color3[2] = 0.91f;*/
01073         
01074         struct pix32 * pix;
01075         
01076         pix = (struct pix32 *) sky_tex_img->pixels;
01077         
01078         float dx;
01079         int x;
01080         int sky_tex_size = 16;
01081         dx = (daytime_cache*sky_tex_size);
01082         x = (int)dx;
01083         dx = dx - x;
01084         x %= sky_tex_size;
01085         //int idx1 = x + 15*sky_tex_size;
01086         int idx1 = x;
01087         x++;
01088         x %= sky_tex_size;
01089         //int idx2 = x + 15*sky_tex_size;
01090         int idx2 = x;
01091         
01092         color3[0] = (1.0f-dx)*pix[idx1].r/255.0f+dx*pix[idx2].r/255.0f;
01093         color3[1] = (1.0f-dx)*pix[idx1].g/255.0f+dx*pix[idx2].g/255.0f;
01094         color3[2] = (1.0f-dx)*pix[idx1].b/255.0f+dx*pix[idx2].b/255.0f;
01095         color3[3] = (1.0f-dx)*pix[idx1].a/255.0f+dx*pix[idx2].a/255.0f;
01096 }
01097 
01098 void BACKDROP::SetFogStrength(float density)
01099 {
01100         fogdensity = density;
01101         glFogf(GL_FOG_DENSITY, fogdensity);
01102 }
01103 
01104 void BACKDROP::SetWindoffset(float windoffset_x, float windoffset_z)
01105 {
01106         windx = windoffset_x;
01107         windz = windoffset_z;
01108 }
01109 
01110 void BACKDROP::SetCloudCover(int newcloudthresh)
01111 {
01112         cloudthresh = newcloudthresh;
01113 }
01114 
01115 void BACKDROP::DrawRain(float day_time)
01116 {
01117         glPushMatrix();
01118         glLoadIdentity();
01119         glPushAttrib(GL_ALL_ATTRIB_BITS);
01120         glDisable(GL_DEPTH_TEST);
01121         glEnable(GL_TEXTURE_2D);
01122         glEnable(GL_BLEND);
01123         glDisable(GL_FOG);
01124         glDisable(GL_LIGHTING);
01125         glTranslatef(0,0,-40.0f);
01126         //glBlendFunc(GL_ONE, GL_SRC_ALPHA)
01127         //glBindTexture(GL_TEXTURE_2D, rain_texture);
01128         rain_texture.Activate();
01129         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
01130         glColor4f(1.0f,1.0f,1.0f,1.0f);
01131         glBegin(GL_TRIANGLES);
01132         
01133         VERTEX pos;
01134         float alpha;
01135         int numdrops = weathersystem.GetRainDrops();
01136         int i;
01137         
01138         VERTEX up, forward, right;
01139         up.y = 1.0f;
01140         up = cam.dir.RotateVec(up);
01141         up.y = sin(up.y);
01142         forward.z = 1.0f;
01143         forward = cam.dir.RotateVec(forward);
01144         if ((forward.y >= 0 && forward.z >= 0) || (forward.y < 0 && forward.z < 0))
01145                 forward.y = 1.0f;
01146         else
01147                 forward.y = -1.0f;
01148         
01149         float randperm;
01150         float lum;
01151         
01152         float dayout = day_time;
01153         float daythresh = 0.1f;
01154         if (dayout >= 0.5)
01155                 dayout = 0.0f;
01156         if (dayout < 0.5f && dayout > 0.5f - daythresh)
01157                 dayout = 0.5-dayout;
01158         dayout /= daythresh;
01159         if (dayout > 1.0f)
01160                 dayout = 1.0f;
01161         dayout = dayout*0.75f+0.25f;
01162         
01163         for (i = 0; i < numdrops; i++)
01164         {
01165                 pos.x = (rand()/(float)RAND_MAX-0.5f)*50.0f;
01166                 pos.y = (rand()/(float)RAND_MAX-0.5f)*40.0f;
01167                 pos.z = (rand()/(float)RAND_MAX);
01168                 alpha = (rand()/(float)RAND_MAX)/4.0f + 0.2f;
01169                 lum = (rand()/(float)RAND_MAX)/2.0f + 0.5f;
01170                 lum *= dayout;
01171                 glColor4f(lum,lum,lum,alpha);
01172                 randperm = (rand()/(float)RAND_MAX-0.5f)*1.0f;
01173                 DrawDrop(pos, randperm, up.y, forward.y);
01174         }
01175         
01176         glEnd();
01177         glPopAttrib();
01178         glPopMatrix();
01179 }
01180 
01181 void BACKDROP::DrawDrop(VERTEX pos, float rndperm, float yscale, float zheight)
01182 {
01183         float dropwidth = 0.25f;
01184         bool negy;
01185         if (zheight >= 0)
01186                 negy = false;
01187         else
01188                 negy = true;
01189         
01190         float dropheight = 10.0f*pos.z*yscale;
01191         if (dropheight < 0.5f)
01192                 dropheight = 0.5f;
01193         float xoffset;
01194         if (negy)
01195                 xoffset = -(1.0f-yscale)*0.2f*pos.x*pos.z;
01196         else
01197                 xoffset = (1.0f-yscale)*0.2f*pos.x*pos.z;
01198         
01199         glTexCoord2f(0,0);
01200         glVertex3f(pos.x-dropwidth, pos.y+dropheight, 0.0f);
01201         glTexCoord2f(0,1);
01202         glVertex3f(pos.x-dropwidth+rndperm+xoffset, pos.y-dropheight, 0.0f);
01203         glTexCoord2f(1,1);
01204         glVertex3f(pos.x+dropwidth+rndperm+xoffset, pos.y-dropheight, 0.0f);
01205 }
01206 
01207 void BACKDROP::DrawMoon(double daytime, float transparency, GLdouble *temp_matrix)
01208 {
01209         int i;
01210         float hem_radius = 10.0f;
01211         float hem_height = hem_radius;
01212         
01213         //okay, now plop on the MOON
01214         glPushAttrib(GL_ALL_ATTRIB_BITS);
01215         glEnable(GL_TEXTURE_2D);
01216         //glEna