| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "Texture.h" |
| 4 |
#include "ishaderexpression.h" |
| 5 |
|
| 6 |
#include <memory> |
| 7 |
#include <vector> |
| 8 |
#include <string> |
| 9 |
|
| 10 |
#include "math/Vector2.h" |
| 11 |
#include "math/Vector4.h" |
| 12 |
#include "math/Matrix4.h" |
| 13 |
#include "render/Colour4.h" |
| 14 |
|
| 15 |
class IRenderEntity; |
| 16 |
|
| 17 |
// Texture repeat types |
| 18 |
enum ClampType |
| 19 |
{ |
| 20 |
CLAMP_REPEAT = 1 << 0, // default = no clamping |
| 21 |
CLAMP_NOREPEAT = 1 << 1, // "clamp" |
| 22 |
CLAMP_ZEROCLAMP = 1 << 2, // "zeroclamp" |
| 23 |
CLAMP_ALPHAZEROCLAMP = 1 << 3, // "alphazeroclamp" |
| 24 |
}; |
| 25 |
|
| 26 |
/** |
| 27 |
* \brief Representation of a GL blend function. |
| 28 |
* |
| 29 |
* A GL blend function consists of two GLenums representing the operations that |
| 30 |
* should be performed on the source and destination pixel colours respectively, |
| 31 |
* before the two results are added together into a final pixel colour. |
| 32 |
*/ |
| 33 |
class BlendFunc |
| 34 |
{ |
| 35 |
public: |
| 36 |
|
| 37 |
// Source pixel function |
| 38 |
GLenum src; |
| 39 |
|
| 40 |
// Destination pixel function |
| 41 |
GLenum dest; |
| 42 |
|
| 43 |
// Constructor |
| 44 |
BlendFunc(GLenum s, GLenum d) |
| 45 |
: src(s), dest(d) |
| 46 |
{ } |
| 47 |
}; |
| 48 |
|
| 49 |
/** |
| 50 |
* \brief A single layer of a material shader. |
| 51 |
* |
| 52 |
* Each shader layer contains an image texture, a blend mode (e.g. add, |
| 53 |
* modulate) and various other data. |
| 54 |
*/ |
| 55 |
class IShaderLayer |
| 56 |
{ |
| 57 |
public: |
| 58 |
using Ptr = std::shared_ptr<IShaderLayer>; |
| 59 |
|
| 60 |
/// Enumeration of layer types |
| 61 |
/// The enum value is also used to sort interaction stages: bump before diffuse before specular |
| 62 |
enum Type |
| 63 |
{ |
| 64 |
BUMP, |
| 65 |
DIFFUSE, |
| 66 |
SPECULAR, |
| 67 |
BLEND |
| 68 |
}; |
| 69 |
|
| 70 |
// Stage flags |
| 71 |
enum Flags |
| 72 |
{ |
| 73 |
FLAG_IGNORE_ALPHATEST = 1 << 0, |
| 74 |
FLAG_FILTER_NEAREST = 1 << 1, |
| 75 |
FLAG_FILTER_LINEAR = 1 << 2, |
| 76 |
FLAG_HIGHQUALITY = 1 << 3, // "uncompressed" |
| 77 |
FLAG_FORCE_HIGHQUALITY = 1 << 4, |
| 78 |
FLAG_NO_PICMIP = 1 << 5, |
| 79 |
FLAG_MASK_RED = 1 << 6, |
| 80 |
FLAG_MASK_GREEN = 1 << 7, |
| 81 |
FLAG_MASK_BLUE = 1 << 8, |
| 82 |
FLAG_MASK_ALPHA = 1 << 9, |
| 83 |
FLAG_MASK_DEPTH = 1 << 10, |
| 84 |
FLAG_IGNORE_DEPTH = 1 << 11, // use depthfunc always |
| 85 |
}; |
| 86 |
|
| 87 |
enum TexGenType |
| 88 |
{ |
| 89 |
TEXGEN_NORMAL = 1 << 0, |
| 90 |
TEXGEN_REFLECT = 1 << 1, |
| 91 |
TEXGEN_SKYBOX = 1 << 2, |
| 92 |
TEXGEN_WOBBLESKY = 1 << 3, |
| 93 |
TEXGEN_SCREEN = 1 << 4, // screen aligned, for mirrorRenders and screen space temporaries |
| 94 |
}; |
| 95 |
|
| 96 |
enum ParseFlags |
| 97 |
{ |
| 98 |
PF_HasTexGenKeyword = 1 << 1, // texgen has been specified |
| 99 |
PF_HasNoclampKeyword = 1 << 2, // noclamp has been specified |
| 100 |
PF_HasColoredKeyword = 1 << 3, // colored has been specified |
| 101 |
}; |
| 102 |
|
| 103 |
// Expression slot selector |
| 104 |
struct Expression |
| 105 |
{ |
| 106 |
enum Slot |
| 107 |
{ |
| 108 |
AlphaTest = 0, |
| 109 |
Condition, |
| 110 |
TexGenParam1, |
| 111 |
TexGenParam2, |
| 112 |
TexGenParam3, |
| 113 |
ColourRed, |
| 114 |
ColourGreen, |
| 115 |
ColourBlue, |
| 116 |
ColourAlpha, |
| 117 |
TextureMatrixRow0Col0, |
| 118 |
TextureMatrixRow0Col1, |
| 119 |
TextureMatrixRow0Col2, |
| 120 |
TextureMatrixRow1Col0, |
| 121 |
TextureMatrixRow1Col1, |
| 122 |
TextureMatrixRow1Col2, |
| 123 |
NumExpressionSlots |
| 124 |
}; |
| 125 |
}; |
| 126 |
|
| 127 |
// The various texture transformations |
| 128 |
enum class TransformType |
| 129 |
{ |
| 130 |
Translate, |
| 131 |
Scale, |
| 132 |
CenterScale, |
| 133 |
Shear, |
| 134 |
Rotate, |
| 135 |
}; |
| 136 |
|
| 137 |
// A texture transformation consists of a type and two arguments at most |
| 138 |
struct Transformation |
| 139 |
{ |
| 140 |
TransformType type; |
| 141 |
shaders::IShaderExpression::Ptr expression1; |
| 142 |
shaders::IShaderExpression::Ptr expression2; |
| 143 |
}; |
| 144 |
|
| 145 |
/** |
| 146 |
* \brief |
| 147 |
* Destructor |
| 148 |
*/ |
| 149 |
virtual ~IShaderLayer() {} |
| 150 |
|
| 151 |
/// Return the layer type. |
| 152 |
virtual Type getType() const = 0; |
| 153 |
|
| 154 |
/** |
| 155 |
* \brief |
| 156 |
* Return the Texture object corresponding to this layer (may be NULL). |
| 157 |
*/ |
| 158 |
virtual TexturePtr getTexture() const = 0; |
| 159 |
|
| 160 |
/** |
| 161 |
* Evaluate all shader expressions used in this stage. Call this once (each frame) |
| 162 |
* before requesting things like getAlphaTest(), getColour() or isVisible() |
| 163 |
*/ |
| 164 |
virtual void evaluateExpressions(std::size_t time) = 0; |
| 165 |
|
| 166 |
/** |
| 167 |
* Like evaluateExpressions(time), but with an additional renderentity as argument |
| 168 |
* to give this stage the ability to resolve parm0..parm11 values. |
| 169 |
*/ |
| 170 |
virtual void evaluateExpressions(std::size_t time, const IRenderEntity& entity) = 0; |
| 171 |
|
| 172 |
// Returns the requested expression |
| 173 |
virtual shaders::IShaderExpression::Ptr getExpression(Expression::Slot slot) = 0; |
| 174 |
|
| 175 |
/** |
| 176 |
* The flags set on this stage. |
| 177 |
*/ |
| 178 |
virtual int getStageFlags() const = 0; |
| 179 |
|
| 180 |
/** |
| 181 |
* Each stage can have its own clamp type, overriding the per-material one. |
| 182 |
*/ |
| 183 |
virtual ClampType getClampType() const = 0; |
| 184 |
|
| 185 |
/** |
| 186 |
* Returns the texgen type: normal, reflect, skybox, etc. |
| 187 |
* Use getTexGenParam(i) to retrieve the wobblesky parameters [0..2] |
| 188 |
*/ |
| 189 |
virtual TexGenType getTexGenType() const = 0; |
| 190 |
|
| 191 |
/** |
| 192 |
* TexGen type wobblesky has 3 parameters, get them here, with index in [0..2] |
| 193 |
*/ |
| 194 |
virtual float getTexGenParam(std::size_t index) const = 0; |
| 195 |
|
| 196 |
// The expressions used to calculate the tex gen params. Index in [0..2] |
| 197 |
virtual shaders::IShaderExpression::Ptr getTexGenExpression(std::size_t index) const = 0; |
| 198 |
|
| 199 |
/** |
| 200 |
* \brief |
| 201 |
* Return the GL blend function for this layer. |
| 202 |
* |
| 203 |
* Only layers of type BLEND use a BlendFunc. Layers of type DIFFUSE, BUMP |
| 204 |
* and SPECULAR do not use blend functions. |
| 205 |
*/ |
| 206 |
virtual BlendFunc getBlendFunc() const = 0; |
| 207 |
|
| 208 |
// Get the blend string as defined in the material def, e.g. "add" or "gl_one, gl_zero" |
| 209 |
virtual const std::pair<std::string, std::string>& getBlendFuncStrings() const = 0; |
| 210 |
|
| 211 |
/** |
| 212 |
* \brief |
| 213 |
* Multiplicative layer colour (set with "red 0.6", "green 0.2" etc) |
| 214 |
*/ |
| 215 |
virtual Colour4 getColour() const = 0; |
| 216 |
|
| 217 |
// An enum used to select which colour components are affected by an operation |
| 218 |
enum ColourComponentSelector |
| 219 |
{ |
| 220 |
COMP_RED, // red only |
| 221 |
COMP_GREEN, // green only |
| 222 |
COMP_BLUE, // blue only |
| 223 |
COMP_ALPHA, // alpha only |
| 224 |
COMP_RGB, // red, green and blue |
| 225 |
COMP_RGBA, // all: red, greeb, blue, alpha |
| 226 |
}; |
| 227 |
|
| 228 |
// Returns the expression to calculate the RGBA vertex colour values |
| 229 |
virtual const shaders::IShaderExpression::Ptr& getColourExpression(ColourComponentSelector component) const = 0; |
| 230 |
|
| 231 |
/** |
| 232 |
* \brief |
| 233 |
* Vertex colour blend mode enumeration. |
| 234 |
*/ |
| 235 |
enum VertexColourMode |
| 236 |
{ |
| 237 |
VERTEX_COLOUR_NONE, // no vertex colours |
| 238 |
VERTEX_COLOUR_MULTIPLY, // "vertexColor" |
| 239 |
VERTEX_COLOUR_INVERSE_MULTIPLY // "inverseVertexColor" |
| 240 |
}; |
| 241 |
|
| 242 |
/** |
| 243 |
* \brief |
| 244 |
* Get the vertex colour mode for this layer. |
| 245 |
*/ |
| 246 |
virtual VertexColourMode getVertexColourMode() const = 0; |
| 247 |
|
| 248 |
enum class MapType |
| 249 |
{ |
| 250 |
Map, // regular map |
| 251 |
CubeMap, // corresponds to CUBE_MAP_OBJECT |
| 252 |
CameraCubeMap, // corresponds to CUBE_MAP_CAMERA |
| 253 |
VideoMap, |
| 254 |
SoundMap, |
| 255 |
MirrorRenderMap, |
| 256 |
RemoteRenderMap, |
| 257 |
}; |
| 258 |
|
| 259 |
// Get the map type used by this stage |
| 260 |
virtual MapType getMapType() const = 0; |
| 261 |
|
| 262 |
/** |
| 263 |
* \brief |
| 264 |
* Enumeration of cube map modes for this layer. |
| 265 |
*/ |
| 266 |
enum CubeMapMode |
| 267 |
{ |
| 268 |
CUBE_MAP_NONE, |
| 269 |
CUBE_MAP_CAMERA, // cube map in camera space ("cameraCubeMap") |
| 270 |
CUBE_MAP_OBJECT // cube map in object space ("cubeMap") |
| 271 |
}; |
| 272 |
|
| 273 |
/** |
| 274 |
* \brief |
| 275 |
* Get the cube map mode for this layer. |
| 276 |
*/ |
| 277 |
virtual CubeMapMode getCubeMapMode() const = 0; |
| 278 |
|
| 279 |
/** |
| 280 |
* Returns the dimensions specifying the map size for |
| 281 |
* stages using the "mirrorRenderMap", "remoteRenderMap" keywords. |
| 282 |
*/ |
| 283 |
virtual const Vector2& getRenderMapSize() const = 0; |
| 284 |
|
| 285 |
// Adds a transformation at the end of the list of existing transforms in this stage |
| 286 |
virtual void appendTransformation(const Transformation& transform) = 0; |
| 287 |
|
| 288 |
// The list of transformations defined in this stage, in the order they appear in the declaration |
| 289 |
virtual const std::vector<Transformation>& getTransformations() = 0; |
| 290 |
|
| 291 |
// Returns the current texture matrix |
| 292 |
virtual Matrix4 getTextureTransform() = 0; |
| 293 |
|
| 294 |
// Returns true if this layer has an alphatest expression defined |
| 295 |
virtual bool hasAlphaTest() const = 0; |
| 296 |
|
| 297 |
/** |
| 298 |
* \brief |
| 299 |
* Get the alpha test value for this layer. |
| 300 |
* |
| 301 |
* \return |
| 302 |
* The alpha test value, within (0..1] if it is set. If no alpha test |
| 303 |
* value is set, 0 will be returned. |
| 304 |
*/ |
| 305 |
virtual float getAlphaTest() const = 0; |
| 306 |
|
| 307 |
// Returns the expression used to calculate the alpha test value |
| 308 |
virtual const shaders::IShaderExpression::Ptr& getAlphaTestExpression() const = 0; |
| 309 |
|
| 310 |
/** |
| 311 |
* Whether this stage is active. Unconditional stages always return true, |
| 312 |
* conditional ones return the result of the most recent condition expression evaluation. |
| 313 |
*/ |
| 314 |
virtual bool isVisible() const = 0; |
| 315 |
|
| 316 |
// Stages can be enabled/disabled programmatically (used when editing materials) |
| 317 |
// This returns true if the stage is enabled |
| 318 |
virtual bool isEnabled() const = 0; |
| 319 |
|
| 320 |
// Returns the if-expression used to evaluate this stage's visibility, or null if none defined |
| 321 |
virtual const shaders::IShaderExpression::Ptr& getConditionExpression() const = 0; |
| 322 |
|
| 323 |
/** |
| 324 |
* Returns the name of this stage's vertex program. |
| 325 |
*/ |
| 326 |
virtual const std::string& getVertexProgram()const = 0; |
| 327 |
|
| 328 |
/** |
| 329 |
* Returns the name of this stage's fragment program. |
| 330 |
*/ |
| 331 |
virtual const std::string& getFragmentProgram() const = 0; |
| 332 |
|
| 333 |
// The number of defined vertex parameters |
| 334 |
virtual int getNumVertexParms() const = 0; |
| 335 |
|
| 336 |
/** |
| 337 |
* Returns the 4 parameter values for the vertexParm index <parm>. |
| 338 |
*/ |
| 339 |
virtual Vector4 getVertexParmValue(int parm) const = 0; |
| 340 |
|
| 341 |
// A vertex parm has an index and 4 expressions at most |
| 342 |
struct VertexParm |
| 343 |
{ |
| 344 |
VertexParm() : |
| 345 |
index(-1) // a negative index indicates this parm has not been defined in the stage |
| 346 |
{} |
| 347 |
|
| 348 |
int index; |
| 349 |
shaders::IShaderExpression::Ptr expressions[4]; |
| 350 |
}; |
| 351 |
|
| 352 |
// Returns the vertex parameter with the given index [0..3] |
| 353 |
virtual const VertexParm& getVertexParm(int index) const = 0; |
| 354 |
|
| 355 |
/** |
| 356 |
* Returns the number of fragment maps in this stage. |
| 357 |
*/ |
| 358 |
virtual std::size_t getNumFragmentMaps() const = 0; |
| 359 |
|
| 360 |
struct FragmentMap |
| 361 |
{ |
| 362 |
FragmentMap() : |
| 363 |
index(-1) |
| 364 |
{} |
| 365 |
|
| 366 |
int index; |
| 367 |
std::vector<std::string> options; |
| 368 |
shaders::IMapExpression::Ptr map; |
| 369 |
}; |
| 370 |
|
| 371 |
virtual const FragmentMap& getFragmentMap(int index) const = 0; |
| 372 |
|
| 373 |
/** |
| 374 |
* Returns the fragment map image with the given index. |
| 375 |
*/ |
| 376 |
virtual TexturePtr getFragmentMapTexture(int index) const = 0; |
| 377 |
|
| 378 |
/** |
| 379 |
* Stage-specific polygon offset, overriding the "global" one defined on the material. |
| 380 |
*/ |
| 381 |
virtual float getPrivatePolygonOffset() const = 0; |
| 382 |
|
| 383 |
// If this stage is referring to a single image file, this will return |
| 384 |
// the VFS path to it with the file extension removed. |
| 385 |
// If this layer doesn't refer to a single image file, an empty string is returned |
| 386 |
virtual std::string getMapImageFilename() const = 0; |
| 387 |
|
| 388 |
// The map expression used to generate/define the texture of this stage |
| 389 |
virtual shaders::IMapExpression::Ptr getMapExpression() const = 0; |
| 390 |
|
| 391 |
// Parser information, to reconstruct the use of certain keywords |
| 392 |
virtual int getParseFlags() const = 0; |
| 393 |
}; |
| 394 |
|
| 395 |
/** |
| 396 |
* \brief |
| 397 |
* Vector of IShaderLayer pointers. |
| 398 |
*/ |
| 399 |
typedef std::vector<IShaderLayer::Ptr> IShaderLayerVector; |
| 400 |
|
| 401 |
// Interface extension to IShaderLayer, offering editing functions |
| 402 |
class IEditableShaderLayer : |
| 403 |
public IShaderLayer |
| 404 |
{ |
| 405 |
public: |
| 406 |
using Ptr = std::shared_ptr<IEditableShaderLayer>; |
| 407 |
|
| 408 |
virtual ~IEditableShaderLayer() {} |
| 409 |
|
| 410 |
// Enable/disable this stage - a disabled stage will return false to isVisible() |
| 411 |
virtual void setEnabled(bool enabled) = 0; |
| 412 |
|
| 413 |
// Set the given stage flag(s) |
| 414 |
virtual void setStageFlag(IShaderLayer::Flags flag) = 0; |
| 415 |
|
| 416 |
// Clears the given stage flag(s) |
| 417 |
virtual void clearStageFlag(IShaderLayer::Flags flag) = 0; |
| 418 |
|
| 419 |
// Set the map type to use by this stage (map, cameraCubeMap, etc.) |
| 420 |
virtual void setMapType(MapType mapType) = 0; |
| 421 |
|
| 422 |
// Set the blend func from the given pair of strings |
| 423 |
virtual void setBlendFuncStrings(const std::pair<std::string, std::string>& pair) = 0; |
| 424 |
|
| 425 |
// Set the alpha test expression from the given string |
| 426 |
virtual void setAlphaTestExpressionFromString(const std::string& expression) = 0; |
| 427 |
|
| 428 |
// Update the "map" expression of this stage |
| 429 |
virtual void setMapExpressionFromString(const std::string& expression) = 0; |
| 430 |
|
| 431 |
// Adds a empty transformation to this layer, returning its new index |
| 432 |
virtual std::size_t addTransformation(TransformType type, const std::string& expression1, const std::string& expression2) = 0; |
| 433 |
|
| 434 |
// Removes the indexed transformation |
| 435 |
virtual void removeTransformation(std::size_t index) = 0; |
| 436 |
|
| 437 |
// Set the type and expressions of the indexed transformation. The transformation index must not be |
| 438 |
// out of bounds, use appendTransformation() to add new ones |
| 439 |
virtual void updateTransformation(std::size_t index, TransformType type, |
| 440 |
const std::string& expression1, const std::string& expression2) = 0; |
| 441 |
|
| 442 |
// Set the colour expression from the given string |
| 443 |
virtual void setColourExpressionFromString(ColourComponentSelector component, const std::string& expression) = 0; |
| 444 |
|
| 445 |
// Set the stage condition expression |
| 446 |
virtual void setConditionExpressionFromString(const std::string& expression) = 0; |
| 447 |
|
| 448 |
// Sets the texgen type of this stage |
| 449 |
virtual void setTexGenType(TexGenType type) = 0; |
| 450 |
|
| 451 |
// Set the texgen expression (used for wobblesky) |
| 452 |
virtual void setTexGenExpressionFromString(std::size_t index, const std::string& expression) = 0; |
| 453 |
|
| 454 |
// Set the vertex colour mode of this stage |
| 455 |
virtual void setVertexColourMode(VertexColourMode mode) = 0; |
| 456 |
|
| 457 |
// Set the clamp type of this stage |
| 458 |
virtual void setClampType(ClampType clampType) = 0; |
| 459 |
|
| 460 |
// Sets the privatePolygonOffset value of this stage |
| 461 |
virtual void setPrivatePolygonOffset(double offset) = 0; |
| 462 |
|
| 463 |
// Sets width and height of the mirrorRenderMap/remoteRenderMap images |
| 464 |
virtual void setRenderMapSize(const Vector2& size) = 0; |
| 465 |
|
| 466 |
// For stages with map type SoundMap, this is used to set the waveform flag |
| 467 |
virtual void setSoundMapWaveForm(bool waveForm) = 0; |
| 468 |
|
| 469 |
// For stages with map type VideoMap, this is used to update the properties |
| 470 |
virtual void setVideoMapProperties(const std::string& filePath, bool looping) = 0; |
| 471 |
}; |