Cafu Engine
TextureMapImpl.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 /**********************************/
8 /*** Texture Map Implementation ***/
9 /**********************************/
10 
11 #ifndef CAFU_MATSYS_TEXTUREMAP_IMPLEMENTATION_HPP_INCLUDED
12 #define CAFU_MATSYS_TEXTUREMAP_IMPLEMENTATION_HPP_INCLUDED
13 
14 // Required for #include <GL/gl.h> with MS VC++.
15 #if defined(_WIN32) && defined(_MSC_VER)
16 #define WIN32_LEAN_AND_MEAN
17 #include <windows.h>
18 #endif
19 
20 #include <GL/gl.h>
21 
22 #include "../MapComposition.hpp"
23 #include "../TextureMap.hpp"
24 
25 
26 /// This class represents a texture-map.
28 {
29  public:
30 
31  // The TextureMapI interface is not repeated here.
32 
33  /// Returns true if this texture was created from a map composition that was "equivalent" to MC_.
34  virtual bool IsCreatedFromMapComp(const MapCompositionT& MC_)=0;
35 
36  /// This function returns an OpenGL object for this texture.
37  virtual GLuint GetOpenGLObject()=0;
38 
39  /// Virtual destructor. See the class design diagram and the C++ FAQ 21.05 for more information.
40  /// (We will delete derived classes via pointers to TextureMapImplT.)
41  virtual ~TextureMapImplT() {}
42 };
43 
44 
45 /// This class represents a 2D texture-map.
47 {
48  private:
49 
50  TextureMap2DT(const TextureMap2DT&); // Use of the Copy Constructor is not allowed.
51  void operator = (const TextureMap2DT&); // Use of the Assignment Operator is not allowed.
52 
53  enum SourceT { MC, RawPtrExt, RawPtrOwn, BitmapPtrExt, BitmapPtrOwn };
54 
55  SourceT Source;
56 
57  MapCompositionT MapComp; ///< Used for Source==MC (and for *ALL* source types for the min/mag filters and wrapping mode).
58  BitmapT* Bitmap; ///< Used for Source==MC or Source==BitmapPtr*
59 
60  char* Data; ///< Used for Source==RawPtr*
61  unsigned long SizeX; ///< Used for Source==RawPtr*
62  unsigned long SizeY; ///< Used for Source==RawPtr*
63  char BytesPerPixel; ///< Used for Source==RawPtr*
64 
65  GLuint OpenGLObject;
66  unsigned long InitCounter; ///< Do we have to re-upload the Bitmap?
67 
68 
69  public:
70 
71  // TextureMapI implementation.
72  // Needed by some user code for computing the texture s/t-coords.
73  unsigned int GetSizeX();
74  unsigned int GetSizeY();
75 
76 
77  // TextureMapImplT implementation.
78  bool IsCreatedFromMapComp(const MapCompositionT& MC_);
79  GLuint GetOpenGLObject();
80  ~TextureMap2DT();
81 
82 
83  // Internal interface.
84  // Constructors.
85  TextureMap2DT(const MapCompositionT& MapComp_);
86  TextureMap2DT(char* Data_, unsigned long SizeX_, unsigned long SizeY_, char BytesPerPixel_, bool MakePrivateCopy, const MapCompositionT& McForFiltersAndWrapping);
87  TextureMap2DT(BitmapT* Bitmap_, bool MakePrivateCopy, const MapCompositionT& McForFiltersAndWrapping);
88 };
89 
90 
91 /// This class represents a cube texture-map.
93 {
94  private:
95 
96  TextureMapCubeT(const TextureMapCubeT&); // Use of the Copy Constructor is not allowed.
97  void operator = (const TextureMapCubeT&); // Use of the Assignment Operator is not allowed.
98 
99  static const GLenum CubeTargets[6];
100  static const std::string CubeSuffixes[6];
101 
102  static std::string GetFullCubeMapString(std::string BaseString, unsigned long SuffixNr);
103 
104  enum SourceT { Files, RawPtrExt, RawPtrOwn, BitmapPtrExt, BitmapPtrOwn };
105 
106  SourceT Source;
107 
108  MapCompositionT MapComp; ///< Used for all source types for the min/mag filters and wrapping modes. If Source==Files, MapComp.GetString() is used as the cube map base name (with '#' placeholders).
109  BitmapT* Bitmap[6]; ///< Used for Source==Files or Source==BitmapPtr*
110 
111  char* Data[6]; ///< Used for Source==RawPtr*
112  unsigned long SizeX; ///< Used for Source==RawPtr*
113  unsigned long SizeY; ///< Used for Source==RawPtr*
114  char BytesPerPixel; ///< Used for Source==RawPtr*
115 
116  GLuint OpenGLObject;
117  unsigned long InitCounter; ///< Do we have to re-upload the Bitmap?
118 
119 
120  public:
121 
122  // TextureMapI implementation.
123  // Needed by some user code for computing the texture s/t-coords.
124  unsigned int GetSizeX();
125  unsigned int GetSizeY();
126 
127 
128  // TextureMapImplT implementation.
129  bool IsCreatedFromMapComp(const MapCompositionT& MC_);
130  GLuint GetOpenGLObject();
131  ~TextureMapCubeT();
132 
133 
134  // Internal implementation.
135  // Constructors.
136  TextureMapCubeT(const MapCompositionT& MapComp_);
137  TextureMapCubeT(char* Data_[6], unsigned long SizeX_, unsigned long SizeY_, char BytesPerPixel_, bool MakePrivateCopy, const MapCompositionT& McForFiltersAndWrapping);
138  TextureMapCubeT(BitmapT* Bitmap_[6], bool MakePrivateCopy, const MapCompositionT& McForFiltersAndWrapping);
139 };
140 
141 
143 {
144  public:
145 
146  // TextureMapManagerI implementation.
147  void SetMaxTextureSize(unsigned long MaxSize);
148  unsigned long GetMaxTextureSize() const;
150  MatSys::TextureMapI* GetTextureMap2D(char* Data, unsigned long SizeX, unsigned long SizeY, char BytesPerPixel, bool MakePrivateCopy, const MapCompositionT& McForFiltersAndWrapping);
151  MatSys::TextureMapI* GetTextureMap2D(BitmapT* Bitmap, bool MakePrivateCopy, const MapCompositionT& McForFiltersAndWrapping);
153 
154 
155  // Internal implementation.
156 
157  /// Creates a 2D texture-map by a texture-map composition. The function never fails.
158  /// Calling this multiple times with the same MapComp will return identical pointers.
160 
161  /// Creates a 2D texture-map from a pointer. The function never fails.
162  /// Calling this multiple times with identical paramaters will each time return a different pointer!
163  /// If MakePrivateCopy=true, the function makes a private copy of the data pointed to by Data. The caller can then free the original data.
164  /// If MakePrivateCopy=false, the function relies on the Data being valid and available during the entire lifetime of the returned texture map.
165  /// SizeX and SizeY MUST be powers of 2, and BytesPerPixel MUST be 3 or 4!
166  TextureMap2DT* GetTextureMap2DInternal(char* Data, unsigned long SizeX, unsigned long SizeY, char BytesPerPixel, bool MakePrivateCopy, const MapCompositionT& McForFiltersAndWrapping);
167 
168  /// Creates a 2D texture-map from a BitmapT. The function never fails.
169  /// Calling this multiple times with identical paramaters will each time return a different pointer!
170  /// If MakePrivateCopy=true, the function makes a private copy of the data pointed to by Data. The caller can then free the original data.
171  /// If MakePrivateCopy=false, the function relies on the Bitmap being valid and available during the entire lifetime of the returned texture map.
172  TextureMap2DT* GetTextureMap2DInternal(BitmapT* Bitmap, bool MakePrivateCopy, const MapCompositionT& McForFiltersAndWrapping);
173 
174  /// Creates a cube texture-map by a texture-map composition. The function never fails.
175  /// Calling this multiple times with the same MapComp will return identical pointers.
177 
178  /// Creates a cube texture-map from a pointer. The function never fails.
179  /// Calling this multiple times with identical paramaters will each time return a different pointer!
180  /// If MakePrivateCopy=true, the function makes a private copy of the data pointed to by Data. The caller can then free the original data.
181  /// If MakePrivateCopy=false, the function relies on the Data being valid and available during the entire lifetime of the returned texture map.
182  /// SizeX and SizeY MUST be powers of 2, and BytesPerPixel MUST be 3 or 4!
183  TextureMapCubeT* GetTextureMapCubeInternal(char* Data[6], unsigned long SizeX, unsigned long SizeY, char BytesPerPixel, bool MakePrivateCopy, const MapCompositionT& McForFiltersAndWrapping);
184 
185  /// Creates a cube texture-map from a BitmapT. The function never fails.
186  /// Calling this multiple times with identical paramaters will each time return a different pointer!
187  /// If MakePrivateCopy=true, the function makes a private copy of the data pointed to by Data. The caller can then free the original data.
188  /// If MakePrivateCopy=false, the function relies on the Bitmap being valid and available during the entire lifetime of the returned texture map.
189  TextureMapCubeT* GetTextureMapCubeInternal(BitmapT* Bitmap[6], bool MakePrivateCopy, const MapCompositionT& McForFiltersAndWrapping);
190 
191  /// Releases the texture map from the texture manager, and all its resources.
193 
194  /// Returns a reference to the texture-map repository.
195  const ArrayT<TextureMapImplT*>& GetTexMapRepository() const { return TexMapRepository; }
196 
197 
198  /// Get a pointer/reference to the texture-map manager singleton.
199  static TextureMapManagerImplT& Get();
200 
201 
202  private:
203 
204  TextureMapManagerImplT() : MaxTextureMapSize(4096) { }
205 
206  TextureMapManagerImplT(const TextureMapManagerImplT&); // Use of the Copy Constructor is not allowed.
207  void operator = (const TextureMapManagerImplT&); // Use of the Assignment Operator is not allowed.
208 
209  ArrayT<TextureMapImplT*> TexMapRepository;
210  ArrayT<unsigned long> TexMapRepositoryCount;
211  unsigned long MaxTextureMapSize;
212 };
213 
214 #endif
GLuint GetOpenGLObject()
This function returns an OpenGL object for this texture.
Definition: TextureMapImpl.cpp:465
unsigned long GetMaxTextureSize() const
Returns the currently set maximum texture size.
Definition: TextureMapImpl.cpp:659
bool IsCreatedFromMapComp(const MapCompositionT &MC_)
Returns true if this texture was created from a map composition that was "equivalent" to MC_...
Definition: TextureMapImpl.cpp:82
virtual bool IsCreatedFromMapComp(const MapCompositionT &MC_)=0
Returns true if this texture was created from a map composition that was "equivalent" to MC_...
GLuint GetOpenGLObject()
This function returns an OpenGL object for this texture.
Definition: TextureMapImpl.cpp:148
static TextureMapManagerImplT & Get()
Get a pointer/reference to the texture-map manager singleton.
Definition: TextureMapImpl.cpp:641
This class represents a RGBA bitmap.
Definition: Bitmap.hpp:20
This is an interface to the texture-map manager.
Definition: TextureMap.hpp:38
Definition: TextureMapImpl.hpp:142
MatSys::TextureMapI * GetTextureMap2D(const MapCompositionT &MapComp)
Creates a 2D texture-map by a texture-map composition.
Definition: TextureMapImpl.cpp:665
void FreeTextureMap(MatSys::TextureMapI *TM)
Releases the texture map from the texture manager, and releases all of its resources.
Definition: TextureMapImpl.cpp:683
bool IsCreatedFromMapComp(const MapCompositionT &MC_)
Returns true if this texture was created from a map composition that was "equivalent" to MC_...
Definition: TextureMapImpl.cpp:391
This is an interface to a texture-map.
Definition: TextureMap.hpp:23
void SetMaxTextureSize(unsigned long MaxSize)
Sets the maximum side length to which textures should be scaled down before they are employed for ren...
Definition: TextureMapImpl.cpp:653
virtual GLuint GetOpenGLObject()=0
This function returns an OpenGL object for this texture.
virtual ~TextureMapImplT()
Virtual destructor.
Definition: TextureMapImpl.hpp:41
const ArrayT< TextureMapImplT * > & GetTexMapRepository() const
Returns a reference to the texture-map repository.
Definition: TextureMapImpl.hpp:195
This class represents a cube texture-map.
Definition: TextureMapImpl.hpp:92
A MapCompositionT is a description of how a SINGLE texture map image is composited from several sourc...
Definition: MapComposition.hpp:18
This class represents a 2D texture-map.
Definition: TextureMapImpl.hpp:46
TextureMap2DT * GetTextureMap2DInternal(const MapCompositionT &MapComp)
Creates a 2D texture-map by a texture-map composition.
Definition: TextureMapImpl.cpp:689
This class represents a texture-map.
Definition: TextureMapImpl.hpp:27
TextureMapCubeT * GetTextureMapCubeInternal(const MapCompositionT &MapComp)
Creates a cube texture-map by a texture-map composition.
Definition: TextureMapImpl.cpp:723