CmGLRenderSystem.cpp 99 KB

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