aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/shared/external/include/ApexMaterial.h
blob: 5f8ff4eba90401432097775be23f0c2b9366bf33 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//  * Neither the name of NVIDIA CORPORATION nor the names of its
//    contributors may be used to endorse or promote products derived
//    from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2018 NVIDIA Corporation. All rights reserved.


#ifndef _MATERIAL_H_
#define _MATERIAL_H_

#include <stdio.h>
#include <string>
#include "ApexUsingNamespace.h"
#include "RenderMesh.h"
#include "MaterialLibrary.h"
#include "PxVec3.h"
#include <vector>

/**
	A generic texture map.  Loads from a variety of file formats, but is stored in a unified basic format.
	May be (de)serialized from/to an physx::PxFileBuf.
*/
class ApexDefaultTextureMap : public TextureMap
{
public:
	ApexDefaultTextureMap();
	ApexDefaultTextureMap(const ApexDefaultTextureMap& textureMap)
	{
		*this = textureMap;
	}
	ApexDefaultTextureMap&	operator = (const ApexDefaultTextureMap& textureMap);
	virtual					~ApexDefaultTextureMap();

	void					build(PixelFormat format, uint32_t width, uint32_t height, uint32_t* fillColor = NULL);

	/** Deallocates all buffers and sets all values to the default constructor values. */
	void					unload();

	/** Saves the generic texture data to an physx::PxFileBuf. */
	void					serialize(physx::PxFileBuf& stream) const;

	/** Loads generic texture data from an physx::PxFileBuf. */
	void					deserialize(physx::PxFileBuf& stream, uint32_t version);

	// Texture API
	PixelFormat		getPixelFormat() const
	{
		return mPixelFormat;
	}
	uint32_t					getWidth() const
	{
		return mWidth;
	}
	uint32_t					getHeight() const
	{
		return mHeight;
	}
	uint32_t					getComponentCount() const
	{
		return mComponentCount;
	}
	uint32_t					getPixelBufferSize() const
	{
		return mPixelBufferSize;
	}
	uint8_t*					getPixels() const
	{
		return mPixelBuffer;
	}

protected:

	PixelFormat	mPixelFormat;
	uint32_t				mWidth;
	uint32_t				mHeight;
	uint32_t				mComponentCount;
	uint32_t				mPixelBufferSize;
	uint8_t*				mPixelBuffer;
};


class ApexDefaultMaterial : public Material
{
public:

	ApexDefaultMaterial();
	ApexDefaultMaterial(const ApexDefaultMaterial& material)
	{
		*this = material;
	}
	ApexDefaultMaterial&	operator = (const ApexDefaultMaterial& material);
	virtual					~ApexDefaultMaterial();

	/** Sets the name of the material, for lookup by the named resource provider. */
	void					setName(const char* name);

	/** Sets one of the material's texture maps (diffuse or normal) */
	bool					setTextureMap(TextureMapType type, ApexDefaultTextureMap* textureMap);

	/** Sets the ambient lighting color. */
	void					setAmbient(const physx::PxVec3& ambient)
	{
		mAmbient = ambient;
	}

	/** Sets the diffuse lighting color. */
	void					setDiffuse(const physx::PxVec3& diffuse)
	{
		mDiffuse = diffuse;
	}

	/** Sets the specular lighting color. */
	void					setSpecular(const physx::PxVec3& specular)
	{
		mSpecular = specular;
	}

	/** Sets material's opacity. */
	void					setAlpha(float alpha)
	{
		mAlpha = alpha;
	}

	/** Sets the material's shininess (specular power). */
	void					setShininess(float shininess)
	{
		mShininess = shininess;
	}

	/** Deallocates all buffers and sets all values to the default constructor values. */
	void					unload();

	/** Saves the material to an physx::PxFileBuf. */
	void					serialize(physx::PxFileBuf& stream) const;

	/** Loads material from an physx::PxFileBuf. */
	void					deserialize(physx::PxFileBuf& stream, uint32_t version);

	// Material API
	const char*				getName() const
	{
		return mName.c_str();
	}
	TextureMap*		getTextureMap(TextureMapType type) const;
	const physx::PxVec3&			getAmbient() const
	{
		return mAmbient;
	}
	const physx::PxVec3&			getDiffuse() const
	{
		return mDiffuse;
	}
	const physx::PxVec3&			getSpecular() const
	{
		return mSpecular;
	}
	float					getAlpha() const
	{
		return mAlpha;
	}
	float					getShininess() const
	{
		return mShininess;
	}

private:

	std::string				mName;

	ApexDefaultTextureMap*	mTextureMaps[TEXTURE_MAP_TYPE_COUNT];

	physx::PxVec3					mAmbient;
	physx::PxVec3					mDiffuse;
	physx::PxVec3					mSpecular;
	float					mAlpha;
	float					mShininess;
};


class ApexDefaultMaterialLibrary : public MaterialLibrary
{
public:

	ApexDefaultMaterialLibrary();
	ApexDefaultMaterialLibrary(const ApexDefaultMaterialLibrary& materialLibrary)
	{
		*this = materialLibrary;
	}
	ApexDefaultMaterialLibrary&	operator = (const ApexDefaultMaterialLibrary& material);
	virtual						~ApexDefaultMaterialLibrary();

	/** Deallocates all buffers and sets all values to the default constructor values. */
	void						unload();

	/** Returns the number of materials in the library */
	uint32_t						getMaterialCount() const
	{
		return (uint32_t)mMaterials.size();
	}

	/**
		Access to the materials by index.
		Valid range of materialIndex is 0 to getMaterialCount()-1.
	*/
	ApexDefaultMaterial*		getMaterial(uint32_t materialIndex) const;

	/**
		Remove and delete named material.
		Returns true if the material was found, false if it was not.
	*/
	virtual bool				deleteMaterial(const char* materialName);

	/**
		Adds the materials from the given materialLibrary, which
		aren't already in this material library.  (Based upon name.)
	*/
	void						merge(const ApexDefaultMaterialLibrary& materialLibrary);

	// MaterialLibrary API

	/** Saves the material to an physx::PxFileBuf. */
	void						serialize(physx::PxFileBuf& stream) const;

	/** Loads material from an physx::PxFileBuf. */
	void						deserialize(physx::PxFileBuf& stream);

	Material*				getMaterial(const char* materialName, bool& created);

	/* Returns -1 if the material is not found */
	int32_t						findMaterialIndex(const char* materialName);

private:

	std::vector<ApexDefaultMaterial*>	mMaterials;
};


#endif // #ifndef _MATERIAL_H_