Fix issue in Rocket.lua script.
[Cafu-Engine.git] / Libs / MaterialSystem / Material.hpp
blobcbb316bb57cb02e07d3f65feb7f3cffe34b88e2e
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 */
7 #ifndef CAFU_MATSYS_MATERIAL_HPP_INCLUDED
8 #define CAFU_MATSYS_MATERIAL_HPP_INCLUDED
10 #include "Expression.hpp"
11 #include "MapComposition.hpp"
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
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
24 public:
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 };
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
34 SP_ClipPlayers = 0x0001,
35 ...
36 SP_BlockBspPortals = ...,
37 ...
38 SP_Ladder = ...
41 /// The properties of the volume that is defined by the brush on whose surface this material is used.
42 enum VolumePropertiesT
44 VP_Trigger = 0x0001
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
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
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
79 enum SurfaceTypeT { ST_None, ST_Stone, ST_Metal, ST_Sand, ST_Wood, ST_Liquid, ST_Glass, ST_Plastic };
82 std::string Name;
84 std::string AmbientShaderName;
85 std::string LightShaderName;
87 MapCompositionT DiffMapComp;
88 MapCompositionT NormMapComp;
89 MapCompositionT SpecMapComp;
90 MapCompositionT LumaMapComp;
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.
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.
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!
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.
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.
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.
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.
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.
138 /// The default constructor.
139 MaterialT();
141 /// A constructor. Throws TextParserT::ParseError on failure.
142 MaterialT(const std::string& MaterialName, const std::string& BaseDir, TextParserT& TP, const ArrayT<TableT*>& ListOfTables);
144 // void SetBaseDir(const std::string& BD);
145 unsigned int GetPixelSizeX() const;
146 unsigned int GetPixelSizeY() const;
148 bool UsesGeneratedLightMap() const
150 return LightMapComp.GetString()=="$lightmap";
153 bool UsesGeneratedSHLMap() const
155 return SHLMapComp.GetString()=="$shlmap";
158 bool HasDefaultBlendFunc() const
160 if (BlendFactorSrc==MaterialT::None) return true;
161 if (BlendFactorDst==MaterialT::None) return true;
162 if (BlendFactorSrc==MaterialT::One && BlendFactorDst==MaterialT::Zero) return true;
164 return false;
167 /// Saves the material into the given stream.
168 void Save(std::ostream& OutStream) const;
171 private:
173 // std::string BaseDir;
174 mutable unsigned int PixelSizeX;
175 mutable unsigned int PixelSizeY;
178 #endif