File: myx_gc_texture.cpp

package info (click to toggle)
mysql-query-browser 1.1.6-1sarge0
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 36,320 kB
  • ctags: 24,680
  • sloc: pascal: 203,479; xml: 136,561; ansic: 47,502; cpp: 28,926; sh: 12,433; objc: 4,823; java: 1,849; php: 1,485; python: 1,225; sql: 1,128; makefile: 872
file content (293 lines) | stat: -rw-r--r-- 9,551 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* Copyright (C) 2004 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/**
 * @file myx_gc_texture.cpp 
 * @brief Implementation of a texture class.
 * 
 */

#ifdef _WINDOWS
  #include <windows.h>
#else
#endif // ifdef _WINDOWS

#include <gl/gl.h>
#include <gl/glu.h>
#include <math.h>

#include "myx_gc_texture.h"
#include "myx_gc_canvas.h"
#include "myx_gc_utilities.h"

//----------------- CGCTexture -----------------------------------------------------------------------------------------

CGCTexture::CGCTexture(CTextureManager* Controller, const string& Filename, const string& ID, GLenum WrapModeS, GLenum WrapModeT, 
                       GLenum MinFilter, GLenum MagFilter, int Dimensions, GLenum TextureMode)
{
  FManager = Controller;
  FLoaded = false;
  FName = ID;
  FFilename = Filename;
  FWrapModeS = WrapModeS;
  FWrapModeT = WrapModeT;
  FMinFilter = MinFilter;
  FMagFilter = MagFilter;
  FTarget = Dimensions == 1 ? GL_TEXTURE_1D : GL_TEXTURE_2D;
  FMode = TextureMode;

  glGenTextures(1, &FHandle);
}

//----------------------------------------------------------------------------------------------------------------------

CGCTexture::~CGCTexture(void)
{
  glDeleteTextures(1, &FHandle);
}

//----------------------------------------------------------------------------------------------------------------------

/**
 *  Rounds the given value up to the next power of two boundary.
 *
 *  @param Value The value to round up.
 *  @return Returns the rounded value.
 */
int RoundUpToPowerOf2(int Value)
{
#define LOG2(value) (log(value) / log(2.0))

  double LogTwo = LOG2((double) Value);
  if (floor(LogTwo) < LogTwo)
    return (int) floor(pow(2.0, floor(LogTwo) + 1));
  else
    return Value;

#undef LOG2
}

//----------------------------------------------------------------------------------------------------------------------

/**
 *  Delay loads texture data. Called from ActivateTexture, that is, when the texture is used the first time.
 */
void CGCTexture::LoadTexture(void)
{
  FLoaded = true;
  TImage* Image = LoadPNG(FFilename);

  if (Image != NULL)
  {
    int ActualWidth = RoundUpToPowerOf2(Image->Width);
    int ActualHeight = RoundUpToPowerOf2(Image->Height);
    unsigned char* Buffer;
    bool FreeBuffer = false;

    GLenum Format = GL_RGB;
    switch (Image->ColorType)
    {
      case COLOR_TYPE_PALETTE:
        {
          Format = GL_COLOR_INDEX;
          break;
        };
      case COLOR_TYPE_GRAY:
        {
          Format = GL_LUMINANCE;
          break;
        };
      case COLOR_TYPE_GRAY_ALPHA:
        {
          Format = GL_LUMINANCE_ALPHA;
          break;
        };
      case COLOR_TYPE_RGB:
        {
          Format = GL_RGB;
          break;
        };
      case COLOR_TYPE_RGB_ALPHA:
        {
          Format = GL_RGBA;
          break;
        };
    };

    if (ActualWidth != Image->Width || ActualHeight != Image->Height)
    {
      FreeBuffer = true;
      Buffer = (unsigned char*) malloc(ActualWidth * ActualHeight * Image->Channels);
      gluScaleImage(Format, Image->Width, Image->Height, GL_UNSIGNED_BYTE, Image->Data, ActualWidth, ActualHeight, 
        GL_UNSIGNED_BYTE, Buffer);
    }
    else
      Buffer = Image->Data;
                 
    if (FTarget == GL_TEXTURE_1D)
    {
      if ((FMinFilter == GL_NEAREST) || (FMinFilter == GL_LINEAR))
        glTexImage1D(GL_TEXTURE_1D, 0, Format, ActualWidth, 0, Format, GL_UNSIGNED_BYTE, Buffer);
      else
        gluBuild1DMipmaps(GL_TEXTURE_1D, Format, ActualWidth, Format, GL_UNSIGNED_BYTE, Buffer);
    }
    else
    {
      if ((FMinFilter == GL_NEAREST) || (FMinFilter == GL_LINEAR))
        glTexImage2D(GL_TEXTURE_2D, 0, Format, ActualWidth, ActualHeight, 0, Format, GL_UNSIGNED_BYTE, Buffer);
      else
        gluBuild2DMipmaps(GL_TEXTURE_2D, Format, ActualWidth, ActualHeight, Format, GL_UNSIGNED_BYTE, Buffer);
    };

    if (Image != NULL)
      FreeImage(Image);
    if (FreeBuffer)
      free(Buffer);
  };
}

