CmGLRenderSystem.h 16 KB

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