CmGLRenderSystem.cpp 99 KB

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