libmoldeo (Moldeo 1.0 Core)  1.0
libmoldeo es el conjunto de objetos y funciones, que permiten ejecutar las operaciones básicas de la plataforma Moldeo, y que compone su núcleo.
moTextureManager.cpp
Ir a la documentación de este archivo.
1 /*******************************************************************************
2 
3  moTextureManager.cpp
4 
5  ****************************************************************************
6  * *
7  * This source is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This code is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15  * General Public License for more details. *
16  * *
17  * A copy of the GNU General Public License is available on the World *
18  * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
19  * obtain it by writing to the Free Software Foundation, *
20  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  * *
22  ****************************************************************************
23 
24  Copyright(C) 2006 Fabricio Costa
25 
26  Authors:
27  Fabricio Costa
28  Andres Colubri
29 
30 *******************************************************************************/
31 
32 #include "moTextureManager.h"
33 #include <FreeImage.h>
34 #include <moDataManager.h>
35 #include <moFileManager.h>
36 
37 #include "moArray.h"
38 moDefineDynamicArray(moTextureBuffers)
39 moDefineDynamicArray(moTextureFrames)
40 
41 void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message);
42 
48 void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message) {
49 
50  if(fif != FIF_UNKNOWN) {
51  //printf("%s Format\n", FreeImage_GetFormatFromFIF(fif));
52  }
53  moDebugManager::Error(moText("FreeImage error:") + moText(message));
54 
55 }
56 
57 
58 //===========================================
59 //
60 // moTextureBuffer
61 //
62 //===========================================
63 
65 
66  //m_type = MO_TYPE_TEXTUREBUFFER;
67  m_ImagesProcessed = slots;
68  m_ActualImage = 0;
69  m_pResourceManager = NULL;
70  m_pDirectory = NULL;
71  m_bLoadCompleted = false;
72  m_pBufferLevels = new moTextureFrames* [100];
73  for( int i=0; i<100; i++ ) {
74  m_pBufferLevels[i] = new moTextureFrames [10];
75  for( int j = 0; j<10; j++ ) {
76  m_pBufferLevels[i][j].Init( 0 , NULL );
77  }
78  }
79 
80  LevelDiagram = NULL;
83  LevelDiagram = new MObyte [ 100 * 100 * 3];
84 
85  m_texture_array = 0;
86 }
87 
89  Finish();
90 }
91 
93 
94  if (!m_pResourceManager)
95  return false;
96 
97 
98 
99  return m_bInitialized;
100 }
101 
102 MOboolean
103 moTextureBuffer::Init( moText p_foldername, moTextureBufferFormat p_bufferformat, int p_pattern_width, int p_pattern_height, moResourceManager* p_pResourceManager ) {
104 
105  moText str_format;
106 
107  pattern_width = p_pattern_width;
108  pattern_height = p_pattern_height;
109 
110  if (p_bufferformat==MO_TEXTURE_BUFFER_FORMAT_LEVELS) {
111 
112  str_format = "LEVELS";
113 
114  } else if (p_bufferformat==MO_TEXTURE_BUFFER_FORMAT_PATTERNS) {
115 
116  str_format = "PATTERNS";
117 
118  } else if (p_bufferformat==MO_TEXTURE_BUFFER_FORMAT_LAYERS) {
119 
120  str_format = "LAYERS";
121 
122  }
123 
124  return Init( p_foldername, str_format, p_pResourceManager );
125 }
126 
127 MOboolean
128 moTextureBuffer::Init( moText p_foldername, moText p_bufferformat, moResourceManager* p_pResourceManager ) {
129  m_pResourceManager = p_pResourceManager;
130 
131  m_FolderName = p_foldername;
133  m_BufferFormat = p_bufferformat;
134 
135 
137 
138  if (!m_pDirectory) return false;
139 
140  //BuildEmpty( width, height);
141 
142  /*
143  moShaderManager* SM = m_pResourceManager->GetShaderMan();
144  moTextureManager* TM = m_pResourceManager->GetTextureMan();
145 
146  m_pShaderCopy = SM->GetShader(SM->GetShaderIndex(moText("shaders/Copy.cfg"),true) );
147  */
148  m_Frames.Init( 0, NULL);
149 
150  max_luminance = 0;//100
151  min_luminance = 100;//0
152 
153  max_contrast = 0;
154  min_contrast = 0;
155 
156  for( int L=0; L<100*100*3; L++) {
157  LevelDiagram[L] = 0;
158  }
159 
160  m_bInitialized = true;
161 
162  return Init();
163 }
164 
166  for(MOuint i=0; i<m_Frames.Count(); i++) {
167  moTextureMemory* pTextureMemory = m_Frames[i];
168  if (pTextureMemory)
169  pTextureMemory->Finish();
170  delete pTextureMemory;
171  }
172  m_Frames.Empty();
173 
174  for(int j=0 ; j<100; j++) {
175  if (m_pBufferLevels[j]!=NULL) {
176  for(int k=0 ; k<10; k++) {
177  m_pBufferLevels[j][k].Empty();
178  }
179  delete [] m_pBufferLevels[j];
180  }
181  }
182  m_pBufferLevels = NULL;
183 
184  if (LevelDiagram) {
185  delete [] LevelDiagram;
186  LevelDiagram = NULL;
187  }
188 
189  m_pResourceManager = NULL;
190  return true;
191 }
192 
193 MOboolean
195  return m_bLoadCompleted;
196 }
197 
199 
200  //carga los frames desde los archivos
201  moFile* pFile;
202 
203  MOint counter = 0;
204 
205  if (!m_pDirectory) return false;
206 
207  if (m_ActualImage>=(MOint)m_pDirectory->GetFiles().Count()) {
208  m_bLoadCompleted = true;
209  return true;
210  }
211 
212  if (m_ActualImage==0)
213  pFile = m_pDirectory->FindFirst();
214  else
215  pFile = m_pDirectory->Find(m_ActualImage);
216 
217  if (pFile)
218  do {
219  if ( pFile->GetType()==MO_FILETYPE_LOCAL && pFile->Exists()) {
220 
221  //LOAD AND UNCOMPRESS IMAGE
222  FREE_IMAGE_FORMAT fif;
223  fif = FreeImage_GetFileType( pFile->GetCompletePath(), 0);
224 
225  if( fif == FIF_UNKNOWN ) {
226  // try to guess the file format from the file extension
227  fif = FreeImage_GetFIFFromFilename(pFile->GetCompletePath());
228  }
229 
230  if( (fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif) ) {
231  //decodificamos el archivo
232  FIBITMAP* pImage;
233  pImage = FreeImage_Load( fif, pFile->GetCompletePath(), 0);
234 
235  //ONCE LOADED SAVE ON EVERY VIDEOBUFFER
236  if (pImage) {
237 
238  //for(MOuint b = 0; b<m_VideoBuffers.Count(); b++) {
239 
240  //moVideoBuffer* pVideoBuffer = m_VideoBuffers[b];
241 
242  //if (pVideoBuffer)
243  //LoadImage( , pImage, m_ActualImage );
244  MODebug2->Push( moText("moTextureBuffer::UpdateImages > Trying to load image:") + (moText)pFile->GetCompletePath() );
245  if ( LoadImage( m_FolderName + moSlash + pFile->GetFileName() , pImage, m_ActualImage ) ) {
247  if ( m_ActualImage == (m_pDirectory->GetFiles().Count()-2) ) {
248  MODebug2->Log( moText(" ####TEXTUREBUFFER LEVEL HISTOGRAM####"));
249  moText barra;
250  moText nivel;
251  barra = moText("###################################################################################");
252 
253  for(int k=0; k<100; k++) {
254  nivel = barra;
255  nivel.Left( m_pBufferLevels[k][0].Count() );
256  MODebug2->Log( moText(" level:") + IntToStr(k) + (moText)nivel );
257  }
258  }
259  }
260 
261  //}
262  FreeImage_Unload(pImage);
263  pImage = NULL;
264 
265  }
266  }
267  }
268 
269  m_ActualImage++;
270  counter++;
271  if (counter==maxfiles && maxfiles!=(-1))
272  break;
273 
274  } while ( (pFile = m_pDirectory->FindNext()) );
275 
276  return true;
277 }
278 
279 
280 MOboolean
281 moTextureBuffer::LoadImage( moText p_ImageName, moBitmap* pImage, int indeximage ) {
282 
283  MOboolean res = false;
284  FIBITMAP* _pImage = (FIBITMAP*)pImage;
285  FIBITMAP* pImageResult = NULL;
286 // FIBITMAP* pImageCropped = NULL;
287  FIBITMAP* pImageScaled = NULL;
288 
289  if (indeximage<0) {
290  MODebug2->Error("moTextureBuffer::LoadImage > indeximage invalid or unused for: " + p_ImageName);
291  return false;
292  }
293  /*
294  if ( m_width!=FreeImage_GetWidth(_pImage) || m_height!=FreeImage_GetHeight(_pImage) ) {
295  //CROP MODE
296  pImageCropped = FreeImage_Copy( _pImage, m_XSource , m_YSource , m_XSource + m_SourceWidth , m_YSource+m_SourceHeight );
297  pImageResult = pImageCropped;
298 
299  } else
300  */
301 
302  pImageResult = _pImage;
303 
304  int w, h;
305  if ( ( FreeImage_GetWidth(_pImage) % 4 ) != 0 || ( FreeImage_GetHeight(_pImage) % 4) == 0 ) {
306 
307  w = FreeImage_GetWidth(_pImage) / 4;
308  w = w * 4;
309 
310  h = FreeImage_GetHeight(_pImage) / 4;
311  h = h * 4;
312 
313  pImageScaled = FreeImage_Rescale( pImageResult, w, h, FILTER_BICUBIC );
314  if (pImageScaled) {
315  //FreeImage_Unload(pImageResult);
316  pImageResult = pImageScaled;
317  }
318  }
319 
320 
321  //if (!GLEW_ARB_texture_non_power_of_two) {
322  if (RenderMan()->IsTextureNonPowerOf2Disabled()) {
323 
324  w = FreeImage_GetWidth(pImageResult);
325  h = FreeImage_GetHeight(pImageResult);
326 
327  int i;
328  for (i = 1; i < w; i *= 2);
329  w = i;
330 
331  for (i = 1; i < h; i *= 2);
332  h = i;
333 
334  w = momin(512,w);
335  h = momin(512,h);
336 
337  pImageScaled = FreeImage_Rescale( pImageResult, w, h, FILTER_BICUBIC );
338  if (pImageScaled) {
339  //FreeImage_Unload(pImageResult);
340  pImageResult = pImageScaled;
341  }
342  }
343 
344  FreeImage_AdjustContrast( pImageResult, 0 );
345  FreeImage_AdjustBrightness( pImageResult, 0 );
346 
347 
348 
349  //RESCALE: NOTE NECESARRY HERE
350  //quizas podamos definir un máximo para el tamaño tanto ancho como alto
351  //o como proporción
352  //forzar proporcion y esas cosas....
353  /*
354  if ( m_width != m_SourceWidth || m_height != m_SourceHeight ) {
355 
356  //FILTER_BOX Box, pulse, Fourier window, 1st order (constant) B-Spline
357  //FILTER_BILINEAR Bilinear filter
358  //FILTER_BSPLINE 4th order (cubic) B-Spline
359  //FILTER_BICUBIC Mitchell and Netravali's two-param cubic filter
360  //FILTER_CATMULLROM Catmull-Rom spline, Overhauser spline
361  //FILTER_LANCZOS3
362  pImageScaled = FreeImage_Rescale( pImageResult, m_width, m_height, FILTER_BICUBIC );
363  if (pImageScaled) {
364  FreeImage_Unload(pImageResult);
365  pImageResult = pImageScaled;
366  }
367  }
368  */
369 
370  moTextureMemory* pTextureMemory = NULL;
371 
373 
374  if (idx>-1) {
375 
376  pTextureMemory = (moTextureMemory*) m_pResourceManager->GetTextureMan()->GetTexture(idx);
377 
378  if (pTextureMemory) {
379  if (pTextureMemory->BuildFromBitmap( pImageResult, m_BufferFormat )) {
380 
381  m_Frames.Add(pTextureMemory);
382 
383  int contrastlevel = pTextureMemory->GetContrast() / 2000;
384  int luminancelevel = (int)((float)pTextureMemory->GetLuminance() / (float)2.56 ) - 1;
385 
386  MODebug2->Message( moText("moTextureBuffer::LoadImage success : ") + (moText)pTextureMemory->GetName());
387  #ifdef _DEBUG
388  MODebug2->Message( moText("moTextureBuffer::LoadImage success : ") + (moText)pTextureMemory->GetName()
389  + moText(" width:") + IntToStr(pTextureMemory->GetWidth())
390  + moText(" height:") + IntToStr(pTextureMemory->GetHeight())
391  + moText(" luminance:") + IntToStr(pTextureMemory->GetLuminance())
392  + moText(" luminancelevel:") + IntToStr(luminancelevel) +
393  + moText(" contrast:") + IntToStr(pTextureMemory->GetContrast())
394  + moText(" contrastlevel:") + IntToStr(contrastlevel) );
395  #endif
396 
397  contrastlevel = 0;
398 
399  if (luminancelevel>=100) {
400  MODebug2->Error( moText("moTextureBuffer::LoadImage Error: luminance out of bound:")+ IntToStr(pTextureMemory->GetLuminance()) );
401  } else {
402  if (max_luminance<luminancelevel) {
403  max_luminance = luminancelevel;
404  } else if (min_luminance>luminancelevel) {
405  min_luminance = luminancelevel;
406  }
407  }
408  if (contrastlevel>=10) {
409  MODebug2->Error( moText("moTextureBuffer::LoadImage Error: contrast out of bound:")+ IntToStr(pTextureMemory->GetContrast()) );
410  }
411  if (0<=luminancelevel && luminancelevel<100 && 0<=contrastlevel && contrastlevel<10) {
412 
413  m_pBufferLevels[luminancelevel][contrastlevel].Add( pTextureMemory );
414 
415  int icc = momin( m_pBufferLevels[luminancelevel][contrastlevel].Count(), 99 );
416  LevelDiagram[ (luminancelevel + icc*100)*3 ] = 250;
417  LevelDiagram[ (luminancelevel+1 + icc*100)*3 ] = 250;
418  LevelDiagram[ (luminancelevel+2 + icc*100)*3 ] = 250;
419  }
420 
421  res = true;
422  } else {
423  res = false;
424  MODebug2->Error( moText("moTextureBuffer::LoadImage Error loading image:")+(moText)pTextureMemory->GetName());
425  }
426  }
427  }
428 
429  if (pImageResult!=_pImage)
430  FreeImage_Unload(pImageResult);
431 
432  //m_nFrames = m_Frames.Count();
433  //m_fFramesPerSecond = 25.0;
434 
435  return (res);
436 }
437 /*
438 MOboolean moTextureBuffer::LoadFromVideo( moText p_moviefile ) {
439  //
440  return true;
441 }
442 */
443 
444 moTextureFrames&
445 moTextureBuffer::GetBufferPatterns( moTexture* p_ImageReference, int x, int y, int width, int height ) {
446 
447  int index = 0;
448 
449  if (p_ImageReference && x>=0 && y>=0 && width>0 && height>0 ) {
450  return m_pBufferPatterns[index+0];
451  }
452 
453  return m_pBufferPatterns[index];
454 }
455 
456 moTextureFrames&
457 moTextureBuffer::GetBufferPatterns( const moTexture& p_ImageReference, int x, int y, int width, int height ) {
458 
459  int index = 0;
460  if (p_ImageReference.GetMOId() && x>=0 && y>=0 && width>0 && height>0 ) {
461  return m_pBufferPatterns[index+0];
462  }
463  return m_pBufferPatterns[index];
464 }
465 
466 
467 moTextureFrames& moTextureBuffer::GetBufferLevels( int L, int C ) {
468 
469  if (0<=L && L<100 && 0<=C && C<10) {
470  return m_pBufferLevels[L][C];
471  } else {
472  //devolvemos el oscuro
473  return m_pBufferLevels[99][0];
474  }
475 
476 }
477 
478 
479 MOuint moTextureBuffer::GetTextureArray( int width, int height, int levels, bool force_reload ) {
480  if (!force_reload && m_texture_array) return m_texture_array;
481 
482  glActiveTexture( GL_TEXTURE0 + 5);
483  glGenTextures( 1,&m_texture_array);
484  //glEnable(GL_TEXTURE_2D_ARRAY);
485  glBindTexture( GL_TEXTURE_2D_ARRAY, m_texture_array );
486  //width = 2;
487  //height = 2;
488  levels = momin( levels, log(width)/log(2) );
489  MODebug2->Message("Creating Texture Array, levels: "+IntToStr(levels));
490  glTexStorage3D(GL_TEXTURE_2D_ARRAY, levels, GL_RGBA8, width, height, m_ImagesProcessed);
491  //int i = 0;
492  if (m_ImagesProcessed && m_Frames.Count()) {
493  for(int i=0; i<m_ImagesProcessed; i++) {
494  //
495  moTextureMemory* pTextureMemory = m_Frames[i];
496  if (pTextureMemory) {
497  FIBITMAP *pImage = (FIBITMAP*) pTextureMemory->LoadFromMemory();
498  if (pImage) {
499  FIBITMAP *pImageS;
500  pImageS = FreeImage_Rescale(pImage, width, height, FILTER_BICUBIC );
501  MOubyte* bits = FreeImage_GetBits(pImageS);
502  /*MOubyte bits[width*height*4];
503  for(int h=0; h<height;h++) {
504  for(int w=0; w<width;w++) {
505  bits[w*4 + h*width*4] = w/2;
506  bits[w*4+1+h*width*4] = h/2;
507  bits[w*4+2+h*width*4] = (w+h)/4;
508  bits[w*4+3+h*width*4] = 55+(i+1)*20;
509  }
510  }*/
511  if (bits) {
512  glTexSubImage3D(GL_TEXTURE_2D_ARRAY,
513  0, //Mipmap number
514  0, 0, i, //xoffset, yoffset, zoffset
515  width, height, 1, //width, height, depth
516  GL_RGBA, //format
517  GL_UNSIGNED_BYTE, //type
518  bits );
519  MODebug2->Message("Adding texture 2d array image:" + IntToStr(i) );
520  }
521  } else {
522  MODebug2->Error("Error adding texture 2d array image (loadfrommemory failed):" + IntToStr(i) );
523  }
524  }
525  }
526  } else if (m_ImagesProcessed) {
527 
528  MOubyte* bits = new MOubyte[width*height*4];
529  for(int h=0;h<height;h++) {
530  for(int w=0;w<width;w++) {
531  bits[w*4+h*width*4] = 255;
532  bits[w*4+h*width*4+1] = 0;
533  bits[w*4+h*width*4+2] = 0;
534  bits[w*4+h*width*4+3] = 255;
535  }
536  }
537  for(int i=0; i<m_ImagesProcessed; i++) {
538 
539  glTexSubImage3D(GL_TEXTURE_2D_ARRAY,
540  0, //Mipmap number
541  0, 0, i, //xoffset, yoffset, zoffset
542  width, height, 1, //width, height, depth
543  GL_RGBA, //format
544  GL_UNSIGNED_BYTE, //type
545  bits );
546  MODebug2->Message("Adding texture 2d array image:" + IntToStr(i) );
547  }
548  }
549  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
550  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
551  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_REPEAT);
552  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_REPEAT);
553  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, levels);
554  if (levels>1) {
555  glGenerateMipmap(GL_TEXTURE_2D_ARRAY);
556  }
557  if (m_pResourceManager) {
558  m_pResourceManager->GetGLMan()->CheckErrors("GetTextureArray");
559  }
560  glBindTexture( GL_TEXTURE_2D_ARRAY, 0 );
561  return m_texture_array;
562 }
563 
564 /*
565 */
567 
568  if ( p_i<m_Frames.Count()) {
569 
570  moTextureMemory* pTextureMemory = m_Frames[p_i];
571 
572  //GLId = pTextureMemory->GetReference();
573  pTextureMemory->GetReference();
574  return pTextureMemory->GetGLId();
575 
576  } else {
577  MODebug2->Error(moText("moTextureBuffer::GetFrame Error: p_i out of range: p_i:") + IntToStr(p_i) + moText(" count:") + IntToStr(m_Frames.Count()) );
578  return -1;
579  }
580 
581 }
582 
583 
585 
586  if ( p_i<m_Frames.Count()) {
587 
588  moTextureMemory* pTextureMemory = m_Frames[p_i];
589 
590  //GLId = pTextureMemory->GetReference();
591  pTextureMemory->ReleaseReference();
592  }
593 }
594 
596  if ( p_i<m_Frames.Count()) {
597 
598  moTextureMemory* pTextureMemory = m_Frames[p_i];
599  return pTextureMemory;
600  }
601  return NULL;
602 }
603 
604 
605 //===========================================
606 //
607 // moTextureManager
608 //
609 //===========================================
610 
612 {
613  SetType( MO_OBJECT_RESOURCE );
614  SetResourceType( MO_RESOURCETYPE_TEXTURE );
615 
616  SetName("texturemanager");
617  SetLabelName("texturemanager");
618 
619  // In your main program ...
620  FreeImage_SetOutputMessage(FreeImageErrorHandler);
621 
622  m_glmanager = NULL;
623  m_fbmanager = NULL;
624 
625  m_textures_buffers.Init(0,NULL);
626 }
627 
629 {
630  Finish();
631 }
632 
634 {
635  if (m_pResourceManager){
636  m_glmanager = m_pResourceManager->GetGLMan();
637  m_fbmanager = m_pResourceManager->GetFBMan();
638  } else return false;
639 
640  m_textures_array.Init( 0, NULL );
641 
642  m_preview_texture_idx = AddTexture("preview_texture", 1024, 1024);
643  //AddTexture("texture_clip1", 1024, 768);
644  //AddTexture("texture_clip2", 1024, 768);
645  //AddTexture("texture_clip3", 1024, 768);
646  int m_id_default = AddTexture( "default", 256, 256);
647  moTexture* DefaultTexture = GetTexture(m_id_default);
649  moSlash +
650  moText("icons") +
651  moSlash +
652  moText("moldeologo.png");
653  MODebug2->Message(moText("moTextureManager::Init > loading default file:")+logoFile);
654 
655  if (DefaultTexture) if (!DefaultTexture->BuildFromFile(logoFile)) {
656  MODebug2->Error("moTextureManager::Init > error loading " + logoFile );
657  }
658 
659  m_id_default = AddTexture( "moldeonegro", 1800, 600);
660  DefaultTexture = GetTexture(m_id_default);
661  logoFile = m_pResourceManager->GetDataMan()->GetAppDataPath() +
662  moSlash +
663  moText("icons") +
664  moSlash +
665  moText("moldeonegro.png");
666  MODebug2->Message(moText("moTextureManager::Init > loading logo file:")+logoFile);
667 
668  m_id_default = AddTexture( "moldeotrans", 1800, 600);
669  DefaultTexture = GetTexture(m_id_default);
670  logoFile = m_pResourceManager->GetDataMan()->GetAppDataPath() +
671  moSlash +
672  moText("icons") +
673  moSlash +
674  moText("moldeotrans.png");
675  MODebug2->Message(moText("moTextureManager::Init > loading logo file:")+logoFile);
676 
677 
678  if (DefaultTexture) if (!DefaultTexture->BuildFromFile(logoFile)) {
679  MODebug2->Error("moTextureManager::Init > error loading " + logoFile );
680  }
681 
682  return (m_glmanager && m_fbmanager);
683 }
684 
686 {
687  m_textures_array.Finish();
688 
689  m_glmanager = NULL;
690  m_fbmanager = NULL;
691 
692  return true;
693 }
694 
696 {
697  MOint moid = GetTextureMOId(param->GetValue().GetSubValue(0).Text(), false);
698  if (-1 < moid) return moid;
699  else
700  if (p_create_tex) return AddTexture(param);
701  else return -1;
702 }
703 
705 {
706  MOint moid = GetTextureMOId( value->GetSubValue(0).Text(), false);
707  if (-1 < moid) return moid;
708  else
709  if (p_create_tex) return AddTexture(value);
710  else return -1;
711 }
712 
713 MOint moTextureManager::GetTextureMOId(moText p_name, MOboolean p_create_tex, bool p_refresh)
714 {
715  moTexture* ptex;
716  if (p_name.Trim() == moText("")) return -1;
717  for (MOuint i = 0; i < m_textures_array.Count(); i++)
718  {
719  ptex = m_textures_array[i];
720  if ((ptex != NULL) && (!stricmp(ptex->GetName(), p_name))) {
721  if (p_refresh) ptex->Refresh();
722  return i;
723  }
724  }
725 
726  if (p_create_tex) return AddTexture(p_name);
727  else return -1;
728 }
729 
730 void
732 
733  moTexture* ptex;
734 
735  for (MOuint i = 0; i < m_textures_array.Count(); i++)
736  {
737  ptex = m_textures_array[i];
738  if (ptex) {
739  if (ptex->IsBuildedFromFile() && ptex->GetName()!="default" ) {
740  moDebugManager::Message( moText("Refreshing:") + ptex->GetName() );
741  ptex->Refresh();
742  }
743  }
744  }
745 }
746 
747 MOint moTextureManager::GetTextureBuffer( moText p_foldername, MOboolean p_create_tex, moText p_bufferformat ) {
748 
749  moTextureBuffer* ptexbuffer;
750  if (p_foldername.Trim() == moText("")) return -1;
751  for (MOuint i = 0; i < m_textures_buffers.Count(); i++)
752  {
753  ptexbuffer = m_textures_buffers[i];
754  if ((ptexbuffer != NULL) && (!stricmp(ptexbuffer->GetName(), p_foldername))) return i;
755  }
756  if (p_create_tex) return AddTextureBuffer( p_foldername, p_bufferformat );
757  else return -1;
758 
759 }
760 
761 
763  if (0<=idx && idx<(int)m_textures_array.Count()) {
764  return m_textures_buffers[idx];
765  } else return NULL;
766 }
767 
768 
770 {
771  moTexture* ptex;
772  for (MOuint i = 0; i < m_textures_array.Count(); i++)
773  {
774  ptex = m_textures_array[i];
775  if ((ptex != NULL) && (ptex->GetGLId() == p_glid)) return i;
776  }
777  return -1;
778 }
779 
781 {
782  moTexture* ptex = CreateTexture(p_type, p_name, p_tex_param);
783  if (ptex != NULL)
784  {
785  m_textures_array.Add(ptex);
786  ptex->SetMOId(m_textures_array.Count() - 1);
787  return ptex->GetMOId();
788  }
789  else return -1;
790 }
791 
793 {
794  moValue* pValue = &p_param->GetValue();
795  return AddTexture( pValue );
796 }
797 
799 {
800  moText name = p_value->GetSubValue(0).Text();
801  MOuint type = GetTypeForFile(name);
802  moTextureMultiple* ptex_mult;
803  moMovie* ptex_mov;
804  moVideoBuffer* ptex_videobuffer;
805  moCircularVideoBuffer* ptex_circularvideobuffer;
806 
807  moTexture* ptex = CreateTexture(type, name);
808  if (ptex != NULL)
809  {
810  MOboolean res = false;
811  if (type == MO_TYPE_TEXTURE) res = ptex->Load( p_value );
812  else if (type == MO_TYPE_TEXTURE_MULTIPLE)
813  {
814  ptex_mult = (moTextureMultiple*)ptex;
815  res = CreateMultipleTex(ptex_mult, p_value);
816  }
817  else if (type == MO_TYPE_MOVIE)
818  {
819  ptex_mov = (moMovie*)ptex;
820  res = ptex_mov->Load(p_value);
821  }
822  else if (type == MO_TYPE_VIDEOBUFFER)
823  {
824  ptex_videobuffer = (moVideoBuffer*)ptex;
825  res = ptex_videobuffer->Load(p_value);
826  }
827  else if (type == MO_TYPE_CIRCULARVIDEOBUFFER)
828  {
829  ptex_circularvideobuffer = (moCircularVideoBuffer*)ptex;
830  res = ptex_circularvideobuffer->Load(p_value);
831  }
832  if (res)
833  {
834  m_textures_array.Add(ptex);
835  ptex->SetMOId(m_textures_array.Count() - 1);
836  return ptex->GetMOId();
837  }
838  else
839  {
840  delete ptex;
841  return -1;
842  }
843  }
844  else return -1;
845 }
846 
847 MOint moTextureManager::AddTexture(moText p_name, MOuint p_width, MOuint p_height, moTexParam p_tex_param)
848 {
849  moTexture* ptex = CreateTexture(MO_TYPE_TEXTURE, p_name, p_tex_param);
850  if (ptex != NULL)
851  {
852  MOboolean res = ptex->BuildEmpty(p_width, p_height);
853  if (res)
854  {
855  m_textures_array.Add(ptex);
856  ptex->SetMOId(m_textures_array.Count() - 1);
857  return ptex->GetMOId();
858  }
859  else
860  {
861  delete ptex;
862  return -1;
863  }
864  }
865  else return -1;
866 }
867 
868 MOint moTextureManager::AddTexture(moText p_name, MOuint p_width, MOuint p_height, GLvoid* p_buffer, GLenum p_pixel_format, GLenum p_pixel_type, moTexParam p_tex_param)
869 {
870  moTexture* ptex = CreateTexture(MO_TYPE_TEXTURE, p_name, p_tex_param);
871  if (ptex != NULL)
872  {
873  MOboolean res = ptex->BuildFromBuffer(p_width, p_height, p_buffer, p_pixel_format, p_pixel_type);
874  if (res)
875  {
876  m_textures_array.Add(ptex);
877  ptex->SetMOId(m_textures_array.Count() - 1);
878  return ptex->GetMOId();
879  }
880  else
881  {
882  delete ptex;
883  return -1;
884  }
885  }
886  else return -1;
887 }
888 
890 {
891  moText name = p_filename;
892  MOuint type = GetTypeForFile(name);
893 
894  moMovie* ptex_mov;
895 
896  moTexture* ptex = CreateTexture(type, name);
897  p_filename = m_pResourceManager->GetDataMan()->GetDataPath() + moSlash + (moText)p_filename;
898 
899  moFile TexFileName( p_filename );
900  p_filename = TexFileName.GetAbsolutePath();
901 
902  if (ptex != NULL)
903  {
904  MOboolean res = false;
905  if (type == MO_TYPE_TEXTURE) {
906 
907  res = ptex->BuildFromFile(p_filename);
908 
909  } else if (type == MO_TYPE_MOVIE) {
910 
911  ptex_mov = (moMovie*)ptex;
912  res = ptex_mov->LoadMovieFile(p_filename);
913 
914  }
915 
916 
917  if (res) {
918 
919  m_textures_array.Add(ptex);
920  ptex->SetMOId(m_textures_array.Count() - 1);
921  return ptex->GetMOId();
922 
923  } else {
924  MODebug2->Error("moTextureManager::AddTexture > filename: " + p_filename + " failed BuildFromFile()/LoadMovieFile()");
925  DeleteTexture(ptex->GetMOId());
926  return -1;
927  }
928  }
929  else return -1;
930 }
931 
932 MOint moTextureManager::AddTextureBuffer( moText p_foldername, moText p_bufferformat ) {
933 
934  moTextureBuffer* ptexbuffer = new moTextureBuffer();
935  if (ptexbuffer) {
936 
937  if ( ptexbuffer->Init( p_foldername, p_bufferformat, m_pResourceManager ) ) {
938  m_textures_buffers.Add( ptexbuffer );
939  MODebug2->Push( moText("TextureBuffer created:") + (moText)p_foldername + moText(" idx:") +IntToStr((m_textures_buffers.Count() - 1)) );
940  return (m_textures_buffers.Count() - 1);
941  } else {
942  MODebug2->Error( moText("moTextureManager::AddTextureBuffer Error: Initializing texturebuffer: ") + (moText)p_foldername );
943  }
944 
945  } else {
946  MODebug2->Error( moText("moTextureManager::AddTextureBuffer Error: Creating texturebuffer: ") + (moText)p_foldername );
947  }
948 
949  return -1;
950 
951 }
952 
954 {
955  if (ValidTexture(p_moid))
956  {
957  moTexture* ptex = m_textures_array[p_moid];
958  if (ptex)
959  delete ptex;
960  m_textures_array.Remove(p_moid);
961  for (MOuint i = p_moid; i < m_textures_array.Count(); i++) m_textures_array[i]->SetMOId(i);
962  return true;
963  }
964  else return false;
965 }
966 
968 {
969  moText extension = p_filename;
970  moText begin = p_filename;
971  begin.Left(7);
972 
973  moFile FileName(p_filename);
974  extension = FileName.GetExtension();
975 
976  if (!stricmp(extension,".tga")
977  || !stricmp(extension,".jpg")
978  || !stricmp(extension,".png")
979  || !stricmp(extension,".bmp")
980  || !stricmp(extension,".tif")
981  || !stricmp(extension,".xpm"))
982  return MO_TYPE_TEXTURE;
983  if (!stricmp(begin,"multiple"))
985  if (!stricmp(extension,".mkv")
986  || !stricmp(extension,".ogg")
987  || !stricmp(extension,".ogv")
988  || !stricmp(extension,".avi")
989  || !stricmp(extension,".mov")
990  || !stricmp(extension,".mpg")
991  || !stricmp(extension,".mpeg")
992  || !stricmp(extension,".mts")
993  || !stricmp(extension,".m2ts")
994  || !stricmp(extension,".vob")
995  || !stricmp(extension,".m2v")
996  || !stricmp(extension,".mp4")
997  || !stricmp(extension,".web")
998  || !stricmp(extension,".webm"))
999  return MO_TYPE_MOVIE;
1000 
1001  return MO_TYPE_TEXTURE;
1002 }
1003 
1005 {
1006  moTexture* ptex;
1007  moTextureMemory* ptex_mem;
1008  moTextureMultiple* ptex_mult;
1009  moMovie* ptex_movie;
1010  moVideoBuffer* ptex_videobuffer;
1011  moCircularVideoBuffer* ptex_circularvideobuffer;
1012 
1013  MOuint new_moid = m_textures_array.Count();
1014 
1015  if ( p_name.Trim().Length() == 0 ) {
1017  MODebug2->Error( moText("moTextureManager::CreateTexture > untitled texture name.") );
1018  return NULL;
1019  }
1020 
1021  if (p_type == MO_TYPE_TEXTURE)
1022  {
1023  ptex = new moTexture();
1024  }
1025  else if (p_type == MO_TYPE_TEXTUREMEMORY)
1026  {
1027  ptex_mem = new moTextureMemory();
1028  ptex = (moTexture*)ptex_mem;
1029  }
1030  else if (p_type == MO_TYPE_TEXTURE_MULTIPLE)
1031  {
1032  ptex_mult = new moTextureMultiple();
1033  ptex = (moTexture*)ptex_mult;
1034  }
1035  else if (p_type == MO_TYPE_MOVIE)
1036  {
1037  ptex_movie = new moMovie();
1038  ptex = (moTexture*)ptex_movie;
1039  }
1040  else if (p_type == MO_TYPE_VIDEOBUFFER)
1041  {
1042  ptex_videobuffer = new moVideoBuffer();
1043  ptex = (moTexture*)ptex_videobuffer;
1044  }
1045  else if (p_type == MO_TYPE_CIRCULARVIDEOBUFFER)
1046  {
1047  ptex_circularvideobuffer = new moCircularVideoBuffer();
1048  ptex = (moTexture*)ptex_circularvideobuffer;
1049  }
1050  else return NULL;
1051 
1052  ptex->Init(p_name, new_moid, m_pResourceManager, p_param);
1053  return ptex;
1054 }
1055 
1057 {
1058  moValue* pValue = &p_param->GetValue();
1059  return CreateMultipleTex(p_tex_mult, pValue);
1060 }
1061 
1063 {
1064  moText texfn;
1065  MOint moid;
1066  MOuint nfiles = p_value->GetSubValueCount() - 1;
1067 
1068  for (MOuint i = 0; i < nfiles; i++)
1069  {
1070  texfn = p_value->GetSubValue(i + 1).Text();
1071  moid = GetTextureMOId(texfn, true);
1072  if (-1 < moid) p_tex_mult->AddFrame(GetTexture(moid));
1073  }
1074 
1075  return 0 < p_tex_mult->GetTextureCount();
1076 }
1077 
1079  return ValidTexture(p_moid) ? m_textures_array[p_moid]->GetType() : MO_UNDEFINED;
1080 
1081 }
1082 
1084  return ValidTexture(p_moid) ? m_textures_array[p_moid]->GetGLId() : 0;
1085 }
1086 
1087 
1089 {
1090  if (Events==NULL) return;
1091  //if ( GetId() == MO_MOLDEOOBJECT_UNDEFINED_ID ) return;
1092 
1094  for(MOuint k=0; k<m_textures_buffers.Count(); k++) {
1095 
1096 
1097  moTextureBuffer* pTextureBuffer = m_textures_buffers[k];
1098 
1099  if (pTextureBuffer && !pTextureBuffer->LoadCompleted()) {
1100 
1101  MODebug2->Push( pTextureBuffer->GetName() + moText(":") + IntToStr(pTextureBuffer->GetImagesProcessed() ));
1102  pTextureBuffer->UpdateImages( 1 );
1103  }
1104 
1105 
1106  }
1107 
1108 }
1109 
virtual void ReleaseFrame(MOuint p_i)
void moBitmap
Definition: moTexture.h:44
void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message)
MOint GetTextureBuffer(moText p_foldername, MOboolean p_create_tex=true, moText p_bufferformat="JPGAVERAGE")
virtual ~moTextureBuffer()
Valor de un Parámetro.
Definition: moValue.h:501
moFile * FindFirst()
Definition: moFile.cpp:314
Parámetros internos de una textura.
Definition: moTypes.h:543
moFileType GetType()
Definition: moFile.cpp:442
buffer de texturas
Definition: moTexture.h:59
static void Message(moText p_text)
Anuncia un mensaje al usuario además de guardarlo en el log de texto.
MOboolean Refresh()
Definition: moTexture.cpp:840
moTextureBufferFormat
Buffer de imágenes.
void Error(moText p_text)
Anuncia y registra un error.
Definition: moAbstract.cpp:79
virtual MOboolean Finish()
Definition: moTexture.cpp:1533
moFile * FindNext()
Definition: moFile.cpp:325
MOuint GetTextureArray(int width, int height, int levels, bool force_load=false)
moValueBase & GetSubValue(MOint p_indexsubvalue=0)
Definition: moValue.h:539
buffer de video
Definition: moTexture.h:57
MOboolean BuildFromBuffer(MOuint p_width, MOuint p_height, const GLvoid *p_buffer, GLenum p_format=GL_RGBA, GLenum p_type=GL_UNSIGNED_BYTE)
Definition: moTexture.cpp:136
moDefineDynamicArray(moTextureBuffers) moDefineDynamicArray(moTextureFrames) void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif
MOboolean LoadImage(moText p_ImageName, moBitmap *pImage, int indeximage)
#define MO_UNDEFINED
Definition: moTypes.h:379
#define MOboolean
Definition: moTypes.h:385
una textura animada basada en una película
Definition: moTexture.h:857
static void Error(moText p_text)
Anuncia un error.
MOboolean Load(moParam *p_param)
Definition: moTexture.cpp:2362
moText0 & Left(MOuint)
Definition: moText.cpp:484
MOboolean LoadCompleted()
virtual moTextureMemory * GetTexture(MOuint p_i)
void SetMOId(MOuint p_moid)
Definition: moTexture.h:240
MOint AddTexture(MOuint p_type, moText p_name, moTexParam p_tex_param=MODefTex2DParams)
textura múltiple
Definition: moTexture.h:55
virtual int GetFrame(MOuint p_i)
MOboolean BuildEmpty(MOuint p_width, MOuint p_height)
Definition: moTexture.cpp:113
void ReleaseReference()
Definition: moTexture.cpp:1520
EACH IMAGE OF THE BUFFER IS ANALIZED (HISTOGRAM LEVELS)
MOboolean LoadMovieFile(moText p_filename)
Definition: moTexture.cpp:2304
#define MObyte
Definition: moTypes.h:400
moTextureFrames m_Frames
MOuint GetHeight() const
Definition: moTexture.h:261
MOboolean m_bLoadCompleted
moFile * Find(MOint index)
Definition: moFile.cpp:352
moText GetExtension()
Get absolute path and filename "/D/PP/myFileName.txt".
Definition: moFile.cpp:543
MOboolean CheckErrors(moText p_location)
MOuint Length() const
Definition: moText.cpp:347
int GetLuminance()
Definition: moTexture.h:503
moGLManager * GetGLMan()
MOboolean Exists()
Definition: moFile.cpp:436
moText GetFileName()
Definition: moFile.cpp:477
void AddFrame(moTexture *p_texture)
Definition: moTexture.cpp:2050
moText GetName() const
Definition: moTexture.h:245
Lista de eventos.
Definition: moEventList.h:139
clase de para manejar textos
Definition: moText.h:75
moTexture * GetTexture(MOuint p_moid)
Dispositivo de entrada/salida, típicamente, interfaces humanas de IO y datos ( teclado, mouse, tableta, tcp, udp, serial )
Definition: moTypes.h:532
void Log(moText p_text)
Escribe un mensaje en el archivo de registro (log)
Definition: moAbstract.cpp:123
moText0 & Trim()
Definition: moText.cpp:604
moResourceManager * m_pResourceManager
MOboolean UpdateImages(int maxfiles=-1)
moText0 moText
Definition: moText.h:291
Buffer de imágenes para video.
virtual MOboolean Init()
Inicializa el objeto.
MOboolean DeleteTexture(MOuint p_moid)
#define MOint
Definition: moTypes.h:388
const char * message
moFileArray & GetFiles()
Definition: moFile.cpp:381
moFileManager * GetFileMan()
MOuint GetGLId(MOuint p_moid)
int GetContrast()
Definition: moTexture.h:501
MOuint GetTypeForFile(moText p_filename)
moTextureBuffer(int slots=0)
MOint GetTextureMOId(moParam *param, MOboolean p_create_tex)
moBitmap * LoadFromMemory()
Definition: moTexture.cpp:1383
clase base para el manejo de una textura
Definition: moTexture.h:78
moDirectory * GetDirectory(moText p_Path)
moText GetCompletePath()
Get inmediate folder name: return "PP" for "PP/myFileName.txt".
Definition: moFile.cpp:538
moDirectory * m_pDirectory
Administrador de recursos.
película
Definition: moTexture.h:56
virtual MOboolean Init()
Definition: moFBManager.cpp:54
MOboolean CreateMultipleTex(moTextureMultiple *p_tex_mult, moParam *p_param)
moTextureFrames & GetBufferPatterns(moTexture *p_ImageReference, int x, int y, int width=0, int height=0)
moFBManager * GetFBMan()
moTextureFrames * m_pBufferPatterns
TEXTURA BASE.
Definition: moTexture.h:54
static moDebug * MODebug2
Clase de impresión de errores para depuración.
Definition: moAbstract.h:225
MOuint GetMOId() const
Definition: moTexture.h:235
virtual void Update(moEventList *Events)
moDataManager * GetDataMan()
virtual MOboolean Init(moText p_name, MOuint p_moid, moResourceManager *p_res, moTexParam p_param=MODefTex2DParams)
Definition: moTexture.cpp:85
moText Text()
Definition: moValue.cpp:539
MOuint GetWidth() const
Definition: moTexture.h:256
MOboolean BuildFromFile(moText p_filename)
Definition: moTexture.cpp:142
moTextureFrames & GetBufferLevels(int L, int C)
virtual MOboolean Init()
void Push(moText p_text)
Apila el mensaje dentro de la pila de mensajes.
Definition: moAbstract.h:115
moValue & GetValue(MOint i=-1)
Definition: moParam.cpp:1204
MOboolean Load(moParam *p_param)
Definition: moTexture.cpp:375
Secuencia de texturas.
Definition: moTexture.h:748
#define RenderMan()
MOboolean IsBuildedFromFile() const
Definition: moTexture.h:363
MOuint GetGLId() const
Definition: moTexture.h:224
moMoldeoObjectType GetType() const
MOint AddTextureBuffer(moText p_foldername, moText p_bufferformat="JPGAVERAGE")
moText GetAppDataPath()
MOuint GetTextureCount()
Definition: moTexture.h:814
moText GetDataPath()
#define MOuint
Definition: moTypes.h:387
LIBMOLDEO_API moText0 IntToStr(int a)
Definition: moText.cpp:1070
virtual MOboolean BuildFromBitmap(moBitmap *p_bitmap, moText p_bufferformat="JPG")
Guarda el bitmap con el formato elegido en memoria, luego construye la textura opengl.
Definition: moTexture.cpp:1476
clase base para el manejo de una textura con su copia comprimida en memoria
Definition: moTexture.h:442
MOuint GetSubValueCount()
Definition: moValue.h:545
MOboolean m_bInitialized
Valor de inicialización.
Definition: moAbstract.h:223
moTextureFrames ** m_pBufferLevels
virtual MOboolean Finish()
Finaliza el objeto, libera recursos.
Buffer Circular de imágenes para video.
EACH IMAGE OF THE BUFFER IS ANALIZED IN A REDUCED PATTERN.
void Message(moText p_text)
Anuncia un mensaje al usuario además de guardarlo en el log de texto.
Definition: moAbstract.cpp:114
moTexture * CreateTexture(MOuint p_type, moText p_name, moTexParam p_param=MODefTex2DParams)
virtual MOboolean Finish()
#define MOubyte
Definition: moTypes.h:399
moTextureManager * GetTextureMan()