CmGLRenderSystem.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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(SamplerState::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. void setClipPlanesImpl(const PlaneList& clipPlanes);
  104. bool activateGLTextureUnit(UINT16 unit);
  105. /// @copydoc RenderSystem::createMultiRenderTarget
  106. virtual MultiRenderTarget * createMultiRenderTarget(const String & name);
  107. /** See
  108. RenderSystem
  109. */
  110. String getErrorDescription(long errorNumber) const;
  111. /** See
  112. RenderSystem
  113. */
  114. void setClipPlane (UINT16 index, float A, float B, float C, float D);
  115. /** See
  116. RenderSystem
  117. */
  118. void enableClipPlane (UINT16 index, bool enable);
  119. // ----------------------------------
  120. // GLRenderSystem specific members
  121. // ----------------------------------
  122. /** One time initialization for the RenderState of a context. Things that
  123. only need to be set once, like the LightingModel can be defined here.
  124. */
  125. void oneTimeContextInitialization();
  126. /** Switch GL context, dealing with involved internal cached states too
  127. */
  128. void switchContext(GLContext *context);
  129. /** Unregister a render target->context mapping. If the context of target
  130. is the current context, change the context to the main context so it
  131. can be destroyed safely.
  132. @note This is automatically called by the destructor of
  133. GLContext.
  134. */
  135. /** Returns the main context */
  136. GLContext* _getMainContext() {return mMainContext;}
  137. public:
  138. // Default constructor / destructor
  139. GLRenderSystem();
  140. ~GLRenderSystem();
  141. // ----------------------------------
  142. // Overridden RenderSystem functions
  143. // ----------------------------------
  144. /** See
  145. RenderSystem
  146. */
  147. const String& getName(void) const;
  148. // -----------------------------
  149. // Low-level overridden members
  150. // -----------------------------
  151. /** See
  152. RenderSystem
  153. */
  154. void startUp_internal();
  155. /** See
  156. RenderSystem
  157. */
  158. void shutdown(void);
  159. /** See
  160. RenderSystem
  161. */
  162. void createRenderWindow_internal(const String &name, unsigned int width, unsigned int height,
  163. bool fullScreen, const NameValuePairList& miscParams, AsyncOp& asyncOp);
  164. /**
  165. * Set current render target to target, enabling its GL context if needed
  166. */
  167. void setRenderTarget_internal(RenderTarget *target);
  168. /** See
  169. RenderSystem
  170. */
  171. void bindGpuProgram_internal(GpuProgramHandle prg);
  172. /** See
  173. RenderSystem
  174. */
  175. void unbindGpuProgram_internal(GpuProgramType gptype);
  176. /** See
  177. RenderSystem
  178. */
  179. void bindGpuProgramParameters_internal(GpuProgramType gptype, GpuProgramParametersSharedPtr params, UINT16 mask);
  180. /** See
  181. RenderSystem
  182. */
  183. void setPointParameters_internal(float size, bool attenuationEnabled,
  184. float constant, float linear, float quadratic, float minSize, float maxSize);
  185. /** See
  186. RenderSystem
  187. */
  188. void setTexture_internal(UINT16 unit, bool enabled, const TexturePtr &tex);
  189. /** See
  190. RenderSystem
  191. */
  192. void setTextureAddressingMode_internal(UINT16 stage, const SamplerState::UVWAddressingMode& uvw);
  193. /** See
  194. RenderSystem
  195. */
  196. void setTextureBorderColor_internal(UINT16 stage, const Color& colour);
  197. /** See
  198. RenderSystem
  199. */
  200. void setTextureMipmapBias_internal(UINT16 unit, float bias);
  201. /** See
  202. RenderSystem
  203. */
  204. void setSceneBlending_internal(SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendOperation op );
  205. /** See
  206. RenderSystem
  207. */
  208. void setSeparateSceneBlending_internal(SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendFactor sourceFactorAlpha, SceneBlendFactor destFactorAlpha, SceneBlendOperation op, SceneBlendOperation alphaOp );
  209. /** See
  210. RenderSystem
  211. */
  212. void _setSceneBlendingOperation(SceneBlendOperation op);
  213. /** See
  214. RenderSystem
  215. */
  216. void _setSeparateSceneBlendingOperation(SceneBlendOperation op, SceneBlendOperation alphaOp);
  217. /** See
  218. RenderSystem
  219. */
  220. void setAlphaRejectSettings_internal(CompareFunction func, unsigned char value, bool alphaToCoverage);
  221. /** See
  222. RenderSystem
  223. */
  224. void setViewport_internal(const Viewport& vp);
  225. /** See
  226. RenderSystem
  227. */
  228. void beginFrame_internal(void);
  229. /** See
  230. RenderSystem
  231. */
  232. void endFrame_internal(void);
  233. /** See
  234. RenderSystem
  235. */
  236. void setCullingMode_internal(CullingMode mode);
  237. /** See
  238. RenderSystem
  239. */
  240. void setDepthBufferParams_internal(bool depthTest = true, bool depthWrite = true, CompareFunction depthFunction = CMPF_LESS_EQUAL);
  241. /** See
  242. RenderSystem
  243. */
  244. void setDepthBufferCheckEnabled_internal(bool enabled = true);
  245. /** See
  246. RenderSystem
  247. */
  248. void setDepthBufferWriteEnabled_internal(bool enabled = true);
  249. /** See
  250. RenderSystem
  251. */
  252. void setDepthBufferFunction_internal(CompareFunction func = CMPF_LESS_EQUAL);
  253. /** See
  254. RenderSystem
  255. */
  256. void setDepthBias_internal(float constantBias, float slopeScaleBias);
  257. /** See
  258. RenderSystem
  259. */
  260. void setColorBufferWriteEnabled_internal(bool red, bool green, bool blue, bool alpha);
  261. /** See
  262. RenderSystem
  263. */
  264. void convertProjectionMatrix(const Matrix4& matrix,
  265. Matrix4& dest, bool forGpuProgram = false);
  266. /** See
  267. RenderSystem
  268. */
  269. void setPolygonMode_internal(PolygonMode level);
  270. /** See
  271. RenderSystem
  272. */
  273. void setStencilCheckEnabled_internal(bool enabled);
  274. /** See
  275. RenderSystem.
  276. */
  277. void setStencilBufferParams_internal(CompareFunction func = CMPF_ALWAYS_PASS,
  278. UINT32 refValue = 0, UINT32 mask = 0xFFFFFFFF,
  279. StencilOperation stencilFailOp = SOP_KEEP,
  280. StencilOperation depthFailOp = SOP_KEEP,
  281. StencilOperation passOp = SOP_KEEP,
  282. bool twoSidedOperation = false);
  283. /** See
  284. RenderSystem
  285. */
  286. void setTextureFiltering_internal(UINT16 unit, FilterType ftype, FilterOptions filter);
  287. /** See
  288. RenderSystem
  289. */
  290. void setTextureAnisotropy_internal(UINT16 unit, unsigned int maxAnisotropy);
  291. /** See
  292. RenderSystem
  293. */
  294. void setVertexDeclaration_internal(VertexDeclarationPtr decl);
  295. /** See
  296. RenderSystem
  297. */
  298. void setVertexBufferBinding_internal(VertexBufferBinding* binding);
  299. /** See
  300. RenderSystem
  301. */
  302. void render_internal(const RenderOperation& op);
  303. /** See
  304. RenderSystem
  305. */
  306. void setScissorTest_internal(bool enabled, UINT32 left = 0, UINT32 top = 0, UINT32 right = 800, UINT32 bottom = 600) ;
  307. void clearFrameBuffer_internal(unsigned int buffers,
  308. const Color& colour = Color::Black,
  309. float depth = 1.0f, unsigned short stencil = 0);
  310. /** See
  311. RenderSystem
  312. */
  313. VertexElementType getColorVertexElementType(void) const;
  314. float getHorizontalTexelOffset(void);
  315. float getVerticalTexelOffset(void);
  316. float getMinimumDepthInputValue(void);
  317. float getMaximumDepthInputValue(void);
  318. void _unregisterContext(GLContext *context);
  319. };
  320. }
  321. #endif