Polygon Crucher SDK - Documentation
Documentation
Loading...
Searching...
No Matches
MaterialMap.h
Go to the documentation of this file.
1//! @file MaterialMap.h
2//! CMaterialMap class is the implementation for a texture map associated to a C3DMaterial.
3//
4//////////////////////////////////////////////////////////////////////
5
6#if !defined(AFX_MATERIALMAP_H__57174DE1_C363_11D3_AC9D_F94D2C3E2938__INCLUDED_)
7#define AFX_MATERIALMAP_H__57174DE1_C363_11D3_AC9D_F94D2C3E2938__INCLUDED_
8
9#ifdef _MSC_VER
10#pragma once
11#endif // _MSC_VER
12
13#include "3DAnimatable.h"
14#include "4x4Matrix.h"
15#include "3DVector.h"
16
17#ifndef MOOTOOLS_NO_BITMAP
18#include "Bmapmgr.h"
19#endif
20
21BEGIN_MOOTOOLS_NAMESPACE
22
23//! @enum MapType
24//! @brief This enum allows to know the kind of texture map.
25typedef enum MapType XEnumType(unsigned int)
26{
27 MAPTYPE_UNDEFINED = 0,
28 MAPTYPE_FIRST = 1,
29 MAPTYPE_DIFFUSE = 1, // BASE_COLOR in PBR
30 //MAPTYPE_BASE_COLOR = 1,
31 MAPTYPE_AMBIENT,
32 MAPTYPE_SPECULAR,
33 MAPTYPE_TRANSPARENCY,
34 MAPTYPE_BUMP,
35 MAPTYPE_NORMALMAP,
36 MAPTYPE_EMISSIVE, // Added for PBR
37 MAPTYPE_OCCLUSION, // Added for PBR
38// MAPTYPE_SPECULAR_GLOSSINESS, // -> RGB + A (for glossiness) ???
39 MAPTYPE_SPECULAR_COLOR, // -> RGB
40 MAPTYPE_SPECULAR_ALPHA, // -> A
41// MAPTYPE_METALLIC_ROUGHNESS, // Added for PBR -> R=metallic G=Roughness (gltf specs)
42 MAPTYPE_ROUGHNESS, // Added for PBR -> ? (3DS, Maya specs)
43 MAPTYPE_METALLIC, // Added for PBR -> ? (3DS, Maya specs)
44 MAPTYPE_GLOSS,
45 MAPTYPE_LAST,
46
47 MAPTYPE_MIXED = MAPTYPE_LAST+10, // This type is not a standard material map type found in a 3dmaterial. Is is a compound type return by MapMixer only
48} MapType;
49
50//! @enum MapProjection
51//! @brief Projection type of the texture map.
52typedef enum XEnumType(unsigned int)
53{
54 MAPPROJ_UNDEFINED = 0,
55 MAPPROJ_UVW, //!< A CUVWChannel is required for the map
56 MAPPROJ_PLANAR, //!< The following projection means that the mapping mode requires UVW to be generated as it is a mapping projection
57 MAPPROJ_CYLINDRICAL,
58 MAPPROJ_SPHERICAL,
59 MAPPROJ_CUBIC, //!< Lightwave cubic mode (front/top/right normal - back/bottom/left inverted projection)
60 MAPPROJ_CUBIC2, //!< C4D cubic mode (no inverted projection)
61 MAPPROJ_FRONT,
63
64//! @enum MapAxis
65//! @brief Axis direction of the mapping projection
66typedef enum XEnumType(unsigned int)
67{
68 MAPAXIS_X = 0,
69 MAPAXIS_Y,
70 MAPAXIS_Z,
71} MapAxis;
72
73//! @enum MAP_PROPERTIES
74//! @brief Different flags for the map
75typedef enum MAP_PROPERTIES XEnumType(unsigned int)
76{
77 MAPPROP_UNKNOWN = 0,
78 MAPPROP_NO_REPEAT_ON_U = 0x01,
79 MAPPROP_NO_REPEAT_ON_V = 0x02,
80 MAPPROP_DISABLE_ALPHA = 0x04, //!< Don't use alpha channel if there is any.
81
82 // Invalidation flags
83 MAPPROP_INVALIDATE = 0x01000000, //!< Something changed for the map. Try to load again the bitmap
85
86#define KEYFRAME_MAP_TILE KEYFRAMEID(C3DKeyframe::KEYFRAME_TYPE_VECTOR, MAKE_CUSTOM_ID('M', 'P', 'T', 'L'))
87#define KEYFRAME_MAP_SWAPMATRIX KEYFRAMEID(C3DKeyframe::KEYFRAME_TYPE_MATRIX, MAKE_CUSTOM_ID('M', 'P', 'S', 'W'))
88
89//! @class CMaterialMap
90//! @brief CMaterialMap handles material texture map.
91//! @details CMaterialMap refers to a bitmap file and optionnaly an alpha bitmap file.
92//! This material map has a type given by CMaterialMap::GetType and can be relative to the material diffuse, normap map, transparency...\n
93//! The material map defines also a projection type, get by CMaterialMap::GetProjection. MAPPROJ_UVW requires a CUVWChannel whereas the other value means that map is projected and the UV values are computed depending on the corresponding projection.
94class DLL_3DFUNCTION CMaterialMap : public CXObject, public C3DKeyframable
95{
96 friend class C3DMaterial;
97 friend class CMaterialMapCache;
98
99public:
100 typedef enum MaterialMapSize
101 {
102 MATERIALMAP_ANY_SIZE = -1, //!< the first bitmap found in the cache whatever the size
103 MATERIALMAP_DEFAULT_SIZE = 0, //!< the bitmap file size
104 } MaterialMapSize;
105
106 typedef enum MapChannelFlags XEnumType(unsigned int)
107 {
108 MAPCHANNEL_UNSPECIFIED = (unsigned int) - 1, //!< This means that existing flags are kept and not replaced by the new one
109 MAPCHANNEL_NONE = 0,
110 MAPCHANNEL_FIRST_CHANNEL_FOUND = 0x01, //!< Setting this value implies that the first channel found will be returned if the designated channel is not found
111 } MapChannelFlags;
112
113private:
114 unsigned int flags;
115 C3DScene* refScene; // Internal. cf. note in constructor
116 C3DMaterial* material;
117 CXString filename, alphafilename;
118 CCustomData data;
119 ChannelID frontChannelID, backChannelID;
120 unsigned int frontChannelFlags, backChannelFlags;
121 MapType type;
122 MapProjection projection;
123 MapAxis axis;
124
125 typedef struct MapPathPair
126 {
127 public:
128 CXString mainPath; // full path name of the file found
129 CXString alphaPath; // full path for the alpha file found
130
131 MapPathPair() {};
132 MapPathPair(const CXString& path, const CXString& alphaPath)
133 {
134 this->mainPath = path;
135 this->alphaPath = alphaPath;
136 this->mainPath.MakeLower();
137 this->alphaPath.MakeLower();
138 }
139 } MapPathPair;
140
142
143#ifndef MOOTOOLS_NO_BITMAP
144 unsigned char rgbaMapping[4];
145
146 typedef struct MapBitmap
147 {
148 MapPathPair paths;
149
150 enum MapFileStatus
151 {
152 MAPSTATUS_NOT_READED = 0, // the file hasn't be readed for the moment...
153 MAPSTATUS_READY, // Map is ready
154 MAPSTATUS_INVALID, // Map file is invalid
155 MAPSTATUS_CANT_FIND_FILE, // Cant find map file
156 } status;
157
158 CXSize fileBitmapSize; // The loaded file bitmap size
159 unsigned int requestedSize; // the size of the requested bitmap
160 Bitmap* bmap; // rgb or rgba bitmap
161 bool inCache; // true by default
162
163 MapBitmap();
164 virtual ~MapBitmap();
165
166 static bool CheckFileChange(const Bitmap* bmap, const MapPathPair& paths);
167 bool CheckFileChange(bool invalidate);
168 void DeleteBitmap(bool resetInvalidStatus = false);
169 bool ReadBitmap(const CXString& filename, const CXString& alphafilename, const unsigned char* rgbaMapping = NULL, unsigned int width = MATERIALMAP_DEFAULT_SIZE);
170 CLayerBitmap* ReadBitmapLayer(const CXString& fullpath, unsigned int width = MATERIALMAP_DEFAULT_SIZE);
171 } MapBitmap;
172
173 MapBitmap bmapinfo;
174 bool ReadBitmap(const CXString& filename, const CXString& alphafilename, unsigned int width = MATERIALMAP_DEFAULT_SIZE);
175#else
176 typedef struct MapBitmap
177 {
178 MapPathPair paths;
179 } MapBitmap;
180
181 MapBitmap bmapinfo;
182#endif
183
184 void SetMaterial(C3DMaterial* material);
185 bool FindBitmap(const CXString& tryname, CXString& findname, bool fullPath) const;
186
187protected: // Access for C3DMaterial
188 static CMaterialMap *Create(C3DMaterial *material, const CXString& filename, ChannelID frontChannelID, MapType type, MapProjection proj, unsigned int frontChannelFlags = MAPCHANNEL_NONE); //!< Create a material map. Use C3DMaterial::AddMaterialMap to create and add it to a C3DMaterial
189 CMaterialMap(C3DMaterial* material); //!< Use only by C3DMaterials
190
191public:
192 virtual ~CMaterialMap();
193 virtual void Serialize( CXArchive& ar );
194
195 C3DMaterial* GetMaterial() const; //!< Return the material to which the map is attached
196 C3DScene* GetScene() const; //!< Return scene to which the map belong to (from C3DMaterials)
197
198 static CXString GetMapName(MapType type);
199
200 CXString GetBitmapPath(bool alpha, bool fullPath = false) const; //!< fullPath = true ensure that we have no relative path, otherwise the path might be relative to the scene filepath. Alpha = true, means that we return the alpha filename
201 CXString AdjustPath(CXString filename, const CXString& newTargetPath = CXString(), bool allowRelative = true) const; //!< Adjust a single filename and make it relative to newTargetPath if provided or the scene path otherwise.
202 void AdjustPaths(const CXString& targetPath, bool relativePathAllowed); //!< Make all internal filenames used by materials relative to provided path
203
204 bool BitmapFileExists(bool alpha = false); //!< Check if the bitmap file exists
205 fileuint GetFileSize(bool alpha = false); //!< The file size of the source bitmap which might differ from the size of the requested bitmap
206 bool HasAlpha(); //!< Check if the map bitmap file has an alpha channel (if MAPPROP_DISABLE_ALPHA not set)
207
208#ifndef MOOTOOLS_NO_BITMAP
209 const Bitmap *GetBitmap(unsigned int maxSize = MATERIALMAP_DEFAULT_SIZE, bool keepAlreadyLoadedIfBigger = false); //!< Bitmap is assumed to have G8_BITMAP, GA_BITMAP, RGB_BITMAP or RGBA_BITMAP type. The bitmap is automatically released by the CMaterialMap
210 bool HasBitmap() const;
211
212 CXSize GetFileBitmapSize(bool alpha = false) const; //!< The size of the source bitmap which might differ from the size of the requested bitmap
213 BitmapType GetFileBitmapType(bool alpha = false); //!< The source bitmap type (no MappingMode applied)
214 BitmapType GetBitmapType() const; //!< The type of the map bitmap (depending on MappingMaskMode, and alpha filename)
215
216 //! @brief Defines an array of 4 char that define how to process rgba pixels
217 //! @details unsigned char mask[4] = { MAPRGBA_INV_G, MAPRGBA_R, MAPRGBA_A, MAPRGBA_INV_B} will read the bitmap then provided rgba bitmap with pixels of type {-green, red, alpha, -blue}
218 //! unsigned char mask[4] = { MAPRGBA_INV_G, MAPRGBA_OFF, MAPRGBA_OFF, MAPRGBA_OFF} will read the bitmap then provided g8 bitmap with pixels of type {-green }\n
219 //! unsigned char mask[4] = { MAPRGBA_R, MAPRGBA_G, MAPRGBA_B, MAPRGBA_A} will read the bitmap then provided rgbaBitmap { red, green, blu, alpha }. This is the default pixel mapping. In that case only, if no alpha is available in the input bitmap, it only returns an rgbBitmap\n
220 void SetMappingMask(const unsigned char rgbaMappingMask[4]);
221 bool GetMappingMask(unsigned char rgbaMappingMask[4]) const; //!< Return true if default mapping
222 bool IsSameMappingMask(const unsigned char rgbaMappingMask[4]) const; //!< Compare both mapping mask. That allow to know that we read a map with a given format, and want to save to the same format
223
224 //! @brief MapMixer allows to compound the bitmaps issued from different map.
225 //! @details You can specify up to 4 maps through the variable arguments which must be terminated by NULL
226 //! CMaterialMap* map1, const unsigned char rgbaMapping1[4], CMaterialMap* map2, const unsigned char rgbaMapping2[4], ..., NULL)\n
227 //! the resulting map is issued from the combination of rgbaMapping1 and rgbaMapping2\n
228 //! unsigned char rgbaMapping1[4] = { MAPRGBA_INV_G, MAPRGBA_OFF, MAPRGBA_OFF, MAPRGBA_OFF}\n
229 //! unsigned char rgbaMapping2[4] = { MAPRGBA_OFF, MAPRGBA_R, MAPRGBA_OFF, MAPRGBA_OFF}\n
230 //! will generate an RGB_BITMAP bitmap with {-g1, r2, 0} where g1 is green of map1 and r2 is red of map2\n
231 //!
232 //! unsigned char rgbaMapping1[4] = { MAPRGBA_INV_G, MAPRGBA_OFF, MAPRGBA_OFF, MAPRGBA_OFF}\n
233 //! unsigned char rgbaMapping2[4] = { MAPRGBA_OFF, MAPRGBA_R, MAPRGBA_OFF, MAPRGBA_INV_A}\n
234 //! will generate an RGBA_BITMAP bitmap with {-g1, r2, 0, -a2}\n
235 //!
236 //! unsigned char rgbaMapping1[4] = { MAPRGBA_INV_G, MAPRGBA_OFF, MAPRGBA_OFF, MAPRGBA_OFF}\n
237 //! will generate a G8_BITMAP from the input bitmap with {-g1 } pixels\n
238 //!
239 //! unsigned char rgbaMapping1[4] = { MAPRGBA_INV_G, MAPRGBA_OFF, MAPRGBA_OFF, MAPRGBA_OFF}\n
240 //! unsigned char rgbaMapping2[4] = { MAPRGBA_OFF, MAPRGBA_OFF, MAPRGBA_OFF, MAPRGBA_INV_A}\n
241 //! will generate a GA_BITMAP from the input bitmap with {-g1, -a2}\n
242
243 //!< The returned material map must be free by the caller using xDelete(map);
244 static CMaterialMap* MapMixer(unsigned int dstMapSize = MATERIALMAP_DEFAULT_SIZE, ...);
245
246 //! @brief Same feature as CMaterialMap::MapMixer except than the variable argument type differs.
247 //! @details You can specify up to 4 maps through the variable arguments which must be terminated by NULL
248 //! const Bitmap* bmap1, const char rgbaMapping1[4], const Bitmap* bmap2, const char rgbaMapping2[4], ..., NULL)
249 static Bitmap* BitmapMixer(unsigned int dstMapSize = MATERIALMAP_DEFAULT_SIZE, ...);
250
251 static const Bitmap* LoadBitmap(C3DScene* scene, const CXString& bmappath); //!< Load a bitmap at the specified path taking advantage of CMaterialMap loading mechanism
252 static bool UnloadBitmap(const Bitmap *bmap); //!< Release the bitmap loaded with LoadBitmap
253#endif
254
255 // operator
256 bool operator==(CMaterialMap &refMap) const;
257 CMaterialMap& operator=(CMaterialMap& refmap);
258
259 CCustomData& GetCustomData();
260 const CCustomData& GetCustomData() const;
261
262 void SetFlag(MAP_PROPERTIES flag, bool set);
263 bool IsFlagSet(MAP_PROPERTIES flag) const;
264
265 void SetType(MapType reftype);
266 MapType GetType() const;
267
268 void SetProjection(MapProjection reftype);
269 MapProjection GetProjection() const;
270
271 void SetAlphaFileName(const CXString& filename, const CXString& newReferencePath = CXString()); //!< operator If newReferencePath is provided, the filename will be make relative to that directory if possible
272 void SetFileName(const CXString& filename);
273 void SetHPBRotation(unsigned int time, const C3DVector& hpbvector);
274
275 void ResetTransformation();
276 void SetScale(unsigned int time, const C3DVector& vect);
277 void Scale(unsigned int time, const C3DVector& vect);
278 void SetTranslation(unsigned int time, const C3DVector& vect);
279 void Translate(unsigned int time, const C3DVector& vect);
280 void SetRotation(unsigned int time, const CQuaternion& quat);
281 void Rotate(unsigned int time, const CQuaternion& quat);
282 void SetTile(unsigned int time, const C3DVector& vect);
283 void SetMatrix(unsigned int time, const C4x4Matrix& matrix);
284 void SetAxis(MapAxis axis);
285
286 //! @brief Handle the channel id of the material map. The corresponding channel is an UVW_CHANNEL
287 //! @details The material might define a back channel ID for double sided face that have both side the same material with different UVs
288 //! If no back channel is define, then front / back side share the same UV and GetChannelID return frontChannelID for front / back side\n
289 void SetChannelID(ChannelID uvwChannelID, bool front = true, unsigned int channelFlags = MAPCHANNEL_UNSPECIFIED);
290 ChannelID GetChannelID(bool front = true, unsigned int *channelFlags = NULL) const; //!< Return the material map channel ID, as set in SetChannelID (the flag MAPPROP_USE_FRONT_CHANNEL_WHEN_BACK_MISSING is not taken into account). Can also return CHANNELID_FIRST_CHANNEL_FOUND. GetUVWChannelID should be used as an helper to get the final channel ID to use for a given object
291
292 void ChangeChannelID(CHashMap<CChannelInfo, const CChannelInfo&, CChannelInfo>& channelMap); //!< Remap the materialmap channel ID to the new channelID provided by the map.
293
294 void GetFileName(CXString& filename) const; //!< This returns the raw image filename as it is stored in the map, when reading a file. The path can be relative, absolute... Use GetBitmapPath resolves absolute / relative path and potentially fixe texture location changes.
295 void GetAlphaFileName(CXString& filename) const; //!< Same as GetFileName, but for the alpha image
296 void GetScale(unsigned int time, C3DVector& vect) const;
297 void GetTranslation(unsigned int time, C3DVector& vect) const;
298 void GetHPBRotation(unsigned int time, C3DVector& vect) const;
299 void GetRotation(unsigned int time, CQuaternion& vect) const;
300 void GetMatrix(unsigned int time, C4x4Matrix& vect) const;
301 void GetTile(unsigned int time, C3DVector& vect) const;
302 MapAxis GetAxis() const;
303
304 void SwapCoordinates(unsigned int swapmode);
305};
306
307END_MOOTOOLS_NAMESPACE
308
309#endif // !defined(AFX_MATERIALMAP_H__57174DE1_C363_11D3_AC9D_F94D2C3E2938__INCLUDED_)
C3DKeyframable class for handling keyframes.
MapProjection
Projection type of the texture map.
Definition MaterialMap.h:53
@ MAPPROJ_PLANAR
The following projection means that the mapping mode requires UVW to be generated as it is a mapping ...
Definition MaterialMap.h:56
@ MAPPROJ_CUBIC
Lightwave cubic mode (front/top/right normal - back/bottom/left inverted projection)
Definition MaterialMap.h:59
@ MAPPROJ_CUBIC2
C4D cubic mode (no inverted projection)
Definition MaterialMap.h:60
@ MAPPROJ_UVW
A CUVWChannel is required for the map.
Definition MaterialMap.h:55
MapType
This enum allows to know the kind of texture map.
Definition MaterialMap.h:26
MapAxis
Axis direction of the mapping projection.
Definition MaterialMap.h:67
MAP_PROPERTIES
Different flags for the map.
Definition MaterialMap.h:76
@ MAPPROP_DISABLE_ALPHA
Don't use alpha channel if there is any.
Definition MaterialMap.h:80
@ MAPPROP_INVALIDATE
Something changed for the map. Try to load again the bitmap.
Definition MaterialMap.h:83
CXTString< TCHAR > CXString
CXString depend on the target OS. Could be CXStringW (Windows) or CXStringA (Linux / Macos)
Definition XString.h:118
This is the base class for any object that can be animated using C3DKeyframe.
Definition 3DAnimatable.h:51
C3DMaterial is the base class for materials. There is currently two kinds of materials: C3DPbrMateria...
Definition 3DMaterial.h:69
The class allows to get access to the scene graph, node hierarchy, material.
Definition 3DScene.h:306
The class defines an x, y, z 3D point which can use int, float or double.
Definition 3DPoint.h:27
CCustomData is a handly class for storing any kind of data.
Definition CustomData.h:106
CMaterialMap handles material texture map.
Definition MaterialMap.h:95
C3DScene * GetScene() const
Return scene to which the map belong to (from C3DMaterials)
fileuint GetFileSize(bool alpha=false)
The file size of the source bitmap which might differ from the size of the requested bitmap.
CXString AdjustPath(CXString filename, const CXString &newTargetPath=CXString(), bool allowRelative=true) const
Adjust a single filename and make it relative to newTargetPath if provided or the scene path otherwis...
bool BitmapFileExists(bool alpha=false)
Check if the bitmap file exists.
CXString GetBitmapPath(bool alpha, bool fullPath=false) const
fullPath = true ensure that we have no relative path, otherwise the path might be relative to the sce...
MaterialMapSize
Definition MaterialMap.h:101
MapChannelFlags
Definition MaterialMap.h:107
void AdjustPaths(const CXString &targetPath, bool relativePathAllowed)
Make all internal filenames used by materials relative to provided path.
void GetFileName(CXString &filename) const
This returns the raw image filename as it is stored in the map, when reading a file....
void ChangeChannelID(CHashMap< CChannelInfo, const CChannelInfo &, CChannelInfo > &channelMap)
Remap the materialmap channel ID to the new channelID provided by the map.
bool HasAlpha()
Check if the map bitmap file has an alpha channel (if MAPPROP_DISABLE_ALPHA not set)
void GetAlphaFileName(CXString &filename) const
Same as GetFileName, but for the alpha image.
void SetAlphaFileName(const CXString &filename, const CXString &newReferencePath=CXString())
operator If newReferencePath is provided, the filename will be make relative to that directory if pos...
C3DMaterial * GetMaterial() const
Return the material to which the map is attached.
void SetChannelID(ChannelID uvwChannelID, bool front=true, unsigned int channelFlags=MAPCHANNEL_UNSPECIFIED)
Handle the channel id of the material map. The corresponding channel is an UVW_CHANNEL.
ChannelID GetChannelID(bool front=true, unsigned int *channelFlags=NULL) const
Return the material map channel ID, as set in SetChannelID (the flag MAPPROP_USE_FRONT_CHANNEL_WHEN_B...
The class defines a quaternion transformation.
Definition Quaternion.h:20
Definition XArchive.h:17
Definition XRect.h:64