| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464 |
- /*
- -----------------------------------------------------------------------------
- This source file is part of OGRE
- (Object-oriented Graphics Rendering Engine)
- For the latest info, see http://www.ogre3d.org
- Copyright (c) 2000-2011 Torus Knot Software Ltd
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.s
- -----------------------------------------------------------------------------
- */
- #include "CmGLRenderSystem.h"
- #include "CmRenderSystem.h"
- #include "CmCamera.h"
- #include "CmGLTextureManager.h"
- #include "CmGLHardwareVertexBuffer.h"
- #include "CmGLHardwareIndexBuffer.h"
- #include "CmGLDefaultHardwareBufferManager.h"
- #include "CmGLUtil.h"
- #include "CmGLGpuProgram.h"
- #include "CmGLGpuNvparseProgram.h"
- #include "ATI_FS_GLGpuProgram.h"
- #include "CmGLGpuProgramManager.h"
- #include "CmException.h"
- #include "CmGLSLExtSupport.h"
- #include "CmGLHardwareOcclusionQuery.h"
- #include "CmGLContext.h"
- #include "CmGLFBORenderTexture.h"
- #include "CmGLPBRenderTexture.h"
- // Convenience macro from ARB_vertex_buffer_object spec
- #define VBO_BUFFER_OFFSET(i) ((char *)NULL + (i))
- #if CM_THREAD_SUPPORT != 1
- GLenum glewContextInit (CamelotEngine::GLSupport *glSupport);
- #endif
- namespace CamelotEngine {
- // Callback function used when registering GLGpuPrograms
- GpuProgram* createGLArbGpuProgram(GpuProgramType gptype, const String& syntaxCode)
- {
- GLArbGpuProgram* ret = new GLArbGpuProgram();
- ret->setType(gptype);
- ret->setSyntaxCode(syntaxCode);
- return ret;
- }
- GpuProgram* createGLGpuNvparseProgram(GpuProgramType gptype, const String& syntaxCode)
- {
- GLGpuNvparseProgram* ret = new GLGpuNvparseProgram();
- ret->setType(gptype);
- ret->setSyntaxCode(syntaxCode);
- return ret;
- }
- GpuProgram* createGL_ATI_FS_GpuProgram(GpuProgramType gptype, const String& syntaxCode)
- {
- ATI_FS_GLGpuProgram* ret = new ATI_FS_GLGpuProgram();
- ret->setType(gptype);
- ret->setSyntaxCode(syntaxCode);
- return ret;
- }
- GLRenderSystem::GLRenderSystem()
- : mDepthWrite(true), mStencilMask(0xFFFFFFFF), mHardwareBufferManager(0),
- mGpuProgramManager(0),
- mGLSLProgramFactory(0),
- mRTTManager(0),
- mActiveTextureUnit(0)
- {
- size_t i;
- // Get our GLSupport
- mGLSupport = getGLSupport();
- for( i=0; i<MAX_LIGHTS; i++ )
- mLights[i] = NULL;
- mWorldMatrix = Matrix4::IDENTITY;
- mViewMatrix = Matrix4::IDENTITY;
- initConfigOptions();
- mColourWrite[0] = mColourWrite[1] = mColourWrite[2] = mColourWrite[3] = true;
- for (i = 0; i < CM_MAX_TEXTURE_LAYERS; i++)
- {
- // Dummy value
- mTextureCoordIndex[i] = 99;
- mTextureTypes[i] = 0;
- }
- mActiveRenderTarget = 0;
- mCurrentContext = 0;
- mMainContext = 0;
- mGLInitialised = false;
- mCurrentLights = 0;
- mMinFilter = FO_LINEAR;
- mMipFilter = FO_POINT;
- mCurrentVertexProgram = 0;
- mCurrentGeometryProgram = 0;
- mCurrentFragmentProgram = 0;
- }
- GLRenderSystem::~GLRenderSystem()
- {
- shutdown();
- // Destroy render windows
- RenderTargetMap::iterator i;
- for (i = mRenderTargets.begin(); i != mRenderTargets.end(); ++i)
- {
- delete i->second;
- }
- mRenderTargets.clear();
- if(mGLSupport)
- delete mGLSupport;
- }
- const String& GLRenderSystem::getName(void) const
- {
- static String strName("OpenGL Rendering Subsystem");
- return strName;
- }
- void GLRenderSystem::initConfigOptions(void)
- {
- mGLSupport->addConfig();
- }
- ConfigOptionMap& GLRenderSystem::getConfigOptions(void)
- {
- return mGLSupport->getConfigOptions();
- }
- void GLRenderSystem::setConfigOption(const String &name, const String &value)
- {
- mGLSupport->setConfigOption(name, value);
- }
- String GLRenderSystem::validateConfigOptions(void)
- {
- // XXX Return an error string if something is invalid
- return mGLSupport->validateConfig();
- }
- RenderWindow* GLRenderSystem::_initialise(bool autoCreateWindow, const String& windowTitle)
- {
- mGLSupport->start();
- RenderWindow* autoWindow = mGLSupport->createWindow(autoCreateWindow, this, windowTitle);
- RenderSystem::_initialise(autoCreateWindow, windowTitle);
- return autoWindow;
- }
- RenderSystemCapabilities* GLRenderSystem::createRenderSystemCapabilities() const
- {
- RenderSystemCapabilities* rsc = new RenderSystemCapabilities();
- rsc->setCategoryRelevant(CAPS_CATEGORY_GL, true);
- rsc->setDriverVersion(mDriverVersion);
- const char* deviceName = (const char*)glGetString(GL_RENDERER);
- const char* vendorName = (const char*)glGetString(GL_VENDOR);
- rsc->setDeviceName(deviceName);
- rsc->setRenderSystemName(getName());
- // determine vendor
- if (strstr(vendorName, "NVIDIA"))
- rsc->setVendor(GPU_NVIDIA);
- else if (strstr(vendorName, "ATI"))
- rsc->setVendor(GPU_ATI);
- else if (strstr(vendorName, "Intel"))
- rsc->setVendor(GPU_INTEL);
- else if (strstr(vendorName, "S3"))
- rsc->setVendor(GPU_S3);
- else if (strstr(vendorName, "Matrox"))
- rsc->setVendor(GPU_MATROX);
- else if (strstr(vendorName, "3DLabs"))
- rsc->setVendor(GPU_3DLABS);
- else if (strstr(vendorName, "SiS"))
- rsc->setVendor(GPU_SIS);
- else
- rsc->setVendor(GPU_UNKNOWN);
- // Supports fixed-function
- rsc->setCapability(RSC_FIXED_FUNCTION);
- // Check for hardware mipmapping support.
- if(GLEW_VERSION_1_4 || GLEW_SGIS_generate_mipmap)
- {
- bool disableAutoMip = false;
- #if CM_PLATFORM == CM_PLATFORM_APPLE || CM_PLATFORM == CM_PLATFORM_LINUX
- // Apple & Linux ATI drivers have faults in hardware mipmap generation
- if (rsc->getVendor() == GPU_ATI)
- disableAutoMip = true;
- #endif
- // The Intel 915G frequently corrupts textures when using hardware mip generation
- // I'm not currently sure how many generations of hardware this affects,
- // so for now, be safe.
- if (rsc->getVendor() == GPU_INTEL)
- disableAutoMip = true;
- // SiS chipsets also seem to have problems with this
- if (rsc->getVendor() == GPU_SIS)
- disableAutoMip = true;
- if (!disableAutoMip)
- rsc->setCapability(RSC_AUTOMIPMAP);
- }
- // Check for blending support
- if(GLEW_VERSION_1_3 ||
- GLEW_ARB_texture_env_combine ||
- GLEW_EXT_texture_env_combine)
- {
- rsc->setCapability(RSC_BLENDING);
- }
- // Check for Multitexturing support and set number of texture units
- if(GLEW_VERSION_1_3 ||
- GLEW_ARB_multitexture)
- {
- GLint units;
- glGetIntegerv( GL_MAX_TEXTURE_UNITS, &units );
- if (GLEW_ARB_fragment_program)
- {
- // Also check GL_MAX_TEXTURE_IMAGE_UNITS_ARB since NV at least
- // only increased this on the FX/6x00 series
- GLint arbUnits;
- glGetIntegerv( GL_MAX_TEXTURE_IMAGE_UNITS_ARB, &arbUnits );
- if (arbUnits > units)
- units = arbUnits;
- }
- rsc->setNumTextureUnits(units);
- }
- else
- {
- // If no multitexture support then set one texture unit
- rsc->setNumTextureUnits(1);
- }
- // Check for Anisotropy support
- if(GLEW_EXT_texture_filter_anisotropic)
- {
- rsc->setCapability(RSC_ANISOTROPY);
- }
- // Check for DOT3 support
- if(GLEW_VERSION_1_3 ||
- GLEW_ARB_texture_env_dot3 ||
- GLEW_EXT_texture_env_dot3)
- {
- rsc->setCapability(RSC_DOT3);
- }
- // Check for cube mapping
- if(GLEW_VERSION_1_3 ||
- GLEW_ARB_texture_cube_map ||
- GLEW_EXT_texture_cube_map)
- {
- rsc->setCapability(RSC_CUBEMAPPING);
- }
- // Point sprites
- if (GLEW_VERSION_2_0 || GLEW_ARB_point_sprite)
- {
- rsc->setCapability(RSC_POINT_SPRITES);
- }
- // Check for point parameters
- if (GLEW_VERSION_1_4)
- {
- rsc->setCapability(RSC_POINT_EXTENDED_PARAMETERS);
- }
- if (GLEW_ARB_point_parameters)
- {
- rsc->setCapability(RSC_POINT_EXTENDED_PARAMETERS_ARB);
- }
- if (GLEW_EXT_point_parameters)
- {
- rsc->setCapability(RSC_POINT_EXTENDED_PARAMETERS_EXT);
- }
- // Check for hardware stencil support and set bit depth
- GLint stencil;
- glGetIntegerv(GL_STENCIL_BITS,&stencil);
- if(stencil)
- {
- rsc->setCapability(RSC_HWSTENCIL);
- rsc->setStencilBufferBitDepth(stencil);
- }
- if(GLEW_VERSION_1_5 || GLEW_ARB_vertex_buffer_object)
- {
- if (!GLEW_ARB_vertex_buffer_object)
- {
- rsc->setCapability(RSC_GL1_5_NOVBO);
- }
- rsc->setCapability(RSC_VBO);
- }
- if(GLEW_ARB_vertex_program)
- {
- rsc->setCapability(RSC_VERTEX_PROGRAM);
- // Vertex Program Properties
- rsc->setVertexProgramConstantBoolCount(0);
- rsc->setVertexProgramConstantIntCount(0);
- GLint floatConstantCount;
- glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB, &floatConstantCount);
- rsc->setVertexProgramConstantFloatCount(floatConstantCount);
- rsc->addShaderProfile("arbvp1");
- if (GLEW_NV_vertex_program2_option)
- {
- rsc->addShaderProfile("vp30");
- }
- if (GLEW_NV_vertex_program3)
- {
- rsc->addShaderProfile("vp40");
- }
- if (GLEW_NV_vertex_program4)
- {
- rsc->addShaderProfile("gp4vp");
- rsc->addShaderProfile("gpu_vp");
- }
- }
- if (GLEW_NV_register_combiners2 &&
- GLEW_NV_texture_shader)
- {
- rsc->setCapability(RSC_FRAGMENT_PROGRAM);
- rsc->addShaderProfile("fp20");
- }
- // NFZ - check for ATI fragment shader support
- if (GLEW_ATI_fragment_shader)
- {
- rsc->setCapability(RSC_FRAGMENT_PROGRAM);
- // no boolean params allowed
- rsc->setFragmentProgramConstantBoolCount(0);
- // no integer params allowed
- rsc->setFragmentProgramConstantIntCount(0);
- // only 8 Vector4 constant floats supported
- rsc->setFragmentProgramConstantFloatCount(8);
- rsc->addShaderProfile("ps_1_4");
- rsc->addShaderProfile("ps_1_3");
- rsc->addShaderProfile("ps_1_2");
- rsc->addShaderProfile("ps_1_1");
- }
- if (GLEW_ARB_fragment_program)
- {
- rsc->setCapability(RSC_FRAGMENT_PROGRAM);
- // Fragment Program Properties
- rsc->setFragmentProgramConstantBoolCount(0);
- rsc->setFragmentProgramConstantIntCount(0);
- GLint floatConstantCount;
- glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB, &floatConstantCount);
- rsc->setFragmentProgramConstantFloatCount(floatConstantCount);
- rsc->addShaderProfile("arbfp1");
- if (GLEW_NV_fragment_program_option)
- {
- rsc->addShaderProfile("fp30");
- }
- if (GLEW_NV_fragment_program2)
- {
- rsc->addShaderProfile("fp40");
- }
- }
- // NFZ - Check if GLSL is supported
- if ( GLEW_VERSION_2_0 ||
- (GLEW_ARB_shading_language_100 &&
- GLEW_ARB_shader_objects &&
- GLEW_ARB_fragment_shader &&
- GLEW_ARB_vertex_shader) )
- {
- rsc->addShaderProfile("glsl");
- }
- // Check if geometry shaders are supported
- if (GLEW_VERSION_2_0 &&
- GLEW_EXT_geometry_shader4)
- {
- rsc->setCapability(RSC_GEOMETRY_PROGRAM);
- rsc->addShaderProfile("nvgp4");
- //Also add the CG profiles
- rsc->addShaderProfile("gpu_gp");
- rsc->addShaderProfile("gp4gp");
- rsc->setGeometryProgramConstantBoolCount(0);
- rsc->setGeometryProgramConstantIntCount(0);
- GLint floatConstantCount = 0;
- glGetIntegerv(GL_MAX_GEOMETRY_UNIFORM_COMPONENTS_EXT, &floatConstantCount);
- rsc->setGeometryProgramConstantFloatCount(floatConstantCount);
- GLint maxOutputVertices;
- glGetIntegerv(GL_MAX_GEOMETRY_OUTPUT_VERTICES_EXT,&maxOutputVertices);
- rsc->setGeometryProgramNumOutputVertices(maxOutputVertices);
- }
-
- //Check if render to vertex buffer (transform feedback in OpenGL)
- if (GLEW_VERSION_2_0 &&
- GLEW_NV_transform_feedback)
- {
- rsc->setCapability(RSC_HWRENDER_TO_VERTEX_BUFFER);
- }
- // Check for texture compression
- if(GLEW_VERSION_1_3 || GLEW_ARB_texture_compression)
- {
- rsc->setCapability(RSC_TEXTURE_COMPRESSION);
-
- // Check for dxt compression
- if(GLEW_EXT_texture_compression_s3tc)
- {
- #if defined(__APPLE__) && defined(__PPC__)
- // Apple on ATI & PPC has errors in DXT
- if (mGLSupport->getGLVendor().find("ATI") == std::string::npos)
- #endif
- rsc->setCapability(RSC_TEXTURE_COMPRESSION_DXT);
- }
- // Check for vtc compression
- if(GLEW_NV_texture_compression_vtc)
- {
- rsc->setCapability(RSC_TEXTURE_COMPRESSION_VTC);
- }
- }
- // Scissor test is standard in GL 1.2 (is it emulated on some cards though?)
- rsc->setCapability(RSC_SCISSOR_TEST);
- // As are user clipping planes
- rsc->setCapability(RSC_USER_CLIP_PLANES);
- // 2-sided stencil?
- if (GLEW_VERSION_2_0 || GLEW_EXT_stencil_two_side)
- {
- rsc->setCapability(RSC_TWO_SIDED_STENCIL);
- }
- // stencil wrapping?
- if (GLEW_VERSION_1_4 || GLEW_EXT_stencil_wrap)
- {
- rsc->setCapability(RSC_STENCIL_WRAP);
- }
- // Check for hardware occlusion support
- if(GLEW_VERSION_1_5 || GLEW_ARB_occlusion_query)
- {
- // Some buggy driver claim that it is GL 1.5 compliant and
- // not support ARB_occlusion_query
- if (!GLEW_ARB_occlusion_query)
- {
- rsc->setCapability(RSC_GL1_5_NOHWOCCLUSION);
- }
- rsc->setCapability(RSC_HWOCCLUSION);
- }
- else if (GLEW_NV_occlusion_query)
- {
- // Support NV extension too for old hardware
- rsc->setCapability(RSC_HWOCCLUSION);
- }
- // UBYTE4 always supported
- rsc->setCapability(RSC_VERTEX_FORMAT_UBYTE4);
- // Infinite far plane always supported
- rsc->setCapability(RSC_INFINITE_FAR_PLANE);
- // Check for non-power-of-2 texture support
- if(GLEW_ARB_texture_non_power_of_two)
- {
- rsc->setCapability(RSC_NON_POWER_OF_2_TEXTURES);
- }
- // Check for Float textures
- if(GLEW_ATI_texture_float || GLEW_ARB_texture_float)
- {
- rsc->setCapability(RSC_TEXTURE_FLOAT);
- }
- // 3D textures should be supported by GL 1.2, which is our minimum version
- rsc->setCapability(RSC_TEXTURE_3D);
- // Check for framebuffer object extension
- if(GLEW_EXT_framebuffer_object)
- {
- // Probe number of draw buffers
- // Only makes sense with FBO support, so probe here
- if(GLEW_VERSION_2_0 ||
- GLEW_ARB_draw_buffers ||
- GLEW_ATI_draw_buffers)
- {
- GLint buffers;
- glGetIntegerv(GL_MAX_DRAW_BUFFERS_ARB, &buffers);
- rsc->setNumMultiRenderTargets(std::min<int>(buffers, (GLint)CM_MAX_MULTIPLE_RENDER_TARGETS));
- rsc->setCapability(RSC_MRT_DIFFERENT_BIT_DEPTHS);
- if(!GLEW_VERSION_2_0)
- {
- // Before GL version 2.0, we need to get one of the extensions
- if(GLEW_ARB_draw_buffers)
- rsc->setCapability(RSC_FBO_ARB);
- if(GLEW_ATI_draw_buffers)
- rsc->setCapability(RSC_FBO_ATI);
- }
- // Set FBO flag for all 3 'subtypes'
- rsc->setCapability(RSC_FBO);
- }
- rsc->setCapability(RSC_HWRENDER_TO_TEXTURE);
- }
- // Check GLSupport for PBuffer support
- if(mGLSupport->supportsPBuffers())
- {
- // Use PBuffers
- rsc->setCapability(RSC_HWRENDER_TO_TEXTURE);
- rsc->setCapability(RSC_PBUFFER);
- }
- // Point size
- if (GLEW_VERSION_1_4)
- {
- float ps;
- glGetFloatv(GL_POINT_SIZE_MAX, &ps);
- rsc->setMaxPointSize(ps);
- }
- else
- {
- GLint vSize[2];
- glGetIntegerv(GL_POINT_SIZE_RANGE,vSize);
- rsc->setMaxPointSize((float)vSize[1]);
- }
- // Vertex texture fetching
- if (mGLSupport->checkExtension("GL_ARB_vertex_shader"))
- {
- GLint vUnits;
- glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS_ARB, &vUnits);
- rsc->setNumVertexTextureUnits(static_cast<UINT16>(vUnits));
- if (vUnits > 0)
- {
- rsc->setCapability(RSC_VERTEX_TEXTURE_FETCH);
- }
- // GL always shares vertex and fragment texture units (for now?)
- rsc->setVertexTextureUnitsShared(true);
- }
- // Mipmap LOD biasing?
- if (GLEW_VERSION_1_4 || GLEW_EXT_texture_lod_bias)
- {
- rsc->setCapability(RSC_MIPMAP_LOD_BIAS);
- }
- // Alpha to coverage?
- if (mGLSupport->checkExtension("GL_ARB_multisample"))
- {
- // Alpha to coverage always 'supported' when MSAA is available
- // although card may ignore it if it doesn't specifically support A2C
- rsc->setCapability(RSC_ALPHA_TO_COVERAGE);
- }
- // Advanced blending operations
- if(GLEW_VERSION_2_0)
- {
- rsc->setCapability(RSC_ADVANCED_BLEND_OPERATIONS);
- }
- return rsc;
- }
- void GLRenderSystem::initialiseFromRenderSystemCapabilities(RenderSystemCapabilities* caps, RenderTarget* primary)
- {
- if(caps->getRenderSystemName() != getName())
- {
- OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
- "Trying to initialize GLRenderSystem from RenderSystemCapabilities that do not support OpenGL",
- "GLRenderSystem::initialiseFromRenderSystemCapabilities");
- }
- // set texture the number of texture units
- mFixedFunctionTextureUnits = caps->getNumTextureUnits();
- //In GL there can be less fixed function texture units than general
- //texture units. Get the minimum of the two.
- if (caps->hasCapability(RSC_FRAGMENT_PROGRAM))
- {
- GLint maxTexCoords = 0;
- glGetIntegerv(GL_MAX_TEXTURE_COORDS_ARB, &maxTexCoords);
- if (mFixedFunctionTextureUnits > maxTexCoords)
- {
- mFixedFunctionTextureUnits = maxTexCoords;
- }
- }
-
- if(caps->hasCapability(RSC_GL1_5_NOVBO))
- {
- // Assign ARB functions same to GL 1.5 version since
- // interface identical
- glBindBufferARB = glBindBuffer;
- glBufferDataARB = glBufferData;
- glBufferSubDataARB = glBufferSubData;
- glDeleteBuffersARB = glDeleteBuffers;
- glGenBuffersARB = glGenBuffers;
- glGetBufferParameterivARB = glGetBufferParameteriv;
- glGetBufferPointervARB = glGetBufferPointerv;
- glGetBufferSubDataARB = glGetBufferSubData;
- glIsBufferARB = glIsBuffer;
- glMapBufferARB = glMapBuffer;
- glUnmapBufferARB = glUnmapBuffer;
- }
- if(caps->hasCapability(RSC_VBO))
- {
- mHardwareBufferManager = new GLHardwareBufferManager;
- }
- else
- {
- mHardwareBufferManager = new GLDefaultHardwareBufferManager;
- }
- // XXX Need to check for nv2 support and make a program manager for it
- // XXX Probably nv1 as well for older cards
- // GPU Program Manager setup
- mGpuProgramManager = new GLGpuProgramManager();
- if(caps->hasCapability(RSC_VERTEX_PROGRAM))
- {
- if(caps->isShaderProfileSupported("arbvp1"))
- {
- mGpuProgramManager->registerProgramFactory("arbvp1", createGLArbGpuProgram);
- }
- if(caps->isShaderProfileSupported("vp30"))
- {
- mGpuProgramManager->registerProgramFactory("vp30", createGLArbGpuProgram);
- }
- if(caps->isShaderProfileSupported("vp40"))
- {
- mGpuProgramManager->registerProgramFactory("vp40", createGLArbGpuProgram);
- }
- if(caps->isShaderProfileSupported("gp4vp"))
- {
- mGpuProgramManager->registerProgramFactory("gp4vp", createGLArbGpuProgram);
- }
- if(caps->isShaderProfileSupported("gpu_vp"))
- {
- mGpuProgramManager->registerProgramFactory("gpu_vp", createGLArbGpuProgram);
- }
- }
- if(caps->hasCapability(RSC_GEOMETRY_PROGRAM))
- {
- //TODO : Should these be createGLArbGpuProgram or createGLGpuNVparseProgram?
- if(caps->isShaderProfileSupported("nvgp4"))
- {
- mGpuProgramManager->registerProgramFactory("nvgp4", createGLArbGpuProgram);
- }
- if(caps->isShaderProfileSupported("gp4gp"))
- {
- mGpuProgramManager->registerProgramFactory("gp4gp", createGLArbGpuProgram);
- }
- if(caps->isShaderProfileSupported("gpu_gp"))
- {
- mGpuProgramManager->registerProgramFactory("gpu_gp", createGLArbGpuProgram);
- }
- }
- if(caps->hasCapability(RSC_FRAGMENT_PROGRAM))
- {
- if(caps->isShaderProfileSupported("fp20"))
- {
- mGpuProgramManager->registerProgramFactory("fp20", createGLGpuNvparseProgram);
- }
- if(caps->isShaderProfileSupported("ps_1_4"))
- {
- mGpuProgramManager->registerProgramFactory("ps_1_4", createGL_ATI_FS_GpuProgram);
- }
- if(caps->isShaderProfileSupported("ps_1_3"))
- {
- mGpuProgramManager->registerProgramFactory("ps_1_3", createGL_ATI_FS_GpuProgram);
- }
- if(caps->isShaderProfileSupported("ps_1_2"))
- {
- mGpuProgramManager->registerProgramFactory("ps_1_2", createGL_ATI_FS_GpuProgram);
- }
- if(caps->isShaderProfileSupported("ps_1_1"))
- {
- mGpuProgramManager->registerProgramFactory("ps_1_1", createGL_ATI_FS_GpuProgram);
- }
- if(caps->isShaderProfileSupported("arbfp1"))
- {
- mGpuProgramManager->registerProgramFactory("arbfp1", createGLArbGpuProgram);
- }
- if(caps->isShaderProfileSupported("fp40"))
- {
- mGpuProgramManager->registerProgramFactory("fp40", createGLArbGpuProgram);
- }
- if(caps->isShaderProfileSupported("fp30"))
- {
- mGpuProgramManager->registerProgramFactory("fp30", createGLArbGpuProgram);
- }
- }
- if(caps->isShaderProfileSupported("glsl"))
- {
- // NFZ - check for GLSL vertex and fragment shader support successful
- mGLSLProgramFactory = new GLSLProgramFactory();
- HighLevelGpuProgramManager::getSingleton().addFactory(mGLSLProgramFactory);
- }
- if(caps->hasCapability(RSC_HWOCCLUSION))
- {
- if(caps->hasCapability(RSC_GL1_5_NOHWOCCLUSION))
- {
- // Assign ARB functions same to GL 1.5 version since
- // interface identical
- glBeginQueryARB = glBeginQuery;
- glDeleteQueriesARB = glDeleteQueries;
- glEndQueryARB = glEndQuery;
- glGenQueriesARB = glGenQueries;
- glGetQueryObjectivARB = glGetQueryObjectiv;
- glGetQueryObjectuivARB = glGetQueryObjectuiv;
- glGetQueryivARB = glGetQueryiv;
- glIsQueryARB = glIsQuery;
- }
- }
- /// Do this after extension function pointers are initialised as the extension
- /// is used to probe further capabilities.
- ConfigOptionMap::iterator cfi = getConfigOptions().find("RTT Preferred Mode");
- // RTT Mode: 0 use whatever available, 1 use PBuffers, 2 force use copying
- int rttMode = 0;
- if (cfi != getConfigOptions().end())
- {
- if (cfi->second.currentValue == "PBuffer")
- {
- rttMode = 1;
- }
- else if (cfi->second.currentValue == "Copy")
- {
- rttMode = 2;
- }
- }
- // Check for framebuffer object extension
- if(caps->hasCapability(RSC_FBO) && rttMode < 1)
- {
- // Before GL version 2.0, we need to get one of the extensions
- if(caps->hasCapability(RSC_FBO_ARB))
- GLEW_GET_FUN(__glewDrawBuffers) = glDrawBuffersARB;
- else if(caps->hasCapability(RSC_FBO_ATI))
- GLEW_GET_FUN(__glewDrawBuffers) = glDrawBuffersATI;
- if(caps->hasCapability(RSC_HWRENDER_TO_TEXTURE))
- {
- // Create FBO manager
- // TODO LOG PORT - Log this somewhere?
- //LogManager::getSingleton().logMessage("GL: Using GL_EXT_framebuffer_object for rendering to textures (best)");
- mRTTManager = new GLFBOManager(false);
- }
- }
- else
- {
- // Check GLSupport for PBuffer support
- if(caps->hasCapability(RSC_PBUFFER) && rttMode < 2)
- {
- if(caps->hasCapability(RSC_HWRENDER_TO_TEXTURE))
- {
- // Use PBuffers
- mRTTManager = new GLPBRTTManager(mGLSupport, primary);
- // TODO LOG PORT - Log this somewhere?
- //LogManager::getSingleton().logMessage("GL: Using PBuffers for rendering to textures");
- }
- }
- else
- {
- // No pbuffer support either -- fallback to simplest copying from framebuffer
- mRTTManager = new GLCopyingRTTManager();
- // TODO LOG PORT - Log this somewhere?
- //LogManager::getSingleton().logMessage("GL: Using framebuffer copy for rendering to textures (worst)");
- //LogManager::getSingleton().logMessage("GL: Warning: RenderTexture size is restricted to size of framebuffer. If you are on Linux, consider using GLX instead of SDL.");
- }
- // Downgrade number of simultaneous targets
- caps->setNumMultiRenderTargets(1);
- }
- /// Create the texture manager
- mTextureManager = new GLTextureManager(*mGLSupport);
- mGLInitialised = true;
- }
- void GLRenderSystem::reinitialise(void)
- {
- this->shutdown();
- this->_initialise(true);
- }
- void GLRenderSystem::shutdown(void)
- {
- RenderSystem::shutdown();
- // Deleting the GLSL program factory
- if (mGLSLProgramFactory)
- {
- // Remove from manager safely
- if (HighLevelGpuProgramManager::getSingletonPtr())
- HighLevelGpuProgramManager::getSingleton().removeFactory(mGLSLProgramFactory);
- delete mGLSLProgramFactory;
- mGLSLProgramFactory = 0;
- }
- // Deleting the GPU program manager and hardware buffer manager. Has to be done before the mGLSupport->stop().
- delete mGpuProgramManager;
- mGpuProgramManager = 0;
- delete mHardwareBufferManager;
- mHardwareBufferManager = 0;
- delete mRTTManager;
- mRTTManager = 0;
- // Delete extra threads contexts
- for (GLContextList::iterator i = mBackgroundContextList.begin();
- i != mBackgroundContextList.end(); ++i)
- {
- GLContext* pCurContext = *i;
- pCurContext->releaseContext();
- delete pCurContext;
- }
- mBackgroundContextList.clear();
- mGLSupport->stop();
- mStopRendering = true;
- delete mTextureManager;
- mTextureManager = 0;
- // There will be a new initial window and so forth, thus any call to test
- // some params will access an invalid pointer, so it is best to reset
- // the whole state.
- mGLInitialised = 0;
- }
- void GLRenderSystem::setAmbientLight(float r, float g, float b)
- {
- GLfloat lmodel_ambient[] = {r, g, b, 1.0};
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
- }
- void GLRenderSystem::setShadingType(ShadeOptions so)
- {
- switch(so)
- {
- case SO_FLAT:
- glShadeModel(GL_FLAT);
- break;
- default:
- glShadeModel(GL_SMOOTH);
- break;
- }
- }
- //---------------------------------------------------------------------
- bool GLRenderSystem::_createRenderWindows(const RenderWindowDescriptionList& renderWindowDescriptions,
- RenderWindowList& createdWindows)
- {
- // Call base render system method.
- if (false == RenderSystem::_createRenderWindows(renderWindowDescriptions, createdWindows))
- return false;
- // Simply call _createRenderWindow in a loop.
- for (size_t i = 0; i < renderWindowDescriptions.size(); ++i)
- {
- const RenderWindowDescription& curRenderWindowDescription = renderWindowDescriptions[i];
- RenderWindow* curWindow = NULL;
- curWindow = _createRenderWindow(curRenderWindowDescription.name,
- curRenderWindowDescription.width,
- curRenderWindowDescription.height,
- curRenderWindowDescription.useFullScreen,
- &curRenderWindowDescription.miscParams);
- createdWindows.push_back(curWindow);
- }
-
- return true;
- }
- //---------------------------------------------------------------------
- RenderWindow* GLRenderSystem::_createRenderWindow(const String &name,
- unsigned int width, unsigned int height, bool fullScreen,
- const NameValuePairList *miscParams)
- {
- if (mRenderTargets.find(name) != mRenderTargets.end())
- {
- OGRE_EXCEPT(
- Exception::ERR_INVALIDPARAMS,
- "Window with name '" + name + "' already exists",
- "GLRenderSystem::_createRenderWindow" );
- }
- // Create the window
- RenderWindow* win = mGLSupport->newWindow(name, width, height,
- fullScreen, miscParams);
- attachRenderTarget( *win );
- if (!mGLInitialised)
- {
- // set up glew and GLSupport
- initialiseContext(win);
- std::vector<CamelotEngine::String> tokens = StringUtil::split(mGLSupport->getGLVersion(), ".");
- if (!tokens.empty())
- {
- mDriverVersion.major = parseInt(tokens[0]);
- if (tokens.size() > 1)
- mDriverVersion.minor = parseInt(tokens[1]);
- if (tokens.size() > 2)
- mDriverVersion.release = parseInt(tokens[2]);
- }
- mDriverVersion.build = 0;
- // Initialise GL after the first window has been created
- // TODO: fire this from emulation options, and don't duplicate float and Current capabilities
- mRealCapabilities = createRenderSystemCapabilities();
- // use real capabilities if custom capabilities are not available
- if(!mUseCustomCapabilities)
- mCurrentCapabilities = mRealCapabilities;
- initialiseFromRenderSystemCapabilities(mCurrentCapabilities, win);
- // Initialise the main context
- _oneTimeContextInitialization();
- if(mCurrentContext)
- mCurrentContext->setInitialized();
- }
- return win;
- }
- void GLRenderSystem::initialiseContext(RenderWindow* primary)
- {
- // Set main and current context
- mMainContext = 0;
- primary->getCustomAttribute("GLCONTEXT", &mMainContext);
- mCurrentContext = mMainContext;
- // Set primary context as active
- if(mCurrentContext)
- mCurrentContext->setCurrent();
- // Setup GLSupport
- mGLSupport->initialiseExtensions();
- // Get extension function pointers
- #if CM_THREAD_SUPPORT != 1
- glewContextInit(mGLSupport);
- #endif
- }
- //-----------------------------------------------------------------------
- MultiRenderTarget * GLRenderSystem::createMultiRenderTarget(const String & name)
- {
- MultiRenderTarget *retval = mRTTManager->createMultiRenderTarget(name);
- attachRenderTarget( *retval );
- return retval;
- }
- //-----------------------------------------------------------------------
- void GLRenderSystem::destroyRenderWindow(RenderWindow* pWin)
- {
- // Find it to remove from list
- RenderTargetMap::iterator i = mRenderTargets.begin();
- while (i != mRenderTargets.end())
- {
- if (i->second == pWin)
- {
- mRenderTargets.erase(i);
- delete pWin;
- break;
- }
- }
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::_useLights(const LightList& lights, unsigned short limit)
- {
- // TODO PORT - I don't use fixed pipleline lights. Remove this
- assert(false);
- //// Save previous modelview
- //glMatrixMode(GL_MODELVIEW);
- //glPushMatrix();
- //// just load view matrix (identity world)
- //GLfloat mat[16];
- //makeGLMatrix(mat, mViewMatrix);
- //glLoadMatrixf(mat);
- //LightList::const_iterator i, iend;
- //iend = lights.end();
- //unsigned short num = 0;
- //for (i = lights.begin(); i != iend && num < limit; ++i, ++num)
- //{
- // setGLLight(num, *i);
- // mLights[num] = *i;
- //}
- //// Disable extra lights
- //for (; num < mCurrentLights; ++num)
- //{
- // setGLLight(num, NULL);
- // mLights[num] = NULL;
- //}
- //mCurrentLights = std::min(limit, static_cast<unsigned short>(lights.size()));
- //setLights();
- //// restore previous
- //glPopMatrix();
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::makeGLMatrix(GLfloat gl_matrix[16], const Matrix4& m)
- {
- size_t x = 0;
- for (size_t i = 0; i < 4; i++)
- {
- for (size_t j = 0; j < 4; j++)
- {
- gl_matrix[x] = m[j][i];
- x++;
- }
- }
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setWorldMatrix( const Matrix4 &m )
- {
- GLfloat mat[16];
- mWorldMatrix = m;
- makeGLMatrix( mat, mViewMatrix * mWorldMatrix );
- glMatrixMode(GL_MODELVIEW);
- glLoadMatrixf(mat);
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setViewMatrix( const Matrix4 &m )
- {
- mViewMatrix = m;
- GLfloat mat[16];
- makeGLMatrix( mat, mViewMatrix * mWorldMatrix );
- glMatrixMode(GL_MODELVIEW);
- glLoadMatrixf(mat);
- // also mark clip planes dirty
- if (!mClipPlanes.empty())
- mClipPlanesDirty = true;
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setProjectionMatrix(const Matrix4 &m)
- {
- GLfloat mat[16];
- makeGLMatrix(mat, m);
- if (mActiveRenderTarget->requiresTextureFlipping())
- {
- // Invert transformed y
- mat[1] = -mat[1];
- mat[5] = -mat[5];
- mat[9] = -mat[9];
- mat[13] = -mat[13];
- }
- glMatrixMode(GL_PROJECTION);
- glLoadMatrixf(mat);
- glMatrixMode(GL_MODELVIEW);
- // also mark clip planes dirty
- if (!mClipPlanes.empty())
- mClipPlanesDirty = true;
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setSurfaceParams(const ColourValue &ambient,
- const ColourValue &diffuse, const ColourValue &specular,
- const ColourValue &emissive, float shininess,
- TrackVertexColourType tracking)
- {
- // Track vertex colour
- if(tracking != TVC_NONE)
- {
- GLenum gt = GL_DIFFUSE;
- // There are actually 15 different combinations for tracking, of which
- // GL only supports the most used 5. This means that we have to do some
- // magic to find the best match. NOTE:
- // GL_AMBIENT_AND_DIFFUSE != GL_AMBIENT | GL__DIFFUSE
- if(tracking & TVC_AMBIENT)
- {
- if(tracking & TVC_DIFFUSE)
- {
- gt = GL_AMBIENT_AND_DIFFUSE;
- }
- else
- {
- gt = GL_AMBIENT;
- }
- }
- else if(tracking & TVC_DIFFUSE)
- {
- gt = GL_DIFFUSE;
- }
- else if(tracking & TVC_SPECULAR)
- {
- gt = GL_SPECULAR;
- }
- else if(tracking & TVC_EMISSIVE)
- {
- gt = GL_EMISSION;
- }
- glColorMaterial(GL_FRONT_AND_BACK, gt);
- glEnable(GL_COLOR_MATERIAL);
- }
- else
- {
- glDisable(GL_COLOR_MATERIAL);
- }
- // XXX Cache previous values?
- // XXX Front or Front and Back?
- GLfloat f4val[4] = {diffuse.r, diffuse.g, diffuse.b, diffuse.a};
- glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, f4val);
- f4val[0] = ambient.r;
- f4val[1] = ambient.g;
- f4val[2] = ambient.b;
- f4val[3] = ambient.a;
- glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, f4val);
- f4val[0] = specular.r;
- f4val[1] = specular.g;
- f4val[2] = specular.b;
- f4val[3] = specular.a;
- glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, f4val);
- f4val[0] = emissive.r;
- f4val[1] = emissive.g;
- f4val[2] = emissive.b;
- f4val[3] = emissive.a;
- glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, f4val);
- glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setPointParameters(float size,
- bool attenuationEnabled, float constant, float linear, float quadratic,
- float minSize, float maxSize)
- {
-
- float val[4] = {1, 0, 0, 1};
-
- if(attenuationEnabled)
- {
- // Point size is still calculated in pixels even when attenuation is
- // enabled, which is pretty awkward, since you typically want a viewport
- // independent size if you're looking for attenuation.
- // So, scale the point size up by viewport size (this is equivalent to
- // what D3D does as standard)
- size = size * mActiveViewport->getActualHeight();
- minSize = minSize * mActiveViewport->getActualHeight();
- if (maxSize == 0.0f)
- maxSize = mCurrentCapabilities->getMaxPointSize(); // pixels
- else
- maxSize = maxSize * mActiveViewport->getActualHeight();
-
- // XXX: why do I need this for results to be consistent with D3D?
- // Equations are supposedly the same once you factor in vp height
- float correction = 0.005f;
- // scaling required
- val[0] = constant;
- val[1] = linear * correction;
- val[2] = quadratic * correction;
- val[3] = 1;
-
- if (mCurrentCapabilities->hasCapability(RSC_VERTEX_PROGRAM))
- glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
-
-
- }
- else
- {
- if (maxSize == 0.0f)
- maxSize = mCurrentCapabilities->getMaxPointSize();
- if (mCurrentCapabilities->hasCapability(RSC_VERTEX_PROGRAM))
- glDisable(GL_VERTEX_PROGRAM_POINT_SIZE);
- }
-
- // no scaling required
- // GL has no disabled flag for this so just set to constant
- glPointSize(size);
-
- if (mCurrentCapabilities->hasCapability(RSC_POINT_EXTENDED_PARAMETERS))
- {
- glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION, val);
- glPointParameterf(GL_POINT_SIZE_MIN, minSize);
- glPointParameterf(GL_POINT_SIZE_MAX, maxSize);
- }
- else if (mCurrentCapabilities->hasCapability(RSC_POINT_EXTENDED_PARAMETERS_ARB))
- {
- glPointParameterfvARB(GL_POINT_DISTANCE_ATTENUATION, val);
- glPointParameterfARB(GL_POINT_SIZE_MIN, minSize);
- glPointParameterfARB(GL_POINT_SIZE_MAX, maxSize);
- }
- else if (mCurrentCapabilities->hasCapability(RSC_POINT_EXTENDED_PARAMETERS_EXT))
- {
- glPointParameterfvEXT(GL_POINT_DISTANCE_ATTENUATION, val);
- glPointParameterfEXT(GL_POINT_SIZE_MIN, minSize);
- glPointParameterfEXT(GL_POINT_SIZE_MAX, maxSize);
- }
-
-
-
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::_setPointSpritesEnabled(bool enabled)
- {
- if (!getCapabilities()->hasCapability(RSC_POINT_SPRITES))
- return;
- if (enabled)
- {
- glEnable(GL_POINT_SPRITE);
- }
- else
- {
- glDisable(GL_POINT_SPRITE);
- }
- // Set sprite texture coord generation
- // Don't offer this as an option since D3D links it to sprite enabled
- for (UINT16 i = 0; i < mFixedFunctionTextureUnits; ++i)
- {
- activateGLTextureUnit(i);
- glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE,
- enabled ? GL_TRUE : GL_FALSE);
- }
- activateGLTextureUnit(0);
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setTexture(size_t stage, bool enabled, const TexturePtr &texPtr)
- {
- GLTexturePtr tex = std::static_pointer_cast<GLTexture>(texPtr);
- GLenum lastTextureType = mTextureTypes[stage];
- if (!activateGLTextureUnit(stage))
- return;
- if (enabled)
- {
- if (tex)
- {
- // note used
- mTextureTypes[stage] = tex->getGLTextureTarget();
- }
- else
- // assume 2D
- mTextureTypes[stage] = GL_TEXTURE_2D;
- if(lastTextureType != mTextureTypes[stage] && lastTextureType != 0)
- {
- if (stage < mFixedFunctionTextureUnits)
- {
- glDisable( lastTextureType );
- }
- }
- if (stage < mFixedFunctionTextureUnits)
- {
- glEnable( mTextureTypes[stage] );
- }
- if(tex)
- glBindTexture( mTextureTypes[stage], tex->getGLID() );
- else
- {
- glBindTexture( mTextureTypes[stage], static_cast<GLTextureManager*>(mTextureManager)->getWarningTextureID() );
- }
- }
- else
- {
- if (stage < mFixedFunctionTextureUnits)
- {
- if (lastTextureType != 0)
- {
- glDisable( mTextureTypes[stage] );
- }
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- }
- // bind zero texture
- glBindTexture(GL_TEXTURE_2D, 0);
- }
- activateGLTextureUnit(0);
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setTextureCoordSet(size_t stage, size_t index)
- {
- mTextureCoordIndex[stage] = index;
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setTextureCoordCalculation(size_t stage, TexCoordCalcMethod m,
- const Frustum* frustum)
- {
- if (stage >= mFixedFunctionTextureUnits)
- {
- // Can't do this
- return;
- }
- GLfloat M[16];
- Matrix4 projectionBias;
- // Default to no extra auto texture matrix
- mUseAutoTextureMatrix = false;
- GLfloat eyePlaneS[] = {1.0, 0.0, 0.0, 0.0};
- GLfloat eyePlaneT[] = {0.0, 1.0, 0.0, 0.0};
- GLfloat eyePlaneR[] = {0.0, 0.0, 1.0, 0.0};
- GLfloat eyePlaneQ[] = {0.0, 0.0, 0.0, 1.0};
- if (!activateGLTextureUnit(stage))
- return;
- switch( m )
- {
- case TEXCALC_NONE:
- glDisable( GL_TEXTURE_GEN_S );
- glDisable( GL_TEXTURE_GEN_T );
- glDisable( GL_TEXTURE_GEN_R );
- glDisable( GL_TEXTURE_GEN_Q );
- break;
- case TEXCALC_ENVIRONMENT_MAP:
- glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP );
- glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP );
- glEnable( GL_TEXTURE_GEN_S );
- glEnable( GL_TEXTURE_GEN_T );
- glDisable( GL_TEXTURE_GEN_R );
- glDisable( GL_TEXTURE_GEN_Q );
- // Need to use a texture matrix to flip the spheremap
- mUseAutoTextureMatrix = true;
- memset(mAutoTextureMatrix, 0, sizeof(GLfloat)*16);
- mAutoTextureMatrix[0] = mAutoTextureMatrix[10] = mAutoTextureMatrix[15] = 1.0f;
- mAutoTextureMatrix[5] = -1.0f;
- break;
- case TEXCALC_ENVIRONMENT_MAP_PLANAR:
- // XXX This doesn't seem right?!
- #ifdef GL_VERSION_1_3
- glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
- glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
- glTexGeni( GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
- glEnable( GL_TEXTURE_GEN_S );
- glEnable( GL_TEXTURE_GEN_T );
- glEnable( GL_TEXTURE_GEN_R );
- glDisable( GL_TEXTURE_GEN_Q );
- #else
- glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP );
- glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP );
- glEnable( GL_TEXTURE_GEN_S );
- glEnable( GL_TEXTURE_GEN_T );
- glDisable( GL_TEXTURE_GEN_R );
- glDisable( GL_TEXTURE_GEN_Q );
- #endif
- break;
- case TEXCALC_ENVIRONMENT_MAP_REFLECTION:
- glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
- glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
- glTexGeni( GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
- glEnable( GL_TEXTURE_GEN_S );
- glEnable( GL_TEXTURE_GEN_T );
- glEnable( GL_TEXTURE_GEN_R );
- glDisable( GL_TEXTURE_GEN_Q );
- // We need an extra texture matrix here
- // This sets the texture matrix to be the inverse of the view matrix
- mUseAutoTextureMatrix = true;
- makeGLMatrix( M, mViewMatrix);
- // Transpose 3x3 in order to invert matrix (rotation)
- // Note that we need to invert the Z _before_ the rotation
- // No idea why we have to invert the Z at all, but reflection is wrong without it
- mAutoTextureMatrix[0] = M[0]; mAutoTextureMatrix[1] = M[4]; mAutoTextureMatrix[2] = -M[8];
- mAutoTextureMatrix[4] = M[1]; mAutoTextureMatrix[5] = M[5]; mAutoTextureMatrix[6] = -M[9];
- mAutoTextureMatrix[8] = M[2]; mAutoTextureMatrix[9] = M[6]; mAutoTextureMatrix[10] = -M[10];
- mAutoTextureMatrix[3] = mAutoTextureMatrix[7] = mAutoTextureMatrix[11] = 0.0f;
- mAutoTextureMatrix[12] = mAutoTextureMatrix[13] = mAutoTextureMatrix[14] = 0.0f;
- mAutoTextureMatrix[15] = 1.0f;
- break;
- case TEXCALC_ENVIRONMENT_MAP_NORMAL:
- glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP );
- glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP );
- glTexGeni( GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP );
- glEnable( GL_TEXTURE_GEN_S );
- glEnable( GL_TEXTURE_GEN_T );
- glEnable( GL_TEXTURE_GEN_R );
- glDisable( GL_TEXTURE_GEN_Q );
- break;
- case TEXCALC_PROJECTIVE_TEXTURE:
- glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
- glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
- glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
- glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
- glTexGenfv(GL_S, GL_EYE_PLANE, eyePlaneS);
- glTexGenfv(GL_T, GL_EYE_PLANE, eyePlaneT);
- glTexGenfv(GL_R, GL_EYE_PLANE, eyePlaneR);
- glTexGenfv(GL_Q, GL_EYE_PLANE, eyePlaneQ);
- glEnable(GL_TEXTURE_GEN_S);
- glEnable(GL_TEXTURE_GEN_T);
- glEnable(GL_TEXTURE_GEN_R);
- glEnable(GL_TEXTURE_GEN_Q);
- mUseAutoTextureMatrix = true;
- // Set scale and translation matrix for projective textures
- projectionBias = Matrix4::CLIPSPACE2DTOIMAGESPACE;
- projectionBias = projectionBias * frustum->getProjectionMatrix();
- if(mTexProjRelative)
- {
- Matrix4 viewMatrix;
- frustum->calcViewMatrixRelative(mTexProjRelativeOrigin, viewMatrix);
- projectionBias = projectionBias * viewMatrix;
- }
- else
- {
- projectionBias = projectionBias * frustum->getViewMatrix();
- }
- projectionBias = projectionBias * mWorldMatrix;
- makeGLMatrix(mAutoTextureMatrix, projectionBias);
- break;
- default:
- break;
- }
- activateGLTextureUnit(0);
- }
- //-----------------------------------------------------------------------------
- GLint GLRenderSystem::getTextureAddressingMode(
- TextureState::TextureAddressingMode tam) const
- {
- switch(tam)
- {
- default:
- case TextureState::TAM_WRAP:
- return GL_REPEAT;
- case TextureState::TAM_MIRROR:
- return GL_MIRRORED_REPEAT;
- case TextureState::TAM_CLAMP:
- return GL_CLAMP_TO_EDGE;
- case TextureState::TAM_BORDER:
- return GL_CLAMP_TO_BORDER;
- }
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setTextureAddressingMode(size_t stage, const TextureState::UVWAddressingMode& uvw)
- {
- if (!activateGLTextureUnit(stage))
- return;
- glTexParameteri( mTextureTypes[stage], GL_TEXTURE_WRAP_S,
- getTextureAddressingMode(uvw.u));
- glTexParameteri( mTextureTypes[stage], GL_TEXTURE_WRAP_T,
- getTextureAddressingMode(uvw.v));
- glTexParameteri( mTextureTypes[stage], GL_TEXTURE_WRAP_R,
- getTextureAddressingMode(uvw.w));
- activateGLTextureUnit(0);
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setTextureBorderColour(size_t stage, const ColourValue& colour)
- {
- GLfloat border[4] = { colour.r, colour.g, colour.b, colour.a };
- if (activateGLTextureUnit(stage))
- {
- glTexParameterfv( mTextureTypes[stage], GL_TEXTURE_BORDER_COLOR, border);
- activateGLTextureUnit(0);
- }
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setTextureMipmapBias(size_t stage, float bias)
- {
- if (mCurrentCapabilities->hasCapability(RSC_MIPMAP_LOD_BIAS))
- {
- if (activateGLTextureUnit(stage))
- {
- glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, bias);
- activateGLTextureUnit(0);
- }
- }
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setTextureMatrix(size_t stage, const Matrix4& xform)
- {
- if (stage >= mFixedFunctionTextureUnits)
- {
- // Can't do this
- return;
- }
- GLfloat mat[16];
- makeGLMatrix(mat, xform);
- if (!activateGLTextureUnit(stage))
- return;
- glMatrixMode(GL_TEXTURE);
- // Load this matrix in
- glLoadMatrixf(mat);
- if (mUseAutoTextureMatrix)
- {
- // Concat auto matrix
- glMultMatrixf(mAutoTextureMatrix);
- }
- glMatrixMode(GL_MODELVIEW);
- activateGLTextureUnit(0);
- }
- //-----------------------------------------------------------------------------
- GLint GLRenderSystem::getBlendMode(SceneBlendFactor ogreBlend) const
- {
- switch(ogreBlend)
- {
- case SBF_ONE:
- return GL_ONE;
- case SBF_ZERO:
- return GL_ZERO;
- case SBF_DEST_COLOUR:
- return GL_DST_COLOR;
- case SBF_SOURCE_COLOUR:
- return GL_SRC_COLOR;
- case SBF_ONE_MINUS_DEST_COLOUR:
- return GL_ONE_MINUS_DST_COLOR;
- case SBF_ONE_MINUS_SOURCE_COLOUR:
- return GL_ONE_MINUS_SRC_COLOR;
- case SBF_DEST_ALPHA:
- return GL_DST_ALPHA;
- case SBF_SOURCE_ALPHA:
- return GL_SRC_ALPHA;
- case SBF_ONE_MINUS_DEST_ALPHA:
- return GL_ONE_MINUS_DST_ALPHA;
- case SBF_ONE_MINUS_SOURCE_ALPHA:
- return GL_ONE_MINUS_SRC_ALPHA;
- };
- // to keep compiler happy
- return GL_ONE;
- }
- void GLRenderSystem::_setSceneBlending(SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendOperation op )
- {
- GLint sourceBlend = getBlendMode(sourceFactor);
- GLint destBlend = getBlendMode(destFactor);
- if(sourceFactor == SBF_ONE && destFactor == SBF_ZERO)
- {
- glDisable(GL_BLEND);
- }
- else
- {
- glEnable(GL_BLEND);
- glBlendFunc(sourceBlend, destBlend);
- }
- GLint func = GL_FUNC_ADD;
- switch(op)
- {
- case SBO_ADD:
- func = GL_FUNC_ADD;
- break;
- case SBO_SUBTRACT:
- func = GL_FUNC_SUBTRACT;
- break;
- case SBO_REVERSE_SUBTRACT:
- func = GL_FUNC_REVERSE_SUBTRACT;
- break;
- case SBO_MIN:
- func = GL_MIN;
- break;
- case SBO_MAX:
- func = GL_MAX;
- break;
- }
- if(GLEW_VERSION_1_4 || GLEW_ARB_imaging)
- {
- glBlendEquation(func);
- }
- else if(GLEW_EXT_blend_minmax && (func == GL_MIN || func == GL_MAX))
- {
- glBlendEquationEXT(func);
- }
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setSeparateSceneBlending(
- SceneBlendFactor sourceFactor, SceneBlendFactor destFactor,
- SceneBlendFactor sourceFactorAlpha, SceneBlendFactor destFactorAlpha,
- SceneBlendOperation op, SceneBlendOperation alphaOp )
- {
- GLint sourceBlend = getBlendMode(sourceFactor);
- GLint destBlend = getBlendMode(destFactor);
- GLint sourceBlendAlpha = getBlendMode(sourceFactorAlpha);
- GLint destBlendAlpha = getBlendMode(destFactorAlpha);
- if(sourceFactor == SBF_ONE && destFactor == SBF_ZERO &&
- sourceFactorAlpha == SBF_ONE && destFactorAlpha == SBF_ZERO)
- {
- glDisable(GL_BLEND);
- }
- else
- {
- glEnable(GL_BLEND);
- glBlendFuncSeparate(sourceBlend, destBlend, sourceBlendAlpha, destBlendAlpha);
- }
- GLint func = GL_FUNC_ADD, alphaFunc = GL_FUNC_ADD;
- switch(op)
- {
- case SBO_ADD:
- func = GL_FUNC_ADD;
- break;
- case SBO_SUBTRACT:
- func = GL_FUNC_SUBTRACT;
- break;
- case SBO_REVERSE_SUBTRACT:
- func = GL_FUNC_REVERSE_SUBTRACT;
- break;
- case SBO_MIN:
- func = GL_MIN;
- break;
- case SBO_MAX:
- func = GL_MAX;
- break;
- }
- switch(alphaOp)
- {
- case SBO_ADD:
- alphaFunc = GL_FUNC_ADD;
- break;
- case SBO_SUBTRACT:
- alphaFunc = GL_FUNC_SUBTRACT;
- break;
- case SBO_REVERSE_SUBTRACT:
- alphaFunc = GL_FUNC_REVERSE_SUBTRACT;
- break;
- case SBO_MIN:
- alphaFunc = GL_MIN;
- break;
- case SBO_MAX:
- alphaFunc = GL_MAX;
- break;
- }
- if(GLEW_VERSION_2_0) {
- glBlendEquationSeparate(func, alphaFunc);
- }
- else if(GLEW_EXT_blend_equation_separate) {
- glBlendEquationSeparateEXT(func, alphaFunc);
- }
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setAlphaRejectSettings(CompareFunction func, unsigned char value, bool alphaToCoverage)
- {
- bool a2c = false;
- static bool lasta2c = false;
- if(func == CMPF_ALWAYS_PASS)
- {
- glDisable(GL_ALPHA_TEST);
- }
- else
- {
- glEnable(GL_ALPHA_TEST);
- a2c = alphaToCoverage;
- glAlphaFunc(convertCompareFunction(func), value / 255.0f);
- }
- if (a2c != lasta2c && getCapabilities()->hasCapability(RSC_ALPHA_TO_COVERAGE))
- {
- if (a2c)
- glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);
- else
- glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
- lasta2c = a2c;
- }
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setViewport(Viewport *vp)
- {
- // Check if viewport is different
- if (vp != mActiveViewport)
- {
- RenderTarget* target;
- target = vp->getTarget();
- _setRenderTarget(target);
- mActiveViewport = vp;
- GLsizei x, y, w, h;
- // Calculate the "lower-left" corner of the viewport
- w = vp->getActualWidth();
- h = vp->getActualHeight();
- x = vp->getActualLeft();
- y = vp->getActualTop();
- if (!target->requiresTextureFlipping())
- {
- // Convert "upper-left" corner to "lower-left"
- y = target->getHeight() - h - y;
- }
- glViewport(x, y, w, h);
- // Configure the viewport clipping
- glScissor(x, y, w, h);
- }
- }
- void GLRenderSystem::setLights()
- {
- // TODO PORT - I'm not using fixed pipeline lights. Remove this later
- //for (size_t i = 0; i < MAX_LIGHTS; ++i)
- //{
- // if (mLights[i] != NULL)
- // {
- // Light* lt = mLights[i];
- // setGLLightPositionDirection(lt, GL_LIGHT0 + i);
- // }
- //}
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_beginFrame(void)
- {
- if (!mActiveViewport)
- OGRE_EXCEPT(Exception::ERR_INVALID_STATE,
- "Cannot begin frame - no viewport selected.",
- "GLRenderSystem::_beginFrame");
- // Activate the viewport clipping
- glEnable(GL_SCISSOR_TEST);
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_endFrame(void)
- {
- // Deactivate the viewport clipping.
- glDisable(GL_SCISSOR_TEST);
- // unbind GPU programs at end of frame
- // this is mostly to avoid holding bound programs that might get deleted
- // outside via the resource manager
- unbindGpuProgram(GPT_VERTEX_PROGRAM);
- unbindGpuProgram(GPT_FRAGMENT_PROGRAM);
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setCullingMode(CullingMode mode)
- {
- mCullingMode = mode;
- // NB: Because two-sided stencil API dependence of the front face, we must
- // use the same 'winding' for the front face everywhere. As the OGRE default
- // culling mode is clockwise, we also treat anticlockwise winding as front
- // face for consistently. On the assumption that, we can't change the front
- // face by glFrontFace anywhere.
- GLenum cullMode;
- switch( mode )
- {
- case CULL_NONE:
- glDisable( GL_CULL_FACE );
- return;
- default:
- case CULL_CLOCKWISE:
- if (mActiveRenderTarget &&
- ((mActiveRenderTarget->requiresTextureFlipping() && !mInvertVertexWinding) ||
- (!mActiveRenderTarget->requiresTextureFlipping() && mInvertVertexWinding)))
- {
- cullMode = GL_FRONT;
- }
- else
- {
- cullMode = GL_BACK;
- }
- break;
- case CULL_ANTICLOCKWISE:
- if (mActiveRenderTarget &&
- ((mActiveRenderTarget->requiresTextureFlipping() && !mInvertVertexWinding) ||
- (!mActiveRenderTarget->requiresTextureFlipping() && mInvertVertexWinding)))
- {
- cullMode = GL_BACK;
- }
- else
- {
- cullMode = GL_FRONT;
- }
- break;
- }
- glEnable( GL_CULL_FACE );
- glCullFace( cullMode );
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setDepthBufferParams(bool depthTest, bool depthWrite, CompareFunction depthFunction)
- {
- _setDepthBufferCheckEnabled(depthTest);
- _setDepthBufferWriteEnabled(depthWrite);
- _setDepthBufferFunction(depthFunction);
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setDepthBufferCheckEnabled(bool enabled)
- {
- if (enabled)
- {
- glClearDepth(1.0f);
- glEnable(GL_DEPTH_TEST);
- }
- else
- {
- glDisable(GL_DEPTH_TEST);
- }
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setDepthBufferWriteEnabled(bool enabled)
- {
- GLboolean flag = enabled ? GL_TRUE : GL_FALSE;
- glDepthMask( flag );
- // Store for reference in _beginFrame
- mDepthWrite = enabled;
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setDepthBufferFunction(CompareFunction func)
- {
- glDepthFunc(convertCompareFunction(func));
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setDepthBias(float constantBias, float slopeScaleBias)
- {
- if (constantBias != 0 || slopeScaleBias != 0)
- {
- glEnable(GL_POLYGON_OFFSET_FILL);
- glEnable(GL_POLYGON_OFFSET_POINT);
- glEnable(GL_POLYGON_OFFSET_LINE);
- glPolygonOffset(-slopeScaleBias, -constantBias);
- }
- else
- {
- glDisable(GL_POLYGON_OFFSET_FILL);
- glDisable(GL_POLYGON_OFFSET_POINT);
- glDisable(GL_POLYGON_OFFSET_LINE);
- }
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setColourBufferWriteEnabled(bool red, bool green, bool blue, bool alpha)
- {
- glColorMask(red, green, blue, alpha);
- // record this
- mColourWrite[0] = red;
- mColourWrite[1] = blue;
- mColourWrite[2] = green;
- mColourWrite[3] = alpha;
- }
- //-----------------------------------------------------------------------------
- String GLRenderSystem::getErrorDescription(long errCode) const
- {
- const GLubyte *errString = gluErrorString (errCode);
- return (errString != 0) ? String((const char*) errString) : StringUtil::BLANK;
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::setLightingEnabled(bool enabled)
- {
- if (enabled)
- {
- glEnable(GL_LIGHTING);
- }
- else
- {
- glDisable(GL_LIGHTING);
- }
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setFog(FogMode mode, const ColourValue& colour, float density, float start, float end)
- {
- GLint fogMode;
- switch (mode)
- {
- case FOG_EXP:
- fogMode = GL_EXP;
- break;
- case FOG_EXP2:
- fogMode = GL_EXP2;
- break;
- case FOG_LINEAR:
- fogMode = GL_LINEAR;
- break;
- default:
- // Give up on it
- glDisable(GL_FOG);
- return;
- }
- glEnable(GL_FOG);
- glFogi(GL_FOG_MODE, fogMode);
- GLfloat fogColor[4] = {colour.r, colour.g, colour.b, colour.a};
- glFogfv(GL_FOG_COLOR, fogColor);
- glFogf(GL_FOG_DENSITY, density);
- glFogf(GL_FOG_START, start);
- glFogf(GL_FOG_END, end);
- // XXX Hint here?
- }
- VertexElementType GLRenderSystem::getColourVertexElementType(void) const
- {
- return VET_COLOUR_ABGR;
- }
- void GLRenderSystem::_convertProjectionMatrix(const Matrix4& matrix,
- Matrix4& dest, bool forGpuProgram)
- {
- // no any conversion request for OpenGL
- dest = matrix;
- }
- void GLRenderSystem::_makeProjectionMatrix(const Radian& fovy, float aspect, float nearPlane,
- float farPlane, Matrix4& dest, bool forGpuProgram)
- {
- Radian thetaY ( fovy / 2.0f );
- float tanThetaY = Math::Tan(thetaY);
- //float thetaX = thetaY * aspect;
- //float tanThetaX = Math::Tan(thetaX);
- // Calc matrix elements
- float w = (1.0f / tanThetaY) / aspect;
- float h = 1.0f / tanThetaY;
- float q, qn;
- if (farPlane == 0)
- {
- // Infinite far plane
- q = Frustum::INFINITE_FAR_PLANE_ADJUST - 1;
- qn = nearPlane * (Frustum::INFINITE_FAR_PLANE_ADJUST - 2);
- }
- else
- {
- q = -(farPlane + nearPlane) / (farPlane - nearPlane);
- qn = -2 * (farPlane * nearPlane) / (farPlane - nearPlane);
- }
- // NB This creates Z in range [-1,1]
- //
- // [ w 0 0 0 ]
- // [ 0 h 0 0 ]
- // [ 0 0 q qn ]
- // [ 0 0 -1 0 ]
- dest = Matrix4::ZERO;
- dest[0][0] = w;
- dest[1][1] = h;
- dest[2][2] = q;
- dest[2][3] = qn;
- dest[3][2] = -1;
- }
- void GLRenderSystem::_makeOrthoMatrix(const Radian& fovy, float aspect, float nearPlane,
- float farPlane, Matrix4& dest, bool forGpuProgram)
- {
- Radian thetaY (fovy / 2.0f);
- float tanThetaY = Math::Tan(thetaY);
- //float thetaX = thetaY * aspect;
- float tanThetaX = tanThetaY * aspect; //Math::Tan(thetaX);
- float half_w = tanThetaX * nearPlane;
- float half_h = tanThetaY * nearPlane;
- float iw = 1.0f / half_w;
- float ih = 1.0f / half_h;
- float q;
- if (farPlane == 0)
- {
- q = 0;
- }
- else
- {
- q = 2.0f / (farPlane - nearPlane);
- }
- dest = Matrix4::ZERO;
- dest[0][0] = iw;
- dest[1][1] = ih;
- dest[2][2] = -q;
- dest[2][3] = - (farPlane + nearPlane)/(farPlane - nearPlane);
- dest[3][3] = 1;
- }
- void GLRenderSystem::_setPolygonMode(PolygonMode level)
- {
- GLenum glmode;
- switch(level)
- {
- case PM_POINTS:
- glmode = GL_POINT;
- break;
- case PM_WIREFRAME:
- glmode = GL_LINE;
- break;
- default:
- case PM_SOLID:
- glmode = GL_FILL;
- break;
- }
- glPolygonMode(GL_FRONT_AND_BACK, glmode);
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::setStencilCheckEnabled(bool enabled)
- {
- if (enabled)
- {
- glEnable(GL_STENCIL_TEST);
- }
- else
- {
- glDisable(GL_STENCIL_TEST);
- }
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::setStencilBufferParams(CompareFunction func,
- UINT32 refValue, UINT32 mask, StencilOperation stencilFailOp,
- StencilOperation depthFailOp, StencilOperation passOp,
- bool twoSidedOperation)
- {
- bool flip;
- mStencilMask = mask;
- if (twoSidedOperation)
- {
- if (!mCurrentCapabilities->hasCapability(RSC_TWO_SIDED_STENCIL))
- OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "2-sided stencils are not supported",
- "GLRenderSystem::setStencilBufferParams");
- // NB: We should always treat CCW as front face for consistent with default
- // culling mode. Therefore, we must take care with two-sided stencil settings.
- flip = (mInvertVertexWinding && !mActiveRenderTarget->requiresTextureFlipping()) ||
- (!mInvertVertexWinding && mActiveRenderTarget->requiresTextureFlipping());
- if(GLEW_VERSION_2_0) // New GL2 commands
- {
- // Back
- glStencilMaskSeparate(GL_BACK, mask);
- glStencilFuncSeparate(GL_BACK, convertCompareFunction(func), refValue, mask);
- glStencilOpSeparate(GL_BACK,
- convertStencilOp(stencilFailOp, !flip),
- convertStencilOp(depthFailOp, !flip),
- convertStencilOp(passOp, !flip));
- // Front
- glStencilMaskSeparate(GL_FRONT, mask);
- glStencilFuncSeparate(GL_FRONT, convertCompareFunction(func), refValue, mask);
- glStencilOpSeparate(GL_FRONT,
- convertStencilOp(stencilFailOp, flip),
- convertStencilOp(depthFailOp, flip),
- convertStencilOp(passOp, flip));
- }
- else // EXT_stencil_two_side
- {
- glEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);
- // Back
- glActiveStencilFaceEXT(GL_BACK);
- glStencilMask(mask);
- glStencilFunc(convertCompareFunction(func), refValue, mask);
- glStencilOp(
- convertStencilOp(stencilFailOp, !flip),
- convertStencilOp(depthFailOp, !flip),
- convertStencilOp(passOp, !flip));
- // Front
- glActiveStencilFaceEXT(GL_FRONT);
- glStencilMask(mask);
- glStencilFunc(convertCompareFunction(func), refValue, mask);
- glStencilOp(
- convertStencilOp(stencilFailOp, flip),
- convertStencilOp(depthFailOp, flip),
- convertStencilOp(passOp, flip));
- }
- }
- else
- {
- if(!GLEW_VERSION_2_0)
- glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
- flip = false;
- glStencilMask(mask);
- glStencilFunc(convertCompareFunction(func), refValue, mask);
- glStencilOp(
- convertStencilOp(stencilFailOp, flip),
- convertStencilOp(depthFailOp, flip),
- convertStencilOp(passOp, flip));
- }
- }
- //---------------------------------------------------------------------
- GLint GLRenderSystem::convertCompareFunction(CompareFunction func) const
- {
- switch(func)
- {
- case CMPF_ALWAYS_FAIL:
- return GL_NEVER;
- case CMPF_ALWAYS_PASS:
- return GL_ALWAYS;
- case CMPF_LESS:
- return GL_LESS;
- case CMPF_LESS_EQUAL:
- return GL_LEQUAL;
- case CMPF_EQUAL:
- return GL_EQUAL;
- case CMPF_NOT_EQUAL:
- return GL_NOTEQUAL;
- case CMPF_GREATER_EQUAL:
- return GL_GEQUAL;
- case CMPF_GREATER:
- return GL_GREATER;
- };
- // to keep compiler happy
- return GL_ALWAYS;
- }
- //---------------------------------------------------------------------
- GLint GLRenderSystem::convertStencilOp(StencilOperation op, bool invert) const
- {
- switch(op)
- {
- case SOP_KEEP:
- return GL_KEEP;
- case SOP_ZERO:
- return GL_ZERO;
- case SOP_REPLACE:
- return GL_REPLACE;
- case SOP_INCREMENT:
- return invert ? GL_DECR : GL_INCR;
- case SOP_DECREMENT:
- return invert ? GL_INCR : GL_DECR;
- case SOP_INCREMENT_WRAP:
- return invert ? GL_DECR_WRAP_EXT : GL_INCR_WRAP_EXT;
- case SOP_DECREMENT_WRAP:
- return invert ? GL_INCR_WRAP_EXT : GL_DECR_WRAP_EXT;
- case SOP_INVERT:
- return GL_INVERT;
- };
- // to keep compiler happy
- return SOP_KEEP;
- }
- //---------------------------------------------------------------------
- GLuint GLRenderSystem::getCombinedMinMipFilter(void) const
- {
- switch(mMinFilter)
- {
- case FO_ANISOTROPIC:
- case FO_LINEAR:
- switch(mMipFilter)
- {
- case FO_ANISOTROPIC:
- case FO_LINEAR:
- // linear min, linear mip
- return GL_LINEAR_MIPMAP_LINEAR;
- case FO_POINT:
- // linear min, point mip
- return GL_LINEAR_MIPMAP_NEAREST;
- case FO_NONE:
- // linear min, no mip
- return GL_LINEAR;
- }
- break;
- case FO_POINT:
- case FO_NONE:
- switch(mMipFilter)
- {
- case FO_ANISOTROPIC:
- case FO_LINEAR:
- // nearest min, linear mip
- return GL_NEAREST_MIPMAP_LINEAR;
- case FO_POINT:
- // nearest min, point mip
- return GL_NEAREST_MIPMAP_NEAREST;
- case FO_NONE:
- // nearest min, no mip
- return GL_NEAREST;
- }
- break;
- }
- // should never get here
- return 0;
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::_setTextureUnitFiltering(size_t unit,
- FilterType ftype, FilterOptions fo)
- {
- if (!activateGLTextureUnit(unit))
- return;
- switch(ftype)
- {
- case FT_MIN:
- mMinFilter = fo;
- // Combine with existing mip filter
- glTexParameteri(
- mTextureTypes[unit],
- GL_TEXTURE_MIN_FILTER,
- getCombinedMinMipFilter());
- break;
- case FT_MAG:
- switch (fo)
- {
- case FO_ANISOTROPIC: // GL treats linear and aniso the same
- case FO_LINEAR:
- glTexParameteri(
- mTextureTypes[unit],
- GL_TEXTURE_MAG_FILTER,
- GL_LINEAR);
- break;
- case FO_POINT:
- case FO_NONE:
- glTexParameteri(
- mTextureTypes[unit],
- GL_TEXTURE_MAG_FILTER,
- GL_NEAREST);
- break;
- }
- break;
- case FT_MIP:
- mMipFilter = fo;
- // Combine with existing min filter
- glTexParameteri(
- mTextureTypes[unit],
- GL_TEXTURE_MIN_FILTER,
- getCombinedMinMipFilter());
- break;
- }
- activateGLTextureUnit(0);
- }
- //---------------------------------------------------------------------
- GLfloat GLRenderSystem::_getCurrentAnisotropy(size_t unit)
- {
- GLfloat curAniso = 0;
- glGetTexParameterfv(mTextureTypes[unit],
- GL_TEXTURE_MAX_ANISOTROPY_EXT, &curAniso);
- return curAniso ? curAniso : 1;
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::_setTextureLayerAnisotropy(size_t unit, unsigned int maxAnisotropy)
- {
- if (!mCurrentCapabilities->hasCapability(RSC_ANISOTROPY))
- return;
- if (!activateGLTextureUnit(unit))
- return;
- GLfloat largest_supported_anisotropy = 0;
- glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &largest_supported_anisotropy);
- if (maxAnisotropy > largest_supported_anisotropy)
- maxAnisotropy = largest_supported_anisotropy ?
- static_cast<UINT32>(largest_supported_anisotropy) : 1;
- if (_getCurrentAnisotropy(unit) != maxAnisotropy)
- glTexParameterf(mTextureTypes[unit], GL_TEXTURE_MAX_ANISOTROPY_EXT, (float)maxAnisotropy);
- activateGLTextureUnit(0);
- }
- //-----------------------------------------------------------------------------
- void GLRenderSystem::_setTextureBlendMode(size_t stage, const LayerBlendModeEx& bm)
- {
- if (stage >= mFixedFunctionTextureUnits)
- {
- // Can't do this
- return;
- }
- // Check to see if blending is supported
- if(!mCurrentCapabilities->hasCapability(RSC_BLENDING))
- return;
- GLenum src1op, src2op, cmd;
- GLfloat cv1[4], cv2[4];
- if (bm.blendType == LBT_COLOUR)
- {
- cv1[0] = bm.colourArg1.r;
- cv1[1] = bm.colourArg1.g;
- cv1[2] = bm.colourArg1.b;
- cv1[3] = bm.colourArg1.a;
- mManualBlendColours[stage][0] = bm.colourArg1;
- cv2[0] = bm.colourArg2.r;
- cv2[1] = bm.colourArg2.g;
- cv2[2] = bm.colourArg2.b;
- cv2[3] = bm.colourArg2.a;
- mManualBlendColours[stage][1] = bm.colourArg2;
- }
- if (bm.blendType == LBT_ALPHA)
- {
- cv1[0] = mManualBlendColours[stage][0].r;
- cv1[1] = mManualBlendColours[stage][0].g;
- cv1[2] = mManualBlendColours[stage][0].b;
- cv1[3] = bm.alphaArg1;
- cv2[0] = mManualBlendColours[stage][1].r;
- cv2[1] = mManualBlendColours[stage][1].g;
- cv2[2] = mManualBlendColours[stage][1].b;
- cv2[3] = bm.alphaArg2;
- }
- switch (bm.source1)
- {
- case LBS_CURRENT:
- src1op = GL_PREVIOUS;
- break;
- case LBS_TEXTURE:
- src1op = GL_TEXTURE;
- break;
- case LBS_MANUAL:
- src1op = GL_CONSTANT;
- break;
- case LBS_DIFFUSE:
- src1op = GL_PRIMARY_COLOR;
- break;
- // XXX
- case LBS_SPECULAR:
- src1op = GL_PRIMARY_COLOR;
- break;
- default:
- src1op = 0;
- }
- switch (bm.source2)
- {
- case LBS_CURRENT:
- src2op = GL_PREVIOUS;
- break;
- case LBS_TEXTURE:
- src2op = GL_TEXTURE;
- break;
- case LBS_MANUAL:
- src2op = GL_CONSTANT;
- break;
- case LBS_DIFFUSE:
- src2op = GL_PRIMARY_COLOR;
- break;
- // XXX
- case LBS_SPECULAR:
- src2op = GL_PRIMARY_COLOR;
- break;
- default:
- src2op = 0;
- }
- switch (bm.operation)
- {
- case LBX_SOURCE1:
- cmd = GL_REPLACE;
- break;
- case LBX_SOURCE2:
- cmd = GL_REPLACE;
- break;
- case LBX_MODULATE:
- cmd = GL_MODULATE;
- break;
- case LBX_MODULATE_X2:
- cmd = GL_MODULATE;
- break;
- case LBX_MODULATE_X4:
- cmd = GL_MODULATE;
- break;
- case LBX_ADD:
- cmd = GL_ADD;
- break;
- case LBX_ADD_SIGNED:
- cmd = GL_ADD_SIGNED;
- break;
- case LBX_ADD_SMOOTH:
- cmd = GL_INTERPOLATE;
- break;
- case LBX_SUBTRACT:
- cmd = GL_SUBTRACT;
- break;
- case LBX_BLEND_DIFFUSE_COLOUR:
- cmd = GL_INTERPOLATE;
- break;
- case LBX_BLEND_DIFFUSE_ALPHA:
- cmd = GL_INTERPOLATE;
- break;
- case LBX_BLEND_TEXTURE_ALPHA:
- cmd = GL_INTERPOLATE;
- break;
- case LBX_BLEND_CURRENT_ALPHA:
- cmd = GL_INTERPOLATE;
- break;
- case LBX_BLEND_MANUAL:
- cmd = GL_INTERPOLATE;
- break;
- case LBX_DOTPRODUCT:
- cmd = mCurrentCapabilities->hasCapability(RSC_DOT3)
- ? GL_DOT3_RGB : GL_MODULATE;
- break;
- default:
- cmd = 0;
- }
- if (!activateGLTextureUnit(stage))
- return;
- glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
- if (bm.blendType == LBT_COLOUR)
- {
- glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, cmd);
- glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, src1op);
- glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, src2op);
- glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_CONSTANT);
- }
- else
- {
- glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, cmd);
- glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, src1op);
- glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, src2op);
- glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, GL_CONSTANT);
- }
- float blendValue[4] = {0, 0, 0, bm.factor};
- switch (bm.operation)
- {
- case LBX_BLEND_DIFFUSE_COLOUR:
- glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_PRIMARY_COLOR);
- glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, GL_PRIMARY_COLOR);
- break;
- case LBX_BLEND_DIFFUSE_ALPHA:
- glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_PRIMARY_COLOR);
- glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, GL_PRIMARY_COLOR);
- break;
- case LBX_BLEND_TEXTURE_ALPHA:
- glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_TEXTURE);
- glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, GL_TEXTURE);
- break;
- case LBX_BLEND_CURRENT_ALPHA:
- glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_PREVIOUS);
- glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, GL_PREVIOUS);
- break;
- case LBX_BLEND_MANUAL:
- glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, blendValue);
- break;
- default:
- break;
- };
- switch (bm.operation)
- {
- case LBX_MODULATE_X2:
- glTexEnvi(GL_TEXTURE_ENV, bm.blendType == LBT_COLOUR ?
- GL_RGB_SCALE : GL_ALPHA_SCALE, 2);
- break;
- case LBX_MODULATE_X4:
- glTexEnvi(GL_TEXTURE_ENV, bm.blendType == LBT_COLOUR ?
- GL_RGB_SCALE : GL_ALPHA_SCALE, 4);
- break;
- default:
- glTexEnvi(GL_TEXTURE_ENV, bm.blendType == LBT_COLOUR ?
- GL_RGB_SCALE : GL_ALPHA_SCALE, 1);
- break;
- }
- if (bm.blendType == LBT_COLOUR){
- glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
- glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
- if (bm.operation == LBX_BLEND_DIFFUSE_COLOUR){
- glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_COLOR);
- } else {
- glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA);
- }
- }
- glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
- glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);
- glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA, GL_SRC_ALPHA);
- if(bm.source1 == LBS_MANUAL)
- glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, cv1);
- if (bm.source2 == LBS_MANUAL)
- glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, cv2);
- activateGLTextureUnit(0);
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::setVertexDeclaration(VertexDeclaration* decl)
- {
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::setVertexBufferBinding(VertexBufferBinding* binding)
- {
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::_render(const RenderOperation& op)
- {
- // Call super class
- RenderSystem::_render(op);
- void* pBufferData = 0;
- bool multitexturing = (getCapabilities()->getNumTextureUnits() > 1);
- const VertexDeclaration::VertexElementList& decl =
- op.vertexData->vertexDeclaration->getElements();
- VertexDeclaration::VertexElementList::const_iterator elem, elemEnd;
- elemEnd = decl.end();
- vector<GLuint>::type attribsBound;
- for (elem = decl.begin(); elem != elemEnd; ++elem)
- {
- if (!op.vertexData->vertexBufferBinding->isBufferBound(elem->getSource()))
- continue; // skip unbound elements
- HardwareVertexBufferPtr vertexBuffer =
- op.vertexData->vertexBufferBinding->getBuffer(elem->getSource());
- if(mCurrentCapabilities->hasCapability(RSC_VBO))
- {
- glBindBufferARB(GL_ARRAY_BUFFER_ARB,
- static_cast<const GLHardwareVertexBuffer*>(vertexBuffer.get())->getGLBufferId());
- pBufferData = VBO_BUFFER_OFFSET(elem->getOffset());
- }
- else
- {
- pBufferData = static_cast<const GLDefaultHardwareVertexBuffer*>(vertexBuffer.get())->getDataPtr(elem->getOffset());
- }
- if (op.vertexData->vertexStart)
- {
- pBufferData = static_cast<char*>(pBufferData) + op.vertexData->vertexStart * vertexBuffer->getVertexSize();
- }
- unsigned int i = 0;
- VertexElementSemantic sem = elem->getSemantic();
-
- bool isCustomAttrib = false;
- if (mCurrentVertexProgram)
- isCustomAttrib = mCurrentVertexProgram->isAttributeValid(sem, elem->getIndex());
-
- // Custom attribute support
- // tangents, binormals, blendweights etc always via this route
- // builtins may be done this way too
- if (isCustomAttrib)
- {
- GLint attrib = mCurrentVertexProgram->getAttributeIndex(sem, elem->getIndex());
- unsigned short typeCount = VertexElement::getTypeCount(elem->getType());
- GLboolean normalised = GL_FALSE;
- switch(elem->getType())
- {
- case VET_COLOUR:
- case VET_COLOUR_ABGR:
- case VET_COLOUR_ARGB:
- // Because GL takes these as a sequence of single unsigned bytes, count needs to be 4
- // VertexElement::getTypeCount treats them as 1 (RGBA)
- // Also need to normalise the fixed-point data
- typeCount = 4;
- normalised = GL_TRUE;
- break;
- default:
- break;
- };
- glVertexAttribPointerARB(
- attrib,
- typeCount,
- GLHardwareBufferManager::getGLType(elem->getType()),
- normalised,
- static_cast<GLsizei>(vertexBuffer->getVertexSize()),
- pBufferData);
- glEnableVertexAttribArrayARB(attrib);
-
- attribsBound.push_back(attrib);
- }
- else
- {
- // fixed-function & builtin attribute support
- switch(sem)
- {
- case VES_POSITION:
- glVertexPointer(VertexElement::getTypeCount(
- elem->getType()),
- GLHardwareBufferManager::getGLType(elem->getType()),
- static_cast<GLsizei>(vertexBuffer->getVertexSize()),
- pBufferData);
- glEnableClientState( GL_VERTEX_ARRAY );
- break;
- case VES_NORMAL:
- glNormalPointer(
- GLHardwareBufferManager::getGLType(elem->getType()),
- static_cast<GLsizei>(vertexBuffer->getVertexSize()),
- pBufferData);
- glEnableClientState( GL_NORMAL_ARRAY );
- break;
- case VES_DIFFUSE:
- glColorPointer(4,
- GLHardwareBufferManager::getGLType(elem->getType()),
- static_cast<GLsizei>(vertexBuffer->getVertexSize()),
- pBufferData);
- glEnableClientState( GL_COLOR_ARRAY );
- break;
- case VES_SPECULAR:
- if (GLEW_EXT_secondary_color)
- {
- glSecondaryColorPointerEXT(4,
- GLHardwareBufferManager::getGLType(elem->getType()),
- static_cast<GLsizei>(vertexBuffer->getVertexSize()),
- pBufferData);
- glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
- }
- break;
- case VES_TEXTURE_COORDINATES:
-
- if (mCurrentVertexProgram)
- {
- // Programmable pipeline - direct UV assignment
- glClientActiveTextureARB(GL_TEXTURE0 + elem->getIndex());
- glTexCoordPointer(
- VertexElement::getTypeCount(elem->getType()),
- GLHardwareBufferManager::getGLType(elem->getType()),
- static_cast<GLsizei>(vertexBuffer->getVertexSize()),
- pBufferData);
- glEnableClientState( GL_TEXTURE_COORD_ARRAY );
- }
- else
- {
- // fixed function matching to units based on tex_coord_set
- for (i = 0; i < mDisabledTexUnitsFrom; i++)
- {
- // Only set this texture unit's texcoord pointer if it
- // is supposed to be using this element's index
- if (mTextureCoordIndex[i] == elem->getIndex() && i < mFixedFunctionTextureUnits)
- {
- if (multitexturing)
- glClientActiveTextureARB(GL_TEXTURE0 + i);
- glTexCoordPointer(
- VertexElement::getTypeCount(elem->getType()),
- GLHardwareBufferManager::getGLType(elem->getType()),
- static_cast<GLsizei>(vertexBuffer->getVertexSize()),
- pBufferData);
- glEnableClientState( GL_TEXTURE_COORD_ARRAY );
- }
- }
- }
- break;
- default:
- break;
- };
- } // isCustomAttrib
-
- }
- if (multitexturing)
- glClientActiveTextureARB(GL_TEXTURE0);
- // Find the correct type to render
- GLint primType;
- //Use adjacency if there is a geometry program and it requested adjacency info
- bool useAdjacency = (mGeometryProgramBound && mCurrentGeometryProgram->isAdjacencyInfoRequired());
- switch (op.operationType)
- {
- case RenderOperation::OT_POINT_LIST:
- primType = GL_POINTS;
- break;
- case RenderOperation::OT_LINE_LIST:
- primType = useAdjacency ? GL_LINES_ADJACENCY_EXT : GL_LINES;
- break;
- case RenderOperation::OT_LINE_STRIP:
- primType = useAdjacency ? GL_LINE_STRIP_ADJACENCY_EXT : GL_LINE_STRIP;
- break;
- default:
- case RenderOperation::OT_TRIANGLE_LIST:
- primType = useAdjacency ? GL_TRIANGLES_ADJACENCY_EXT : GL_TRIANGLES;
- break;
- case RenderOperation::OT_TRIANGLE_STRIP:
- primType = useAdjacency ? GL_TRIANGLE_STRIP_ADJACENCY_EXT : GL_TRIANGLE_STRIP;
- break;
- case RenderOperation::OT_TRIANGLE_FAN:
- primType = GL_TRIANGLE_FAN;
- break;
- }
- if (op.useIndexes)
- {
- if(mCurrentCapabilities->hasCapability(RSC_VBO))
- {
- glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
- static_cast<GLHardwareIndexBuffer*>(
- op.indexData->indexBuffer.get())->getGLBufferId());
- pBufferData = VBO_BUFFER_OFFSET(
- op.indexData->indexStart * op.indexData->indexBuffer->getIndexSize());
- }
- else
- {
- pBufferData = static_cast<GLDefaultHardwareIndexBuffer*>(
- op.indexData->indexBuffer.get())->getDataPtr(
- op.indexData->indexStart * op.indexData->indexBuffer->getIndexSize());
- }
- GLenum indexType = (op.indexData->indexBuffer->getType() == HardwareIndexBuffer::IT_16BIT) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
- do
- {
- // Update derived depth bias
- if (mDerivedDepthBias && mCurrentPassIterationNum > 0)
- {
- _setDepthBias(mDerivedDepthBiasBase +
- mDerivedDepthBiasMultiplier * mCurrentPassIterationNum,
- mDerivedDepthBiasSlopeScale);
- }
- glDrawElements(primType, op.indexData->indexCount, indexType, pBufferData);
- } while (updatePassIterationRenderState());
- }
- else
- {
- do
- {
- // Update derived depth bias
- if (mDerivedDepthBias && mCurrentPassIterationNum > 0)
- {
- _setDepthBias(mDerivedDepthBiasBase +
- mDerivedDepthBiasMultiplier * mCurrentPassIterationNum,
- mDerivedDepthBiasSlopeScale);
- }
- glDrawArrays(primType, 0, op.vertexData->vertexCount);
- } while (updatePassIterationRenderState());
- }
- glDisableClientState( GL_VERTEX_ARRAY );
- // only valid up to GL_MAX_TEXTURE_UNITS, which is recorded in mFixedFunctionTextureUnits
- if (multitexturing)
- {
- for (int i = 0; i < mFixedFunctionTextureUnits; i++)
- {
- glClientActiveTextureARB(GL_TEXTURE0 + i);
- glDisableClientState( GL_TEXTURE_COORD_ARRAY );
- }
- glClientActiveTextureARB(GL_TEXTURE0);
- }
- else
- {
- glDisableClientState( GL_TEXTURE_COORD_ARRAY );
- }
- glDisableClientState( GL_NORMAL_ARRAY );
- glDisableClientState( GL_COLOR_ARRAY );
- if (GLEW_EXT_secondary_color)
- {
- glDisableClientState( GL_SECONDARY_COLOR_ARRAY );
- }
- // unbind any custom attributes
- for (vector<GLuint>::type::iterator ai = attribsBound.begin(); ai != attribsBound.end(); ++ai)
- {
- glDisableVertexAttribArrayARB(*ai);
-
- }
-
- glColor4f(1,1,1,1);
- if (GLEW_EXT_secondary_color)
- {
- glSecondaryColor3fEXT(0.0f, 0.0f, 0.0f);
- }
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::setNormaliseNormals(bool normalise)
- {
- if (normalise)
- glEnable(GL_NORMALIZE);
- else
- glDisable(GL_NORMALIZE);
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::bindGpuProgram(GpuProgram* prg)
- {
- GLGpuProgram* glprg = static_cast<GLGpuProgram*>(prg);
- // Unbind previous gpu program first.
- //
- // Note:
- // 1. Even if both previous and current are the same object, we can't
- // bypass re-bind completely since the object itself maybe modified.
- // But we can bypass unbind based on the assumption that object
- // internally GL program type shouldn't be changed after it has
- // been created. The behavior of bind to a GL program type twice
- // should be same as unbind and rebind that GL program type, even
- // for difference objects.
- // 2. We also assumed that the program's type (vertex or fragment) should
- // not be changed during it's in using. If not, the following switch
- // statement will confuse GL state completely, and we can't fix it
- // here. To fix this case, we must coding the program implementation
- // itself, if type is changing (during load/unload, etc), and it's inuse,
- // unbind and notify render system to correct for its state.
- //
- switch (glprg->getType())
- {
- case GPT_VERTEX_PROGRAM:
- if (mCurrentVertexProgram != glprg)
- {
- if (mCurrentVertexProgram)
- mCurrentVertexProgram->unbindProgram();
- mCurrentVertexProgram = glprg;
- }
- break;
- case GPT_FRAGMENT_PROGRAM:
- if (mCurrentFragmentProgram != glprg)
- {
- if (mCurrentFragmentProgram)
- mCurrentFragmentProgram->unbindProgram();
- mCurrentFragmentProgram = glprg;
- }
- break;
- case GPT_GEOMETRY_PROGRAM:
- if (mCurrentGeometryProgram != glprg)
- {
- if (mCurrentGeometryProgram)
- mCurrentGeometryProgram->unbindProgram();
- mCurrentGeometryProgram = glprg;
- }
- break;
- }
- // Bind the program
- glprg->bindProgram();
- RenderSystem::bindGpuProgram(prg);
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::unbindGpuProgram(GpuProgramType gptype)
- {
- if (gptype == GPT_VERTEX_PROGRAM && mCurrentVertexProgram)
- {
- mActiveVertexGpuProgramParameters = nullptr;
- mCurrentVertexProgram->unbindProgram();
- mCurrentVertexProgram = 0;
- }
- else if (gptype == GPT_GEOMETRY_PROGRAM && mCurrentGeometryProgram)
- {
- mActiveGeometryGpuProgramParameters = nullptr;
- mCurrentGeometryProgram->unbindProgram();
- mCurrentGeometryProgram = 0;
- }
- else if (gptype == GPT_FRAGMENT_PROGRAM && mCurrentFragmentProgram)
- {
- mActiveFragmentGpuProgramParameters = nullptr;
- mCurrentFragmentProgram->unbindProgram();
- mCurrentFragmentProgram = 0;
- }
- RenderSystem::unbindGpuProgram(gptype);
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::bindGpuProgramParameters(GpuProgramType gptype, GpuProgramParametersSharedPtr params, UINT16 mask)
- {
- if (mask & (UINT16)GPV_GLOBAL)
- {
- // We could maybe use GL_EXT_bindable_uniform here to produce Dx10-style
- // shared constant buffers, but GPU support seems fairly weak?
- // for now, just copy
- params->_copySharedParams();
- }
- switch (gptype)
- {
- case GPT_VERTEX_PROGRAM:
- mActiveVertexGpuProgramParameters = params;
- mCurrentVertexProgram->bindProgramParameters(params, mask);
- break;
- case GPT_GEOMETRY_PROGRAM:
- mActiveGeometryGpuProgramParameters = params;
- mCurrentGeometryProgram->bindProgramParameters(params, mask);
- break;
- case GPT_FRAGMENT_PROGRAM:
- mActiveFragmentGpuProgramParameters = params;
- mCurrentFragmentProgram->bindProgramParameters(params, mask);
- break;
- }
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::bindGpuProgramPassIterationParameters(GpuProgramType gptype)
- {
- switch (gptype)
- {
- case GPT_VERTEX_PROGRAM:
- mCurrentVertexProgram->bindProgramPassIterationParameters(mActiveVertexGpuProgramParameters);
- break;
- case GPT_GEOMETRY_PROGRAM:
- mCurrentGeometryProgram->bindProgramPassIterationParameters(mActiveGeometryGpuProgramParameters);
- break;
- case GPT_FRAGMENT_PROGRAM:
- mCurrentFragmentProgram->bindProgramPassIterationParameters(mActiveFragmentGpuProgramParameters);
- break;
- }
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::setClipPlanesImpl(const PlaneList& clipPlanes)
- {
- // A note on GL user clipping:
- // When an ARB vertex program is enabled in GL, user clipping is completely
- // disabled. There is no way around this, it's just turned off.
- // When using GLSL, user clipping can work but you have to include a
- // glClipVertex command in your vertex shader.
- // Thus the planes set here may not actually be respected.
- size_t i = 0;
- size_t numClipPlanes;
- GLdouble clipPlane[4];
- // Save previous modelview
- glMatrixMode(GL_MODELVIEW);
- glPushMatrix();
- // just load view matrix (identity world)
- GLfloat mat[16];
- makeGLMatrix(mat, mViewMatrix);
- glLoadMatrixf(mat);
- numClipPlanes = clipPlanes.size();
- for (i = 0; i < numClipPlanes; ++i)
- {
- GLenum clipPlaneId = static_cast<GLenum>(GL_CLIP_PLANE0 + i);
- const Plane& plane = clipPlanes[i];
- if (i >= 6/*GL_MAX_CLIP_PLANES*/)
- {
- OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Unable to set clip plane",
- "GLRenderSystem::setClipPlanes");
- }
- clipPlane[0] = plane.normal.x;
- clipPlane[1] = plane.normal.y;
- clipPlane[2] = plane.normal.z;
- clipPlane[3] = plane.d;
- glClipPlane(clipPlaneId, clipPlane);
- glEnable(clipPlaneId);
- }
- // disable remaining clip planes
- for ( ; i < 6/*GL_MAX_CLIP_PLANES*/; ++i)
- {
- glDisable(static_cast<GLenum>(GL_CLIP_PLANE0 + i));
- }
- // restore matrices
- glPopMatrix();
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::setScissorTest(bool enabled, size_t left,
- size_t top, size_t right, size_t bottom)
- {
- // If request texture flipping, use "upper-left", otherwise use "lower-left"
- bool flipping = mActiveRenderTarget->requiresTextureFlipping();
- // GL measures from the bottom, not the top
- size_t targetHeight = mActiveRenderTarget->getHeight();
- // Calculate the "lower-left" corner of the viewport
- GLsizei x = 0, y = 0, w = 0, h = 0;
- if (enabled)
- {
- glEnable(GL_SCISSOR_TEST);
- // NB GL uses width / height rather than right / bottom
- x = left;
- if (flipping)
- y = top;
- else
- y = targetHeight - bottom;
- w = right - left;
- h = bottom - top;
- glScissor(x, y, w, h);
- }
- else
- {
- glDisable(GL_SCISSOR_TEST);
- // GL requires you to reset the scissor when disabling
- w = mActiveViewport->getActualWidth();
- h = mActiveViewport->getActualHeight();
- x = mActiveViewport->getActualLeft();
- if (flipping)
- y = mActiveViewport->getActualTop();
- else
- y = targetHeight - mActiveViewport->getActualTop() - h;
- glScissor(x, y, w, h);
- }
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::clearFrameBuffer(unsigned int buffers,
- const ColourValue& colour, float depth, unsigned short stencil)
- {
- bool colourMask = !mColourWrite[0] || !mColourWrite[1]
- || !mColourWrite[2] || !mColourWrite[3];
- GLbitfield flags = 0;
- if (buffers & FBT_COLOUR)
- {
- flags |= GL_COLOR_BUFFER_BIT;
- // Enable buffer for writing if it isn't
- if (colourMask)
- {
- glColorMask(true, true, true, true);
- }
- glClearColor(colour.r, colour.g, colour.b, colour.a);
- }
- if (buffers & FBT_DEPTH)
- {
- flags |= GL_DEPTH_BUFFER_BIT;
- // Enable buffer for writing if it isn't
- if (!mDepthWrite)
- {
- glDepthMask( GL_TRUE );
- }
- glClearDepth(depth);
- }
- if (buffers & FBT_STENCIL)
- {
- flags |= GL_STENCIL_BUFFER_BIT;
- // Enable buffer for writing if it isn't
- glStencilMask(0xFFFFFFFF);
- glClearStencil(stencil);
- }
- // Should be enable scissor test due the clear region is
- // relied on scissor box bounds.
- GLboolean scissorTestEnabled = glIsEnabled(GL_SCISSOR_TEST);
- if (!scissorTestEnabled)
- {
- glEnable(GL_SCISSOR_TEST);
- }
- // Sets the scissor box as same as viewport
- GLint viewport[4] = { 0, 0, 0, 0 };
- GLint scissor[4] = { 0, 0, 0, 0 };
- glGetIntegerv(GL_VIEWPORT, viewport);
- glGetIntegerv(GL_SCISSOR_BOX, scissor);
- bool scissorBoxDifference =
- viewport[0] != scissor[0] || viewport[1] != scissor[1] ||
- viewport[2] != scissor[2] || viewport[3] != scissor[3];
- if (scissorBoxDifference)
- {
- glScissor(viewport[0], viewport[1], viewport[2], viewport[3]);
- }
- // Clear buffers
- glClear(flags);
- // Restore scissor box
- if (scissorBoxDifference)
- {
- glScissor(scissor[0], scissor[1], scissor[2], scissor[3]);
- }
- // Restore scissor test
- if (!scissorTestEnabled)
- {
- glDisable(GL_SCISSOR_TEST);
- }
- // Reset buffer write state
- if (!mDepthWrite && (buffers & FBT_DEPTH))
- {
- glDepthMask( GL_FALSE );
- }
- if (colourMask && (buffers & FBT_COLOUR))
- {
- glColorMask(mColourWrite[0], mColourWrite[1], mColourWrite[2], mColourWrite[3]);
- }
- if (buffers & FBT_STENCIL)
- {
- glStencilMask(mStencilMask);
- }
- }
- // ------------------------------------------------------------------
- void GLRenderSystem::_makeProjectionMatrix(float left, float right,
- float bottom, float top, float nearPlane, float farPlane, Matrix4& dest,
- bool forGpuProgram)
- {
- float width = right - left;
- float height = top - bottom;
- float q, qn;
- if (farPlane == 0)
- {
- // Infinite far plane
- q = Frustum::INFINITE_FAR_PLANE_ADJUST - 1;
- qn = nearPlane * (Frustum::INFINITE_FAR_PLANE_ADJUST - 2);
- }
- else
- {
- q = -(farPlane + nearPlane) / (farPlane - nearPlane);
- qn = -2 * (farPlane * nearPlane) / (farPlane - nearPlane);
- }
- dest = Matrix4::ZERO;
- dest[0][0] = 2 * nearPlane / width;
- dest[0][2] = (right+left) / width;
- dest[1][1] = 2 * nearPlane / height;
- dest[1][2] = (top+bottom) / height;
- dest[2][2] = q;
- dest[2][3] = qn;
- dest[3][2] = -1;
- }
- //---------------------------------------------------------------------
- HardwareOcclusionQuery* GLRenderSystem::createHardwareOcclusionQuery(void)
- {
- GLHardwareOcclusionQuery* ret = new GLHardwareOcclusionQuery();
- mHwOcclusionQueries.push_back(ret);
- return ret;
- }
- //---------------------------------------------------------------------
- float GLRenderSystem::getHorizontalTexelOffset(void)
- {
- // No offset in GL
- return 0.0f;
- }
- //---------------------------------------------------------------------
- float GLRenderSystem::getVerticalTexelOffset(void)
- {
- // No offset in GL
- return 0.0f;
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::_applyObliqueDepthProjection(Matrix4& matrix, const Plane& plane,
- bool forGpuProgram)
- {
- // Thanks to Eric Lenyel for posting this calculation at www.terathon.com
- // Calculate the clip-space corner point opposite the clipping plane
- // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
- // transform it into camera space by multiplying it
- // by the inverse of the projection matrix
- Vector4 q;
- q.x = (Math::Sign(plane.normal.x) + matrix[0][2]) / matrix[0][0];
- q.y = (Math::Sign(plane.normal.y) + matrix[1][2]) / matrix[1][1];
- q.z = -1.0F;
- q.w = (1.0F + matrix[2][2]) / matrix[2][3];
- // Calculate the scaled plane vector
- Vector4 clipPlane4d(plane.normal.x, plane.normal.y, plane.normal.z, plane.d);
- Vector4 c = clipPlane4d * (2.0F / (clipPlane4d.dotProduct(q)));
- // Replace the third row of the projection matrix
- matrix[2][0] = c.x;
- matrix[2][1] = c.y;
- matrix[2][2] = c.z + 1.0F;
- matrix[2][3] = c.w;
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::_oneTimeContextInitialization()
- {
- if (GLEW_VERSION_1_2)
- {
- // Set nicer lighting model -- d3d9 has this by default
- glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
- glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
- }
- if (GLEW_VERSION_1_4)
- {
- glEnable(GL_COLOR_SUM);
- glDisable(GL_DITHER);
- }
- // Check for FSAA
- // Enable the extension if it was enabled by the GLSupport
- if (mGLSupport->checkExtension("GL_ARB_multisample"))
- {
- int fsaa_active = false;
- glGetIntegerv(GL_SAMPLE_BUFFERS_ARB,(GLint*)&fsaa_active);
- if(fsaa_active)
- {
- glEnable(GL_MULTISAMPLE_ARB);
- }
- }
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::_switchContext(GLContext *context)
- {
- // Unbind GPU programs and rebind to new context later, because
- // scene manager treat render system as ONE 'context' ONLY, and it
- // cached the GPU programs using state.
- if (mCurrentVertexProgram)
- mCurrentVertexProgram->unbindProgram();
- if (mCurrentGeometryProgram)
- mCurrentGeometryProgram->unbindProgram();
- if (mCurrentFragmentProgram)
- mCurrentFragmentProgram->unbindProgram();
- // Disable lights
- for (unsigned short i = 0; i < mCurrentLights; ++i)
- {
- mLights[i] = NULL;
- }
- mCurrentLights = 0;
- // Disable textures
- _disableTextureUnitsFrom(0);
- // It's ready for switching
- if (mCurrentContext)
- mCurrentContext->endCurrent();
- mCurrentContext = context;
- mCurrentContext->setCurrent();
- // Check if the context has already done one-time initialisation
- if(!mCurrentContext->getInitialized())
- {
- _oneTimeContextInitialization();
- mCurrentContext->setInitialized();
- }
- // Rebind GPU programs to new context
- if (mCurrentVertexProgram)
- mCurrentVertexProgram->bindProgram();
- if (mCurrentGeometryProgram)
- mCurrentGeometryProgram->bindProgram();
- if (mCurrentFragmentProgram)
- mCurrentFragmentProgram->bindProgram();
- // Must reset depth/colour write mask to according with user desired, otherwise,
- // clearFrameBuffer would be wrong because the value we are recorded may be
- // difference with the really state stored in GL context.
- glDepthMask(mDepthWrite);
- glColorMask(mColourWrite[0], mColourWrite[1], mColourWrite[2], mColourWrite[3]);
- glStencilMask(mStencilMask);
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::_setRenderTarget(RenderTarget *target)
- {
- // Unbind frame buffer object
- if(mActiveRenderTarget)
- mRTTManager->unbind(mActiveRenderTarget);
- mActiveRenderTarget = target;
- // Switch context if different from current one
- GLContext *newContext = 0;
- target->getCustomAttribute("GLCONTEXT", &newContext);
- if(newContext && mCurrentContext != newContext)
- {
- _switchContext(newContext);
- }
- // Bind frame buffer object
- mRTTManager->bind(target);
- if (GLEW_EXT_framebuffer_sRGB)
- {
- // Enable / disable sRGB states
- if (target->isHardwareGammaEnabled())
- {
- glEnable(GL_FRAMEBUFFER_SRGB_EXT);
-
- // Note: could test GL_FRAMEBUFFER_SRGB_CAPABLE_EXT here before
- // enabling, but GL spec says incapable surfaces ignore the setting
- // anyway. We test the capability to enable isHardwareGammaEnabled.
- }
- else
- {
- glDisable(GL_FRAMEBUFFER_SRGB_EXT);
- }
- }
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::_unregisterContext(GLContext *context)
- {
- if(mCurrentContext == context) {
- // Change the context to something else so that a valid context
- // remains active. When this is the main context being unregistered,
- // we set the main context to 0.
- if(mCurrentContext != mMainContext) {
- _switchContext(mMainContext);
- } else {
- /// No contexts remain
- mCurrentContext->endCurrent();
- mCurrentContext = 0;
- mMainContext = 0;
- }
- }
- }
- //---------------------------------------------------------------------
- float GLRenderSystem::getMinimumDepthInputValue(void)
- {
- // Range [-1.0f, 1.0f]
- return -1.0f;
- }
- //---------------------------------------------------------------------
- float GLRenderSystem::getMaximumDepthInputValue(void)
- {
- // Range [-1.0f, 1.0f]
- return 1.0f;
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::registerThread()
- {
- CM_LOCK_MUTEX(mThreadInitMutex)
- // This is only valid once we've created the main context
- if (!mMainContext)
- {
- OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
- "Cannot register a background thread before the main context "
- "has been created.",
- "GLRenderSystem::registerThread");
- }
- // Create a new context for this thread. Cloning from the main context
- // will ensure that resources are shared with the main context
- // We want a separate context so that we can safely create GL
- // objects in parallel with the main thread
- GLContext* newContext = mMainContext->clone();
- mBackgroundContextList.push_back(newContext);
- // Bind this new context to this thread.
- newContext->setCurrent();
- _oneTimeContextInitialization();
- newContext->setInitialized();
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::unregisterThread()
- {
- // nothing to do here?
- // Don't need to worry about active context, just make sure we delete
- // on shutdown.
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::preExtraThreadsStarted()
- {
- CM_LOCK_MUTEX(mThreadInitMutex)
- // free context, we'll need this to share lists
- mCurrentContext->endCurrent();
- }
- //---------------------------------------------------------------------
- void GLRenderSystem::postExtraThreadsStarted()
- {
- CM_LOCK_MUTEX(mThreadInitMutex)
- // reacquire context
- mCurrentContext->setCurrent();
- }
- //---------------------------------------------------------------------
- bool GLRenderSystem::activateGLTextureUnit(size_t unit)
- {
- if (mActiveTextureUnit != unit)
- {
- if (GLEW_VERSION_1_2 && unit < getCapabilities()->getNumTextureUnits())
- {
- glActiveTextureARB(GL_TEXTURE0 + unit);
- mActiveTextureUnit = unit;
- return true;
- }
- else if (!unit)
- {
- // always ok to use the first unit
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return true;
- }
- }
- //---------------------------------------------------------------------
- unsigned int GLRenderSystem::getDisplayMonitorCount() const
- {
- return mGLSupport->getDisplayMonitorCount();
- }
- }
|