CmGLRenderSystem.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef __GLRenderSystem_H__
  25. #define __GLRenderSystem_H__
  26. #include "CmGLPrerequisites.h"
  27. #include "CmRenderSystem.h"
  28. #include "CmGLHardwareBufferManager.h"
  29. #include "CmGLGpuProgramManager.h"
  30. #include "CmGLSLProgramFactory.h"
  31. #include "CmConfigOptionMap.h"
  32. #include "CmCgProgramFactory.h"
  33. #include "CmVector4.h"
  34. namespace CamelotEngine {
  35. /**
  36. Implementation of GL as a rendering system.
  37. */
  38. class CM_RSGL_EXPORT GLRenderSystem : public RenderSystem
  39. {
  40. private:
  41. /// Rendering loop control
  42. bool mStopRendering;
  43. /// View matrix to set world against
  44. Matrix4 mViewMatrix;
  45. Matrix4 mWorldMatrix;
  46. Matrix4 mTextureMatrix;
  47. /// Last min & mip filtering options, so we can combine them
  48. FilterOptions mMinFilter;
  49. FilterOptions mMipFilter;
  50. /// What texture coord set each texture unit is using
  51. UINT32 mTextureCoordIndex[CM_MAX_TEXTURE_LAYERS];
  52. /// Holds texture type settings for every stage
  53. GLenum mTextureTypes[CM_MAX_TEXTURE_LAYERS];
  54. /// Number of fixed-function texture units
  55. unsigned short mFixedFunctionTextureUnits;
  56. void initConfigOptions(void);
  57. void initInputDevices(void);
  58. void processInputDevices(void);
  59. void makeGLMatrix(GLfloat gl_matrix[16], const Matrix4& m);
  60. GLint getBlendMode(SceneBlendFactor ogreBlend) const;
  61. GLint getTextureAddressingMode(TextureAddressingMode tam) const;
  62. void initialiseContext(RenderWindow* primary);
  63. /** See
  64. RenderSystem
  65. */
  66. virtual RenderSystemCapabilities* createRenderSystemCapabilities() const;
  67. /** See
  68. RenderSystem
  69. */
  70. void initialiseFromRenderSystemCapabilities(RenderSystemCapabilities* caps, RenderTarget* primary);
  71. /// Store last depth write state
  72. bool mDepthWrite;
  73. /// Store last stencil mask state
  74. UINT32 mStencilMask;
  75. /// Store last colour write state
  76. bool mColourWrite[4];
  77. GLint convertCompareFunction(CompareFunction func) const;
  78. GLint convertStencilOp(StencilOperation op, bool invert = false) const;
  79. /// Internal method for anisotropy validation
  80. GLfloat _getCurrentAnisotropy(UINT16 unit);
  81. /// GL support class, used for creating windows etc.
  82. GLSupport* mGLSupport;
  83. bool mUseAutoTextureMatrix;
  84. GLfloat mAutoTextureMatrix[16];
  85. /// Check if the GL system has already been initialised
  86. bool mGLInitialised;
  87. GLSLProgramFactory* mGLSLProgramFactory;
  88. CgProgramFactory* mCgProgramFactory;
  89. unsigned short mCurrentLights;
  90. GLuint getCombinedMinMipFilter(void) const;
  91. GLGpuProgram* mCurrentVertexProgram;
  92. GLGpuProgram* mCurrentFragmentProgram;
  93. GLGpuProgram* mCurrentGeometryProgram;
  94. /* The main GL context - main thread only */
  95. GLContext *mMainContext;
  96. /* The current GL context - main thread only */
  97. GLContext *mCurrentContext;
  98. typedef list<GLContext*>::type GLContextList;
  99. /// List of background thread contexts
  100. GLContextList mBackgroundContextList;
  101. UINT16 mActiveTextureUnit;
  102. protected:
  103. /** See
  104. RenderSystem
  105. */
  106. void startUp_internal();
  107. /** See
  108. RenderSystem
  109. */
  110. void shutdown_internal(void);
  111. void setClipPlanesImpl(const PlaneList& clipPlanes);
  112. bool activateGLTextureUnit(UINT16 unit);
  113. /// @copydoc RenderSystem::createMultiRenderTarget
  114. virtual MultiRenderTarget * createMultiRenderTarget(const String & name);
  115. /** See
  116. RenderSystem
  117. */
  118. String getErrorDescription(long errorNumber) const;
  119. /** See
  120. RenderSystem
  121. */
  122. void setClipPlane (UINT16 index, float A, float B, float C, float D);
  123. /** See
  124. RenderSystem
  125. */
  126. void enableClipPlane (UINT16 index, bool enable);
  127. /** Sets the texture addressing mode for a texture unit.*/
  128. void setTextureAddressingMode(UINT16 stage, const UVWAddressingMode& uvw);
  129. /** Sets the texture border color for a texture unit.*/
  130. void setTextureBorderColor(UINT16 stage, const Color& color);
  131. /** Sets the mipmap bias value for a given texture unit.
  132. @remarks
  133. This allows you to adjust the mipmap calculation up or down for a
  134. given texture unit. Negative values force a larger mipmap to be used,
  135. positive values force a smaller mipmap to be used. Units are in numbers
  136. of levels, so +1 forces the mipmaps to one smaller level.
  137. @note Only does something if render system has capability RSC_MIPMAP_LOD_BIAS.
  138. */
  139. void setTextureMipmapBias(UINT16 unit, float bias);
  140. /** Sets a single filter for a given texture unit.
  141. @param unit The texture unit to set the filtering options for
  142. @param ftype The filter type
  143. @param filter The filter to be used
  144. */
  145. void setTextureFiltering(UINT16 unit, FilterType ftype, FilterOptions filter);
  146. /** Sets the maximal anisotropy for the specified texture unit.*/
  147. void setTextureAnisotropy(UINT16 unit, unsigned int maxAnisotropy);
  148. // ----------------------------------
  149. // GLRenderSystem specific members
  150. // ----------------------------------
  151. /** One time initialization for the RenderState of a context. Things that
  152. only need to be set once, like the LightingModel can be defined here.
  153. */
  154. void oneTimeContextInitialization();
  155. /** Switch GL context, dealing with involved internal cached states too
  156. */
  157. void switchContext(GLContext *context);
  158. /** Unregister a render target->context mapping. If the context of target
  159. is the current context, change the context to the main context so it
  160. can be destroyed safely.
  161. @note This is automatically called by the destructor of
  162. GLContext.
  163. */
  164. /** Returns the main context */
  165. GLContext* _getMainContext() {return mMainContext;}
  166. public:
  167. // Default constructor / destructor
  168. GLRenderSystem();
  169. ~GLRenderSystem();
  170. // ----------------------------------
  171. // Overridden RenderSystem functions
  172. // ----------------------------------
  173. /** See
  174. RenderSystem
  175. */
  176. const String& getName(void) const;
  177. // -----------------------------
  178. // Low-level overridden members
  179. // -----------------------------
  180. /** See
  181. RenderSystem
  182. */
  183. void createRenderWindow_internal(const String &name, unsigned int width, unsigned int height,
  184. bool fullScreen, const NameValuePairList& miscParams, AsyncOp& asyncOp);
  185. /**
  186. * Set current render target to target, enabling its GL context if needed
  187. */
  188. void setRenderTarget(RenderTarget *target);
  189. /** See
  190. RenderSystem
  191. */
  192. void bindGpuProgram(GpuProgramHandle prg);
  193. /** See
  194. RenderSystem
  195. */
  196. void unbindGpuProgram(GpuProgramType gptype);
  197. /** See
  198. RenderSystem
  199. */
  200. void bindGpuProgramParameters(GpuProgramType gptype, GpuProgramParametersSharedPtr params, UINT16 mask);
  201. /** See
  202. RenderSystem
  203. */
  204. void setPointParameters(float size, bool attenuationEnabled,
  205. float constant, float linear, float quadratic, float minSize, float maxSize);
  206. /** See
  207. RenderSystem
  208. */
  209. void setTexture(UINT16 unit, bool enabled, const TexturePtr &tex);
  210. /**
  211. * @copydoc RenderSystem::setSamplerState()
  212. */
  213. void setSamplerState(UINT16 unit, const SamplerState& state);
  214. void setSceneBlending(SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendOperation op );
  215. /** See
  216. RenderSystem
  217. */
  218. void setSeparateSceneBlending(SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendFactor sourceFactorAlpha, SceneBlendFactor destFactorAlpha, SceneBlendOperation op, SceneBlendOperation alphaOp );
  219. /** See
  220. RenderSystem
  221. */
  222. void _setSceneBlendingOperation(SceneBlendOperation op);
  223. /** See
  224. RenderSystem
  225. */
  226. void _setSeparateSceneBlendingOperation(SceneBlendOperation op, SceneBlendOperation alphaOp);
  227. /** See
  228. RenderSystem
  229. */
  230. void setAlphaRejectSettings(CompareFunction func, unsigned char value, bool alphaToCoverage);
  231. /** See
  232. RenderSystem
  233. */
  234. void setViewport(const Viewport& vp);
  235. /** See
  236. RenderSystem
  237. */
  238. void beginFrame(void);
  239. /** See
  240. RenderSystem
  241. */
  242. void endFrame(void);
  243. /** See
  244. RenderSystem
  245. */
  246. void setCullingMode(CullingMode mode);
  247. /** See
  248. RenderSystem
  249. */
  250. void setDepthBufferParams(bool depthTest = true, bool depthWrite = true, CompareFunction depthFunction = CMPF_LESS_EQUAL);
  251. /** See
  252. RenderSystem
  253. */
  254. void setDepthBufferCheckEnabled(bool enabled = true);
  255. /** See
  256. RenderSystem
  257. */
  258. void setDepthBufferWriteEnabled(bool enabled = true);
  259. /** See
  260. RenderSystem
  261. */
  262. void setDepthBufferFunction(CompareFunction func = CMPF_LESS_EQUAL);
  263. /** See
  264. RenderSystem
  265. */
  266. void setDepthBias(float constantBias, float slopeScaleBias);
  267. /** See
  268. RenderSystem
  269. */
  270. void setColorBufferWriteEnabled(bool red, bool green, bool blue, bool alpha);
  271. /** See
  272. RenderSystem
  273. */
  274. void convertProjectionMatrix(const Matrix4& matrix,
  275. Matrix4& dest, bool forGpuProgram = false);
  276. /** See
  277. RenderSystem
  278. */
  279. void setPolygonMode(PolygonMode level);
  280. /** See
  281. RenderSystem
  282. */
  283. void setStencilCheckEnabled(bool enabled);
  284. /** See
  285. RenderSystem.
  286. */
  287. void setStencilBufferParams(CompareFunction func = CMPF_ALWAYS_PASS,
  288. UINT32 refValue = 0, UINT32 mask = 0xFFFFFFFF,
  289. StencilOperation stencilFailOp = SOP_KEEP,
  290. StencilOperation depthFailOp = SOP_KEEP,
  291. StencilOperation passOp = SOP_KEEP,
  292. bool twoSidedOperation = false);
  293. /** See
  294. RenderSystem
  295. */
  296. void setVertexDeclaration(VertexDeclarationPtr decl);
  297. /** See
  298. RenderSystem
  299. */
  300. void setVertexBufferBinding(VertexBufferBinding* binding);
  301. /** See
  302. RenderSystem
  303. */
  304. void render(const RenderOperation& op);
  305. /** See
  306. RenderSystem
  307. */
  308. void setScissorTest(bool enabled, UINT32 left = 0, UINT32 top = 0, UINT32 right = 800, UINT32 bottom = 600) ;
  309. void clearFrameBuffer(unsigned int buffers,
  310. const Color& colour = Color::Black,
  311. float depth = 1.0f, unsigned short stencil = 0);
  312. /** See
  313. RenderSystem
  314. */
  315. VertexElementType getColorVertexElementType(void) const;
  316. float getHorizontalTexelOffset(void);
  317. float getVerticalTexelOffset(void);
  318. float getMinimumDepthInputValue(void);
  319. float getMaximumDepthInputValue(void);
  320. void _unregisterContext(GLContext *context);
  321. };
  322. }
  323. #endif