Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/graphics/importer/texture.cc @ 3863

Last change on this file since 3863 was 3863, checked in by bensch, 19 years ago

orxonox/trunk: efence compile support, minor changes at animation, and texture has now only support for sdl-image

File size: 3.6 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER
17
18#include "texture.h"
19
20#include "graphics_engine.h"
21
22/**
23   \brief Constructor for a Texture
24*/
25Texture::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*/
36Texture::Texture(const char* imageName)
37{
38  this->pImage = new Image;
39  this->pImage->data = NULL;
40  this->map = NULL;
41  this->texture = 0;
42  this->loadImage(imageName);
43} 
44
45/**
46   \brief Destructor of a Texture
47   
48   Frees Data, and deletes the textures from GL
49*/
50Texture::~Texture(void)
51{
52  if (this->pImage->data)
53    delete []this->pImage->data;
54  delete pImage;
55  if (this->texture)
56    glDeleteTextures(1, &this->texture);
57}
58
59/**
60   \brief a Simple function that switches two char values
61   \param a The first value
62   \param b The second value
63*/
64void Texture::swap (unsigned char &a, unsigned char &b)
65{
66  unsigned char temp;
67  temp = a;
68  a    = b;
69  b    = temp;
70}
71
72
73/**
74   \brief Loads a Texture to the openGL-environment.
75   \param pImage The Image to load to openGL
76*/
77bool Texture::loadTexToGL (Image* pImage)
78{
79  if (GraphicsEngine::texturesEnabled)
80    {
81      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);
90     
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);     
93    }
94}
95
96/**
97   \brief loads an Image from a file to a Texture
98   \param imageName The image to load
99*/
100bool Texture::loadImage(const char* imageName)
101{
102  if (GraphicsEngine::texturesEnabled)
103    {
104      if (imageName)
105        {
106          this->map=IMG_Load(imageName);
107          if(!map)
108            {
109              PRINTF(1)("IMG_Load: %s\n", IMG_GetError());
110              return false;
111            }
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            }
123
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]);
136         
137          this->loadTexToGL (this->pImage);
138          SDL_FreeSurface(map);
139          pImage->data = NULL;
140        }
141      else
142        {
143          PRINTF(2)("Image not Found: %s\n", imageName);
144          return false;
145        }
146    }
147}
Note: See TracBrowser for help on using the repository browser.