Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5768 in orxonox.OLD for trunk/src/lib


Ignore:
Timestamp:
Nov 24, 2005, 8:24:52 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: font is a Texture now (this is a procedural texture)

Location:
trunk/src/lib/graphics
Files:
6 edited

Legend:

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

    r5755 r5768  
    6666    {
    6767      if (this->image != NULL)
    68         {
    69           SDL_FreeSurface(this->image);
    70           this->image = NULL;
    71         }
     68        {
     69          SDL_FreeSurface(this->image);
     70          this->image = NULL;
     71        }
    7272      if (this->texture != 0)
    73         {
    74           glDeleteTextures(1, &this->texture);
    75           this->texture = 0;
    76         }
     73        {
     74          glDeleteTextures(1, &this->texture);
     75          this->texture = 0;
     76        }
    7777      if (imageName != NULL)
    7878        {
     
    8585          {
    8686            PRINTF(4)("loading Image %s\n", imageName);
    87             this->image = this->prepareSurface(tmpSurf);
    88             this->texture = loadTexToGL(this->image);
     87            if (this->prepareSurface(tmpSurf))
     88              loadTexToGL();
    8989            SDL_FreeSurface(tmpSurf);
    9090            return true;
     
    9999      else
    100100        {
    101           PRINTF(2)("Image-Name not specified\n");
     101          PRINTF(2)("Image-Name not specified\n");
    102102          return false;
    103103        }
     
    117117    {
    118118      PRINTF(4)("Reloading Texture %s\n", this->getName());
    119       this->texture = this->loadTexToGL(this->image);
     119      this->loadTexToGL();
    120120    }
    121  
     121
    122122}
    123123
     
    127127 * @returns a !!new!! Surface, that is loadable by openGL.
    128128 */
    129 SDL_Surface* Texture::prepareSurface(SDL_Surface* surface)
     129bool Texture::prepareSurface(SDL_Surface* surface)
    130130{
    131131  PRINTF(4)("Loading texture to OpenGL-Environment.\n");
    132  
    133   SDL_Surface *image;
     132
     133  SDL_Surface* putSurface;
    134134  SDL_Rect area;
    135135  Uint32 saved_flags;
    136136  Uint8  saved_alpha;
    137  
    138   image = SDL_CreateRGBSurface(SDL_SWSURFACE,
    139                                surface->w, surface->h,
    140                                32,
     137
     138  putSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,
     139                               surface->w, surface->h,
     140                               32,
    141141#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
    142                                0x000000FF,
    143                                0x0000FF00,
    144                                0x00FF0000,
    145                                0xFF000000
     142                               0x000000FF,
     143                               0x0000FF00,
     144                               0x00FF0000,
     145                               0xFF000000
    146146#else
    147                                0xFF000000,
    148                                0x00FF0000,
    149                                0x0000FF00,
    150                                0x000000FF
     147                               0xFF000000,
     148                               0x00FF0000,
     149                               0x0000FF00,
     150                               0x000000FF
    151151#endif
    152                                );
    153   if ( image == NULL )
    154     return NULL;
    155  
     152                               );
     153  if ( putSurface == NULL )
     154  {
     155    this->setSurface(NULL);
     156    return false;
     157  }
     158
    156159  /* Save the alpha blending attributes */
    157160  saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
     
    160163    SDL_SetAlpha(surface, 0, 0);
    161164  }
    162  
     165
    163166  /* Copy the surface into the GL texture image */
    164167  area.x = 0;
     
    166169  area.w = surface->w;
    167170  area.h = surface->h;
    168   SDL_BlitSurface(surface, &area, image, &area);
    169  
     171  SDL_BlitSurface(surface, &area, putSurface, &area);
     172
    170173  /* Restore the alpha blending attributes */
    171   if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
    172     {
    173       SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);
    174       this->bAlpha = true;
    175     }
    176   return image;
     174  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
     175  {
     176    SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);
     177    this->bAlpha = true;
     178  }
     179
     180  return (this->setSurface(putSurface));
     181}
     182
     183bool Texture::setSurface(SDL_Surface* newSurface)
     184{
     185  if (this->image != NULL)
     186    SDL_FreeSurface(this->image);
     187
     188  this->image = newSurface;
     189
     190  return (this->image != NULL);
    177191}
    178192
     
    183197 * @returns The ID of the texture.
    184198 */
    185 GLuint Texture::loadTexToGL (SDL_Surface* surface) const
    186 {
    187   GLuint texture = 0;
     199GLuint Texture::loadTexToGL ()
     200{
     201  if (this->texture != 0 && glIsTexture(this->texture))
     202    glDeleteTextures(1, &this->texture);
     203  this->texture = 0;
     204
     205  if (this->image == NULL)
     206    return 0;
     207
    188208  /* Create an OpenGL texture for the image */
    189   glGenTextures(1, &texture);
    190   glBindTexture(GL_TEXTURE_2D, texture);
     209  glGenTextures(1, &this->texture);
     210  glBindTexture(GL_TEXTURE_2D, this->texture);
    191211  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    192212  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    193213  // build the Texture
    194214  glTexImage2D(GL_TEXTURE_2D,
    195                0,
    196                GL_RGBA,
    197                surface->w, surface->h,
    198                0,
    199                GL_RGBA,
    200                GL_UNSIGNED_BYTE,
    201                surface->pixels);
     215               0,
     216               GL_RGBA,
     217               this->image->w, this->image->h,
     218               0,
     219               GL_RGBA,
     220               GL_UNSIGNED_BYTE,
     221               this->image->pixels);
    202222  // build the MipMaps
    203223  gluBuild2DMipmaps(GL_TEXTURE_2D,
    204                     GL_RGBA,
    205                     surface->w,
    206                     surface->h,
    207                     GL_RGBA,
    208                     GL_UNSIGNED_BYTE,
    209                     surface->pixels);
     224                    GL_RGBA,
     225                    this->image->w,
     226                    this->image->h,
     227                    GL_RGBA,
     228                    GL_UNSIGNED_BYTE,
     229                    this->image->pixels);
    210230  glBindTexture(GL_TEXTURE_2D, 0);
    211   return texture;
    212 }
     231  return this->texture;
     232}
  • trunk/src/lib/graphics/importer/texture.h

    r5755 r5768  
    11/*!
    2   \file texture.h
    3   \brief Contains the texture class, that handles the reading of Images into Texutre-files.
    4 
    5   @todo procedural textures
    6 */
     2 * @file texture.h
     3 * @brief Contains the texture class, that handles the reading of Images into Texutre-files.
     4 */
    75
    86#ifndef _TEXTURE_H
    97#define _TEXTURE_H
    108
    11 #include "glincl.h"
    129#include "base_object.h"
    1310
    14 #include "debug.h"
     11#include "glincl.h"
    1512
     13/* Forward Declaration */
    1614struct SDL_Surface;
    1715
    1816//! an enumerator for different procedural texture-types
    1917typedef enum TEXTURE_TYPE { TEXTURE_RADIAL_ALIAS,
    20                             TEXTURE_NOISE };
     18  TEXTURE_NOISE };
    2119
    2220//! A Class, that reads in Textures from different fileformats.
    23 class Texture : public BaseObject
    24 {
    25  public:
    26   Texture(const char* imageName = NULL);
     21  class Texture : public BaseObject
     22  {
     23    public:
     24      Texture(const char* imageName = NULL);
    2725  //  Texture(TEXTURE_TYPE type, int resolution);
    28   ~Texture();
     26      ~Texture();
    2927
    30   bool loadImage(const char* imageName);
    31   bool rebuild();
     28      bool loadImage(const char* imageName);
     29      bool rebuild();
    3230
    33   /** @returns The textureID of this texture.  */
    34   inline GLuint getTexture() const { return this->texture; };
    35   /** @returns true if texture has alpha, false otherwise */
    36   inline bool hasAlpha() const {return bAlpha;}
     31      /** @returns The textureID of this texture.  */
     32      inline GLuint getTexture() const { return this->texture; };
     33      /** @returns true if texture has alpha, false otherwise */
     34      inline bool hasAlpha() const {return bAlpha;}
    3735
    38  private:
    39   SDL_Surface* prepareSurface(SDL_Surface* input);
    40   GLuint loadTexToGL (SDL_Surface* surface) const;
     36    protected:
     37      bool prepareSurface(SDL_Surface* input);
     38      bool setSurface(SDL_Surface* newSurface);
    4139
    42  private:
    43   GLuint        texture;            //!< The Texture-ID of opengl from this Texture.
    44   bool          bAlpha;             //!< if the texture has an alpha channel.
    45   SDL_Surface*  image;   
    46 };
     40      GLuint loadTexToGL ();
     41
     42    private:
     43      GLuint        texture;            //!< The Texture-ID of opengl from this Texture.
     44      bool          bAlpha;             //!< if the texture has an alpha channel.
     45      SDL_Surface*  image;              //!< The SDL_Surfce that stores the Texture on it.
     46  };
    4747
    4848#endif /* _TEXTURE_H */
  • trunk/src/lib/graphics/text_engine/font.cc

    r5427 r5768  
    132132  this->fontTTF = NULL;
    133133  this->glyphArray = NULL;
    134   this->fastTextureID = 0;
    135134}
    136135
     
    149148    this->fontTTF = NULL;
    150149  }
    151   if (this->fastTextureID != 0)
    152   {
    153     if(glIsTexture(this->fastTextureID))
    154       glDeleteTextures(1, &this->fastTextureID);
    155     this->fastTextureID = 0;
    156   }
     150
    157151
    158152  this->setName(fontFile);
     
    161155  if(this->fontTTF != NULL)
    162156  {
    163     this->fastTextureID = this->createFastTexture();
    164     if (this->fastTextureID != 0)
    165       return true;
    166     else
    167       return false;
     157    this->createFastTexture();
     158    return (this->getTexture() != 0);
    168159  }
    169160  else
     
    190181    this->fontTTF = NULL;
    191182  }
    192   if (this->fastTextureID != 0)
    193   {
    194     if(glIsTexture(this->fastTextureID))
    195       glDeleteTextures(1, &this->fastTextureID);
    196     this->fastTextureID = 0;
    197   }
    198 
    199   this->fastTextureID = Text::loadTexture(surface, NULL);
     183  if (this->prepareSurface(surface))
     184    this->loadTexToGL( );
    200185
    201186  // initializing the Glyphs.
     
    251236Font* Font::defaultFont = NULL;
    252237
     238/**
     239 * creates and exports an Image, that has all the characters
     240 * stored in a Array (as an image)
     241 * @param fileName the File to write the image into.
     242 */
    253243void Font::createAsciiImage(const char* fileName)
    254244{
     
    400390 * creates a Fast-Texture of this Font
    401391 */
    402 GLuint Font::createFastTexture()
     392bool Font::createFastTexture()
    403393{
    404394  /* interesting GLYPHS:
     
    500490  //   SDL_SaveBMP(tmpSurf, outName);
    501491
    502   GLuint texture;
    503   glGenTextures(1, &texture);
    504   glBindTexture(GL_TEXTURE_2D, texture);
    505   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    506   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    507   glTexImage2D(GL_TEXTURE_2D,
    508                0,
    509                GL_RGBA,
    510                tmpSurf->w, tmpSurf->h,
    511                0,
    512                GL_RGBA,
    513                GL_UNSIGNED_BYTE,
    514                tmpSurf->pixels);
    515   SDL_FreeSurface(tmpSurf);
    516   return texture;
     492  if (this->setSurface(tmpSurf))
     493    loadTexToGL();
    517494}
    518495
  • trunk/src/lib/graphics/text_engine/font.h

    r5767 r5768  
    1111#define _FONT_H
    1212
    13 #include "base_object.h"
     13#include "texture.h"
    1414
    1515#include "glincl.h"
     
    5252
    5353//! A class to handle a Font of a certain ttf-File/image-file, Size.
    54 class Font : public BaseObject
     54class Font : public Texture
    5555{
    5656  public:
     
    6363    void init();
    6464
    65   // font
     65  //  font
    6666    bool loadFontFromTTF(const char* fontFile);
    6767    bool loadFontFromSDL_Surface(SDL_Surface* surface);
     
    7171    /** @returns a Pointer to the Array of Glyphs */
    7272    inline Glyph** getGlyphArray() const { return this->glyphArray; };
    73     /** @returns the texture to the fast-texture */
    74     inline GLuint getFastTextureID() const { return this->fastTextureID; };
     73    /** @returns the a pointer to the TTF */
     74    inline TTF_Font* getTTF() const { return this->fontTTF; };
     75
     76
    7577    /** @returns the default Font */
    7678    inline static Font* getDefaultFont() { if (Font::defaultFont == NULL) initDefaultFont(); return Font::defaultFont; };
    77     /** @returns the a pointer to the TTF */
    78     inline TTF_Font* getTTF() const { return this->fontTTF; };
    7979
    8080    void createAsciiImage(const char* fileName);
    8181    static void initDefaultFont();
    8282    static void removeDefaultFont();
    83 
    8483
    8584  private:
     
    8988    Glyph* getGlyphMetrics(Uint16 character);
    9089
    91     GLuint createFastTexture();
     90    bool createFastTexture();
    9291
    9392    void initGlyphs(Uint16 from, Uint16 count);
     
    104103
    105104    Glyph**       glyphArray;          //!< An Array of all the Glyphs stored in the Array of Glyphs.
    106     GLuint        fastTextureID;       //!< The fast textureID.
    107105};
    108106
  • trunk/src/lib/graphics/text_engine/text.cc

    r5767 r5768  
    3838  if (fontFile != NULL)
    3939    this->setFont(fontFile, FONT_DEFAULT_RENDER_SIZE);
    40   this->setSizeY2D(this->size = textSize);
     40  this->setSizeY2D(textSize);
    4141}
    4242
     
    6969  this->blending = TEXT_DEFAULT_BLENDING;
    7070  this->color = TEXT_DEFAULT_COLOR;
    71   this->size = TEXT_DEFAULT_SIZE;
    72 
     71  this->setSize(TEXT_DEFAULT_SIZE);
    7372  this->setText(NULL);
    7473}
     
    182181  {
    183182    glyphArray = this->font->getGlyphArray();
    184     glBindTexture(GL_TEXTURE_2D, font->getFastTextureID());
     183    glBindTexture(GL_TEXTURE_2D, font->getTexture());
    185184  }
    186185  else
     
    189188      Font::initDefaultFont();
    190189    glyphArray = Font::getDefaultFont()->getGlyphArray();
    191     glBindTexture(GL_TEXTURE_2D, Font::getDefaultFont()->getFastTextureID());
     190    glBindTexture(GL_TEXTURE_2D, Font::getDefaultFont()->getTexture());
    192191  }
    193192  const char* tmpText = this->externText;
     
    210209
    211210        glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
    212         glVertex2d(posX, this->size);
     211        glVertex2d(posX, this->getSizeY2D());
    213212
    214213        glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
    215         glVertex2d(posX+tmpGlyph->width*this->size, this->size);
     214        glVertex2d(posX+tmpGlyph->width*this->getSizeY2D(), this->getSizeY2D());
    216215
    217216        glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
    218         glVertex2d(posX+tmpGlyph->width*this->size, 0);
     217        glVertex2d(posX+tmpGlyph->width*this->getSizeY2D(), 0);
    219218
    220219        glEnd();
    221220        glEndList();
    222         posX += tmpGlyph->advance * this->size;
     221        posX += tmpGlyph->advance * this->getSizeY2D();
    223222      }
    224223      ++tmpText;
  • trunk/src/lib/graphics/text_engine/text.h

    r5767 r5768  
    5454    void setColor(float r, float g, float b) { this->color = Vector(r, g, b); };
    5555    /** sets the Size of the Font */
    56     void setSize(float size) { this->setSizeY2D(this->size = size); };
     56    void setSize(float size) { this->setSizeY2D(size); };
    5757    /** @returns the Size of the Text */
    5858//    void getSize(float &x, float& y) const { return this->size; };
     
    7373    Vector            color;          //!< The color of the font.
    7474    float             blending;       //!< The blending intensity.
    75     float             size;           //!< The size of the Font.
    7675};
    7776
Note: See TracChangeset for help on using the changeset viewer.