Cafu Engine
Material.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 #ifndef CAFU_MATSYS_MATERIAL_HPP_INCLUDED
8 #define CAFU_MATSYS_MATERIAL_HPP_INCLUDED
9 
10 #include "Expression.hpp"
11 #include "MapComposition.hpp"
12 
13 
14 // *Somewhere* (in some Linux clib header?!) apparently #define None 0L occurs,
15 // which causes problems below. Grrr. TODO: Use g++ -E to find out where this comes from.
16 #undef None
17 
18 
19 /// This class represents a surface material ("A datastructural representation of a scripts material def.").
20 /// It's definition is usually obtained with the help of the MaterialManager from a material script file (.cmat).
21 /// Note that materials are entirely independent from the renderer implementation!
22 class MaterialT
23 {
24  public:
25 
26  enum BlendFactorT { None, Zero, One, DstColor, SrcColor, OneMinusDstColor, OneMinusSrcColor, DstAlpha, SrcAlpha, OneMinusDstAlpha, OneMinusSrcAlpha };
27  enum TexCoordGenT { Disabled, ObjectSpacePlane, EyeSpacePlane, SphereMap };
28  enum PolygonModeT { Filled, Wireframe, Points };
29 
30 #if 0 // I think it should be like this:
31  /// The properties of the surfaces that this material is assigned to.
32  enum SurfacePropertiesT
33  {
34  SP_ClipPlayers = 0x0001,
35  ...
36  SP_BlockBspPortals = ...,
37  ...
38  SP_Ladder = ...
39  }
40 
41  /// The properties of the volume that is defined by the brush on whose surface this material is used.
42  enum VolumePropertiesT
43  {
44  VP_Trigger = 0x0001
45  }
46 #else
47  // Note that Materials are applied to surfaces, and thus they inherently can have surface flags/properties/attributes/types,
48  // but not easily (naturally) describe volume/contents properties...
49  // Volume properties should only be contents we can teleport into or out of, e.g. water, trigger volumes, etc.
50  enum ClipFlagsT
51  {
52  Clip_Players =0x0001,
53  Clip_Monsters =0x0002,
54  Clip_Moveables =0x0004,
55  Clip_IK =0x0008,
56  Clip_Projectiles=0x0010,
57  Clip_Sight =0x0020,
58  Clip_BspPortals =0x0040,
59  Clip_Radiance =0x0080,
60  Clip_AllBlocking=0x00FF,
61  Clip_BlkButUtils=Clip_AllBlocking & ~Clip_BspPortals & ~Clip_Radiance, // 0x003F
62  Clip_Trigger =0x0100,
63  SP_Ladder =0x0200
64  };
65 
66  // TODO: Rename ClipFlagsT to ContentsT (as in Q3, D3 etc.)? Or is contents==volume, and thus something different from ClipFlags?
67  // TODO: Extend this to not only have flags relevant to traces, but also to position (volume) tests (e.g. water, trigger, ladder, ...)??
68  // (Consider if we should have a separate VolumeFlagsT enum (orthogonal to ClipFlagsT), or if this enum should be a part of the ClipFlagsT enum.)
69  // (Idea: One reason for having the VolumeFlagsT seperate/orthogonal is that brushes can have "mixed" clipflags sides, but not mixed volumeflags sides.)
70  // TODO: BspPortals and Radiance are actually "meta-flags" for CaBSP and CaLight, respectively, not really for the ClipSys / game-code:
71  // BspPortals is for CaBSP only. CaLight may use the ClipSys or the draw BSP tree for its purposes, so Radiance is half a meta, and half a clip flag...
72  // NOTE: A reaonsable consideration seems to think of maps as being made of triangle lists by 3D Modelling programs, not as brushes by CaWE.
73  // Then, it makes sense to have a VolumeFlagsT member that keeps track of the *volume* properties of the material, e.g. the "contents"
74  // of the volume like WATER, TRIGGER, PAIN, etc. Separately stored are the *surface* properties, which may be a set of flags plus the
75  // surface type, etc. The clip flags would then be a subset of the surface properties/flags. Only remaining issue: This is currently
76  // a bit inconsistent with the assumptions of the ClipSys... REVISE!
77 #endif
78 
79  enum SurfaceTypeT { ST_None, ST_Stone, ST_Metal, ST_Sand, ST_Wood, ST_Liquid, ST_Glass, ST_Plastic };
80 
81 
82  std::string Name;
83 
84  std::string AmbientShaderName;
85  std::string LightShaderName;
86 
87  MapCompositionT DiffMapComp;
88  MapCompositionT NormMapComp;
89  MapCompositionT SpecMapComp;
90  MapCompositionT LumaMapComp;
91 
92  MapCompositionT LightMapComp; ///< This is normally empty or "$lightmap". Everything else works, too, but doesn't make much sense.
93  MapCompositionT SHLMapComp; ///< This is normally empty or "$shlmap". Everything else works, too, but doesn't make much sense.
94 
95  MapCompositionT CubeMap1Comp; ///< This materials 1st cubemap. Requires Ambient- and/or LightShaderName to be explicitly set -- the auto-detection doesn't take cubemaps into account. Use '#' as a placeholder for the actual side suffixes.
96  MapCompositionT CubeMap2Comp; ///< This materials 2nd cubemap. Requires Ambient- and/or LightShaderName to be explicitly set -- the auto-detection doesn't take cubemaps into account. Use '#' as a placeholder for the actual side suffixes.
97 
98  ArrayT<ExpressionT> ShaderParamExpr;///< Parameters for the shader that renders this material. The meanings depend on the shader!
99  ArrayT<MapCompositionT> ShaderParamMapC;///< Parameters for the shader that renders this material. The meanings depend on the shader!
100 
101  // Global material rendering parameters (never looked at by the map compile tools).
102  bool NoDraw; ///< If true, this material does not render at all. Mostly useful for debugging.
103  bool TwoSided; ///< Normally, back-face culling is enabled per default. If TwoSided is true however, culling gets disabled.
104  float DepthOffset; ///< Depth buffer offset to combat z-fighting. Useful e.g. for decals or CaWE materials.
105  PolygonModeT PolygonMode; ///< The mode in which the polygon is rendered: filled, wireframe, or as points. Applies to both the front- and back-side of the polygon.
106 
107  // Ambient material rendering parameters (only relevant for the *ambient* contribution, and never looked at by the map compile tools <-- WRONG! E.g. CaBSP looks for blended or perforated materials when calculating portals...).
108  ExpressionT AlphaTestValue; ///< The value for the alpha test (alpha > AmbientTestValue?). Negative for no test.
109  BlendFactorT BlendFactorSrc; ///< The source factor of the blend function for the ambient contribution.
110  BlendFactorT BlendFactorDst; ///< The destination factor of the blend function for the ambient contribution.
111  ExpressionT RedGen;
112  ExpressionT GreenGen;
113  ExpressionT BlueGen;
114  ExpressionT AlphaGen;
115  bool AmbientMask[5]; ///< Buffer mask for the ambient contribution. Elements 0 to 4 correspond to red, green, blue, alpha and depth.
116  bool UseMeshColors; ///< Modulates the RGBA color with the colors specified at the mesh vertices. Normally, the mesh vertex colors are ignored.
117 
118  // Light material rendering parameters (only relevant for the *lighting* contribution, and never looked at by the map compile tools).
119  bool NoDynLight; ///< Entirely turns off per-lightsource interaction, that is, the complete light shader. If true, this material does not receive (or rather, reflect) light by dynamic light sources, only the ambient contribution is rendered. Useful e.g. for sky domes, additive effects like particles, translucent surfaces like glass etc. It may still cast shadows, though.
120  bool NoShadows; ///< Meshes with this material applied won't cast any (stencil-buffer) shadows if this is true. This should in a sense actually be a "meta" parameter, as it is taken into account only by the code that computes the shadow volumes, *not* by the MatSys! (It can't - materials are usually unknown to it while rendering stencil shadow volumes.)
121  bool LightMask[5]; ///< Buffer mask for the lighting contribution. Elements 0 to 4 correspond to red, green, blue, alpha and depth.
122 
123  // Material (meta-)parameters for the compile tools, the game code etc. Not directly related to the rendering of the material.
124  ClipFlagsT ClipFlags; ///< The collision detection (trace) code may want to consider only materials with certain clip flags set.
125  SurfaceTypeT SurfaceType; ///< The game code usually wants to play footstep sounds and ricochet effects according to the surface type.
126 
127  // Material meta-parameters for the compile tools etc. Not directly related to the rendering of the material.
128  MapCompositionT meta_EditorImage; ///< Image shown in CaWE.
129  bool meta_EditorSave; ///< If \c true, this is a material that the user has created and/or manipulated in the editor (CaWE) and that the editor thus should save, possibly overwriting a previous definition. This flag is used for keeping such materials separate from custom, hand-crafted material definitions that the editor should not touch or overwrite. The editor saves such materials typically in a separate file whose name ends like <tt>_editor.cmat</tt> that in turn is included from another <tt>.cmat</tt> file.
130  float meta_RadiantExitance_Values[3]; ///< Radiant Exitance RGB values in [W/m^2]. Used by CaLight.
131  MapCompositionT meta_RadiantExitance_ByImage_FileName; ///< Radiant Exitance RGB values from image file. Used by CaLight.
132  float meta_RadiantExitance_ByImage_Scale; ///< Radiant Exitance intensity (scale) for the RGB values from image file. Used by CaLight.
133  float meta_SunLight_Irr[3]; ///< Irradiance of the sunlight in Watt/m^2 that comes (or shines) through this material.
134  float meta_SunLight_Dir[3]; ///< The direction of the incoming sunlight rays. The z-component should be negative.
135  bool meta_AlphaModulatesRadiosityLight; ///< Makes CaLight handle the DiffMapComps alpha channel and the RGBAGens properly. For fences, grates, glass, water, etc.
136 
137 
138  /// The default constructor.
139  MaterialT();
140 
141  /// A constructor. Throws TextParserT::ParseError on failure.
142  MaterialT(const std::string& MaterialName, const std::string& BaseDir, TextParserT& TP, const ArrayT<TableT*>& ListOfTables);
143 
144  // void SetBaseDir(const std::string& BD);
145  unsigned int GetPixelSizeX() const;
146  unsigned int GetPixelSizeY() const;
147 
148  bool UsesGeneratedLightMap() const
149  {
150  return LightMapComp.GetString()=="$lightmap";
151  }
152 
153  bool UsesGeneratedSHLMap() const
154  {
155  return SHLMapComp.GetString()=="$shlmap";
156  }
157 
158  bool HasDefaultBlendFunc() const
159  {
160  if (BlendFactorSrc==MaterialT::None) return true;
161  if (BlendFactorDst==MaterialT::None) return true;
162  if (BlendFactorSrc==MaterialT::One && BlendFactorDst==MaterialT::Zero) return true;
163 
164  return false;
165  }
166 
167  /// Saves the material into the given stream.
168  void Save(std::ostream& OutStream) const;
169 
170 
171  private:
172 
173  // std::string BaseDir;
174  mutable unsigned int PixelSizeX;
175  mutable unsigned int PixelSizeY;
176 };
177 
178 #endif
MapCompositionT meta_EditorImage
Image shown in CaWE.
Definition: Material.hpp:128
ClipFlagsT ClipFlags
The collision detection (trace) code may want to consider only materials with certain clip flags set...
Definition: Material.hpp:124
bool UseMeshColors
Modulates the RGBA color with the colors specified at the mesh vertices. Normally, the mesh vertex colors are ignored.
Definition: Material.hpp:116
bool meta_EditorSave
If true, this is a material that the user has created and/or manipulated in the editor (CaWE) and tha...
Definition: Material.hpp:129
bool meta_AlphaModulatesRadiosityLight
Makes CaLight handle the DiffMapComps alpha channel and the RGBAGens properly. For fences...
Definition: Material.hpp:135
bool TwoSided
Normally, back-face culling is enabled per default. If TwoSided is true however, culling gets disable...
Definition: Material.hpp:103
ArrayT< ExpressionT > ShaderParamExpr
Parameters for the shader that renders this material. The meanings depend on the shader! ...
Definition: Material.hpp:98
MapCompositionT CubeMap2Comp
This materials 2nd cubemap. Requires Ambient- and/or LightShaderName to be explicitly set – the auto-...
Definition: Material.hpp:96
MapCompositionT LightMapComp
This is normally empty or "$lightmap". Everything else works, too, but doesn't make much sense...
Definition: Material.hpp:92
bool NoDynLight
Entirely turns off per-lightsource interaction, that is, the complete light shader. If true, this material does not receive (or rather, reflect) light by dynamic light sources, only the ambient contribution is rendered. Useful e.g. for sky domes, additive effects like particles, translucent surfaces like glass etc. It may still cast shadows, though.
Definition: Material.hpp:119
float meta_SunLight_Dir[3]
The direction of the incoming sunlight rays. The z-component should be negative.
Definition: Material.hpp:134
MapCompositionT meta_RadiantExitance_ByImage_FileName
Radiant Exitance RGB values from image file. Used by CaLight.
Definition: Material.hpp:131
MapCompositionT CubeMap1Comp
This materials 1st cubemap. Requires Ambient- and/or LightShaderName to be explicitly set – the auto-...
Definition: Material.hpp:95
float meta_RadiantExitance_ByImage_Scale
Radiant Exitance intensity (scale) for the RGB values from image file. Used by CaLight.
Definition: Material.hpp:132
This class represents a surface material ("A datastructural representation of a scripts material def...
Definition: Material.hpp:22
float DepthOffset
Depth buffer offset to combat z-fighting. Useful e.g. for decals or CaWE materials.
Definition: Material.hpp:104
A typed expression class.
Definition: Expression.hpp:43
float meta_RadiantExitance_Values[3]
Radiant Exitance RGB values in [W/m^2]. Used by CaLight.
Definition: Material.hpp:130
bool NoDraw
If true, this material does not render at all. Mostly useful for debugging.
Definition: Material.hpp:102
MapCompositionT SHLMapComp
This is normally empty or "$shlmap". Everything else works, too, but doesn't make much sense...
Definition: Material.hpp:93
std::string GetString() const
Returns a string description of this MapCompositionT (quasi the counter-piece to the constructor)...
Definition: MapComposition.cpp:659
bool AmbientMask[5]
Buffer mask for the ambient contribution. Elements 0 to 4 correspond to red, green, blue, alpha and depth.
Definition: Material.hpp:115
bool LightMask[5]
Buffer mask for the lighting contribution. Elements 0 to 4 correspond to red, green, blue, alpha and depth.
Definition: Material.hpp:121
BlendFactorT BlendFactorSrc
The source factor of the blend function for the ambient contribution.
Definition: Material.hpp:109
bool NoShadows
Meshes with this material applied won't cast any (stencil-buffer) shadows if this is true...
Definition: Material.hpp:120
A MapCompositionT is a description of how a SINGLE texture map image is composited from several sourc...
Definition: MapComposition.hpp:18
float meta_SunLight_Irr[3]
Irradiance of the sunlight in Watt/m^2 that comes (or shines) through this material.
Definition: Material.hpp:133
void Save(std::ostream &OutStream) const
Saves the material into the given stream.
Definition: Material.cpp:473
SurfaceTypeT SurfaceType
The game code usually wants to play footstep sounds and ricochet effects according to the surface typ...
Definition: Material.hpp:125
MaterialT()
The default constructor.
Definition: Material.cpp:44
BlendFactorT BlendFactorDst
The destination factor of the blend function for the ambient contribution.
Definition: Material.hpp:110
PolygonModeT PolygonMode
The mode in which the polygon is rendered: filled, wireframe, or as points. Applies to both the front...
Definition: Material.hpp:105
ArrayT< MapCompositionT > ShaderParamMapC
Parameters for the shader that renders this material. The meanings depend on the shader! ...
Definition: Material.hpp:99
ExpressionT AlphaTestValue
The value for the alpha test (alpha > AmbientTestValue?). Negative for no test.
Definition: Material.hpp:108
This is a class for parsing text.
Definition: TextParser.hpp:21