CNullDriver.cpp 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #include "CNullDriver.h"
  5. #include "os.h"
  6. #include "CImage.h"
  7. #include "CAttributes.h"
  8. #include "IReadFile.h"
  9. #include "IWriteFile.h"
  10. #include "IImageLoader.h"
  11. #include "IImageWriter.h"
  12. #include "IMaterialRenderer.h"
  13. #include "IAnimatedMeshSceneNode.h"
  14. #include "CMeshManipulator.h"
  15. #include "CColorConverter.h"
  16. #include "IAttributeExchangingObject.h"
  17. namespace irr
  18. {
  19. namespace video
  20. {
  21. //! creates a loader which is able to load windows bitmaps
  22. IImageLoader* createImageLoaderBMP();
  23. //! creates a loader which is able to load jpeg images
  24. IImageLoader* createImageLoaderJPG();
  25. //! creates a loader which is able to load targa images
  26. IImageLoader* createImageLoaderTGA();
  27. //! creates a loader which is able to load psd images
  28. IImageLoader* createImageLoaderPSD();
  29. //! creates a loader which is able to load dds images
  30. IImageLoader* createImageLoaderDDS();
  31. //! creates a loader which is able to load pcx images
  32. IImageLoader* createImageLoaderPCX();
  33. //! creates a loader which is able to load png images
  34. IImageLoader* createImageLoaderPNG();
  35. //! creates a loader which is able to load WAL images
  36. IImageLoader* createImageLoaderWAL();
  37. //! creates a loader which is able to load halflife images
  38. IImageLoader* createImageLoaderHalfLife();
  39. //! creates a loader which is able to load lmp images
  40. IImageLoader* createImageLoaderLMP();
  41. //! creates a loader which is able to load ppm/pgm/pbm images
  42. IImageLoader* createImageLoaderPPM();
  43. //! creates a loader which is able to load rgb images
  44. IImageLoader* createImageLoaderRGB();
  45. //! creates a writer which is able to save bmp images
  46. IImageWriter* createImageWriterBMP();
  47. //! creates a writer which is able to save jpg images
  48. IImageWriter* createImageWriterJPG();
  49. //! creates a writer which is able to save tga images
  50. IImageWriter* createImageWriterTGA();
  51. //! creates a writer which is able to save psd images
  52. IImageWriter* createImageWriterPSD();
  53. //! creates a writer which is able to save pcx images
  54. IImageWriter* createImageWriterPCX();
  55. //! creates a writer which is able to save png images
  56. IImageWriter* createImageWriterPNG();
  57. //! creates a writer which is able to save ppm images
  58. IImageWriter* createImageWriterPPM();
  59. //! constructor
  60. CNullDriver::CNullDriver(io::IFileSystem* io, const core::dimension2d<u32>& screenSize)
  61. : FileSystem(io), MeshManipulator(0), ViewPort(0,0,0,0), ScreenSize(screenSize),
  62. PrimitivesDrawn(0), MinVertexCountForVBO(500), TextureCreationFlags(0),
  63. OverrideMaterial2DEnabled(false), AllowZWriteOnTransparent(false)
  64. {
  65. #ifdef _DEBUG
  66. setDebugName("CNullDriver");
  67. #endif
  68. DriverAttributes = new io::CAttributes();
  69. DriverAttributes->addInt("MaxTextures", _IRR_MATERIAL_MAX_TEXTURES_);
  70. DriverAttributes->addInt("MaxSupportedTextures", _IRR_MATERIAL_MAX_TEXTURES_);
  71. DriverAttributes->addInt("MaxLights", getMaximalDynamicLightAmount());
  72. DriverAttributes->addInt("MaxAnisotropy", 1);
  73. // DriverAttributes->addInt("MaxUserClipPlanes", 0);
  74. // DriverAttributes->addInt("MaxAuxBuffers", 0);
  75. DriverAttributes->addInt("MaxMultipleRenderTargets", 1);
  76. DriverAttributes->addInt("MaxIndices", -1);
  77. DriverAttributes->addInt("MaxTextureSize", -1);
  78. // DriverAttributes->addInt("MaxGeometryVerticesOut", 0);
  79. // DriverAttributes->addFloat("MaxTextureLODBias", 0.f);
  80. DriverAttributes->addInt("Version", 1);
  81. DriverAttributes->setAttribute("MAX_TEXTURE_SIZE", core::dimension2du(2048, 2048));
  82. // DriverAttributes->addInt("ShaderLanguageVersion", 0);
  83. // DriverAttributes->addInt("AntiAlias", 0);
  84. setFog();
  85. setTextureCreationFlag(ETCF_ALWAYS_32_BIT, true);
  86. setTextureCreationFlag(ETCF_CREATE_MIP_MAPS, true);
  87. ViewPort = core::rect<s32>(core::position2d<s32>(0,0), core::dimension2di(screenSize));
  88. // create manipulator
  89. MeshManipulator = new scene::CMeshManipulator();
  90. if (FileSystem)
  91. FileSystem->grab();
  92. // create surface loader
  93. #ifdef _IRR_COMPILE_WITH_PNG_LOADER_
  94. SurfaceLoader.push_back(video::createImageLoaderPNG());
  95. #endif
  96. #ifdef _IRR_COMPILE_WITH_JPG_LOADER_
  97. SurfaceLoader.push_back(video::createImageLoaderJPG());
  98. #endif
  99. #ifdef _IRR_COMPILE_WITH_BMP_LOADER_
  100. SurfaceLoader.push_back(video::createImageLoaderBMP());
  101. #endif
  102. #ifdef _IRR_COMPILE_WITH_JPG_WRITER_
  103. SurfaceWriter.push_back(video::createImageWriterJPG());
  104. #endif
  105. #ifdef _IRR_COMPILE_WITH_PNG_WRITER_
  106. SurfaceWriter.push_back(video::createImageWriterPNG());
  107. #endif
  108. #ifdef _IRR_COMPILE_WITH_BMP_WRITER_
  109. SurfaceWriter.push_back(video::createImageWriterBMP());
  110. #endif
  111. // set ExposedData to 0
  112. memset(&ExposedData, 0, sizeof(ExposedData));
  113. for (u32 i=0; i<video::EVDF_COUNT; ++i)
  114. FeatureEnabled[i]=true;
  115. InitMaterial2D.AntiAliasing=video::EAAM_OFF;
  116. InitMaterial2D.Lighting=false;
  117. InitMaterial2D.ZWriteEnable=false;
  118. InitMaterial2D.ZBuffer=video::ECFN_NEVER;
  119. InitMaterial2D.UseMipMaps=false;
  120. for (u32 i=0; i<video::MATERIAL_MAX_TEXTURES; ++i)
  121. {
  122. InitMaterial2D.TextureLayer[i].BilinearFilter=false;
  123. InitMaterial2D.TextureLayer[i].TextureWrapU=video::ETC_REPEAT;
  124. InitMaterial2D.TextureLayer[i].TextureWrapV=video::ETC_REPEAT;
  125. }
  126. OverrideMaterial2D=InitMaterial2D;
  127. }
  128. //! destructor
  129. CNullDriver::~CNullDriver()
  130. {
  131. if (DriverAttributes)
  132. DriverAttributes->drop();
  133. if (FileSystem)
  134. FileSystem->drop();
  135. if (MeshManipulator)
  136. MeshManipulator->drop();
  137. deleteAllTextures();
  138. u32 i;
  139. for (i=0; i<SurfaceLoader.size(); ++i)
  140. SurfaceLoader[i]->drop();
  141. for (i=0; i<SurfaceWriter.size(); ++i)
  142. SurfaceWriter[i]->drop();
  143. // delete material renderers
  144. deleteMaterialRenders();
  145. // delete hardware mesh buffers
  146. removeAllHardwareBuffers();
  147. }
  148. //! Adds an external surface loader to the engine.
  149. void CNullDriver::addExternalImageLoader(IImageLoader* loader)
  150. {
  151. if (!loader)
  152. return;
  153. loader->grab();
  154. SurfaceLoader.push_back(loader);
  155. }
  156. //! Adds an external surface writer to the engine.
  157. void CNullDriver::addExternalImageWriter(IImageWriter* writer)
  158. {
  159. if (!writer)
  160. return;
  161. writer->grab();
  162. SurfaceWriter.push_back(writer);
  163. }
  164. //! Retrieve the number of image loaders
  165. u32 CNullDriver::getImageLoaderCount() const
  166. {
  167. return SurfaceLoader.size();
  168. }
  169. //! Retrieve the given image loader
  170. IImageLoader* CNullDriver::getImageLoader(u32 n)
  171. {
  172. if (n < SurfaceLoader.size())
  173. return SurfaceLoader[n];
  174. return 0;
  175. }
  176. //! Retrieve the number of image writers
  177. u32 CNullDriver::getImageWriterCount() const
  178. {
  179. return SurfaceWriter.size();
  180. }
  181. //! Retrieve the given image writer
  182. IImageWriter* CNullDriver::getImageWriter(u32 n)
  183. {
  184. if (n < SurfaceWriter.size())
  185. return SurfaceWriter[n];
  186. return 0;
  187. }
  188. //! deletes all textures
  189. void CNullDriver::deleteAllTextures()
  190. {
  191. // we need to remove previously set textures which might otherwise be kept in the
  192. // last set material member. Could be optimized to reduce state changes.
  193. setMaterial(SMaterial());
  194. for (u32 i=0; i<Textures.size(); ++i)
  195. Textures[i].Surface->drop();
  196. Textures.clear();
  197. }
  198. //! applications must call this method before performing any rendering. returns false if failed.
  199. bool CNullDriver::beginScene(bool backBuffer, bool zBuffer, SColor color,
  200. const SExposedVideoData& videoData, core::rect<s32>* sourceRect)
  201. {
  202. core::clearFPUException();
  203. PrimitivesDrawn = 0;
  204. return true;
  205. }
  206. //! applications must call this method after performing any rendering. returns false if failed.
  207. bool CNullDriver::endScene()
  208. {
  209. FPSCounter.registerFrame(os::Timer::getRealTime(), PrimitivesDrawn);
  210. updateAllHardwareBuffers();
  211. updateAllOcclusionQueries();
  212. return true;
  213. }
  214. //! Disable a feature of the driver.
  215. void CNullDriver::disableFeature(E_VIDEO_DRIVER_FEATURE feature, bool flag)
  216. {
  217. FeatureEnabled[feature]=!flag;
  218. }
  219. //! queries the features of the driver, returns true if feature is available
  220. bool CNullDriver::queryFeature(E_VIDEO_DRIVER_FEATURE feature) const
  221. {
  222. return false;
  223. }
  224. //! Get attributes of the actual video driver
  225. const io::IAttributes& CNullDriver::getDriverAttributes() const
  226. {
  227. return *DriverAttributes;
  228. }
  229. //! Get attributes of the actual video driver
  230. io::IAttributes& CNullDriver::getNonConstDriverAttributes()
  231. {
  232. return *DriverAttributes;
  233. }
  234. //! sets transformation
  235. void CNullDriver::setTransform(E_TRANSFORMATION_STATE state, const core::matrix4& mat)
  236. {
  237. }
  238. //! Returns the transformation set by setTransform
  239. const core::matrix4& CNullDriver::getTransform(E_TRANSFORMATION_STATE state) const
  240. {
  241. return TransformationMatrix;
  242. }
  243. //! sets a material
  244. void CNullDriver::setMaterial(const SMaterial& material)
  245. {
  246. }
  247. //! Removes a texture from the texture cache and deletes it, freeing lot of
  248. //! memory.
  249. void CNullDriver::removeTexture(ITexture* texture)
  250. {
  251. if (!texture)
  252. return;
  253. for (u32 i=0; i<Textures.size(); ++i)
  254. {
  255. if (Textures[i].Surface == texture)
  256. {
  257. texture->drop();
  258. Textures.erase(i);
  259. }
  260. }
  261. }
  262. //! Removes all texture from the texture cache and deletes them, freeing lot of
  263. //! memory.
  264. void CNullDriver::removeAllTextures()
  265. {
  266. setMaterial ( SMaterial() );
  267. deleteAllTextures();
  268. }
  269. //! Returns a texture by index
  270. ITexture* CNullDriver::getTextureByIndex(u32 i)
  271. {
  272. if ( i < Textures.size() )
  273. return Textures[i].Surface;
  274. return 0;
  275. }
  276. //! Returns amount of textures currently loaded
  277. u32 CNullDriver::getTextureCount() const
  278. {
  279. return Textures.size();
  280. }
  281. //! Renames a texture
  282. void CNullDriver::renameTexture(ITexture* texture, const io::path& newName)
  283. {
  284. // we can do a const_cast here safely, the name of the ITexture interface
  285. // is just readonly to prevent the user changing the texture name without invoking
  286. // this method, because the textures will need resorting afterwards
  287. io::SNamedPath& name = const_cast<io::SNamedPath&>(texture->getName());
  288. name.setPath(newName);
  289. Textures.sort();
  290. }
  291. //! loads a Texture
  292. ITexture* CNullDriver::getTexture(const io::path& filename)
  293. {
  294. // Identify textures by their absolute filenames if possible.
  295. const io::path absolutePath = FileSystem->getAbsolutePath(filename);
  296. ITexture* texture = findTexture(absolutePath);
  297. if (texture)
  298. return texture;
  299. // Then try the raw filename, which might be in an Archive
  300. texture = findTexture(filename);
  301. if (texture)
  302. return texture;
  303. // Now try to open the file using the complete path.
  304. io::IReadFile* file = FileSystem->createAndOpenFile(absolutePath);
  305. if (!file)
  306. {
  307. // Try to open it using the raw filename.
  308. file = FileSystem->createAndOpenFile(filename);
  309. }
  310. if (file)
  311. {
  312. // Re-check name for actual archive names
  313. texture = findTexture(file->getFileName());
  314. if (texture)
  315. {
  316. file->drop();
  317. return texture;
  318. }
  319. texture = loadTextureFromFile(file);
  320. file->drop();
  321. if (texture)
  322. {
  323. addTexture(texture);
  324. texture->drop(); // drop it because we created it, one grab too much
  325. }
  326. else
  327. os::Printer::log("Could not load texture", filename, ELL_ERROR);
  328. return texture;
  329. }
  330. else
  331. {
  332. os::Printer::log("Could not open file of texture", filename, ELL_WARNING);
  333. return 0;
  334. }
  335. }
  336. //! loads a Texture
  337. ITexture* CNullDriver::getTexture(io::IReadFile* file)
  338. {
  339. ITexture* texture = 0;
  340. if (file)
  341. {
  342. texture = findTexture(file->getFileName());
  343. if (texture)
  344. return texture;
  345. texture = loadTextureFromFile(file);
  346. if (texture)
  347. {
  348. addTexture(texture);
  349. texture->drop(); // drop it because we created it, one grab too much
  350. }
  351. if (!texture)
  352. os::Printer::log("Could not load texture", file->getFileName(), ELL_WARNING);
  353. }
  354. return texture;
  355. }
  356. //! opens the file and loads it into the surface
  357. video::ITexture* CNullDriver::loadTextureFromFile(io::IReadFile* file, const io::path& hashName )
  358. {
  359. ITexture* texture = 0;
  360. IImage* image = createImageFromFile(file);
  361. if (image)
  362. {
  363. // create texture from surface
  364. texture = createDeviceDependentTexture(image, hashName.size() ? hashName : file->getFileName() );
  365. os::Printer::log("Loaded texture", file->getFileName());
  366. image->drop();
  367. }
  368. return texture;
  369. }
  370. //! adds a surface, not loaded or created by the Irrlicht Engine
  371. void CNullDriver::addTexture(video::ITexture* texture)
  372. {
  373. if (texture)
  374. {
  375. SSurface s;
  376. s.Surface = texture;
  377. texture->grab();
  378. Textures.push_back(s);
  379. // the new texture is now at the end of the texture list. when searching for
  380. // the next new texture, the texture array will be sorted and the index of this texture
  381. // will be changed. to let the order be more consistent to the user, sort
  382. // the textures now already although this isn't necessary:
  383. Textures.sort();
  384. }
  385. }
  386. //! looks if the image is already loaded
  387. video::ITexture* CNullDriver::findTexture(const io::path& filename)
  388. {
  389. SSurface s;
  390. SDummyTexture dummy(filename);
  391. s.Surface = &dummy;
  392. s32 index = Textures.binary_search(s);
  393. if (index != -1)
  394. return Textures[index].Surface;
  395. return 0;
  396. }
  397. //! Creates a texture from a loaded IImage.
  398. ITexture* CNullDriver::addTexture(const io::path& name, IImage* image, void* mipmapData)
  399. {
  400. if ( 0 == name.size() || !image)
  401. return 0;
  402. ITexture* t = createDeviceDependentTexture(image, name, mipmapData);
  403. if (t)
  404. {
  405. addTexture(t);
  406. t->drop();
  407. }
  408. return t;
  409. }
  410. //! creates a Texture
  411. ITexture* CNullDriver::addTexture(const core::dimension2d<u32>& size,
  412. const io::path& name, ECOLOR_FORMAT format)
  413. {
  414. if(IImage::isRenderTargetOnlyFormat(format))
  415. {
  416. os::Printer::log("Could not create ITexture, format only supported for render target textures.", ELL_WARNING);
  417. return 0;
  418. }
  419. if ( 0 == name.size () )
  420. return 0;
  421. IImage* image = new CImage(format, size);
  422. ITexture* t = createDeviceDependentTexture(image, name);
  423. image->drop();
  424. addTexture(t);
  425. if (t)
  426. t->drop();
  427. return t;
  428. }
  429. //! returns a device dependent texture from a software surface (IImage)
  430. //! THIS METHOD HAS TO BE OVERRIDDEN BY DERIVED DRIVERS WITH OWN TEXTURES
  431. ITexture* CNullDriver::createDeviceDependentTexture(IImage* surface, const io::path& name, void* mipmapData)
  432. {
  433. return new SDummyTexture(name);
  434. }
  435. //! set or reset special render targets
  436. bool CNullDriver::setRenderTarget(video::E_RENDER_TARGET target, bool clearTarget,
  437. bool clearZBuffer, SColor color)
  438. {
  439. if (ERT_FRAME_BUFFER==target)
  440. return setRenderTarget(0,clearTarget, clearZBuffer, color);
  441. else
  442. return false;
  443. }
  444. //! sets a render target
  445. bool CNullDriver::setRenderTarget(video::ITexture* texture, bool clearBackBuffer,
  446. bool clearZBuffer, SColor color)
  447. {
  448. return false;
  449. }
  450. //! Sets multiple render targets
  451. bool CNullDriver::setRenderTarget(const core::array<video::IRenderTarget>& texture,
  452. bool clearBackBuffer, bool clearZBuffer, SColor color)
  453. {
  454. return false;
  455. }
  456. //! sets a viewport
  457. void CNullDriver::setViewPort(const core::rect<s32>& area)
  458. {
  459. }
  460. //! gets the area of the current viewport
  461. const core::rect<s32>& CNullDriver::getViewPort() const
  462. {
  463. return ViewPort;
  464. }
  465. //! draws a vertex primitive list
  466. void CNullDriver::drawVertexPrimitiveList(const void* vertices, u32 vertexCount, const void* indexList, u32 primitiveCount, E_VERTEX_TYPE vType, scene::E_PRIMITIVE_TYPE pType, E_INDEX_TYPE iType)
  467. {
  468. if ((iType==EIT_16BIT) && (vertexCount>65536))
  469. os::Printer::log("Too many vertices for 16bit index type, render artifacts may occur.");
  470. PrimitivesDrawn += primitiveCount;
  471. }
  472. //! draws a vertex primitive list in 2d
  473. void CNullDriver::draw2DVertexPrimitiveList(const void* vertices, u32 vertexCount, const void* indexList, u32 primitiveCount, E_VERTEX_TYPE vType, scene::E_PRIMITIVE_TYPE pType, E_INDEX_TYPE iType)
  474. {
  475. if ((iType==EIT_16BIT) && (vertexCount>65536))
  476. os::Printer::log("Too many vertices for 16bit index type, render artifacts may occur.");
  477. PrimitivesDrawn += primitiveCount;
  478. }
  479. //! Draws a 3d line.
  480. void CNullDriver::draw3DLine(const core::vector3df& start,
  481. const core::vector3df& end, SColor color)
  482. {
  483. }
  484. //! Draws a 3d triangle.
  485. void CNullDriver::draw3DTriangle(const core::triangle3df& triangle, SColor color)
  486. {
  487. S3DVertex vertices[3];
  488. vertices[0].Pos=triangle.pointA;
  489. vertices[0].Color=color;
  490. vertices[0].Normal=triangle.getNormal().normalize();
  491. vertices[0].TCoords.set(0.f,0.f);
  492. vertices[1].Pos=triangle.pointB;
  493. vertices[1].Color=color;
  494. vertices[1].Normal=vertices[0].Normal;
  495. vertices[1].TCoords.set(0.5f,1.f);
  496. vertices[2].Pos=triangle.pointC;
  497. vertices[2].Color=color;
  498. vertices[2].Normal=vertices[0].Normal;
  499. vertices[2].TCoords.set(1.f,0.f);
  500. const u16 indexList[] = {0,1,2};
  501. drawVertexPrimitiveList(vertices, 3, indexList, 1, EVT_STANDARD, scene::EPT_TRIANGLES, EIT_16BIT);
  502. }
  503. //! Draws a 3d axis aligned box.
  504. void CNullDriver::draw3DBox(const core::aabbox3d<f32>& box, SColor color)
  505. {
  506. core::vector3df edges[8];
  507. box.getEdges(edges);
  508. // TODO: optimize into one big drawIndexPrimitive call.
  509. draw3DLine(edges[5], edges[1], color);
  510. draw3DLine(edges[1], edges[3], color);
  511. draw3DLine(edges[3], edges[7], color);
  512. draw3DLine(edges[7], edges[5], color);
  513. draw3DLine(edges[0], edges[2], color);
  514. draw3DLine(edges[2], edges[6], color);
  515. draw3DLine(edges[6], edges[4], color);
  516. draw3DLine(edges[4], edges[0], color);
  517. draw3DLine(edges[1], edges[0], color);
  518. draw3DLine(edges[3], edges[2], color);
  519. draw3DLine(edges[7], edges[6], color);
  520. draw3DLine(edges[5], edges[4], color);
  521. }
  522. //! draws an 2d image
  523. void CNullDriver::draw2DImage(const video::ITexture* texture, const core::position2d<s32>& destPos)
  524. {
  525. if (!texture)
  526. return;
  527. draw2DImage(texture,destPos, core::rect<s32>(core::position2d<s32>(0,0),
  528. core::dimension2di(texture->getOriginalSize())));
  529. }
  530. //! draws a set of 2d images, using a color and the alpha channel of the
  531. //! texture if desired. The images are drawn beginning at pos and concatenated
  532. //! in one line. All drawings are clipped against clipRect (if != 0).
  533. //! The subtextures are defined by the array of sourceRects and are chosen
  534. //! by the indices given.
  535. void CNullDriver::draw2DImageBatch(const video::ITexture* texture,
  536. const core::position2d<s32>& pos,
  537. const core::array<core::rect<s32> >& sourceRects,
  538. const core::array<s32>& indices,
  539. s32 kerningWidth,
  540. const core::rect<s32>* clipRect, SColor color,
  541. bool useAlphaChannelOfTexture)
  542. {
  543. core::position2d<s32> target(pos);
  544. for (u32 i=0; i<indices.size(); ++i)
  545. {
  546. draw2DImage(texture, target, sourceRects[indices[i]],
  547. clipRect, color, useAlphaChannelOfTexture);
  548. target.X += sourceRects[indices[i]].getWidth();
  549. target.X += kerningWidth;
  550. }
  551. }
  552. //! draws a set of 2d images, using a color and the alpha channel of the
  553. //! texture if desired.
  554. void CNullDriver::draw2DImageBatch(const video::ITexture* texture,
  555. const core::array<core::position2d<s32> >& positions,
  556. const core::array<core::rect<s32> >& sourceRects,
  557. const core::rect<s32>* clipRect,
  558. SColor color,
  559. bool useAlphaChannelOfTexture)
  560. {
  561. const irr::u32 drawCount = core::min_<u32>(positions.size(), sourceRects.size());
  562. for (u32 i=0; i<drawCount; ++i)
  563. {
  564. draw2DImage(texture, positions[i], sourceRects[i],
  565. clipRect, color, useAlphaChannelOfTexture);
  566. }
  567. }
  568. //! Draws a part of the texture into the rectangle.
  569. void CNullDriver::draw2DImage(const video::ITexture* texture, const core::rect<s32>& destRect,
  570. const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect,
  571. const video::SColor* const colors, bool useAlphaChannelOfTexture)
  572. {
  573. if (destRect.isValid())
  574. draw2DImage(texture, core::position2d<s32>(destRect.UpperLeftCorner),
  575. sourceRect, clipRect, colors?colors[0]:video::SColor(0xffffffff),
  576. useAlphaChannelOfTexture);
  577. }
  578. //! Draws a 2d image, using a color (if color is other then Color(255,255,255,255)) and the alpha channel of the texture if wanted.
  579. void CNullDriver::draw2DImage(const video::ITexture* texture, const core::position2d<s32>& destPos,
  580. const core::rect<s32>& sourceRect,
  581. const core::rect<s32>* clipRect, SColor color,
  582. bool useAlphaChannelOfTexture)
  583. {
  584. }
  585. //! Draws the outline of a 2d rectangle
  586. void CNullDriver::draw2DRectangleOutline(const core::recti& pos, SColor color)
  587. {
  588. draw2DLine(pos.UpperLeftCorner, core::position2di(pos.LowerRightCorner.X, pos.UpperLeftCorner.Y), color);
  589. draw2DLine(core::position2di(pos.LowerRightCorner.X, pos.UpperLeftCorner.Y), pos.LowerRightCorner, color);
  590. draw2DLine(pos.LowerRightCorner, core::position2di(pos.UpperLeftCorner.X, pos.LowerRightCorner.Y), color);
  591. draw2DLine(core::position2di(pos.UpperLeftCorner.X, pos.LowerRightCorner.Y), pos.UpperLeftCorner, color);
  592. }
  593. //! Draw a 2d rectangle
  594. void CNullDriver::draw2DRectangle(SColor color, const core::rect<s32>& pos, const core::rect<s32>* clip)
  595. {
  596. draw2DRectangle(pos, color, color, color, color, clip);
  597. }
  598. //! Draws a 2d rectangle with a gradient.
  599. void CNullDriver::draw2DRectangle(const core::rect<s32>& pos,
  600. SColor colorLeftUp, SColor colorRightUp, SColor colorLeftDown, SColor colorRightDown,
  601. const core::rect<s32>* clip)
  602. {
  603. }
  604. //! Draws a 2d line.
  605. void CNullDriver::draw2DLine(const core::position2d<s32>& start,
  606. const core::position2d<s32>& end, SColor color)
  607. {
  608. }
  609. //! Draws a pixel
  610. void CNullDriver::drawPixel(u32 x, u32 y, const SColor & color)
  611. {
  612. }
  613. //! Draws a non filled concyclic regular 2d polyon.
  614. void CNullDriver::draw2DPolygon(core::position2d<s32> center,
  615. f32 radius, video::SColor color, s32 count)
  616. {
  617. if (count < 2)
  618. return;
  619. core::position2d<s32> first;
  620. core::position2d<s32> a,b;
  621. for (s32 j=0; j<count; ++j)
  622. {
  623. b = a;
  624. f32 p = j / (f32)count * (core::PI*2);
  625. a = center + core::position2d<s32>((s32)(sin(p)*radius), (s32)(cos(p)*radius));
  626. if (j==0)
  627. first = a;
  628. else
  629. draw2DLine(a, b, color);
  630. }
  631. draw2DLine(a, first, color);
  632. }
  633. //! returns color format
  634. ECOLOR_FORMAT CNullDriver::getColorFormat() const
  635. {
  636. return ECF_R5G6B5;
  637. }
  638. //! returns screen size
  639. const core::dimension2d<u32>& CNullDriver::getScreenSize() const
  640. {
  641. return ScreenSize;
  642. }
  643. //! returns the current render target size,
  644. //! or the screen size if render targets are not implemented
  645. const core::dimension2d<u32>& CNullDriver::getCurrentRenderTargetSize() const
  646. {
  647. return ScreenSize;
  648. }
  649. // returns current frames per second value
  650. s32 CNullDriver::getFPS() const
  651. {
  652. return FPSCounter.getFPS();
  653. }
  654. //! returns amount of primitives (mostly triangles) were drawn in the last frame.
  655. //! very useful method for statistics.
  656. u32 CNullDriver::getPrimitiveCountDrawn( u32 param ) const
  657. {
  658. return (0 == param) ? FPSCounter.getPrimitive() : (1 == param) ? FPSCounter.getPrimitiveAverage() : FPSCounter.getPrimitiveTotal();
  659. }
  660. //! Sets the dynamic ambient light color. The default color is
  661. //! (0,0,0,0) which means it is dark.
  662. //! \param color: New color of the ambient light.
  663. void CNullDriver::setAmbientLight(const SColorf& color)
  664. {
  665. }
  666. //! \return Returns the name of the video driver. Example: In case of the DIRECT3D8
  667. //! driver, it would return "Direct3D8".
  668. const wchar_t* CNullDriver::getName() const
  669. {
  670. return L"Irrlicht NullDevice";
  671. }
  672. //! Draws a shadow volume into the stencil buffer. To draw a stencil shadow, do
  673. //! this: Frist, draw all geometry. Then use this method, to draw the shadow
  674. //! volume. Then, use IVideoDriver::drawStencilShadow() to visualize the shadow.
  675. void CNullDriver::drawStencilShadowVolume(const core::array<core::vector3df>& triangles, bool zfail, u32 debugDataVisible)
  676. {
  677. }
  678. //! Fills the stencil shadow with color. After the shadow volume has been drawn
  679. //! into the stencil buffer using IVideoDriver::drawStencilShadowVolume(), use this
  680. //! to draw the color of the shadow.
  681. void CNullDriver::drawStencilShadow(bool clearStencilBuffer,
  682. video::SColor leftUpEdge, video::SColor rightUpEdge,
  683. video::SColor leftDownEdge, video::SColor rightDownEdge)
  684. {
  685. }
  686. //! deletes all dynamic lights there are
  687. void CNullDriver::deleteAllDynamicLights()
  688. {
  689. Lights.set_used(0);
  690. }
  691. //! adds a dynamic light
  692. s32 CNullDriver::addDynamicLight(const SLight& light)
  693. {
  694. Lights.push_back(light);
  695. return Lights.size() - 1;
  696. }
  697. //! Turns a dynamic light on or off
  698. //! \param lightIndex: the index returned by addDynamicLight
  699. //! \param turnOn: true to turn the light on, false to turn it off
  700. void CNullDriver::turnLightOn(s32 lightIndex, bool turnOn)
  701. {
  702. // Do nothing
  703. }
  704. //! returns the maximal amount of dynamic lights the device can handle
  705. u32 CNullDriver::getMaximalDynamicLightAmount() const
  706. {
  707. return 0;
  708. }
  709. //! Returns current amount of dynamic lights set
  710. //! \return Current amount of dynamic lights set
  711. u32 CNullDriver::getDynamicLightCount() const
  712. {
  713. return Lights.size();
  714. }
  715. //! Returns light data which was previously set by IVideoDriver::addDynamicLight().
  716. //! \param idx: Zero based index of the light. Must be greater than 0 and smaller
  717. //! than IVideoDriver()::getDynamicLightCount.
  718. //! \return Light data.
  719. const SLight& CNullDriver::getDynamicLight(u32 idx) const
  720. {
  721. if ( idx < Lights.size() )
  722. return Lights[idx];
  723. else
  724. return *((SLight*)0);
  725. }
  726. //! Creates a boolean alpha channel of the texture based of an color key.
  727. void CNullDriver::makeColorKeyTexture(video::ITexture* texture,
  728. video::SColor color,
  729. bool zeroTexels) const
  730. {
  731. if (!texture)
  732. return;
  733. if (texture->getColorFormat() != ECF_A1R5G5B5 &&
  734. texture->getColorFormat() != ECF_A8R8G8B8 )
  735. {
  736. os::Printer::log("Error: Unsupported texture color format for making color key channel.", ELL_ERROR);
  737. return;
  738. }
  739. if (texture->getColorFormat() == ECF_A1R5G5B5)
  740. {
  741. u16 *p = (u16*)texture->lock();
  742. if (!p)
  743. {
  744. os::Printer::log("Could not lock texture for making color key channel.", ELL_ERROR);
  745. return;
  746. }
  747. const core::dimension2d<u32> dim = texture->getSize();
  748. const u32 pitch = texture->getPitch() / 2;
  749. // color with alpha disabled (i.e. fully transparent)
  750. const u16 refZeroAlpha = (0x7fff & color.toA1R5G5B5());
  751. const u32 pixels = pitch * dim.Height;
  752. for (u32 pixel = 0; pixel < pixels; ++ pixel)
  753. {
  754. // If the color matches the reference color, ignoring alphas,
  755. // set the alpha to zero.
  756. if(((*p) & 0x7fff) == refZeroAlpha)
  757. {
  758. if(zeroTexels)
  759. (*p) = 0;
  760. else
  761. (*p) = refZeroAlpha;
  762. }
  763. ++p;
  764. }
  765. texture->unlock();
  766. }
  767. else
  768. {
  769. u32 *p = (u32*)texture->lock();
  770. if (!p)
  771. {
  772. os::Printer::log("Could not lock texture for making color key channel.", ELL_ERROR);
  773. return;
  774. }
  775. core::dimension2d<u32> dim = texture->getSize();
  776. u32 pitch = texture->getPitch() / 4;
  777. // color with alpha disabled (fully transparent)
  778. const u32 refZeroAlpha = 0x00ffffff & color.color;
  779. const u32 pixels = pitch * dim.Height;
  780. for (u32 pixel = 0; pixel < pixels; ++ pixel)
  781. {
  782. // If the color matches the reference color, ignoring alphas,
  783. // set the alpha to zero.
  784. if(((*p) & 0x00ffffff) == refZeroAlpha)
  785. {
  786. if(zeroTexels)
  787. (*p) = 0;
  788. else
  789. (*p) = refZeroAlpha;
  790. }
  791. ++p;
  792. }
  793. texture->unlock();
  794. }
  795. texture->regenerateMipMapLevels();
  796. }
  797. //! Creates an boolean alpha channel of the texture based of an color key position.
  798. void CNullDriver::makeColorKeyTexture(video::ITexture* texture,
  799. core::position2d<s32> colorKeyPixelPos,
  800. bool zeroTexels) const
  801. {
  802. if (!texture)
  803. return;
  804. if (texture->getColorFormat() != ECF_A1R5G5B5 &&
  805. texture->getColorFormat() != ECF_A8R8G8B8 )
  806. {
  807. os::Printer::log("Error: Unsupported texture color format for making color key channel.", ELL_ERROR);
  808. return;
  809. }
  810. SColor colorKey;
  811. if (texture->getColorFormat() == ECF_A1R5G5B5)
  812. {
  813. u16 *p = (u16*)texture->lock(ETLM_READ_ONLY);
  814. if (!p)
  815. {
  816. os::Printer::log("Could not lock texture for making color key channel.", ELL_ERROR);
  817. return;
  818. }
  819. u32 pitch = texture->getPitch() / 2;
  820. const u16 key16Bit = 0x7fff & p[colorKeyPixelPos.Y*pitch + colorKeyPixelPos.X];
  821. colorKey = video::A1R5G5B5toA8R8G8B8(key16Bit);
  822. }
  823. else
  824. {
  825. u32 *p = (u32*)texture->lock(ETLM_READ_ONLY);
  826. if (!p)
  827. {
  828. os::Printer::log("Could not lock texture for making color key channel.", ELL_ERROR);
  829. return;
  830. }
  831. u32 pitch = texture->getPitch() / 4;
  832. colorKey = 0x00ffffff & p[colorKeyPixelPos.Y*pitch + colorKeyPixelPos.X];
  833. }
  834. texture->unlock();
  835. makeColorKeyTexture(texture, colorKey, zeroTexels);
  836. }
  837. //! Creates a normal map from a height map texture.
  838. //! \param amplitude: Constant value by which the height information is multiplied.
  839. void CNullDriver::makeNormalMapTexture(video::ITexture* texture, f32 amplitude) const
  840. {
  841. if (!texture)
  842. return;
  843. if (texture->getColorFormat() != ECF_A1R5G5B5 &&
  844. texture->getColorFormat() != ECF_A8R8G8B8 )
  845. {
  846. os::Printer::log("Error: Unsupported texture color format for making normal map.", ELL_ERROR);
  847. return;
  848. }
  849. core::dimension2d<u32> dim = texture->getSize();
  850. amplitude = amplitude / 255.0f;
  851. f32 vh = dim.Height / (f32)dim.Width;
  852. f32 hh = dim.Width / (f32)dim.Height;
  853. if (texture->getColorFormat() == ECF_A8R8G8B8)
  854. {
  855. // ECF_A8R8G8B8 version
  856. s32 *p = (s32*)texture->lock();
  857. if (!p)
  858. {
  859. os::Printer::log("Could not lock texture for making normal map.", ELL_ERROR);
  860. return;
  861. }
  862. // copy texture
  863. u32 pitch = texture->getPitch() / 4;
  864. s32* in = new s32[dim.Height * pitch];
  865. memcpy(in, p, dim.Height * pitch * 4);
  866. for (s32 x=0; x < s32(pitch); ++x)
  867. for (s32 y=0; y < s32(dim.Height); ++y)
  868. {
  869. // TODO: this could be optimized really a lot
  870. core::vector3df h1((x-1)*hh, nml32(x-1, y, pitch, dim.Height, in)*amplitude, y*vh);
  871. core::vector3df h2((x+1)*hh, nml32(x+1, y, pitch, dim.Height, in)*amplitude, y*vh);
  872. //core::vector3df v1(x*hh, nml32(x, y-1, pitch, dim.Height, in)*amplitude, (y-1)*vh);
  873. //core::vector3df v2(x*hh, nml32(x, y+1, pitch, dim.Height, in)*amplitude, (y+1)*vh);
  874. core::vector3df v1(x*hh, nml32(x, y+1, pitch, dim.Height, in)*amplitude, (y-1)*vh);
  875. core::vector3df v2(x*hh, nml32(x, y-1, pitch, dim.Height, in)*amplitude, (y+1)*vh);
  876. core::vector3df v = v1-v2;
  877. core::vector3df h = h1-h2;
  878. core::vector3df n = v.crossProduct(h);
  879. n.normalize();
  880. n *= 0.5f;
  881. n += core::vector3df(0.5f,0.5f,0.5f); // now between 0 and 1
  882. n *= 255.0f;
  883. s32 height = (s32)nml32(x, y, pitch, dim.Height, in);
  884. p[y*pitch + x] = video::SColor(
  885. height, // store height in alpha
  886. (s32)n.X, (s32)n.Z, (s32)n.Y).color;
  887. }
  888. delete [] in;
  889. texture->unlock();
  890. }
  891. else
  892. {
  893. // ECF_A1R5G5B5 version
  894. s16 *p = (s16*)texture->lock();
  895. if (!p)
  896. {
  897. os::Printer::log("Could not lock texture for making normal map.", ELL_ERROR);
  898. return;
  899. }
  900. u32 pitch = texture->getPitch() / 2;
  901. // copy texture
  902. s16* in = new s16[dim.Height * pitch];
  903. memcpy(in, p, dim.Height * pitch * 2);
  904. for (s32 x=0; x < s32(pitch); ++x)
  905. for (s32 y=0; y < s32(dim.Height); ++y)
  906. {
  907. // TODO: this could be optimized really a lot
  908. core::vector3df h1((x-1)*hh, nml16(x-1, y, pitch, dim.Height, in)*amplitude, y*vh);
  909. core::vector3df h2((x+1)*hh, nml16(x+1, y, pitch, dim.Height, in)*amplitude, y*vh);
  910. core::vector3df v1(x*hh, nml16(x, y-1, pitch, dim.Height, in)*amplitude, (y-1)*vh);
  911. core::vector3df v2(x*hh, nml16(x, y+1, pitch, dim.Height, in)*amplitude, (y+1)*vh);
  912. core::vector3df v = v1-v2;
  913. core::vector3df h = h1-h2;
  914. core::vector3df n = v.crossProduct(h);
  915. n.normalize();
  916. n *= 0.5f;
  917. n += core::vector3df(0.5f,0.5f,0.5f); // now between 0 and 1
  918. n *= 255.0f;
  919. p[y*pitch + x] = video::RGBA16((u32)n.X, (u32)n.Z, (u32)n.Y);
  920. }
  921. delete [] in;
  922. texture->unlock();
  923. }
  924. texture->regenerateMipMapLevels();
  925. }
  926. //! Returns the maximum amount of primitives (mostly vertices) which
  927. //! the device is able to render with one drawIndexedTriangleList
  928. //! call.
  929. u32 CNullDriver::getMaximalPrimitiveCount() const
  930. {
  931. return 0xFFFFFFFF;
  932. }
  933. //! checks triangle count and print warning if wrong
  934. bool CNullDriver::checkPrimitiveCount(u32 prmCount) const
  935. {
  936. const u32 m = getMaximalPrimitiveCount();
  937. if (prmCount > m)
  938. {
  939. char tmp[1024];
  940. sprintf(tmp,"Could not draw triangles, too many primitives(%u), maxium is %u.", prmCount, m);
  941. os::Printer::log(tmp, ELL_ERROR);
  942. return false;
  943. }
  944. return true;
  945. }
  946. //! Enables or disables a texture creation flag.
  947. void CNullDriver::setTextureCreationFlag(E_TEXTURE_CREATION_FLAG flag, bool enabled)
  948. {
  949. if (enabled && ((flag == ETCF_ALWAYS_16_BIT) || (flag == ETCF_ALWAYS_32_BIT)
  950. || (flag == ETCF_OPTIMIZED_FOR_QUALITY) || (flag == ETCF_OPTIMIZED_FOR_SPEED)))
  951. {
  952. // disable other formats
  953. setTextureCreationFlag(ETCF_ALWAYS_16_BIT, false);
  954. setTextureCreationFlag(ETCF_ALWAYS_32_BIT, false);
  955. setTextureCreationFlag(ETCF_OPTIMIZED_FOR_QUALITY, false);
  956. setTextureCreationFlag(ETCF_OPTIMIZED_FOR_SPEED, false);
  957. }
  958. // set flag
  959. TextureCreationFlags = (TextureCreationFlags & (~flag)) |
  960. ((((u32)!enabled)-1) & flag);
  961. }
  962. //! Returns if a texture creation flag is enabled or disabled.
  963. bool CNullDriver::getTextureCreationFlag(E_TEXTURE_CREATION_FLAG flag) const
  964. {
  965. return (TextureCreationFlags & flag)!=0;
  966. }
  967. //! Creates a software image from a file.
  968. IImage* CNullDriver::createImageFromFile(const io::path& filename)
  969. {
  970. if (!filename.size())
  971. return 0;
  972. IImage* image = 0;
  973. io::IReadFile* file = FileSystem->createAndOpenFile(filename);
  974. if (file)
  975. {
  976. image = createImageFromFile(file);
  977. file->drop();
  978. }
  979. else
  980. os::Printer::log("Could not open file of image", filename, ELL_WARNING);
  981. return image;
  982. }
  983. //! Creates a software image from a file.
  984. IImage* CNullDriver::createImageFromFile(io::IReadFile* file, video::IImageLoader** loader)
  985. {
  986. if (!file)
  987. return 0;
  988. IImage* image = 0;
  989. s32 i;
  990. // try to load file based on file extension
  991. for (i=SurfaceLoader.size()-1; i>=0; --i)
  992. {
  993. if (SurfaceLoader[i]->isALoadableFileExtension(file->getFileName()))
  994. {
  995. if (loader)
  996. {
  997. *loader = SurfaceLoader[i];
  998. return 0;
  999. }
  1000. // reset file position which might have changed due to previous loadImage calls
  1001. file->seek(0);
  1002. image = SurfaceLoader[i]->loadImage(file);
  1003. if (image)
  1004. return image;
  1005. }
  1006. }
  1007. // try to load file based on what is in it
  1008. for (i=SurfaceLoader.size()-1; i>=0; --i)
  1009. {
  1010. // dito
  1011. file->seek(0);
  1012. if (SurfaceLoader[i]->isALoadableFileFormat(file))
  1013. {
  1014. if (loader)
  1015. {
  1016. *loader = SurfaceLoader[i];
  1017. return 0;
  1018. }
  1019. file->seek(0);
  1020. image = SurfaceLoader[i]->loadImage(file);
  1021. if (image)
  1022. return image;
  1023. }
  1024. }
  1025. return 0; // failed to load
  1026. }
  1027. //! Writes the provided image to disk file
  1028. bool CNullDriver::writeImageToFile(IImage* image, const io::path& filename,u32 param)
  1029. {
  1030. io::IWriteFile* file = FileSystem->createAndWriteFile(filename);
  1031. if(!file)
  1032. return false;
  1033. bool result = writeImageToFile(image, file, param);
  1034. file->drop();
  1035. return result;
  1036. }
  1037. //! Writes the provided image to a file.
  1038. bool CNullDriver::writeImageToFile(IImage* image, io::IWriteFile * file, u32 param)
  1039. {
  1040. if(!file)
  1041. return false;
  1042. for (s32 i=SurfaceWriter.size()-1; i>=0; --i)
  1043. {
  1044. if (SurfaceWriter[i]->isAWriteableFileExtension(file->getFileName()))
  1045. {
  1046. bool written = SurfaceWriter[i]->writeImage(file, image, param);
  1047. if (written)
  1048. return true;
  1049. }
  1050. }
  1051. return false; // failed to write
  1052. }
  1053. //! Creates a software image from a byte array.
  1054. IImage* CNullDriver::createImageFromData(ECOLOR_FORMAT format,
  1055. const core::dimension2d<u32>& size,
  1056. void *data, bool ownForeignMemory,
  1057. bool deleteMemory)
  1058. {
  1059. if(IImage::isRenderTargetOnlyFormat(format))
  1060. {
  1061. os::Printer::log("Could not create IImage, format only supported for render target textures.", ELL_WARNING);
  1062. return 0;
  1063. }
  1064. return new CImage(format, size, data, ownForeignMemory, deleteMemory);
  1065. }
  1066. //! Creates an empty software image.
  1067. IImage* CNullDriver::createImage(ECOLOR_FORMAT format, const core::dimension2d<u32>& size)
  1068. {
  1069. if(IImage::isRenderTargetOnlyFormat(format))
  1070. {
  1071. os::Printer::log("Could not create IImage, format only supported for render target textures.", ELL_WARNING);
  1072. return 0;
  1073. }
  1074. return new CImage(format, size);
  1075. }
  1076. //! Creates a software image from another image.
  1077. IImage* CNullDriver::createImage(ECOLOR_FORMAT format, IImage *imageToCopy)
  1078. {
  1079. os::Printer::log("Deprecated method, please create an empty image instead and use copyTo().", ELL_WARNING);
  1080. if(IImage::isRenderTargetOnlyFormat(format))
  1081. {
  1082. os::Printer::log("Could not create IImage, format only supported for render target textures.", ELL_WARNING);
  1083. return 0;
  1084. }
  1085. CImage* tmp = new CImage(format, imageToCopy->getDimension());
  1086. imageToCopy->copyTo(tmp);
  1087. return tmp;
  1088. }
  1089. //! Creates a software image from part of another image.
  1090. IImage* CNullDriver::createImage(IImage* imageToCopy, const core::position2d<s32>& pos, const core::dimension2d<u32>& size)
  1091. {
  1092. os::Printer::log("Deprecated method, please create an empty image instead and use copyTo().", ELL_WARNING);
  1093. CImage* tmp = new CImage(imageToCopy->getColorFormat(), imageToCopy->getDimension());
  1094. imageToCopy->copyTo(tmp, core::position2di(0,0), core::recti(pos,size));
  1095. return tmp;
  1096. }
  1097. //! Creates a software image from part of a texture.
  1098. IImage* CNullDriver::createImage(ITexture* texture, const core::position2d<s32>& pos, const core::dimension2d<u32>& size)
  1099. {
  1100. if ((pos==core::position2di(0,0)) && (size == texture->getSize()))
  1101. {
  1102. IImage* image = new CImage(texture->getColorFormat(), size, texture->lock(ETLM_READ_ONLY), false);
  1103. texture->unlock();
  1104. return image;
  1105. }
  1106. else
  1107. {
  1108. // make sure to avoid buffer overruns
  1109. // make the vector a separate variable for g++ 3.x
  1110. const core::vector2d<u32> leftUpper(core::clamp(static_cast<u32>(pos.X), 0u, texture->getSize().Width),
  1111. core::clamp(static_cast<u32>(pos.Y), 0u, texture->getSize().Height));
  1112. const core::rect<u32> clamped(leftUpper,
  1113. core::dimension2du(core::clamp(static_cast<u32>(size.Width), 0u, texture->getSize().Width),
  1114. core::clamp(static_cast<u32>(size.Height), 0u, texture->getSize().Height)));
  1115. if (!clamped.isValid())
  1116. return 0;
  1117. u8* src = static_cast<u8*>(texture->lock(ETLM_READ_ONLY));
  1118. if (!src)
  1119. return 0;
  1120. IImage* image = new CImage(texture->getColorFormat(), clamped.getSize());
  1121. u8* dst = static_cast<u8*>(image->lock());
  1122. src += clamped.UpperLeftCorner.Y * texture->getPitch() + image->getBytesPerPixel() * clamped.UpperLeftCorner.X;
  1123. for (u32 i=0; i<clamped.getHeight(); ++i)
  1124. {
  1125. video::CColorConverter::convert_viaFormat(src, texture->getColorFormat(), clamped.getWidth(), dst, image->getColorFormat());
  1126. src += texture->getPitch();
  1127. dst += image->getPitch();
  1128. }
  1129. image->unlock();
  1130. texture->unlock();
  1131. return image;
  1132. }
  1133. }
  1134. //! Sets the fog mode.
  1135. void CNullDriver::setFog(SColor color, E_FOG_TYPE fogType, f32 start, f32 end,
  1136. f32 density, bool pixelFog, bool rangeFog)
  1137. {
  1138. FogColor = color;
  1139. FogType = fogType;
  1140. FogStart = start;
  1141. FogEnd = end;
  1142. FogDensity = density;
  1143. PixelFog = pixelFog;
  1144. RangeFog = rangeFog;
  1145. }
  1146. //! Gets the fog mode.
  1147. void CNullDriver::getFog(SColor& color, E_FOG_TYPE& fogType, f32& start, f32& end,
  1148. f32& density, bool& pixelFog, bool& rangeFog)
  1149. {
  1150. color = FogColor;
  1151. fogType = FogType;
  1152. start = FogStart;
  1153. end = FogEnd;
  1154. density = FogDensity;
  1155. pixelFog = PixelFog;
  1156. rangeFog = RangeFog;
  1157. }
  1158. //! Draws a mesh buffer
  1159. void CNullDriver::drawMeshBuffer(const scene::IMeshBuffer* mb)
  1160. {
  1161. if (!mb)
  1162. return;
  1163. //IVertexBuffer and IIndexBuffer later
  1164. SHWBufferLink *HWBuffer=getBufferLink(mb);
  1165. if (HWBuffer)
  1166. drawHardwareBuffer(HWBuffer);
  1167. else
  1168. drawVertexPrimitiveList(mb->getVertices(), mb->getVertexCount(), mb->getIndices(), indiceToPrimitiveCount(mb->getPrimitiveType(), mb->getIndexCount()), mb->getVertexType(), mb->getPrimitiveType(), mb->getIndexType());
  1169. }
  1170. //! Draws the normals of a mesh buffer
  1171. void CNullDriver::drawMeshBufferNormals(const scene::IMeshBuffer* mb, f32 length, SColor color)
  1172. {
  1173. const u32 count = mb->getVertexCount();
  1174. const bool normalize = mb->getMaterial().NormalizeNormals;
  1175. for (u32 i=0; i < count; ++i)
  1176. {
  1177. core::vector3df normalizedNormal = mb->getNormal(i);
  1178. if (normalize)
  1179. normalizedNormal.normalize();
  1180. const core::vector3df& pos = mb->getPosition(i);
  1181. draw3DLine(pos, pos + (normalizedNormal * length), color);
  1182. }
  1183. }
  1184. CNullDriver::SHWBufferLink *CNullDriver::getBufferLink(const scene::IMeshBuffer* mb)
  1185. {
  1186. if (!mb || !isHardwareBufferRecommend(mb))
  1187. return 0;
  1188. //search for hardware links
  1189. core::map< const scene::IMeshBuffer*,SHWBufferLink* >::Node* node = HWBufferMap.find(mb);
  1190. if (node)
  1191. return node->getValue();
  1192. return createHardwareBuffer(mb); //no hardware links, and mesh wants one, create it
  1193. }
  1194. //! Update all hardware buffers, remove unused ones
  1195. void CNullDriver::updateAllHardwareBuffers()
  1196. {
  1197. core::map<const scene::IMeshBuffer*,SHWBufferLink*>::ParentFirstIterator Iterator=HWBufferMap.getParentFirstIterator();
  1198. for (;!Iterator.atEnd();Iterator++)
  1199. {
  1200. SHWBufferLink *Link=Iterator.getNode()->getValue();
  1201. Link->LastUsed++;
  1202. if (Link->LastUsed>20000)
  1203. {
  1204. deleteHardwareBuffer(Link);
  1205. // todo: needs better fix
  1206. Iterator = HWBufferMap.getParentFirstIterator();
  1207. }
  1208. }
  1209. }
  1210. void CNullDriver::deleteHardwareBuffer(SHWBufferLink *HWBuffer)
  1211. {
  1212. if (!HWBuffer)
  1213. return;
  1214. HWBufferMap.remove(HWBuffer->MeshBuffer);
  1215. delete HWBuffer;
  1216. }
  1217. //! Remove hardware buffer
  1218. void CNullDriver::removeHardwareBuffer(const scene::IMeshBuffer* mb)
  1219. {
  1220. core::map<const scene::IMeshBuffer*,SHWBufferLink*>::Node* node = HWBufferMap.find(mb);
  1221. if (node)
  1222. deleteHardwareBuffer(node->getValue());
  1223. }
  1224. //! Remove all hardware buffers
  1225. void CNullDriver::removeAllHardwareBuffers()
  1226. {
  1227. while (HWBufferMap.size())
  1228. deleteHardwareBuffer(HWBufferMap.getRoot()->getValue());
  1229. }
  1230. bool CNullDriver::isHardwareBufferRecommend(const scene::IMeshBuffer* mb)
  1231. {
  1232. if (!mb || (mb->getHardwareMappingHint_Index()==scene::EHM_NEVER && mb->getHardwareMappingHint_Vertex()==scene::EHM_NEVER))
  1233. return false;
  1234. if (mb->getVertexCount()<MinVertexCountForVBO)
  1235. return false;
  1236. return true;
  1237. }
  1238. //! Create occlusion query.
  1239. /** Use node for identification and mesh for occlusion test. */
  1240. void CNullDriver::addOcclusionQuery(scene::ISceneNode* node, const scene::IMesh* mesh)
  1241. {
  1242. if (!node)
  1243. return;
  1244. if (!mesh)
  1245. {
  1246. if ((node->getType() != scene::ESNT_MESH) && (node->getType() != scene::ESNT_ANIMATED_MESH))
  1247. return;
  1248. else if (node->getType() == scene::ESNT_MESH)
  1249. mesh = static_cast<scene::IMeshSceneNode*>(node)->getMesh();
  1250. else
  1251. mesh = static_cast<scene::IAnimatedMeshSceneNode*>(node)->getMesh()->getMesh(0);
  1252. if (!mesh)
  1253. return;
  1254. }
  1255. //search for query
  1256. s32 index = OcclusionQueries.linear_search(SOccQuery(node));
  1257. if (index != -1)
  1258. {
  1259. if (OcclusionQueries[index].Mesh != mesh)
  1260. {
  1261. OcclusionQueries[index].Mesh->drop();
  1262. OcclusionQueries[index].Mesh = mesh;
  1263. mesh->grab();
  1264. }
  1265. }
  1266. else
  1267. {
  1268. OcclusionQueries.push_back(SOccQuery(node, mesh));
  1269. node->setAutomaticCulling(node->getAutomaticCulling() | scene::EAC_OCC_QUERY);
  1270. }
  1271. }
  1272. //! Remove occlusion query.
  1273. void CNullDriver::removeOcclusionQuery(scene::ISceneNode* node)
  1274. {
  1275. //search for query
  1276. s32 index = OcclusionQueries.linear_search(SOccQuery(node));
  1277. if (index != -1)
  1278. {
  1279. node->setAutomaticCulling(node->getAutomaticCulling() & ~scene::EAC_OCC_QUERY);
  1280. OcclusionQueries.erase(index);
  1281. }
  1282. }
  1283. //! Remove all occlusion queries.
  1284. void CNullDriver::removeAllOcclusionQueries()
  1285. {
  1286. for (s32 i=OcclusionQueries.size()-1; i>=0; --i)
  1287. {
  1288. removeOcclusionQuery(OcclusionQueries[i].Node);
  1289. }
  1290. }
  1291. //! Run occlusion query. Draws mesh stored in query.
  1292. /** If the mesh shall be rendered visible, use
  1293. flag to enable the proper material setting. */
  1294. void CNullDriver::runOcclusionQuery(scene::ISceneNode* node, bool visible)
  1295. {
  1296. if(!node)
  1297. return;
  1298. s32 index = OcclusionQueries.linear_search(SOccQuery(node));
  1299. if (index==-1)
  1300. return;
  1301. OcclusionQueries[index].Run=0;
  1302. if (!visible)
  1303. {
  1304. SMaterial mat;
  1305. mat.Lighting=false;
  1306. mat.AntiAliasing=0;
  1307. mat.ColorMask=ECP_NONE;
  1308. mat.GouraudShading=false;
  1309. mat.ZWriteEnable=false;
  1310. setMaterial(mat);
  1311. }
  1312. setTransform(video::ETS_WORLD, node->getAbsoluteTransformation());
  1313. const scene::IMesh* mesh = OcclusionQueries[index].Mesh;
  1314. for (u32 i=0; i<mesh->getMeshBufferCount(); ++i)
  1315. {
  1316. if (visible)
  1317. setMaterial(mesh->getMeshBuffer(i)->getMaterial());
  1318. drawMeshBuffer(mesh->getMeshBuffer(i));
  1319. }
  1320. }
  1321. //! Run all occlusion queries. Draws all meshes stored in queries.
  1322. /** If the meshes shall not be rendered visible, use
  1323. overrideMaterial to disable the color and depth buffer. */
  1324. void CNullDriver::runAllOcclusionQueries(bool visible)
  1325. {
  1326. for (u32 i=0; i<OcclusionQueries.size(); ++i)
  1327. runOcclusionQuery(OcclusionQueries[i].Node, visible);
  1328. }
  1329. //! Update occlusion query. Retrieves results from GPU.
  1330. /** If the query shall not block, set the flag to false.
  1331. Update might not occur in this case, though */
  1332. void CNullDriver::updateOcclusionQuery(scene::ISceneNode* node, bool block)
  1333. {
  1334. }
  1335. //! Update all occlusion queries. Retrieves results from GPU.
  1336. /** If the query shall not block, set the flag to false.
  1337. Update might not occur in this case, though */
  1338. void CNullDriver::updateAllOcclusionQueries(bool block)
  1339. {
  1340. for (u32 i=0; i<OcclusionQueries.size(); ++i)
  1341. {
  1342. if (OcclusionQueries[i].Run==u32(~0))
  1343. continue;
  1344. updateOcclusionQuery(OcclusionQueries[i].Node, block);
  1345. ++OcclusionQueries[i].Run;
  1346. if (OcclusionQueries[i].Run>1000)
  1347. removeOcclusionQuery(OcclusionQueries[i].Node);
  1348. }
  1349. }
  1350. //! Return query result.
  1351. /** Return value is the number of visible pixels/fragments.
  1352. The value is a safe approximation, i.e. can be larger then the
  1353. actual value of pixels. */
  1354. u32 CNullDriver::getOcclusionQueryResult(scene::ISceneNode* node) const
  1355. {
  1356. return ~0;
  1357. }
  1358. //! Only used by the internal engine. Used to notify the driver that
  1359. //! the window was resized.
  1360. void CNullDriver::OnResize(const core::dimension2d<u32>& size)
  1361. {
  1362. if (ViewPort.getWidth() == (s32)ScreenSize.Width &&
  1363. ViewPort.getHeight() == (s32)ScreenSize.Height)
  1364. ViewPort = core::rect<s32>(core::position2d<s32>(0,0),
  1365. core::dimension2di(size));
  1366. ScreenSize = size;
  1367. }
  1368. // adds a material renderer and drops it afterwards. To be used for internal creation
  1369. s32 CNullDriver::addAndDropMaterialRenderer(IMaterialRenderer* m)
  1370. {
  1371. s32 i = addMaterialRenderer(m);
  1372. if (m)
  1373. m->drop();
  1374. return i;
  1375. }
  1376. //! Adds a new material renderer to the video device.
  1377. s32 CNullDriver::addMaterialRenderer(IMaterialRenderer* renderer, const char* name)
  1378. {
  1379. if (!renderer)
  1380. return -1;
  1381. SMaterialRenderer r;
  1382. r.Renderer = renderer;
  1383. r.Name = name;
  1384. if (name == 0 && (MaterialRenderers.size() < (sizeof(sBuiltInMaterialTypeNames) / sizeof(char*))-1 ))
  1385. {
  1386. // set name of built in renderer so that we don't have to implement name
  1387. // setting in all available renderers.
  1388. r.Name = sBuiltInMaterialTypeNames[MaterialRenderers.size()];
  1389. }
  1390. MaterialRenderers.push_back(r);
  1391. renderer->grab();
  1392. return MaterialRenderers.size()-1;
  1393. }
  1394. //! Sets the name of a material renderer.
  1395. void CNullDriver::setMaterialRendererName(s32 idx, const char* name)
  1396. {
  1397. if (idx < s32(sizeof(sBuiltInMaterialTypeNames) / sizeof(char*))-1 ||
  1398. idx >= (s32)MaterialRenderers.size())
  1399. return;
  1400. MaterialRenderers[idx].Name = name;
  1401. }
  1402. //! Creates material attributes list from a material, usable for serialization and more.
  1403. io::IAttributes* CNullDriver::createAttributesFromMaterial(const video::SMaterial& material,
  1404. io::SAttributeReadWriteOptions* options)
  1405. {
  1406. io::CAttributes* attr = new io::CAttributes(this);
  1407. attr->addEnum("Type", material.MaterialType, sBuiltInMaterialTypeNames);
  1408. attr->addColor("Ambient", material.AmbientColor);
  1409. attr->addColor("Diffuse", material.DiffuseColor);
  1410. attr->addColor("Emissive", material.EmissiveColor);
  1411. attr->addColor("Specular", material.SpecularColor);
  1412. attr->addFloat("Shininess", material.Shininess);
  1413. attr->addFloat("Param1", material.MaterialTypeParam);
  1414. attr->addFloat("Param2", material.MaterialTypeParam2);
  1415. core::stringc prefix="Texture";
  1416. u32 i;
  1417. for (i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  1418. {
  1419. if (options && (options->Flags&io::EARWF_USE_RELATIVE_PATHS) && options->Filename && material.getTexture(i))
  1420. {
  1421. io::path path = FileSystem->getRelativeFilename(
  1422. FileSystem->getAbsolutePath(material.getTexture(i)->getName()), options->Filename);
  1423. attr->addTexture((prefix+core::stringc(i+1)).c_str(), material.getTexture(i), path);
  1424. }
  1425. else
  1426. attr->addTexture((prefix+core::stringc(i+1)).c_str(), material.getTexture(i));
  1427. }
  1428. attr->addBool("Wireframe", material.Wireframe);
  1429. attr->addBool("GouraudShading", material.GouraudShading);
  1430. attr->addBool("Lighting", material.Lighting);
  1431. attr->addBool("ZWriteEnable", material.ZWriteEnable);
  1432. attr->addInt("ZBuffer", material.ZBuffer);
  1433. attr->addBool("BackfaceCulling", material.BackfaceCulling);
  1434. attr->addBool("FrontfaceCulling", material.FrontfaceCulling);
  1435. attr->addBool("FogEnable", material.FogEnable);
  1436. attr->addBool("NormalizeNormals", material.NormalizeNormals);
  1437. attr->addBool("UseMipMaps", material.UseMipMaps);
  1438. attr->addInt("AntiAliasing", material.AntiAliasing);
  1439. attr->addInt("ColorMask", material.ColorMask);
  1440. attr->addInt("ColorMaterial", material.ColorMaterial);
  1441. attr->addInt("PolygonOffsetFactor", material.PolygonOffsetFactor);
  1442. attr->addEnum("PolygonOffsetDirection", material.PolygonOffsetDirection, video::PolygonOffsetDirectionNames);
  1443. prefix = "BilinearFilter";
  1444. for (i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  1445. attr->addBool((prefix+core::stringc(i+1)).c_str(), material.TextureLayer[i].BilinearFilter);
  1446. prefix = "TrilinearFilter";
  1447. for (i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  1448. attr->addBool((prefix+core::stringc(i+1)).c_str(), material.TextureLayer[i].TrilinearFilter);
  1449. prefix = "AnisotropicFilter";
  1450. for (i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  1451. attr->addInt((prefix+core::stringc(i+1)).c_str(), material.TextureLayer[i].AnisotropicFilter);
  1452. prefix="TextureWrapU";
  1453. for (i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  1454. attr->addEnum((prefix+core::stringc(i+1)).c_str(), material.TextureLayer[i].TextureWrapU, aTextureClampNames);
  1455. prefix="TextureWrapV";
  1456. for (i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  1457. attr->addEnum((prefix+core::stringc(i+1)).c_str(), material.TextureLayer[i].TextureWrapV, aTextureClampNames);
  1458. prefix="LODBias";
  1459. for (i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  1460. attr->addInt((prefix+core::stringc(i+1)).c_str(), material.TextureLayer[i].LODBias);
  1461. return attr;
  1462. }
  1463. //! Fills an SMaterial structure from attributes.
  1464. void CNullDriver::fillMaterialStructureFromAttributes(video::SMaterial& outMaterial, io::IAttributes* attr)
  1465. {
  1466. outMaterial.MaterialType = video::EMT_SOLID;
  1467. core::stringc name = attr->getAttributeAsString("Type");
  1468. u32 i;
  1469. for ( i=0; i < MaterialRenderers.size(); ++i)
  1470. if ( name == MaterialRenderers[i].Name )
  1471. {
  1472. outMaterial.MaterialType = (video::E_MATERIAL_TYPE)i;
  1473. break;
  1474. }
  1475. outMaterial.AmbientColor = attr->getAttributeAsColor("Ambient");
  1476. outMaterial.DiffuseColor = attr->getAttributeAsColor("Diffuse");
  1477. outMaterial.EmissiveColor = attr->getAttributeAsColor("Emissive");
  1478. outMaterial.SpecularColor = attr->getAttributeAsColor("Specular");
  1479. outMaterial.Shininess = attr->getAttributeAsFloat("Shininess");
  1480. outMaterial.MaterialTypeParam = attr->getAttributeAsFloat("Param1");
  1481. outMaterial.MaterialTypeParam2 = attr->getAttributeAsFloat("Param2");
  1482. core::stringc prefix="Texture";
  1483. for (i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  1484. outMaterial.setTexture(i, attr->getAttributeAsTexture((prefix+core::stringc(i+1)).c_str()));
  1485. outMaterial.Wireframe = attr->getAttributeAsBool("Wireframe");
  1486. outMaterial.GouraudShading = attr->getAttributeAsBool("GouraudShading");
  1487. outMaterial.Lighting = attr->getAttributeAsBool("Lighting");
  1488. outMaterial.ZWriteEnable = attr->getAttributeAsBool("ZWriteEnable");
  1489. outMaterial.ZBuffer = (u8)attr->getAttributeAsInt("ZBuffer");
  1490. outMaterial.BackfaceCulling = attr->getAttributeAsBool("BackfaceCulling");
  1491. outMaterial.FrontfaceCulling = attr->getAttributeAsBool("FrontfaceCulling");
  1492. outMaterial.FogEnable = attr->getAttributeAsBool("FogEnable");
  1493. outMaterial.NormalizeNormals = attr->getAttributeAsBool("NormalizeNormals");
  1494. if (attr->existsAttribute("UseMipMaps")) // legacy
  1495. outMaterial.UseMipMaps = attr->getAttributeAsBool("UseMipMaps");
  1496. else
  1497. outMaterial.UseMipMaps = true;
  1498. // default 0 is ok
  1499. outMaterial.AntiAliasing = attr->getAttributeAsInt("AntiAliasing");
  1500. if (attr->existsAttribute("ColorMask"))
  1501. outMaterial.ColorMask = attr->getAttributeAsInt("ColorMask");
  1502. if (attr->existsAttribute("ColorMaterial"))
  1503. outMaterial.ColorMaterial = attr->getAttributeAsInt("ColorMaterial");
  1504. if (attr->existsAttribute("PolygonOffsetFactor"))
  1505. outMaterial.PolygonOffsetFactor = attr->getAttributeAsInt("PolygonOffsetFactor");
  1506. if (attr->existsAttribute("PolygonOffsetDirection"))
  1507. outMaterial.PolygonOffsetDirection = (video::E_POLYGON_OFFSET)attr->getAttributeAsEnumeration("PolygonOffsetDirection", video::PolygonOffsetDirectionNames);
  1508. prefix = "BilinearFilter";
  1509. if (attr->existsAttribute(prefix.c_str())) // legacy
  1510. outMaterial.setFlag(EMF_BILINEAR_FILTER, attr->getAttributeAsBool(prefix.c_str()));
  1511. else
  1512. for (i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  1513. outMaterial.TextureLayer[i].BilinearFilter = attr->getAttributeAsBool((prefix+core::stringc(i+1)).c_str());
  1514. prefix = "TrilinearFilter";
  1515. if (attr->existsAttribute(prefix.c_str())) // legacy
  1516. outMaterial.setFlag(EMF_TRILINEAR_FILTER, attr->getAttributeAsBool(prefix.c_str()));
  1517. else
  1518. for (i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  1519. outMaterial.TextureLayer[i].TrilinearFilter = attr->getAttributeAsBool((prefix+core::stringc(i+1)).c_str());
  1520. prefix = "AnisotropicFilter";
  1521. if (attr->existsAttribute(prefix.c_str())) // legacy
  1522. outMaterial.setFlag(EMF_ANISOTROPIC_FILTER, attr->getAttributeAsBool(prefix.c_str()));
  1523. else
  1524. for (i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  1525. outMaterial.TextureLayer[i].AnisotropicFilter = attr->getAttributeAsInt((prefix+core::stringc(i+1)).c_str());
  1526. prefix = "TextureWrap";
  1527. if (attr->existsAttribute(prefix.c_str())) // legacy
  1528. {
  1529. for (i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  1530. {
  1531. outMaterial.TextureLayer[i].TextureWrapU = (E_TEXTURE_CLAMP)attr->getAttributeAsEnumeration((prefix+core::stringc(i+1)).c_str(), aTextureClampNames);
  1532. outMaterial.TextureLayer[i].TextureWrapV = outMaterial.TextureLayer[i].TextureWrapU;
  1533. }
  1534. }
  1535. else
  1536. {
  1537. for (i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  1538. {
  1539. outMaterial.TextureLayer[i].TextureWrapU = (E_TEXTURE_CLAMP)attr->getAttributeAsEnumeration((prefix+"U"+core::stringc(i+1)).c_str(), aTextureClampNames);
  1540. outMaterial.TextureLayer[i].TextureWrapV = (E_TEXTURE_CLAMP)attr->getAttributeAsEnumeration((prefix+"V"+core::stringc(i+1)).c_str(), aTextureClampNames);
  1541. }
  1542. }
  1543. // default 0 is ok
  1544. prefix="LODBias";
  1545. for (i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  1546. outMaterial.TextureLayer[i].LODBias = attr->getAttributeAsInt((prefix+core::stringc(i+1)).c_str());
  1547. }
  1548. //! Returns driver and operating system specific data about the IVideoDriver.
  1549. const SExposedVideoData& CNullDriver::getExposedVideoData()
  1550. {
  1551. return ExposedData;
  1552. }
  1553. //! Returns type of video driver
  1554. E_DRIVER_TYPE CNullDriver::getDriverType() const
  1555. {
  1556. return EDT_NULL;
  1557. }
  1558. //! deletes all material renderers
  1559. void CNullDriver::deleteMaterialRenders()
  1560. {
  1561. // delete material renderers
  1562. for (u32 i=0; i<MaterialRenderers.size(); ++i)
  1563. if (MaterialRenderers[i].Renderer)
  1564. MaterialRenderers[i].Renderer->drop();
  1565. MaterialRenderers.clear();
  1566. }
  1567. //! Returns pointer to material renderer or null
  1568. IMaterialRenderer* CNullDriver::getMaterialRenderer(u32 idx)
  1569. {
  1570. if ( idx < MaterialRenderers.size() )
  1571. return MaterialRenderers[idx].Renderer;
  1572. else
  1573. return 0;
  1574. }
  1575. //! Returns amount of currently available material renderers.
  1576. u32 CNullDriver::getMaterialRendererCount() const
  1577. {
  1578. return MaterialRenderers.size();
  1579. }
  1580. //! Returns name of the material renderer
  1581. const char* CNullDriver::getMaterialRendererName(u32 idx) const
  1582. {
  1583. if ( idx < MaterialRenderers.size() )
  1584. return MaterialRenderers[idx].Name.c_str();
  1585. return 0;
  1586. }
  1587. //! Returns pointer to the IGPUProgrammingServices interface.
  1588. IGPUProgrammingServices* CNullDriver::getGPUProgrammingServices()
  1589. {
  1590. return this;
  1591. }
  1592. //! Adds a new material renderer to the VideoDriver, based on a high level shading language.
  1593. s32 CNullDriver::addHighLevelShaderMaterial(
  1594. const c8* vertexShaderProgram,
  1595. const c8* vertexShaderEntryPointName,
  1596. E_VERTEX_SHADER_TYPE vsCompileTarget,
  1597. const c8* pixelShaderProgram,
  1598. const c8* pixelShaderEntryPointName,
  1599. E_PIXEL_SHADER_TYPE psCompileTarget,
  1600. const c8* geometryShaderProgram,
  1601. const c8* geometryShaderEntryPointName,
  1602. E_GEOMETRY_SHADER_TYPE gsCompileTarget,
  1603. scene::E_PRIMITIVE_TYPE inType, scene::E_PRIMITIVE_TYPE outType,
  1604. u32 verticesOut,
  1605. IShaderConstantSetCallBack* callback,
  1606. E_MATERIAL_TYPE baseMaterial,
  1607. s32 userData, E_GPU_SHADING_LANGUAGE shadingLang)
  1608. {
  1609. os::Printer::log("High level shader materials not available (yet) in this driver, sorry");
  1610. return -1;
  1611. }
  1612. //! Like IGPUProgrammingServices::addShaderMaterial() (look there for a detailed description),
  1613. //! but tries to load the programs from files.
  1614. s32 CNullDriver::addHighLevelShaderMaterialFromFiles(
  1615. const io::path& vertexShaderProgramFileName,
  1616. const c8* vertexShaderEntryPointName,
  1617. E_VERTEX_SHADER_TYPE vsCompileTarget,
  1618. const io::path& pixelShaderProgramFileName,
  1619. const c8* pixelShaderEntryPointName,
  1620. E_PIXEL_SHADER_TYPE psCompileTarget,
  1621. const io::path& geometryShaderProgramFileName,
  1622. const c8* geometryShaderEntryPointName,
  1623. E_GEOMETRY_SHADER_TYPE gsCompileTarget,
  1624. scene::E_PRIMITIVE_TYPE inType, scene::E_PRIMITIVE_TYPE outType,
  1625. u32 verticesOut,
  1626. IShaderConstantSetCallBack* callback,
  1627. E_MATERIAL_TYPE baseMaterial,
  1628. s32 userData, E_GPU_SHADING_LANGUAGE shadingLang)
  1629. {
  1630. io::IReadFile* vsfile = 0;
  1631. io::IReadFile* psfile = 0;
  1632. io::IReadFile* gsfile = 0;
  1633. if (vertexShaderProgramFileName.size() )
  1634. {
  1635. vsfile = FileSystem->createAndOpenFile(vertexShaderProgramFileName);
  1636. if (!vsfile)
  1637. {
  1638. os::Printer::log("Could not open vertex shader program file",
  1639. vertexShaderProgramFileName, ELL_WARNING);
  1640. }
  1641. }
  1642. if (pixelShaderProgramFileName.size() )
  1643. {
  1644. psfile = FileSystem->createAndOpenFile(pixelShaderProgramFileName);
  1645. if (!psfile)
  1646. {
  1647. os::Printer::log("Could not open pixel shader program file",
  1648. pixelShaderProgramFileName, ELL_WARNING);
  1649. }
  1650. }
  1651. if (geometryShaderProgramFileName.size() )
  1652. {
  1653. gsfile = FileSystem->createAndOpenFile(geometryShaderProgramFileName);
  1654. if (!gsfile)
  1655. {
  1656. os::Printer::log("Could not open geometry shader program file",
  1657. geometryShaderProgramFileName, ELL_WARNING);
  1658. }
  1659. }
  1660. s32 result = addHighLevelShaderMaterialFromFiles(
  1661. vsfile, vertexShaderEntryPointName, vsCompileTarget,
  1662. psfile, pixelShaderEntryPointName, psCompileTarget,
  1663. gsfile, geometryShaderEntryPointName, gsCompileTarget,
  1664. inType, outType, verticesOut,
  1665. callback, baseMaterial, userData, shadingLang);
  1666. if (psfile)
  1667. psfile->drop();
  1668. if (vsfile)
  1669. vsfile->drop();
  1670. if (gsfile)
  1671. gsfile->drop();
  1672. return result;
  1673. }
  1674. //! Like IGPUProgrammingServices::addShaderMaterial() (look there for a detailed description),
  1675. //! but tries to load the programs from files.
  1676. s32 CNullDriver::addHighLevelShaderMaterialFromFiles(
  1677. io::IReadFile* vertexShaderProgram,
  1678. const c8* vertexShaderEntryPointName,
  1679. E_VERTEX_SHADER_TYPE vsCompileTarget,
  1680. io::IReadFile* pixelShaderProgram,
  1681. const c8* pixelShaderEntryPointName,
  1682. E_PIXEL_SHADER_TYPE psCompileTarget,
  1683. io::IReadFile* geometryShaderProgram,
  1684. const c8* geometryShaderEntryPointName,
  1685. E_GEOMETRY_SHADER_TYPE gsCompileTarget,
  1686. scene::E_PRIMITIVE_TYPE inType, scene::E_PRIMITIVE_TYPE outType,
  1687. u32 verticesOut,
  1688. IShaderConstantSetCallBack* callback,
  1689. E_MATERIAL_TYPE baseMaterial,
  1690. s32 userData, E_GPU_SHADING_LANGUAGE shadingLang)
  1691. {
  1692. c8* vs = 0;
  1693. c8* ps = 0;
  1694. c8* gs = 0;
  1695. if (vertexShaderProgram)
  1696. {
  1697. const long size = vertexShaderProgram->getSize();
  1698. if (size)
  1699. {
  1700. vs = new c8[size+1];
  1701. vertexShaderProgram->read(vs, size);
  1702. vs[size] = 0;
  1703. }
  1704. }
  1705. if (pixelShaderProgram)
  1706. {
  1707. const long size = pixelShaderProgram->getSize();
  1708. if (size)
  1709. {
  1710. // if both handles are the same we must reset the file
  1711. if (pixelShaderProgram==vertexShaderProgram)
  1712. pixelShaderProgram->seek(0);
  1713. ps = new c8[size+1];
  1714. pixelShaderProgram->read(ps, size);
  1715. ps[size] = 0;
  1716. }
  1717. }
  1718. if (geometryShaderProgram)
  1719. {
  1720. const long size = geometryShaderProgram->getSize();
  1721. if (size)
  1722. {
  1723. // if both handles are the same we must reset the file
  1724. if ((geometryShaderProgram==vertexShaderProgram) ||
  1725. (geometryShaderProgram==pixelShaderProgram))
  1726. geometryShaderProgram->seek(0);
  1727. gs = new c8[size+1];
  1728. geometryShaderProgram->read(gs, size);
  1729. gs[size] = 0;
  1730. }
  1731. }
  1732. s32 result = this->addHighLevelShaderMaterial(
  1733. vs, vertexShaderEntryPointName, vsCompileTarget,
  1734. ps, pixelShaderEntryPointName, psCompileTarget,
  1735. gs, geometryShaderEntryPointName, gsCompileTarget,
  1736. inType, outType, verticesOut,
  1737. callback, baseMaterial, userData, shadingLang);
  1738. delete [] vs;
  1739. delete [] ps;
  1740. delete [] gs;
  1741. return result;
  1742. }
  1743. //! Adds a new material renderer to the VideoDriver, using pixel and/or
  1744. //! vertex shaders to render geometry.
  1745. s32 CNullDriver::addShaderMaterial(const c8* vertexShaderProgram,
  1746. const c8* pixelShaderProgram,
  1747. IShaderConstantSetCallBack* callback,
  1748. E_MATERIAL_TYPE baseMaterial,
  1749. s32 userData)
  1750. {
  1751. os::Printer::log("Shader materials not implemented yet in this driver, sorry.");
  1752. return -1;
  1753. }
  1754. //! Like IGPUProgrammingServices::addShaderMaterial(), but tries to load the
  1755. //! programs from files.
  1756. s32 CNullDriver::addShaderMaterialFromFiles(io::IReadFile* vertexShaderProgram,
  1757. io::IReadFile* pixelShaderProgram,
  1758. IShaderConstantSetCallBack* callback,
  1759. E_MATERIAL_TYPE baseMaterial,
  1760. s32 userData)
  1761. {
  1762. c8* vs = 0;
  1763. c8* ps = 0;
  1764. if (vertexShaderProgram)
  1765. {
  1766. const long size = vertexShaderProgram->getSize();
  1767. if (size)
  1768. {
  1769. vs = new c8[size+1];
  1770. vertexShaderProgram->read(vs, size);
  1771. vs[size] = 0;
  1772. }
  1773. }
  1774. if (pixelShaderProgram)
  1775. {
  1776. const long size = pixelShaderProgram->getSize();
  1777. if (size)
  1778. {
  1779. ps = new c8[size+1];
  1780. pixelShaderProgram->read(ps, size);
  1781. ps[size] = 0;
  1782. }
  1783. }
  1784. s32 result = addShaderMaterial(vs, ps, callback, baseMaterial, userData);
  1785. delete [] vs;
  1786. delete [] ps;
  1787. return result;
  1788. }
  1789. //! Like IGPUProgrammingServices::addShaderMaterial(), but tries to load the
  1790. //! programs from files.
  1791. s32 CNullDriver::addShaderMaterialFromFiles(const io::path& vertexShaderProgramFileName,
  1792. const io::path& pixelShaderProgramFileName,
  1793. IShaderConstantSetCallBack* callback,
  1794. E_MATERIAL_TYPE baseMaterial,
  1795. s32 userData)
  1796. {
  1797. io::IReadFile* vsfile = 0;
  1798. io::IReadFile* psfile = 0;
  1799. if (vertexShaderProgramFileName.size())
  1800. {
  1801. vsfile = FileSystem->createAndOpenFile(vertexShaderProgramFileName);
  1802. if (!vsfile)
  1803. {
  1804. os::Printer::log("Could not open vertex shader program file",
  1805. vertexShaderProgramFileName, ELL_WARNING);
  1806. return -1;
  1807. }
  1808. }
  1809. if (pixelShaderProgramFileName.size())
  1810. {
  1811. psfile = FileSystem->createAndOpenFile(pixelShaderProgramFileName);
  1812. if (!psfile)
  1813. {
  1814. os::Printer::log("Could not open pixel shader program file",
  1815. pixelShaderProgramFileName, ELL_WARNING);
  1816. if (vsfile)
  1817. vsfile->drop();
  1818. return -1;
  1819. }
  1820. }
  1821. s32 result = addShaderMaterialFromFiles(vsfile, psfile, callback,
  1822. baseMaterial, userData);
  1823. if (psfile)
  1824. psfile->drop();
  1825. if (vsfile)
  1826. vsfile->drop();
  1827. return result;
  1828. }
  1829. //! Creates a render target texture.
  1830. ITexture* CNullDriver::addRenderTargetTexture(const core::dimension2d<u32>& size,
  1831. const io::path&name, const ECOLOR_FORMAT format,
  1832. const bool useStencil)
  1833. {
  1834. return 0;
  1835. }
  1836. //! Clears the ZBuffer.
  1837. void CNullDriver::clearZBuffer()
  1838. {
  1839. }
  1840. //! Returns a pointer to the mesh manipulator.
  1841. scene::IMeshManipulator* CNullDriver::getMeshManipulator()
  1842. {
  1843. return MeshManipulator;
  1844. }
  1845. //! Returns an image created from the last rendered frame.
  1846. IImage* CNullDriver::createScreenShot(video::ECOLOR_FORMAT format, video::E_RENDER_TARGET target)
  1847. {
  1848. return 0;
  1849. }
  1850. // prints renderer version
  1851. void CNullDriver::printVersion()
  1852. {
  1853. core::stringw namePrint = L"Using renderer: ";
  1854. namePrint += getName();
  1855. os::Printer::log(namePrint.c_str(), ELL_INFORMATION);
  1856. }
  1857. //! creates a video driver
  1858. IVideoDriver* createNullDriver(io::IFileSystem* io, const core::dimension2d<u32>& screenSize)
  1859. {
  1860. CNullDriver* nullDriver = new CNullDriver(io, screenSize);
  1861. // create empty material renderers
  1862. for(u32 i=0; sBuiltInMaterialTypeNames[i]; ++i)
  1863. {
  1864. IMaterialRenderer* imr = new IMaterialRenderer();
  1865. nullDriver->addMaterialRenderer(imr);
  1866. imr->drop();
  1867. }
  1868. return nullDriver;
  1869. }
  1870. //! Set/unset a clipping plane.
  1871. //! There are at least 6 clipping planes available for the user to set at will.
  1872. //! \param index: The plane index. Must be between 0 and MaxUserClipPlanes.
  1873. //! \param plane: The plane itself.
  1874. //! \param enable: If true, enable the clipping plane else disable it.
  1875. bool CNullDriver::setClipPlane(u32 index, const core::plane3df& plane, bool enable)
  1876. {
  1877. return false;
  1878. }
  1879. //! Enable/disable a clipping plane.
  1880. void CNullDriver::enableClipPlane(u32 index, bool enable)
  1881. {
  1882. // not necessary
  1883. }
  1884. ITexture* CNullDriver::createRenderTargetTexture(const core::dimension2d<u32>& size,
  1885. const c8* name)
  1886. {
  1887. os::Printer::log("createRenderTargetTexture is deprecated, use addRenderTargetTexture instead");
  1888. ITexture* tex = addRenderTargetTexture(size, name);
  1889. tex->grab();
  1890. return tex;
  1891. }
  1892. void CNullDriver::setMinHardwareBufferVertexCount(u32 count)
  1893. {
  1894. MinVertexCountForVBO = count;
  1895. }
  1896. SOverrideMaterial& CNullDriver::getOverrideMaterial()
  1897. {
  1898. return OverrideMaterial;
  1899. }
  1900. //! Get the 2d override material for altering its values
  1901. SMaterial& CNullDriver::getMaterial2D()
  1902. {
  1903. return OverrideMaterial2D;
  1904. }
  1905. //! Enable the 2d override material
  1906. void CNullDriver::enableMaterial2D(bool enable)
  1907. {
  1908. OverrideMaterial2DEnabled=enable;
  1909. }
  1910. core::dimension2du CNullDriver::getMaxTextureSize() const
  1911. {
  1912. return core::dimension2du(0x10000,0x10000); // maybe large enough
  1913. }
  1914. //! Color conversion convenience function
  1915. /** Convert an image (as array of pixels) from source to destination
  1916. array, thereby converting the color format. The pixel size is
  1917. determined by the color formats.
  1918. \param sP Pointer to source
  1919. \param sF Color format of source
  1920. \param sN Number of pixels to convert, both array must be large enough
  1921. \param dP Pointer to destination
  1922. \param dF Color format of destination
  1923. */
  1924. void CNullDriver::convertColor(const void* sP, ECOLOR_FORMAT sF, s32 sN,
  1925. void* dP, ECOLOR_FORMAT dF) const
  1926. {
  1927. video::CColorConverter::convert_viaFormat(sP, sF, sN, dP, dF);
  1928. }
  1929. u32 CNullDriver::indiceToPrimitiveCount(scene::E_PRIMITIVE_TYPE ptype, u32 count) const
  1930. {
  1931. switch (ptype)
  1932. {
  1933. case scene::EPT_POINTS:
  1934. case scene::EPT_POINT_SPRITES:
  1935. case scene::EPT_LINE_LOOP:
  1936. case scene::EPT_POLYGON:
  1937. return count;
  1938. case scene::EPT_LINE_STRIP:
  1939. return count - 1;
  1940. case scene::EPT_LINES:
  1941. return count/2;
  1942. case scene::EPT_TRIANGLE_STRIP:
  1943. case scene::EPT_TRIANGLE_FAN:
  1944. return count - 2;
  1945. case scene::EPT_TRIANGLES:
  1946. return count/3;
  1947. case scene::EPT_QUAD_STRIP:
  1948. return (count - 2) / 2;
  1949. case scene::EPT_QUADS:
  1950. return count/4;
  1951. default:
  1952. return count;
  1953. }
  1954. }
  1955. } // end namespace
  1956. } // end namespace