CmGLRenderSystem.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 "CmVector4.h"
  33. namespace CamelotEngine {
  34. /**
  35. Implementation of GL as a rendering system.
  36. */
  37. class _OgreGLExport GLRenderSystem : public RenderSystem
  38. {
  39. private:
  40. /// Rendering loop control
  41. bool mStopRendering;
  42. /// View matrix to set world against
  43. Matrix4 mViewMatrix;
  44. Matrix4 mWorldMatrix;
  45. Matrix4 mTextureMatrix;
  46. /// Last min & mip filtering options, so we can combine them
  47. FilterOptions mMinFilter;
  48. FilterOptions mMipFilter;
  49. /// What texture coord set each texture unit is using
  50. size_t mTextureCoordIndex[CM_MAX_TEXTURE_LAYERS];
  51. /// Holds texture type settings for every stage
  52. GLenum mTextureTypes[CM_MAX_TEXTURE_LAYERS];
  53. /// Number of fixed-function texture units
  54. unsigned short mFixedFunctionTextureUnits;
  55. void initConfigOptions(void);
  56. void initInputDevices(void);
  57. void processInputDevices(void);
  58. void makeGLMatrix(GLfloat gl_matrix[16], const Matrix4& m);
  59. GLint getBlendMode(SceneBlendFactor ogreBlend) const;
  60. GLint getTextureAddressingMode(TextureState::TextureAddressingMode tam) const;
  61. void initialiseContext(RenderWindow* primary);
  62. void setLights();
  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. HardwareBufferManager* mHardwareBufferManager;
  80. GLGpuProgramManager* mGpuProgramManager;
  81. GLSLProgramFactory* mGLSLProgramFactory;
  82. unsigned short mCurrentLights;
  83. GLuint getCombinedMinMipFilter(void) const;
  84. GLGpuProgram* mCurrentVertexProgram;
  85. GLGpuProgram* mCurrentFragmentProgram;
  86. GLGpuProgram* mCurrentGeometryProgram;
  87. /* The main GL context - main thread only */
  88. GLContext *mMainContext;
  89. /* The current GL context - main thread only */
  90. GLContext *mCurrentContext;
  91. typedef list<GLContext*>::type GLContextList;
  92. /// List of background thread contexts
  93. GLContextList mBackgroundContextList;
  94. /** Manager object for creating render textures.
  95. Direct render to texture via GL_EXT_framebuffer_object is preferable
  96. to pbuffers, which depend on the GL support used and are generally
  97. unwieldy and slow. However, FBO support for stencil buffers is poor.
  98. */
  99. GLRTTManager *mRTTManager;
  100. UINT16 mActiveTextureUnit;
  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. RenderWindow* _initialise(bool autoCreateWindow, const String& windowTitle = "OGRE Render Window");
  131. /** See
  132. RenderSystem
  133. */
  134. virtual RenderSystemCapabilities* createRenderSystemCapabilities() const;
  135. /** See
  136. RenderSystem
  137. */
  138. void initialiseFromRenderSystemCapabilities(RenderSystemCapabilities* caps, RenderTarget* primary);
  139. /** See
  140. RenderSystem
  141. */
  142. void reinitialise(void); // Used if settings changed mid-rendering
  143. /** See
  144. RenderSystem
  145. */
  146. void shutdown(void);
  147. /** See
  148. RenderSystem
  149. */
  150. void setAmbientLight(float r, float g, float b);
  151. /** See
  152. RenderSystem
  153. */
  154. void setShadingType(ShadeOptions so);
  155. /** See
  156. RenderSystem
  157. */
  158. void setLightingEnabled(bool enabled);
  159. /// @copydoc RenderSystem::_createRenderWindow
  160. RenderWindow* _createRenderWindow(const String &name, unsigned int width, unsigned int height,
  161. bool fullScreen, const NameValuePairList *miscParams = 0);
  162. /// @copydoc RenderSystem::_createRenderWindows
  163. bool _createRenderWindows(const RenderWindowDescriptionList& renderWindowDescriptions,
  164. RenderWindowList& createdWindows);
  165. /// @copydoc RenderSystem::createMultiRenderTarget
  166. virtual MultiRenderTarget * createMultiRenderTarget(const String & name);
  167. /** See
  168. RenderSystem
  169. */
  170. void destroyRenderWindow(RenderWindow* pWin);
  171. /** See
  172. RenderSystem
  173. */
  174. String getErrorDescription(long errorNumber) const;
  175. /** See
  176. RenderSystem
  177. */
  178. VertexElementType getColourVertexElementType(void) const;
  179. /** See
  180. RenderSystem
  181. */
  182. void setNormaliseNormals(bool normalise);
  183. // -----------------------------
  184. // Low-level overridden members
  185. // -----------------------------
  186. /** See
  187. RenderSystem
  188. */
  189. bool areFixedFunctionLightsInViewSpace() const { return true; }
  190. /** See
  191. RenderSystem
  192. */
  193. void _setWorldMatrix(const Matrix4 &m);
  194. /** See
  195. RenderSystem
  196. */
  197. void _setViewMatrix(const Matrix4 &m);
  198. /** See
  199. RenderSystem
  200. */
  201. void _setProjectionMatrix(const Matrix4 &m);
  202. /** See
  203. RenderSystem
  204. */
  205. void _setSurfaceParams(const Color &ambient,
  206. const Color &diffuse, const Color &specular,
  207. const Color &emissive, float shininess,
  208. TrackVertexColourType tracking);
  209. /** See
  210. RenderSystem
  211. */
  212. void _setPointParameters(float size, bool attenuationEnabled,
  213. float constant, float linear, float quadratic, float minSize, float maxSize);
  214. /** See
  215. RenderSystem
  216. */
  217. void _setPointSpritesEnabled(bool enabled);
  218. /** See
  219. RenderSystem
  220. */
  221. void _setTexture(size_t unit, bool enabled, const TexturePtr &tex);
  222. /** See
  223. RenderSystem
  224. */
  225. void _setTextureCoordSet(size_t stage, size_t index);
  226. /** See
  227. RenderSystem
  228. */
  229. void _setTextureCoordCalculation(size_t stage, TexCoordCalcMethod m,
  230. const Frustum* frustum = 0);
  231. /** See
  232. RenderSystem
  233. */
  234. void _setTextureAddressingMode(size_t stage, const TextureState::UVWAddressingMode& uvw);
  235. /** See
  236. RenderSystem
  237. */
  238. void _setTextureBorderColour(size_t stage, const Color& colour);
  239. /** See
  240. RenderSystem
  241. */
  242. void _setTextureMipmapBias(size_t unit, float bias);
  243. /** See
  244. RenderSystem
  245. */
  246. void _setTextureMatrix(size_t stage, const Matrix4& xform);
  247. /** See
  248. RenderSystem
  249. */
  250. void _setSceneBlending(SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendOperation op );
  251. /** See
  252. RenderSystem
  253. */
  254. void _setSeparateSceneBlending(SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendFactor sourceFactorAlpha, SceneBlendFactor destFactorAlpha, SceneBlendOperation op, SceneBlendOperation alphaOp );
  255. /** See
  256. RenderSystem
  257. */
  258. void _setSceneBlendingOperation(SceneBlendOperation op);
  259. /** See
  260. RenderSystem
  261. */
  262. void _setSeparateSceneBlendingOperation(SceneBlendOperation op, SceneBlendOperation alphaOp);
  263. /** See
  264. RenderSystem
  265. */
  266. void _setAlphaRejectSettings(CompareFunction func, unsigned char value, bool alphaToCoverage);
  267. /** See
  268. RenderSystem
  269. */
  270. void _setViewport(Viewport *vp);
  271. /** See
  272. RenderSystem
  273. */
  274. void _beginFrame(void);
  275. /** See
  276. RenderSystem
  277. */
  278. void _endFrame(void);
  279. /** See
  280. RenderSystem
  281. */
  282. void _setCullingMode(CullingMode mode);
  283. /** See
  284. RenderSystem
  285. */
  286. void _setDepthBufferParams(bool depthTest = true, bool depthWrite = true, CompareFunction depthFunction = CMPF_LESS_EQUAL);
  287. /** See
  288. RenderSystem
  289. */
  290. void _setDepthBufferCheckEnabled(bool enabled = true);
  291. /** See
  292. RenderSystem
  293. */
  294. void _setDepthBufferWriteEnabled(bool enabled = true);
  295. /** See
  296. RenderSystem
  297. */
  298. void _setDepthBufferFunction(CompareFunction func = CMPF_LESS_EQUAL);
  299. /** See
  300. RenderSystem
  301. */
  302. void _setDepthBias(float constantBias, float slopeScaleBias);
  303. /** See
  304. RenderSystem
  305. */
  306. void _setColourBufferWriteEnabled(bool red, bool green, bool blue, bool alpha);
  307. /** See
  308. RenderSystem
  309. */
  310. void _setFog(FogMode mode, const Color& colour, float density, float start, float end);
  311. /** See
  312. RenderSystem
  313. */
  314. void _convertProjectionMatrix(const Matrix4& matrix,
  315. Matrix4& dest, bool forGpuProgram = false);
  316. /** See
  317. RenderSystem
  318. */
  319. void _makeProjectionMatrix(const Radian& fovy, float aspect, float nearPlane, float farPlane,
  320. Matrix4& dest, bool forGpuProgram = false);
  321. /** See
  322. RenderSystem
  323. */
  324. void _makeProjectionMatrix(float left, float right, float bottom, float top,
  325. float nearPlane, float farPlane, Matrix4& dest, bool forGpuProgram = false);
  326. /** See
  327. RenderSystem
  328. */
  329. void _makeOrthoMatrix(const Radian& fovy, float aspect, float nearPlane, float farPlane,
  330. Matrix4& dest, bool forGpuProgram = false);
  331. /** See
  332. RenderSystem
  333. */
  334. void _applyObliqueDepthProjection(Matrix4& matrix, const Plane& plane,
  335. bool forGpuProgram);
  336. /** See
  337. RenderSystem
  338. */
  339. void setClipPlane (UINT16 index, float A, float B, float C, float D);
  340. /** See
  341. RenderSystem
  342. */
  343. void enableClipPlane (UINT16 index, bool enable);
  344. /** See
  345. RenderSystem
  346. */
  347. void _setPolygonMode(PolygonMode level);
  348. /** See
  349. RenderSystem
  350. */
  351. void setStencilCheckEnabled(bool enabled);
  352. /** See
  353. RenderSystem.
  354. */
  355. void setStencilBufferParams(CompareFunction func = CMPF_ALWAYS_PASS,
  356. UINT32 refValue = 0, UINT32 mask = 0xFFFFFFFF,
  357. StencilOperation stencilFailOp = SOP_KEEP,
  358. StencilOperation depthFailOp = SOP_KEEP,
  359. StencilOperation passOp = SOP_KEEP,
  360. bool twoSidedOperation = false);
  361. /** See
  362. RenderSystem
  363. */
  364. void _setTextureUnitFiltering(size_t unit, FilterType ftype, FilterOptions filter);
  365. /** See
  366. RenderSystem
  367. */
  368. void _setTextureLayerAnisotropy(size_t unit, unsigned int maxAnisotropy);
  369. /** See
  370. RenderSystem
  371. */
  372. void setVertexDeclaration(VertexDeclaration* decl);
  373. /** See
  374. RenderSystem
  375. */
  376. void setVertexBufferBinding(VertexBufferBinding* binding);
  377. /** See
  378. RenderSystem
  379. */
  380. void _render(const RenderOperation& op);
  381. /** See
  382. RenderSystem
  383. */
  384. void bindGpuProgram(GpuProgram* prg);
  385. /** See
  386. RenderSystem
  387. */
  388. void unbindGpuProgram(GpuProgramType gptype);
  389. /** See
  390. RenderSystem
  391. */
  392. void bindGpuProgramParameters(GpuProgramType gptype,
  393. GpuProgramParametersSharedPtr params, UINT16 variabilityMask);
  394. /** See
  395. RenderSystem
  396. */
  397. void bindGpuProgramPassIterationParameters(GpuProgramType gptype);
  398. /** See
  399. RenderSystem
  400. */
  401. void setScissorTest(bool enabled, size_t left = 0, size_t top = 0, size_t right = 800, size_t bottom = 600) ;
  402. void clearFrameBuffer(unsigned int buffers,
  403. const Color& colour = Color::Black,
  404. float depth = 1.0f, unsigned short stencil = 0);
  405. HardwareOcclusionQuery* createHardwareOcclusionQuery(void);
  406. float getHorizontalTexelOffset(void);
  407. float getVerticalTexelOffset(void);
  408. float getMinimumDepthInputValue(void);
  409. float getMaximumDepthInputValue(void);
  410. CM_MUTEX(mThreadInitMutex)
  411. void registerThread();
  412. void unregisterThread();
  413. void preExtraThreadsStarted();
  414. void postExtraThreadsStarted();
  415. // ----------------------------------
  416. // GLRenderSystem specific members
  417. // ----------------------------------
  418. /** One time initialization for the RenderState of a context. Things that
  419. only need to be set once, like the LightingModel can be defined here.
  420. */
  421. void _oneTimeContextInitialization();
  422. /** Switch GL context, dealing with involved internal cached states too
  423. */
  424. void _switchContext(GLContext *context);
  425. /**
  426. * Set current render target to target, enabling its GL context if needed
  427. */
  428. void _setRenderTarget(RenderTarget *target);
  429. /** Unregister a render target->context mapping. If the context of target
  430. is the current context, change the context to the main context so it
  431. can be destroyed safely.
  432. @note This is automatically called by the destructor of
  433. GLContext.
  434. */
  435. void _unregisterContext(GLContext *context);
  436. /** Returns the main context */
  437. GLContext* _getMainContext() {return mMainContext;}
  438. /// @copydoc RenderSystem::getDisplayMonitorCount
  439. unsigned int getDisplayMonitorCount() const;
  440. };
  441. }
  442. #endif