1 #ifdef LINUX
2 #include <GL/gl.h>
3 #endif
4 #ifdef WIN32
5 #include "glew.h"
6 #endif
7 #ifdef __APPLE__
8 #include <OpenGL/gl.h>
9 #endif
10 
11 #ifdef USE_DEVIL
12 #include <IL/ilut.h>
13 #else
14 #include "SOIL/SOIL.h"
15 #endif
16 
17 #ifdef WIN32
18 #include "win32-dirent.h"
19 #include <stdlib.h>
20 #endif
21 
22 #ifdef LINUX
23 #include <dirent.h>
24 #endif
25 
26 #ifdef MACOS
27 #include <dirent.h>
28 #endif
29 #include "TextureManager.hpp"
30 #include "Common.hpp"
31 #include "IdleTextures.hpp"
32 
33 
34 
TextureManager(const std::string _presetURL)35 TextureManager::TextureManager(const std::string _presetURL): presetURL(_presetURL)
36 {
37 #ifdef USE_DEVIL
38 ilInit();
39 iluInit();
40 ilutInit();
41 ilutRenderer(ILUT_OPENGL);
42 #endif
43 
44  Preload();
45  loadTextureDir();
46 }
47 
~TextureManager()48 TextureManager::~TextureManager()
49 {
50  Clear();
51 }
52 
Preload()53 void TextureManager::Preload()
54 {
55 
56 #ifdef USE_DEVIL
57 	ILuint image;
58 	ilGenImages(1, &image);
59 	ilBindImage(image);
60 	ilLoadL(IL_TYPE_UNKNOWN,(ILvoid*) M_data, M_bytes);
61 	GLuint tex = ilutGLBindTexImage();
62 #else
63 	 unsigned int tex = SOIL_load_OGL_texture_from_memory(
64 					  M_data,
65 					  M_bytes,
66 					  SOIL_LOAD_AUTO,
67 					  SOIL_CREATE_NEW_ID,
68 
69 					    SOIL_FLAG_POWER_OF_TWO
70 					  |  SOIL_FLAG_MULTIPLY_ALPHA
71 					 // |  SOIL_FLAG_COMPRESS_TO_DXT
72 					  );
73 #endif
74 
75   textures["M.tga"]=tex;
76 
77 #ifdef USE_DEVIL
78   ilLoadL(IL_TYPE_UNKNOWN,(ILvoid*) project_data,project_bytes);
79   tex = ilutGLBindTexImage();
80 #else
81   tex = SOIL_load_OGL_texture_from_memory(
82 					  project_data,
83 					  project_bytes,
84 					  SOIL_LOAD_AUTO,
85 					  SOIL_CREATE_NEW_ID,
86 
87 					  SOIL_FLAG_POWER_OF_TWO
88 					  |  SOIL_FLAG_MULTIPLY_ALPHA
89 					  //|  SOIL_FLAG_COMPRESS_TO_DXT
90 					  );
91 #endif
92 
93   textures["project.tga"]=tex;
94 
95 #ifdef USE_DEVIL
96   ilLoadL(IL_TYPE_UNKNOWN,(ILvoid*) headphones_data, headphones_bytes);
97   tex = ilutGLBindTexImage();
98 #else
99   tex = SOIL_load_OGL_texture_from_memory(
100 					  headphones_data,
101 					  headphones_bytes,
102 					  SOIL_LOAD_AUTO,
103 					  SOIL_CREATE_NEW_ID,
104 
105 					  SOIL_FLAG_POWER_OF_TWO
106 					  |  SOIL_FLAG_MULTIPLY_ALPHA
107 					 // |  SOIL_FLAG_COMPRESS_TO_DXT
108 					  );
109 #endif
110 
111   textures["headphones.tga"]=tex;
112 }
113 
Clear()114 void TextureManager::Clear()
115 {
116 
117 
118   for(std::map<std::string, GLuint>::const_iterator iter = textures.begin(); iter != textures.end(); iter++)
119     {
120       glDeleteTextures(1,&iter->second);
121     }
122   textures.clear();
123 }
124 
setTexture(const std::string name,const unsigned int texId,const int width,const int height)125 void TextureManager::setTexture(const std::string name, const unsigned int texId, const int width, const int height)
126 {
127 		textures[name] = texId;
128 		widths[name] = width;
129 		heights[name] = height;
130 }
131 
132 //void TextureManager::unloadTextures(const PresetOutputs::cshape_container &shapes)
133 //{
134   /*
135    for (PresetOutputs::cshape_container::const_iterator pos = shapes.begin();
136 	pos != shapes.end(); ++pos)
137     {
138 
139       if( (*pos)->enabled==1)
140 	{
141 
142 	  if ( (*pos)->textured)
143 	    {
144 	      std::string imageUrl = (*pos)->getImageUrl();
145 	      if (imageUrl != "")
146 		{
147 		  std::string fullUrl = presetURL + "/" + imageUrl;
148 		  ReleaseTexture(LoadTexture(fullUrl.c_str()));
149 		}
150 	    }
151 	}
152     }
153   */
154 //}
155 
getTexture(const std::string filename)156 GLuint TextureManager::getTexture(const std::string filename)
157 {
158 	std::string fullURL = presetURL + PATH_SEPARATOR + filename;
159 	return getTextureFullpath(filename,fullURL);
160 }
161 
getTextureFullpath(const std::string filename,const std::string imageURL)162 GLuint TextureManager::getTextureFullpath(const std::string filename, const std::string imageURL)
163 {
164 
165    if (textures.find(filename)!= textures.end())
166      {
167        return textures[filename];
168      }
169    else
170      {
171 
172 #ifdef USE_DEVIL
173        GLuint tex = ilutGLLoadImage((char *)imageURL.c_str());
174 #else
175        int width, height;
176 
177        unsigned int tex = SOIL_load_OGL_texture_size(
178     		   imageURL.c_str(),
179 					  SOIL_LOAD_AUTO,
180 					  SOIL_CREATE_NEW_ID,
181 
182 					    //SOIL_FLAG_POWER_OF_TWO
183 					  //  SOIL_FLAG_MIPMAPS
184 					    SOIL_FLAG_MULTIPLY_ALPHA
185 					  //|  SOIL_FLAG_COMPRESS_TO_DXT
186 					  //| SOIL_FLAG_DDS_LOAD_DIRECT
187 					  ,&width,&height);
188 
189 #endif
190        textures[filename]=tex;
191        widths[filename]=width;
192        heights[filename]=height;
193        return tex;
194 
195 
196      }
197 }
198 
getTextureWidth(const std::string imageURL)199 int TextureManager::getTextureWidth(const std::string imageURL)
200 {
201 	return widths[imageURL];
202 }
203 
getTextureHeight(const std::string imageURL)204 int TextureManager::getTextureHeight(const std::string imageURL)
205 {
206 	return heights[imageURL];
207 }
208 
getTextureMemorySize()209 unsigned int TextureManager::getTextureMemorySize()
210 {
211   return 0;
212 }
213 
loadTextureDir()214 void TextureManager::loadTextureDir()
215 {
216 	std::string dirname = CMAKE_INSTALL_PREFIX "/share/projectM/textures";
217 
218 	  DIR * m_dir;
219 
220 	 // Allocate a new a stream given the current directory name
221 	  if ((m_dir = opendir(dirname.c_str())) == NULL)
222 	  {
223       //std::cout<<"No Textures Loaded from "<<dirname<<std::endl;
224 	    return; // no files loaded. m_entries is empty
225 	  }
226 
227 	  struct dirent * dir_entry;
228 
229 	  while ((dir_entry = readdir(m_dir)) != NULL)
230 	  {
231 
232 	    // Convert char * to friendly string
233 	    std::string filename(dir_entry->d_name);
234 
235 	    if (filename.length() > 0 && filename[0] == '.')
236 		continue;
237 
238 	    // Create full path name
239 	    std::string fullname = dirname + PATH_SEPARATOR + filename;
240 
241 	    unsigned int texId = getTextureFullpath(filename, fullname);
242 	    if(texId != 0)
243 	    {
244 	    	user_textures.push_back(texId);
245 	    	textures[filename]=texId;
246 	    	user_texture_names.push_back(filename);
247 	    }
248 	  }
249 
250 	  if (m_dir)
251 	    {
252 	      closedir(m_dir);
253 	      m_dir = 0;
254 	    }
255 
256 }
257 
getRandomTextureName(std::string random_id)258 std::string TextureManager::getRandomTextureName(std::string random_id)
259 {
260 	if (user_texture_names.size() > 0)
261 	{
262 		std::string random_name = user_texture_names[rand() % user_texture_names.size()];
263 		random_textures.push_back(random_id);
264 		textures[random_id] = textures[random_name];
265 		return random_name;
266 	}
267 	else return "";
268 }
269 
clearRandomTextures()270 void TextureManager::clearRandomTextures()
271 {
272 	for (std::vector<std::string>::iterator pos = random_textures.begin(); pos	!= random_textures.end(); ++pos)
273 				{
274 					textures.erase(*pos);
275 					widths.erase(*pos);
276 					heights.erase(*pos);
277 				}
278 	random_textures.clear();
279 
280 }
281