CmGLRenderSystem.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 CM_RSGL_EXPORT 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. GLSLProgramFactory* mGLSLProgramFactory;
  80. unsigned short mCurrentLights;
  81. GLuint getCombinedMinMipFilter(void) const;
  82. GLGpuProgram* mCurrentVertexProgram;
  83. GLGpuProgram* mCurrentFragmentProgram;
  84. GLGpuProgram* mCurrentGeometryProgram;
  85. /* The main GL context - main thread only */
  86. GLContext *mMainContext;
  87. /* The current GL context - main thread only */
  88. GLContext *mCurrentContext;
  89. typedef list<GLContext*>::type GLContextList;
  90. /// List of background thread contexts
  91. GLContextList mBackgroundContextList;
  92. UINT16 mActiveTextureUnit;
  93. protected:
  94. void setClipPlanesImpl(const PlaneList& clipPlanes);
  95. bool activateGLTextureUnit(size_t unit);
  96. public:
  97. // Default constructor / destructor
  98. GLRenderSystem();
  99. ~GLRenderSystem();
  100. // ----------------------------------
  101. // Overridden RenderSystem functions
  102. // ----------------------------------
  103. /** See
  104. RenderSystem
  105. */
  106. const String& getName(void) const;
  107. /** See
  108. RenderSystem
  109. */
  110. ConfigOptionMap& getConfigOptions(void);
  111. /** See
  112. RenderSystem
  113. */
  114. void setConfigOption(const String &name, const String &value);
  115. /** See
  116. RenderSystem
  117. */
  118. String validateConfigOptions(void);
  119. /** See
  120. RenderSystem
  121. */
  122. RenderWindow* _initialise(bool autoCreateWindow, const String& windowTitle = "OGRE Render Window");
  123. /** See
  124. RenderSystem
  125. */
  126. virtual RenderSystemCapabilities* createRenderSystemCapabilities() const;
  127. /** See
  128. RenderSystem
  129. */
  130. void initialiseFromRenderSystemCapabilities(RenderSystemCapabilities* caps, RenderTarget* primary);
  131. /** See
  132. RenderSystem
  133. */
  134. void reinitialise(void); // Used if settings changed mid-rendering
  135. /** See
  136. RenderSystem
  137. */
  138. void shutdown(void);
  139. /** See
  140. RenderSystem
  141. */
  142. void setAmbientLight(float r, float g, float b);
  143. /** See
  144. RenderSystem
  145. */
  146. void setShadingType(ShadeOptions so);
  147. /** See
  148. RenderSystem
  149. */
  150. void setLightingEnabled(bool enabled);
  151. /// @copydoc RenderSystem::_createRenderWindow
  152. RenderWindow* _createRenderWindow(const String &name, unsigned int width, unsigned int height,
  153. bool fullScreen, const NameValuePairList *miscParams = 0);
  154. /// @copydoc RenderSystem::_createRenderWindows
  155. bool _createRenderWindows(const RenderWindowDescriptionList& renderWindowDescriptions,
  156. RenderWindowList& createdWindows);
  157. /// @copydoc RenderSystem::createMultiRenderTarget
  158. virtual MultiRenderTarget * createMultiRenderTarget(const String & name);
  159. /** See
  160. RenderSystem
  161. */
  162. void destroyRenderWindow(RenderWindow* pWin);
  163. /** See
  164. RenderSystem
  165. */
  166. String getErrorDescription(long errorNumber) const;
  167. /** See
  168. RenderSystem
  169. */
  170. VertexElementType getColourVertexElementType(void) const;
  171. /** See
  172. RenderSystem
  173. */
  174. void setNormaliseNormals(bool normalise);
  175. // -----------------------------
  176. // Low-level overridden members
  177. // -----------------------------
  178. /** See
  179. RenderSystem
  180. */
  181. bool areFixedFunctionLightsInViewSpace() const { return true; }
  182. /** See
  183. RenderSystem
  184. */
  185. void _setWorldMatrix(const Matrix4 &m);
  186. /** See
  187. RenderSystem
  188. */
  189. void _setViewMatrix(const Matrix4 &m);
  190. /** See
  191. RenderSystem
  192. */
  193. void _setProjectionMatrix(const Matrix4 &m);
  194. /** See
  195. RenderSystem
  196. */
  197. void _setSurfaceParams(const Color &ambient,
  198. const Color &diffuse, const Color &specular,
  199. const Color &emissive, float shininess,
  200. TrackVertexColourType tracking);
  201. /** See
  202. RenderSystem
  203. */
  204. void _setPointParameters(float size, bool attenuationEnabled,
  205. float constant, float linear, float quadratic, float minSize, float maxSize);
  206. /** See
  207. RenderSystem
  208. */
  209. void _setPointSpritesEnabled(bool enabled);
  210. /** See
  211. RenderSystem
  212. */
  213. void _setTexture(size_t unit, bool enabled, const TexturePtr &tex);
  214. /** See
  215. RenderSystem
  216. */
  217. void _setTextureCoordSet(size_t stage, size_t index);
  218. /** See
  219. RenderSystem
  220. */
  221. void _setTextureCoordCalculation(size_t stage, TexCoordCalcMethod m,
  222. const Frustum* frustum = 0);
  223. /** See
  224. RenderSystem
  225. */
  226. void _setTextureAddressingMode(size_t stage, const TextureState::UVWAddressingMode& uvw);
  227. /** See
  228. RenderSystem
  229. */
  230. void _setTextureBorderColour(size_t stage, const Color& colour);
  231. /** See
  232. RenderSystem
  233. */
  234. void _setTextureMipmapBias(size_t unit, float bias);
  235. /** See
  236. RenderSystem
  237. */
  238. void _setTextureMatrix(size_t stage, const Matrix4& xform);
  239. /** See
  240. RenderSystem
  241. */
  242. void _setSceneBlending(SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendOperation op );
  243. /** See
  244. RenderSystem
  245. */
  246. void _setSeparateSceneBlending(SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendFactor sourceFactorAlpha, SceneBlendFactor destFactorAlpha, SceneBlendOperation op, SceneBlendOperation alphaOp );
  247. /** See
  248. RenderSystem
  249. */
  250. void _setSceneBlendingOperation(SceneBlendOperation op);
  251. /** See
  252. RenderSystem
  253. */
  254. void _setSeparateSceneBlendingOperation(SceneBlendOperation op, SceneBlendOperation alphaOp);
  255. /** See
  256. RenderSystem
  257. */
  258. void _setAlphaRejectSettings(CompareFunction func, unsigned char value, bool alphaToCoverage);
  259. /** See
  260. RenderSystem
  261. */
  262. void _setViewport(Viewport *vp);
  263. /** See
  264. RenderSystem
  265. */
  266. void _beginFrame(void);
  267. /** See
  268. RenderSystem
  269. */
  270. void _endFrame(void);
  271. /** See
  272. RenderSystem
  273. */
  274. void _setCullingMode(CullingMode mode);
  275. /** See
  276. RenderSystem
  277. */
  278. void _setDepthBufferParams(bool depthTest = true, bool depthWrite = true, CompareFunction depthFunction = CMPF_LESS_EQUAL);
  279. /** See
  280. RenderSystem
  281. */
  282. void _setDepthBufferCheckEnabled(bool enabled = true);
  283. /** See
  284. RenderSystem
  285. */
  286. void _setDepthBufferWriteEnabled(bool enabled = true);
  287. /** See
  288. RenderSystem
  289. */
  290. void _setDepthBufferFunction(CompareFunction func = CMPF_LESS_EQUAL);
  291. /** See
  292. RenderSystem
  293. */
  294. void _setDepthBias(float constantBias, float slopeScaleBias);
  295. /** See
  296. RenderSystem
  297. */
  298. void _setColourBufferWriteEnabled(bool red, bool green, bool blue, bool alpha);
  299. /** See
  300. RenderSystem
  301. */
  302. void _setFog(FogMode mode, const Color& colour, float density, float start, float end);
  303. /** See
  304. RenderSystem
  305. */
  306. void _convertProjectionMatrix(const Matrix4& matrix,
  307. Matrix4& dest, bool forGpuProgram = false);
  308. /** See
  309. RenderSystem
  310. */
  311. void _makeProjectionMatrix(const Radian& fovy, float aspect, float nearPlane, float farPlane,
  312. Matrix4& dest, bool forGpuProgram = false);
  313. /** See
  314. RenderSystem
  315. */
  316. void _makeProjectionMatrix(float left, float right, float bottom, float top,
  317. float nearPlane, float farPlane, Matrix4& dest, bool forGpuProgram = false);
  318. /** See
  319. RenderSystem
  320. */
  321. void _makeOrthoMatrix(const Radian& fovy, float aspect, float nearPlane, float farPlane,
  322. Matrix4& dest, bool forGpuProgram = false);
  323. /** See
  324. RenderSystem
  325. */
  326. void _applyObliqueDepthProjection(Matrix4& matrix, const Plane& plane,
  327. bool forGpuProgram);
  328. /** See
  329. RenderSystem
  330. */
  331. void setClipPlane (UINT16 index, float A, float B, float C, float D);
  332. /** See
  333. RenderSystem
  334. */
  335. void enableClipPlane (UINT16 index, bool enable);
  336. /** See
  337. RenderSystem
  338. */
  339. void _setPolygonMode(PolygonMode level);
  340. /** See
  341. RenderSystem
  342. */
  343. void setStencilCheckEnabled(bool enabled);
  344. /** See
  345. RenderSystem.
  346. */
  347. void setStencilBufferParams(CompareFunction func = CMPF_ALWAYS_PASS,
  348. UINT32 refValue = 0, UINT32 mask = 0xFFFFFFFF,
  349. StencilOperation stencilFailOp = SOP_KEEP,
  350. StencilOperation depthFailOp = SOP_KEEP,
  351. StencilOperation passOp = SOP_KEEP,
  352. bool twoSidedOperation = false);
  353. /** See
  354. RenderSystem
  355. */
  356. void _setTextureUnitFiltering(size_t unit, FilterType ftype, FilterOptions filter);
  357. /** See
  358. RenderSystem
  359. */
  360. void _setTextureLayerAnisotropy(size_t unit, unsigned int maxAnisotropy);
  361. /** See
  362. RenderSystem
  363. */
  364. void setVertexDeclaration(VertexDeclaration* decl);
  365. /** See
  366. RenderSystem
  367. */
  368. void setVertexBufferBinding(VertexBufferBinding* binding);
  369. /** See
  370. RenderSystem
  371. */
  372. void _render(const RenderOperation& op);
  373. /** See
  374. RenderSystem
  375. */
  376. void bindGpuProgram(GpuProgram* prg);
  377. /** See
  378. RenderSystem
  379. */
  380. void unbindGpuProgram(GpuProgramType gptype);
  381. /** See
  382. RenderSystem
  383. */
  384. void bindGpuProgramParameters(GpuProgramType gptype,
  385. GpuProgramParametersSharedPtr params, UINT16 variabilityMask);
  386. /** See
  387. RenderSystem
  388. */
  389. void bindGpuProgramPassIterationParameters(GpuProgramType gptype);
  390. /** See
  391. RenderSystem
  392. */
  393. void setScissorTest(bool enabled, size_t left = 0, size_t top = 0, size_t right = 800, size_t bottom = 600) ;
  394. void clearFrameBuffer(unsigned int buffers,
  395. const Color& colour = Color::Black,
  396. float depth = 1.0f, unsigned short stencil = 0);
  397. HardwareOcclusionQuery* createHardwareOcclusionQuery(void);
  398. float getHorizontalTexelOffset(void);
  399. float getVerticalTexelOffset(void);
  400. float getMinimumDepthInputValue(void);
  401. float getMaximumDepthInputValue(void);
  402. CM_MUTEX(mThreadInitMutex)
  403. void registerThread();
  404. void unregisterThread();
  405. void preExtraThreadsStarted();
  406. void postExtraThreadsStarted();
  407. // ----------------------------------
  408. // GLRenderSystem specific members
  409. // ----------------------------------
  410. /** One time initialization for the RenderState of a context. Things that
  411. only need to be set once, like the LightingModel can be defined here.
  412. */
  413. void _oneTimeContextInitialization();
  414. /** Switch GL context, dealing with involved internal cached states too
  415. */
  416. void _switchContext(GLContext *context);
  417. /**
  418. * Set current render target to target, enabling its GL context if needed
  419. */
  420. void _setRenderTarget(RenderTarget *target);
  421. /** Unregister a render target->context mapping. If the context of target
  422. is the current context, change the context to the main context so it
  423. can be destroyed safely.
  424. @note This is automatically called by the destructor of
  425. GLContext.
  426. */
  427. void _unregisterContext(GLContext *context);
  428. /** Returns the main context */
  429. GLContext* _getMainContext() {return mMainContext;}
  430. /// @copydoc RenderSystem::getDisplayMonitorCount
  431. unsigned int getDisplayMonitorCount() const;
  432. };
  433. }
  434. #endif