Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3905 in orxonox.OLD


Ignore:
Timestamp:
Apr 20, 2005, 12:39:05 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: image now gets loaded with the an SDL_Surface instead of a strange pointer to a Struct that is only redundant

Location:
orxonox/trunk/src/lib/graphics/importer
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • orxonox/trunk/src/lib/graphics/importer/model.cc

    r3895 r3905  
    124124      if (counter == groupNumber)
    125125        {
    126           PRINTF(2)("Drawing model number %i named %s\n", counter, walker->name);
     126          PRINTF(4)("Drawing model number %i named %s\n", counter, walker->name);
    127127          glCallList (walker->listNumber);
    128128          return;
     
    498498bool Model::addVertexNormal(float x, float y, float z)
    499499{
    500   PRINTF(3)("found vertex-Normal %f, %f, %f\n", x, y, z);
     500  PRINTF(5)("found vertex-Normal %f, %f, %f\n", x, y, z);
    501501  this->normals->addEntry(x, y, z);
    502502}
  • orxonox/trunk/src/lib/graphics/importer/texture.cc

    r3863 r3905  
    2323   \brief Constructor for a Texture
    2424*/
    25 Texture::Texture(void)
    26 {
    27   this->pImage = new Image;
    28   this->pImage->data = NULL;
    29   this->map = NULL;
    30   this->texture = 0;
    31 }
    32 
    33 /**
    34    \brief Constructor for a Texture
    35 */
    3625Texture::Texture(const char* imageName)
    3726{
    38   this->pImage = new Image;
    39   this->pImage->data = NULL;
    40   this->map = NULL;
    4127  this->texture = 0;
    42   this->loadImage(imageName);
     28  if (imageName)
     29    this->loadImage(imageName);
    4330
    4431
     
    5037Texture::~Texture(void)
    5138{
    52   if (this->pImage->data)
    53     delete []this->pImage->data;
    54   delete pImage;
    5539  if (this->texture)
    5640    glDeleteTextures(1, &this->texture);
     
    7357/**
    7458   \brief Loads a Texture to the openGL-environment.
    75    \param pImage The Image to load to openGL
     59   \param surface the Image to load to openGL
     60   \returns The ID of the texture.
    7661*/
    77 bool Texture::loadTexToGL (Image* pImage)
     62GLuint Texture::loadTexToGL (SDL_Surface* surface)
    7863{
    7964  if (GraphicsEngine::texturesEnabled)
    8065    {
    8166      PRINTF(4)("Loading texture to OpenGL-Environment.\n");
    82       glGenTextures(1, &this->texture);
    83       glBindTexture(GL_TEXTURE_2D, this->texture);
    84       /* not Working, and not needed.
    85          glTexImage2D( GL_TEXTURE_2D, 0, 3, width,
    86          height, 0, GL_BGR,
    87          GL_UNSIGNED_BYTE, map->pixels );
    88       */
    89       gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pImage->width, pImage->height, pImage->format, GL_UNSIGNED_BYTE, pImage->data);
     67
     68      GLuint texture;
     69      int w, h;
     70      SDL_Surface *image;
     71      SDL_Rect area;
     72      Uint32 saved_flags;
     73      Uint8  saved_alpha;
    9074     
    91       glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
    92       glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);     
     75      w = surface->w;
     76      h = surface->h;
     77     
     78      image = SDL_CreateRGBSurface(SDL_SWSURFACE,
     79                                   w, h,
     80                                   32,
     81#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
     82                                   0x000000FF,
     83                                   0x0000FF00,
     84                                   0x00FF0000,
     85                                   0xFF000000
     86#else
     87                                   0xFF000000,
     88                                   0x00FF0000,
     89                                   0x0000FF00,
     90                                   0x000000FF
     91#endif
     92                                   );
     93      if ( image == NULL ) {
     94        return 0;
     95      }
     96     
     97      /* Save the alpha blending attributes */
     98      saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
     99      saved_alpha = surface->format->alpha;
     100      if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
     101        SDL_SetAlpha(surface, 0, 0);
     102      }
     103     
     104      /* Copy the surface into the GL texture image */
     105      area.x = 0;
     106      area.y = 0;
     107      area.w = surface->w;
     108      area.h = surface->h;
     109      SDL_BlitSurface(surface, &area, image, &area);
     110     
     111      /* Restore the alpha blending attributes */
     112      if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
     113        SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);
     114      }
     115     
     116      /* Create an OpenGL texture for the image */
     117      glGenTextures(1, &texture);
     118      glBindTexture(GL_TEXTURE_2D, texture);
     119      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
     120      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     121      /*      glTexImage2D(GL_TEXTURE_2D,
     122                   0,
     123                   GL_RGBA,
     124                   w, h,
     125                   0,
     126                   GL_RGBA,
     127                   GL_UNSIGNED_BYTE,
     128                   image->pixels);
     129      */
     130      gluBuild2DMipmaps(GL_TEXTURE_2D,
     131                        3,
     132                        w,
     133                        h,
     134                        GL_RGBA,
     135                        GL_UNSIGNED_BYTE,
     136                        image->pixels);
     137
     138      SDL_FreeSurface(image); /* No longer needed */
     139     
     140      return texture;
    93141    }
    94142}
     
    104152      if (imageName)
    105153        {
    106           this->map=IMG_Load(imageName);
    107           if(!map)
     154          SDL_Surface* tmpSurf;
     155          if (this->texture)
     156            glDeleteTextures(1, &this->texture);
     157          // load the new Image to memory
     158          tmpSurf = IMG_Load(imageName);
     159          if(!tmpSurf)
    108160            {
    109161              PRINTF(1)("IMG_Load: %s\n", IMG_GetError());
    110162              return false;
    111163            }
    112           pImage->height = map->h;
    113           pImage->width  = map->w;
    114           pImage->data   = (GLubyte*)map->pixels;
    115           pImage->bpp    = map->format->BytesPerPixel;
    116           if (pImage->bpp == 3)
    117             pImage->format = GL_RGB;
    118           else if (pImage->bpp == 4)
    119             {
    120               pImage->format = GL_RGBA;
    121               SDL_SetAlpha(this->map, 0, 0);
    122             }
    123164
    124           if( !IMG_isPNG(SDL_RWFromFile(imageName, "rb")) && !IMG_isJPG(SDL_RWFromFile(imageName, "rb")))
    125             for (int i=0;i<map->h * map->w *3;i+=3)
    126               {
    127                 GLuint temp = pImage->data[i];
    128                 pImage->data[i] = pImage->data[i+2];
    129                 pImage->data[i+2] = temp;
    130               }
    131           /* this is the real swapping algorithm */
    132           for( int i = 0 ; i < (pImage->height / 2) ; ++i )
    133             for( int j = 0 ; j < pImage->width * pImage->bpp; j += pImage->bpp )
    134               for(int k = 0; k < pImage->bpp; ++k)
    135                 swap( pImage->data[ (i * pImage->width * pImage->bpp) + j + k], pImage->data[ ( (pImage->height - i - 1) * pImage->width * pImage->bpp ) + j + k]);
     165          GLubyte* pixels = (GLubyte*)tmpSurf->pixels;
    136166         
    137           this->loadTexToGL (this->pImage);
    138           SDL_FreeSurface(map);
    139           pImage->data = NULL;
     167          /* this swaps the Mapping so lowel left will be upper left */
     168          for( int i = 0 ; i < (tmpSurf->h / 2) ; ++i )
     169            for( int j = 0 ; j < tmpSurf->w * tmpSurf->format->BytesPerPixel; j += tmpSurf->format->BytesPerPixel )
     170              for(int k = 0; k < tmpSurf->format->BytesPerPixel; ++k)
     171                swap( pixels[(i * tmpSurf->w * tmpSurf->format->BytesPerPixel) + j + k],
     172                      pixels[ ( (tmpSurf->h - i - 1) * tmpSurf->w *  tmpSurf->format->BytesPerPixel) + j + k]);
     173
     174          PRINTF(3)("loading Image %s\n", imageName);
     175          if (tmpSurf)
     176            this->texture = loadTexToGL(tmpSurf);
     177
     178         
     179          SDL_FreeSurface(tmpSurf);
     180          return true;
    140181        }
    141182      else
  • orxonox/trunk/src/lib/graphics/importer/texture.h

    r3863 r3905  
    2020{
    2121 private:
    22   //! Struct to handle Infos about an Image
    23   struct Image
    24   {
    25     int rowSpan;    //!< The count of the rows this Image has.
    26     GLuint width;   //!< The width of the Image.
    27     GLuint height;  //!< The height of the Image.
    28     GLuint bpp;     //!< BytesPerPixel
    29     GLenum format;  //!< The Format of the PixelData
    30     GLuint type;    //!< Type of the Image.
    31     GLubyte *data;  //!< The Image Data comes here! DANGER: uncompressed data.
    32   };
    33   Image* pImage;    //!< The data of an Image
    3422  GLuint texture;   //!< The Texture-ID of opengl from this Texture.
    35   SDL_Surface* map; //!< The map SDL initializes for this element.
    3623  char* searchTextureInPaths(const char* texName) const;
    3724  void swap(unsigned char &a, unsigned char &b);
    3825 public:
    39   Texture(void);
    40   Texture(const char* imageName);
     26  Texture(const char* imageName = NULL);
    4127  ~Texture(void);
     28
    4229  /** \returns The textureID of this texture.  */
    4330  inline GLuint getTexture(void) {return this->texture;}
    44   bool loadTexToGL (Image* pImage);
     31  GLuint loadTexToGL (SDL_Surface* surface);
    4532
    4633  bool loadImage(const char* imageName);
Note: See TracChangeset for help on using the changeset viewer.