/[projet1]/public/pc/shared_libraries/freeimage/v3.12.0/Examples/OpenGL/TextureManager/TextureManager.cpp
Defence Force logotype

Contents of /public/pc/shared_libraries/freeimage/v3.12.0/Examples/OpenGL/TextureManager/TextureManager.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 126 - (show annotations)
Mon Jul 13 12:20:10 2009 UTC (10 years, 7 months ago) by dbug
File size: 3933 byte(s)
Added some shared libraries (UnitTest++, and FreeImage) to avoid having every single project brings its own libraries.
Ideally people should add them in a way we can upgrade versions without breaking things:
-> public/pc/shared_libraries/library_name/library_version/actuall_content
1 //**********************************************
2 //Singleton Texture Manager class
3 //Written by Ben English
4 //benjamin.english@oit.edu
5 //
6 //For use with OpenGL and the FreeImage library
7 //**********************************************
8
9 #include "TextureManager.h"
10
11 TextureManager* TextureManager::m_inst(0);
12
13 TextureManager* TextureManager::Inst()
14 {
15 if(!m_inst)
16 m_inst = new TextureManager();
17
18 return m_inst;
19 }
20
21 TextureManager::TextureManager()
22 {
23 // call this ONLY when linking with FreeImage as a static library
24 #ifdef FREEIMAGE_LIB
25 FreeImage_Initialise();
26 #endif
27 }
28
29 //these should never be called
30 //TextureManager::TextureManager(const TextureManager& tm){}
31 //TextureManager& TextureManager::operator=(const TextureManager& tm){}
32
33 TextureManager::~TextureManager()
34 {
35 // call this ONLY when linking with FreeImage as a static library
36 #ifdef FREEIMAGE_LIB
37 FreeImage_DeInitialise();
38 #endif
39
40 UnloadAllTextures();
41 m_inst = 0;
42 }
43
44 bool TextureManager::LoadTexture(const char* filename, const unsigned int texID, GLenum image_format, GLint internal_format, GLint level, GLint border)
45 {
46 //image format
47 FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
48 //pointer to the image, once loaded
49 FIBITMAP *dib(0);
50 //pointer to the image data
51 BYTE* bits(0);
52 //image width and height
53 unsigned int width(0), height(0);
54 //OpenGL's image ID to map to
55 GLuint gl_texID;
56
57 //check the file signature and deduce its format
58 fif = FreeImage_GetFileType(filename, 0);
59 //if still unknown, try to guess the file format from the file extension
60 if(fif == FIF_UNKNOWN)
61 fif = FreeImage_GetFIFFromFilename(filename);
62 //if still unkown, return failure
63 if(fif == FIF_UNKNOWN)
64 return false;
65
66 //check that the plugin has reading capabilities and load the file
67 if(FreeImage_FIFSupportsReading(fif))
68 dib = FreeImage_Load(fif, filename);
69 //if the image failed to load, return failure
70 if(!dib)
71 return false;
72
73 //retrieve the image data
74 bits = FreeImage_GetBits(dib);
75 //get the image width and height
76 width = FreeImage_GetWidth(dib);
77 height = FreeImage_GetHeight(dib);
78 //if this somehow one of these failed (they shouldn't), return failure
79 if((bits == 0) || (width == 0) || (height == 0))
80 return false;
81
82 //if this texture ID is in use, unload the current texture
83 if(m_texID.find(texID) != m_texID.end())
84 glDeleteTextures(1, &(m_texID[texID]));
85
86 //generate an OpenGL texture ID for this texture
87 glGenTextures(1, &gl_texID);
88 //store the texture ID mapping
89 m_texID[texID] = gl_texID;
90 //bind to the new texture ID
91 glBindTexture(GL_TEXTURE_2D, gl_texID);
92 //store the texture data for OpenGL use
93 glTexImage2D(GL_TEXTURE_2D, level, internal_format, width, height,
94 border, image_format, GL_UNSIGNED_BYTE, bits);
95
96 //Free FreeImage's copy of the data
97 FreeImage_Unload(dib);
98
99 //return success
100 return true;
101 }
102
103 bool TextureManager::UnloadTexture(const unsigned int texID)
104 {
105 bool result(true);
106 //if this texture ID mapped, unload it's texture, and remove it from the map
107 if(m_texID.find(texID) != m_texID.end())
108 {
109 glDeleteTextures(1, &(m_texID[texID]));
110 m_texID.erase(texID);
111 }
112 //otherwise, unload failed
113 else
114 {
115 result = false;
116 }
117
118 return result;
119 }
120
121 bool TextureManager::BindTexture(const unsigned int texID)
122 {
123 bool result(true);
124 //if this texture ID mapped, bind it's texture as current
125 if(m_texID.find(texID) != m_texID.end())
126 glBindTexture(GL_TEXTURE_2D, m_texID[texID]);
127 //otherwise, binding failed
128 else
129 result = false;
130
131 return result;
132 }
133
134 void TextureManager::UnloadAllTextures()
135 {
136 //start at the begginning of the texture map
137 std::map<unsigned int, GLuint>::iterator i = m_texID.begin();
138
139 //Unload the textures untill the end of the texture map is found
140 while(i != m_texID.end())
141 UnloadTexture(i->first);
142
143 //clear the texture map
144 m_texID.clear();
145 }

  ViewVC Help
Powered by ViewVC 1.1.26