All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
UnityTextureFileLoader.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "ITextureFileLoader.h"
6 #include "Types.h"
7 #include "Helpers.h"
8 #include "GLHelpers.h"
9 #include "IJpegLoader.h"
10 #include "IPNGDecoder.h"
11 #include "UnityCallbacks.h"
12 #include "UnityMaterialToTextureMap.h"
13 #include <string>
14 #include <pthread.h>
15 
16 namespace Eegeo
17 {
18  namespace Unity
19  {
20  using namespace Eegeo::Helpers;
21 
23  {
24  private:
25 
26  struct TextureBuffer
27  {
28  void* data;
29  int sizeInBytes;
30  int width;
31  int height;
32  int format;
33  bool mipMaps;
34  };
35 
36  Eegeo::Helpers::IFileIO* m_pFileIO;
39  TextureCallbacks m_textureCallbacks;
40  UnityMaterialToTextureMap m_materialToTextureMap;
41 
42  TextureBuffer* AllocateTextureBuffer(int size, int width, int height, int format, bool mipmap);
43  GLuint BeginUploadTextureBuffer(TextureBuffer* textureBuffer);
44 
45  bool LoadPng(Eegeo::Helpers::GLHelpers::TextureInfo &textureInfo, const std::string& filename, bool mipmap, bool invertVertically);
46 
47  bool LoadDDS(Eegeo::Helpers::GLHelpers::TextureInfo& textureInfo, const std::string& filename);
48  bool LoadDDSFromStream(Eegeo::Helpers::GLHelpers::TextureInfo &textureInfo, std::istream& stream, size_t size, const void* data);
49  bool LoadDDSFromBuffer(Eegeo::Helpers::GLHelpers::TextureInfo &textureInfo, const void* resourceBuffer, size_t resourceBufferSizeBytes);
50 
51  bool LoadPVR(Eegeo::Helpers::GLHelpers::TextureInfo& textureInfo, const std::string& filename);
52  bool LoadPVRFromStream(Eegeo::Helpers::GLHelpers::TextureInfo &textureInfo, std::istream& stream, size_t size, const void* data);
53  bool LoadPVRFromBuffer(Eegeo::Helpers::GLHelpers::TextureInfo &textureInfo, const void* resourceBuffer, size_t resourceBufferSizeBytes);
54 
55  bool LoadKtx(Eegeo::Helpers::GLHelpers::TextureInfo& textureInfo, const std::string& filename);
56  bool LoadKtxFromStream(Eegeo::Helpers::GLHelpers::TextureInfo &textureInfo, std::istream& stream, size_t size, const void* data);
57  bool LoadKtxFromBuffer(Eegeo::Helpers::GLHelpers::TextureInfo &textureInfo, const void* resourceBuffer, size_t resourceBufferSizeBytes);
58 
59  bool LoadPngFromStream(Eegeo::Helpers::GLHelpers::TextureInfo &textureInfo, std::istream& stream, bool mipmap, bool invertVertically);
60  bool LoadPngFromBuffer(Eegeo::Helpers::GLHelpers::TextureInfo &textureInfo, const void* resourceBuffer, size_t resourceBufferSizeBytes, bool mipmap, bool invertVertically);
61  static GLenum CalcPNGGLPixelFormat(int color_type);
62 
63  bool LoadJpg(Eegeo::Helpers::GLHelpers::TextureInfo& textureInfo, const std::string& filename, bool mipmap, bool invertVertically);
64  Byte *CreateImageDataFromPngStream(std::istream& stream, u32& width, u32& height, int& colorType, u32& size, bool invertVertically);
65  bool LoadJpgFromBuffer(Eegeo::Helpers::GLHelpers::TextureInfo& textureInfo, const void* resourceBuffer, size_t resourceBufferSizeBytes, bool mipmap, bool invertVertically);
66 
67  public:
69  Eegeo::Helpers::IFileIO *pFileIO,
72  const TextureCallbacks& textureCallbacks);
73 
74  virtual ~UnityTextureFileLoader();
75 
76  GLuint GetTextureIDForMaterial(const std::string& materialName);
77  void SetTextureForMaterial(const std::string& materialName, const GLuint textureID);
78  void ReleaseTexture(GLuint texureId);
79 
80  virtual bool LoadTexture(Eegeo::Helpers::GLHelpers::TextureInfo& info, const std::string& fileName, bool mipmap = false);
81  virtual bool LoadCubeTexture(Eegeo::Helpers::GLHelpers::TextureInfo& out_textureInfo, const CubeTextureName& cubeTextureName);
82  virtual bool LoadFromBuffer(Eegeo::Helpers::GLHelpers::TextureInfo& textureInfo, const std::string& fileExtension, const Byte* resourceBuffer, size_t bufferSizeBytes, bool mipmap = false);
83  virtual bool LoadFromCubeFaces(Eegeo::Helpers::GLHelpers::TextureInfo& out_textureInfo, const Helpers::CubeFaceFileNames& cubeFaceFileNames);
84  virtual bool LoadFromCubeFaces(Eegeo::Helpers::GLHelpers::TextureInfo& out_textureInfo, const std::vector<const std::vector<Byte>*>& cubeFaceBuffers);
85 
86  UnityMaterialToTextureMap& GetMaterialToTextureMap() { return m_materialToTextureMap; }
87 
88  virtual Rendering::AsyncTexturing::IAsyncTextureLoad& LoadFromFileAsync(
90  const std::string& fileName,
91  void* pUserData,
92  bool mipmap = false);
93 
94  virtual Rendering::AsyncTexturing::IAsyncTextureLoad& LoadCubeFromFileAsync(
96  const Helpers::CubeTextureName& textureName,
97  void* pUserData);
98 
99  virtual Rendering::AsyncTexturing::IAsyncTextureLoad& LoadFromBufferAsync(
101  const std::string& fileExtension,
102  const std::vector<Byte>& resourceBuffer,
103  void* pUserData,
104  bool mipmap = false
105  );
106 
107  virtual Rendering::AsyncTexturing::IAsyncTextureLoad& LoadCubeFromBuffersAsync(
109  const std::vector<const std::vector<Byte>*>& cubeFaceBuffers,
110  void* pUserData
111  );
112  };
113  }
114 }