OgreGLRenderSystem.h 17 KB

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