OgreGLRenderSystem.h 17 KB

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