Kigs Framework  Doc version 0.8
Open source multi purpose Rapid Application Development framework
OpenGLTexturePlatformSpecific.inl.h
1 
2 #include "Platform/Core/PlatformCore.h"
3 
4 
5 bool OpenGLTexture::initWithETCImage(FileHandle * aFile)
6 {
7  bool L_Result = false;
8  unsigned long L_filelength = 0;
9  CoreRawBuffer* L_RawBuffer = ModuleFileManager::LoadFile(aFile, L_filelength);
10  if(L_RawBuffer)
11  {
12  myTextureType = TEXTURE_2D;
13  struct ETC_Header
14  {
15  unsigned int sizex; // if sizex&0xFFFF0000 => content size
16  unsigned int sizey; // if sizey&0xFFFF0000 => content size
17  unsigned int format;
18  unsigned int datasize;
19  };
20 
21  //Read Header
22  ETC_Header* imageheader=(ETC_Header*)L_RawBuffer->buffer();
23 
24  //int dataSize=L_filelength-sizeof(ETC_Header);
25  int L_DataLen=imageheader->datasize;
26 
27  //bool L_bPreMulti = true;
28  unsigned char* imgdata=(unsigned char*)L_RawBuffer->buffer();
29  imgdata+=sizeof(ETC_Header);
30  unsigned char* L_pData = new unsigned char[L_DataLen];
31  memcpy(L_pData, imgdata, L_DataLen);
32  myPow2Width = imageheader->sizex&0xFFFF;
33  myPow2Height = imageheader->sizey&0xFFFF;
34 
35  myWidth = (short)myPow2Width;
36  myHeight = (short)myPow2Height;
37 
38  if((imageheader->sizex & 0xFFFF0000) || (imageheader->sizey & 0xFFFF0000))
39  {
40  myWidth = ((imageheader->sizex >> 16)& 0xFFFF);
41  myHeight = ((imageheader->sizey >> 16)& 0xFFFF);
42  }
43 
44  // compressed
45  //int L_nBitsPerComponent = 0;
46 
47  bool L_bHasAlpha = false;
48  unsigned int L_Format = (imageheader->format & 0xFFFF);
49  if (2 == L_Format) // ETC1A8
50  L_bHasAlpha = true;
51 
52  if(L_bHasAlpha)
53  glGenTextures(1, &myIDETCAlphaTexture);
54  /*Set texture Param*/
55  glBindTexture( GL_TEXTURE_2D, myTextureGLIndex );
56  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
57  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
58  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
59  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
60 
61  if(myIDETCAlphaTexture) // if alpha texture, apply same params
62  {
63  glBindTexture( GL_TEXTURE_2D, myIDETCAlphaTexture );
64  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
65  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
66  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
67  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
68  }
69 
70 
71  unsigned int L_TotalSize = (myPow2Width*myPow2Height)/2;
72  if(!L_bHasAlpha) // ETC1
73  {
74  glBindTexture(GL_TEXTURE_2D, myTextureGLIndex);
75  #ifndef __APPLE__
76  glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_ETC1_RGB8_OES, (GLsizei)myPow2Width, (GLsizei)myPow2Height, 0,L_TotalSize, L_pData);
77  #endif
78  myTransparencyType = 0;
79  }
80  else//ETC1A8
81  {
82  glBindTexture(GL_TEXTURE_2D, myTextureGLIndex);
83  #ifndef __APPLE__
84  glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_ETC1_RGB8_OES, (GLsizei)myPow2Width, (GLsizei)myPow2Height, 0,L_TotalSize, L_pData);
85  #endif
86  glBindTexture(GL_TEXTURE_2D, myIDETCAlphaTexture);
87  unsigned char* alphastart=(unsigned char*)L_pData;
88  alphastart += L_TotalSize;
89  glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, (GLsizei)myPow2Width, (GLsizei)myPow2Height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, alphastart);
90  myTransparencyType = 2;
91  }
92 
93  L_Result = true;
94  }
95 #ifdef _DEBUG
96  else
97  {
98  printf( "LoadFile %s Failed\n", aFile->myFullFileName.c_str());
99  }
100 #endif
101  return L_Result;
102 }
103 
104 // special case for cube map
105 // generate file names from base name
106 bool OpenGLTexture::CubeMapGeneration()
107 {
108  bool result=true;
109  FilePathManager* pathManager=(FilePathManager*)KigsCore::GetSingleton(_S_2_ID("FilePathManager"));
110  kstl::string fullfilename;
111  char asciiCount[16];
112  asciiCount[0]=0;
113 
114  // get extension
115  kstl::string extension=myFileName;
116  extension=extension.substr(extension.rfind("."));
117  // remove extension
118  kstl::string basefilename=myFileName;
119  basefilename=basefilename.substr(0,basefilename.length()-extension.length());
120 
121  // check if all 6 textures are ok
122  int index;
123  for(index=0;index<6;index++)
124  {
125  kstl::string filename=basefilename;
126  filename+="_";
127  sprintf(asciiCount,"%d",(index+1));
128  filename+=asciiCount;
129  filename+=extension;
130 
131  SmartPointer<FileHandle> fullfilenamehandle = pathManager->FindFullName(filename);
132  if(fullfilenamehandle != 0)
133  {
134  fullfilename = fullfilenamehandle->myFullFileName;
135  }
136  else
137  {
138  result=false;
139  break;
140  }
141  }
142 
143  if(result) // ok, the 6 textures are here
144  {
145  // first load them all
146 
147  //kfloat minwidth=1024;
148  //kfloat minheight=1024;
149 
150  // TODO
151 
152  return true;
153 
154  }
155 
156  return result;
157 }
158 
160 {
161  if(image == 0)
162  return false;
163 
164  int pow2sizex = 1;
165  while (pow2sizex < image->GetWidth())
166  {
167  pow2sizex = pow2sizex << 1;
168  }
169 
170  int pow2sizey = 1;
171  while (pow2sizey < image->GetHeight())
172  {
173  pow2sizey = pow2sizey << 1;
174  }
175 
176 
177  // check if we need to resize the image
178 
179  if (((pow2sizex != image->GetWidth()) || (pow2sizey != image->GetHeight())) )
180  {
181  if ((image->GetFormat() != TinyImage::BC1) && (image->GetFormat() != TinyImage::BC2) && (image->GetFormat() != TinyImage::BC3) )
182  image->FastResize(pow2sizex, pow2sizey);
183  }
184  myPow2Width=pow2sizex;
185  myPow2Height=pow2sizey;
186 
187  // no palette image can be converted directly
188  switch(image->GetFormat())
189  {
190  case TinyImage::ABGR_16_1555_DIRECT_COLOR:
191  case TinyImage::RGB_16_565 :
192  case TinyImage::RGBA_16_5551 :
193  case TinyImage::RGBA_32_8888 :
194  case TinyImage::RGB_24_888 :
195  return CreateFromBuffer(image->GetPixelData(),image->GetWidth(),image->GetHeight(),image->GetFormat());
196  break;
197  default:
198  break;
199  }
200 
201  // and here for palette conversion
202  unsigned int L_TextureFormat;
203  unsigned int L_TextureLevel;
204  unsigned int L_TextureTypes;
205 
206  myWidth = image->GetWidth();
207  myHeight = image->GetHeight();
208  L_TextureTypes = TEXTURE_2D;
209 
210  // compute power of two textures
211  /*int pow2size=1;
212  while(pow2size < image->GetWidth())
213  {
214  pow2size=pow2size<<1;
215  }
216  myPow2Width=pow2size;
217 
218  pow2size=1;
219  while(pow2size < image->GetHeight())
220  {
221  pow2size=pow2size<<1;
222  }
223  myPow2Height=pow2size;*/
224 
225  unsigned char* imageData = image->GetPixelData();
226 
227  unsigned int dataSize = image->GetPixelDataSize();
228 
229  unsigned int pixCount = image->GetHeight()*image->GetWidth();
230 
231  unsigned short* data = new unsigned short[pixCount];
232 
233  KIGS_MESSAGE("Create From image\n");
234  char message[512];
235  sprintf(message,"DataSize = %i\n", dataSize);
236  KIGS_MESSAGE(message);
237 
238  sprintf(message,"pixCount = %i\n", pixCount);
239  KIGS_MESSAGE(message);
240 
241  sprintf(message,"myWidth = %i\n", (int)myWidth);
242  KIGS_MESSAGE(message);
243 
244  sprintf(message,"myHeight = %i\n", (int)myHeight);
245  KIGS_MESSAGE(message);
246 
247  switch (image->GetFormat())
248  {
249 
250  case TinyImage::PALETTE16_256_COLOR :
251  {
252  L_TextureLevel = GL_RGBA;
253  L_TextureFormat = GL_RGBA;
254  L_TextureTypes = GL_UNSIGNED_SHORT_5_5_5_1;
255  unsigned short* palette = (unsigned short*)image->GetPaletteData();
256  unsigned int colorCount = image->GetPaletteDataSize()/2; // 1 unsigned short by color
257 
258  // set from ABGR to BGRA
259  // alpha = 0 for index 0
260  palette[0] = (palette[0]<<1);
261  // alpha = 1 for the others
262  for(int index=1; index < colorCount; index++)
263  palette[index] = (palette[index]<<1)|0x1;
264 
265  // replace index by associate color
266  for(int index=0; index < dataSize; index++)
267  data[index] = palette[imageData[index]];
268  //glGenTextures(1,&myTextureGLIndex);
269  glBindTexture (GL_TEXTURE_2D, myTextureGLIndex);
270  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
271  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
272 
273  glTexImage2D (GL_TEXTURE_2D, 0, L_TextureLevel, myWidth, myHeight, 0, L_TextureFormat, L_TextureTypes, data);
274  myTransparencyType = 1;
275  KIGS_ASSERT(glGetError()==GL_NO_ERROR);
276  glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
277  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
278  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
279  glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
280  }
281  break;
282  case TinyImage::PALETTE16_16_COLOR :
283  {
284  L_TextureLevel = GL_RGBA;
285  L_TextureFormat = GL_RGBA;
286  L_TextureTypes = GL_UNSIGNED_SHORT_5_5_5_1;
287  unsigned short* palette = (unsigned short*)image->GetPaletteData();
288  unsigned int colorCount = image->GetPaletteDataSize()/2; // 1 unsigned short by color
289  int index;
290 
291  // set from ABGR to BGRA
292  // alpha = 0 for index 0
293  palette[0] = (palette[0]<<1);
294  // alpha = 1 for the others
295  for(index=1; index < colorCount; index++)
296  palette[index] = (palette[index]<<1)|0x1;
297 
298  // replace index by associate color
299  for(index=0; index < dataSize; index++) {
300  unsigned char p1 = imageData[index]&0x0f;
301  unsigned char p2 = imageData[index]>>4;
302 
303  // 2 pixel per char
304  data[2*index] = palette[p1];
305  data[2*index+1] = palette[p2];
306  }
307  //glGenTextures(1,&myTextureGLIndex);
308  glBindTexture (GL_TEXTURE_2D, myTextureGLIndex);
309  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
310  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
311 
312  glTexImage2D (GL_TEXTURE_2D, 0, L_TextureLevel, myWidth, myHeight, 0, L_TextureFormat, L_TextureTypes, data);
313  myTransparencyType = 1;
314  KIGS_ASSERT(glGetError()==GL_NO_ERROR);
315  glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
316  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
317  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
318  glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
319  }
320  break;
321 
322  default:
323  KIGS_ASSERT(0 && "not supported format");
324  break;
325 
326  }
327  ComputeRatio();
328  delete[] data;
329 
330  return true;
331 
332 }
333 
334 bool OpenGLTexture::UpdateBufferZone(unsigned char* bitmapbuffer, const BBox2DI& zone, const Point2DI& bitmapSize)
335 {
336  return false;
337 }
338 
340 
341 bool OpenGLTexture::CanUseDynamicTexture(TinyImage::ImageFormat format)
342 {
343  return false;
344 }
345 bool OpenGLTexture::UseDynamicTexture(unsigned char* buffer, unsigned int width, unsigned int height, TinyImage::ImageFormat format, bool needRealloc)
346 {
347  return true;
348 }
TinyImage
Base class for picture file loading.
Definition: TinyImage.h:29
OpenGLTexture::CreateFromImage
bool CreateFromImage(const SmartPointer< TinyImage > &image, bool directInit=false) override
create a texture from an image
Definition: OpenGLTexturePlatformSpecific.inl.h:161
TinyImage::ImageFormat
ImageFormat
enumeration of supported texture format
Definition: TinyImage.h:37
OpenGLTexture::UpdateBufferZone
bool UpdateBufferZone(unsigned char *bitmapbuffer, const BBox2DI &zone, const Point2DI &bitmapSize) override
update zone in texture (only RGBA8888)
Definition: OpenGL3TexturePlatformSpecific.inl.h:94
ModuleFileManager::LoadFile
static CoreRawBuffer * LoadFile(const char *pFilename, u64 &filelength, u64 startOffset=0, unsigned int trailing_zero=0)
Definition: ModuleFileManager.cpp:61
FilePathManager
Definition: FilePathManager.h:178
KigsCore::GetSingleton
static CMSP & GetSingleton(const KigsID &classname)
return an unique instance of the given class (Design Pattern Singleton)
Definition: Core.cpp:537
FilePathManager::FindFullName
SmartPointer< FileHandle > FindFullName(const std::string &filename)
find the fullPath for the given file
Definition: FilePathManager.cpp:396