2
0

CmGLRenderSystem.cpp 99 KB

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