rasterizer_iphone.cpp 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763
  1. /*************************************************************************/
  2. /* rasterizer_iphone.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifdef IPHONE_ENABLED
  30. #include "rasterizer_iphone.h"
  31. #include "os/os.h"
  32. #include "globals.h"
  33. #include <stdio.h>
  34. _FORCE_INLINE_ static void _gl_load_transform(const Transform& tr) {
  35. GLfloat matrix[16]={ /* build a 16x16 matrix */
  36. tr.basis.elements[0][0],
  37. tr.basis.elements[1][0],
  38. tr.basis.elements[2][0],
  39. 0,
  40. tr.basis.elements[0][1],
  41. tr.basis.elements[1][1],
  42. tr.basis.elements[2][1],
  43. 0,
  44. tr.basis.elements[0][2],
  45. tr.basis.elements[1][2],
  46. tr.basis.elements[2][2],
  47. 0,
  48. tr.origin.x,
  49. tr.origin.y,
  50. tr.origin.z,
  51. 1
  52. };
  53. glLoadMatrixf(matrix);
  54. };
  55. _FORCE_INLINE_ static void _gl_mult_transform(const Transform& tr) {
  56. GLfloat matrix[16]={ /* build a 16x16 matrix */
  57. tr.basis.elements[0][0],
  58. tr.basis.elements[1][0],
  59. tr.basis.elements[2][0],
  60. 0,
  61. tr.basis.elements[0][1],
  62. tr.basis.elements[1][1],
  63. tr.basis.elements[2][1],
  64. 0,
  65. tr.basis.elements[0][2],
  66. tr.basis.elements[1][2],
  67. tr.basis.elements[2][2],
  68. 0,
  69. tr.origin.x,
  70. tr.origin.y,
  71. tr.origin.z,
  72. 1
  73. };
  74. glMultMatrixf(matrix);
  75. };
  76. static const GLenum prim_type[]={GL_POINTS,GL_LINES,GL_TRIANGLES,GL_TRIANGLE_FAN};
  77. static void _draw_primitive(int p_points, const float *p_vertices, const float *p_normals, const float* p_colors, const float *p_uvs,const Plane *p_tangents=NULL,int p_instanced=1) {
  78. ERR_FAIL_COND(!p_vertices);
  79. ERR_FAIL_COND(p_points <1 || p_points>4);
  80. GLenum type = prim_type[p_points - 1];
  81. if (!p_colors) {
  82. glColor4f(1, 1, 1, 1);
  83. };
  84. glEnableClientState(GL_VERTEX_ARRAY);
  85. glVertexPointer(3, GL_FLOAT, 0, (GLvoid*)p_vertices);
  86. if (p_normals) {
  87. glEnableClientState(GL_NORMAL_ARRAY);
  88. glNormalPointer(GL_FLOAT, 0, (GLvoid*)p_normals);
  89. };
  90. if (p_colors) {
  91. glEnableClientState(GL_COLOR_ARRAY);
  92. glColorPointer(4,GL_FLOAT, 0, p_colors);
  93. };
  94. if (p_uvs) {
  95. glClientActiveTexture(GL_TEXTURE0);
  96. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  97. glTexCoordPointer(2, GL_FLOAT, 0, p_uvs);
  98. };
  99. glDrawArrays( type, 0, p_points);
  100. glDisableClientState(GL_VERTEX_ARRAY);
  101. glDisableClientState(GL_NORMAL_ARRAY);
  102. glDisableClientState(GL_COLOR_ARRAY);
  103. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  104. };
  105. /* TEXTURE API */
  106. static Image _get_gl_image_and_format(const Image& p_image, Image::Format p_format, uint32_t p_flags,GLenum& r_gl_format,int &r_gl_components,bool &r_has_alpha_cache) {
  107. r_has_alpha_cache=false;
  108. Image image=p_image;
  109. switch(p_format) {
  110. case Image::FORMAT_GRAYSCALE: {
  111. r_gl_components=1;
  112. r_gl_format=GL_LUMINANCE;
  113. } break;
  114. case Image::FORMAT_INTENSITY: {
  115. image.convert(Image::FORMAT_RGBA);
  116. r_gl_components=4;
  117. r_gl_format=GL_RGBA;
  118. r_has_alpha_cache=true;
  119. } break;
  120. case Image::FORMAT_GRAYSCALE_ALPHA: {
  121. image.convert(Image::FORMAT_RGBA);
  122. r_gl_components=4;
  123. r_gl_format=GL_RGBA;
  124. r_has_alpha_cache=true;
  125. } break;
  126. case Image::FORMAT_INDEXED: {
  127. image.convert(Image::FORMAT_RGB);
  128. r_gl_components=3;
  129. r_gl_format=GL_RGB;
  130. } break;
  131. case Image::FORMAT_INDEXED_ALPHA: {
  132. image.convert(Image::FORMAT_RGBA);
  133. r_gl_components=4;
  134. r_gl_format=GL_RGB;
  135. r_has_alpha_cache=true;
  136. } break;
  137. case Image::FORMAT_RGB: {
  138. r_gl_components=3; r_gl_format=GL_RGB;
  139. } break;
  140. case Image::FORMAT_RGBA: {
  141. r_gl_components=4;
  142. r_gl_format=GL_RGBA;
  143. r_has_alpha_cache=true;
  144. } break;
  145. default: {
  146. ERR_FAIL_V(Image());
  147. }
  148. }
  149. return image;
  150. }
  151. RID RasterizerIPhone::texture_create() {
  152. Texture *texture = memnew(Texture);
  153. ERR_FAIL_COND_V(!texture,RID());
  154. glGenTextures(1, &texture->tex_id);
  155. texture->active=false;
  156. return texture_owner.make_rid( texture );
  157. }
  158. void RasterizerIPhone::texture_allocate(RID p_texture,int p_width, int p_height,Image::Format p_format,uint32_t p_flags) {
  159. bool has_alpha_cache;
  160. int components;
  161. GLenum format;
  162. Texture *texture = texture_owner.get( p_texture );
  163. ERR_FAIL_COND(!texture);
  164. texture->width=p_width;
  165. texture->height=p_height;
  166. texture->format=p_format;
  167. texture->flags=p_flags;
  168. //texture->target = (p_flags & VS::TEXTURE_FLAG_CUBEMAP) ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D;
  169. texture->target = GL_TEXTURE_2D;
  170. _get_gl_image_and_format(Image(),texture->format,texture->flags,format,components,has_alpha_cache);
  171. texture->gl_components_cache=components;
  172. texture->gl_format_cache=format;
  173. texture->format_has_alpha=has_alpha_cache;
  174. texture->has_alpha=false; //by default it doesn't have alpha unless something with alpha is blitteds
  175. glBindTexture(texture->target, texture->tex_id);
  176. if (texture->flags&VS::TEXTURE_FLAG_MIPMAPS) {
  177. glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE );
  178. }
  179. if (texture->target==GL_TEXTURE_2D) {
  180. glTexImage2D(texture->target, 0, format, texture->width, texture->height, 0, format, GL_UNSIGNED_BYTE,NULL);
  181. }
  182. /*
  183. else {
  184. //cubemappor
  185. for (int i=0;i<6;i++)
  186. glTexImage2D(_cube_side_enum[i], 0, format, texture->width, texture->height, 0, format, GL_UNSIGNED_BYTE,NULL);
  187. }
  188. */
  189. glTexParameteri(texture->target,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering
  190. if (texture->flags&VS::TEXTURE_FLAG_FILTER) {
  191. glTexParameteri(texture->target,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering
  192. if (texture->flags&VS::TEXTURE_FLAG_MIPMAPS) {
  193. //glTexParameteri(texture->target,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
  194. };
  195. }
  196. if (texture->flags&VS::TEXTURE_FLAG_REPEAT /* && texture->target != GL_TEXTURE_CUBE_MAP*/) {
  197. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  198. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  199. } else {
  200. //glTexParameterf( texture->target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
  201. glTexParameterf( texture->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
  202. glTexParameterf( texture->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
  203. }
  204. texture->active=true;
  205. }
  206. void RasterizerIPhone::texture_blit_rect(RID p_texture,int p_x,int p_y, const Image& p_image,VS::CubeMapSide p_cube_side) {
  207. Texture * texture = texture_owner.get(p_texture);
  208. ERR_FAIL_COND(!texture);
  209. ERR_FAIL_COND(!texture->active);
  210. ERR_FAIL_COND(texture->format != p_image.get_format() );
  211. int components;
  212. GLenum format;
  213. bool alpha;
  214. Image img = _get_gl_image_and_format(p_image, p_image.get_format(),texture->flags,format,components,alpha);
  215. if (img.detect_alpha())
  216. texture->has_alpha=true;
  217. GLenum blit_target = GL_TEXTURE_2D; //(texture->target == GL_TEXTURE_CUBE_MAP)?_cube_side_enum[p_cube_side]:GL_TEXTURE_2D;
  218. DVector<uint8_t>::Read read = img.get_data().read();
  219. glBindTexture(texture->target, texture->tex_id);
  220. glTexSubImage2D( blit_target, 0, p_x,p_y,img.get_width(),img.get_height(),format,GL_UNSIGNED_BYTE,read.ptr() );
  221. //glGenerateMipmap( texture->target );
  222. }
  223. Image RasterizerIPhone::texture_get_rect(RID p_texture,int p_x,int p_y,int p_width, int p_height,VS::CubeMapSide p_cube_side) const {
  224. return Image();
  225. }
  226. void RasterizerIPhone::texture_set_flags(RID p_texture,uint32_t p_flags) {
  227. Texture *texture = texture_owner.get( p_texture );
  228. ERR_FAIL_COND(!texture);
  229. glBindTexture(texture->target, texture->tex_id);
  230. uint32_t cube = texture->flags & VS::TEXTURE_FLAG_CUBEMAP;
  231. texture->flags=p_flags|cube; // can't remove a cube from being a cube
  232. if (texture->flags&VS::TEXTURE_FLAG_REPEAT /*&& texture->target != GL_TEXTURE_CUBE_MAP*/) {
  233. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  234. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  235. } else {
  236. //glTexParameterf( texture->target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
  237. glTexParameterf( texture->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
  238. glTexParameterf( texture->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
  239. }
  240. if (texture->flags&VS::TEXTURE_FLAG_FILTER) {
  241. glTexParameteri(texture->target,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering
  242. if (texture->flags&VS::TEXTURE_FLAG_MIPMAPS)
  243. glTexParameteri(texture->target,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
  244. } else {
  245. glTexParameteri(texture->target,GL_TEXTURE_MAG_FILTER,GL_NEAREST); // nearest
  246. }
  247. }
  248. uint32_t RasterizerIPhone::texture_get_flags(RID p_texture) const {
  249. Texture * texture = texture_owner.get(p_texture);
  250. ERR_FAIL_COND_V(!texture,0);
  251. return texture->flags;
  252. }
  253. Image::Format RasterizerIPhone::texture_get_format(RID p_texture) const {
  254. Texture * texture = texture_owner.get(p_texture);
  255. ERR_FAIL_COND_V(!texture,Image::FORMAT_GRAYSCALE);
  256. return texture->format;
  257. }
  258. uint32_t RasterizerIPhone::texture_get_width(RID p_texture) const {
  259. Texture * texture = texture_owner.get(p_texture);
  260. ERR_FAIL_COND_V(!texture,0);
  261. return texture->width;
  262. }
  263. uint32_t RasterizerIPhone::texture_get_height(RID p_texture) const {
  264. Texture * texture = texture_owner.get(p_texture);
  265. ERR_FAIL_COND_V(!texture,0);
  266. return texture->height;
  267. }
  268. bool RasterizerIPhone::texture_has_alpha(RID p_texture) const {
  269. Texture * texture = texture_owner.get(p_texture);
  270. ERR_FAIL_COND_V(!texture,0);
  271. return texture->has_alpha;
  272. }
  273. /* SHADER API */
  274. RID RasterizerIPhone::shader_create() {
  275. return RID();
  276. }
  277. void RasterizerIPhone::shader_node_add(RID p_shader,VS::ShaderNodeType p_type,int p_id) {
  278. }
  279. void RasterizerIPhone::shader_node_remove(RID p_shader,int p_id) {
  280. }
  281. void RasterizerIPhone::shader_node_change_type(RID p_shader, int p_id, VS::ShaderNodeType p_type) {
  282. }
  283. void RasterizerIPhone::shader_node_set_param(RID p_shader, int p_id, const Variant& p_value) {
  284. }
  285. void RasterizerIPhone::shader_get_node_list(RID p_shader,List<int> *p_node_list) const {
  286. }
  287. VS::ShaderNodeType RasterizerIPhone::shader_node_get_type(RID p_shader,int p_id) const {
  288. return VS::NODE_ADD;
  289. }
  290. Variant RasterizerIPhone::shader_node_get_param(RID p_shader,int p_id) const {
  291. return Variant();
  292. }
  293. void RasterizerIPhone::shader_connect(RID p_shader,int p_src_id,int p_src_slot, int p_dst_id,int p_dst_slot) {
  294. }
  295. bool RasterizerIPhone::shader_is_connected(RID p_shader,int p_src_id,int p_src_slot, int p_dst_id,int p_dst_slot) const {
  296. return false;
  297. }
  298. void RasterizerIPhone::shader_disconnect(RID p_shader,int p_src_id,int p_src_slot, int p_dst_id,int p_dst_slot) {
  299. }
  300. void RasterizerIPhone::shader_get_connections(RID p_shader,List<VS::ShaderConnection> *p_connections) const {
  301. }
  302. void RasterizerIPhone::shader_clear(RID p_shader) {
  303. }
  304. /* COMMON MATERIAL API */
  305. void RasterizerIPhone::material_set_param(RID p_material, const StringName& p_param, const Variant& p_value) {
  306. }
  307. Variant RasterizerIPhone::material_get_param(RID p_material, const StringName& p_param) const {
  308. return Variant();
  309. }
  310. void RasterizerIPhone::material_get_param_list(RID p_material, List<String> *p_param_list) const {
  311. }
  312. void RasterizerIPhone::material_set_flag(RID p_material, VS::MaterialFlag p_flag,bool p_enabled) {
  313. }
  314. bool RasterizerIPhone::material_get_flag(RID p_material,VS::MaterialFlag p_flag) const {
  315. return false;
  316. }
  317. void RasterizerIPhone::material_set_blend_mode(RID p_material,VS::MaterialBlendMode p_mode) {
  318. }
  319. VS::MaterialBlendMode RasterizerIPhone::material_get_blend_mode(RID p_material) const {
  320. return VS::MATERIAL_BLEND_MODE_ADD;
  321. }
  322. void RasterizerIPhone::material_set_line_width(RID p_material,float p_line_width) {
  323. }
  324. float RasterizerIPhone::material_get_line_width(RID p_material) const {
  325. return 0;
  326. }
  327. /* FIXED MATERIAL */
  328. RID RasterizerIPhone::material_create() {
  329. return material_owner.make_rid( memnew( Material ) );
  330. }
  331. void RasterizerIPhone::fixed_material_set_parameter(RID p_material, VS::FixedMaterialParam p_parameter, const Variant& p_value) {
  332. Material *m=material_owner.get( p_material );
  333. ERR_FAIL_COND(!m);
  334. ERR_FAIL_INDEX(p_parameter, VisualServer::FIXED_MATERIAL_PARAM_MAX);
  335. m->parameters[p_parameter] = p_value;
  336. }
  337. Variant RasterizerIPhone::fixed_material_get_parameter(RID p_material,VS::FixedMaterialParam p_parameter) const {
  338. Material *m=material_owner.get( p_material );
  339. ERR_FAIL_COND_V(!m, Variant());
  340. ERR_FAIL_INDEX_V(p_parameter, VisualServer::FIXED_MATERIAL_PARAM_MAX, Variant());
  341. return m->parameters[p_parameter];
  342. }
  343. void RasterizerIPhone::fixed_material_set_texture(RID p_material,VS::FixedMaterialParam p_parameter, RID p_texture) {
  344. Material *m=material_owner.get( p_material );
  345. ERR_FAIL_COND(!m);
  346. ERR_FAIL_INDEX(p_parameter, VisualServer::FIXED_MATERIAL_PARAM_MAX);
  347. m->textures[p_parameter] = p_texture;
  348. }
  349. RID RasterizerIPhone::fixed_material_get_texture(RID p_material,VS::FixedMaterialParam p_parameter) const {
  350. Material *m=material_owner.get( p_material );
  351. ERR_FAIL_COND_V(!m, RID());
  352. ERR_FAIL_INDEX_V(p_parameter, VisualServer::FIXED_MATERIAL_PARAM_MAX, Variant());
  353. return m->textures[p_parameter];
  354. }
  355. void RasterizerIPhone::fixed_material_set_detail_blend_mode(RID p_material,VS::MaterialBlendMode p_mode) {
  356. Material *m=material_owner.get( p_material );
  357. ERR_FAIL_COND(!m);
  358. m->detail_blend_mode = p_mode;
  359. }
  360. VS::MaterialBlendMode RasterizerIPhone::fixed_material_get_detail_blend_mode(RID p_material) const {
  361. Material *m=material_owner.get( p_material );
  362. ERR_FAIL_COND_V(!m, VS::MATERIAL_BLEND_MODE_MIX);
  363. return m->detail_blend_mode;
  364. }
  365. void RasterizerIPhone::fixed_material_set_texcoord_mode(RID p_material,VS::FixedMaterialParam p_parameter, VS::FixedMaterialTexCoordMode p_mode) {
  366. Material *m=material_owner.get( p_material );
  367. ERR_FAIL_COND(!m);
  368. ERR_FAIL_INDEX(p_parameter, VisualServer::FIXED_MATERIAL_PARAM_MAX);
  369. m->texcoord_mode[p_parameter] = p_mode;
  370. }
  371. VS::FixedMaterialTexCoordMode RasterizerIPhone::fixed_material_get_texcoord_mode(RID p_material,VS::FixedMaterialParam p_parameter) const {
  372. Material *m=material_owner.get( p_material );
  373. ERR_FAIL_COND_V(!m, VS::FIXED_MATERIAL_TEXCOORD_TEXGEN);
  374. ERR_FAIL_INDEX_V(p_parameter, VisualServer::FIXED_MATERIAL_PARAM_MAX, VS::FIXED_MATERIAL_TEXCOORD_UV);
  375. return m->texcoord_mode[p_parameter]; // for now
  376. }
  377. void RasterizerIPhone::fixed_material_set_texgen_mode(RID p_material,VS::FixedMaterialTexGenMode p_mode) {
  378. Material *m=material_owner.get( p_material );
  379. ERR_FAIL_COND(!m);
  380. m->texgen_mode = p_mode;
  381. };
  382. VS::FixedMaterialTexGenMode RasterizerIPhone::fixed_material_get_texgen_mode(RID p_material) const {
  383. Material *m=material_owner.get( p_material );
  384. ERR_FAIL_COND_V(!m, VS::FIXED_MATERIAL_TEXGEN_SPHERE);
  385. return m->texgen_mode;
  386. };
  387. void RasterizerIPhone::fixed_material_set_uv_transform(RID p_material,const Transform& p_transform) {
  388. Material *m=material_owner.get( p_material );
  389. ERR_FAIL_COND(!m);
  390. m->uv_transform = p_transform;
  391. }
  392. Transform RasterizerIPhone::fixed_material_get_uv_transform(RID p_material) const {
  393. Material *m=material_owner.get( p_material );
  394. ERR_FAIL_COND_V(!m, Transform());
  395. return m->uv_transform;
  396. }
  397. /* SHADER MATERIAL */
  398. RID RasterizerIPhone::shader_material_create() const {
  399. return RID();
  400. }
  401. void RasterizerIPhone::shader_material_set_vertex_shader(RID p_material,RID p_shader,bool p_owned) {
  402. }
  403. RID RasterizerIPhone::shader_material_get_vertex_shader(RID p_material) const {
  404. return RID();
  405. }
  406. void RasterizerIPhone::shader_material_set_fragment_shader(RID p_material,RID p_shader,bool p_owned) {
  407. }
  408. RID RasterizerIPhone::shader_material_get_fragment_shader(RID p_material) const {
  409. return RID();
  410. }
  411. /* MESH API */
  412. RID RasterizerIPhone::mesh_create() {
  413. return mesh_owner.make_rid( memnew( Mesh ) );
  414. }
  415. void RasterizerIPhone::mesh_add_surface(RID p_mesh,VS::PrimitiveType p_primitive,uint32_t p_format,int p_array_len,int p_index_array_len) {
  416. Mesh *mesh = mesh_owner.get( p_mesh );
  417. ERR_FAIL_COND(!mesh);
  418. ERR_FAIL_COND((p_format&VS::ARRAY_FORMAT_VERTEX)==0); // mandatory
  419. ERR_FAIL_COND( p_array_len<=0 );
  420. ERR_FAIL_COND( p_index_array_len==0 );
  421. ERR_FAIL_INDEX( p_primitive, VS::PRIMITIVE_MAX );
  422. Surface *surface = memnew( Surface );
  423. ERR_FAIL_COND( !surface );
  424. int total_elem_size=0;
  425. bool use_VBO=true; //glGenBuffersARB!=NULL; // TODO detect if it's in there
  426. if (p_format&VS::ARRAY_FORMAT_WEIGHTS) {
  427. use_VBO=false;
  428. }
  429. for (int i=0;i<VS::ARRAY_MAX;i++) {
  430. Surface::ArrayData&ad=surface->array[i];
  431. ad.size=0;
  432. ad.configured=false;
  433. ad.ofs=0;
  434. int elem_size=0;
  435. int elem_count=0;
  436. if (!(p_format&(1<<i))) // no array
  437. continue;
  438. switch(i) {
  439. case VS::ARRAY_VERTEX:
  440. case VS::ARRAY_NORMAL: {
  441. elem_size=3*sizeof(GLfloat); // vertex
  442. elem_count=3;
  443. } break;
  444. case VS::ARRAY_TANGENT: {
  445. elem_size=4*sizeof(GLfloat); // vertex
  446. elem_count=4;
  447. } break;
  448. case VS::ARRAY_COLOR: {
  449. elem_size=4; /* RGBA */
  450. elem_count=4;
  451. } break;
  452. case VS::ARRAY_TEX_UV: {
  453. elem_size=2*sizeof(GLfloat);
  454. elem_count=2;
  455. } break;
  456. case VS::ARRAY_WEIGHTS:
  457. case VS::ARRAY_BONES: {
  458. elem_size=VS::ARRAY_WEIGHTS_SIZE*sizeof(GLfloat);
  459. elem_count=VS::ARRAY_WEIGHTS_SIZE;
  460. } break;
  461. case VS::ARRAY_INDEX: {
  462. if (p_index_array_len<=0) {
  463. ERR_PRINT("p_index_array_len==NO_INDEX_ARRAY");
  464. break;
  465. }
  466. /* determine wether using 8 or 16 bits indices */
  467. if (p_index_array_len>(1<<8)) {
  468. elem_size=2;
  469. } else {
  470. elem_size=1;
  471. }
  472. if (use_VBO) {
  473. glGenBuffers(1,&surface->index_id);
  474. ERR_FAIL_COND(surface->index_id==0);
  475. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,surface->index_id);
  476. glBufferData(GL_ELEMENT_ARRAY_BUFFER,p_index_array_len*elem_size,NULL,GL_STATIC_DRAW);
  477. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0); //unbind
  478. } else {
  479. surface->index_array_local = (uint8_t*)memalloc(p_index_array_len*elem_size);
  480. };
  481. surface->index_array_len=p_index_array_len; // only way it can exist
  482. ad.ofs=0;
  483. ad.size=elem_size;
  484. ad.configured=false;
  485. ad.components=1;
  486. continue;
  487. } break;
  488. default: {
  489. ERR_FAIL( );
  490. }
  491. }
  492. ad.ofs=total_elem_size;
  493. ad.size=elem_size;
  494. ad.components=elem_count;
  495. total_elem_size+=elem_size;
  496. ad.configured=false;
  497. }
  498. surface->stride=total_elem_size;
  499. surface->array_len=p_array_len;
  500. surface->format=p_format;
  501. surface->primitive=p_primitive;
  502. /* bind the bigass buffers */
  503. if (use_VBO) {
  504. glGenBuffers(1,&surface->vertex_id);
  505. ERR_FAIL_COND(surface->vertex_id==0);
  506. glBindBuffer(GL_ARRAY_BUFFER,surface->vertex_id);
  507. glBufferData(GL_ARRAY_BUFFER,surface->array_len*surface->stride,NULL,GL_STATIC_DRAW);
  508. glBindBuffer(GL_ARRAY_BUFFER,0); //unbind
  509. } else {
  510. surface->array_local = (uint8_t*)memalloc(surface->array_len*surface->stride);
  511. };
  512. mesh->surfaces.push_back(surface);
  513. }
  514. Error RasterizerIPhone::mesh_surface_set_array(RID p_mesh, int p_surface,VS::ArrayType p_type,const Variant& p_array) {
  515. ERR_FAIL_INDEX_V(p_type, VS::ARRAY_MAX, ERR_INVALID_PARAMETER );
  516. Mesh *mesh = mesh_owner.get( p_mesh );
  517. ERR_FAIL_COND_V(!mesh,ERR_INVALID_PARAMETER);
  518. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), ERR_INVALID_PARAMETER );
  519. Surface *surface = mesh->surfaces[p_surface];
  520. ERR_FAIL_COND_V( !surface, ERR_INVALID_PARAMETER );
  521. ERR_FAIL_COND_V( surface->array[p_type].size==0, ERR_INVALID_PARAMETER );
  522. Surface::ArrayData &a=surface->array[p_type];
  523. switch(p_type) {
  524. case VS::ARRAY_INDEX: {
  525. ERR_FAIL_COND_V( surface->index_array_len<=0, ERR_INVALID_DATA );
  526. ERR_FAIL_COND_V( p_array.get_type() != Variant::INT_ARRAY, ERR_INVALID_PARAMETER );
  527. DVector<int> indices = p_array;
  528. ERR_FAIL_COND_V( indices.size() == 0, ERR_INVALID_PARAMETER );
  529. ERR_FAIL_COND_V( indices.size() != surface->index_array_len, ERR_INVALID_PARAMETER );
  530. /* determine wether using 16 or 32 bits indices */
  531. if (surface->index_array_local == 0) {
  532. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,surface->index_id);
  533. };
  534. DVector<int>::Read read = indices.read();
  535. const int *src=read.ptr();
  536. for (int i=0;i<surface->index_array_len;i++) {
  537. if (surface->index_array_local) {
  538. if (a.size<=(1<<8)) {
  539. uint8_t v=src[i];
  540. copymem(&surface->array_local[i*a.size], &v, a.size);
  541. } else {
  542. uint16_t v=src[i];
  543. copymem(&surface->array_local[i*a.size], &v, a.size);
  544. }
  545. } else {
  546. if (a.size<=(1<<8)) {
  547. uint8_t v=src[i];
  548. glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, i*a.size, a.size, &v );
  549. } else {
  550. uint16_t v=src[i];
  551. glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, i*a.size, a.size, &v );
  552. }
  553. };
  554. }
  555. if (surface->index_array_local == 0) {
  556. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
  557. };
  558. a.configured=true;
  559. return OK;
  560. } break;
  561. case VS::ARRAY_VERTEX:
  562. case VS::ARRAY_NORMAL: {
  563. ERR_FAIL_COND_V( p_array.get_type() != Variant::VECTOR3_ARRAY, ERR_INVALID_PARAMETER );
  564. DVector<Vector3> array = p_array;
  565. ERR_FAIL_COND_V( array.size() != surface->array_len, ERR_INVALID_PARAMETER );
  566. if (surface->array_local == 0) {
  567. glBindBuffer(GL_ARRAY_BUFFER,surface->vertex_id);
  568. };
  569. DVector<Vector3>::Read read = array.read();
  570. const Vector3* src=read.ptr();
  571. // setting vertices means regenerating the AABB
  572. if (p_type==VS::ARRAY_VERTEX)
  573. surface->aabb=AABB();
  574. for (int i=0;i<surface->array_len;i++) {
  575. GLfloat vector[3]={ src[i].x, src[i].y, src[i].z };
  576. if (surface->array_local == 0) {
  577. glBufferSubData( GL_ARRAY_BUFFER, a.ofs+i*surface->stride, a.size , vector );
  578. } else {
  579. copymem(&surface->array_local[a.ofs+i*surface->stride], vector, a.size);
  580. }
  581. if (p_type==VS::ARRAY_VERTEX) {
  582. if (i==0) {
  583. surface->aabb=AABB(src[i],Vector3());
  584. } else {
  585. surface->aabb.expand_to( src[i] );
  586. }
  587. }
  588. }
  589. if (surface->array_local == 0) {
  590. glBindBuffer(GL_ARRAY_BUFFER,0);
  591. };
  592. } break;
  593. case VS::ARRAY_TANGENT: {
  594. ERR_FAIL_COND_V( p_array.get_type() != Variant::REAL_ARRAY, ERR_INVALID_PARAMETER );
  595. DVector<real_t> array = p_array;
  596. ERR_FAIL_COND_V( array.size() != surface->array_len*4, ERR_INVALID_PARAMETER );
  597. if (surface->array_local == 0) {
  598. glBindBuffer(GL_ARRAY_BUFFER,surface->vertex_id);
  599. };
  600. DVector<real_t>::Read read = array.read();
  601. const real_t* src = read.ptr();
  602. for (int i=0;i<surface->array_len;i++) {
  603. GLfloat xyzw[4]={
  604. src[i*4+0],
  605. src[i*4+1],
  606. src[i*4+2],
  607. src[i*4+3]
  608. };
  609. if (surface->array_local == 0) {
  610. glBufferSubData( GL_ARRAY_BUFFER, a.ofs+i*surface->stride, a.size , xyzw );
  611. } else {
  612. copymem(&surface->array_local[a.ofs+i*surface->stride], xyzw, a.size);
  613. };
  614. }
  615. if (surface->array_local == 0) {
  616. glBindBuffer(GL_ARRAY_BUFFER,0);
  617. };
  618. } break;
  619. case VS::ARRAY_COLOR: {
  620. ERR_FAIL_COND_V( p_array.get_type() != Variant::COLOR_ARRAY, ERR_INVALID_PARAMETER );
  621. DVector<Color> array = p_array;
  622. ERR_FAIL_COND_V( array.size() != surface->array_len, ERR_INVALID_PARAMETER );
  623. if (surface->array_local == 0)
  624. glBindBuffer(GL_ARRAY_BUFFER,surface->vertex_id);
  625. DVector<Color>::Read read = array.read();
  626. const Color* src = read.ptr();
  627. surface->has_alpha_cache=false;
  628. for (int i=0;i<surface->array_len;i++) {
  629. if (src[i].a<0.98) // tolerate alpha a bit, for crappy exporters
  630. surface->has_alpha_cache=true;
  631. uint8_t colors[4]={ src[i].r * 255.0 , src[i].g * 255.0, src[i].b * 255.0, src[i].a * 255.0 };
  632. // I'm not sure if this is correct, endianness-wise, i should re-check the GL spec
  633. if (surface->array_local == 0)
  634. glBufferSubData( GL_ARRAY_BUFFER, a.ofs+i*surface->stride, a.size , colors );
  635. else
  636. copymem(&surface->array_local[a.ofs+i*surface->stride], colors, a.size);
  637. }
  638. if (surface->array_local == 0)
  639. glBindBuffer(GL_ARRAY_BUFFER,0);
  640. } break;
  641. case VS::ARRAY_TEX_UV: {
  642. ERR_FAIL_COND_V( p_array.get_type() != Variant::VECTOR3_ARRAY, ERR_INVALID_PARAMETER );
  643. DVector<Vector3> array = p_array;
  644. ERR_FAIL_COND_V( array.size() != surface->array_len , ERR_INVALID_PARAMETER);
  645. if (surface->array_local == 0)
  646. glBindBuffer(GL_ARRAY_BUFFER,surface->vertex_id);
  647. DVector<Vector3>::Read read = array.read();
  648. const Vector3 * src=read.ptr();
  649. for (int i=0;i<surface->array_len;i++) {
  650. GLfloat uv[2]={ src[i].x , src[i].y };
  651. if (surface->array_local == 0)
  652. glBufferSubData( GL_ARRAY_BUFFER, a.ofs+i*surface->stride, a.size , uv );
  653. else
  654. copymem(&surface->array_local[a.ofs+i*surface->stride], uv, a.size);
  655. }
  656. if (surface->array_local == 0)
  657. glBindBuffer(GL_ARRAY_BUFFER,0);
  658. } break;
  659. case VS::ARRAY_BONES:
  660. case VS::ARRAY_WEIGHTS: {
  661. ERR_FAIL_COND_V( p_array.get_type() != Variant::REAL_ARRAY, ERR_INVALID_PARAMETER );
  662. DVector<real_t> array = p_array;
  663. ERR_FAIL_COND_V( array.size() != surface->array_len*VS::ARRAY_WEIGHTS_SIZE, ERR_INVALID_PARAMETER );
  664. if (surface->array_local == 0)
  665. glBindBuffer(GL_ARRAY_BUFFER,surface->vertex_id);
  666. DVector<real_t>::Read read = array.read();
  667. const real_t * src = read.ptr();
  668. for (int i=0;i<surface->array_len;i++) {
  669. GLfloat data[VS::ARRAY_WEIGHTS_SIZE];
  670. for (int j=0;j<VS::ARRAY_WEIGHTS_SIZE;j++)
  671. data[j]=src[i*VS::ARRAY_WEIGHTS_SIZE+j];
  672. if (surface->array_local == 0)
  673. glBufferSubData( GL_ARRAY_BUFFER, a.ofs+i*surface->stride, a.size , data );
  674. else
  675. copymem(&surface->array_local[a.ofs+i*surface->stride], data, a.size);
  676. }
  677. if (surface->array_local == 0)
  678. glBindBuffer(GL_ARRAY_BUFFER,0);
  679. } break;
  680. default: { ERR_FAIL_V(ERR_INVALID_PARAMETER);}
  681. }
  682. a.configured=true;
  683. return OK;
  684. }
  685. Variant RasterizerIPhone::mesh_surface_get_array(RID p_mesh, int p_surface,VS::ArrayType p_type) const {
  686. return Variant();
  687. }
  688. void RasterizerIPhone::mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material,bool p_owned) {
  689. Mesh *mesh = mesh_owner.get( p_mesh );
  690. ERR_FAIL_COND(!mesh);
  691. ERR_FAIL_INDEX(p_surface, mesh->surfaces.size() );
  692. Surface *surface = mesh->surfaces[p_surface];
  693. ERR_FAIL_COND( !surface);
  694. if (surface->material_owned && surface->material.is_valid())
  695. free(surface->material);
  696. surface->material_owned=p_owned;
  697. surface->material=p_material;
  698. }
  699. RID RasterizerIPhone::mesh_surface_get_material(RID p_mesh, int p_surface) const {
  700. Mesh *mesh = mesh_owner.get( p_mesh );
  701. ERR_FAIL_COND_V(!mesh,RID());
  702. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), RID() );
  703. Surface *surface = mesh->surfaces[p_surface];
  704. ERR_FAIL_COND_V( !surface, RID() );
  705. return surface->material;
  706. }
  707. int RasterizerIPhone::mesh_surface_get_array_len(RID p_mesh, int p_surface) const {
  708. Mesh *mesh = mesh_owner.get( p_mesh );
  709. ERR_FAIL_COND_V(!mesh,-1);
  710. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), -1 );
  711. Surface *surface = mesh->surfaces[p_surface];
  712. ERR_FAIL_COND_V( !surface, -1 );
  713. return surface->array_len;
  714. }
  715. int RasterizerIPhone::mesh_surface_get_array_index_len(RID p_mesh, int p_surface) const {
  716. Mesh *mesh = mesh_owner.get( p_mesh );
  717. ERR_FAIL_COND_V(!mesh,-1);
  718. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), -1 );
  719. Surface *surface = mesh->surfaces[p_surface];
  720. ERR_FAIL_COND_V( !surface, -1 );
  721. return surface->index_array_len;
  722. }
  723. uint32_t RasterizerIPhone::mesh_surface_get_format(RID p_mesh, int p_surface) const {
  724. Mesh *mesh = mesh_owner.get( p_mesh );
  725. ERR_FAIL_COND_V(!mesh,0);
  726. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), 0 );
  727. Surface *surface = mesh->surfaces[p_surface];
  728. ERR_FAIL_COND_V( !surface, 0 );
  729. return surface->format;
  730. }
  731. VS::PrimitiveType RasterizerIPhone::mesh_surface_get_primitive_type(RID p_mesh, int p_surface) const {
  732. Mesh *mesh = mesh_owner.get( p_mesh );
  733. ERR_FAIL_COND_V(!mesh,VS::PRIMITIVE_POINTS);
  734. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), VS::PRIMITIVE_POINTS );
  735. Surface *surface = mesh->surfaces[p_surface];
  736. ERR_FAIL_COND_V( !surface, VS::PRIMITIVE_POINTS );
  737. return surface->primitive;
  738. }
  739. void RasterizerIPhone::mesh_erase_surface(RID p_mesh,int p_index) {
  740. Mesh *mesh = mesh_owner.get( p_mesh );
  741. ERR_FAIL_COND(!mesh);
  742. ERR_FAIL_INDEX(p_index, mesh->surfaces.size() );
  743. Surface *surface = mesh->surfaces[p_index];
  744. ERR_FAIL_COND( !surface);
  745. memdelete( mesh->surfaces[p_index] );
  746. mesh->surfaces.remove(p_index);
  747. }
  748. int RasterizerIPhone::mesh_get_surface_count(RID p_mesh) const {
  749. Mesh *mesh = mesh_owner.get( p_mesh );
  750. ERR_FAIL_COND_V(!mesh,-1);
  751. return mesh->surfaces.size();
  752. }
  753. AABB RasterizerIPhone::mesh_get_aabb(RID p_mesh) const {
  754. Mesh *mesh = mesh_owner.get( p_mesh );
  755. ERR_FAIL_COND_V(!mesh,AABB());
  756. AABB aabb;
  757. for (int i=0;i<mesh->surfaces.size();i++) {
  758. if (i==0)
  759. aabb=mesh->surfaces[i]->aabb;
  760. else
  761. aabb.merge_with(mesh->surfaces[i]->aabb);
  762. }
  763. return aabb;
  764. }
  765. /* MULTIMESH API */
  766. RID RasterizerIPhone::multimesh_create() {
  767. return RID();
  768. }
  769. void RasterizerIPhone::multimesh_set_instance_count(RID p_multimesh,int p_count) {
  770. }
  771. int RasterizerIPhone::multimesh_get_instance_count(RID p_multimesh) const {
  772. return 0;
  773. }
  774. void RasterizerIPhone::multimesh_set_mesh(RID p_multimesh,RID p_mesh) {
  775. }
  776. void RasterizerIPhone::multimesh_set_aabb(RID p_multimesh,const AABB& p_aabb) {
  777. }
  778. void RasterizerIPhone::multimesh_instance_set_transform(RID p_multimesh,int p_index,const Transform& p_transform) {
  779. }
  780. void RasterizerIPhone::multimesh_instance_set_color(RID p_multimesh,int p_index,const Color& p_color) {
  781. }
  782. RID RasterizerIPhone::multimesh_get_mesh(RID p_multimesh) const {
  783. return RID();
  784. }
  785. AABB RasterizerIPhone::multimesh_get_aabb(RID p_multimesh) const {
  786. return AABB();
  787. }
  788. Transform RasterizerIPhone::multimesh_instance_get_transform(RID p_multimesh,int p_index) const {
  789. return Transform();
  790. }
  791. Color RasterizerIPhone::multimesh_instance_get_color(RID p_multimesh,int p_index) const {
  792. return Color();
  793. }
  794. /* POLY API */
  795. RID RasterizerIPhone::poly_create() {
  796. return RID();
  797. }
  798. void RasterizerIPhone::poly_set_material(RID p_poly, RID p_material,bool p_owned) {
  799. }
  800. void RasterizerIPhone::poly_add_primitive(RID p_poly, const Vector<Vector3>& p_points,const Vector<Vector3>& p_normals,const Vector<Color>& p_colors,const Vector<Vector3>& p_uvs) {
  801. }
  802. void RasterizerIPhone::poly_clear(RID p_poly) {
  803. }
  804. AABB RasterizerIPhone::poly_get_aabb(RID p_poly) const {
  805. return AABB();
  806. }
  807. /* PARTICLES API */
  808. RID RasterizerIPhone::particles_create() {
  809. return RID();
  810. }
  811. void RasterizerIPhone::particles_set_amount(RID p_particles, int p_amount) {
  812. }
  813. int RasterizerIPhone::particles_get_amount(RID p_particles) const {
  814. return 0;
  815. }
  816. void RasterizerIPhone::particles_set_emitting(RID p_particles, bool p_emitting) {
  817. }
  818. bool RasterizerIPhone::particles_is_emitting(RID p_particles) const {
  819. return false;
  820. }
  821. void RasterizerIPhone::particles_set_visibility_aabb(RID p_particles, const AABB& p_visibility) {
  822. }
  823. AABB RasterizerIPhone::particles_get_visibility_aabb(RID p_particles) const {
  824. return AABB();
  825. }
  826. void RasterizerIPhone::particles_set_emission_half_extents(RID p_particles, const Vector3& p_half_extents) {
  827. }
  828. Vector3 RasterizerIPhone::particles_get_emission_half_extents(RID p_particles) const {
  829. return Vector3();
  830. }
  831. void RasterizerIPhone::particles_set_gravity_normal(RID p_particles, const Vector3& p_normal) {
  832. }
  833. Vector3 RasterizerIPhone::particles_get_gravity_normal(RID p_particles) const {
  834. return Vector3();
  835. }
  836. void RasterizerIPhone::particles_set_variable(RID p_particles, VS::ParticleVariable p_variable,float p_value) {
  837. }
  838. float RasterizerIPhone::particles_get_variable(RID p_particles, VS::ParticleVariable p_variable) const {
  839. return 0;
  840. }
  841. void RasterizerIPhone::particles_set_randomness(RID p_particles, VS::ParticleVariable p_variable,float p_randomness) {
  842. }
  843. float RasterizerIPhone::particles_get_randomness(RID p_particles, VS::ParticleVariable p_variable) const {
  844. return 0;
  845. }
  846. void RasterizerIPhone::particles_set_color_phase_pos(RID p_particles, int p_phase, float p_pos) {
  847. }
  848. float RasterizerIPhone::particles_get_color_phase_pos(RID p_particles, int p_phase) const {
  849. return 0;
  850. }
  851. void RasterizerIPhone::particles_set_color_phases(RID p_particles, int p_phases) {
  852. }
  853. int RasterizerIPhone::particles_get_color_phases(RID p_particles) const {
  854. return 0;
  855. }
  856. void RasterizerIPhone::particles_set_color_phase_color(RID p_particles, int p_phase, const Color& p_color) {
  857. }
  858. Color RasterizerIPhone::particles_get_color_phase_color(RID p_particles, int p_phase) const {
  859. return Color();
  860. }
  861. void RasterizerIPhone::particles_set_attractors(RID p_particles, int p_attractors) {
  862. }
  863. int RasterizerIPhone::particles_get_attractors(RID p_particles) const {
  864. return 0;
  865. }
  866. void RasterizerIPhone::particles_set_attractor_pos(RID p_particles, int p_attractor, const Vector3& p_pos) {
  867. }
  868. Vector3 RasterizerIPhone::particles_get_attractor_pos(RID p_particles,int p_attractor) const {
  869. return Vector3();
  870. }
  871. void RasterizerIPhone::particles_set_attractor_strength(RID p_particles, int p_attractor, float p_force) {
  872. }
  873. float RasterizerIPhone::particles_get_attractor_strength(RID p_particles,int p_attractor) const {
  874. return 0;
  875. }
  876. void RasterizerIPhone::particles_set_material(RID p_particles, RID p_material,bool p_owned) {
  877. }
  878. RID RasterizerIPhone::particles_get_material(RID p_particles) const {
  879. return RID();
  880. }
  881. AABB RasterizerIPhone::particles_get_aabb(RID p_particles) const {
  882. return AABB();
  883. }
  884. /* BEAM API */
  885. RID RasterizerIPhone::beam_create() {
  886. return RID();
  887. }
  888. void RasterizerIPhone::beam_set_point_count(RID p_beam, int p_count) {
  889. }
  890. int RasterizerIPhone::beam_get_point_count(RID p_beam) const {
  891. return 0;
  892. }
  893. void RasterizerIPhone::beam_clear(RID p_beam) {
  894. }
  895. void RasterizerIPhone::beam_set_point(RID p_beam,int p_point,Vector3& p_pos) {
  896. }
  897. Vector3 RasterizerIPhone::beam_get_point(RID p_beam,int p_point) const {
  898. return Vector3();
  899. }
  900. void RasterizerIPhone::beam_set_primitive(RID p_beam,VS::BeamPrimitive p_primitive) {
  901. }
  902. VS::BeamPrimitive RasterizerIPhone::beam_get_primitive(RID p_beam) const {
  903. return VS::BEAM_CUBIC;
  904. }
  905. void RasterizerIPhone::beam_set_material(RID p_beam, RID p_material) {
  906. }
  907. RID RasterizerIPhone::beam_get_material(RID p_beam) const {
  908. return RID();
  909. }
  910. AABB RasterizerIPhone::beam_get_aabb(RID p_particles) const {
  911. return AABB();
  912. }
  913. /* SKELETON API */
  914. RID RasterizerIPhone::skeleton_create() {
  915. Skeleton *skeleton = memnew( Skeleton );
  916. ERR_FAIL_COND_V(!skeleton,RID());
  917. return skeleton_owner.make_rid( skeleton );
  918. }
  919. void RasterizerIPhone::skeleton_resize(RID p_skeleton,int p_bones) {
  920. Skeleton *skeleton = skeleton_owner.get( p_skeleton );
  921. ERR_FAIL_COND(!skeleton);
  922. if (p_bones == skeleton->bones.size()) {
  923. return;
  924. };
  925. ERR_FAIL_COND( p_bones<0 || p_bones>256);
  926. skeleton->bones.resize(p_bones);
  927. }
  928. int RasterizerIPhone::skeleton_get_bone_count(RID p_skeleton) const {
  929. Skeleton *skeleton = skeleton_owner.get( p_skeleton );
  930. ERR_FAIL_COND_V(!skeleton, -1);
  931. return skeleton->bones.size();
  932. }
  933. void RasterizerIPhone::skeleton_bone_set_transform(RID p_skeleton,int p_bone, const Transform& p_transform) {
  934. Skeleton *skeleton = skeleton_owner.get( p_skeleton );
  935. ERR_FAIL_COND(!skeleton);
  936. ERR_FAIL_INDEX( p_bone, skeleton->bones.size() );
  937. skeleton->bones[p_bone] = p_transform;
  938. }
  939. Transform RasterizerIPhone::skeleton_bone_get_transform(RID p_skeleton,int p_bone) {
  940. Skeleton *skeleton = skeleton_owner.get( p_skeleton );
  941. ERR_FAIL_COND_V(!skeleton, Transform());
  942. ERR_FAIL_INDEX_V( p_bone, skeleton->bones.size(), Transform() );
  943. // something
  944. return skeleton->bones[p_bone];
  945. }
  946. /* LIGHT API */
  947. RID RasterizerIPhone::light_create(VS::LightType p_type) {
  948. Light *light = memnew( Light );
  949. light->type=p_type;
  950. return light_owner.make_rid(light);
  951. }
  952. VS::LightType RasterizerIPhone::light_get_type(RID p_light) const {
  953. Light *light = light_owner.get(p_light);
  954. ERR_FAIL_COND_V(!light,VS::LIGHT_OMNI);
  955. return light->type;
  956. }
  957. void RasterizerIPhone::light_set_color(RID p_light,VS::LightColor p_type, const Color& p_color) {
  958. Light *light = light_owner.get(p_light);
  959. ERR_FAIL_COND(!light);
  960. ERR_FAIL_INDEX( p_type, 3 );
  961. light->colors[p_type]=p_color;
  962. }
  963. Color RasterizerIPhone::light_get_color(RID p_light,VS::LightColor p_type) const {
  964. Light *light = light_owner.get(p_light);
  965. ERR_FAIL_COND_V(!light, Color());
  966. ERR_FAIL_INDEX_V( p_type, 3, Color() );
  967. return light->colors[p_type];
  968. }
  969. void RasterizerIPhone::light_set_shadow(RID p_light,bool p_enabled) {
  970. Light *light = light_owner.get(p_light);
  971. ERR_FAIL_COND(!light);
  972. light->shadow_enabled=p_enabled;
  973. }
  974. bool RasterizerIPhone::light_has_shadow(RID p_light) const {
  975. Light *light = light_owner.get(p_light);
  976. ERR_FAIL_COND_V(!light,false);
  977. return light->shadow_enabled;
  978. }
  979. void RasterizerIPhone::light_set_volumetric(RID p_light,bool p_enabled) {
  980. Light *light = light_owner.get(p_light);
  981. ERR_FAIL_COND(!light);
  982. light->volumetric_enabled=p_enabled;
  983. }
  984. bool RasterizerIPhone::light_is_volumetric(RID p_light) const {
  985. Light *light = light_owner.get(p_light);
  986. ERR_FAIL_COND_V(!light,false);
  987. return light->volumetric_enabled;
  988. }
  989. void RasterizerIPhone::light_set_projector(RID p_light,RID p_texture) {
  990. Light *light = light_owner.get(p_light);
  991. ERR_FAIL_COND(!light);
  992. light->projector=p_texture;
  993. }
  994. RID RasterizerIPhone::light_get_projector(RID p_light) const {
  995. Light *light = light_owner.get(p_light);
  996. ERR_FAIL_COND_V(!light,RID());
  997. return light->projector;
  998. }
  999. void RasterizerIPhone::light_set_var(RID p_light, VS::LightParam p_var, float p_value) {
  1000. Light * light = light_owner.get( p_light );
  1001. ERR_FAIL_COND(!light);
  1002. ERR_FAIL_INDEX( p_var, VS::LIGHT_PARAM_MAX );
  1003. light->vars[p_var]=p_value;
  1004. }
  1005. float RasterizerIPhone::light_get_var(RID p_light, VS::LightParam p_var) const {
  1006. Light * light = light_owner.get( p_light );
  1007. ERR_FAIL_COND_V(!light,0);
  1008. ERR_FAIL_INDEX_V( p_var, VS::LIGHT_PARAM_MAX,0 );
  1009. return light->vars[p_var];
  1010. }
  1011. AABB RasterizerIPhone::light_get_aabb(RID p_light) const {
  1012. Light *light = light_owner.get( p_light );
  1013. ERR_FAIL_COND_V(!light,AABB());
  1014. switch( light->type ) {
  1015. case VS::LIGHT_SPOT: {
  1016. float len=light->vars[VS::LIGHT_PARAM_RADIUS];
  1017. float size=Math::tan(Math::deg2rad(light->vars[VS::LIGHT_PARAM_SPOT_ANGLE]))*len;
  1018. return AABB( Vector3( -size,-size,-len ), Vector3( size*2, size*2, len ) );
  1019. } break;
  1020. case VS::LIGHT_OMNI: {
  1021. float r = light->vars[VS::LIGHT_PARAM_RADIUS];
  1022. return AABB( -Vector3(r,r,r), Vector3(r,r,r)*2 );
  1023. } break;
  1024. case VS::LIGHT_DIRECTIONAL: {
  1025. return AABB();
  1026. } break;
  1027. default: {}
  1028. }
  1029. ERR_FAIL_V( AABB() );
  1030. }
  1031. RID RasterizerIPhone::light_instance_create(RID p_light) {
  1032. Light *light = light_owner.get( p_light );
  1033. ERR_FAIL_COND_V(!light, RID());
  1034. LightInstance *light_instance = memnew( LightInstance );
  1035. light_instance->light=p_light;
  1036. light_instance->base=light;
  1037. light_instance->last_pass=0;
  1038. return light_instance_owner.make_rid( light_instance );
  1039. }
  1040. void RasterizerIPhone::light_instance_set_transform(RID p_light_instance,const Transform& p_transform) {
  1041. LightInstance *lighti = light_instance_owner.get( p_light_instance );
  1042. ERR_FAIL_COND(!lighti);
  1043. lighti->transform=p_transform;
  1044. }
  1045. void RasterizerIPhone::light_instance_set_active_hint(RID p_light_instance) {
  1046. LightInstance *lighti = light_instance_owner.get( p_light_instance );
  1047. ERR_FAIL_COND(!lighti);
  1048. lighti->last_pass=frame;
  1049. }
  1050. bool RasterizerIPhone::light_instance_has_shadow(RID p_light_instance) const {
  1051. return false;
  1052. }
  1053. bool RasterizerIPhone::light_instance_assign_shadow(RID p_light_instance) {
  1054. return false;
  1055. }
  1056. Rasterizer::ShadowType RasterizerIPhone::light_instance_get_shadow_type(RID p_light_instance) const {
  1057. return Rasterizer::SHADOW_CUBE;
  1058. }
  1059. int RasterizerIPhone::light_instance_get_shadow_passes(RID p_light_instance) const {
  1060. return 0;
  1061. }
  1062. void RasterizerIPhone::light_instance_set_pssm_split_info(RID p_light_instance, int p_split, float p_near,float p_far, const CameraMatrix& p_camera, const Transform& p_transform) {
  1063. }
  1064. /* PARTICLES INSTANCE */
  1065. RID RasterizerIPhone::particles_instance_create(RID p_particles) {
  1066. return RID();
  1067. }
  1068. void RasterizerIPhone::particles_instance_set_transform(RID p_particles_instance,const Transform& p_transform) {
  1069. }
  1070. /* RENDER API */
  1071. /* all calls (inside begin/end shadow) are always warranted to be in the following order: */
  1072. static GLfloat rtri; // Angle For The Triangle ( NEW )
  1073. static GLfloat rquad; // Angle For The Quad ( NEW )
  1074. void RasterizerIPhone::begin_frame() {
  1075. window_size = Size2( OS::get_singleton()->get_video_mode().width, OS::get_singleton()->get_video_mode().height );
  1076. double time = (OS::get_singleton()->get_ticks_usec()/1000); // get msec
  1077. time/=1000.0; // make secs
  1078. time_delta=time-last_time;
  1079. last_time=time;
  1080. frame++;
  1081. glClearColor(0,0,1,1);
  1082. glClear(GL_COLOR_BUFFER_BIT);
  1083. /* nehe ?*/
  1084. #if 0
  1085. glViewport(0,0,window_size.width,window_size.height); // Reset The Current Viewport
  1086. glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
  1087. glLoadIdentity(); // Reset The Projection Matrix
  1088. // Calculate The Aspect Ratio Of The Window
  1089. gluPerspective(45.0f,(GLfloat)window_size.width/(GLfloat)window_size.height,0.1f,100.0f);
  1090. glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
  1091. glLoadIdentity(); // Reset The Modelview Matrix
  1092. glShadeModel(GL_SMOOTH); // Enable Smooth Shading
  1093. glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
  1094. glClearDepth(1.0f); // Depth Buffer Setup
  1095. glEnable(GL_DEPTH_TEST); // Enables Depth Testing
  1096. glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
  1097. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
  1098. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
  1099. glLoadIdentity(); // Reset The Current Modelview Matrix
  1100. glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
  1101. glRotatef(rtri,0.0f,1.0f,0.0f); // Rotate The Triangle On The Y axis ( NEW )
  1102. glBegin(GL_TRIANGLES); // Start Drawing A Triangle
  1103. glColor3f(1.0f,0.0f,0.0f); // Red
  1104. glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Front)
  1105. glColor3f(0.0f,1.0f,0.0f); // Green
  1106. glVertex3f(-1.0f,-1.0f, 1.0f); // Left Of Triangle (Front)
  1107. glColor3f(0.0f,0.0f,1.0f); // Blue
  1108. glVertex3f( 1.0f,-1.0f, 1.0f); // Right Of Triangle (Front)
  1109. glColor3f(1.0f,0.0f,0.0f); // Red
  1110. glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Right)
  1111. glColor3f(0.0f,0.0f,1.0f); // Blue
  1112. glVertex3f( 1.0f,-1.0f, 1.0f); // Left Of Triangle (Right)
  1113. glColor3f(0.0f,1.0f,0.0f); // Green
  1114. glVertex3f( 1.0f,-1.0f, -1.0f); // Right Of Triangle (Right)
  1115. glColor3f(1.0f,0.0f,0.0f); // Red
  1116. glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Back)
  1117. glColor3f(0.0f,1.0f,0.0f); // Green
  1118. glVertex3f( 1.0f,-1.0f, -1.0f); // Left Of Triangle (Back)
  1119. glColor3f(0.0f,0.0f,1.0f); // Blue
  1120. glVertex3f(-1.0f,-1.0f, -1.0f); // Right Of Triangle (Back)
  1121. glColor3f(1.0f,0.0f,0.0f); // Red
  1122. glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Left)
  1123. glColor3f(0.0f,0.0f,1.0f); // Blue
  1124. glVertex3f(-1.0f,-1.0f,-1.0f); // Left Of Triangle (Left)
  1125. glColor3f(0.0f,1.0f,0.0f); // Green
  1126. glVertex3f(-1.0f,-1.0f, 1.0f); // Right Of Triangle (Left)
  1127. glEnd(); // Done Drawing The Pyramid
  1128. glLoadIdentity(); // Reset The Current Modelview Matrix
  1129. glTranslatef(1.5f,0.0f,-7.0f); // Move Right 1.5 Units And Into The Screen 7.0
  1130. glRotatef(rquad,1.0f,1.0f,1.0f); // Rotate The Quad On The X axis ( NEW )
  1131. glBegin(GL_QUADS); // Draw A Quad
  1132. glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green
  1133. glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
  1134. glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
  1135. glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
  1136. glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
  1137. glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange
  1138. glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
  1139. glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
  1140. glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
  1141. glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)
  1142. glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
  1143. glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
  1144. glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
  1145. glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
  1146. glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)
  1147. glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow
  1148. glVertex3f( 1.0f,-1.0f,-1.0f); // Top Right Of The Quad (Back)
  1149. glVertex3f(-1.0f,-1.0f,-1.0f); // Top Left Of The Quad (Back)
  1150. glVertex3f(-1.0f, 1.0f,-1.0f); // Bottom Left Of The Quad (Back)
  1151. glVertex3f( 1.0f, 1.0f,-1.0f); // Bottom Right Of The Quad (Back)
  1152. glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
  1153. glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
  1154. glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
  1155. glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
  1156. glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)
  1157. glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet
  1158. glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
  1159. glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
  1160. glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
  1161. glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
  1162. glEnd(); // Done Drawing The Quad
  1163. rtri+=0.2f; // Increase The Rotation Variable For The Triangle ( NEW )
  1164. rquad-=0.15f; // Decrease The Rotation Variable For The Quad ( NEW )
  1165. #endif
  1166. }
  1167. void RasterizerIPhone::set_viewport(const VS::ViewportRect& p_viewport) {
  1168. viewport=p_viewport;
  1169. canvas_transform=Transform();
  1170. canvas_transform.translate(-(viewport.width / 2.0f), -(viewport.height / 2.0f), 0.0f);
  1171. canvas_transform.scale( Vector3( 2.0f / viewport.width, -2.0f / viewport.height, 1.0f ) );
  1172. glViewport( viewport.x, window_size.height-(viewport.height+viewport.y), viewport.width,viewport.height );
  1173. }
  1174. void RasterizerIPhone::begin_scene(RID p_fx,VS::ScenarioDebugMode p_debug) {
  1175. opaque_render_list.clear();
  1176. alpha_render_list.clear();
  1177. light_instance_count=0;
  1178. scene_fx = p_fx.is_valid() ? fx_owner.get(p_fx) : NULL;
  1179. };
  1180. void RasterizerIPhone::begin_shadow_map( RID p_light_instance, int p_shadow_pass ) {
  1181. }
  1182. void RasterizerIPhone::set_camera(const Transform& p_world,const CameraMatrix& p_projection) {
  1183. camera_transform=p_world;
  1184. camera_transform_inverse=camera_transform.inverse();
  1185. camera_projection=p_projection;
  1186. camera_plane = Plane( camera_transform.origin, camera_transform.basis.get_axis(2) );
  1187. camera_z_near=camera_projection.get_z_near();
  1188. camera_z_far=camera_projection.get_z_far();
  1189. camera_projection.get_viewport_size(camera_vp_size.x,camera_vp_size.y);
  1190. }
  1191. void RasterizerIPhone::add_light( RID p_light_instance ) {
  1192. #define LIGHT_FADE_TRESHOLD 0.05
  1193. ERR_FAIL_COND( light_instance_count >= MAX_LIGHTS );
  1194. LightInstance *li = light_instance_owner.get(p_light_instance);
  1195. ERR_FAIL_COND(!li);
  1196. /* make light hash */
  1197. // actually, not really a hash, but helps to sort the lights
  1198. // and avoid recompiling redudant shader versions
  1199. li->hash_aux=li->base->type;
  1200. if (li->base->shadow_enabled)
  1201. li->hash_aux|=(1<<3);
  1202. if (li->base->projector.is_valid())
  1203. li->hash_aux|=(1<<4);
  1204. if (li->base->shadow_enabled && li->base->volumetric_enabled)
  1205. li->hash_aux|=(1<<5);
  1206. switch(li->base->type) {
  1207. case VisualServer::LIGHT_DIRECTIONAL: {
  1208. Vector3 dir = li->transform.basis.get_axis(2);
  1209. li->light_vector.x=dir.x;
  1210. li->light_vector.y=dir.y;
  1211. li->light_vector.z=dir.z;
  1212. } break;
  1213. case VisualServer::LIGHT_OMNI: {
  1214. float radius = li->base->vars[VisualServer::LIGHT_PARAM_RADIUS];
  1215. if (radius==0)
  1216. radius=0.0001;
  1217. li->linear_att=(1/LIGHT_FADE_TRESHOLD)/radius;
  1218. li->light_vector.x=li->transform.origin.x;
  1219. li->light_vector.y=li->transform.origin.y;
  1220. li->light_vector.z=li->transform.origin.z;
  1221. } break;
  1222. case VisualServer::LIGHT_SPOT: {
  1223. float radius = li->base->vars[VisualServer::LIGHT_PARAM_RADIUS];
  1224. if (radius==0)
  1225. radius=0.0001;
  1226. li->linear_att=(1/LIGHT_FADE_TRESHOLD)/radius;
  1227. li->light_vector.x=li->transform.origin.x;
  1228. li->light_vector.y=li->transform.origin.y;
  1229. li->light_vector.z=li->transform.origin.z;
  1230. Vector3 dir = -li->transform.basis.get_axis(2);
  1231. li->spot_vector.x=dir.x;
  1232. li->spot_vector.y=dir.y;
  1233. li->spot_vector.z=dir.z;
  1234. } break;
  1235. }
  1236. light_instances[light_instance_count++]=li;
  1237. }
  1238. void RasterizerIPhone::_add_geometry( const Geometry* p_geometry, const Transform& p_world, uint32_t p_vertex_format, const RID* p_light_instances, int p_light_count, const ParamOverrideMap* p_material_overrides,const Skeleton* p_skeleton, GeometryOwner *p_owner) {
  1239. Material *m=NULL;
  1240. if (p_geometry->material.is_valid())
  1241. m=material_owner.get( p_geometry->material );
  1242. if (!m) {
  1243. m=material_owner.get( default_material );
  1244. }
  1245. ERR_FAIL_COND(!m);
  1246. LightInstance *lights[RenderList::MAX_LIGHTS];
  1247. int light_count=0;
  1248. RenderList *render_list=&opaque_render_list;
  1249. if (p_geometry->has_alpha || m->detail_blend_mode!=VS::MATERIAL_BLEND_MODE_MIX) {
  1250. render_list = &alpha_render_list;
  1251. };
  1252. if (!m->flags[VS::MATERIAL_FLAG_UNSHADED]) {
  1253. light_count=p_light_count;
  1254. for(int i=0;i<light_count;i++) {
  1255. lights[i]=light_instance_owner.get( p_light_instances[i] );
  1256. }
  1257. }
  1258. render_list->add_element( p_geometry, m, p_world, lights, light_count, p_material_overrides,p_skeleton, camera_plane.distance(p_world.origin), p_owner );
  1259. }
  1260. void RasterizerIPhone::add_mesh( RID p_mesh, const Transform* p_world, const RID* p_light_instances, int p_light_count, const ParamOverrideMap* p_material_overrides, RID p_skeleton) {
  1261. Mesh *mesh = mesh_owner.get(p_mesh);
  1262. int ssize = mesh->surfaces.size();
  1263. for (int i=0;i<ssize;i++) {
  1264. Surface *s = mesh->surfaces[i];
  1265. Skeleton *sk = p_skeleton.is_valid()?skeleton_owner.get(p_skeleton):NULL;
  1266. _add_geometry(s,*p_world,s->format,p_light_instances,p_light_count,p_material_overrides,sk,NULL);
  1267. }
  1268. mesh->last_pass=frame;
  1269. }
  1270. void RasterizerIPhone::add_multimesh( RID p_multimesh, const Transform* p_world, const RID* p_light_instances, int p_light_count, const ParamOverrideMap* p_material_overrides) {
  1271. }
  1272. void RasterizerIPhone::add_poly( RID p_poly, const Transform* p_world, const RID* p_light_instances, int p_light_count, const ParamOverrideMap* p_material_overrides) {
  1273. Poly *p = poly_owner.get(p_poly);
  1274. if (!p->primitives.empty()) {
  1275. const Poly::Primitive *pp = &p->primitives[0];
  1276. uint32_t format=VisualServer::ARRAY_FORMAT_VERTEX;
  1277. if (!pp->normals.empty())
  1278. format|=VisualServer::ARRAY_FORMAT_NORMAL;
  1279. if (!pp->colors.empty())
  1280. format|=VisualServer::ARRAY_FORMAT_COLOR;
  1281. if (!pp->uvs.empty())
  1282. format|=VisualServer::ARRAY_TEX_UV;
  1283. _add_geometry(p,*p_world,format,p_light_instances,p_light_count,p_material_overrides,NULL, NULL);
  1284. }
  1285. }
  1286. void RasterizerIPhone::add_beam( RID p_beam, const Transform* p_world, const RID* p_light_instances, int p_light_count, const ParamOverrideMap* p_material_overrides) {
  1287. }
  1288. void RasterizerIPhone::add_particles( RID p_particle_instance, const RID* p_light_instances, int p_light_count, const ParamOverrideMap* p_material_overrides) {
  1289. }
  1290. void RasterizerIPhone::_setup_material(const Geometry *p_geometry,const Material *p_material) {
  1291. if (p_material->flags[VS::MATERIAL_FLAG_DOUBLE_SIDED])
  1292. glDisable(GL_CULL_FACE);
  1293. else {
  1294. glEnable(GL_CULL_FACE);
  1295. glCullFace( (p_material->flags[VS::MATERIAL_FLAG_INVERT_FACES])?GL_FRONT:GL_BACK);
  1296. }
  1297. glEnable(GL_COLOR_MATERIAL); /* unused, unless color array */
  1298. //glColorMaterial( GL_FRONT_AND_BACK, GL_DIFFUSE );
  1299. glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
  1300. ///ambient @TODO offer global ambient group option
  1301. float ambient_rgba[4]={
  1302. 1,
  1303. 1,
  1304. 1,
  1305. 1.0
  1306. };
  1307. glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,ambient_rgba);
  1308. ///diffuse
  1309. const Color &diffuse_color=p_material->parameters[VS::FIXED_MATERIAL_PARAM_DIFFUSE];
  1310. float diffuse_rgba[4]={
  1311. (float)diffuse_color.r,
  1312. (float)diffuse_color.g,
  1313. (float)diffuse_color.b,
  1314. (float)diffuse_color.a
  1315. };
  1316. glColor4f( diffuse_rgba[0],diffuse_rgba[1],diffuse_rgba[2],diffuse_rgba[3]);
  1317. glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,diffuse_rgba);
  1318. //specular
  1319. const Color &specular_color=p_material->parameters[VS::FIXED_MATERIAL_PARAM_SPECULAR];
  1320. float specular_rgba[4]={
  1321. (float)specular_color.r,
  1322. (float)specular_color.g,
  1323. (float)specular_color.b,
  1324. 1.0
  1325. };
  1326. glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,specular_rgba);
  1327. const Color &emission_color=p_material->parameters[VS::FIXED_MATERIAL_PARAM_EMISSION];
  1328. float emission_rgba[4]={
  1329. (float)emission_color.r,
  1330. (float)emission_color.g,
  1331. (float)emission_color.b,
  1332. 1.0
  1333. };
  1334. glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,emission_rgba);
  1335. glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,p_material->parameters[VS::FIXED_MATERIAL_PARAM_SPECULAR_EXP]);
  1336. if (p_material->flags[VS::MATERIAL_FLAG_UNSHADED]) {
  1337. glDisable(GL_LIGHTING);
  1338. } else {
  1339. glEnable(GL_LIGHTING);
  1340. glDisable(GL_LIGHTING);
  1341. }
  1342. //depth test?
  1343. /*
  1344. if (p_material->flags[VS::MATERIAL_FLAG_WIREFRAME])
  1345. glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  1346. else
  1347. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  1348. */
  1349. if (p_material->textures[VS::FIXED_MATERIAL_PARAM_DIFFUSE]) {
  1350. Texture *texture = texture_owner.get( p_material->textures[VS::FIXED_MATERIAL_PARAM_DIFFUSE] );
  1351. ERR_FAIL_COND(!texture);
  1352. glActiveTexture(GL_TEXTURE0);
  1353. glEnable(GL_TEXTURE_2D);
  1354. glBindTexture( GL_TEXTURE_2D,texture->tex_id );
  1355. };
  1356. };
  1357. void RasterizerIPhone::_setup_light(LightInstance* p_instance, int p_idx) {
  1358. Light* ld = p_instance->base;
  1359. int glid = GL_LIGHT0 + p_idx;
  1360. glLightfv(glid , GL_AMBIENT, ld->colors[VS::LIGHT_COLOR_AMBIENT].components );
  1361. glLightfv(glid, GL_DIFFUSE, ld->colors[VS::LIGHT_COLOR_DIFFUSE].components );
  1362. glLightfv(glid, GL_SPECULAR, ld->colors[VS::LIGHT_COLOR_SPECULAR].components );
  1363. switch(ld->type) {
  1364. case VS::LIGHT_DIRECTIONAL: {
  1365. /* This doesnt have attenuation */
  1366. glMatrixMode(GL_MODELVIEW);
  1367. glPushMatrix();
  1368. glLoadIdentity();
  1369. Vector3 v(0.0,0.0,-1.0); // directional lights point up by default
  1370. v = p_instance->transform.get_basis().xform( v );
  1371. v = camera_transform_inverse.get_basis().xform( v );
  1372. v.normalize(); // this sucks, so it will be optimized at some point
  1373. v = -v;
  1374. float lightpos[4]={v.x,v.y,v.z,0.0};
  1375. glLightfv(glid,GL_POSITION,lightpos); //at modelview
  1376. glPopMatrix();
  1377. } break;
  1378. case VS::LIGHT_OMNI: {
  1379. glLightf(glid,GL_SPOT_CUTOFF,180.0);
  1380. glLightf(glid,GL_CONSTANT_ATTENUATION, ld->vars[VS::LIGHT_PARAM_ATTENUATION]);
  1381. glLightf(glid,GL_LINEAR_ATTENUATION, ld->vars[VS::LIGHT_PARAM_RADIUS]);
  1382. glLightf(glid,GL_QUADRATIC_ATTENUATION, ld->vars[VS::LIGHT_PARAM_ENERGY]); // wut?
  1383. glMatrixMode(GL_MODELVIEW);
  1384. glPushMatrix();
  1385. glLoadIdentity();
  1386. Vector3 pos = p_instance->transform.get_origin();
  1387. pos = camera_transform_inverse.xform(pos);
  1388. float lightpos[4]={pos.x,pos.y,pos.z,1.0};
  1389. glLightfv(glid,GL_POSITION,lightpos); //at modelview
  1390. glPopMatrix();
  1391. } break;
  1392. case VS::LIGHT_SPOT: {
  1393. glLightf(glid,GL_SPOT_CUTOFF, ld->vars[VS::LIGHT_PARAM_SPOT_ANGLE]);
  1394. glLightf(glid,GL_SPOT_EXPONENT, ld->vars[VS::LIGHT_PARAM_SPOT_ATTENUATION]);
  1395. glLightf(glid,GL_CONSTANT_ATTENUATION, ld->vars[VS::LIGHT_PARAM_ATTENUATION]);
  1396. glLightf(glid,GL_LINEAR_ATTENUATION, ld->vars[VS::LIGHT_PARAM_RADIUS]);
  1397. glLightf(glid,GL_QUADRATIC_ATTENUATION, ld->vars[VS::LIGHT_PARAM_ENERGY]); // wut?
  1398. glMatrixMode(GL_MODELVIEW);
  1399. glPushMatrix();
  1400. glLoadIdentity();
  1401. Vector3 v(0.0,0.0,-1.0); // directional lights point up by default
  1402. v = p_instance->transform.get_basis().xform( v );
  1403. v = camera_transform_inverse.get_basis().xform( v );
  1404. v.normalize(); // this sucks, so it will be optimized at some point
  1405. float lightdir[4]={v.x, v.y, v.z, 1.0};
  1406. glLightfv(glid,GL_SPOT_DIRECTION,lightdir); //at modelview
  1407. v = p_instance->transform.get_origin();
  1408. v = camera_transform_inverse.xform(v);
  1409. float lightpos[4]={v.x,v.y,v.z,1.0};
  1410. glLightfv(glid,GL_POSITION,lightpos); //at modelview
  1411. glPopMatrix();
  1412. } break;
  1413. default: break;
  1414. }
  1415. };
  1416. void RasterizerIPhone::_setup_lights(LightInstance **p_lights,int p_light_count) {
  1417. for (int i=0; i<MAX_LIGHTS; i++) {
  1418. if (i<p_light_count) {
  1419. glEnable(GL_LIGHT0 + i);
  1420. _setup_light(p_lights[i], i);
  1421. } else {
  1422. glDisable(GL_LIGHT0 + i);
  1423. }
  1424. }
  1425. }
  1426. static const int gl_client_states[] = {
  1427. GL_VERTEX_ARRAY,
  1428. GL_NORMAL_ARRAY,
  1429. -1, // ARRAY_TANGENT
  1430. GL_COLOR_ARRAY,
  1431. GL_TEXTURE_COORD_ARRAY, // ARRAY_TEX_UV
  1432. GL_TEXTURE_COORD_ARRAY, // ARRAY_TEX_UV2
  1433. -1, // ARRAY_BONES
  1434. -1, // ARRAY_WEIGHTS
  1435. -1, // ARRAY_INDEX
  1436. };
  1437. void RasterizerIPhone::_setup_geometry(const Geometry *p_geometry, const Material* p_material) {
  1438. switch(p_geometry->type) {
  1439. case Geometry::GEOMETRY_SURFACE: {
  1440. Surface *surf = (Surface*)p_geometry;
  1441. uint8_t *base=0;
  1442. bool use_VBO = (surf->array_local==0);
  1443. if (!use_VBO) {
  1444. base = surf->array_local;
  1445. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1446. } else {
  1447. glBindBuffer(GL_ARRAY_BUFFER, surf->vertex_id);
  1448. };
  1449. const Surface::ArrayData* a=surf->array;
  1450. for (int i=0;i<VS::ARRAY_MAX;i++) {
  1451. const Surface::ArrayData& ad=surf->array[i];
  1452. if (ad.size==0) {
  1453. if (gl_client_states[i] != -1) {
  1454. glDisableClientState(gl_client_states[i]);
  1455. };
  1456. continue; // this one is disabled.
  1457. }
  1458. ERR_CONTINUE( !ad.configured );
  1459. if (gl_client_states[i] != -1) {
  1460. glEnableClientState(gl_client_states[i]);
  1461. };
  1462. switch (i) {
  1463. case VS::ARRAY_VERTEX:
  1464. if (!use_VBO)
  1465. glVertexPointer(3,GL_FLOAT,surf->stride,(GLvoid*)&base[a->ofs]);
  1466. else
  1467. if (surf->array[VS::ARRAY_BONES].size)
  1468. glVertexPointer(3, GL_FLOAT, 0, skinned_buffer);
  1469. else
  1470. glVertexPointer(3,GL_FLOAT,surf->stride,(GLvoid*)a->ofs);
  1471. break;
  1472. case VS::ARRAY_NORMAL:
  1473. if (use_VBO)
  1474. glNormalPointer(GL_FLOAT,surf->stride,(GLvoid*)a->ofs);
  1475. else
  1476. glNormalPointer(GL_FLOAT,surf->stride,(GLvoid*)&base[a->ofs]);
  1477. break;
  1478. case VS::ARRAY_TANGENT:
  1479. break;
  1480. case VS::ARRAY_COLOR:
  1481. if (use_VBO)
  1482. glColorPointer(4,GL_UNSIGNED_BYTE,surf->stride,(GLvoid*)a->ofs);
  1483. else
  1484. glColorPointer(4,GL_UNSIGNED_BYTE,surf->stride,(GLvoid*)&base[a->ofs]);
  1485. break;
  1486. case VS::ARRAY_TEX_UV:
  1487. case VS::ARRAY_TEX_UV2:
  1488. if (use_VBO)
  1489. glTexCoordPointer(2,GL_FLOAT,surf->stride,(GLvoid*)a->ofs);
  1490. else
  1491. glTexCoordPointer(2,GL_FLOAT,surf->stride,&base[a->ofs]);
  1492. break;
  1493. case VS::ARRAY_BONES:
  1494. case VS::ARRAY_WEIGHTS:
  1495. case VS::ARRAY_INDEX:
  1496. break;
  1497. };
  1498. }
  1499. // process skeleton here
  1500. } break;
  1501. default: break;
  1502. };
  1503. };
  1504. static const GLenum gl_primitive[]={
  1505. GL_POINTS,
  1506. GL_LINES,
  1507. GL_LINE_STRIP,
  1508. GL_LINE_LOOP,
  1509. GL_TRIANGLES,
  1510. GL_TRIANGLE_STRIP,
  1511. GL_TRIANGLE_FAN
  1512. };
  1513. void RasterizerIPhone::_render(const Geometry *p_geometry,const Material *p_material, const Skeleton* p_skeleton) {
  1514. switch(p_geometry->type) {
  1515. case Geometry::GEOMETRY_SURFACE: {
  1516. Surface *s = (Surface*)p_geometry;
  1517. if (s->index_array_len>0) {
  1518. if (s->index_array_local) {
  1519. glDrawElements(gl_primitive[s->primitive], s->index_array_len, (s->index_array_len>(1<<8))?GL_UNSIGNED_SHORT:GL_UNSIGNED_BYTE, s->index_array_local);
  1520. } else {
  1521. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,s->index_id);
  1522. glDrawElements(gl_primitive[s->primitive],s->index_array_len, (s->index_array_len>(1<<8))?GL_UNSIGNED_SHORT:GL_UNSIGNED_BYTE,0);
  1523. }
  1524. } else {
  1525. glDrawArrays(gl_primitive[s->primitive],0,s->array_len);
  1526. };
  1527. } break;
  1528. default: break;
  1529. };
  1530. };
  1531. void RasterizerIPhone::_render_list_forward(RenderList *p_render_list) {
  1532. const Material *prev_material=NULL;
  1533. uint64_t prev_light_hash=0;
  1534. const Skeleton *prev_skeleton=NULL;
  1535. const Geometry *prev_geometry=NULL;
  1536. const ParamOverrideMap* prev_overrides=NULL; // make it diferent than NULL
  1537. Geometry::Type prev_geometry_type=Geometry::GEOMETRY_INVALID;
  1538. glMatrixMode(GL_PROJECTION);
  1539. glLoadMatrixf(&camera_projection.matrix[0][0]);
  1540. for (int i=0;i<p_render_list->element_count;i++) {
  1541. RenderList::Element *e = p_render_list->elements[i];
  1542. const Material *material = e->material;
  1543. uint64_t light_hash = e->light_hash;
  1544. const Skeleton *skeleton = e->skeleton;
  1545. const Geometry *geometry = e->geometry;
  1546. const ParamOverrideMap* material_overrides=e->material_overrides;
  1547. if (material!=prev_material || geometry->type!=prev_geometry_type) {
  1548. _setup_material(e->geometry,material);
  1549. //_setup_material_overrides(e->material,NULL,material_overrides);
  1550. //_setup_material_skeleton(material,skeleton);
  1551. } else {
  1552. if (material_overrides != prev_overrides) {
  1553. //_setup_material_overrides(e->material,prev_overrides,material_overrides);
  1554. }
  1555. if (prev_skeleton!=skeleton) {
  1556. //_setup_material_skeleton(material,skeleton);
  1557. };
  1558. }
  1559. if (geometry!=prev_geometry || geometry->type!=prev_geometry_type) {
  1560. _setup_geometry(geometry, material);
  1561. };
  1562. if (i==0 || light_hash!=prev_light_hash)
  1563. _setup_lights(e->lights,e->light_count);
  1564. glMatrixMode(GL_MODELVIEW);
  1565. _gl_load_transform(camera_transform_inverse);
  1566. _gl_mult_transform(e->transform);
  1567. _render(geometry, material, skeleton);
  1568. prev_material=material;
  1569. prev_skeleton=skeleton;
  1570. prev_geometry=geometry;
  1571. prev_light_hash=e->light_hash;
  1572. prev_geometry_type=geometry->type;
  1573. prev_overrides=material_overrides;
  1574. }
  1575. };
  1576. void RasterizerIPhone::end_scene() {
  1577. glEnable(GL_BLEND);
  1578. glDepthMask(GL_FALSE);
  1579. opaque_render_list.sort_mat_light();
  1580. _render_list_forward(&opaque_render_list);
  1581. glDisable(GL_BLEND);
  1582. glDepthMask(GL_TRUE);
  1583. alpha_render_list.sort_z();
  1584. _render_list_forward(&alpha_render_list);
  1585. }
  1586. void RasterizerIPhone::end_shadow_map() {
  1587. }
  1588. void RasterizerIPhone::end_frame() {
  1589. //ContextGL::get_singleton()->swap_buffers();
  1590. }
  1591. /* CANVAS API */
  1592. void RasterizerIPhone::canvas_begin() {
  1593. glDisable(GL_CULL_FACE);
  1594. glDisable(GL_DEPTH_TEST);
  1595. glEnable(GL_BLEND);
  1596. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1597. glLineWidth(1.0);
  1598. glDisable(GL_LIGHTING);
  1599. glMatrixMode(GL_PROJECTION);
  1600. glLoadIdentity();
  1601. }
  1602. void RasterizerIPhone::canvas_set_transparency(float p_transparency) {
  1603. }
  1604. void RasterizerIPhone::canvas_set_rect(const Rect2& p_rect, bool p_clip) {
  1605. glMatrixMode(GL_MODELVIEW);
  1606. glLoadIdentity();
  1607. glScalef(2.0 / window_size.x, -2.0 / window_size.y, 0);
  1608. glTranslatef((-(window_size.x / 2.0)) + p_rect.pos.x, (-(window_size.y / 2.0)) + p_rect.pos.y, 0);
  1609. if (p_clip) {
  1610. glEnable(GL_SCISSOR_TEST);
  1611. glScissor(viewport.x+p_rect.pos.x,viewport.y+ (viewport.height-(p_rect.pos.y+p_rect.size.height)),
  1612. p_rect.size.width,p_rect.size.height);
  1613. } else {
  1614. glDisable(GL_SCISSOR_TEST);
  1615. }
  1616. }
  1617. void RasterizerIPhone::canvas_draw_line(const Point2& p_from, const Point2& p_to,const Color& p_color,float p_width) {
  1618. glColor4f(1, 1, 1, 1);
  1619. float verts[6]={
  1620. p_from.x,p_from.y,0,
  1621. p_to.x,p_to.y,0
  1622. };
  1623. float colors[]={
  1624. p_color.r, p_color.g, p_color.b, p_color.a,
  1625. p_color.r, p_color.g, p_color.b, p_color.a,
  1626. };
  1627. glLineWidth(p_width);
  1628. _draw_primitive(2,verts,0,colors,0);
  1629. }
  1630. static void _draw_textured_quad(const Rect2& p_rect, const Rect2& p_src_region, const Size2& p_tex_size ) {
  1631. float texcoords[]= {
  1632. p_src_region.pos.x/p_tex_size.width,
  1633. p_src_region.pos.y/p_tex_size.height,
  1634. (p_src_region.pos.x+p_src_region.size.width)/p_tex_size.width,
  1635. p_src_region.pos.y/p_tex_size.height,
  1636. (p_src_region.pos.x+p_src_region.size.width)/p_tex_size.width,
  1637. (p_src_region.pos.y+p_src_region.size.height)/p_tex_size.height,
  1638. p_src_region.pos.x/p_tex_size.width,
  1639. (p_src_region.pos.y+p_src_region.size.height)/p_tex_size.height,
  1640. };
  1641. float coords[]= {
  1642. p_rect.pos.x, p_rect.pos.y, 0,
  1643. p_rect.pos.x+p_rect.size.width, p_rect.pos.y, 0,
  1644. p_rect.pos.x+p_rect.size.width, p_rect.pos.y+p_rect.size.height, 0,
  1645. p_rect.pos.x,p_rect.pos.y+p_rect.size.height, 0
  1646. };
  1647. _draw_primitive(4,coords,0,0,texcoords);
  1648. }
  1649. static void _draw_quad(const Rect2& p_rect) {
  1650. float coords[]= {
  1651. p_rect.pos.x,p_rect.pos.y, 0,
  1652. p_rect.pos.x+p_rect.size.width,p_rect.pos.y, 0,
  1653. p_rect.pos.x+p_rect.size.width,p_rect.pos.y+p_rect.size.height, 0,
  1654. p_rect.pos.x,p_rect.pos.y+p_rect.size.height, 0
  1655. };
  1656. _draw_primitive(4,coords,0,0,0);
  1657. }
  1658. void RasterizerIPhone::canvas_draw_rect(const Rect2& p_rect, bool p_region, const Rect2& p_source,bool p_tile,RID p_texture,const Color& p_modulate) {
  1659. glColor4f(p_modulate.r, p_modulate.g, p_modulate.b, p_modulate.a);
  1660. if ( p_texture.is_valid() ) {
  1661. glEnable(GL_TEXTURE_2D);
  1662. Texture *texture = texture_owner.get( p_texture );
  1663. ERR_FAIL_COND(!texture);
  1664. glActiveTexture(GL_TEXTURE0);
  1665. glBindTexture( GL_TEXTURE_2D,texture->tex_id );
  1666. if (!p_region) {
  1667. Rect2 region = Rect2(0,0,texture->width,texture->height);
  1668. _draw_textured_quad(p_rect,region,region.size);
  1669. } else {
  1670. _draw_textured_quad(p_rect, p_source, Size2(texture->width,texture->height) );
  1671. }
  1672. } else {
  1673. _draw_quad( p_rect );
  1674. }
  1675. }
  1676. void RasterizerIPhone::canvas_draw_style_box(const Rect2& p_rect, RID p_texture,const float *p_margin, bool p_draw_center) {
  1677. glColor4f(1, 1, 1, 1);
  1678. Texture *texture = texture_owner.get( p_texture );
  1679. ERR_FAIL_COND(!texture);
  1680. glEnable(GL_TEXTURE_2D);
  1681. glActiveTexture(GL_TEXTURE0);
  1682. glBindTexture( GL_TEXTURE_2D,texture->tex_id );
  1683. /* CORNERS */
  1684. _draw_textured_quad( // top left
  1685. Rect2( p_rect.pos, Size2(p_margin[MARGIN_LEFT],p_margin[MARGIN_TOP])),
  1686. Rect2( Point2(), Size2(p_margin[MARGIN_LEFT],p_margin[MARGIN_TOP])),
  1687. Size2( texture->width, texture->height ) );
  1688. _draw_textured_quad( // top right
  1689. Rect2( Point2( p_rect.pos.x + p_rect.size.width - p_margin[MARGIN_RIGHT], p_rect.pos.y), Size2(p_margin[MARGIN_RIGHT],p_margin[MARGIN_TOP])),
  1690. Rect2( Point2(texture->width-p_margin[MARGIN_RIGHT],0), Size2(p_margin[MARGIN_RIGHT],p_margin[MARGIN_TOP])),
  1691. Size2( texture->width, texture->height ) );
  1692. _draw_textured_quad( // bottom left
  1693. Rect2( Point2(p_rect.pos.x,p_rect.pos.y + p_rect.size.height - p_margin[MARGIN_BOTTOM]), Size2(p_margin[MARGIN_LEFT],p_margin[MARGIN_BOTTOM])),
  1694. Rect2( Point2(0,texture->height-p_margin[MARGIN_BOTTOM]), Size2(p_margin[MARGIN_LEFT],p_margin[MARGIN_BOTTOM])),
  1695. Size2( texture->width, texture->height ) );
  1696. _draw_textured_quad( // bottom right
  1697. Rect2( Point2( p_rect.pos.x + p_rect.size.width - p_margin[MARGIN_RIGHT], p_rect.pos.y + p_rect.size.height - p_margin[MARGIN_BOTTOM]), Size2(p_margin[MARGIN_RIGHT],p_margin[MARGIN_BOTTOM])),
  1698. Rect2( Point2(texture->width-p_margin[MARGIN_RIGHT],texture->height-p_margin[MARGIN_BOTTOM]), Size2(p_margin[MARGIN_RIGHT],p_margin[MARGIN_BOTTOM])),
  1699. Size2( texture->width, texture->height ) );
  1700. Rect2 rect_center( p_rect.pos+Point2( p_margin[MARGIN_LEFT], p_margin[MARGIN_TOP]), Size2( p_rect.size.width - p_margin[MARGIN_LEFT] - p_margin[MARGIN_RIGHT], p_rect.size.height - p_margin[MARGIN_TOP] - p_margin[MARGIN_BOTTOM] ));
  1701. Rect2 src_center( Point2( p_margin[MARGIN_LEFT], p_margin[MARGIN_TOP]), Size2( texture->width - p_margin[MARGIN_LEFT] - p_margin[MARGIN_RIGHT], texture->height - p_margin[MARGIN_TOP] - p_margin[MARGIN_BOTTOM] ));
  1702. _draw_textured_quad( // top
  1703. Rect2( Point2(rect_center.pos.x,p_rect.pos.y),Size2(rect_center.size.width,p_margin[MARGIN_TOP])),
  1704. Rect2( Point2(p_margin[MARGIN_LEFT],0), Size2(src_center.size.width,p_margin[MARGIN_TOP])),
  1705. Size2( texture->width, texture->height ) );
  1706. _draw_textured_quad( // bottom
  1707. Rect2( Point2(rect_center.pos.x,rect_center.pos.y+rect_center.size.height),Size2(rect_center.size.width,p_margin[MARGIN_BOTTOM])),
  1708. Rect2( Point2(p_margin[MARGIN_LEFT],src_center.pos.y+src_center.size.height), Size2(src_center.size.width,p_margin[MARGIN_BOTTOM])),
  1709. Size2( texture->width, texture->height ) );
  1710. _draw_textured_quad( // left
  1711. Rect2( Point2(p_rect.pos.x,rect_center.pos.y),Size2(p_margin[MARGIN_LEFT],rect_center.size.height)),
  1712. Rect2( Point2(0,p_margin[MARGIN_TOP]), Size2(p_margin[MARGIN_LEFT],src_center.size.height)),
  1713. Size2( texture->width, texture->height ) );
  1714. _draw_textured_quad( // right
  1715. Rect2( Point2(rect_center.pos.x+rect_center.size.width,rect_center.pos.y),Size2(p_margin[MARGIN_RIGHT],rect_center.size.height)),
  1716. Rect2( Point2(src_center.pos.x+src_center.size.width,p_margin[MARGIN_TOP]), Size2(p_margin[MARGIN_RIGHT],src_center.size.height)),
  1717. Size2( texture->width, texture->height ) );
  1718. if (p_draw_center) {
  1719. _draw_textured_quad(
  1720. rect_center,
  1721. src_center,
  1722. Size2( texture->width, texture->height ));
  1723. }
  1724. }
  1725. void RasterizerIPhone::canvas_draw_primitive(const Vector<Point2>& p_points, const Vector<Color>& p_colors,const Vector<Point2>& p_uvs, RID p_texture) {
  1726. ERR_FAIL_COND(p_points.size()<1);
  1727. float verts[12];
  1728. float uvs[8];
  1729. float colors[16];
  1730. glColor4f(1, 1, 1, 1);
  1731. int idx = 0;
  1732. for(int i=0;i<p_points.size();i++) {
  1733. verts[idx++]=p_points[i].x;
  1734. verts[idx++]=p_points[i].y;
  1735. verts[idx++]=0;
  1736. }
  1737. idx = 0;
  1738. for(int i=0;i<p_uvs.size();i++) {
  1739. uvs[idx++] = p_uvs[i].x;
  1740. uvs[idx++] = p_uvs[i].y;
  1741. }
  1742. idx = 0;
  1743. for (int i=0; i<p_colors.size(); i++) {
  1744. colors[idx++] = p_colors[i].r;
  1745. colors[idx++] = p_colors[i].g;
  1746. colors[idx++] = p_colors[i].b;
  1747. colors[idx++] = p_colors[i].a;
  1748. };
  1749. if (p_texture.is_valid()) {
  1750. glEnable(GL_TEXTURE_2D);
  1751. Texture *texture = texture_owner.get( p_texture );
  1752. if (texture) {
  1753. glActiveTexture(GL_TEXTURE0);
  1754. glBindTexture( GL_TEXTURE_2D,texture->tex_id );
  1755. }
  1756. }
  1757. _draw_primitive(p_points.size(),&verts[0],NULL,p_colors.size()?&colors[0]:NULL,p_uvs.size()?uvs:NULL);
  1758. }
  1759. /* FX */
  1760. RID RasterizerIPhone::fx_create() {
  1761. return RID();
  1762. }
  1763. void RasterizerIPhone::fx_get_effects(RID p_fx,List<String> *p_effects) const {
  1764. }
  1765. void RasterizerIPhone::fx_set_active(RID p_fx,const String& p_effect, bool p_active) {
  1766. }
  1767. bool RasterizerIPhone::fx_is_active(RID p_fx,const String& p_effect) const {
  1768. return false;
  1769. }
  1770. void RasterizerIPhone::fx_get_effect_params(RID p_fx,const String& p_effect,List<PropertyInfo> *p_params) const {
  1771. }
  1772. Variant RasterizerIPhone::fx_get_effect_param(RID p_fx,const String& p_effect,const String& p_param) const {
  1773. return Variant();
  1774. }
  1775. void RasterizerIPhone::fx_set_effect_param(RID p_fx,const String& p_effect, const String& p_param, const Variant& p_pvalue) {
  1776. }
  1777. /*MISC*/
  1778. bool RasterizerIPhone::is_texture(const RID& p_rid) const {
  1779. return texture_owner.owns(p_rid);
  1780. }
  1781. bool RasterizerIPhone::is_material(const RID& p_rid) const {
  1782. return material_owner.owns(p_rid);
  1783. }
  1784. bool RasterizerIPhone::is_mesh(const RID& p_rid) const {
  1785. return mesh_owner.owns(p_rid);
  1786. }
  1787. bool RasterizerIPhone::is_multimesh(const RID& p_rid) const {
  1788. return false;
  1789. }
  1790. bool RasterizerIPhone::is_poly(const RID& p_rid) const {
  1791. return poly_owner.owns(p_rid);
  1792. }
  1793. bool RasterizerIPhone::is_particles(const RID &p_beam) const {
  1794. return false;
  1795. }
  1796. bool RasterizerIPhone::is_beam(const RID &p_beam) const {
  1797. return false;
  1798. }
  1799. bool RasterizerIPhone::is_light(const RID& p_rid) const {
  1800. return light_owner.owns(p_rid);
  1801. }
  1802. bool RasterizerIPhone::is_light_instance(const RID& p_rid) const {
  1803. return light_instance_owner.owns(p_rid);
  1804. }
  1805. bool RasterizerIPhone::is_particles_instance(const RID& p_rid) const {
  1806. return false;
  1807. }
  1808. bool RasterizerIPhone::is_skeleton(const RID& p_rid) const {
  1809. return skeleton_owner.owns(p_rid);
  1810. }
  1811. bool RasterizerIPhone::is_fx(const RID& p_rid) const {
  1812. return fx_owner.owns(p_rid);
  1813. }
  1814. bool RasterizerIPhone::is_shader(const RID& p_rid) const {
  1815. return false;
  1816. }
  1817. void RasterizerIPhone::free(const RID& p_rid) const {
  1818. if (texture_owner.owns(p_rid)) {
  1819. // delete the texture
  1820. Texture *texture = texture_owner.get(p_rid);
  1821. glDeleteTextures( 1,&texture->tex_id );
  1822. texture_owner.free(p_rid);
  1823. memdelete(texture);
  1824. } else if (material_owner.owns(p_rid)) {
  1825. Material *material = material_owner.get( p_rid );
  1826. ERR_FAIL_COND(!material);
  1827. material_owner.free(p_rid);
  1828. memdelete(material);
  1829. } else if (mesh_owner.owns(p_rid)) {
  1830. Mesh *mesh = mesh_owner.get(p_rid);
  1831. ERR_FAIL_COND(!mesh);
  1832. for (int i=0;i<mesh->surfaces.size();i++) {
  1833. Surface *surface = mesh->surfaces[i];
  1834. if (surface->array_local != 0) {
  1835. memfree(surface->array_local);
  1836. };
  1837. if (surface->index_array_local != 0) {
  1838. memfree(surface->index_array_local);
  1839. };
  1840. if (surface->vertex_id)
  1841. glDeleteBuffers(1,&surface->vertex_id);
  1842. if (surface->index_id)
  1843. glDeleteBuffers(1,&surface->index_id);
  1844. memdelete( surface );
  1845. };
  1846. mesh->surfaces.clear();
  1847. mesh_owner.free(p_rid);
  1848. memdelete(mesh);
  1849. } else if (skeleton_owner.owns(p_rid)) {
  1850. Skeleton *skeleton = skeleton_owner.get( p_rid );
  1851. ERR_FAIL_COND(!skeleton)
  1852. skeleton_owner.free(p_rid);
  1853. memdelete(skeleton);
  1854. } else if (light_owner.owns(p_rid)) {
  1855. Light *light = light_owner.get( p_rid );
  1856. ERR_FAIL_COND(!light)
  1857. light_owner.free(p_rid);
  1858. memdelete(light);
  1859. } else if (light_instance_owner.owns(p_rid)) {
  1860. LightInstance *light_instance = light_instance_owner.get( p_rid );
  1861. ERR_FAIL_COND(!light_instance);
  1862. light_instance_owner.free(p_rid);
  1863. memdelete( light_instance );
  1864. } else if (fx_owner.owns(p_rid)) {
  1865. FX *fx = fx_owner.get( p_rid );
  1866. ERR_FAIL_COND(!fx);
  1867. fx_owner.free(p_rid);
  1868. memdelete( fx );
  1869. };
  1870. }
  1871. void RasterizerIPhone::init() {
  1872. glEnable(GL_DEPTH_TEST);
  1873. glDepthFunc(GL_LEQUAL);
  1874. glFrontFace(GL_CW);
  1875. glEnable(GL_TEXTURE_2D);
  1876. }
  1877. void RasterizerIPhone::finish() {
  1878. }
  1879. int RasterizerIPhone::get_render_info(VS::RenderInfo p_info) {
  1880. return false;
  1881. }
  1882. RasterizerIPhone::RasterizerIPhone() {
  1883. frame = 0;
  1884. };
  1885. RasterizerIPhone::~RasterizerIPhone() {
  1886. };
  1887. #endif