CmGLRenderSystem.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. /// Store last depth write state
  64. bool mDepthWrite;
  65. /// Store last stencil mask state
  66. UINT32 mStencilMask;
  67. /// Store last colour write state
  68. bool mColourWrite[4];
  69. GLint convertCompareFunction(CompareFunction func) const;
  70. GLint convertStencilOp(StencilOperation op, bool invert = false) const;
  71. /// Internal method for anisotropy validation
  72. GLfloat _getCurrentAnisotropy(size_t unit);
  73. /// GL support class, used for creating windows etc.
  74. GLSupport* mGLSupport;
  75. bool mUseAutoTextureMatrix;
  76. GLfloat mAutoTextureMatrix[16];
  77. /// Check if the GL system has already been initialised
  78. bool mGLInitialised;
  79. GLSLProgramFactory* mGLSLProgramFactory;
  80. CgProgramFactory* mCgProgramFactory;
  81. unsigned short mCurrentLights;
  82. GLuint getCombinedMinMipFilter(void) const;
  83. GLGpuProgram* mCurrentVertexProgram;
  84. GLGpuProgram* mCurrentFragmentProgram;
  85. GLGpuProgram* mCurrentGeometryProgram;
  86. /* The main GL context - main thread only */
  87. GLContext *mMainContext;
  88. /* The current GL context - main thread only */
  89. GLContext *mCurrentContext;
  90. typedef list<GLContext*>::type GLContextList;
  91. /// List of background thread contexts
  92. GLContextList mBackgroundContextList;
  93. UINT16 mActiveTextureUnit;
  94. /************************************************************************/
  95. /* INTERNAL CALLBACKS */
  96. /************************************************************************/
  97. protected:
  98. void startUp_internal(AsyncOp& asyncOp);
  99. void createRenderWindow_internal(const String &name, unsigned int width, unsigned int height,
  100. bool fullScreen, const NameValuePairList& miscParams, AsyncOp& asyncOp);
  101. protected:
  102. void setClipPlanesImpl(const PlaneList& clipPlanes);
  103. bool activateGLTextureUnit(size_t unit);
  104. public:
  105. // Default constructor / destructor
  106. GLRenderSystem();
  107. ~GLRenderSystem();
  108. // ----------------------------------
  109. // Overridden RenderSystem functions
  110. // ----------------------------------
  111. /** See
  112. RenderSystem
  113. */
  114. const String& getName(void) const;
  115. /** See
  116. RenderSystem
  117. */
  118. ConfigOptionMap& getConfigOptions(void);
  119. /** See
  120. RenderSystem
  121. */
  122. void setConfigOption(const String &name, const String &value);
  123. /** See
  124. RenderSystem
  125. */
  126. String validateConfigOptions(void);
  127. /** See
  128. RenderSystem
  129. */
  130. virtual RenderSystemCapabilities* createRenderSystemCapabilities() const;
  131. /** See
  132. RenderSystem
  133. */
  134. void initialiseFromRenderSystemCapabilities(RenderSystemCapabilities* caps, RenderTarget* primary);
  135. /** See
  136. RenderSystem
  137. */
  138. void shutdown(void);
  139. /// @copydoc RenderSystem::createMultiRenderTarget
  140. virtual MultiRenderTarget * createMultiRenderTarget(const String & name);
  141. /** See
  142. RenderSystem
  143. */
  144. void destroyRenderWindow(RenderWindow* pWin);
  145. /** See
  146. RenderSystem
  147. */
  148. String getErrorDescription(long errorNumber) const;
  149. /** See
  150. RenderSystem
  151. */
  152. VertexElementType getColorVertexElementType(void) const;
  153. // -----------------------------
  154. // Low-level overridden members
  155. // -----------------------------
  156. /** See
  157. RenderSystem
  158. */
  159. void setPointParameters(float size, bool attenuationEnabled,
  160. float constant, float linear, float quadratic, float minSize, float maxSize);
  161. /** See
  162. RenderSystem
  163. */
  164. void setTexture(size_t unit, bool enabled, const TexturePtr &tex);
  165. /** See
  166. RenderSystem
  167. */
  168. void setTextureAddressingMode(size_t stage, const SamplerState::UVWAddressingMode& uvw);
  169. /** See
  170. RenderSystem
  171. */
  172. void setTextureBorderColor(size_t stage, const Color& colour);
  173. /** See
  174. RenderSystem
  175. */
  176. void setTextureMipmapBias(size_t unit, float bias);
  177. /** See
  178. RenderSystem
  179. */
  180. void setSceneBlending(SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendOperation op );
  181. /** See
  182. RenderSystem
  183. */
  184. void setSeparateSceneBlending(SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendFactor sourceFactorAlpha, SceneBlendFactor destFactorAlpha, SceneBlendOperation op, SceneBlendOperation alphaOp );
  185. /** See
  186. RenderSystem
  187. */
  188. void _setSceneBlendingOperation(SceneBlendOperation op);
  189. /** See
  190. RenderSystem
  191. */
  192. void _setSeparateSceneBlendingOperation(SceneBlendOperation op, SceneBlendOperation alphaOp);
  193. /** See
  194. RenderSystem
  195. */
  196. void setAlphaRejectSettings(CompareFunction func, unsigned char value, bool alphaToCoverage);
  197. /** See
  198. RenderSystem
  199. */
  200. void setViewport(const Viewport& vp);
  201. /** See
  202. RenderSystem
  203. */
  204. void beginFrame(void);
  205. /** See
  206. RenderSystem
  207. */
  208. void endFrame(void);
  209. /** See
  210. RenderSystem
  211. */
  212. void setCullingMode(CullingMode mode);
  213. /** See
  214. RenderSystem
  215. */
  216. void setDepthBufferParams(bool depthTest = true, bool depthWrite = true, CompareFunction depthFunction = CMPF_LESS_EQUAL);
  217. /** See
  218. RenderSystem
  219. */
  220. void setDepthBufferCheckEnabled(bool enabled = true);
  221. /** See
  222. RenderSystem
  223. */
  224. void setDepthBufferWriteEnabled(bool enabled = true);
  225. /** See
  226. RenderSystem
  227. */
  228. void setDepthBufferFunction(CompareFunction func = CMPF_LESS_EQUAL);
  229. /** See
  230. RenderSystem
  231. */
  232. void setDepthBias(float constantBias, float slopeScaleBias);
  233. /** See
  234. RenderSystem
  235. */
  236. void setColorBufferWriteEnabled(bool red, bool green, bool blue, bool alpha);
  237. /** See
  238. RenderSystem
  239. */
  240. void _convertProjectionMatrix(const Matrix4& matrix,
  241. Matrix4& dest, bool forGpuProgram = false);
  242. /** See
  243. RenderSystem
  244. */
  245. void setClipPlane (UINT16 index, float A, float B, float C, float D);
  246. /** See
  247. RenderSystem
  248. */
  249. void enableClipPlane (UINT16 index, bool enable);
  250. /** See
  251. RenderSystem
  252. */
  253. void setPolygonMode(PolygonMode level);
  254. /** See
  255. RenderSystem
  256. */
  257. void setStencilCheckEnabled(bool enabled);
  258. /** See
  259. RenderSystem.
  260. */
  261. void setStencilBufferParams(CompareFunction func = CMPF_ALWAYS_PASS,
  262. UINT32 refValue = 0, UINT32 mask = 0xFFFFFFFF,
  263. StencilOperation stencilFailOp = SOP_KEEP,
  264. StencilOperation depthFailOp = SOP_KEEP,
  265. StencilOperation passOp = SOP_KEEP,
  266. bool twoSidedOperation = false);
  267. /** See
  268. RenderSystem
  269. */
  270. void setTextureFiltering(size_t unit, FilterType ftype, FilterOptions filter);
  271. /** See
  272. RenderSystem
  273. */
  274. void setTextureAnisotropy(size_t unit, unsigned int maxAnisotropy);
  275. /** See
  276. RenderSystem
  277. */
  278. void setVertexDeclaration(VertexDeclarationPtr decl);
  279. /** See
  280. RenderSystem
  281. */
  282. void setVertexBufferBinding(VertexBufferBinding* binding);
  283. /** See
  284. RenderSystem
  285. */
  286. void render(const RenderOperation& op);
  287. /** See
  288. RenderSystem
  289. */
  290. void bindGpuProgram(GpuProgramRef prg);
  291. /** See
  292. RenderSystem
  293. */
  294. void unbindGpuProgram(GpuProgramType gptype);
  295. /** See
  296. RenderSystem
  297. */
  298. void bindGpuProgramParameters(GpuProgramType gptype,
  299. GpuProgramParametersSharedPtr params, UINT16 variabilityMask);
  300. /** See
  301. RenderSystem
  302. */
  303. void setScissorTest(bool enabled, size_t left = 0, size_t top = 0, size_t right = 800, size_t bottom = 600) ;
  304. void clearFrameBuffer(unsigned int buffers,
  305. const Color& colour = Color::Black,
  306. float depth = 1.0f, unsigned short stencil = 0);
  307. float getHorizontalTexelOffset(void);
  308. float getVerticalTexelOffset(void);
  309. float getMinimumDepthInputValue(void);
  310. float getMaximumDepthInputValue(void);
  311. CM_MUTEX(mThreadInitMutex)
  312. void registerThread();
  313. void unregisterThread();
  314. // ----------------------------------
  315. // GLRenderSystem specific members
  316. // ----------------------------------
  317. /** One time initialization for the RenderState of a context. Things that
  318. only need to be set once, like the LightingModel can be defined here.
  319. */
  320. void _oneTimeContextInitialization();
  321. /** Switch GL context, dealing with involved internal cached states too
  322. */
  323. void _switchContext(GLContext *context);
  324. /**
  325. * Set current render target to target, enabling its GL context if needed
  326. */
  327. void setRenderTarget(RenderTarget *target);
  328. /** Unregister a render target->context mapping. If the context of target
  329. is the current context, change the context to the main context so it
  330. can be destroyed safely.
  331. @note This is automatically called by the destructor of
  332. GLContext.
  333. */
  334. void _unregisterContext(GLContext *context);
  335. /** Returns the main context */
  336. GLContext* _getMainContext() {return mMainContext;}
  337. /// @copydoc RenderSystem::getDisplayMonitorCount
  338. unsigned int getDisplayMonitorCount() const;
  339. };
  340. }
  341. #endif