OgreGLRenderSystem.cpp 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466
  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.s
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "OgreGLRenderSystem.h"
  25. #include "OgreRenderSystem.h"
  26. #include "OgreStringConverter.h"
  27. #include "OgreCamera.h"
  28. #include "OgreGLTextureManager.h"
  29. #include "OgreGLHardwareVertexBuffer.h"
  30. #include "OgreGLHardwareIndexBuffer.h"
  31. #include "OgreGLDefaultHardwareBufferManager.h"
  32. #include "OgreGLUtil.h"
  33. #include "OgreGLGpuProgram.h"
  34. #include "OgreGLGpuNvparseProgram.h"
  35. #include "ATI_FS_GLGpuProgram.h"
  36. #include "OgreGLGpuProgramManager.h"
  37. #include "OgreException.h"
  38. #include "OgreGLSLExtSupport.h"
  39. #include "OgreGLHardwareOcclusionQuery.h"
  40. #include "OgreGLContext.h"
  41. #include "OgreGLFBORenderTexture.h"
  42. #include "OgreGLPBRenderTexture.h"
  43. #include "OgreConfig.h"
  44. // Convenience macro from ARB_vertex_buffer_object spec
  45. #define VBO_BUFFER_OFFSET(i) ((char *)NULL + (i))
  46. #if CM_THREAD_SUPPORT != 1
  47. GLenum glewContextInit (Ogre::GLSupport *glSupport);
  48. #endif
  49. namespace Ogre {
  50. // Callback function used when registering GLGpuPrograms
  51. GpuProgram* createGLArbGpuProgram(GpuProgramType gptype, const String& syntaxCode)
  52. {
  53. GLArbGpuProgram* ret = new GLArbGpuProgram();
  54. ret->setType(gptype);
  55. ret->setSyntaxCode(syntaxCode);
  56. return ret;
  57. }
  58. GpuProgram* createGLGpuNvparseProgram(GpuProgramType gptype, const String& syntaxCode)
  59. {
  60. GLGpuNvparseProgram* ret = new GLGpuNvparseProgram();
  61. ret->setType(gptype);
  62. ret->setSyntaxCode(syntaxCode);
  63. return ret;
  64. }
  65. GpuProgram* createGL_ATI_FS_GpuProgram(GpuProgramType gptype, const String& syntaxCode)
  66. {
  67. ATI_FS_GLGpuProgram* ret = new ATI_FS_GLGpuProgram();
  68. ret->setType(gptype);
  69. ret->setSyntaxCode(syntaxCode);
  70. return ret;
  71. }
  72. GLRenderSystem::GLRenderSystem()
  73. : mDepthWrite(true), mStencilMask(0xFFFFFFFF), mHardwareBufferManager(0),
  74. mGpuProgramManager(0),
  75. mGLSLProgramFactory(0),
  76. mRTTManager(0),
  77. mActiveTextureUnit(0)
  78. {
  79. size_t i;
  80. // Get our GLSupport
  81. mGLSupport = getGLSupport();
  82. for( i=0; i<MAX_LIGHTS; i++ )
  83. mLights[i] = NULL;
  84. mWorldMatrix = Matrix4::IDENTITY;
  85. mViewMatrix = Matrix4::IDENTITY;
  86. initConfigOptions();
  87. mColourWrite[0] = mColourWrite[1] = mColourWrite[2] = mColourWrite[3] = true;
  88. for (i = 0; i < OGRE_MAX_TEXTURE_LAYERS; i++)
  89. {
  90. // Dummy value
  91. mTextureCoordIndex[i] = 99;
  92. mTextureTypes[i] = 0;
  93. }
  94. mActiveRenderTarget = 0;
  95. mCurrentContext = 0;
  96. mMainContext = 0;
  97. mGLInitialised = false;
  98. mCurrentLights = 0;
  99. mMinFilter = FO_LINEAR;
  100. mMipFilter = FO_POINT;
  101. mCurrentVertexProgram = 0;
  102. mCurrentGeometryProgram = 0;
  103. mCurrentFragmentProgram = 0;
  104. }
  105. GLRenderSystem::~GLRenderSystem()
  106. {
  107. shutdown();
  108. // Destroy render windows
  109. RenderTargetMap::iterator i;
  110. for (i = mRenderTargets.begin(); i != mRenderTargets.end(); ++i)
  111. {
  112. delete i->second;
  113. }
  114. mRenderTargets.clear();
  115. if(mGLSupport)
  116. delete mGLSupport;
  117. }
  118. const String& GLRenderSystem::getName(void) const
  119. {
  120. static String strName("OpenGL Rendering Subsystem");
  121. return strName;
  122. }
  123. void GLRenderSystem::initConfigOptions(void)
  124. {
  125. mGLSupport->addConfig();
  126. }
  127. ConfigOptionMap& GLRenderSystem::getConfigOptions(void)
  128. {
  129. return mGLSupport->getConfigOptions();
  130. }
  131. void GLRenderSystem::setConfigOption(const String &name, const String &value)
  132. {
  133. mGLSupport->setConfigOption(name, value);
  134. }
  135. String GLRenderSystem::validateConfigOptions(void)
  136. {
  137. // XXX Return an error string if something is invalid
  138. return mGLSupport->validateConfig();
  139. }
  140. RenderWindow* GLRenderSystem::_initialise(bool autoCreateWindow, const String& windowTitle)
  141. {
  142. mGLSupport->start();
  143. RenderWindow* autoWindow = mGLSupport->createWindow(autoCreateWindow, this, windowTitle);
  144. RenderSystem::_initialise(autoCreateWindow, windowTitle);
  145. return autoWindow;
  146. }
  147. RenderSystemCapabilities* GLRenderSystem::createRenderSystemCapabilities() const
  148. {
  149. RenderSystemCapabilities* rsc = new RenderSystemCapabilities();
  150. rsc->setCategoryRelevant(CAPS_CATEGORY_GL, true);
  151. rsc->setDriverVersion(mDriverVersion);
  152. const char* deviceName = (const char*)glGetString(GL_RENDERER);
  153. const char* vendorName = (const char*)glGetString(GL_VENDOR);
  154. rsc->setDeviceName(deviceName);
  155. rsc->setRenderSystemName(getName());
  156. // determine vendor
  157. if (strstr(vendorName, "NVIDIA"))
  158. rsc->setVendor(GPU_NVIDIA);
  159. else if (strstr(vendorName, "ATI"))
  160. rsc->setVendor(GPU_ATI);
  161. else if (strstr(vendorName, "Intel"))
  162. rsc->setVendor(GPU_INTEL);
  163. else if (strstr(vendorName, "S3"))
  164. rsc->setVendor(GPU_S3);
  165. else if (strstr(vendorName, "Matrox"))
  166. rsc->setVendor(GPU_MATROX);
  167. else if (strstr(vendorName, "3DLabs"))
  168. rsc->setVendor(GPU_3DLABS);
  169. else if (strstr(vendorName, "SiS"))
  170. rsc->setVendor(GPU_SIS);
  171. else
  172. rsc->setVendor(GPU_UNKNOWN);
  173. // Supports fixed-function
  174. rsc->setCapability(RSC_FIXED_FUNCTION);
  175. // Check for hardware mipmapping support.
  176. if(GLEW_VERSION_1_4 || GLEW_SGIS_generate_mipmap)
  177. {
  178. bool disableAutoMip = false;
  179. #if CM_PLATFORM == CM_PLATFORM_APPLE || CM_PLATFORM == CM_PLATFORM_LINUX
  180. // Apple & Linux ATI drivers have faults in hardware mipmap generation
  181. if (rsc->getVendor() == GPU_ATI)
  182. disableAutoMip = true;
  183. #endif
  184. // The Intel 915G frequently corrupts textures when using hardware mip generation
  185. // I'm not currently sure how many generations of hardware this affects,
  186. // so for now, be safe.
  187. if (rsc->getVendor() == GPU_INTEL)
  188. disableAutoMip = true;
  189. // SiS chipsets also seem to have problems with this
  190. if (rsc->getVendor() == GPU_SIS)
  191. disableAutoMip = true;
  192. if (!disableAutoMip)
  193. rsc->setCapability(RSC_AUTOMIPMAP);
  194. }
  195. // Check for blending support
  196. if(GLEW_VERSION_1_3 ||
  197. GLEW_ARB_texture_env_combine ||
  198. GLEW_EXT_texture_env_combine)
  199. {
  200. rsc->setCapability(RSC_BLENDING);
  201. }
  202. // Check for Multitexturing support and set number of texture units
  203. if(GLEW_VERSION_1_3 ||
  204. GLEW_ARB_multitexture)
  205. {
  206. GLint units;
  207. glGetIntegerv( GL_MAX_TEXTURE_UNITS, &units );
  208. if (GLEW_ARB_fragment_program)
  209. {
  210. // Also check GL_MAX_TEXTURE_IMAGE_UNITS_ARB since NV at least
  211. // only increased this on the FX/6x00 series
  212. GLint arbUnits;
  213. glGetIntegerv( GL_MAX_TEXTURE_IMAGE_UNITS_ARB, &arbUnits );
  214. if (arbUnits > units)
  215. units = arbUnits;
  216. }
  217. rsc->setNumTextureUnits(units);
  218. }
  219. else
  220. {
  221. // If no multitexture support then set one texture unit
  222. rsc->setNumTextureUnits(1);
  223. }
  224. // Check for Anisotropy support
  225. if(GLEW_EXT_texture_filter_anisotropic)
  226. {
  227. rsc->setCapability(RSC_ANISOTROPY);
  228. }
  229. // Check for DOT3 support
  230. if(GLEW_VERSION_1_3 ||
  231. GLEW_ARB_texture_env_dot3 ||
  232. GLEW_EXT_texture_env_dot3)
  233. {
  234. rsc->setCapability(RSC_DOT3);
  235. }
  236. // Check for cube mapping
  237. if(GLEW_VERSION_1_3 ||
  238. GLEW_ARB_texture_cube_map ||
  239. GLEW_EXT_texture_cube_map)
  240. {
  241. rsc->setCapability(RSC_CUBEMAPPING);
  242. }
  243. // Point sprites
  244. if (GLEW_VERSION_2_0 || GLEW_ARB_point_sprite)
  245. {
  246. rsc->setCapability(RSC_POINT_SPRITES);
  247. }
  248. // Check for point parameters
  249. if (GLEW_VERSION_1_4)
  250. {
  251. rsc->setCapability(RSC_POINT_EXTENDED_PARAMETERS);
  252. }
  253. if (GLEW_ARB_point_parameters)
  254. {
  255. rsc->setCapability(RSC_POINT_EXTENDED_PARAMETERS_ARB);
  256. }
  257. if (GLEW_EXT_point_parameters)
  258. {
  259. rsc->setCapability(RSC_POINT_EXTENDED_PARAMETERS_EXT);
  260. }
  261. // Check for hardware stencil support and set bit depth
  262. GLint stencil;
  263. glGetIntegerv(GL_STENCIL_BITS,&stencil);
  264. if(stencil)
  265. {
  266. rsc->setCapability(RSC_HWSTENCIL);
  267. rsc->setStencilBufferBitDepth(stencil);
  268. }
  269. if(GLEW_VERSION_1_5 || GLEW_ARB_vertex_buffer_object)
  270. {
  271. if (!GLEW_ARB_vertex_buffer_object)
  272. {
  273. rsc->setCapability(RSC_GL1_5_NOVBO);
  274. }
  275. rsc->setCapability(RSC_VBO);
  276. }
  277. if(GLEW_ARB_vertex_program)
  278. {
  279. rsc->setCapability(RSC_VERTEX_PROGRAM);
  280. // Vertex Program Properties
  281. rsc->setVertexProgramConstantBoolCount(0);
  282. rsc->setVertexProgramConstantIntCount(0);
  283. GLint floatConstantCount;
  284. glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB, &floatConstantCount);
  285. rsc->setVertexProgramConstantFloatCount(floatConstantCount);
  286. rsc->addShaderProfile("arbvp1");
  287. if (GLEW_NV_vertex_program2_option)
  288. {
  289. rsc->addShaderProfile("vp30");
  290. }
  291. if (GLEW_NV_vertex_program3)
  292. {
  293. rsc->addShaderProfile("vp40");
  294. }
  295. if (GLEW_NV_vertex_program4)
  296. {
  297. rsc->addShaderProfile("gp4vp");
  298. rsc->addShaderProfile("gpu_vp");
  299. }
  300. }
  301. if (GLEW_NV_register_combiners2 &&
  302. GLEW_NV_texture_shader)
  303. {
  304. rsc->setCapability(RSC_FRAGMENT_PROGRAM);
  305. rsc->addShaderProfile("fp20");
  306. }
  307. // NFZ - check for ATI fragment shader support
  308. if (GLEW_ATI_fragment_shader)
  309. {
  310. rsc->setCapability(RSC_FRAGMENT_PROGRAM);
  311. // no boolean params allowed
  312. rsc->setFragmentProgramConstantBoolCount(0);
  313. // no integer params allowed
  314. rsc->setFragmentProgramConstantIntCount(0);
  315. // only 8 Vector4 constant floats supported
  316. rsc->setFragmentProgramConstantFloatCount(8);
  317. rsc->addShaderProfile("ps_1_4");
  318. rsc->addShaderProfile("ps_1_3");
  319. rsc->addShaderProfile("ps_1_2");
  320. rsc->addShaderProfile("ps_1_1");
  321. }
  322. if (GLEW_ARB_fragment_program)
  323. {
  324. rsc->setCapability(RSC_FRAGMENT_PROGRAM);
  325. // Fragment Program Properties
  326. rsc->setFragmentProgramConstantBoolCount(0);
  327. rsc->setFragmentProgramConstantIntCount(0);
  328. GLint floatConstantCount;
  329. glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB, &floatConstantCount);
  330. rsc->setFragmentProgramConstantFloatCount(floatConstantCount);
  331. rsc->addShaderProfile("arbfp1");
  332. if (GLEW_NV_fragment_program_option)
  333. {
  334. rsc->addShaderProfile("fp30");
  335. }
  336. if (GLEW_NV_fragment_program2)
  337. {
  338. rsc->addShaderProfile("fp40");
  339. }
  340. }
  341. // NFZ - Check if GLSL is supported
  342. if ( GLEW_VERSION_2_0 ||
  343. (GLEW_ARB_shading_language_100 &&
  344. GLEW_ARB_shader_objects &&
  345. GLEW_ARB_fragment_shader &&
  346. GLEW_ARB_vertex_shader) )
  347. {
  348. rsc->addShaderProfile("glsl");
  349. }
  350. // Check if geometry shaders are supported
  351. if (GLEW_VERSION_2_0 &&
  352. GLEW_EXT_geometry_shader4)
  353. {
  354. rsc->setCapability(RSC_GEOMETRY_PROGRAM);
  355. rsc->addShaderProfile("nvgp4");
  356. //Also add the CG profiles
  357. rsc->addShaderProfile("gpu_gp");
  358. rsc->addShaderProfile("gp4gp");
  359. rsc->setGeometryProgramConstantBoolCount(0);
  360. rsc->setGeometryProgramConstantIntCount(0);
  361. GLint floatConstantCount = 0;
  362. glGetIntegerv(GL_MAX_GEOMETRY_UNIFORM_COMPONENTS_EXT, &floatConstantCount);
  363. rsc->setGeometryProgramConstantFloatCount(floatConstantCount);
  364. GLint maxOutputVertices;
  365. glGetIntegerv(GL_MAX_GEOMETRY_OUTPUT_VERTICES_EXT,&maxOutputVertices);
  366. rsc->setGeometryProgramNumOutputVertices(maxOutputVertices);
  367. }
  368. //Check if render to vertex buffer (transform feedback in OpenGL)
  369. if (GLEW_VERSION_2_0 &&
  370. GLEW_NV_transform_feedback)
  371. {
  372. rsc->setCapability(RSC_HWRENDER_TO_VERTEX_BUFFER);
  373. }
  374. // Check for texture compression
  375. if(GLEW_VERSION_1_3 || GLEW_ARB_texture_compression)
  376. {
  377. rsc->setCapability(RSC_TEXTURE_COMPRESSION);
  378. // Check for dxt compression
  379. if(GLEW_EXT_texture_compression_s3tc)
  380. {
  381. #if defined(__APPLE__) && defined(__PPC__)
  382. // Apple on ATI & PPC has errors in DXT
  383. if (mGLSupport->getGLVendor().find("ATI") == std::string::npos)
  384. #endif
  385. rsc->setCapability(RSC_TEXTURE_COMPRESSION_DXT);
  386. }
  387. // Check for vtc compression
  388. if(GLEW_NV_texture_compression_vtc)
  389. {
  390. rsc->setCapability(RSC_TEXTURE_COMPRESSION_VTC);
  391. }
  392. }
  393. // Scissor test is standard in GL 1.2 (is it emulated on some cards though?)
  394. rsc->setCapability(RSC_SCISSOR_TEST);
  395. // As are user clipping planes
  396. rsc->setCapability(RSC_USER_CLIP_PLANES);
  397. // 2-sided stencil?
  398. if (GLEW_VERSION_2_0 || GLEW_EXT_stencil_two_side)
  399. {
  400. rsc->setCapability(RSC_TWO_SIDED_STENCIL);
  401. }
  402. // stencil wrapping?
  403. if (GLEW_VERSION_1_4 || GLEW_EXT_stencil_wrap)
  404. {
  405. rsc->setCapability(RSC_STENCIL_WRAP);
  406. }
  407. // Check for hardware occlusion support
  408. if(GLEW_VERSION_1_5 || GLEW_ARB_occlusion_query)
  409. {
  410. // Some buggy driver claim that it is GL 1.5 compliant and
  411. // not support ARB_occlusion_query
  412. if (!GLEW_ARB_occlusion_query)
  413. {
  414. rsc->setCapability(RSC_GL1_5_NOHWOCCLUSION);
  415. }
  416. rsc->setCapability(RSC_HWOCCLUSION);
  417. }
  418. else if (GLEW_NV_occlusion_query)
  419. {
  420. // Support NV extension too for old hardware
  421. rsc->setCapability(RSC_HWOCCLUSION);
  422. }
  423. // UBYTE4 always supported
  424. rsc->setCapability(RSC_VERTEX_FORMAT_UBYTE4);
  425. // Infinite far plane always supported
  426. rsc->setCapability(RSC_INFINITE_FAR_PLANE);
  427. // Check for non-power-of-2 texture support
  428. if(GLEW_ARB_texture_non_power_of_two)
  429. {
  430. rsc->setCapability(RSC_NON_POWER_OF_2_TEXTURES);
  431. }
  432. // Check for Float textures
  433. if(GLEW_ATI_texture_float || GLEW_ARB_texture_float)
  434. {
  435. rsc->setCapability(RSC_TEXTURE_FLOAT);
  436. }
  437. // 3D textures should be supported by GL 1.2, which is our minimum version
  438. rsc->setCapability(RSC_TEXTURE_3D);
  439. // Check for framebuffer object extension
  440. if(GLEW_EXT_framebuffer_object)
  441. {
  442. // Probe number of draw buffers
  443. // Only makes sense with FBO support, so probe here
  444. if(GLEW_VERSION_2_0 ||
  445. GLEW_ARB_draw_buffers ||
  446. GLEW_ATI_draw_buffers)
  447. {
  448. GLint buffers;
  449. glGetIntegerv(GL_MAX_DRAW_BUFFERS_ARB, &buffers);
  450. rsc->setNumMultiRenderTargets(std::min<int>(buffers, (GLint)OGRE_MAX_MULTIPLE_RENDER_TARGETS));
  451. rsc->setCapability(RSC_MRT_DIFFERENT_BIT_DEPTHS);
  452. if(!GLEW_VERSION_2_0)
  453. {
  454. // Before GL version 2.0, we need to get one of the extensions
  455. if(GLEW_ARB_draw_buffers)
  456. rsc->setCapability(RSC_FBO_ARB);
  457. if(GLEW_ATI_draw_buffers)
  458. rsc->setCapability(RSC_FBO_ATI);
  459. }
  460. // Set FBO flag for all 3 'subtypes'
  461. rsc->setCapability(RSC_FBO);
  462. }
  463. rsc->setCapability(RSC_HWRENDER_TO_TEXTURE);
  464. }
  465. // Check GLSupport for PBuffer support
  466. if(mGLSupport->supportsPBuffers())
  467. {
  468. // Use PBuffers
  469. rsc->setCapability(RSC_HWRENDER_TO_TEXTURE);
  470. rsc->setCapability(RSC_PBUFFER);
  471. }
  472. // Point size
  473. if (GLEW_VERSION_1_4)
  474. {
  475. float ps;
  476. glGetFloatv(GL_POINT_SIZE_MAX, &ps);
  477. rsc->setMaxPointSize(ps);
  478. }
  479. else
  480. {
  481. GLint vSize[2];
  482. glGetIntegerv(GL_POINT_SIZE_RANGE,vSize);
  483. rsc->setMaxPointSize((Ogre::Real)vSize[1]);
  484. }
  485. // Vertex texture fetching
  486. if (mGLSupport->checkExtension("GL_ARB_vertex_shader"))
  487. {
  488. GLint vUnits;
  489. glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS_ARB, &vUnits);
  490. rsc->setNumVertexTextureUnits(static_cast<ushort>(vUnits));
  491. if (vUnits > 0)
  492. {
  493. rsc->setCapability(RSC_VERTEX_TEXTURE_FETCH);
  494. }
  495. // GL always shares vertex and fragment texture units (for now?)
  496. rsc->setVertexTextureUnitsShared(true);
  497. }
  498. // Mipmap LOD biasing?
  499. if (GLEW_VERSION_1_4 || GLEW_EXT_texture_lod_bias)
  500. {
  501. rsc->setCapability(RSC_MIPMAP_LOD_BIAS);
  502. }
  503. // Alpha to coverage?
  504. if (mGLSupport->checkExtension("GL_ARB_multisample"))
  505. {
  506. // Alpha to coverage always 'supported' when MSAA is available
  507. // although card may ignore it if it doesn't specifically support A2C
  508. rsc->setCapability(RSC_ALPHA_TO_COVERAGE);
  509. }
  510. // Advanced blending operations
  511. if(GLEW_VERSION_2_0)
  512. {
  513. rsc->setCapability(RSC_ADVANCED_BLEND_OPERATIONS);
  514. }
  515. return rsc;
  516. }
  517. void GLRenderSystem::initialiseFromRenderSystemCapabilities(RenderSystemCapabilities* caps, RenderTarget* primary)
  518. {
  519. if(caps->getRenderSystemName() != getName())
  520. {
  521. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
  522. "Trying to initialize GLRenderSystem from RenderSystemCapabilities that do not support OpenGL",
  523. "GLRenderSystem::initialiseFromRenderSystemCapabilities");
  524. }
  525. // set texture the number of texture units
  526. mFixedFunctionTextureUnits = caps->getNumTextureUnits();
  527. //In GL there can be less fixed function texture units than general
  528. //texture units. Get the minimum of the two.
  529. if (caps->hasCapability(RSC_FRAGMENT_PROGRAM))
  530. {
  531. GLint maxTexCoords = 0;
  532. glGetIntegerv(GL_MAX_TEXTURE_COORDS_ARB, &maxTexCoords);
  533. if (mFixedFunctionTextureUnits > maxTexCoords)
  534. {
  535. mFixedFunctionTextureUnits = maxTexCoords;
  536. }
  537. }
  538. if(caps->hasCapability(RSC_GL1_5_NOVBO))
  539. {
  540. // Assign ARB functions same to GL 1.5 version since
  541. // interface identical
  542. glBindBufferARB = glBindBuffer;
  543. glBufferDataARB = glBufferData;
  544. glBufferSubDataARB = glBufferSubData;
  545. glDeleteBuffersARB = glDeleteBuffers;
  546. glGenBuffersARB = glGenBuffers;
  547. glGetBufferParameterivARB = glGetBufferParameteriv;
  548. glGetBufferPointervARB = glGetBufferPointerv;
  549. glGetBufferSubDataARB = glGetBufferSubData;
  550. glIsBufferARB = glIsBuffer;
  551. glMapBufferARB = glMapBuffer;
  552. glUnmapBufferARB = glUnmapBuffer;
  553. }
  554. if(caps->hasCapability(RSC_VBO))
  555. {
  556. mHardwareBufferManager = new GLHardwareBufferManager;
  557. }
  558. else
  559. {
  560. mHardwareBufferManager = new GLDefaultHardwareBufferManager;
  561. }
  562. // XXX Need to check for nv2 support and make a program manager for it
  563. // XXX Probably nv1 as well for older cards
  564. // GPU Program Manager setup
  565. mGpuProgramManager = new GLGpuProgramManager();
  566. if(caps->hasCapability(RSC_VERTEX_PROGRAM))
  567. {
  568. if(caps->isShaderProfileSupported("arbvp1"))
  569. {
  570. mGpuProgramManager->registerProgramFactory("arbvp1", createGLArbGpuProgram);
  571. }
  572. if(caps->isShaderProfileSupported("vp30"))
  573. {
  574. mGpuProgramManager->registerProgramFactory("vp30", createGLArbGpuProgram);
  575. }
  576. if(caps->isShaderProfileSupported("vp40"))
  577. {
  578. mGpuProgramManager->registerProgramFactory("vp40", createGLArbGpuProgram);
  579. }
  580. if(caps->isShaderProfileSupported("gp4vp"))
  581. {
  582. mGpuProgramManager->registerProgramFactory("gp4vp", createGLArbGpuProgram);
  583. }
  584. if(caps->isShaderProfileSupported("gpu_vp"))
  585. {
  586. mGpuProgramManager->registerProgramFactory("gpu_vp", createGLArbGpuProgram);
  587. }
  588. }
  589. if(caps->hasCapability(RSC_GEOMETRY_PROGRAM))
  590. {
  591. //TODO : Should these be createGLArbGpuProgram or createGLGpuNVparseProgram?
  592. if(caps->isShaderProfileSupported("nvgp4"))
  593. {
  594. mGpuProgramManager->registerProgramFactory("nvgp4", createGLArbGpuProgram);
  595. }
  596. if(caps->isShaderProfileSupported("gp4gp"))
  597. {
  598. mGpuProgramManager->registerProgramFactory("gp4gp", createGLArbGpuProgram);
  599. }
  600. if(caps->isShaderProfileSupported("gpu_gp"))
  601. {
  602. mGpuProgramManager->registerProgramFactory("gpu_gp", createGLArbGpuProgram);
  603. }
  604. }
  605. if(caps->hasCapability(RSC_FRAGMENT_PROGRAM))
  606. {
  607. if(caps->isShaderProfileSupported("fp20"))
  608. {
  609. mGpuProgramManager->registerProgramFactory("fp20", createGLGpuNvparseProgram);
  610. }
  611. if(caps->isShaderProfileSupported("ps_1_4"))
  612. {
  613. mGpuProgramManager->registerProgramFactory("ps_1_4", createGL_ATI_FS_GpuProgram);
  614. }
  615. if(caps->isShaderProfileSupported("ps_1_3"))
  616. {
  617. mGpuProgramManager->registerProgramFactory("ps_1_3", createGL_ATI_FS_GpuProgram);
  618. }
  619. if(caps->isShaderProfileSupported("ps_1_2"))
  620. {
  621. mGpuProgramManager->registerProgramFactory("ps_1_2", createGL_ATI_FS_GpuProgram);
  622. }
  623. if(caps->isShaderProfileSupported("ps_1_1"))
  624. {
  625. mGpuProgramManager->registerProgramFactory("ps_1_1", createGL_ATI_FS_GpuProgram);
  626. }
  627. if(caps->isShaderProfileSupported("arbfp1"))
  628. {
  629. mGpuProgramManager->registerProgramFactory("arbfp1", createGLArbGpuProgram);
  630. }
  631. if(caps->isShaderProfileSupported("fp40"))
  632. {
  633. mGpuProgramManager->registerProgramFactory("fp40", createGLArbGpuProgram);
  634. }
  635. if(caps->isShaderProfileSupported("fp30"))
  636. {
  637. mGpuProgramManager->registerProgramFactory("fp30", createGLArbGpuProgram);
  638. }
  639. }
  640. if(caps->isShaderProfileSupported("glsl"))
  641. {
  642. // NFZ - check for GLSL vertex and fragment shader support successful
  643. mGLSLProgramFactory = new GLSLProgramFactory();
  644. HighLevelGpuProgramManager::getSingleton().addFactory(mGLSLProgramFactory);
  645. }
  646. if(caps->hasCapability(RSC_HWOCCLUSION))
  647. {
  648. if(caps->hasCapability(RSC_GL1_5_NOHWOCCLUSION))
  649. {
  650. // Assign ARB functions same to GL 1.5 version since
  651. // interface identical
  652. glBeginQueryARB = glBeginQuery;
  653. glDeleteQueriesARB = glDeleteQueries;
  654. glEndQueryARB = glEndQuery;
  655. glGenQueriesARB = glGenQueries;
  656. glGetQueryObjectivARB = glGetQueryObjectiv;
  657. glGetQueryObjectuivARB = glGetQueryObjectuiv;
  658. glGetQueryivARB = glGetQueryiv;
  659. glIsQueryARB = glIsQuery;
  660. }
  661. }
  662. /// Do this after extension function pointers are initialised as the extension
  663. /// is used to probe further capabilities.
  664. ConfigOptionMap::iterator cfi = getConfigOptions().find("RTT Preferred Mode");
  665. // RTT Mode: 0 use whatever available, 1 use PBuffers, 2 force use copying
  666. int rttMode = 0;
  667. if (cfi != getConfigOptions().end())
  668. {
  669. if (cfi->second.currentValue == "PBuffer")
  670. {
  671. rttMode = 1;
  672. }
  673. else if (cfi->second.currentValue == "Copy")
  674. {
  675. rttMode = 2;
  676. }
  677. }
  678. // Check for framebuffer object extension
  679. if(caps->hasCapability(RSC_FBO) && rttMode < 1)
  680. {
  681. // Before GL version 2.0, we need to get one of the extensions
  682. if(caps->hasCapability(RSC_FBO_ARB))
  683. GLEW_GET_FUN(__glewDrawBuffers) = glDrawBuffersARB;
  684. else if(caps->hasCapability(RSC_FBO_ATI))
  685. GLEW_GET_FUN(__glewDrawBuffers) = glDrawBuffersATI;
  686. if(caps->hasCapability(RSC_HWRENDER_TO_TEXTURE))
  687. {
  688. // Create FBO manager
  689. // TODO LOG PORT - Log this somewhere?
  690. //LogManager::getSingleton().logMessage("GL: Using GL_EXT_framebuffer_object for rendering to textures (best)");
  691. mRTTManager = new GLFBOManager(false);
  692. }
  693. }
  694. else
  695. {
  696. // Check GLSupport for PBuffer support
  697. if(caps->hasCapability(RSC_PBUFFER) && rttMode < 2)
  698. {
  699. if(caps->hasCapability(RSC_HWRENDER_TO_TEXTURE))
  700. {
  701. // Use PBuffers
  702. mRTTManager = new GLPBRTTManager(mGLSupport, primary);
  703. // TODO LOG PORT - Log this somewhere?
  704. //LogManager::getSingleton().logMessage("GL: Using PBuffers for rendering to textures");
  705. }
  706. }
  707. else
  708. {
  709. // No pbuffer support either -- fallback to simplest copying from framebuffer
  710. mRTTManager = new GLCopyingRTTManager();
  711. // TODO LOG PORT - Log this somewhere?
  712. //LogManager::getSingleton().logMessage("GL: Using framebuffer copy for rendering to textures (worst)");
  713. //LogManager::getSingleton().logMessage("GL: Warning: RenderTexture size is restricted to size of framebuffer. If you are on Linux, consider using GLX instead of SDL.");
  714. }
  715. // Downgrade number of simultaneous targets
  716. caps->setNumMultiRenderTargets(1);
  717. }
  718. /// Create the texture manager
  719. mTextureManager = new GLTextureManager(*mGLSupport);
  720. mGLInitialised = true;
  721. }
  722. void GLRenderSystem::reinitialise(void)
  723. {
  724. this->shutdown();
  725. this->_initialise(true);
  726. }
  727. void GLRenderSystem::shutdown(void)
  728. {
  729. RenderSystem::shutdown();
  730. // Deleting the GLSL program factory
  731. if (mGLSLProgramFactory)
  732. {
  733. // Remove from manager safely
  734. if (HighLevelGpuProgramManager::getSingletonPtr())
  735. HighLevelGpuProgramManager::getSingleton().removeFactory(mGLSLProgramFactory);
  736. delete mGLSLProgramFactory;
  737. mGLSLProgramFactory = 0;
  738. }
  739. // Deleting the GPU program manager and hardware buffer manager. Has to be done before the mGLSupport->stop().
  740. delete mGpuProgramManager;
  741. mGpuProgramManager = 0;
  742. delete mHardwareBufferManager;
  743. mHardwareBufferManager = 0;
  744. delete mRTTManager;
  745. mRTTManager = 0;
  746. // Delete extra threads contexts
  747. for (GLContextList::iterator i = mBackgroundContextList.begin();
  748. i != mBackgroundContextList.end(); ++i)
  749. {
  750. GLContext* pCurContext = *i;
  751. pCurContext->releaseContext();
  752. delete pCurContext;
  753. }
  754. mBackgroundContextList.clear();
  755. mGLSupport->stop();
  756. mStopRendering = true;
  757. delete mTextureManager;
  758. mTextureManager = 0;
  759. // There will be a new initial window and so forth, thus any call to test
  760. // some params will access an invalid pointer, so it is best to reset
  761. // the whole state.
  762. mGLInitialised = 0;
  763. }
  764. void GLRenderSystem::setAmbientLight(float r, float g, float b)
  765. {
  766. GLfloat lmodel_ambient[] = {r, g, b, 1.0};
  767. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  768. }
  769. void GLRenderSystem::setShadingType(ShadeOptions so)
  770. {
  771. switch(so)
  772. {
  773. case SO_FLAT:
  774. glShadeModel(GL_FLAT);
  775. break;
  776. default:
  777. glShadeModel(GL_SMOOTH);
  778. break;
  779. }
  780. }
  781. //---------------------------------------------------------------------
  782. bool GLRenderSystem::_createRenderWindows(const RenderWindowDescriptionList& renderWindowDescriptions,
  783. RenderWindowList& createdWindows)
  784. {
  785. // Call base render system method.
  786. if (false == RenderSystem::_createRenderWindows(renderWindowDescriptions, createdWindows))
  787. return false;
  788. // Simply call _createRenderWindow in a loop.
  789. for (size_t i = 0; i < renderWindowDescriptions.size(); ++i)
  790. {
  791. const RenderWindowDescription& curRenderWindowDescription = renderWindowDescriptions[i];
  792. RenderWindow* curWindow = NULL;
  793. curWindow = _createRenderWindow(curRenderWindowDescription.name,
  794. curRenderWindowDescription.width,
  795. curRenderWindowDescription.height,
  796. curRenderWindowDescription.useFullScreen,
  797. &curRenderWindowDescription.miscParams);
  798. createdWindows.push_back(curWindow);
  799. }
  800. return true;
  801. }
  802. //---------------------------------------------------------------------
  803. RenderWindow* GLRenderSystem::_createRenderWindow(const String &name,
  804. unsigned int width, unsigned int height, bool fullScreen,
  805. const NameValuePairList *miscParams)
  806. {
  807. if (mRenderTargets.find(name) != mRenderTargets.end())
  808. {
  809. OGRE_EXCEPT(
  810. Exception::ERR_INVALIDPARAMS,
  811. "Window with name '" + name + "' already exists",
  812. "GLRenderSystem::_createRenderWindow" );
  813. }
  814. // Create the window
  815. RenderWindow* win = mGLSupport->newWindow(name, width, height,
  816. fullScreen, miscParams);
  817. attachRenderTarget( *win );
  818. if (!mGLInitialised)
  819. {
  820. // set up glew and GLSupport
  821. initialiseContext(win);
  822. std::vector<Ogre::String> tokens = StringUtil::split(mGLSupport->getGLVersion(), ".");
  823. if (!tokens.empty())
  824. {
  825. mDriverVersion.major = StringConverter::parseInt(tokens[0]);
  826. if (tokens.size() > 1)
  827. mDriverVersion.minor = StringConverter::parseInt(tokens[1]);
  828. if (tokens.size() > 2)
  829. mDriverVersion.release = StringConverter::parseInt(tokens[2]);
  830. }
  831. mDriverVersion.build = 0;
  832. // Initialise GL after the first window has been created
  833. // TODO: fire this from emulation options, and don't duplicate Real and Current capabilities
  834. mRealCapabilities = createRenderSystemCapabilities();
  835. // use real capabilities if custom capabilities are not available
  836. if(!mUseCustomCapabilities)
  837. mCurrentCapabilities = mRealCapabilities;
  838. initialiseFromRenderSystemCapabilities(mCurrentCapabilities, win);
  839. // Initialise the main context
  840. _oneTimeContextInitialization();
  841. if(mCurrentContext)
  842. mCurrentContext->setInitialized();
  843. }
  844. return win;
  845. }
  846. void GLRenderSystem::initialiseContext(RenderWindow* primary)
  847. {
  848. // Set main and current context
  849. mMainContext = 0;
  850. primary->getCustomAttribute("GLCONTEXT", &mMainContext);
  851. mCurrentContext = mMainContext;
  852. // Set primary context as active
  853. if(mCurrentContext)
  854. mCurrentContext->setCurrent();
  855. // Setup GLSupport
  856. mGLSupport->initialiseExtensions();
  857. // Get extension function pointers
  858. #if CM_THREAD_SUPPORT != 1
  859. glewContextInit(mGLSupport);
  860. #endif
  861. }
  862. //-----------------------------------------------------------------------
  863. MultiRenderTarget * GLRenderSystem::createMultiRenderTarget(const String & name)
  864. {
  865. MultiRenderTarget *retval = mRTTManager->createMultiRenderTarget(name);
  866. attachRenderTarget( *retval );
  867. return retval;
  868. }
  869. //-----------------------------------------------------------------------
  870. void GLRenderSystem::destroyRenderWindow(RenderWindow* pWin)
  871. {
  872. // Find it to remove from list
  873. RenderTargetMap::iterator i = mRenderTargets.begin();
  874. while (i != mRenderTargets.end())
  875. {
  876. if (i->second == pWin)
  877. {
  878. mRenderTargets.erase(i);
  879. delete pWin;
  880. break;
  881. }
  882. }
  883. }
  884. //---------------------------------------------------------------------
  885. void GLRenderSystem::_useLights(const LightList& lights, unsigned short limit)
  886. {
  887. // TODO PORT - I don't use fixed pipleline lights. Remove this
  888. assert(false);
  889. //// Save previous modelview
  890. //glMatrixMode(GL_MODELVIEW);
  891. //glPushMatrix();
  892. //// just load view matrix (identity world)
  893. //GLfloat mat[16];
  894. //makeGLMatrix(mat, mViewMatrix);
  895. //glLoadMatrixf(mat);
  896. //LightList::const_iterator i, iend;
  897. //iend = lights.end();
  898. //unsigned short num = 0;
  899. //for (i = lights.begin(); i != iend && num < limit; ++i, ++num)
  900. //{
  901. // setGLLight(num, *i);
  902. // mLights[num] = *i;
  903. //}
  904. //// Disable extra lights
  905. //for (; num < mCurrentLights; ++num)
  906. //{
  907. // setGLLight(num, NULL);
  908. // mLights[num] = NULL;
  909. //}
  910. //mCurrentLights = std::min(limit, static_cast<unsigned short>(lights.size()));
  911. //setLights();
  912. //// restore previous
  913. //glPopMatrix();
  914. }
  915. //-----------------------------------------------------------------------------
  916. void GLRenderSystem::makeGLMatrix(GLfloat gl_matrix[16], const Matrix4& m)
  917. {
  918. size_t x = 0;
  919. for (size_t i = 0; i < 4; i++)
  920. {
  921. for (size_t j = 0; j < 4; j++)
  922. {
  923. gl_matrix[x] = m[j][i];
  924. x++;
  925. }
  926. }
  927. }
  928. //-----------------------------------------------------------------------------
  929. void GLRenderSystem::_setWorldMatrix( const Matrix4 &m )
  930. {
  931. GLfloat mat[16];
  932. mWorldMatrix = m;
  933. makeGLMatrix( mat, mViewMatrix * mWorldMatrix );
  934. glMatrixMode(GL_MODELVIEW);
  935. glLoadMatrixf(mat);
  936. }
  937. //-----------------------------------------------------------------------------
  938. void GLRenderSystem::_setViewMatrix( const Matrix4 &m )
  939. {
  940. mViewMatrix = m;
  941. GLfloat mat[16];
  942. makeGLMatrix( mat, mViewMatrix * mWorldMatrix );
  943. glMatrixMode(GL_MODELVIEW);
  944. glLoadMatrixf(mat);
  945. // also mark clip planes dirty
  946. if (!mClipPlanes.empty())
  947. mClipPlanesDirty = true;
  948. }
  949. //-----------------------------------------------------------------------------
  950. void GLRenderSystem::_setProjectionMatrix(const Matrix4 &m)
  951. {
  952. GLfloat mat[16];
  953. makeGLMatrix(mat, m);
  954. if (mActiveRenderTarget->requiresTextureFlipping())
  955. {
  956. // Invert transformed y
  957. mat[1] = -mat[1];
  958. mat[5] = -mat[5];
  959. mat[9] = -mat[9];
  960. mat[13] = -mat[13];
  961. }
  962. glMatrixMode(GL_PROJECTION);
  963. glLoadMatrixf(mat);
  964. glMatrixMode(GL_MODELVIEW);
  965. // also mark clip planes dirty
  966. if (!mClipPlanes.empty())
  967. mClipPlanesDirty = true;
  968. }
  969. //-----------------------------------------------------------------------------
  970. void GLRenderSystem::_setSurfaceParams(const ColourValue &ambient,
  971. const ColourValue &diffuse, const ColourValue &specular,
  972. const ColourValue &emissive, Real shininess,
  973. TrackVertexColourType tracking)
  974. {
  975. // Track vertex colour
  976. if(tracking != TVC_NONE)
  977. {
  978. GLenum gt = GL_DIFFUSE;
  979. // There are actually 15 different combinations for tracking, of which
  980. // GL only supports the most used 5. This means that we have to do some
  981. // magic to find the best match. NOTE:
  982. // GL_AMBIENT_AND_DIFFUSE != GL_AMBIENT | GL__DIFFUSE
  983. if(tracking & TVC_AMBIENT)
  984. {
  985. if(tracking & TVC_DIFFUSE)
  986. {
  987. gt = GL_AMBIENT_AND_DIFFUSE;
  988. }
  989. else
  990. {
  991. gt = GL_AMBIENT;
  992. }
  993. }
  994. else if(tracking & TVC_DIFFUSE)
  995. {
  996. gt = GL_DIFFUSE;
  997. }
  998. else if(tracking & TVC_SPECULAR)
  999. {
  1000. gt = GL_SPECULAR;
  1001. }
  1002. else if(tracking & TVC_EMISSIVE)
  1003. {
  1004. gt = GL_EMISSION;
  1005. }
  1006. glColorMaterial(GL_FRONT_AND_BACK, gt);
  1007. glEnable(GL_COLOR_MATERIAL);
  1008. }
  1009. else
  1010. {
  1011. glDisable(GL_COLOR_MATERIAL);
  1012. }
  1013. // XXX Cache previous values?
  1014. // XXX Front or Front and Back?
  1015. GLfloat f4val[4] = {diffuse.r, diffuse.g, diffuse.b, diffuse.a};
  1016. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, f4val);
  1017. f4val[0] = ambient.r;
  1018. f4val[1] = ambient.g;
  1019. f4val[2] = ambient.b;
  1020. f4val[3] = ambient.a;
  1021. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, f4val);
  1022. f4val[0] = specular.r;
  1023. f4val[1] = specular.g;
  1024. f4val[2] = specular.b;
  1025. f4val[3] = specular.a;
  1026. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, f4val);
  1027. f4val[0] = emissive.r;
  1028. f4val[1] = emissive.g;
  1029. f4val[2] = emissive.b;
  1030. f4val[3] = emissive.a;
  1031. glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, f4val);
  1032. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
  1033. }
  1034. //-----------------------------------------------------------------------------
  1035. void GLRenderSystem::_setPointParameters(Real size,
  1036. bool attenuationEnabled, Real constant, Real linear, Real quadratic,
  1037. Real minSize, Real maxSize)
  1038. {
  1039. float val[4] = {1, 0, 0, 1};
  1040. if(attenuationEnabled)
  1041. {
  1042. // Point size is still calculated in pixels even when attenuation is
  1043. // enabled, which is pretty awkward, since you typically want a viewport
  1044. // independent size if you're looking for attenuation.
  1045. // So, scale the point size up by viewport size (this is equivalent to
  1046. // what D3D does as standard)
  1047. size = size * mActiveViewport->getActualHeight();
  1048. minSize = minSize * mActiveViewport->getActualHeight();
  1049. if (maxSize == 0.0f)
  1050. maxSize = mCurrentCapabilities->getMaxPointSize(); // pixels
  1051. else
  1052. maxSize = maxSize * mActiveViewport->getActualHeight();
  1053. // XXX: why do I need this for results to be consistent with D3D?
  1054. // Equations are supposedly the same once you factor in vp height
  1055. Real correction = 0.005f;
  1056. // scaling required
  1057. val[0] = constant;
  1058. val[1] = linear * correction;
  1059. val[2] = quadratic * correction;
  1060. val[3] = 1;
  1061. if (mCurrentCapabilities->hasCapability(RSC_VERTEX_PROGRAM))
  1062. glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
  1063. }
  1064. else
  1065. {
  1066. if (maxSize == 0.0f)
  1067. maxSize = mCurrentCapabilities->getMaxPointSize();
  1068. if (mCurrentCapabilities->hasCapability(RSC_VERTEX_PROGRAM))
  1069. glDisable(GL_VERTEX_PROGRAM_POINT_SIZE);
  1070. }
  1071. // no scaling required
  1072. // GL has no disabled flag for this so just set to constant
  1073. glPointSize(size);
  1074. if (mCurrentCapabilities->hasCapability(RSC_POINT_EXTENDED_PARAMETERS))
  1075. {
  1076. glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION, val);
  1077. glPointParameterf(GL_POINT_SIZE_MIN, minSize);
  1078. glPointParameterf(GL_POINT_SIZE_MAX, maxSize);
  1079. }
  1080. else if (mCurrentCapabilities->hasCapability(RSC_POINT_EXTENDED_PARAMETERS_ARB))
  1081. {
  1082. glPointParameterfvARB(GL_POINT_DISTANCE_ATTENUATION, val);
  1083. glPointParameterfARB(GL_POINT_SIZE_MIN, minSize);
  1084. glPointParameterfARB(GL_POINT_SIZE_MAX, maxSize);
  1085. }
  1086. else if (mCurrentCapabilities->hasCapability(RSC_POINT_EXTENDED_PARAMETERS_EXT))
  1087. {
  1088. glPointParameterfvEXT(GL_POINT_DISTANCE_ATTENUATION, val);
  1089. glPointParameterfEXT(GL_POINT_SIZE_MIN, minSize);
  1090. glPointParameterfEXT(GL_POINT_SIZE_MAX, maxSize);
  1091. }
  1092. }
  1093. //---------------------------------------------------------------------
  1094. void GLRenderSystem::_setPointSpritesEnabled(bool enabled)
  1095. {
  1096. if (!getCapabilities()->hasCapability(RSC_POINT_SPRITES))
  1097. return;
  1098. if (enabled)
  1099. {
  1100. glEnable(GL_POINT_SPRITE);
  1101. }
  1102. else
  1103. {
  1104. glDisable(GL_POINT_SPRITE);
  1105. }
  1106. // Set sprite texture coord generation
  1107. // Don't offer this as an option since D3D links it to sprite enabled
  1108. for (ushort i = 0; i < mFixedFunctionTextureUnits; ++i)
  1109. {
  1110. activateGLTextureUnit(i);
  1111. glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE,
  1112. enabled ? GL_TRUE : GL_FALSE);
  1113. }
  1114. activateGLTextureUnit(0);
  1115. }
  1116. //-----------------------------------------------------------------------------
  1117. void GLRenderSystem::_setTexture(size_t stage, bool enabled, const TexturePtr &texPtr)
  1118. {
  1119. GLTexturePtr tex = std::static_pointer_cast<GLTexture>(texPtr);
  1120. GLenum lastTextureType = mTextureTypes[stage];
  1121. if (!activateGLTextureUnit(stage))
  1122. return;
  1123. if (enabled)
  1124. {
  1125. if (tex)
  1126. {
  1127. // note used
  1128. mTextureTypes[stage] = tex->getGLTextureTarget();
  1129. }
  1130. else
  1131. // assume 2D
  1132. mTextureTypes[stage] = GL_TEXTURE_2D;
  1133. if(lastTextureType != mTextureTypes[stage] && lastTextureType != 0)
  1134. {
  1135. if (stage < mFixedFunctionTextureUnits)
  1136. {
  1137. glDisable( lastTextureType );
  1138. }
  1139. }
  1140. if (stage < mFixedFunctionTextureUnits)
  1141. {
  1142. glEnable( mTextureTypes[stage] );
  1143. }
  1144. if(tex)
  1145. glBindTexture( mTextureTypes[stage], tex->getGLID() );
  1146. else
  1147. {
  1148. glBindTexture( mTextureTypes[stage], static_cast<GLTextureManager*>(mTextureManager)->getWarningTextureID() );
  1149. }
  1150. }
  1151. else
  1152. {
  1153. if (stage < mFixedFunctionTextureUnits)
  1154. {
  1155. if (lastTextureType != 0)
  1156. {
  1157. glDisable( mTextureTypes[stage] );
  1158. }
  1159. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  1160. }
  1161. // bind zero texture
  1162. glBindTexture(GL_TEXTURE_2D, 0);
  1163. }
  1164. activateGLTextureUnit(0);
  1165. }
  1166. //-----------------------------------------------------------------------------
  1167. void GLRenderSystem::_setTextureCoordSet(size_t stage, size_t index)
  1168. {
  1169. mTextureCoordIndex[stage] = index;
  1170. }
  1171. //-----------------------------------------------------------------------------
  1172. void GLRenderSystem::_setTextureCoordCalculation(size_t stage, TexCoordCalcMethod m,
  1173. const Frustum* frustum)
  1174. {
  1175. if (stage >= mFixedFunctionTextureUnits)
  1176. {
  1177. // Can't do this
  1178. return;
  1179. }
  1180. GLfloat M[16];
  1181. Matrix4 projectionBias;
  1182. // Default to no extra auto texture matrix
  1183. mUseAutoTextureMatrix = false;
  1184. GLfloat eyePlaneS[] = {1.0, 0.0, 0.0, 0.0};
  1185. GLfloat eyePlaneT[] = {0.0, 1.0, 0.0, 0.0};
  1186. GLfloat eyePlaneR[] = {0.0, 0.0, 1.0, 0.0};
  1187. GLfloat eyePlaneQ[] = {0.0, 0.0, 0.0, 1.0};
  1188. if (!activateGLTextureUnit(stage))
  1189. return;
  1190. switch( m )
  1191. {
  1192. case TEXCALC_NONE:
  1193. glDisable( GL_TEXTURE_GEN_S );
  1194. glDisable( GL_TEXTURE_GEN_T );
  1195. glDisable( GL_TEXTURE_GEN_R );
  1196. glDisable( GL_TEXTURE_GEN_Q );
  1197. break;
  1198. case TEXCALC_ENVIRONMENT_MAP:
  1199. glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP );
  1200. glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP );
  1201. glEnable( GL_TEXTURE_GEN_S );
  1202. glEnable( GL_TEXTURE_GEN_T );
  1203. glDisable( GL_TEXTURE_GEN_R );
  1204. glDisable( GL_TEXTURE_GEN_Q );
  1205. // Need to use a texture matrix to flip the spheremap
  1206. mUseAutoTextureMatrix = true;
  1207. memset(mAutoTextureMatrix, 0, sizeof(GLfloat)*16);
  1208. mAutoTextureMatrix[0] = mAutoTextureMatrix[10] = mAutoTextureMatrix[15] = 1.0f;
  1209. mAutoTextureMatrix[5] = -1.0f;
  1210. break;
  1211. case TEXCALC_ENVIRONMENT_MAP_PLANAR:
  1212. // XXX This doesn't seem right?!
  1213. #ifdef GL_VERSION_1_3
  1214. glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
  1215. glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
  1216. glTexGeni( GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
  1217. glEnable( GL_TEXTURE_GEN_S );
  1218. glEnable( GL_TEXTURE_GEN_T );
  1219. glEnable( GL_TEXTURE_GEN_R );
  1220. glDisable( GL_TEXTURE_GEN_Q );
  1221. #else
  1222. glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP );
  1223. glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP );
  1224. glEnable( GL_TEXTURE_GEN_S );
  1225. glEnable( GL_TEXTURE_GEN_T );
  1226. glDisable( GL_TEXTURE_GEN_R );
  1227. glDisable( GL_TEXTURE_GEN_Q );
  1228. #endif
  1229. break;
  1230. case TEXCALC_ENVIRONMENT_MAP_REFLECTION:
  1231. glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
  1232. glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
  1233. glTexGeni( GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
  1234. glEnable( GL_TEXTURE_GEN_S );
  1235. glEnable( GL_TEXTURE_GEN_T );
  1236. glEnable( GL_TEXTURE_GEN_R );
  1237. glDisable( GL_TEXTURE_GEN_Q );
  1238. // We need an extra texture matrix here
  1239. // This sets the texture matrix to be the inverse of the view matrix
  1240. mUseAutoTextureMatrix = true;
  1241. makeGLMatrix( M, mViewMatrix);
  1242. // Transpose 3x3 in order to invert matrix (rotation)
  1243. // Note that we need to invert the Z _before_ the rotation
  1244. // No idea why we have to invert the Z at all, but reflection is wrong without it
  1245. mAutoTextureMatrix[0] = M[0]; mAutoTextureMatrix[1] = M[4]; mAutoTextureMatrix[2] = -M[8];
  1246. mAutoTextureMatrix[4] = M[1]; mAutoTextureMatrix[5] = M[5]; mAutoTextureMatrix[6] = -M[9];
  1247. mAutoTextureMatrix[8] = M[2]; mAutoTextureMatrix[9] = M[6]; mAutoTextureMatrix[10] = -M[10];
  1248. mAutoTextureMatrix[3] = mAutoTextureMatrix[7] = mAutoTextureMatrix[11] = 0.0f;
  1249. mAutoTextureMatrix[12] = mAutoTextureMatrix[13] = mAutoTextureMatrix[14] = 0.0f;
  1250. mAutoTextureMatrix[15] = 1.0f;
  1251. break;
  1252. case TEXCALC_ENVIRONMENT_MAP_NORMAL:
  1253. glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP );
  1254. glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP );
  1255. glTexGeni( GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP );
  1256. glEnable( GL_TEXTURE_GEN_S );
  1257. glEnable( GL_TEXTURE_GEN_T );
  1258. glEnable( GL_TEXTURE_GEN_R );
  1259. glDisable( GL_TEXTURE_GEN_Q );
  1260. break;
  1261. case TEXCALC_PROJECTIVE_TEXTURE:
  1262. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  1263. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  1264. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  1265. glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  1266. glTexGenfv(GL_S, GL_EYE_PLANE, eyePlaneS);
  1267. glTexGenfv(GL_T, GL_EYE_PLANE, eyePlaneT);
  1268. glTexGenfv(GL_R, GL_EYE_PLANE, eyePlaneR);
  1269. glTexGenfv(GL_Q, GL_EYE_PLANE, eyePlaneQ);
  1270. glEnable(GL_TEXTURE_GEN_S);
  1271. glEnable(GL_TEXTURE_GEN_T);
  1272. glEnable(GL_TEXTURE_GEN_R);
  1273. glEnable(GL_TEXTURE_GEN_Q);
  1274. mUseAutoTextureMatrix = true;
  1275. // Set scale and translation matrix for projective textures
  1276. projectionBias = Matrix4::CLIPSPACE2DTOIMAGESPACE;
  1277. projectionBias = projectionBias * frustum->getProjectionMatrix();
  1278. if(mTexProjRelative)
  1279. {
  1280. Matrix4 viewMatrix;
  1281. frustum->calcViewMatrixRelative(mTexProjRelativeOrigin, viewMatrix);
  1282. projectionBias = projectionBias * viewMatrix;
  1283. }
  1284. else
  1285. {
  1286. projectionBias = projectionBias * frustum->getViewMatrix();
  1287. }
  1288. projectionBias = projectionBias * mWorldMatrix;
  1289. makeGLMatrix(mAutoTextureMatrix, projectionBias);
  1290. break;
  1291. default:
  1292. break;
  1293. }
  1294. activateGLTextureUnit(0);
  1295. }
  1296. //-----------------------------------------------------------------------------
  1297. GLint GLRenderSystem::getTextureAddressingMode(
  1298. TextureState::TextureAddressingMode tam) const
  1299. {
  1300. switch(tam)
  1301. {
  1302. default:
  1303. case TextureState::TAM_WRAP:
  1304. return GL_REPEAT;
  1305. case TextureState::TAM_MIRROR:
  1306. return GL_MIRRORED_REPEAT;
  1307. case TextureState::TAM_CLAMP:
  1308. return GL_CLAMP_TO_EDGE;
  1309. case TextureState::TAM_BORDER:
  1310. return GL_CLAMP_TO_BORDER;
  1311. }
  1312. }
  1313. //-----------------------------------------------------------------------------
  1314. void GLRenderSystem::_setTextureAddressingMode(size_t stage, const TextureState::UVWAddressingMode& uvw)
  1315. {
  1316. if (!activateGLTextureUnit(stage))
  1317. return;
  1318. glTexParameteri( mTextureTypes[stage], GL_TEXTURE_WRAP_S,
  1319. getTextureAddressingMode(uvw.u));
  1320. glTexParameteri( mTextureTypes[stage], GL_TEXTURE_WRAP_T,
  1321. getTextureAddressingMode(uvw.v));
  1322. glTexParameteri( mTextureTypes[stage], GL_TEXTURE_WRAP_R,
  1323. getTextureAddressingMode(uvw.w));
  1324. activateGLTextureUnit(0);
  1325. }
  1326. //-----------------------------------------------------------------------------
  1327. void GLRenderSystem::_setTextureBorderColour(size_t stage, const ColourValue& colour)
  1328. {
  1329. GLfloat border[4] = { colour.r, colour.g, colour.b, colour.a };
  1330. if (activateGLTextureUnit(stage))
  1331. {
  1332. glTexParameterfv( mTextureTypes[stage], GL_TEXTURE_BORDER_COLOR, border);
  1333. activateGLTextureUnit(0);
  1334. }
  1335. }
  1336. //-----------------------------------------------------------------------------
  1337. void GLRenderSystem::_setTextureMipmapBias(size_t stage, float bias)
  1338. {
  1339. if (mCurrentCapabilities->hasCapability(RSC_MIPMAP_LOD_BIAS))
  1340. {
  1341. if (activateGLTextureUnit(stage))
  1342. {
  1343. glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, bias);
  1344. activateGLTextureUnit(0);
  1345. }
  1346. }
  1347. }
  1348. //-----------------------------------------------------------------------------
  1349. void GLRenderSystem::_setTextureMatrix(size_t stage, const Matrix4& xform)
  1350. {
  1351. if (stage >= mFixedFunctionTextureUnits)
  1352. {
  1353. // Can't do this
  1354. return;
  1355. }
  1356. GLfloat mat[16];
  1357. makeGLMatrix(mat, xform);
  1358. if (!activateGLTextureUnit(stage))
  1359. return;
  1360. glMatrixMode(GL_TEXTURE);
  1361. // Load this matrix in
  1362. glLoadMatrixf(mat);
  1363. if (mUseAutoTextureMatrix)
  1364. {
  1365. // Concat auto matrix
  1366. glMultMatrixf(mAutoTextureMatrix);
  1367. }
  1368. glMatrixMode(GL_MODELVIEW);
  1369. activateGLTextureUnit(0);
  1370. }
  1371. //-----------------------------------------------------------------------------
  1372. GLint GLRenderSystem::getBlendMode(SceneBlendFactor ogreBlend) const
  1373. {
  1374. switch(ogreBlend)
  1375. {
  1376. case SBF_ONE:
  1377. return GL_ONE;
  1378. case SBF_ZERO:
  1379. return GL_ZERO;
  1380. case SBF_DEST_COLOUR:
  1381. return GL_DST_COLOR;
  1382. case SBF_SOURCE_COLOUR:
  1383. return GL_SRC_COLOR;
  1384. case SBF_ONE_MINUS_DEST_COLOUR:
  1385. return GL_ONE_MINUS_DST_COLOR;
  1386. case SBF_ONE_MINUS_SOURCE_COLOUR:
  1387. return GL_ONE_MINUS_SRC_COLOR;
  1388. case SBF_DEST_ALPHA:
  1389. return GL_DST_ALPHA;
  1390. case SBF_SOURCE_ALPHA:
  1391. return GL_SRC_ALPHA;
  1392. case SBF_ONE_MINUS_DEST_ALPHA:
  1393. return GL_ONE_MINUS_DST_ALPHA;
  1394. case SBF_ONE_MINUS_SOURCE_ALPHA:
  1395. return GL_ONE_MINUS_SRC_ALPHA;
  1396. };
  1397. // to keep compiler happy
  1398. return GL_ONE;
  1399. }
  1400. void GLRenderSystem::_setSceneBlending(SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendOperation op )
  1401. {
  1402. GLint sourceBlend = getBlendMode(sourceFactor);
  1403. GLint destBlend = getBlendMode(destFactor);
  1404. if(sourceFactor == SBF_ONE && destFactor == SBF_ZERO)
  1405. {
  1406. glDisable(GL_BLEND);
  1407. }
  1408. else
  1409. {
  1410. glEnable(GL_BLEND);
  1411. glBlendFunc(sourceBlend, destBlend);
  1412. }
  1413. GLint func = GL_FUNC_ADD;
  1414. switch(op)
  1415. {
  1416. case SBO_ADD:
  1417. func = GL_FUNC_ADD;
  1418. break;
  1419. case SBO_SUBTRACT:
  1420. func = GL_FUNC_SUBTRACT;
  1421. break;
  1422. case SBO_REVERSE_SUBTRACT:
  1423. func = GL_FUNC_REVERSE_SUBTRACT;
  1424. break;
  1425. case SBO_MIN:
  1426. func = GL_MIN;
  1427. break;
  1428. case SBO_MAX:
  1429. func = GL_MAX;
  1430. break;
  1431. }
  1432. if(GLEW_VERSION_1_4 || GLEW_ARB_imaging)
  1433. {
  1434. glBlendEquation(func);
  1435. }
  1436. else if(GLEW_EXT_blend_minmax && (func == GL_MIN || func == GL_MAX))
  1437. {
  1438. glBlendEquationEXT(func);
  1439. }
  1440. }
  1441. //-----------------------------------------------------------------------------
  1442. void GLRenderSystem::_setSeparateSceneBlending(
  1443. SceneBlendFactor sourceFactor, SceneBlendFactor destFactor,
  1444. SceneBlendFactor sourceFactorAlpha, SceneBlendFactor destFactorAlpha,
  1445. SceneBlendOperation op, SceneBlendOperation alphaOp )
  1446. {
  1447. GLint sourceBlend = getBlendMode(sourceFactor);
  1448. GLint destBlend = getBlendMode(destFactor);
  1449. GLint sourceBlendAlpha = getBlendMode(sourceFactorAlpha);
  1450. GLint destBlendAlpha = getBlendMode(destFactorAlpha);
  1451. if(sourceFactor == SBF_ONE && destFactor == SBF_ZERO &&
  1452. sourceFactorAlpha == SBF_ONE && destFactorAlpha == SBF_ZERO)
  1453. {
  1454. glDisable(GL_BLEND);
  1455. }
  1456. else
  1457. {
  1458. glEnable(GL_BLEND);
  1459. glBlendFuncSeparate(sourceBlend, destBlend, sourceBlendAlpha, destBlendAlpha);
  1460. }
  1461. GLint func = GL_FUNC_ADD, alphaFunc = GL_FUNC_ADD;
  1462. switch(op)
  1463. {
  1464. case SBO_ADD:
  1465. func = GL_FUNC_ADD;
  1466. break;
  1467. case SBO_SUBTRACT:
  1468. func = GL_FUNC_SUBTRACT;
  1469. break;
  1470. case SBO_REVERSE_SUBTRACT:
  1471. func = GL_FUNC_REVERSE_SUBTRACT;
  1472. break;
  1473. case SBO_MIN:
  1474. func = GL_MIN;
  1475. break;
  1476. case SBO_MAX:
  1477. func = GL_MAX;
  1478. break;
  1479. }
  1480. switch(alphaOp)
  1481. {
  1482. case SBO_ADD:
  1483. alphaFunc = GL_FUNC_ADD;
  1484. break;
  1485. case SBO_SUBTRACT:
  1486. alphaFunc = GL_FUNC_SUBTRACT;
  1487. break;
  1488. case SBO_REVERSE_SUBTRACT:
  1489. alphaFunc = GL_FUNC_REVERSE_SUBTRACT;
  1490. break;
  1491. case SBO_MIN:
  1492. alphaFunc = GL_MIN;
  1493. break;
  1494. case SBO_MAX:
  1495. alphaFunc = GL_MAX;
  1496. break;
  1497. }
  1498. if(GLEW_VERSION_2_0) {
  1499. glBlendEquationSeparate(func, alphaFunc);
  1500. }
  1501. else if(GLEW_EXT_blend_equation_separate) {
  1502. glBlendEquationSeparateEXT(func, alphaFunc);
  1503. }
  1504. }
  1505. //-----------------------------------------------------------------------------
  1506. void GLRenderSystem::_setAlphaRejectSettings(CompareFunction func, unsigned char value, bool alphaToCoverage)
  1507. {
  1508. bool a2c = false;
  1509. static bool lasta2c = false;
  1510. if(func == CMPF_ALWAYS_PASS)
  1511. {
  1512. glDisable(GL_ALPHA_TEST);
  1513. }
  1514. else
  1515. {
  1516. glEnable(GL_ALPHA_TEST);
  1517. a2c = alphaToCoverage;
  1518. glAlphaFunc(convertCompareFunction(func), value / 255.0f);
  1519. }
  1520. if (a2c != lasta2c && getCapabilities()->hasCapability(RSC_ALPHA_TO_COVERAGE))
  1521. {
  1522. if (a2c)
  1523. glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);
  1524. else
  1525. glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
  1526. lasta2c = a2c;
  1527. }
  1528. }
  1529. //-----------------------------------------------------------------------------
  1530. void GLRenderSystem::_setViewport(Viewport *vp)
  1531. {
  1532. // Check if viewport is different
  1533. if (vp != mActiveViewport)
  1534. {
  1535. RenderTarget* target;
  1536. target = vp->getTarget();
  1537. _setRenderTarget(target);
  1538. mActiveViewport = vp;
  1539. GLsizei x, y, w, h;
  1540. // Calculate the "lower-left" corner of the viewport
  1541. w = vp->getActualWidth();
  1542. h = vp->getActualHeight();
  1543. x = vp->getActualLeft();
  1544. y = vp->getActualTop();
  1545. if (!target->requiresTextureFlipping())
  1546. {
  1547. // Convert "upper-left" corner to "lower-left"
  1548. y = target->getHeight() - h - y;
  1549. }
  1550. glViewport(x, y, w, h);
  1551. // Configure the viewport clipping
  1552. glScissor(x, y, w, h);
  1553. }
  1554. }
  1555. void GLRenderSystem::setLights()
  1556. {
  1557. // TODO PORT - I'm not using fixed pipeline lights. Remove this later
  1558. //for (size_t i = 0; i < MAX_LIGHTS; ++i)
  1559. //{
  1560. // if (mLights[i] != NULL)
  1561. // {
  1562. // Light* lt = mLights[i];
  1563. // setGLLightPositionDirection(lt, GL_LIGHT0 + i);
  1564. // }
  1565. //}
  1566. }
  1567. //-----------------------------------------------------------------------------
  1568. void GLRenderSystem::_beginFrame(void)
  1569. {
  1570. if (!mActiveViewport)
  1571. OGRE_EXCEPT(Exception::ERR_INVALID_STATE,
  1572. "Cannot begin frame - no viewport selected.",
  1573. "GLRenderSystem::_beginFrame");
  1574. // Activate the viewport clipping
  1575. glEnable(GL_SCISSOR_TEST);
  1576. }
  1577. //-----------------------------------------------------------------------------
  1578. void GLRenderSystem::_endFrame(void)
  1579. {
  1580. // Deactivate the viewport clipping.
  1581. glDisable(GL_SCISSOR_TEST);
  1582. // unbind GPU programs at end of frame
  1583. // this is mostly to avoid holding bound programs that might get deleted
  1584. // outside via the resource manager
  1585. unbindGpuProgram(GPT_VERTEX_PROGRAM);
  1586. unbindGpuProgram(GPT_FRAGMENT_PROGRAM);
  1587. }
  1588. //-----------------------------------------------------------------------------
  1589. void GLRenderSystem::_setCullingMode(CullingMode mode)
  1590. {
  1591. mCullingMode = mode;
  1592. // NB: Because two-sided stencil API dependence of the front face, we must
  1593. // use the same 'winding' for the front face everywhere. As the OGRE default
  1594. // culling mode is clockwise, we also treat anticlockwise winding as front
  1595. // face for consistently. On the assumption that, we can't change the front
  1596. // face by glFrontFace anywhere.
  1597. GLenum cullMode;
  1598. switch( mode )
  1599. {
  1600. case CULL_NONE:
  1601. glDisable( GL_CULL_FACE );
  1602. return;
  1603. default:
  1604. case CULL_CLOCKWISE:
  1605. if (mActiveRenderTarget &&
  1606. ((mActiveRenderTarget->requiresTextureFlipping() && !mInvertVertexWinding) ||
  1607. (!mActiveRenderTarget->requiresTextureFlipping() && mInvertVertexWinding)))
  1608. {
  1609. cullMode = GL_FRONT;
  1610. }
  1611. else
  1612. {
  1613. cullMode = GL_BACK;
  1614. }
  1615. break;
  1616. case CULL_ANTICLOCKWISE:
  1617. if (mActiveRenderTarget &&
  1618. ((mActiveRenderTarget->requiresTextureFlipping() && !mInvertVertexWinding) ||
  1619. (!mActiveRenderTarget->requiresTextureFlipping() && mInvertVertexWinding)))
  1620. {
  1621. cullMode = GL_BACK;
  1622. }
  1623. else
  1624. {
  1625. cullMode = GL_FRONT;
  1626. }
  1627. break;
  1628. }
  1629. glEnable( GL_CULL_FACE );
  1630. glCullFace( cullMode );
  1631. }
  1632. //-----------------------------------------------------------------------------
  1633. void GLRenderSystem::_setDepthBufferParams(bool depthTest, bool depthWrite, CompareFunction depthFunction)
  1634. {
  1635. _setDepthBufferCheckEnabled(depthTest);
  1636. _setDepthBufferWriteEnabled(depthWrite);
  1637. _setDepthBufferFunction(depthFunction);
  1638. }
  1639. //-----------------------------------------------------------------------------
  1640. void GLRenderSystem::_setDepthBufferCheckEnabled(bool enabled)
  1641. {
  1642. if (enabled)
  1643. {
  1644. glClearDepth(1.0f);
  1645. glEnable(GL_DEPTH_TEST);
  1646. }
  1647. else
  1648. {
  1649. glDisable(GL_DEPTH_TEST);
  1650. }
  1651. }
  1652. //-----------------------------------------------------------------------------
  1653. void GLRenderSystem::_setDepthBufferWriteEnabled(bool enabled)
  1654. {
  1655. GLboolean flag = enabled ? GL_TRUE : GL_FALSE;
  1656. glDepthMask( flag );
  1657. // Store for reference in _beginFrame
  1658. mDepthWrite = enabled;
  1659. }
  1660. //-----------------------------------------------------------------------------
  1661. void GLRenderSystem::_setDepthBufferFunction(CompareFunction func)
  1662. {
  1663. glDepthFunc(convertCompareFunction(func));
  1664. }
  1665. //-----------------------------------------------------------------------------
  1666. void GLRenderSystem::_setDepthBias(float constantBias, float slopeScaleBias)
  1667. {
  1668. if (constantBias != 0 || slopeScaleBias != 0)
  1669. {
  1670. glEnable(GL_POLYGON_OFFSET_FILL);
  1671. glEnable(GL_POLYGON_OFFSET_POINT);
  1672. glEnable(GL_POLYGON_OFFSET_LINE);
  1673. glPolygonOffset(-slopeScaleBias, -constantBias);
  1674. }
  1675. else
  1676. {
  1677. glDisable(GL_POLYGON_OFFSET_FILL);
  1678. glDisable(GL_POLYGON_OFFSET_POINT);
  1679. glDisable(GL_POLYGON_OFFSET_LINE);
  1680. }
  1681. }
  1682. //-----------------------------------------------------------------------------
  1683. void GLRenderSystem::_setColourBufferWriteEnabled(bool red, bool green, bool blue, bool alpha)
  1684. {
  1685. glColorMask(red, green, blue, alpha);
  1686. // record this
  1687. mColourWrite[0] = red;
  1688. mColourWrite[1] = blue;
  1689. mColourWrite[2] = green;
  1690. mColourWrite[3] = alpha;
  1691. }
  1692. //-----------------------------------------------------------------------------
  1693. String GLRenderSystem::getErrorDescription(long errCode) const
  1694. {
  1695. const GLubyte *errString = gluErrorString (errCode);
  1696. return (errString != 0) ? String((const char*) errString) : StringUtil::BLANK;
  1697. }
  1698. //-----------------------------------------------------------------------------
  1699. void GLRenderSystem::setLightingEnabled(bool enabled)
  1700. {
  1701. if (enabled)
  1702. {
  1703. glEnable(GL_LIGHTING);
  1704. }
  1705. else
  1706. {
  1707. glDisable(GL_LIGHTING);
  1708. }
  1709. }
  1710. //-----------------------------------------------------------------------------
  1711. void GLRenderSystem::_setFog(FogMode mode, const ColourValue& colour, Real density, Real start, Real end)
  1712. {
  1713. GLint fogMode;
  1714. switch (mode)
  1715. {
  1716. case FOG_EXP:
  1717. fogMode = GL_EXP;
  1718. break;
  1719. case FOG_EXP2:
  1720. fogMode = GL_EXP2;
  1721. break;
  1722. case FOG_LINEAR:
  1723. fogMode = GL_LINEAR;
  1724. break;
  1725. default:
  1726. // Give up on it
  1727. glDisable(GL_FOG);
  1728. return;
  1729. }
  1730. glEnable(GL_FOG);
  1731. glFogi(GL_FOG_MODE, fogMode);
  1732. GLfloat fogColor[4] = {colour.r, colour.g, colour.b, colour.a};
  1733. glFogfv(GL_FOG_COLOR, fogColor);
  1734. glFogf(GL_FOG_DENSITY, density);
  1735. glFogf(GL_FOG_START, start);
  1736. glFogf(GL_FOG_END, end);
  1737. // XXX Hint here?
  1738. }
  1739. VertexElementType GLRenderSystem::getColourVertexElementType(void) const
  1740. {
  1741. return VET_COLOUR_ABGR;
  1742. }
  1743. void GLRenderSystem::_convertProjectionMatrix(const Matrix4& matrix,
  1744. Matrix4& dest, bool forGpuProgram)
  1745. {
  1746. // no any conversion request for OpenGL
  1747. dest = matrix;
  1748. }
  1749. void GLRenderSystem::_makeProjectionMatrix(const Radian& fovy, Real aspect, Real nearPlane,
  1750. Real farPlane, Matrix4& dest, bool forGpuProgram)
  1751. {
  1752. Radian thetaY ( fovy / 2.0f );
  1753. Real tanThetaY = Math::Tan(thetaY);
  1754. //Real thetaX = thetaY * aspect;
  1755. //Real tanThetaX = Math::Tan(thetaX);
  1756. // Calc matrix elements
  1757. Real w = (1.0f / tanThetaY) / aspect;
  1758. Real h = 1.0f / tanThetaY;
  1759. Real q, qn;
  1760. if (farPlane == 0)
  1761. {
  1762. // Infinite far plane
  1763. q = Frustum::INFINITE_FAR_PLANE_ADJUST - 1;
  1764. qn = nearPlane * (Frustum::INFINITE_FAR_PLANE_ADJUST - 2);
  1765. }
  1766. else
  1767. {
  1768. q = -(farPlane + nearPlane) / (farPlane - nearPlane);
  1769. qn = -2 * (farPlane * nearPlane) / (farPlane - nearPlane);
  1770. }
  1771. // NB This creates Z in range [-1,1]
  1772. //
  1773. // [ w 0 0 0 ]
  1774. // [ 0 h 0 0 ]
  1775. // [ 0 0 q qn ]
  1776. // [ 0 0 -1 0 ]
  1777. dest = Matrix4::ZERO;
  1778. dest[0][0] = w;
  1779. dest[1][1] = h;
  1780. dest[2][2] = q;
  1781. dest[2][3] = qn;
  1782. dest[3][2] = -1;
  1783. }
  1784. void GLRenderSystem::_makeOrthoMatrix(const Radian& fovy, Real aspect, Real nearPlane,
  1785. Real farPlane, Matrix4& dest, bool forGpuProgram)
  1786. {
  1787. Radian thetaY (fovy / 2.0f);
  1788. Real tanThetaY = Math::Tan(thetaY);
  1789. //Real thetaX = thetaY * aspect;
  1790. Real tanThetaX = tanThetaY * aspect; //Math::Tan(thetaX);
  1791. Real half_w = tanThetaX * nearPlane;
  1792. Real half_h = tanThetaY * nearPlane;
  1793. Real iw = 1.0f / half_w;
  1794. Real ih = 1.0f / half_h;
  1795. Real q;
  1796. if (farPlane == 0)
  1797. {
  1798. q = 0;
  1799. }
  1800. else
  1801. {
  1802. q = 2.0f / (farPlane - nearPlane);
  1803. }
  1804. dest = Matrix4::ZERO;
  1805. dest[0][0] = iw;
  1806. dest[1][1] = ih;
  1807. dest[2][2] = -q;
  1808. dest[2][3] = - (farPlane + nearPlane)/(farPlane - nearPlane);
  1809. dest[3][3] = 1;
  1810. }
  1811. void GLRenderSystem::_setPolygonMode(PolygonMode level)
  1812. {
  1813. GLenum glmode;
  1814. switch(level)
  1815. {
  1816. case PM_POINTS:
  1817. glmode = GL_POINT;
  1818. break;
  1819. case PM_WIREFRAME:
  1820. glmode = GL_LINE;
  1821. break;
  1822. default:
  1823. case PM_SOLID:
  1824. glmode = GL_FILL;
  1825. break;
  1826. }
  1827. glPolygonMode(GL_FRONT_AND_BACK, glmode);
  1828. }
  1829. //---------------------------------------------------------------------
  1830. void GLRenderSystem::setStencilCheckEnabled(bool enabled)
  1831. {
  1832. if (enabled)
  1833. {
  1834. glEnable(GL_STENCIL_TEST);
  1835. }
  1836. else
  1837. {
  1838. glDisable(GL_STENCIL_TEST);
  1839. }
  1840. }
  1841. //---------------------------------------------------------------------
  1842. void GLRenderSystem::setStencilBufferParams(CompareFunction func,
  1843. uint32 refValue, uint32 mask, StencilOperation stencilFailOp,
  1844. StencilOperation depthFailOp, StencilOperation passOp,
  1845. bool twoSidedOperation)
  1846. {
  1847. bool flip;
  1848. mStencilMask = mask;
  1849. if (twoSidedOperation)
  1850. {
  1851. if (!mCurrentCapabilities->hasCapability(RSC_TWO_SIDED_STENCIL))
  1852. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "2-sided stencils are not supported",
  1853. "GLRenderSystem::setStencilBufferParams");
  1854. // NB: We should always treat CCW as front face for consistent with default
  1855. // culling mode. Therefore, we must take care with two-sided stencil settings.
  1856. flip = (mInvertVertexWinding && !mActiveRenderTarget->requiresTextureFlipping()) ||
  1857. (!mInvertVertexWinding && mActiveRenderTarget->requiresTextureFlipping());
  1858. if(GLEW_VERSION_2_0) // New GL2 commands
  1859. {
  1860. // Back
  1861. glStencilMaskSeparate(GL_BACK, mask);
  1862. glStencilFuncSeparate(GL_BACK, convertCompareFunction(func), refValue, mask);
  1863. glStencilOpSeparate(GL_BACK,
  1864. convertStencilOp(stencilFailOp, !flip),
  1865. convertStencilOp(depthFailOp, !flip),
  1866. convertStencilOp(passOp, !flip));
  1867. // Front
  1868. glStencilMaskSeparate(GL_FRONT, mask);
  1869. glStencilFuncSeparate(GL_FRONT, convertCompareFunction(func), refValue, mask);
  1870. glStencilOpSeparate(GL_FRONT,
  1871. convertStencilOp(stencilFailOp, flip),
  1872. convertStencilOp(depthFailOp, flip),
  1873. convertStencilOp(passOp, flip));
  1874. }
  1875. else // EXT_stencil_two_side
  1876. {
  1877. glEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);
  1878. // Back
  1879. glActiveStencilFaceEXT(GL_BACK);
  1880. glStencilMask(mask);
  1881. glStencilFunc(convertCompareFunction(func), refValue, mask);
  1882. glStencilOp(
  1883. convertStencilOp(stencilFailOp, !flip),
  1884. convertStencilOp(depthFailOp, !flip),
  1885. convertStencilOp(passOp, !flip));
  1886. // Front
  1887. glActiveStencilFaceEXT(GL_FRONT);
  1888. glStencilMask(mask);
  1889. glStencilFunc(convertCompareFunction(func), refValue, mask);
  1890. glStencilOp(
  1891. convertStencilOp(stencilFailOp, flip),
  1892. convertStencilOp(depthFailOp, flip),
  1893. convertStencilOp(passOp, flip));
  1894. }
  1895. }
  1896. else
  1897. {
  1898. if(!GLEW_VERSION_2_0)
  1899. glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
  1900. flip = false;
  1901. glStencilMask(mask);
  1902. glStencilFunc(convertCompareFunction(func), refValue, mask);
  1903. glStencilOp(
  1904. convertStencilOp(stencilFailOp, flip),
  1905. convertStencilOp(depthFailOp, flip),
  1906. convertStencilOp(passOp, flip));
  1907. }
  1908. }
  1909. //---------------------------------------------------------------------
  1910. GLint GLRenderSystem::convertCompareFunction(CompareFunction func) const
  1911. {
  1912. switch(func)
  1913. {
  1914. case CMPF_ALWAYS_FAIL:
  1915. return GL_NEVER;
  1916. case CMPF_ALWAYS_PASS:
  1917. return GL_ALWAYS;
  1918. case CMPF_LESS:
  1919. return GL_LESS;
  1920. case CMPF_LESS_EQUAL:
  1921. return GL_LEQUAL;
  1922. case CMPF_EQUAL:
  1923. return GL_EQUAL;
  1924. case CMPF_NOT_EQUAL:
  1925. return GL_NOTEQUAL;
  1926. case CMPF_GREATER_EQUAL:
  1927. return GL_GEQUAL;
  1928. case CMPF_GREATER:
  1929. return GL_GREATER;
  1930. };
  1931. // to keep compiler happy
  1932. return GL_ALWAYS;
  1933. }
  1934. //---------------------------------------------------------------------
  1935. GLint GLRenderSystem::convertStencilOp(StencilOperation op, bool invert) const
  1936. {
  1937. switch(op)
  1938. {
  1939. case SOP_KEEP:
  1940. return GL_KEEP;
  1941. case SOP_ZERO:
  1942. return GL_ZERO;
  1943. case SOP_REPLACE:
  1944. return GL_REPLACE;
  1945. case SOP_INCREMENT:
  1946. return invert ? GL_DECR : GL_INCR;
  1947. case SOP_DECREMENT:
  1948. return invert ? GL_INCR : GL_DECR;
  1949. case SOP_INCREMENT_WRAP:
  1950. return invert ? GL_DECR_WRAP_EXT : GL_INCR_WRAP_EXT;
  1951. case SOP_DECREMENT_WRAP:
  1952. return invert ? GL_INCR_WRAP_EXT : GL_DECR_WRAP_EXT;
  1953. case SOP_INVERT:
  1954. return GL_INVERT;
  1955. };
  1956. // to keep compiler happy
  1957. return SOP_KEEP;
  1958. }
  1959. //---------------------------------------------------------------------
  1960. GLuint GLRenderSystem::getCombinedMinMipFilter(void) const
  1961. {
  1962. switch(mMinFilter)
  1963. {
  1964. case FO_ANISOTROPIC:
  1965. case FO_LINEAR:
  1966. switch(mMipFilter)
  1967. {
  1968. case FO_ANISOTROPIC:
  1969. case FO_LINEAR:
  1970. // linear min, linear mip
  1971. return GL_LINEAR_MIPMAP_LINEAR;
  1972. case FO_POINT:
  1973. // linear min, point mip
  1974. return GL_LINEAR_MIPMAP_NEAREST;
  1975. case FO_NONE:
  1976. // linear min, no mip
  1977. return GL_LINEAR;
  1978. }
  1979. break;
  1980. case FO_POINT:
  1981. case FO_NONE:
  1982. switch(mMipFilter)
  1983. {
  1984. case FO_ANISOTROPIC:
  1985. case FO_LINEAR:
  1986. // nearest min, linear mip
  1987. return GL_NEAREST_MIPMAP_LINEAR;
  1988. case FO_POINT:
  1989. // nearest min, point mip
  1990. return GL_NEAREST_MIPMAP_NEAREST;
  1991. case FO_NONE:
  1992. // nearest min, no mip
  1993. return GL_NEAREST;
  1994. }
  1995. break;
  1996. }
  1997. // should never get here
  1998. return 0;
  1999. }
  2000. //---------------------------------------------------------------------
  2001. void GLRenderSystem::_setTextureUnitFiltering(size_t unit,
  2002. FilterType ftype, FilterOptions fo)
  2003. {
  2004. if (!activateGLTextureUnit(unit))
  2005. return;
  2006. switch(ftype)
  2007. {
  2008. case FT_MIN:
  2009. mMinFilter = fo;
  2010. // Combine with existing mip filter
  2011. glTexParameteri(
  2012. mTextureTypes[unit],
  2013. GL_TEXTURE_MIN_FILTER,
  2014. getCombinedMinMipFilter());
  2015. break;
  2016. case FT_MAG:
  2017. switch (fo)
  2018. {
  2019. case FO_ANISOTROPIC: // GL treats linear and aniso the same
  2020. case FO_LINEAR:
  2021. glTexParameteri(
  2022. mTextureTypes[unit],
  2023. GL_TEXTURE_MAG_FILTER,
  2024. GL_LINEAR);
  2025. break;
  2026. case FO_POINT:
  2027. case FO_NONE:
  2028. glTexParameteri(
  2029. mTextureTypes[unit],
  2030. GL_TEXTURE_MAG_FILTER,
  2031. GL_NEAREST);
  2032. break;
  2033. }
  2034. break;
  2035. case FT_MIP:
  2036. mMipFilter = fo;
  2037. // Combine with existing min filter
  2038. glTexParameteri(
  2039. mTextureTypes[unit],
  2040. GL_TEXTURE_MIN_FILTER,
  2041. getCombinedMinMipFilter());
  2042. break;
  2043. }
  2044. activateGLTextureUnit(0);
  2045. }
  2046. //---------------------------------------------------------------------
  2047. GLfloat GLRenderSystem::_getCurrentAnisotropy(size_t unit)
  2048. {
  2049. GLfloat curAniso = 0;
  2050. glGetTexParameterfv(mTextureTypes[unit],
  2051. GL_TEXTURE_MAX_ANISOTROPY_EXT, &curAniso);
  2052. return curAniso ? curAniso : 1;
  2053. }
  2054. //---------------------------------------------------------------------
  2055. void GLRenderSystem::_setTextureLayerAnisotropy(size_t unit, unsigned int maxAnisotropy)
  2056. {
  2057. if (!mCurrentCapabilities->hasCapability(RSC_ANISOTROPY))
  2058. return;
  2059. if (!activateGLTextureUnit(unit))
  2060. return;
  2061. GLfloat largest_supported_anisotropy = 0;
  2062. glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &largest_supported_anisotropy);
  2063. if (maxAnisotropy > largest_supported_anisotropy)
  2064. maxAnisotropy = largest_supported_anisotropy ?
  2065. static_cast<uint>(largest_supported_anisotropy) : 1;
  2066. if (_getCurrentAnisotropy(unit) != maxAnisotropy)
  2067. glTexParameterf(mTextureTypes[unit], GL_TEXTURE_MAX_ANISOTROPY_EXT, (float)maxAnisotropy);
  2068. activateGLTextureUnit(0);
  2069. }
  2070. //-----------------------------------------------------------------------------
  2071. void GLRenderSystem::_setTextureBlendMode(size_t stage, const LayerBlendModeEx& bm)
  2072. {
  2073. if (stage >= mFixedFunctionTextureUnits)
  2074. {
  2075. // Can't do this
  2076. return;
  2077. }
  2078. // Check to see if blending is supported
  2079. if(!mCurrentCapabilities->hasCapability(RSC_BLENDING))
  2080. return;
  2081. GLenum src1op, src2op, cmd;
  2082. GLfloat cv1[4], cv2[4];
  2083. if (bm.blendType == LBT_COLOUR)
  2084. {
  2085. cv1[0] = bm.colourArg1.r;
  2086. cv1[1] = bm.colourArg1.g;
  2087. cv1[2] = bm.colourArg1.b;
  2088. cv1[3] = bm.colourArg1.a;
  2089. mManualBlendColours[stage][0] = bm.colourArg1;
  2090. cv2[0] = bm.colourArg2.r;
  2091. cv2[1] = bm.colourArg2.g;
  2092. cv2[2] = bm.colourArg2.b;
  2093. cv2[3] = bm.colourArg2.a;
  2094. mManualBlendColours[stage][1] = bm.colourArg2;
  2095. }
  2096. if (bm.blendType == LBT_ALPHA)
  2097. {
  2098. cv1[0] = mManualBlendColours[stage][0].r;
  2099. cv1[1] = mManualBlendColours[stage][0].g;
  2100. cv1[2] = mManualBlendColours[stage][0].b;
  2101. cv1[3] = bm.alphaArg1;
  2102. cv2[0] = mManualBlendColours[stage][1].r;
  2103. cv2[1] = mManualBlendColours[stage][1].g;
  2104. cv2[2] = mManualBlendColours[stage][1].b;
  2105. cv2[3] = bm.alphaArg2;
  2106. }
  2107. switch (bm.source1)
  2108. {
  2109. case LBS_CURRENT:
  2110. src1op = GL_PREVIOUS;
  2111. break;
  2112. case LBS_TEXTURE:
  2113. src1op = GL_TEXTURE;
  2114. break;
  2115. case LBS_MANUAL:
  2116. src1op = GL_CONSTANT;
  2117. break;
  2118. case LBS_DIFFUSE:
  2119. src1op = GL_PRIMARY_COLOR;
  2120. break;
  2121. // XXX
  2122. case LBS_SPECULAR:
  2123. src1op = GL_PRIMARY_COLOR;
  2124. break;
  2125. default:
  2126. src1op = 0;
  2127. }
  2128. switch (bm.source2)
  2129. {
  2130. case LBS_CURRENT:
  2131. src2op = GL_PREVIOUS;
  2132. break;
  2133. case LBS_TEXTURE:
  2134. src2op = GL_TEXTURE;
  2135. break;
  2136. case LBS_MANUAL:
  2137. src2op = GL_CONSTANT;
  2138. break;
  2139. case LBS_DIFFUSE:
  2140. src2op = GL_PRIMARY_COLOR;
  2141. break;
  2142. // XXX
  2143. case LBS_SPECULAR:
  2144. src2op = GL_PRIMARY_COLOR;
  2145. break;
  2146. default:
  2147. src2op = 0;
  2148. }
  2149. switch (bm.operation)
  2150. {
  2151. case LBX_SOURCE1:
  2152. cmd = GL_REPLACE;
  2153. break;
  2154. case LBX_SOURCE2:
  2155. cmd = GL_REPLACE;
  2156. break;
  2157. case LBX_MODULATE:
  2158. cmd = GL_MODULATE;
  2159. break;
  2160. case LBX_MODULATE_X2:
  2161. cmd = GL_MODULATE;
  2162. break;
  2163. case LBX_MODULATE_X4:
  2164. cmd = GL_MODULATE;
  2165. break;
  2166. case LBX_ADD:
  2167. cmd = GL_ADD;
  2168. break;
  2169. case LBX_ADD_SIGNED:
  2170. cmd = GL_ADD_SIGNED;
  2171. break;
  2172. case LBX_ADD_SMOOTH:
  2173. cmd = GL_INTERPOLATE;
  2174. break;
  2175. case LBX_SUBTRACT:
  2176. cmd = GL_SUBTRACT;
  2177. break;
  2178. case LBX_BLEND_DIFFUSE_COLOUR:
  2179. cmd = GL_INTERPOLATE;
  2180. break;
  2181. case LBX_BLEND_DIFFUSE_ALPHA:
  2182. cmd = GL_INTERPOLATE;
  2183. break;
  2184. case LBX_BLEND_TEXTURE_ALPHA:
  2185. cmd = GL_INTERPOLATE;
  2186. break;
  2187. case LBX_BLEND_CURRENT_ALPHA:
  2188. cmd = GL_INTERPOLATE;
  2189. break;
  2190. case LBX_BLEND_MANUAL:
  2191. cmd = GL_INTERPOLATE;
  2192. break;
  2193. case LBX_DOTPRODUCT:
  2194. cmd = mCurrentCapabilities->hasCapability(RSC_DOT3)
  2195. ? GL_DOT3_RGB : GL_MODULATE;
  2196. break;
  2197. default:
  2198. cmd = 0;
  2199. }
  2200. if (!activateGLTextureUnit(stage))
  2201. return;
  2202. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
  2203. if (bm.blendType == LBT_COLOUR)
  2204. {
  2205. glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, cmd);
  2206. glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, src1op);
  2207. glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, src2op);
  2208. glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_CONSTANT);
  2209. }
  2210. else
  2211. {
  2212. glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, cmd);
  2213. glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, src1op);
  2214. glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, src2op);
  2215. glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, GL_CONSTANT);
  2216. }
  2217. float blendValue[4] = {0, 0, 0, bm.factor};
  2218. switch (bm.operation)
  2219. {
  2220. case LBX_BLEND_DIFFUSE_COLOUR:
  2221. glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_PRIMARY_COLOR);
  2222. glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, GL_PRIMARY_COLOR);
  2223. break;
  2224. case LBX_BLEND_DIFFUSE_ALPHA:
  2225. glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_PRIMARY_COLOR);
  2226. glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, GL_PRIMARY_COLOR);
  2227. break;
  2228. case LBX_BLEND_TEXTURE_ALPHA:
  2229. glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_TEXTURE);
  2230. glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, GL_TEXTURE);
  2231. break;
  2232. case LBX_BLEND_CURRENT_ALPHA:
  2233. glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_PREVIOUS);
  2234. glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, GL_PREVIOUS);
  2235. break;
  2236. case LBX_BLEND_MANUAL:
  2237. glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, blendValue);
  2238. break;
  2239. default:
  2240. break;
  2241. };
  2242. switch (bm.operation)
  2243. {
  2244. case LBX_MODULATE_X2:
  2245. glTexEnvi(GL_TEXTURE_ENV, bm.blendType == LBT_COLOUR ?
  2246. GL_RGB_SCALE : GL_ALPHA_SCALE, 2);
  2247. break;
  2248. case LBX_MODULATE_X4:
  2249. glTexEnvi(GL_TEXTURE_ENV, bm.blendType == LBT_COLOUR ?
  2250. GL_RGB_SCALE : GL_ALPHA_SCALE, 4);
  2251. break;
  2252. default:
  2253. glTexEnvi(GL_TEXTURE_ENV, bm.blendType == LBT_COLOUR ?
  2254. GL_RGB_SCALE : GL_ALPHA_SCALE, 1);
  2255. break;
  2256. }
  2257. if (bm.blendType == LBT_COLOUR){
  2258. glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
  2259. glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
  2260. if (bm.operation == LBX_BLEND_DIFFUSE_COLOUR){
  2261. glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_COLOR);
  2262. } else {
  2263. glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA);
  2264. }
  2265. }
  2266. glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
  2267. glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);
  2268. glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA, GL_SRC_ALPHA);
  2269. if(bm.source1 == LBS_MANUAL)
  2270. glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, cv1);
  2271. if (bm.source2 == LBS_MANUAL)
  2272. glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, cv2);
  2273. activateGLTextureUnit(0);
  2274. }
  2275. //---------------------------------------------------------------------
  2276. void GLRenderSystem::setVertexDeclaration(VertexDeclaration* decl)
  2277. {
  2278. }
  2279. //---------------------------------------------------------------------
  2280. void GLRenderSystem::setVertexBufferBinding(VertexBufferBinding* binding)
  2281. {
  2282. }
  2283. //---------------------------------------------------------------------
  2284. void GLRenderSystem::_render(const RenderOperation& op)
  2285. {
  2286. // Call super class
  2287. RenderSystem::_render(op);
  2288. void* pBufferData = 0;
  2289. bool multitexturing = (getCapabilities()->getNumTextureUnits() > 1);
  2290. const VertexDeclaration::VertexElementList& decl =
  2291. op.vertexData->vertexDeclaration->getElements();
  2292. VertexDeclaration::VertexElementList::const_iterator elem, elemEnd;
  2293. elemEnd = decl.end();
  2294. vector<GLuint>::type attribsBound;
  2295. for (elem = decl.begin(); elem != elemEnd; ++elem)
  2296. {
  2297. if (!op.vertexData->vertexBufferBinding->isBufferBound(elem->getSource()))
  2298. continue; // skip unbound elements
  2299. HardwareVertexBufferPtr vertexBuffer =
  2300. op.vertexData->vertexBufferBinding->getBuffer(elem->getSource());
  2301. if(mCurrentCapabilities->hasCapability(RSC_VBO))
  2302. {
  2303. glBindBufferARB(GL_ARRAY_BUFFER_ARB,
  2304. static_cast<const GLHardwareVertexBuffer*>(vertexBuffer.get())->getGLBufferId());
  2305. pBufferData = VBO_BUFFER_OFFSET(elem->getOffset());
  2306. }
  2307. else
  2308. {
  2309. pBufferData = static_cast<const GLDefaultHardwareVertexBuffer*>(vertexBuffer.get())->getDataPtr(elem->getOffset());
  2310. }
  2311. if (op.vertexData->vertexStart)
  2312. {
  2313. pBufferData = static_cast<char*>(pBufferData) + op.vertexData->vertexStart * vertexBuffer->getVertexSize();
  2314. }
  2315. unsigned int i = 0;
  2316. VertexElementSemantic sem = elem->getSemantic();
  2317. bool isCustomAttrib = false;
  2318. if (mCurrentVertexProgram)
  2319. isCustomAttrib = mCurrentVertexProgram->isAttributeValid(sem, elem->getIndex());
  2320. // Custom attribute support
  2321. // tangents, binormals, blendweights etc always via this route
  2322. // builtins may be done this way too
  2323. if (isCustomAttrib)
  2324. {
  2325. GLint attrib = mCurrentVertexProgram->getAttributeIndex(sem, elem->getIndex());
  2326. unsigned short typeCount = VertexElement::getTypeCount(elem->getType());
  2327. GLboolean normalised = GL_FALSE;
  2328. switch(elem->getType())
  2329. {
  2330. case VET_COLOUR:
  2331. case VET_COLOUR_ABGR:
  2332. case VET_COLOUR_ARGB:
  2333. // Because GL takes these as a sequence of single unsigned bytes, count needs to be 4
  2334. // VertexElement::getTypeCount treats them as 1 (RGBA)
  2335. // Also need to normalise the fixed-point data
  2336. typeCount = 4;
  2337. normalised = GL_TRUE;
  2338. break;
  2339. default:
  2340. break;
  2341. };
  2342. glVertexAttribPointerARB(
  2343. attrib,
  2344. typeCount,
  2345. GLHardwareBufferManager::getGLType(elem->getType()),
  2346. normalised,
  2347. static_cast<GLsizei>(vertexBuffer->getVertexSize()),
  2348. pBufferData);
  2349. glEnableVertexAttribArrayARB(attrib);
  2350. attribsBound.push_back(attrib);
  2351. }
  2352. else
  2353. {
  2354. // fixed-function & builtin attribute support
  2355. switch(sem)
  2356. {
  2357. case VES_POSITION:
  2358. glVertexPointer(VertexElement::getTypeCount(
  2359. elem->getType()),
  2360. GLHardwareBufferManager::getGLType(elem->getType()),
  2361. static_cast<GLsizei>(vertexBuffer->getVertexSize()),
  2362. pBufferData);
  2363. glEnableClientState( GL_VERTEX_ARRAY );
  2364. break;
  2365. case VES_NORMAL:
  2366. glNormalPointer(
  2367. GLHardwareBufferManager::getGLType(elem->getType()),
  2368. static_cast<GLsizei>(vertexBuffer->getVertexSize()),
  2369. pBufferData);
  2370. glEnableClientState( GL_NORMAL_ARRAY );
  2371. break;
  2372. case VES_DIFFUSE:
  2373. glColorPointer(4,
  2374. GLHardwareBufferManager::getGLType(elem->getType()),
  2375. static_cast<GLsizei>(vertexBuffer->getVertexSize()),
  2376. pBufferData);
  2377. glEnableClientState( GL_COLOR_ARRAY );
  2378. break;
  2379. case VES_SPECULAR:
  2380. if (GLEW_EXT_secondary_color)
  2381. {
  2382. glSecondaryColorPointerEXT(4,
  2383. GLHardwareBufferManager::getGLType(elem->getType()),
  2384. static_cast<GLsizei>(vertexBuffer->getVertexSize()),
  2385. pBufferData);
  2386. glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
  2387. }
  2388. break;
  2389. case VES_TEXTURE_COORDINATES:
  2390. if (mCurrentVertexProgram)
  2391. {
  2392. // Programmable pipeline - direct UV assignment
  2393. glClientActiveTextureARB(GL_TEXTURE0 + elem->getIndex());
  2394. glTexCoordPointer(
  2395. VertexElement::getTypeCount(elem->getType()),
  2396. GLHardwareBufferManager::getGLType(elem->getType()),
  2397. static_cast<GLsizei>(vertexBuffer->getVertexSize()),
  2398. pBufferData);
  2399. glEnableClientState( GL_TEXTURE_COORD_ARRAY );
  2400. }
  2401. else
  2402. {
  2403. // fixed function matching to units based on tex_coord_set
  2404. for (i = 0; i < mDisabledTexUnitsFrom; i++)
  2405. {
  2406. // Only set this texture unit's texcoord pointer if it
  2407. // is supposed to be using this element's index
  2408. if (mTextureCoordIndex[i] == elem->getIndex() && i < mFixedFunctionTextureUnits)
  2409. {
  2410. if (multitexturing)
  2411. glClientActiveTextureARB(GL_TEXTURE0 + i);
  2412. glTexCoordPointer(
  2413. VertexElement::getTypeCount(elem->getType()),
  2414. GLHardwareBufferManager::getGLType(elem->getType()),
  2415. static_cast<GLsizei>(vertexBuffer->getVertexSize()),
  2416. pBufferData);
  2417. glEnableClientState( GL_TEXTURE_COORD_ARRAY );
  2418. }
  2419. }
  2420. }
  2421. break;
  2422. default:
  2423. break;
  2424. };
  2425. } // isCustomAttrib
  2426. }
  2427. if (multitexturing)
  2428. glClientActiveTextureARB(GL_TEXTURE0);
  2429. // Find the correct type to render
  2430. GLint primType;
  2431. //Use adjacency if there is a geometry program and it requested adjacency info
  2432. bool useAdjacency = (mGeometryProgramBound && mCurrentGeometryProgram->isAdjacencyInfoRequired());
  2433. switch (op.operationType)
  2434. {
  2435. case RenderOperation::OT_POINT_LIST:
  2436. primType = GL_POINTS;
  2437. break;
  2438. case RenderOperation::OT_LINE_LIST:
  2439. primType = useAdjacency ? GL_LINES_ADJACENCY_EXT : GL_LINES;
  2440. break;
  2441. case RenderOperation::OT_LINE_STRIP:
  2442. primType = useAdjacency ? GL_LINE_STRIP_ADJACENCY_EXT : GL_LINE_STRIP;
  2443. break;
  2444. default:
  2445. case RenderOperation::OT_TRIANGLE_LIST:
  2446. primType = useAdjacency ? GL_TRIANGLES_ADJACENCY_EXT : GL_TRIANGLES;
  2447. break;
  2448. case RenderOperation::OT_TRIANGLE_STRIP:
  2449. primType = useAdjacency ? GL_TRIANGLE_STRIP_ADJACENCY_EXT : GL_TRIANGLE_STRIP;
  2450. break;
  2451. case RenderOperation::OT_TRIANGLE_FAN:
  2452. primType = GL_TRIANGLE_FAN;
  2453. break;
  2454. }
  2455. if (op.useIndexes)
  2456. {
  2457. if(mCurrentCapabilities->hasCapability(RSC_VBO))
  2458. {
  2459. glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
  2460. static_cast<GLHardwareIndexBuffer*>(
  2461. op.indexData->indexBuffer.get())->getGLBufferId());
  2462. pBufferData = VBO_BUFFER_OFFSET(
  2463. op.indexData->indexStart * op.indexData->indexBuffer->getIndexSize());
  2464. }
  2465. else
  2466. {
  2467. pBufferData = static_cast<GLDefaultHardwareIndexBuffer*>(
  2468. op.indexData->indexBuffer.get())->getDataPtr(
  2469. op.indexData->indexStart * op.indexData->indexBuffer->getIndexSize());
  2470. }
  2471. GLenum indexType = (op.indexData->indexBuffer->getType() == HardwareIndexBuffer::IT_16BIT) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
  2472. do
  2473. {
  2474. // Update derived depth bias
  2475. if (mDerivedDepthBias && mCurrentPassIterationNum > 0)
  2476. {
  2477. _setDepthBias(mDerivedDepthBiasBase +
  2478. mDerivedDepthBiasMultiplier * mCurrentPassIterationNum,
  2479. mDerivedDepthBiasSlopeScale);
  2480. }
  2481. glDrawElements(primType, op.indexData->indexCount, indexType, pBufferData);
  2482. } while (updatePassIterationRenderState());
  2483. }
  2484. else
  2485. {
  2486. do
  2487. {
  2488. // Update derived depth bias
  2489. if (mDerivedDepthBias && mCurrentPassIterationNum > 0)
  2490. {
  2491. _setDepthBias(mDerivedDepthBiasBase +
  2492. mDerivedDepthBiasMultiplier * mCurrentPassIterationNum,
  2493. mDerivedDepthBiasSlopeScale);
  2494. }
  2495. glDrawArrays(primType, 0, op.vertexData->vertexCount);
  2496. } while (updatePassIterationRenderState());
  2497. }
  2498. glDisableClientState( GL_VERTEX_ARRAY );
  2499. // only valid up to GL_MAX_TEXTURE_UNITS, which is recorded in mFixedFunctionTextureUnits
  2500. if (multitexturing)
  2501. {
  2502. for (int i = 0; i < mFixedFunctionTextureUnits; i++)
  2503. {
  2504. glClientActiveTextureARB(GL_TEXTURE0 + i);
  2505. glDisableClientState( GL_TEXTURE_COORD_ARRAY );
  2506. }
  2507. glClientActiveTextureARB(GL_TEXTURE0);
  2508. }
  2509. else
  2510. {
  2511. glDisableClientState( GL_TEXTURE_COORD_ARRAY );
  2512. }
  2513. glDisableClientState( GL_NORMAL_ARRAY );
  2514. glDisableClientState( GL_COLOR_ARRAY );
  2515. if (GLEW_EXT_secondary_color)
  2516. {
  2517. glDisableClientState( GL_SECONDARY_COLOR_ARRAY );
  2518. }
  2519. // unbind any custom attributes
  2520. for (vector<GLuint>::type::iterator ai = attribsBound.begin(); ai != attribsBound.end(); ++ai)
  2521. {
  2522. glDisableVertexAttribArrayARB(*ai);
  2523. }
  2524. glColor4f(1,1,1,1);
  2525. if (GLEW_EXT_secondary_color)
  2526. {
  2527. glSecondaryColor3fEXT(0.0f, 0.0f, 0.0f);
  2528. }
  2529. }
  2530. //---------------------------------------------------------------------
  2531. void GLRenderSystem::setNormaliseNormals(bool normalise)
  2532. {
  2533. if (normalise)
  2534. glEnable(GL_NORMALIZE);
  2535. else
  2536. glDisable(GL_NORMALIZE);
  2537. }
  2538. //---------------------------------------------------------------------
  2539. void GLRenderSystem::bindGpuProgram(GpuProgram* prg)
  2540. {
  2541. GLGpuProgram* glprg = static_cast<GLGpuProgram*>(prg);
  2542. // Unbind previous gpu program first.
  2543. //
  2544. // Note:
  2545. // 1. Even if both previous and current are the same object, we can't
  2546. // bypass re-bind completely since the object itself maybe modified.
  2547. // But we can bypass unbind based on the assumption that object
  2548. // internally GL program type shouldn't be changed after it has
  2549. // been created. The behavior of bind to a GL program type twice
  2550. // should be same as unbind and rebind that GL program type, even
  2551. // for difference objects.
  2552. // 2. We also assumed that the program's type (vertex or fragment) should
  2553. // not be changed during it's in using. If not, the following switch
  2554. // statement will confuse GL state completely, and we can't fix it
  2555. // here. To fix this case, we must coding the program implementation
  2556. // itself, if type is changing (during load/unload, etc), and it's inuse,
  2557. // unbind and notify render system to correct for its state.
  2558. //
  2559. switch (glprg->getType())
  2560. {
  2561. case GPT_VERTEX_PROGRAM:
  2562. if (mCurrentVertexProgram != glprg)
  2563. {
  2564. if (mCurrentVertexProgram)
  2565. mCurrentVertexProgram->unbindProgram();
  2566. mCurrentVertexProgram = glprg;
  2567. }
  2568. break;
  2569. case GPT_FRAGMENT_PROGRAM:
  2570. if (mCurrentFragmentProgram != glprg)
  2571. {
  2572. if (mCurrentFragmentProgram)
  2573. mCurrentFragmentProgram->unbindProgram();
  2574. mCurrentFragmentProgram = glprg;
  2575. }
  2576. break;
  2577. case GPT_GEOMETRY_PROGRAM:
  2578. if (mCurrentGeometryProgram != glprg)
  2579. {
  2580. if (mCurrentGeometryProgram)
  2581. mCurrentGeometryProgram->unbindProgram();
  2582. mCurrentGeometryProgram = glprg;
  2583. }
  2584. break;
  2585. }
  2586. // Bind the program
  2587. glprg->bindProgram();
  2588. RenderSystem::bindGpuProgram(prg);
  2589. }
  2590. //---------------------------------------------------------------------
  2591. void GLRenderSystem::unbindGpuProgram(GpuProgramType gptype)
  2592. {
  2593. if (gptype == GPT_VERTEX_PROGRAM && mCurrentVertexProgram)
  2594. {
  2595. mActiveVertexGpuProgramParameters = nullptr;
  2596. mCurrentVertexProgram->unbindProgram();
  2597. mCurrentVertexProgram = 0;
  2598. }
  2599. else if (gptype == GPT_GEOMETRY_PROGRAM && mCurrentGeometryProgram)
  2600. {
  2601. mActiveGeometryGpuProgramParameters = nullptr;
  2602. mCurrentGeometryProgram->unbindProgram();
  2603. mCurrentGeometryProgram = 0;
  2604. }
  2605. else if (gptype == GPT_FRAGMENT_PROGRAM && mCurrentFragmentProgram)
  2606. {
  2607. mActiveFragmentGpuProgramParameters = nullptr;
  2608. mCurrentFragmentProgram->unbindProgram();
  2609. mCurrentFragmentProgram = 0;
  2610. }
  2611. RenderSystem::unbindGpuProgram(gptype);
  2612. }
  2613. //---------------------------------------------------------------------
  2614. void GLRenderSystem::bindGpuProgramParameters(GpuProgramType gptype, GpuProgramParametersSharedPtr params, uint16 mask)
  2615. {
  2616. if (mask & (uint16)GPV_GLOBAL)
  2617. {
  2618. // We could maybe use GL_EXT_bindable_uniform here to produce Dx10-style
  2619. // shared constant buffers, but GPU support seems fairly weak?
  2620. // for now, just copy
  2621. params->_copySharedParams();
  2622. }
  2623. switch (gptype)
  2624. {
  2625. case GPT_VERTEX_PROGRAM:
  2626. mActiveVertexGpuProgramParameters = params;
  2627. mCurrentVertexProgram->bindProgramParameters(params, mask);
  2628. break;
  2629. case GPT_GEOMETRY_PROGRAM:
  2630. mActiveGeometryGpuProgramParameters = params;
  2631. mCurrentGeometryProgram->bindProgramParameters(params, mask);
  2632. break;
  2633. case GPT_FRAGMENT_PROGRAM:
  2634. mActiveFragmentGpuProgramParameters = params;
  2635. mCurrentFragmentProgram->bindProgramParameters(params, mask);
  2636. break;
  2637. }
  2638. }
  2639. //---------------------------------------------------------------------
  2640. void GLRenderSystem::bindGpuProgramPassIterationParameters(GpuProgramType gptype)
  2641. {
  2642. switch (gptype)
  2643. {
  2644. case GPT_VERTEX_PROGRAM:
  2645. mCurrentVertexProgram->bindProgramPassIterationParameters(mActiveVertexGpuProgramParameters);
  2646. break;
  2647. case GPT_GEOMETRY_PROGRAM:
  2648. mCurrentGeometryProgram->bindProgramPassIterationParameters(mActiveGeometryGpuProgramParameters);
  2649. break;
  2650. case GPT_FRAGMENT_PROGRAM:
  2651. mCurrentFragmentProgram->bindProgramPassIterationParameters(mActiveFragmentGpuProgramParameters);
  2652. break;
  2653. }
  2654. }
  2655. //---------------------------------------------------------------------
  2656. void GLRenderSystem::setClipPlanesImpl(const PlaneList& clipPlanes)
  2657. {
  2658. // A note on GL user clipping:
  2659. // When an ARB vertex program is enabled in GL, user clipping is completely
  2660. // disabled. There is no way around this, it's just turned off.
  2661. // When using GLSL, user clipping can work but you have to include a
  2662. // glClipVertex command in your vertex shader.
  2663. // Thus the planes set here may not actually be respected.
  2664. size_t i = 0;
  2665. size_t numClipPlanes;
  2666. GLdouble clipPlane[4];
  2667. // Save previous modelview
  2668. glMatrixMode(GL_MODELVIEW);
  2669. glPushMatrix();
  2670. // just load view matrix (identity world)
  2671. GLfloat mat[16];
  2672. makeGLMatrix(mat, mViewMatrix);
  2673. glLoadMatrixf(mat);
  2674. numClipPlanes = clipPlanes.size();
  2675. for (i = 0; i < numClipPlanes; ++i)
  2676. {
  2677. GLenum clipPlaneId = static_cast<GLenum>(GL_CLIP_PLANE0 + i);
  2678. const Plane& plane = clipPlanes[i];
  2679. if (i >= 6/*GL_MAX_CLIP_PLANES*/)
  2680. {
  2681. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Unable to set clip plane",
  2682. "GLRenderSystem::setClipPlanes");
  2683. }
  2684. clipPlane[0] = plane.normal.x;
  2685. clipPlane[1] = plane.normal.y;
  2686. clipPlane[2] = plane.normal.z;
  2687. clipPlane[3] = plane.d;
  2688. glClipPlane(clipPlaneId, clipPlane);
  2689. glEnable(clipPlaneId);
  2690. }
  2691. // disable remaining clip planes
  2692. for ( ; i < 6/*GL_MAX_CLIP_PLANES*/; ++i)
  2693. {
  2694. glDisable(static_cast<GLenum>(GL_CLIP_PLANE0 + i));
  2695. }
  2696. // restore matrices
  2697. glPopMatrix();
  2698. }
  2699. //---------------------------------------------------------------------
  2700. void GLRenderSystem::setScissorTest(bool enabled, size_t left,
  2701. size_t top, size_t right, size_t bottom)
  2702. {
  2703. // If request texture flipping, use "upper-left", otherwise use "lower-left"
  2704. bool flipping = mActiveRenderTarget->requiresTextureFlipping();
  2705. // GL measures from the bottom, not the top
  2706. size_t targetHeight = mActiveRenderTarget->getHeight();
  2707. // Calculate the "lower-left" corner of the viewport
  2708. GLsizei x = 0, y = 0, w = 0, h = 0;
  2709. if (enabled)
  2710. {
  2711. glEnable(GL_SCISSOR_TEST);
  2712. // NB GL uses width / height rather than right / bottom
  2713. x = left;
  2714. if (flipping)
  2715. y = top;
  2716. else
  2717. y = targetHeight - bottom;
  2718. w = right - left;
  2719. h = bottom - top;
  2720. glScissor(x, y, w, h);
  2721. }
  2722. else
  2723. {
  2724. glDisable(GL_SCISSOR_TEST);
  2725. // GL requires you to reset the scissor when disabling
  2726. w = mActiveViewport->getActualWidth();
  2727. h = mActiveViewport->getActualHeight();
  2728. x = mActiveViewport->getActualLeft();
  2729. if (flipping)
  2730. y = mActiveViewport->getActualTop();
  2731. else
  2732. y = targetHeight - mActiveViewport->getActualTop() - h;
  2733. glScissor(x, y, w, h);
  2734. }
  2735. }
  2736. //---------------------------------------------------------------------
  2737. void GLRenderSystem::clearFrameBuffer(unsigned int buffers,
  2738. const ColourValue& colour, Real depth, unsigned short stencil)
  2739. {
  2740. bool colourMask = !mColourWrite[0] || !mColourWrite[1]
  2741. || !mColourWrite[2] || !mColourWrite[3];
  2742. GLbitfield flags = 0;
  2743. if (buffers & FBT_COLOUR)
  2744. {
  2745. flags |= GL_COLOR_BUFFER_BIT;
  2746. // Enable buffer for writing if it isn't
  2747. if (colourMask)
  2748. {
  2749. glColorMask(true, true, true, true);
  2750. }
  2751. glClearColor(colour.r, colour.g, colour.b, colour.a);
  2752. }
  2753. if (buffers & FBT_DEPTH)
  2754. {
  2755. flags |= GL_DEPTH_BUFFER_BIT;
  2756. // Enable buffer for writing if it isn't
  2757. if (!mDepthWrite)
  2758. {
  2759. glDepthMask( GL_TRUE );
  2760. }
  2761. glClearDepth(depth);
  2762. }
  2763. if (buffers & FBT_STENCIL)
  2764. {
  2765. flags |= GL_STENCIL_BUFFER_BIT;
  2766. // Enable buffer for writing if it isn't
  2767. glStencilMask(0xFFFFFFFF);
  2768. glClearStencil(stencil);
  2769. }
  2770. // Should be enable scissor test due the clear region is
  2771. // relied on scissor box bounds.
  2772. GLboolean scissorTestEnabled = glIsEnabled(GL_SCISSOR_TEST);
  2773. if (!scissorTestEnabled)
  2774. {
  2775. glEnable(GL_SCISSOR_TEST);
  2776. }
  2777. // Sets the scissor box as same as viewport
  2778. GLint viewport[4] = { 0, 0, 0, 0 };
  2779. GLint scissor[4] = { 0, 0, 0, 0 };
  2780. glGetIntegerv(GL_VIEWPORT, viewport);
  2781. glGetIntegerv(GL_SCISSOR_BOX, scissor);
  2782. bool scissorBoxDifference =
  2783. viewport[0] != scissor[0] || viewport[1] != scissor[1] ||
  2784. viewport[2] != scissor[2] || viewport[3] != scissor[3];
  2785. if (scissorBoxDifference)
  2786. {
  2787. glScissor(viewport[0], viewport[1], viewport[2], viewport[3]);
  2788. }
  2789. // Clear buffers
  2790. glClear(flags);
  2791. // Restore scissor box
  2792. if (scissorBoxDifference)
  2793. {
  2794. glScissor(scissor[0], scissor[1], scissor[2], scissor[3]);
  2795. }
  2796. // Restore scissor test
  2797. if (!scissorTestEnabled)
  2798. {
  2799. glDisable(GL_SCISSOR_TEST);
  2800. }
  2801. // Reset buffer write state
  2802. if (!mDepthWrite && (buffers & FBT_DEPTH))
  2803. {
  2804. glDepthMask( GL_FALSE );
  2805. }
  2806. if (colourMask && (buffers & FBT_COLOUR))
  2807. {
  2808. glColorMask(mColourWrite[0], mColourWrite[1], mColourWrite[2], mColourWrite[3]);
  2809. }
  2810. if (buffers & FBT_STENCIL)
  2811. {
  2812. glStencilMask(mStencilMask);
  2813. }
  2814. }
  2815. // ------------------------------------------------------------------
  2816. void GLRenderSystem::_makeProjectionMatrix(Real left, Real right,
  2817. Real bottom, Real top, Real nearPlane, Real farPlane, Matrix4& dest,
  2818. bool forGpuProgram)
  2819. {
  2820. Real width = right - left;
  2821. Real height = top - bottom;
  2822. Real q, qn;
  2823. if (farPlane == 0)
  2824. {
  2825. // Infinite far plane
  2826. q = Frustum::INFINITE_FAR_PLANE_ADJUST - 1;
  2827. qn = nearPlane * (Frustum::INFINITE_FAR_PLANE_ADJUST - 2);
  2828. }
  2829. else
  2830. {
  2831. q = -(farPlane + nearPlane) / (farPlane - nearPlane);
  2832. qn = -2 * (farPlane * nearPlane) / (farPlane - nearPlane);
  2833. }
  2834. dest = Matrix4::ZERO;
  2835. dest[0][0] = 2 * nearPlane / width;
  2836. dest[0][2] = (right+left) / width;
  2837. dest[1][1] = 2 * nearPlane / height;
  2838. dest[1][2] = (top+bottom) / height;
  2839. dest[2][2] = q;
  2840. dest[2][3] = qn;
  2841. dest[3][2] = -1;
  2842. }
  2843. //---------------------------------------------------------------------
  2844. HardwareOcclusionQuery* GLRenderSystem::createHardwareOcclusionQuery(void)
  2845. {
  2846. GLHardwareOcclusionQuery* ret = new GLHardwareOcclusionQuery();
  2847. mHwOcclusionQueries.push_back(ret);
  2848. return ret;
  2849. }
  2850. //---------------------------------------------------------------------
  2851. Real GLRenderSystem::getHorizontalTexelOffset(void)
  2852. {
  2853. // No offset in GL
  2854. return 0.0f;
  2855. }
  2856. //---------------------------------------------------------------------
  2857. Real GLRenderSystem::getVerticalTexelOffset(void)
  2858. {
  2859. // No offset in GL
  2860. return 0.0f;
  2861. }
  2862. //---------------------------------------------------------------------
  2863. void GLRenderSystem::_applyObliqueDepthProjection(Matrix4& matrix, const Plane& plane,
  2864. bool forGpuProgram)
  2865. {
  2866. // Thanks to Eric Lenyel for posting this calculation at www.terathon.com
  2867. // Calculate the clip-space corner point opposite the clipping plane
  2868. // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
  2869. // transform it into camera space by multiplying it
  2870. // by the inverse of the projection matrix
  2871. Vector4 q;
  2872. q.x = (Math::Sign(plane.normal.x) + matrix[0][2]) / matrix[0][0];
  2873. q.y = (Math::Sign(plane.normal.y) + matrix[1][2]) / matrix[1][1];
  2874. q.z = -1.0F;
  2875. q.w = (1.0F + matrix[2][2]) / matrix[2][3];
  2876. // Calculate the scaled plane vector
  2877. Vector4 clipPlane4d(plane.normal.x, plane.normal.y, plane.normal.z, plane.d);
  2878. Vector4 c = clipPlane4d * (2.0F / (clipPlane4d.dotProduct(q)));
  2879. // Replace the third row of the projection matrix
  2880. matrix[2][0] = c.x;
  2881. matrix[2][1] = c.y;
  2882. matrix[2][2] = c.z + 1.0F;
  2883. matrix[2][3] = c.w;
  2884. }
  2885. //---------------------------------------------------------------------
  2886. void GLRenderSystem::_oneTimeContextInitialization()
  2887. {
  2888. if (GLEW_VERSION_1_2)
  2889. {
  2890. // Set nicer lighting model -- d3d9 has this by default
  2891. glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
  2892. glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
  2893. }
  2894. if (GLEW_VERSION_1_4)
  2895. {
  2896. glEnable(GL_COLOR_SUM);
  2897. glDisable(GL_DITHER);
  2898. }
  2899. // Check for FSAA
  2900. // Enable the extension if it was enabled by the GLSupport
  2901. if (mGLSupport->checkExtension("GL_ARB_multisample"))
  2902. {
  2903. int fsaa_active = false;
  2904. glGetIntegerv(GL_SAMPLE_BUFFERS_ARB,(GLint*)&fsaa_active);
  2905. if(fsaa_active)
  2906. {
  2907. glEnable(GL_MULTISAMPLE_ARB);
  2908. }
  2909. }
  2910. }
  2911. //---------------------------------------------------------------------
  2912. void GLRenderSystem::_switchContext(GLContext *context)
  2913. {
  2914. // Unbind GPU programs and rebind to new context later, because
  2915. // scene manager treat render system as ONE 'context' ONLY, and it
  2916. // cached the GPU programs using state.
  2917. if (mCurrentVertexProgram)
  2918. mCurrentVertexProgram->unbindProgram();
  2919. if (mCurrentGeometryProgram)
  2920. mCurrentGeometryProgram->unbindProgram();
  2921. if (mCurrentFragmentProgram)
  2922. mCurrentFragmentProgram->unbindProgram();
  2923. // Disable lights
  2924. for (unsigned short i = 0; i < mCurrentLights; ++i)
  2925. {
  2926. mLights[i] = NULL;
  2927. }
  2928. mCurrentLights = 0;
  2929. // Disable textures
  2930. _disableTextureUnitsFrom(0);
  2931. // It's ready for switching
  2932. if (mCurrentContext)
  2933. mCurrentContext->endCurrent();
  2934. mCurrentContext = context;
  2935. mCurrentContext->setCurrent();
  2936. // Check if the context has already done one-time initialisation
  2937. if(!mCurrentContext->getInitialized())
  2938. {
  2939. _oneTimeContextInitialization();
  2940. mCurrentContext->setInitialized();
  2941. }
  2942. // Rebind GPU programs to new context
  2943. if (mCurrentVertexProgram)
  2944. mCurrentVertexProgram->bindProgram();
  2945. if (mCurrentGeometryProgram)
  2946. mCurrentGeometryProgram->bindProgram();
  2947. if (mCurrentFragmentProgram)
  2948. mCurrentFragmentProgram->bindProgram();
  2949. // Must reset depth/colour write mask to according with user desired, otherwise,
  2950. // clearFrameBuffer would be wrong because the value we are recorded may be
  2951. // difference with the really state stored in GL context.
  2952. glDepthMask(mDepthWrite);
  2953. glColorMask(mColourWrite[0], mColourWrite[1], mColourWrite[2], mColourWrite[3]);
  2954. glStencilMask(mStencilMask);
  2955. }
  2956. //---------------------------------------------------------------------
  2957. void GLRenderSystem::_setRenderTarget(RenderTarget *target)
  2958. {
  2959. // Unbind frame buffer object
  2960. if(mActiveRenderTarget)
  2961. mRTTManager->unbind(mActiveRenderTarget);
  2962. mActiveRenderTarget = target;
  2963. // Switch context if different from current one
  2964. GLContext *newContext = 0;
  2965. target->getCustomAttribute("GLCONTEXT", &newContext);
  2966. if(newContext && mCurrentContext != newContext)
  2967. {
  2968. _switchContext(newContext);
  2969. }
  2970. // Bind frame buffer object
  2971. mRTTManager->bind(target);
  2972. if (GLEW_EXT_framebuffer_sRGB)
  2973. {
  2974. // Enable / disable sRGB states
  2975. if (target->isHardwareGammaEnabled())
  2976. {
  2977. glEnable(GL_FRAMEBUFFER_SRGB_EXT);
  2978. // Note: could test GL_FRAMEBUFFER_SRGB_CAPABLE_EXT here before
  2979. // enabling, but GL spec says incapable surfaces ignore the setting
  2980. // anyway. We test the capability to enable isHardwareGammaEnabled.
  2981. }
  2982. else
  2983. {
  2984. glDisable(GL_FRAMEBUFFER_SRGB_EXT);
  2985. }
  2986. }
  2987. }
  2988. //---------------------------------------------------------------------
  2989. void GLRenderSystem::_unregisterContext(GLContext *context)
  2990. {
  2991. if(mCurrentContext == context) {
  2992. // Change the context to something else so that a valid context
  2993. // remains active. When this is the main context being unregistered,
  2994. // we set the main context to 0.
  2995. if(mCurrentContext != mMainContext) {
  2996. _switchContext(mMainContext);
  2997. } else {
  2998. /// No contexts remain
  2999. mCurrentContext->endCurrent();
  3000. mCurrentContext = 0;
  3001. mMainContext = 0;
  3002. }
  3003. }
  3004. }
  3005. //---------------------------------------------------------------------
  3006. Real GLRenderSystem::getMinimumDepthInputValue(void)
  3007. {
  3008. // Range [-1.0f, 1.0f]
  3009. return -1.0f;
  3010. }
  3011. //---------------------------------------------------------------------
  3012. Real GLRenderSystem::getMaximumDepthInputValue(void)
  3013. {
  3014. // Range [-1.0f, 1.0f]
  3015. return 1.0f;
  3016. }
  3017. //---------------------------------------------------------------------
  3018. void GLRenderSystem::registerThread()
  3019. {
  3020. CM_LOCK_MUTEX(mThreadInitMutex)
  3021. // This is only valid once we've created the main context
  3022. if (!mMainContext)
  3023. {
  3024. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
  3025. "Cannot register a background thread before the main context "
  3026. "has been created.",
  3027. "GLRenderSystem::registerThread");
  3028. }
  3029. // Create a new context for this thread. Cloning from the main context
  3030. // will ensure that resources are shared with the main context
  3031. // We want a separate context so that we can safely create GL
  3032. // objects in parallel with the main thread
  3033. GLContext* newContext = mMainContext->clone();
  3034. mBackgroundContextList.push_back(newContext);
  3035. // Bind this new context to this thread.
  3036. newContext->setCurrent();
  3037. _oneTimeContextInitialization();
  3038. newContext->setInitialized();
  3039. }
  3040. //---------------------------------------------------------------------
  3041. void GLRenderSystem::unregisterThread()
  3042. {
  3043. // nothing to do here?
  3044. // Don't need to worry about active context, just make sure we delete
  3045. // on shutdown.
  3046. }
  3047. //---------------------------------------------------------------------
  3048. void GLRenderSystem::preExtraThreadsStarted()
  3049. {
  3050. CM_LOCK_MUTEX(mThreadInitMutex)
  3051. // free context, we'll need this to share lists
  3052. mCurrentContext->endCurrent();
  3053. }
  3054. //---------------------------------------------------------------------
  3055. void GLRenderSystem::postExtraThreadsStarted()
  3056. {
  3057. CM_LOCK_MUTEX(mThreadInitMutex)
  3058. // reacquire context
  3059. mCurrentContext->setCurrent();
  3060. }
  3061. //---------------------------------------------------------------------
  3062. bool GLRenderSystem::activateGLTextureUnit(size_t unit)
  3063. {
  3064. if (mActiveTextureUnit != unit)
  3065. {
  3066. if (GLEW_VERSION_1_2 && unit < getCapabilities()->getNumTextureUnits())
  3067. {
  3068. glActiveTextureARB(GL_TEXTURE0 + unit);
  3069. mActiveTextureUnit = unit;
  3070. return true;
  3071. }
  3072. else if (!unit)
  3073. {
  3074. // always ok to use the first unit
  3075. return true;
  3076. }
  3077. else
  3078. {
  3079. return false;
  3080. }
  3081. }
  3082. else
  3083. {
  3084. return true;
  3085. }
  3086. }
  3087. //---------------------------------------------------------------------
  3088. unsigned int GLRenderSystem::getDisplayMonitorCount() const
  3089. {
  3090. return mGLSupport->getDisplayMonitorCount();
  3091. }
  3092. }