E:/Eigene Dateien/Eigene Projekte/c0re/TextureInterface.cpp

Go to the documentation of this file.
00001 /*
00002         This file is part of c0re.
00003 
00004         c0re is a multiplayer RTS on a hexagonal map with an evolving unit concept.
00005     Copyright (C) 2007 Stephan Hofmann
00006 
00007     c0re is free software: you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation, either version 3 of the License, or
00010     (at your option) any later version.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 #include "TextureInterface.h"
00022 
00023 #include "system.h"
00024 #include <GL/gl.h>
00025 #include <GL/glu.h>
00026 #include <IL/il.h>
00027 
00028 int TextureInterface::s_handlerIndex = 1;
00029 
00030 TextureInterface::TextureInterface()
00031 {
00032                 ilInit();
00033 }
00034 
00035 TextureInterface::~TextureInterface()
00036 {
00037 }
00038 
00039 TextureData* TextureInterface::loadBMP(std::string p_filename)
00040 {
00041         TextureData* ret = 0;
00042         int i;
00043 
00044         int j=0; //Index variables
00045         FILE *l_file; //File pointer
00046 
00047         if( fopen_s(&l_file, p_filename.c_str(), "rb") == 0) 
00048         {  
00049                 ret = new TextureData;
00050 
00051                 fread(&(ret->fileheader), sizeof(ret->fileheader), 1, l_file); // Read the fileheader
00052                 
00053                 fseek(l_file, sizeof(ret->fileheader), SEEK_SET); // Jump the fileheader
00054                 fread(&(ret->infoheader), sizeof(ret->infoheader), 1, l_file); // and read the infoheader
00055 
00056                 // Now we need to allocate the memory for our image (width * height * color deep)
00057                 ret->l_texture = (unsigned char *) malloc(ret->infoheader.biWidth * ret->infoheader.biHeight * 4);
00058                 // And fill it with zeros
00059                 memset(ret->l_texture, 0, ret->infoheader.biWidth * ret->infoheader.biHeight * 4);
00060                 
00061                 // At this point we can read every pixel of the image
00062                 for (i=0; i < (ret->infoheader.biWidth) * (ret->infoheader.biHeight); i++)
00063                 {            
00064                                 // We load an RGB value from the file
00065                                 fread(&(ret->rgb), sizeof(ret->rgb), 1, l_file); 
00066 
00067                                 // And store it
00068                                 ret->l_texture[j+0] = ret->rgb.rgbtRed; // Red component
00069                                 ret->l_texture[j+1] = ret->rgb.rgbtGreen; // Green component
00070                                 ret->l_texture[j+2] = ret->rgb.rgbtBlue; // Blue component
00071                                 ret->l_texture[j+3] = 255; // Alpha value
00072                                 j += 4; // Go to the next position
00073                 }
00074 
00075                 fclose(l_file); // Closes the file stream
00076         }
00077 
00078         return ret;
00079 }
00080 
00081 int TextureInterface::bind(TextureData* data)
00082 {
00083         int ret = 0;
00084 
00085         s_handlerIndex++;
00086         glBindTexture(GL_TEXTURE_2D, ret=s_handlerIndex); // Bind the ID texture specified by the 2nd parameter
00087 
00088         // The next commands sets the texture parameters
00089         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // If the u,v coordinates overflow the range 0,1 the image is repeated
00090         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00091         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // The magnification function ("linear" produces better results)
00092         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); //The minifying function
00093 
00094         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // We don't combine the color with the original surface color, use only the texture map.
00095 
00096         // Finally we define the 2d texture
00097         glTexImage2D(GL_TEXTURE_2D, 0, 4, data->infoheader.biWidth, data->infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, data->l_texture);
00098 
00099         // And create 2d mipmaps for the minifying function
00100         gluBuild2DMipmaps(GL_TEXTURE_2D, 4, data->infoheader.biWidth, data->infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, data->l_texture);
00101 
00102         glBindTexture(GL_TEXTURE_2D, 0);
00103 
00104         return ret;
00105 }
00106 
00107 TextureData* TextureInterface::loadPNG(std::string p_filename)
00108 {
00109         TextureData *ret = 0;
00110         ilBindImage(0);
00111         if (ilLoadImage(p_filename.c_str()) == IL_TRUE)
00112         {
00113                 ret = new TextureData();
00114                 ret->infoheader.biWidth = ilGetInteger(IL_IMAGE_WIDTH);
00115                 ret->infoheader.biHeight = ilGetInteger(IL_IMAGE_HEIGHT);
00116                 ret->l_texture = (unsigned char*)malloc(ret->infoheader.biWidth * ret->infoheader.biHeight * sizeof(unsigned char) * 4);
00117                 ilCopyPixels(0, 0, 0, ret->infoheader.biWidth, ret->infoheader.biHeight, 1, IL_RGBA, IL_UNSIGNED_BYTE, ret->l_texture);
00118                 while (ilGetError() != IL_NO_ERROR)
00119                         int stop = 1;
00120         }
00121         int stop;
00122         while (stop = ilGetError() != IL_NO_ERROR)
00123                 stop = 1;
00124         return ret;
00125 }

Generated on Tue Jul 17 22:02:22 2007 for C0re by  doxygen 1.5.2