Debian Codegraph @

src/texture.cpp

raw symbols SCIP
1 /***************************************************************************
2 texture.cpp - description
3 -------------------
4 begin : july 2nd, 2004
5 copyright : (C) 2004-2008 by Duong Khang NGUYEN
6 email : neoneurone @ gmail com
7
8 $Id: texture.cpp 450 2010-11-21 19:11:43Z neoneurone $
9 ***************************************************************************/
10
11 /***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * any later version. *
17 * *
18 ***************************************************************************/
19
20 // Useful enumerations
21 #include "opencity_direction.h"
22 #include "opencity_structure_type.h"
23
24 // OpenCity headers
25 #include "texture.h"
26 #include "buildinglayer.h"
27 #include "structure.h"
28
29 // Global settings
30 #include "globalvar.h"
31 extern GlobalVar gVars;
32
33 // Libraries headers
34 #include "SDL_image.h"
35
36 // Class static variable implementation
37 map<string, pair<GLuint, uint> > Texture::mmapTextureCache;
38
39
40 /*=====================================================================*/
41 Texture::Texture():
42 muiWidth( 0 ),
43 muiHeight( 0 ),
44 muiTextureName( 0 )
45 {
46 OPENCITY_DEBUG("Ctor");
47 }
48
49
50 /*=====================================================================*/
51 Texture::Texture( const string& rcFile )
52 {
53 OPENCITY_DEBUG("Pctor 1");
54 msTextureFile = Texture::_ResolveRelativePath( rcFile );
55 muiTextureName = Texture::_Load( msTextureFile, muiWidth, muiHeight );
56 }
57
58
59 /*=====================================================================*/
60 Texture::Texture( const string& rcFile, bool b3d )
61 {
62 OPENCITY_DEBUG("Pctor 2");
63 msTextureFile = Texture::_ResolveRelativePath( rcFile );
64 muiTextureName = Texture::_Load3D( msTextureFile, muiWidth, muiHeight );
65 }
66
67
68 /*=====================================================================*/
69 Texture::~Texture()
70 {
71 OPENCITY_DEBUG("dtor");
72
73 // Remove the unreferenced texture from cache
74 if (--Texture::mmapTextureCache[ msTextureFile ].first <= 0) {
75 Texture::mmapTextureCache.erase( msTextureFile );
76 glDeleteTextures( 1, &muiTextureName );
77 }
78 }
79
80
81 /*=====================================================================*/
82 GLuint
83 Texture::GetName() const
84 {
85 return muiTextureName;
86 }
87
88
89 //========================================================================
90 // PRIVATE STATIC METHODS
91 //========================================================================
92 const GLuint
93 Texture::_Load( const string& rcFile )
94 {
95 uint w, h;
96 GLuint uiName = 0;
97
98 // IF the requested texture is in the cache AND valid THEN
99 if ((Texture::mmapTextureCache.find( rcFile ) != Texture::mmapTextureCache.end())
100 and (glIsTexture( Texture::mmapTextureCache[ rcFile ].first )) )
101 {
102 // OPENCITY_DEBUG( "Texture cache hit for: " << rcFile << " with name: " << tex);
103 uiName = Texture::mmapTextureCache[ rcFile ].first;
104 Texture::mmapTextureCache[ rcFile ].second++;
105 }
106 else {
107 uiName = Texture::_Load( rcFile, w, h );
108 Texture::mmapTextureCache[ rcFile ] = std::make_pair(uiName, 1);
109 }
110
111 return uiName;
112 }
113
114
115 /*=====================================================================*/
116 const GLuint
117 Texture::_Load3D( const string& rcFile )
118 {
119 uint w, h;
120 GLuint uiName = 0;
121
122 // IF the requested texture is in the cache AND valid THEN
123 if ((Texture::mmapTextureCache.find( rcFile ) != Texture::mmapTextureCache.end())
124 and (glIsTexture( Texture::mmapTextureCache[ rcFile ].first )) )
125 {
126 // OPENCITY_DEBUG( "Texture cache hit for: " << rcFile << " with name: " << tex);
127 uiName = Texture::mmapTextureCache[ rcFile ].first;
128 Texture::mmapTextureCache[ rcFile ].second++;
129 }
130 else {
131 uiName = Texture::_Load3D( rcFile, w, h );
132 Texture::mmapTextureCache[ rcFile ] = std::make_pair(uiName, 1);
133 }
134
135 return uiName;
136 }
137
138
139 /*=====================================================================*/
140 const GLuint
141 Texture::_Load
142 (
143 const string& rcFile,
144 uint& ruiW,
145 uint& ruiH
146 )
147 {
148 SDL_Surface* pImage = NULL;
149 SDL_Surface* pMirror = NULL;
150 GLuint uiTexture = 0; // tell glIsTexture that this is not a texture name
151
152 OPENCITY_DEBUG( "Loading texture file: " << rcFile );
153
154 // Load the image to the SDL surface
155 pImage = IMG_Load( rcFile.c_str() );
156 if (pImage == NULL) {
157 OPENCITY_FATAL( "Texture file \"" << rcFile << "\" not found");
158 abort();
159 }
160
161 // Return the width and height
162 ruiW = pImage->w;
163 ruiH = pImage->h;
164
165 // Check the bytes per pixel
166 // cout << "Bytes per pixel: " << (int)pImage->format->BytesPerPixel << endl;
167 assert( pImage->format->BytesPerPixel == 4 );
168
169 // note: the texture coordinates are flipped in the vertical direction
170 // because the SDL_image library reads the image's pixels
171 // from the upper left corner, left to right, to the bottom right
172 // corner whereas the glTexImage2D builds the texture from
173 // the bottom left corner, left to right, to the upper right corner
174 // Take the horizontal mirror of current image
175 // pMirror = Texture::HorizontalMirror( pConverted );
176 pMirror = Texture::HorizontalMirror( pImage );
177
178 // convert the surface to texture
179 // create the new texture if needed
180 Texture::Surface2Texture( pMirror, uiTexture );
181 SDL_FreeSurface( pImage );
182 SDL_FreeSurface( pMirror );
183
184 return uiTexture;
185 }
186
187
188 /*=====================================================================*/
189 const GLuint
190 Texture::_Load3D
191 (
192 const string& rcFile,
193 uint& ruiW,
194 uint& ruiH
195 )
196 {
197 OPENCITY_DEBUG( rcFile.c_str() );
198
199 // Load the image to the SDL surface
200 SDL_Surface* pImage = IMG_Load( rcFile.c_str() );
201 assert( pImage != NULL );
202
203 // Return the width and height
204 ruiW = pImage->w;
205 ruiH = pImage->h;
206
207 // Load the texture
208 GLuint uiTexture = 0;
209 uint bpp = pImage->format->BytesPerPixel;
210 if (bpp == 3) {
211 OPENCITY_DEBUG( "3D texture format: GL_RGB" );
212 Texture::Surface2Texture3D( pImage, uiTexture, GL_RGB );
213 }
214 else if (bpp == 4) {
215 OPENCITY_DEBUG( "3D texture format: GL_RGBA" );
216 Texture::Surface2Texture3D( pImage, uiTexture, GL_RGBA );
217 }
218
219 SDL_FreeSurface( pImage );
220
221 assert( uiTexture != 0 );
222 return uiTexture;
223 }
224
225
226 /*=====================================================================*/
227 SDL_Surface*
228 Texture::HorizontalMirror(
229 const SDL_Surface* const psurface
230 )
231 {
232 SDL_Surface* psNew = NULL;
233 int h, iBytesPerRow;
234 char* pPixelDest;
235 char* pPixelSrc;
236
237 // IF the surface needs locking THEN
238 if (SDL_MUSTLOCK( psurface )) {
239 SDL_LockSurface( const_cast<SDL_Surface*>(psurface) );
240 }
241
242 // Create the new surface, then lock it
243 psNew = SDL_CreateRGBSurface(
244 SDL_SWSURFACE,
245 psurface->w,
246 psurface->h,
247 psurface->format->BitsPerPixel,
248 psurface->format->Rmask,
249 psurface->format->Gmask,
250 psurface->format->Bmask,
251 psurface->format->Amask );
252 assert( psNew != NULL );
253 SDL_LockSurface( psNew );
254
255 // Variables initialization
256 iBytesPerRow = psurface->w * psurface->format->BytesPerPixel;
257 pPixelDest = (char*)psNew->pixels;
258 pPixelSrc = (char*)psurface->pixels + ((psurface->h-1) * iBytesPerRow);
259 //debug cout << "BPR: " << iBytesPerRow << endl;
260
261 // Copy the pixels
262 for ( h = psurface->h-1; h >= 0; h-- ) {
263 memcpy( pPixelDest, pPixelSrc, iBytesPerRow );
264 pPixelDest += iBytesPerRow;
265 pPixelSrc -= iBytesPerRow;
266 //debug cout << "bla: " << h << endl;
267 }
268
269 SDL_UnlockSurface( psNew );
270
271 // IF the surface needs locking THEN
272 if (SDL_MUSTLOCK( psurface )) {
273 SDL_UnlockSurface( const_cast<SDL_Surface*>(psurface) );
274 }
275
276 return psNew;
277 }
278
279
280 /*=====================================================================*/
281 void
282 Texture::Surface2Texture(
283 const SDL_Surface* const psurface,
284 GLuint& ruiTexture )
285 {
286 // Delete the existing texture
287 if (glIsTexture( ruiTexture ) == GL_TRUE)
288 glDeleteTextures( 1, &ruiTexture );
289
290 // Create the texture to hold the image
291 glGenTextures( 1, &ruiTexture );
292 glBindTexture( GL_TEXTURE_2D, ruiTexture );
293 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
294 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
295
296 SDL_LockSurface( const_cast<SDL_Surface*>(psurface) );
297
298 // If the image doesn't have the correct size then scale it before converting
299 uint glW, glH;
300 if (Texture::_GetCorrectSize( psurface->w, psurface->h, glW, glH )) {
301 OPENCITY_DEBUG("Texture's size has been fixed");
302 void* pPixels = malloc( glW * glH * psurface->format->BytesPerPixel );
303 assert( pPixels != NULL );
304
305 // Scale the input image
306 (void)gluScaleImage(
307 GL_RGBA, // Image format
308 psurface->w, // The image's size
309 psurface->h,
310 GL_UNSIGNED_BYTE, // The data type of the input pixel
311 psurface->pixels, // The pixels' data
312 glW, // The wanted scaled image's size
313 glH,
314 GL_UNSIGNED_BYTE, // The data type of the output pixel
315 pPixels // The pointer which will holds the output data
316 );
317
318 // Convert the scaled image to texture
319 glTexImage2D(
320 GL_TEXTURE_2D, // texture 2D
321 0, // base image
322 GL_RGBA, // internal format
323 glW, // texture width, must be 2n >= 64
324 glH, // texture height, must be 2n >= 64
325 0, // no border
326 GL_RGBA, // pixel format
327 GL_UNSIGNED_BYTE, // pixel data format
328 pPixels // point to the pixels' data
329 );
330
331 free( pPixels );
332 pPixels = NULL; // Safe
333 }
334 else {
335 // Convert the surface to the OpenGL texture
336 glTexImage2D(
337 GL_TEXTURE_2D, // texture 2D
338 0, // base image
339 GL_RGBA, // internal format
340 psurface->w, // texture width, must be 2n >= 64
341 psurface->h, // texture height, must be 2n >= 64
342 0, // no border
343 GL_RGBA, // pixel format
344 GL_UNSIGNED_BYTE, // pixel data format
345 psurface->pixels // point to the pixels' data
346 );
347 }
348
349 SDL_UnlockSurface( const_cast<SDL_Surface*>(psurface) );
350
351 // GL error checking
352 GLenum glError = glGetError();
353 if (glError != GL_NO_ERROR) {
354 GLint maxTexSize;
355 glGetIntegerv( GL_MAX_TEXTURE_SIZE, &maxTexSize );
356
357 OPENCITY_DEBUG( "CRITIC GL ERROR while loading texture" );
358 cout << "Max texture size is: " << maxTexSize << endl;
359 cout << "GL error code: 0x";
360 cout.flags( cout.hex );
361 cout << glError << endl;
362 cout.flags( cout.dec );
363 }
364 }
365
366
367 /*=====================================================================*/
368 void
369 Texture::Surface2Texture3D
370 (
371 const SDL_Surface* const psurface,
372 GLuint& ruiTexture,
373 GLint format
374 )
375 {
376 /* old version, kept for reference, september 05th, 2008
377 // Dynamically look up the glTexImage3D extension
378 PFNGLTEXIMAGE3DEXTPROC glTexImage3D = (PFNGLTEXIMAGE3DEXTPROC) SDL_GL_GetProcAddress("glTexImage3DEXT");
379 if (glTexImage3D == NULL) {
380 OPENCITY_FATAL( "glTexImage3D function not found" );
381 abort();
382 }
383 */
384
385 // Delete the existing texture
386 if (glIsTexture( ruiTexture ) == GL_TRUE)
387 glDeleteTextures( 1, &ruiTexture );
388
389 // Create the texture to hold the image
390 glGenTextures( 1, &ruiTexture );
391 glBindTexture( GL_TEXTURE_3D, ruiTexture );
392 SDL_LockSurface( const_cast<SDL_Surface*>(psurface) );
393
394 // If the image doesn't have the correct size then scale it before converting
395 // Convert the surface to the OpenGL texture
396 // NOTE: the texture internal format is the same as the pixels' one
397 gVars.gpExtensionMgr->glTexImage3D(
398 GL_TEXTURE_3D, // texture 3D
399 0, // base image
400 format, // internal format
401 psurface->w, // texture width, must be 2n >= 64
402 psurface->w, // texture width, must be 2n >= 64 // The 3D texture is 64x64x64 RGBA
403 psurface->h / psurface->w, // Depth
404 0, // no border
405 format, // pixel format
406 GL_UNSIGNED_BYTE, // pixel data format
407 psurface->pixels // point to the pixels' data
408 );
409
410 SDL_UnlockSurface( const_cast<SDL_Surface*>(psurface) );
411
412 // GL error checking
413 GLenum glError = glGetError();
414 if (glError != GL_NO_ERROR) {
415 GLint maxTexSize;
416 glGetIntegerv( GL_MAX_TEXTURE_SIZE, &maxTexSize );
417
418 OPENCITY_DEBUG( "CRITIC GL ERROR while loading texture" );
419 cout << "Max texture size is: " << maxTexSize << endl;
420 cout << "GL error code: 0x";
421 cout.flags( cout.hex );
422 cout << glError << endl;
423 cout.flags( cout.dec );
424 }
425 }
426
427
428 /*=====================================================================*/
429 void
430 Texture::Building2Texture
431 (
432 const BuildingLayer* const pLayer,
433 GLuint& ruiTexture
434 )
435 {
436 #define BYTE_PER_PIXEL 4
437
438 assert( pLayer != NULL );
439
440 // Delete the existing texture
441 if (glIsTexture( ruiTexture ) == GL_TRUE)
442 glDeleteTextures( 1, &ruiTexture );
443
444 // Create the texture to hold the image
445 glGenTextures( 1, &ruiTexture );
446 glBindTexture( GL_TEXTURE_2D, ruiTexture );
447 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
448 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
449
450 // Create the minimap data
451 uint i = 0;
452 Structure* pStruct = NULL;
453 OC_BYTE* pData = (OC_BYTE*)malloc( gVars.guiCityWidth * gVars.guiCityLength * BYTE_PER_PIXEL );
454 assert( pData != NULL );
455 for (int l = gVars.guiCityLength-1; l >= 0; l--) {
456 for (uint w = 0; w < gVars.guiCityWidth; w++, i+=BYTE_PER_PIXEL) {
457 pStruct = pLayer->GetStructure( w, l );
458 if (pStruct == NULL) {
459 pData[i] = 0x50; pData[i+1] = 0xA0; pData[i+2] = 0x50; pData[i+3] = 0xE5;
460 continue;
461 }
462
463 switch (pStruct->GetCode()) {
464 case OC_STRUCTURE_RES:
465 pData[i] = 0x50; pData[i+1] = 0xFF; pData[i+2] = 0x50; pData[i+3] = 0xE5;
466 break;
467 case OC_STRUCTURE_COM:
468 pData[i] = 0x50; pData[i+1] = 0x50; pData[i+2] = 0xFF; pData[i+3] = 0xE5;
469 break;
470 case OC_STRUCTURE_IND:
471 pData[i] = 0xFF; pData[i+1] = 0xFF; pData[i+2] = 0x50; pData[i+3] = 0xE5;
472 break;
473 default:
474 pData[i] = 0x50; pData[i+1] = 0x50; pData[i+2] = 0x50; pData[i+3] = 0xE5;
475 break;
476 }
477 }
478 }
479
480 // If the image doesn't have the correct size then scale it before converting
481 uint glW, glH;
482 if (Texture::_GetCorrectSize( gVars.guiCityWidth, gVars.guiCityLength, glW, glH )) {
483 OPENCITY_DEBUG("Texture's size has been fixed");
484 void* pPixels = malloc( glW * glH * BYTE_PER_PIXEL );
485 assert( pPixels != NULL );
486
487 // Scale the input image
488 (void)gluScaleImage(
489 GL_RGBA, // Image format
490 gVars.guiCityWidth, // The minimap size
491 gVars.guiCityLength,
492 GL_UNSIGNED_BYTE, // The data type of the input pixel
493 pData, // The minimap data
494 glW, // The expected scaled image's size
495 glH,
496 GL_UNSIGNED_BYTE, // The data type of the output pixel
497 pPixels // The pointer which will holds the output data
498 );
499
500 // Convert the scaled image to texture
501 glTexImage2D(
502 GL_TEXTURE_2D, // Texture 2D
503 0, // Base image
504 GL_RGBA, // Internal format
505 glW, // Texture width, must be 2n >= 64
506 glH, // Texture height, must be 2n >= 64
507 0, // No border
508 GL_RGBA, // Pixel format
509 GL_UNSIGNED_BYTE, // Pixel data format
510 pPixels // point to the pixels' data
511 );
512
513 free( pPixels );
514 pPixels = NULL; // Safe
515 }
516 else {
517 // Convert directly the minimap data to the OpenGL texture
518 glTexImage2D(
519 GL_TEXTURE_2D, // Texture 2D
520 0, // Base image
521 GL_RGBA, // Internal format
522 gVars.guiCityWidth, // Texture width, must be 2n >= 64
523 gVars.guiCityLength,// Texture height, must be 2n >= 64
524 0, // No border
525 GL_RGBA, // Pixel format
526 GL_UNSIGNED_BYTE, // Pixel data format
527 pData // point to the pixels' data
528 );
529 }
530
531 free( pData );
532 pData = NULL;
533 }
534
535
536 /*=====================================================================*/
537 bool
538 Texture::_GetCorrectSize(
539 const uint w, const uint h,
540 uint & rW, uint & rH )
541 {
542 rW = 64;
543 while (rW < w) rW = rW << 1;
544
545 rH = 64;
546 while (rH < h) rH = rH << 1;
547
548 return (w != rW || h != rH);
549 }
550
551
552 /*=====================================================================*/
553 string
554 Texture::_ResolveRelativePath(const string& rcsPath)
555 {
556 string output = rcsPath;
557
558 // Search for "/../"
559 string::size_type start, end = 0;
560 while ((end = output.find("/../")) != output.npos) {
561 if (end <= 0)
562 break;
563
564 // Search for the first "/" before "/../"
565 start = output.rfind("/", end-1);
566 if (start == output.npos)
567 break;
568
569 // Resolve the relative path
570 output.erase(start, end+3-start);
571 }
572
573 return output;
574 }
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601