ScummVM API documentation
Material.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * Copyright (C) 2006-2010 - Frictional Games
24  *
25  * This file is part of HPL1 Engine.
26  */
27 
28 #ifndef HPL_MATERIAL_H
29 #define HPL_MATERIAL_H
30 
31 #include "common/array.h"
32 #include "common/list.h"
33 #include "hpl1/engine/graphics/GPUProgram.h"
34 #include "hpl1/engine/graphics/LowLevelGraphics.h"
35 #include "hpl1/engine/graphics/Texture.h"
36 #include "hpl1/engine/resources/ResourceBase.h"
37 #include "hpl1/engine/resources/ResourceImage.h"
38 #include "hpl1/engine/system/SystemTypes.h"
39 
40 class TiXmlElement;
41 
42 namespace hpl {
43 
44 #define kMaxProgramNum (5)
45 
46 enum eMaterialQuality {
47  eMaterialQuality_VeryLow = 0,
48  eMaterialQuality_Low = 1,
49  eMaterialQuality_Medium = 2,
50  eMaterialQuality_High = 3,
51  eMaterialQuality_VeryHigh = 4,
52  eMaterialQuality_LastEnum = 5
53 };
54 
55 enum eMaterialTexture {
56  eMaterialTexture_Diffuse,
57  eMaterialTexture_NMap,
58  eMaterialTexture_Specular,
59  eMaterialTexture_Alpha,
60  eMaterialTexture_Illumination,
61  eMaterialTexture_CubeMap,
62  eMaterialTexture_Refraction,
63  eMaterialTexture_LastEnum
64 };
65 
66 enum eMaterialType {
67  eMaterialType_Null,
68 
69  eMaterialType_Diffuse,
70  eMaterialType_DiffuseAlpha,
71  eMaterialType_DiffuseAdditive,
72  eMaterialType_DiffuseNMap,
73  eMaterialType_DiffuseSpecular,
74  eMaterialType_BumpSpec,
75  eMaterialType_Smoke,
76 
77  eMaterialType_FontNormal,
78 
79  eMaterialType_LastEnum
80 };
81 
82 enum eMaterialPicture {
83  eMaterialPicture_Image,
84  eMaterialPicture_Texture,
85  eMaterialPicture_LastEnum
86 };
87 
88 enum eMaterialRenderType {
89  eMaterialRenderType_Z,
90  eMaterialRenderType_Light,
91  eMaterialRenderType_Diffuse,
92  eMaterialRenderType_LastEnum
93 };
94 
95 enum eMaterialBlendMode {
96  eMaterialBlendMode_None,
97  eMaterialBlendMode_Add,
98  eMaterialBlendMode_Mul,
99  eMaterialBlendMode_MulX2,
100  eMaterialBlendMode_Replace,
101  eMaterialBlendMode_Alpha,
102  eMaterialBlendMode_DestAlphaAdd,
103  eMaterialBlendMode_LastEnum
104 };
105 
106 enum eMaterialAlphaMode {
107  eMaterialAlphaMode_Solid,
108  eMaterialAlphaMode_Trans,
109  eMaterialAlphaMode_LastEnum,
110 };
111 
113 enum eMaterialChannelMode {
114  eMaterialChannelMode_RGBA,
115  eMaterialChannelMode_RGB,
116  eMaterialChannelMode_A,
117  eMaterialChannelMode_Z,
118  eMaterialChannelMode_LastEnum,
119 };
120 
121 //---------------------------------------------------
122 
123 class cRenderer2D;
124 class cRenderer3D;
125 class cRenderSettings;
126 class cTextureManager;
127 class cImageManager;
128 class cGpuProgramManager;
129 class iLight;
130 class iCamera;
131 class iLight3D;
132 
133 //---------------------------------------------------
134 
135 class iGLStateProgram : public iGpuProgram {
136 public:
137  iGLStateProgram(tString asName)
138  : iGpuProgram(asName) {
139  mbSetUpDone = false;
140  }
141  virtual ~iGLStateProgram() {}
142 
143  void SetUp(iLowLevelGraphics *apLowLevelGraphics) {
144  if (mbSetUpDone == false) {
145  mpLowGfx = apLowLevelGraphics;
146  mbSetUpDone = true;
147  InitData();
148  }
149  }
150 
151  virtual void Bind() = 0;
152  virtual void UnBind() = 0;
153 
154  bool CreateFromFile(const tString &asFile, const tString &asEntry) { return false; }
155  bool SetFloat(const tString &asName, float afX) { return false; }
156 
157  bool SetVec2f(const tString &asName, float afX, float afY) { return false; }
158  bool SetVec3f(const tString &asName, float afX, float afY, float afZ) { return false; }
159  bool SetVec4f(const tString &asName, float afX, float afY, float afZ, float afW) { return false; }
160  bool SetMatrixf(const tString &asName, const cMatrixf &mMtx) { return false; }
161  bool SetMatrixf(const tString &asName, eGpuProgramMatrix mType, eGpuProgramMatrixOp mOp) { return false; }
162  bool SetTexture(const tString &asName, iTexture *apTexture, bool abAutoDisable = true) { return false; }
163  bool SetTextureToUnit(int alUnit, iTexture *apTexture) { return false; }
164 
165  eGpuProgramType GetType() { return eGpuProgramType_LastEnum; }
166 
167  bool reload() { return false; }
168  void unload() {}
169  void destroy() {}
170 
171 protected:
172  iLowLevelGraphics *mpLowGfx;
173 
174  bool mbSetUpDone;
175 
176  virtual void InitData() = 0;
177 };
178 
179 //---------------------------------------------------------------
180 
182 public:
183  virtual ~iMaterialProgramSetup() = default;
184  virtual void Setup(iGpuProgram *apProgram, cRenderSettings *apRenderSettings) = 0;
185  virtual void SetupMatrix(cMatrixf *apModelMatrix, cRenderSettings *apRenderSettings) {}
186 };
187 
188 //---------------------------------------------------
189 
191 public:
192  cTextureType(tString asSuffix, eMaterialTexture aType) : msSuffix(asSuffix), mType(aType) {}
193 
194  tString msSuffix;
195  eMaterialTexture mType;
196 };
197 
200 
201 //---------------------------------------------------
202 
203 class iMaterial : public iResourceBase {
204 public:
205  iMaterial(const tString &asName, iLowLevelGraphics *apLowLevelGraphics,
206  cImageManager *apImageManager, cTextureManager *apTextureManager,
207  cRenderer2D *apRenderer, cGpuProgramManager *apProgramManager,
208  eMaterialPicture aPicture, cRenderer3D *apRenderer3D);
209  virtual ~iMaterial();
210 
211  // resources stuff.
212  bool reload() { return false; }
213  void unload() {}
214  void destroy() {}
215 
216  virtual void Compile() = 0;
217 
218  virtual void Update(float afTimeStep) {}
219 
220  // The new render system stuff
221  virtual iGpuProgram *getGpuProgram(const eMaterialRenderType aType, const int alPass, iLight3D *apLight) { return nullptr; }
222  virtual iMaterialProgramSetup *getGpuProgramSetup(const eMaterialRenderType aType, const int alPass, iLight3D *apLight) { return nullptr; };
223 
224  virtual iGpuProgram *GetVertexProgram(eMaterialRenderType aType, int alPass, iLight3D *apLight) { return NULL; }
225  virtual bool VertexProgramUsesLight(eMaterialRenderType aType, int alPass, iLight3D *apLight) { return false; }
226  virtual bool VertexProgramUsesEye(eMaterialRenderType aType, int alPass, iLight3D *apLight) { return false; }
227  virtual iMaterialProgramSetup *GetVertexProgramSetup(eMaterialRenderType aType, int alPass, iLight3D *apLight) { return NULL; }
228 
229  virtual iGpuProgram *GetFragmentProgram(eMaterialRenderType aType, int alPass, iLight3D *apLight) { return NULL; }
230  virtual iMaterialProgramSetup *GetFragmentProgramSetup(eMaterialRenderType aType, int alPass, iLight3D *apLight) { return NULL; }
231 
232  virtual eMaterialAlphaMode GetAlphaMode(eMaterialRenderType aType, int alPass, iLight3D *apLight) { return eMaterialAlphaMode_Solid; }
233  virtual eMaterialBlendMode GetBlendMode(eMaterialRenderType aType, int alPass, iLight3D *apLight) { return eMaterialBlendMode_Replace; }
234  virtual eMaterialChannelMode GetChannelMode(eMaterialRenderType aType, int alPass, iLight3D *apLight) { return eMaterialChannelMode_RGBA; }
235 
236  virtual iTexture *GetTexture(int alUnit, eMaterialRenderType aType, int alPass, iLight3D *apLight) { return NULL; }
237  virtual eMaterialBlendMode GetTextureBlend(int alUnit, eMaterialRenderType aType, int alPass, iLight3D *apLight) { return eMaterialBlendMode_None; }
238 
239  virtual int GetNumOfPasses(eMaterialRenderType aType, iLight3D *apLight) { return 0; }
240 
241  virtual bool UsesType(eMaterialRenderType aType) { return false; }
242 
243  bool HasAlpha() { return mbHasAlpha; }
244  void SetHasAlpha(bool abX) { mbHasAlpha = abX; }
245 
246  bool GetDepthTest() { return mbDepthTest; }
247  void SetDepthTest(bool abX) { mbDepthTest = abX; }
248 
249  float GetValue() { return mfValue; }
250  void SetValue(float afX) { mfValue = afX; }
251 
252  virtual bool LoadData(TiXmlElement *apRootElem) { return true; }
253 
261  virtual bool StartRendering(eMaterialRenderType aType, iCamera *apCam, iLight *pLight) = 0;
266  virtual void EndRendering(eMaterialRenderType mType) = 0;
272  virtual tVtxBatchFlag GetBatchFlags(eMaterialRenderType mType) = 0;
273 
279  virtual bool NextPass(eMaterialRenderType mType) = 0;
285  virtual bool HasMultiplePasses(eMaterialRenderType mType) = 0;
286 
292  virtual eMaterialType GetType(eMaterialRenderType mType) = 0;
293  virtual void EditVertexes(eMaterialRenderType mType, iCamera *apCam, iLight *pLight,
294  tVertexVec *apVtxVec, cVector3f *apTransform, unsigned int alIndexAdd) = 0;
295 
296  iTexture *GetTexture(eMaterialTexture aType);
297  cRect2f GetTextureOffset(eMaterialTexture aType);
298 
299  void SetTexture(iTexture *apTex, eMaterialTexture aType) { mvTexture[aType] = apTex; }
300  void SetImage(cResourceImage *apImg, eMaterialTexture aType) {
301  mvImage[aType] = apImg;
302  }
303  cResourceImage *GetImage(eMaterialTexture aType) { return mvImage[aType]; }
304 
305  void SetProgram(iGpuProgram *apProgram, eGpuProgramType aType, unsigned int alNum) {
306  mpProgram[aType][alNum] = apProgram;
307  }
308  iGpuProgram *GetProgram(eGpuProgramType aType, unsigned int alNum) {
309  return mpProgram[aType][alNum];
310  }
311 
316  virtual bool IsTransperant() { return mbIsTransperant; }
317  virtual bool IsGlowing() { return mbIsGlowing; }
322  virtual bool UsesLights() { return mbUsesLights; }
323 
324  virtual tTextureTypeList GetTextureTypes() {
325  tTextureTypeList vTypes;
326  vTypes.push_back(cTextureType("", eMaterialTexture_Diffuse));
327  return vTypes;
328  }
329 
334  void SetId(int alId) { mlId = alId; }
335  int GetId() { return mlId; }
336 
337  virtual iGpuProgram *getRefractionProgram() { return nullptr; }
338 
339  virtual bool GetRefractionUsesDiffuse() { return false; }
340  virtual eMaterialTexture GetRefractionDiffuseTexture() { return eMaterialTexture_Diffuse; }
341 
342  virtual bool GetRefractionUsesEye() { return false; }
343  virtual bool GetRefractionUsesTime() { return false; }
344  virtual bool GetRefractionSkipsStandardTrans() { return false; }
345 
346  const tString &GetPhysicsMaterial() { return msPhysicsMaterial; }
347  void SetPhysicsMaterial(const tString &asName) { msPhysicsMaterial = asName; }
348 
349  static void SetQuality(eMaterialQuality aQuality) { mQuality = aQuality; }
350  static eMaterialQuality GetQuality() { return mQuality; }
351 
352 protected:
353  iLowLevelGraphics *mpLowLevelGraphics;
354  cImageManager *mpImageManager;
355  cTextureManager *mpTextureManager;
356  cRenderer2D *mpRenderer;
357  cRenderer3D *mpRenderer3D;
358  cRenderSettings *mpRenderSettings;
359  cGpuProgramManager *mpProgramManager;
360 
361  static eMaterialQuality mQuality;
362 
363  bool mbIsTransperant;
364  bool mbIsGlowing;
365  bool mbUsesLights;
366  bool mbHasAlpha;
367  bool mbDepthTest;
368  float mfValue;
369 
370  int mlId;
371 
372  tString msPhysicsMaterial;
373 
374  tTextureVec mvTexture;
375  tResourceImageVec mvImage;
376 
377  eMaterialType mType;
378  eMaterialPicture mPicture;
379 
380  iGpuProgram *mpProgram[2][kMaxProgramNum];
381 
382  int mlPassCount;
383 
384  // void Destroy();
385 };
386 
388 typedef tMaterialVec::iterator tMaterialVecIt;
389 
391 public:
392  virtual ~iMaterialType() = default;
393  virtual bool IsCorrect(tString asName) = 0;
394  virtual iMaterial *Create(const tString &asName, iLowLevelGraphics *apLowLevelGraphics,
395  cImageManager *apImageManager, cTextureManager *apTextureManager,
396  cRenderer2D *apRenderer, cGpuProgramManager *apProgramManager,
397  eMaterialPicture aPicture, cRenderer3D *apRenderer3D) = 0;
398 };
399 
402 
403 } // namespace hpl
404 
405 #endif // HPL_MATERIAL_H
Definition: AI.h:36
void destroy()
Definition: Material.h:169
Definition: Material.h:390
Definition: str.h:59
Definition: Renderer3D.h:77
virtual void UnBind()=0
Definition: ResourceBase.h:36
virtual void Bind()=0
Definition: Light3D.h:117
Definition: Material.h:190
Definition: GpuProgramManager.h:39
Definition: Texture.h:88
Definition: list.h:44
iMaterial * * iterator
Definition: array.h:54
Definition: Material.h:181
virtual bool IsTransperant()
Definition: Material.h:316
Definition: ResourceImage.h:43
bool reload()
Definition: Material.h:212
bool reload()
Definition: Material.h:167
Definition: Light.h:38
Definition: Renderer2D.h:79
Definition: Renderer3D.h:139
Definition: TextureManager.h:47
Definition: Material.h:135
Definition: ImageManager.h:48
Definition: tinyxml.h:864
Definition: list_intern.h:51
void destroy()
Definition: Material.h:214
virtual bool UsesLights()
Definition: Material.h:322
Definition: Camera.h:43
Definition: Material.h:203
void push_back(const t_T &element)
Definition: list.h:140
void SetId(int alId)
Definition: Material.h:334
void unload()
Definition: Material.h:213
Definition: LowLevelGraphics.h:200
Definition: GPUProgram.h:62
void unload()
Definition: Material.h:168