2
0

CmGLRenderSystem.cpp 98 KB

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