Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 7, 2005, 3:54:49 PM (19 years ago)
Author:
chris
Message:

orxonox/branches/levelloader: Merged trunk into branch… still not working though…

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/levelloader/src/lib/graphics/importer/texture.cc

    r3605 r3746  
    2222#include "texture.h"
    2323
    24 // headers only for PathList
    25 #include <unistd.h>
    26 #include <sys/types.h>
    27 #include <sys/stat.h>
    28 #include <stdlib.h>
    29 #include <fstream>
    30 
    31 /**
    32    \brief creates a ned PathList.
    33    
    34    It is a good idea to use this as an initial List,
    35    because if you give on a name the Path will not be checked for its existence.
    36 */
    37 PathList::PathList()
    38 {
    39   this->pathName = NULL;
    40   this->next = NULL;
    41 }
    42 
    43 /**
    44    \brief Creates a new PathList with a Name.
    45    \param pName the Name of The Path.
    46 
    47    This function just adds the Path without checking if it exists.
    48 */
    49 PathList::PathList(char* pName)
    50 {
    51   this->pathName = new char [strlen(pName)+1];
    52   strcpy (this->pathName, pName);
    53   this->next = NULL;
    54 }
    55 
    56 /**
    57    \brief destroys a PathList
    58 
    59    It does this by deleting the Name and then delete its preceding PathList.
    60 */
    61 PathList::~PathList()
    62 {
    63   if (this->pathName)
    64     delete []this->pathName;
    65   if (this->next)
    66     delete this->next;
    67 }
    68 
    69 PathList* PathList::firstPath = NULL;
    70 
    71 /**
    72    \returns A Pointer to the first Path of the Pathlist
    73 */
    74 PathList* PathList::getInstance(void)
    75 {
    76   if (firstPath)
    77     return firstPath;
    78   firstPath = new PathList();
    79 }
    80 /**
    81    \brief Adds a new Pathlist Element.
    82    \param pName
    83    
    84    Adding a Path automatically checks if the Path exists,
    85    and if it does not it will not add it to the List.
    86 */
    87 void PathList::addPath (char* pName)
    88 {
    89   if (pName[0] == '\0')
    90     {
    91       PRINTF(2)("not Adding empty Path to the List.\n");
    92       return;
    93     }
    94   char* tmpPName = new char[strlen(pName)];
    95   strncpy(tmpPName, pName, strlen(pName)-1);
    96   tmpPName[strlen(pName)-1] = '\0';
    97   if (access (tmpPName, F_OK) == 0)
    98     {
    99       struct stat status;
    100       stat(tmpPName, &status);
    101       if (status.st_mode & S_IFDIR)
    102         {
    103           PRINTF(4)("Adding Path %s to the PathList.\n", pName);
    104           PathList* tmpPathList = this;
    105           while (tmpPathList->next)
    106             tmpPathList = tmpPathList->next;
    107           tmpPathList->next = new PathList(pName);
    108         }
    109       else
    110         PRINTF(2)("You tried to add non-folder %s to a PathList.\n", tmpPName);
    111     }
    112   else
    113       PRINTF(2)("You tried to add non-existing folder %s to a PathList.\n", tmpPName);
    114   delete []tmpPName;
    115 }
    116 
    117 
     24#include "graphics_engine.h"
    11825
    11926/**
     
    12734  this->texture = 0;
    12835}
     36
     37/**
     38   \brief Constructor for a Texture
     39*/
     40Texture::Texture(const char* imageName)
     41{
     42  this->pImage = new Image;
     43  this->pImage->data = NULL;
     44  this->map = NULL;
     45  this->texture = 0;
     46  this->loadImage(imageName);
     47
    12948
    13049/**
     
    14362
    14463/**
    145    \brief Searches for a Texture inside one of the defined Paths
    146    \param texName The name of the texture o search for.
    147    \returns pathName+texName if texName was found in the pathList. NULL if the Texture is not found.
    148 */
    149 char* Texture::searchTextureInPaths(char* texName) const
    150 {
    151   char* tmpName = NULL;
    152   PathList* pList = PathList::getInstance();
    153   while (pList)
    154     {
    155       if (pList->pathName)
    156         {
    157           tmpName = new char [strlen(pList->pathName)+strlen(texName)+1];
    158           strcpy(tmpName, pList->pathName);
    159         }
    160       else
    161         {
    162           tmpName = new char [strlen(texName)+1];
    163           tmpName[0]='\0';
    164         }
    165       strcat(tmpName, texName);
    166       if (access (tmpName, F_OK) == 0)
    167         return tmpName;
    168      
    169       if (tmpName)
    170         delete []tmpName;
    171       tmpName = NULL;
    172       pList = pList->next;
    173     }
    174   return NULL;
    175 }
    176 
    177 /**
    17864   \brief a Simple function that switches two char values
    17965   \param a The first value
     
    19581bool Texture::loadTexToGL (Image* pImage)
    19682{
    197   PRINTF(4)("Loading texture to OpenGL-Environment.\n");
    198   glGenTextures(1, &this->texture);
    199   glBindTexture(GL_TEXTURE_2D, this->texture);
    200   /* not Working, and not needed.
    201   glTexImage2D( GL_TEXTURE_2D, 0, 3, width,
    202                 height, 0, GL_BGR,
    203                 GL_UNSIGNED_BYTE, map->pixels );
    204   */
    205   gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pImage->width, pImage->height, pImage->format, GL_UNSIGNED_BYTE, pImage->data);
    206  
    207   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
    208   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
    209 }
    210 
     83  if (GraphicsEngine::texturesEnabled)
     84    {
     85      PRINTF(4)("Loading texture to OpenGL-Environment.\n");
     86      glGenTextures(1, &this->texture);
     87      glBindTexture(GL_TEXTURE_2D, this->texture);
     88      /* not Working, and not needed.
     89         glTexImage2D( GL_TEXTURE_2D, 0, 3, width,
     90         height, 0, GL_BGR,
     91         GL_UNSIGNED_BYTE, map->pixels );
     92      */
     93      gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pImage->width, pImage->height, pImage->format, GL_UNSIGNED_BYTE, pImage->data);
     94     
     95      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
     96      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);     
     97    }
     98}
    21199
    212100#ifdef HAVE_SDL_SDL_IMAGE_H
    213 bool Texture::loadImage(char* imageName)
    214 {
    215   char* imgNameWithPath = searchTextureInPaths(imageName);
    216   if (imgNameWithPath)
    217     {
    218       this->map=IMG_Load(imgNameWithPath);
    219       if(!map)
    220         {
    221           PRINTF(1)("IMG_Load: %s\n", IMG_GetError());
     101bool Texture::loadImage(const char* imageName)
     102{
     103  if (GraphicsEngine::texturesEnabled)
     104    {
     105      if (imageName)
     106        {
     107          this->map=IMG_Load(imageName);
     108          if(!map)
     109            {
     110              PRINTF(1)("IMG_Load: %s\n", IMG_GetError());
     111              return false;
     112            }
     113          pImage->height = map->h;
     114          pImage->width  = map->w;
     115          pImage->data   = (GLubyte*)map->pixels;
     116          pImage->bpp    = map->format->BytesPerPixel;
     117          if (pImage->bpp == 3)
     118            pImage->format = GL_RGB;
     119          else if (pImage->bpp == 4)
     120            pImage->format = GL_RGBA;
     121         
     122          if( !IMG_isPNG(SDL_RWFromFile(imageName, "rb")) && !IMG_isJPG(SDL_RWFromFile(imageName, "rb")))
     123            for (int i=0;i<map->h * map->w *3;i+=3)
     124              {
     125                GLuint temp = pImage->data[i];
     126                pImage->data[i] = pImage->data[i+2];
     127                pImage->data[i+2] = temp;
     128              }
     129          /* this is the real swapping algorithm */
     130          for( int i = 0 ; i < (pImage->height / 2) ; ++i )
     131            for( int j = 0 ; j < pImage->width * pImage->bpp; j += pImage->bpp )
     132              for(int k = 0; k < pImage->bpp; ++k)
     133                swap( pImage->data[ (i * pImage->width * pImage->bpp) + j + k], pImage->data[ ( (pImage->height - i - 1) * pImage->width * pImage->bpp ) + j + k]);
     134         
     135          this->loadTexToGL (this->pImage);
     136          SDL_FreeSurface(map);
     137          this->pImage->data = NULL;
     138        }
     139      else
     140        {
     141          PRINTF(2)("Image not Found: %s\n", imageName);
    222142          return false;
    223143        }
    224       pImage->height = map->h;
    225       pImage->width  = map->w;
    226       pImage->data   = (GLubyte*)map->pixels;
    227       pImage->bpp    = map->format->BytesPerPixel;
    228       if (pImage->bpp == 3)
    229         pImage->format = GL_RGB;
    230       else if (pImage->bpp == 4)
    231         pImage->format = GL_RGBA;
    232          
    233       if( !IMG_isPNG(SDL_RWFromFile(imgNameWithPath, "rb")) && !IMG_isJPG(SDL_RWFromFile(imgNameWithPath, "rb")))
    234         for (int i=0;i<map->h * map->w *3;i+=3)
    235           {
    236             GLuint temp = pImage->data[i];
    237             pImage->data[i] = pImage->data[i+2];
    238             pImage->data[i+2] = temp;
    239           }
    240       /* this is the real swapping algorithm */
    241       for( int i = 0 ; i < (pImage->height / 2) ; ++i )
    242         for( int j = 0 ; j < pImage->width * pImage->bpp; j += pImage->bpp )
    243           for(int k = 0; k < pImage->bpp; ++k)
    244             swap( pImage->data[ (i * pImage->width * pImage->bpp) + j + k], pImage->data[ ( (pImage->height - i - 1) * pImage->width * pImage->bpp ) + j + k]);
    245  
    246       this->loadTexToGL (this->pImage);
    247       SDL_FreeSurface(map);
    248       this->pImage->data = NULL;
    249     }
    250   else
    251     {
    252       PRINTF(2)("Image not Found: %s\n", imgNameWithPath);
    253       return false;
    254144    }
    255145}
     
    262152   \todo Checks where to find the Image
    263153*/
    264 bool Texture::loadImage(char* imageName)
    265 {
    266   char* imgNameWithPath = searchTextureInPaths(imageName);
    267   if (imgNameWithPath)
    268     {
    269       if (!strncmp(imgNameWithPath+strlen(imgNameWithPath)-4, ".bmp", 4))
    270         {
    271           PRINTF(4)("Requested bmp-image. Trying to Import.\n");
    272           return this->loadBMP(imgNameWithPath);
    273         }
    274      
    275       else if (!strncmp(imgNameWithPath+strlen(imgNameWithPath)-4, ".jpg", 4) || !strncmp(imgNameWithPath+strlen(imgNameWithPath)-5, ".jpg", 5))
    276         {
    277           PRINTF(4)("Requested jpeg-image. Trying to Import\n");
    278           return this->loadJPG(imgNameWithPath);
    279         }
    280       else if (!strncmp(imgNameWithPath+strlen(imgNameWithPath)-4, ".tga", 4))
    281         {
    282           PRINTF(4)("Requested tga-image. Trying to Import\n");
    283           return this->loadTGA(imgNameWithPath);
    284         }
    285       else if (!strncmp(imgNameWithPath+strlen(imgNameWithPath)-4, ".png", 4))
    286         {
    287           PRINTF(4)("Requested png-image. Trying to Import\n");
    288           return this->loadPNG(imgNameWithPath);
     154bool Texture::loadImage(const char* imageName)
     155{
     156  if (GraphicsEngine::texturesEnabled)
     157    {
     158      if (imageName)
     159        {
     160          if (!strncmp(imageName+strlen(imageName)-4, ".bmp", 4))
     161            {
     162              PRINTF(4)("Requested bmp-image. Trying to Import.\n");
     163              return this->loadBMP(imageName);
     164            }
     165         
     166          else if (!strncmp(imageName+strlen(imageName)-4, ".jpg", 4) || !strncmp(imageName+strlen(imageName)-5, ".jpg", 5))
     167            {
     168              PRINTF(4)("Requested jpeg-image. Trying to Import\n");
     169              return this->loadJPG(imageName);
     170            }
     171          else if (!strncmp(imageName+strlen(imageName)-4, ".tga", 4))
     172            {
     173              PRINTF(4)("Requested tga-image. Trying to Import\n");
     174              return this->loadTGA(imageName);
     175            }
     176          else if (!strncmp(imageName+strlen(imageName)-4, ".png", 4))
     177            {
     178              PRINTF(4)("Requested png-image. Trying to Import\n");
     179              return this->loadPNG(imageName);
     180            }
     181          else
     182            {
     183              PRINTF(2)("Requested Image was not recognized in its type. (Maybe a type-Cast-error.)\n FileName: %s", imageName);
     184              return false;
     185            }
    289186        }
    290187      else
    291188        {
    292           PRINTF(2)("Requested Image was not recognized in its type. (Maybe a type-Cast-error.)\n FileName: %s", imgNameWithPath);
     189          PRINTF(2)("Image not Found: %s\n", imageName);
    293190          return false;
    294191        }
    295192    }
    296   else
    297     {
    298       PRINTF(2)("Image not Found: %s\n", imgNameWithPath);
    299       return false;
    300     }
    301 }
    302 
     193}
    303194/**
    304195   \brief reads in a Windows BMP-file, and imports it to openGL.
Note: See TracChangeset for help on using the changeset viewer.