RenderState.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. #ifndef RENDERSTATE_H_
  2. #define RENDERSTATE_H_
  3. #include "Ref.h"
  4. namespace gameplay
  5. {
  6. class MaterialParameter;
  7. class Node;
  8. class NodeCloneContext;
  9. class Pass;
  10. /**
  11. * Defines the render state of the graphics device.
  12. */
  13. class RenderState : public Ref
  14. {
  15. friend class Game;
  16. friend class Material;
  17. friend class Technique;
  18. friend class Pass;
  19. friend class Model;
  20. public:
  21. /**
  22. * Built-in auto-bind targets for material parameters.
  23. */
  24. enum AutoBinding
  25. {
  26. NONE,
  27. /**
  28. * Binds a node's World matrix.
  29. */
  30. WORLD_MATRIX,
  31. /**
  32. * Binds the View matrix of the active camera for the node's scene.
  33. */
  34. VIEW_MATRIX,
  35. /**
  36. * Binds the Projection matrix of the active camera for the node's scene.
  37. */
  38. PROJECTION_MATRIX,
  39. /**
  40. * Binds a node's WorldView matrix.
  41. */
  42. WORLD_VIEW_MATRIX,
  43. /**
  44. * Binds the ViewProjection matrix of the active camera for the node's scene.
  45. */
  46. VIEW_PROJECTION_MATRIX,
  47. /**
  48. * Binds a node's WorldViewProjection matrix.
  49. */
  50. WORLD_VIEW_PROJECTION_MATRIX,
  51. /**
  52. * Binds a node's InverseTransposeWorl matrix.
  53. */
  54. INVERSE_TRANSPOSE_WORLD_MATRIX,
  55. /**
  56. * Binds a node's InverseTransposeWorldView matrix.
  57. */
  58. INVERSE_TRANSPOSE_WORLD_VIEW_MATRIX,
  59. /**
  60. * Binds the position (Vector3) of the active camera for the node's scene.
  61. */
  62. CAMERA_WORLD_POSITION,
  63. /**
  64. * Binds the view-space position (Vector3) of the active camera for the node's scene.
  65. */
  66. CAMERA_VIEW_POSITION,
  67. /**
  68. * Binds the matrix palette of MeshSkin attached to a node's model.
  69. */
  70. MATRIX_PALETTE
  71. };
  72. /**
  73. * Defines blend constants supported by the blend function.
  74. */
  75. enum Blend
  76. {
  77. BLEND_ZERO = GL_ZERO,
  78. BLEND_ONE = GL_ONE,
  79. BLEND_SRC_COLOR = GL_SRC_COLOR,
  80. BLEN_ONE_MINUS_SRC_COLOR = GL_ONE_MINUS_SRC_COLOR,
  81. BLEND_DST_COLOR = GL_DST_COLOR,
  82. BLEND_ONE_MINUS_DST_COLOR = GL_ONE_MINUS_DST_COLOR,
  83. BLEND_SRC_ALPHA = GL_SRC_ALPHA,
  84. BLEND_ONE_MINUS_SRC_ALPHA = GL_ONE_MINUS_SRC_ALPHA,
  85. BLEND_DST_ALPHA = GL_DST_ALPHA,
  86. BLEND_ONE_MINUS_DST_ALPHA = GL_ONE_MINUS_DST_ALPHA,
  87. BLEND_CONSTANT_ALPHA = GL_CONSTANT_ALPHA,
  88. BLEND_ONE_MINUS_CONSTANT_ALPHA = GL_ONE_MINUS_CONSTANT_ALPHA,
  89. BLEND_SRC_ALPHA_SATURATE = GL_SRC_ALPHA_SATURATE
  90. };
  91. /**
  92. * Defines a block of fixed-function render states that can be applied to a
  93. * RenderState object.
  94. */
  95. class StateBlock : public Ref
  96. {
  97. friend class RenderState;
  98. friend class Game;
  99. public:
  100. /**
  101. * Creates a new StateBlock with default render state settings.
  102. * @script{create}
  103. */
  104. static StateBlock* create();
  105. /**
  106. * Binds the state in this StateBlock to the renderer.
  107. *
  108. * This method handles both setting and restoring of render states to ensure that
  109. * only the state explicitly defined by this StateBlock is applied to the renderer.
  110. */
  111. void bind();
  112. /**
  113. * Toggles blending.
  114. *
  115. * @param enabled true to enable, false to disable.
  116. */
  117. void setBlend(bool enabled);
  118. /**
  119. * Explicitly sets the source used in the blend function for this render state.
  120. *
  121. * Note that the blend function is only applied when blending is enabled.
  122. *
  123. * @param blend Specifies how the source blending factors are computed.
  124. */
  125. void setBlendSrc(Blend blend);
  126. /**
  127. * Explicitly sets the source used in the blend function for this render state.
  128. *
  129. * Note that the blend function is only applied when blending is enabled.
  130. *
  131. * @param blend Specifies how the destination blending factors are computed.
  132. */
  133. void setBlendDst(Blend blend);
  134. /**
  135. * Explicitly enables or disables backface culling.
  136. *
  137. * @param enabled true to enable, false to disable.
  138. */
  139. void setCullFace(bool enabled);
  140. /**
  141. * Toggles depth testing.
  142. *
  143. * @param enabled true to enable, false to disable.
  144. */
  145. void setDepthTest(bool enabled);
  146. /**
  147. * Toggles depth writing.
  148. *
  149. * @param enabled true to enable, false to disable.
  150. */
  151. void setDepthWrite(bool enabled);
  152. /**
  153. * Sets a render state from the given name and value strings.
  154. *
  155. * This method attempts to interpret the passed in strings as render state
  156. * name and value. This is normally used when loading render states from
  157. * material files.
  158. *
  159. * @param name Name of the render state to set.
  160. * @param value Value of the specified render state.
  161. */
  162. void setState(const char* name, const char* value);
  163. private:
  164. /**
  165. * Constructor.
  166. */
  167. StateBlock();
  168. /**
  169. * Copy constructor.
  170. */
  171. StateBlock(const StateBlock& copy);
  172. /**
  173. * Destructor.
  174. */
  175. ~StateBlock();
  176. void bindNoRestore();
  177. static void restore(long stateOverrideBits);
  178. static void enableDepthWrite();
  179. // States
  180. bool _cullFaceEnabled;
  181. bool _depthTestEnabled;
  182. bool _depthWriteEnabled;
  183. bool _blendEnabled;
  184. Blend _blendSrc;
  185. Blend _blendDst;
  186. long _bits;
  187. static StateBlock* _defaultState;
  188. };
  189. /**
  190. * Returns a MaterialParameter for the specified name.
  191. *
  192. * The returned MaterialParameter can be used to set values for the specified
  193. * parameter name.
  194. *
  195. * @param name Material parameter (uniform) name.
  196. *
  197. * @return A MaterialParameter for the specified name.
  198. */
  199. MaterialParameter* getParameter(const char* name) const;
  200. /**
  201. * Sets a material parameter auto-binding.
  202. *
  203. * @param name The name of the material parameter to store an auto-binding for.
  204. * @param autoBinding A valid AutoBinding value.
  205. */
  206. void setParameterAutoBinding(const char* name, AutoBinding autoBinding);
  207. /**
  208. * Sets a material parameter auto-binding.
  209. *
  210. * This method parses the passed in autoBinding string and attempts to convert it
  211. * to an AutoBinding enumeration value, which is then stored in this render state.
  212. *
  213. * @param name The name of the material parameter to store an auto-binding for.
  214. * @param autoBinding A string matching one of the built-in AutoBinding enum constants.
  215. */
  216. void setParameterAutoBinding(const char* name, const char* autoBinding);
  217. /**
  218. * Sets the fixed-function render state of this object to the state contained
  219. * in the specified StateBlock.
  220. *
  221. * The passed in StateBlock is stored in this RenderState object with an
  222. * increased reference count and released when either a different StateBlock
  223. * is assigned, or when this RenderState object is destroyed.
  224. *
  225. * @param state The state block to set.
  226. */
  227. void setStateBlock(StateBlock* state);
  228. /**
  229. * Gets the fixed-function StateBlock for this RenderState object.
  230. *
  231. * The returned StateBlock is referenced by this RenderState and therefore
  232. * should not be released by the user. To release a StateBlock for a
  233. * RenderState, the setState(StateBlock*) method should be called, passing
  234. * NULL. This removes the StateBlock and resets the fixed-function render
  235. * state to the default state.
  236. *
  237. * It is legal to pass the returned StateBlock to another RenderState object.
  238. * In this case, the StateBlock will be referenced by both RenderState objects
  239. * and any changes to the StateBlock will be reflected in all objects
  240. * that reference it.
  241. *
  242. * @return The StateBlock for this RenderState.
  243. */
  244. StateBlock* getStateBlock() const;
  245. protected:
  246. /**
  247. * Constructor.
  248. */
  249. RenderState();
  250. /**
  251. * Destructor.
  252. */
  253. virtual ~RenderState();
  254. /**
  255. * Static initializer that is called during game startup.
  256. */
  257. static void initialize();
  258. /**
  259. * Static finalizer that is called during game shutdown.
  260. */
  261. static void finalize();
  262. /**
  263. * Sets the node that this render state is bound to.
  264. *
  265. * The specified node is used to apply auto-bindings for the render state.
  266. * This is typically set to the node of the model that a material is
  267. * applied to.
  268. *
  269. * @param node The node to use for applying auto-bindings.
  270. */
  271. void setNodeBinding(Node* node);
  272. /**
  273. * Applies the specified auto-binding.
  274. */
  275. void applyAutoBinding(const char* uniformName, AutoBinding binding);
  276. /**
  277. * Binds the render state for this RenderState and any of its parents, top-down,
  278. * for the given pass.
  279. */
  280. void bind(Pass* pass);
  281. /**
  282. * Returns the topmost RenderState in the hierarchy below the given RenderState.
  283. */
  284. RenderState* getTopmost(RenderState* below);
  285. /**
  286. * Copies the data from this RenderState into the given RenderState.
  287. *
  288. * @param renderState The RenderState to copy the data to.
  289. * @param context The clone context.
  290. */
  291. void cloneInto(RenderState* renderState, NodeCloneContext& context) const;
  292. private:
  293. /**
  294. * Hidden copy constructor.
  295. */
  296. RenderState(const RenderState& copy);
  297. /**
  298. * Hidden copy assignment operator.
  299. */
  300. RenderState& operator=(const RenderState&);
  301. protected:
  302. /**
  303. * Collection of MaterialParameter's to be applied to the gamplay::Effect.
  304. */
  305. mutable std::vector<MaterialParameter*> _parameters;
  306. /**
  307. * Map of IDs to AutoBindings.
  308. */
  309. std::map<std::string, AutoBinding> _autoBindings;
  310. /**
  311. * The Node bound to the RenderState.
  312. */
  313. Node* _nodeBinding;
  314. /**
  315. * The StateBlock of fixed-function render states that can be applied to the RenderState.
  316. */
  317. mutable StateBlock* _state;
  318. /**
  319. * The RenderState's parent.
  320. */
  321. RenderState* _parent;
  322. };
  323. }
  324. // Include MaterialParameter after the Pass class declaration
  325. // to avoid an erroneous circular dependency during compilation.
  326. #include "MaterialParameter.h"
  327. #endif