//----------------------------------------------------------------------------------------------------------------------

// Activates this texture in OpenGL so all following vertex definitions are textured using this texture.
// If the texture has not been loaded yet it will be done now. Additionally, texture mode is enabled in OpenGL.
void CGCTexture::ActivateTexture(void)
{                                                          
  glEnable(FTarget);
  glBindTexture(FTarget, FHandle); 

  if (!FLoaded)
    LoadTexture();

  glTexParameteri(FTarget, GL_TEXTURE_WRAP_S, FWrapModeS);
  if (FTarget == GL_TEXTURE_2D)
    glTexParameteri(FTarget, GL_TEXTURE_WRAP_T, FWrapModeT);
	glTexParameteri(FTarget, GL_TEXTURE_MIN_FILTER, FMinFilter);
	glTexParameteri(FTarget, GL_TEXTURE_MAG_FILTER, FMagFilter);

  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, FMode);
}

//----------------------------------------------------------------------------------------------------------------------

// Deactivates this texture and the texture mode in OpenGL.
void CGCTexture::DeactivateTexture(void)
{
  glDisable(FTarget);
  glBindTexture(FTarget, 0);
}

//----------------- CTextureManager ------------------------------------------------------------------------------------

CTextureManager InternalTextureManager; // Singleton texture manager instance.

CTextureManager* TextureManager()
{
  return &InternalTextureManager;
}

//----------------------------------------------------------------------------------------------------------------------

CTextureManager::~CTextureManager(void)
{
  ClearTextures();
}

//----------------------------------------------------------------------------------------------------------------------

// Looks throught the textures and attempts to find one with the given name.
CGCTexture* CTextureManager::FindTexture(const string& Name)
{
  CTextureIterator Iterator = FTextures.find(Name);
  if (Iterator == FTextures.end())
    return NULL;
  else
    return Iterator->second;
}

//----------------------------------------------------------------------------------------------------------------------

// Creates a new texture entry and adds the entry to the texture list.
// No image data is loaded yet. This will happen when the texture is used the first time.
void CTextureManager::CreateTextureEntry(const string& Filename, const string& ID, const string& WrapH, const string& WrapV, 
                                         const string& MinificationFilterStr, const string& MagnificationFilterStr,
                                         int Dimensions, const string& Mode)
{
  // These both constants are only available with OpenGL 1.2 or up and are not defined in the standard gl.h header file.
  #define GL_CLAMP_TO_BORDER 0x812D
  #define GL_CLAMP_TO_EDGE   0x812F

  static stdext::hash_map<string, GLenum> Mapper;
  stdext::hash_map<string, GLenum>::const_iterator Iterator;

  // Fill our lookup table first if not yet done.
  if (Mapper.size() == 0)
  {
    Mapper["clamp"] = GL_CLAMP;
    Mapper["clamp-to-border"] = GL_CLAMP_TO_BORDER;
    Mapper["clamp-to-edge"] = GL_CLAMP_TO_EDGE;
    Mapper["repeat"] = GL_REPEAT;
    Mapper["nearest"] = GL_NEAREST;
    Mapper["linear"] = GL_LINEAR;
    Mapper["nearest-mipmap-nearest"] = GL_NEAREST_MIPMAP_NEAREST;
    Mapper["linear-mipmap-nearest"] = GL_LINEAR_MIPMAP_NEAREST;
    Mapper["nearest-mipmap-linear"] = GL_NEAREST_MIPMAP_LINEAR;
    Mapper["linear-mipmap-linear"] = GL_LINEAR_MIPMAP_LINEAR;
    Mapper["decal"] = GL_DECAL;
    Mapper["modulate"] = GL_MODULATE;
    Mapper["blend"] = GL_BLEND;
    Mapper["replace"] = GL_REPLACE;
  };

  GLenum WrapModeS = GL_CLAMP;
  Iterator = Mapper.find(WrapH);
  if (Iterator != Mapper.end())
    WrapModeS = Iterator->second;

  GLenum WrapModeT = GL_CLAMP;
  Iterator = Mapper.find(WrapV);
  if (Iterator != Mapper.end())
    WrapModeT = Iterator->second;

  GLenum MinFilter = GL_NEAREST;
  Iterator = Mapper.find(MinificationFilterStr);
  if (Iterator != Mapper.end())
    MinFilter = Iterator->second;

  GLenum MagFilter = GL_NEAREST;
  Iterator = Mapper.find(MagnificationFilterStr);
  if (Iterator != Mapper.end())
    MagFilter = Iterator->second;

  GLenum TextureMode = GL_DECAL;
  Iterator = Mapper.find(Mode);
  if (Iterator != Mapper.end())
    TextureMode = Iterator->second;

  FTextures[ID] = new CGCTexture(this, Filename, ID, WrapModeS, WrapModeT, MinFilter, MagFilter, Dimensions, TextureMode);
}

//----------------------------------------------------------------------------------------------------------------------

void CTextureManager::ClearTextures(void)
{
  for (CTextureIterator Iterator = FTextures.begin(); Iterator != FTextures.end(); Iterator++)
    delete Iterator->second;

  FTextures.clear();
}

//----------------------------------------------------------------------------------------------------------------------