CmGLRenderSystem.h 13 KB

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