Textures.cpp

00001 #include "Textures.h"
00002 
00003 GLuint Textures::tex_id[MAX_TEXTURES];
00004 
00005 void Textures::Load ()
00006 {
00007         PerlinNoise * perlin;
00008 
00009         glGenTextures(MAX_TEXTURES, tex_id);
00010 
00011         printf ("loading textures...\n");
00012 
00013         // first, load our only bitmap texture from disk
00014         if (!LoadBMP ("Textures\\Space4.bmp", tex_id[T_SPACE]))
00015         {printf ("Failed to load texture...\n");}
00016 
00017         printf ("generating textures...\n");
00018 
00019         // generate perlin noise textures
00020         perlin = new PerlinNoise (512, 512);
00021         perlin->SetBackgroundColor (1.0f, 0.5f, 0.5f, 1.0f);
00022         perlin->SetForegroundColor (0.0f, 0.1f, 0.8f, 1.0f);
00023         perlin->SetParameters (16.0, 0.5, 8, false);
00024         perlin->GenerateNoise ();
00025         perlin->TransformAB (2.0, 0.5, PerlinNoise::LinearTransform);
00026         perlin->FlushToColorBuffer ();
00027         CreatePerlinTexture (perlin, tex_id[T_PERLIN]);
00028         perlin->Push ();
00029                 perlin->SetBackgroundColor (1.0f, 0.5f, 0.5f, 1.0f);
00030                 perlin->SetForegroundColor (0.5f, 0.1f, 0.0f, 1.0f);
00031                 perlin->SetParameters (4.0, 0.5, 8, false);
00032                 perlin->GenerateNoise ();
00033                 perlin->TransformAB (2.0, 0.5, PerlinNoise::LinearTransform);
00034                 perlin->FlushToColorBuffer ();
00035                 CreatePerlinTexture (perlin, tex_id[T_PERLIN2]);
00036                 perlin->CombineColorBuffersAB (0.5, 0.5, PerlinNoise::WeightedSumCombiner);
00037         CreatePerlinTexture (perlin, tex_id[T_PERLIN3]);
00038 
00039         perlin->SetBackgroundColor (0.3f, 0.3f, 1.0f, 1.0f);
00040         perlin->SetForegroundColor (1.0f, 1.0f, 1.5f, 1.0f);
00041         perlin->SetParameters (20.0, 0.65, 8, false);
00042         perlin->GenerateNoise ();
00043         perlin->TransformAB (0.0f, 1.0f, PerlinNoise::RangeTransform);
00044         perlin->SmoothBorder (64);
00045         perlin->FlushToColorBuffer ();
00046         CreatePerlinTexture (perlin, tex_id[T_SKY]);
00047 
00048         delete perlin;
00049 }
00050 
00051 void Textures::Release ()
00052 {
00053         glDeleteTextures (MAX_TEXTURES, tex_id);
00054 }
00055 
00056 GLuint Textures::GetId (int texture_id)
00057 {
00058         return tex_id[texture_id];
00059 }
00060 
00061 bool Textures::LoadBMP (char * file_name, GLuint texture_id)
00062 {
00063         FILE * file;
00064         BITMAPFILEHEADER header;
00065         BITMAPINFOHEADER info;
00066         GLubyte * raw_data;
00067         GLubyte BGR_swap;
00068 
00069         raw_data = NULL;
00070         file = NULL;
00071 
00072         file = fopen (file_name, "rb");
00073 
00074         if (file == NULL)
00075         {return NULL;}
00076 
00077         fread(&header, sizeof(BITMAPFILEHEADER), 1, file);
00078 
00079         if (header.bfType != 0x4D42)    // flag must have value 19778   
00080         {
00081                 fclose(file);
00082                 return false;
00083         }
00084 
00085         fread(&info, sizeof(BITMAPINFOHEADER), 1, file);
00086 
00087         if (info.biBitCount != 24)
00088         {
00089                 printf ("Can only load bitmpas with 24-bit color depth\n");
00090                 fclose (file);
00091                 return false;
00092         }
00093 
00094         fseek(file, header.bfOffBits, SEEK_SET);
00095 
00096         info.biSizeImage = info.biWidth * info.biHeight * info.biBitCount / 8;
00097 
00098         raw_data = new GLubyte[info.biSizeImage];
00099 
00100         fread(raw_data, info.biSizeImage, 1, file);
00101 
00102         if (!raw_data)
00103         {
00104                 fclose (file);
00105                 return false;
00106         }
00107 
00108         for (unsigned int i = 0; i < info.biSizeImage; i+=3)
00109         {
00110                 BGR_swap = raw_data[i];
00111                 raw_data[i] = raw_data[i+2];
00112                 raw_data[i+2] = BGR_swap;
00113         }
00114 
00115         // create texture now
00116         CreateTexture (texture_id, info.biWidth, info.biHeight, raw_data);
00117 
00118         // clean up ressources
00119         fclose (file);
00120         delete raw_data;
00121 
00122         return true;
00123 }
00124 
00125 void Textures::CreatePerlinTexture (PerlinNoise * perlin, GLuint texture_id)
00126 {
00127         GLubyte * raw_data;
00128         int size_x, size_y;
00129 
00130         perlin->GetDimensions (size_x, size_y);
00131         raw_data = new GLubyte [size_x*size_y*3];
00132         perlin->ExportRGB256 (raw_data);
00133         CreateTexture (texture_id, size_x, size_y, raw_data);
00134         
00135         delete raw_data;
00136 }
00137 
00138 void Textures::CreateTexture (GLuint texture_id, int size_x, int size_y, GLubyte * raw_data)
00139 {
00140         glBindTexture(GL_TEXTURE_2D, texture_id);
00141         gluBuild2DMipmaps (GL_TEXTURE_2D, 3, size_x, size_y, GL_RGB, GL_UNSIGNED_BYTE, raw_data);
00142         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00143         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00144 }

Generated on Sun Jul 2 13:20:39 2006 for Demo by  doxygen 1.4.6-NO