visual_server.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. /*************************************************************************/
  2. /* visual_server.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 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. #include "visual_server.h"
  30. #include "globals.h"
  31. #include "method_bind_ext.inc"
  32. VisualServer *VisualServer::singleton=NULL;
  33. VisualServer* (*VisualServer::create_func)()=NULL;
  34. VisualServer *VisualServer::get_singleton() {
  35. return singleton;
  36. }
  37. void VisualServer::set_mipmap_policy(MipMapPolicy p_policy) {
  38. mm_policy=p_policy;
  39. }
  40. VisualServer::MipMapPolicy VisualServer::get_mipmap_policy() const {
  41. return (VisualServer::MipMapPolicy)mm_policy;
  42. }
  43. DVector<String> VisualServer::_shader_get_param_list(RID p_shader) const {
  44. //remove at some point
  45. DVector<String> pl;
  46. #if 0
  47. List<StringName> params;
  48. shader_get_param_list(p_shader,&params);
  49. for(List<StringName>::Element *E=params.front();E;E=E->next()) {
  50. pl.push_back(E->get());
  51. }
  52. #endif
  53. return pl;
  54. }
  55. VisualServer *VisualServer::create() {
  56. ERR_FAIL_COND_V(singleton,NULL);
  57. if (create_func)
  58. return create_func();
  59. return NULL;
  60. }
  61. RID VisualServer::texture_create_from_image(const Image& p_image,uint32_t p_flags) {
  62. RID texture = texture_create();
  63. texture_allocate(texture,p_image.get_width(), p_image.get_height(), p_image.get_format(), p_flags); //if it has mipmaps, use, else generate
  64. ERR_FAIL_COND_V(!texture.is_valid(),texture);
  65. texture_set_data(texture, p_image );
  66. return texture;
  67. }
  68. RID VisualServer::get_test_texture() {
  69. if (test_texture) {
  70. return test_texture;
  71. };
  72. #define TEST_TEXTURE_SIZE 256
  73. Image data(TEST_TEXTURE_SIZE,TEST_TEXTURE_SIZE,0,Image::FORMAT_RGB);
  74. for (int x=0;x<TEST_TEXTURE_SIZE;x++) {
  75. for (int y=0;y<TEST_TEXTURE_SIZE;y++) {
  76. Color c;
  77. int r=255-(x+y)/2;
  78. if ((x%(TEST_TEXTURE_SIZE/8))<2 ||(y%(TEST_TEXTURE_SIZE/8))<2) {
  79. c.r=y;
  80. c.g=r;
  81. c.b=x;
  82. } else {
  83. c.r=r;
  84. c.g=x;
  85. c.b=y;
  86. }
  87. data.put_pixel(x, y, c);
  88. }
  89. }
  90. test_texture = texture_create_from_image(data);
  91. return test_texture;
  92. };
  93. void VisualServer::_free_internal_rids() {
  94. if (test_texture.is_valid())
  95. free(test_texture);
  96. if (white_texture.is_valid())
  97. free(white_texture);
  98. if (test_material.is_valid())
  99. free(test_material);
  100. for(int i=0;i<16;i++) {
  101. if (material_2d[i].is_valid())
  102. free(material_2d[i]);
  103. }
  104. }
  105. RID VisualServer::_make_test_cube() {
  106. DVector<Vector3> vertices;
  107. DVector<Vector3> normals;
  108. DVector<float> tangents;
  109. DVector<Vector3> uvs;
  110. int vtx_idx=0;
  111. #define ADD_VTX(m_idx);\
  112. vertices.push_back( face_points[m_idx] );\
  113. normals.push_back( normal_points[m_idx] );\
  114. tangents.push_back( normal_points[m_idx][1] );\
  115. tangents.push_back( normal_points[m_idx][2] );\
  116. tangents.push_back( normal_points[m_idx][0] );\
  117. tangents.push_back( 1.0 );\
  118. uvs.push_back( Vector3(uv_points[m_idx*2+0],uv_points[m_idx*2+1],0) );\
  119. vtx_idx++;\
  120. for (int i=0;i<6;i++) {
  121. Vector3 face_points[4];
  122. Vector3 normal_points[4];
  123. float uv_points[8]={0,0,0,1,1,1,1,0};
  124. for (int j=0;j<4;j++) {
  125. float v[3];
  126. v[0]=1.0;
  127. v[1]=1-2*((j>>1)&1);
  128. v[2]=v[1]*(1-2*(j&1));
  129. for (int k=0;k<3;k++) {
  130. if (i<3)
  131. face_points[j][(i+k)%3]=v[k]*(i>=3?-1:1);
  132. else
  133. face_points[3-j][(i+k)%3]=v[k]*(i>=3?-1:1);
  134. }
  135. normal_points[j]=Vector3();
  136. normal_points[j][i%3]=(i>=3?-1:1);
  137. }
  138. //tri 1
  139. ADD_VTX(0);
  140. ADD_VTX(1);
  141. ADD_VTX(2);
  142. //tri 2
  143. ADD_VTX(2);
  144. ADD_VTX(3);
  145. ADD_VTX(0);
  146. }
  147. RID test_cube = mesh_create();
  148. Array d;
  149. d.resize(VS::ARRAY_MAX);
  150. d[VisualServer::ARRAY_NORMAL]= normals ;
  151. d[VisualServer::ARRAY_TANGENT]= tangents ;
  152. d[VisualServer::ARRAY_TEX_UV]= uvs ;
  153. d[VisualServer::ARRAY_VERTEX]= vertices ;
  154. DVector<int> indices;
  155. indices.resize(vertices.size());
  156. for(int i=0;i<vertices.size();i++)
  157. indices.set(i,i);
  158. d[VisualServer::ARRAY_INDEX]=indices;
  159. mesh_add_surface( test_cube, PRIMITIVE_TRIANGLES,d );
  160. test_material = fixed_material_create();
  161. //material_set_flag(material, MATERIAL_FLAG_BILLBOARD_TOGGLE,true);
  162. fixed_material_set_texture( test_material, FIXED_MATERIAL_PARAM_DIFFUSE, get_test_texture() );
  163. fixed_material_set_param( test_material, FIXED_MATERIAL_PARAM_SPECULAR_EXP, 70 );
  164. fixed_material_set_param( test_material, FIXED_MATERIAL_PARAM_EMISSION, Color(0.2,0.2,0.2) );
  165. fixed_material_set_param( test_material, FIXED_MATERIAL_PARAM_DIFFUSE, Color(1, 1, 1) );
  166. fixed_material_set_param( test_material, FIXED_MATERIAL_PARAM_SPECULAR, Color(1,1,1) );
  167. mesh_surface_set_material(test_cube, 0, test_material );
  168. return test_cube;
  169. }
  170. RID VisualServer::make_sphere_mesh(int p_lats,int p_lons,float p_radius) {
  171. DVector<Vector3> vertices;
  172. DVector<Vector3> normals;
  173. for(int i = 1; i <= p_lats; i++) {
  174. double lat0 = Math_PI * (-0.5 + (double) (i - 1) / p_lats);
  175. double z0 = Math::sin(lat0);
  176. double zr0 = Math::cos(lat0);
  177. double lat1 = Math_PI * (-0.5 + (double) i / p_lats);
  178. double z1 = Math::sin(lat1);
  179. double zr1 = Math::cos(lat1);
  180. for(int j = p_lons; j >= 1; j--) {
  181. double lng0 = 2 * Math_PI * (double) (j - 1) / p_lons;
  182. double x0 = Math::cos(lng0);
  183. double y0 = Math::sin(lng0);
  184. double lng1 = 2 * Math_PI * (double) (j) / p_lons;
  185. double x1 = Math::cos(lng1);
  186. double y1 = Math::sin(lng1);
  187. Vector3 v[4]={
  188. Vector3(x1 * zr0, z0, y1 *zr0),
  189. Vector3(x1 * zr1, z1, y1 *zr1),
  190. Vector3(x0 * zr1, z1, y0 *zr1),
  191. Vector3(x0 * zr0, z0, y0 *zr0)
  192. };
  193. #define ADD_POINT(m_idx)\
  194. normals.push_back(v[m_idx]); \
  195. vertices.push_back(v[m_idx]*p_radius);\
  196. ADD_POINT(0);
  197. ADD_POINT(1);
  198. ADD_POINT(2);
  199. ADD_POINT(2);
  200. ADD_POINT(3);
  201. ADD_POINT(0);
  202. }
  203. }
  204. RID mesh = mesh_create();
  205. Array d;
  206. d.resize(VS::ARRAY_MAX);
  207. d[ARRAY_VERTEX]=vertices;
  208. d[ARRAY_NORMAL]=normals;
  209. mesh_add_surface(mesh,PRIMITIVE_TRIANGLES,d);
  210. return mesh;
  211. }
  212. RID VisualServer::material_2d_get(bool p_shaded, bool p_transparent, bool p_cut_alpha, bool p_opaque_prepass) {
  213. int version=0;
  214. if (p_shaded)
  215. version=1;
  216. if (p_transparent)
  217. version|=2;
  218. if (p_cut_alpha)
  219. version|=4;
  220. if (p_opaque_prepass)
  221. version|=8;
  222. if (material_2d[version].is_valid())
  223. return material_2d[version];
  224. //not valid, make
  225. material_2d[version]=fixed_material_create();
  226. fixed_material_set_flag(material_2d[version],FIXED_MATERIAL_FLAG_USE_ALPHA,p_transparent);
  227. fixed_material_set_flag(material_2d[version],FIXED_MATERIAL_FLAG_USE_COLOR_ARRAY,true);
  228. fixed_material_set_flag(material_2d[version],FIXED_MATERIAL_FLAG_DISCARD_ALPHA,p_cut_alpha);
  229. material_set_flag(material_2d[version],MATERIAL_FLAG_UNSHADED,!p_shaded);
  230. material_set_flag(material_2d[version],MATERIAL_FLAG_DOUBLE_SIDED,true);
  231. material_set_depth_draw_mode(material_2d[version],p_opaque_prepass?MATERIAL_DEPTH_DRAW_OPAQUE_PRE_PASS_ALPHA:MATERIAL_DEPTH_DRAW_OPAQUE_ONLY);
  232. fixed_material_set_texture(material_2d[version],FIXED_MATERIAL_PARAM_DIFFUSE,get_white_texture());
  233. //material cut alpha?
  234. return material_2d[version];
  235. }
  236. RID VisualServer::get_white_texture() {
  237. if (white_texture.is_valid())
  238. return white_texture;
  239. DVector<uint8_t> wt;
  240. wt.resize(16*3);
  241. {
  242. DVector<uint8_t>::Write w =wt.write();
  243. for(int i=0;i<16*3;i++)
  244. w[i]=255;
  245. }
  246. Image white(4,4,0,Image::FORMAT_RGB,wt);
  247. white_texture=texture_create();
  248. texture_allocate(white_texture,4,4,Image::FORMAT_RGB);
  249. texture_set_data(white_texture,white);
  250. return white_texture;
  251. }
  252. void VisualServer::_bind_methods() {
  253. ObjectTypeDB::bind_method(_MD("texture_create"),&VisualServer::texture_create);
  254. ObjectTypeDB::bind_method(_MD("texture_create_from_image"),&VisualServer::texture_create_from_image,DEFVAL( TEXTURE_FLAGS_DEFAULT ) );
  255. //ObjectTypeDB::bind_method(_MD("texture_allocate"),&VisualServer::texture_allocate,DEFVAL( TEXTURE_FLAGS_DEFAULT ) );
  256. //ObjectTypeDB::bind_method(_MD("texture_set_data"),&VisualServer::texture_blit_rect,DEFVAL( CUBEMAP_LEFT ) );
  257. //ObjectTypeDB::bind_method(_MD("texture_get_rect"),&VisualServer::texture_get_rect );
  258. ObjectTypeDB::bind_method(_MD("texture_set_flags"),&VisualServer::texture_set_flags );
  259. ObjectTypeDB::bind_method(_MD("texture_get_flags"),&VisualServer::texture_get_flags );
  260. ObjectTypeDB::bind_method(_MD("texture_get_width"),&VisualServer::texture_get_width );
  261. ObjectTypeDB::bind_method(_MD("texture_get_height"),&VisualServer::texture_get_height );
  262. ObjectTypeDB::bind_method(_MD("texture_set_shrink_all_x2_on_set_data","shrink"),&VisualServer::texture_set_shrink_all_x2_on_set_data );
  263. #ifndef _3D_DISABLED
  264. ObjectTypeDB::bind_method(_MD("shader_create","mode"),&VisualServer::shader_create,DEFVAL(SHADER_MATERIAL));
  265. ObjectTypeDB::bind_method(_MD("shader_set_mode","shader","mode"),&VisualServer::shader_set_mode);
  266. ObjectTypeDB::bind_method(_MD("material_create"),&VisualServer::material_create);
  267. ObjectTypeDB::bind_method(_MD("material_set_shader","shader"),&VisualServer::material_set_shader);
  268. ObjectTypeDB::bind_method(_MD("material_get_shader"),&VisualServer::material_get_shader);
  269. ObjectTypeDB::bind_method(_MD("material_set_param"),&VisualServer::material_set_param);
  270. ObjectTypeDB::bind_method(_MD("material_get_param"),&VisualServer::material_get_param);
  271. ObjectTypeDB::bind_method(_MD("material_set_flag"),&VisualServer::material_set_flag);
  272. ObjectTypeDB::bind_method(_MD("material_get_flag"),&VisualServer::material_get_flag);
  273. ObjectTypeDB::bind_method(_MD("material_set_blend_mode"),&VisualServer::material_set_blend_mode);
  274. ObjectTypeDB::bind_method(_MD("material_get_blend_mode"),&VisualServer::material_get_blend_mode);
  275. ObjectTypeDB::bind_method(_MD("material_set_line_width"),&VisualServer::material_set_line_width);
  276. ObjectTypeDB::bind_method(_MD("material_get_line_width"),&VisualServer::material_get_line_width);
  277. ObjectTypeDB::bind_method(_MD("mesh_create"),&VisualServer::mesh_create);
  278. ObjectTypeDB::bind_method(_MD("mesh_add_surface"),&VisualServer::mesh_add_surface, DEFVAL(NO_INDEX_ARRAY));
  279. ObjectTypeDB::bind_method(_MD("mesh_surface_set_material"),&VisualServer::mesh_surface_set_material,DEFVAL(false));
  280. ObjectTypeDB::bind_method(_MD("mesh_surface_get_material"),&VisualServer::mesh_surface_get_material);
  281. ObjectTypeDB::bind_method(_MD("mesh_surface_get_array_len"),&VisualServer::mesh_surface_get_array_len);
  282. ObjectTypeDB::bind_method(_MD("mesh_surface_get_array_index_len"),&VisualServer::mesh_surface_get_array_index_len);
  283. ObjectTypeDB::bind_method(_MD("mesh_surface_get_format"),&VisualServer::mesh_surface_get_format);
  284. ObjectTypeDB::bind_method(_MD("mesh_surface_get_primitive_type"),&VisualServer::mesh_surface_get_primitive_type);
  285. ObjectTypeDB::bind_method(_MD("mesh_remove_surface"),&VisualServer::mesh_remove_surface);
  286. ObjectTypeDB::bind_method(_MD("mesh_get_surface_count"),&VisualServer::mesh_get_surface_count);
  287. ObjectTypeDB::bind_method(_MD("multimesh_create"),&VisualServer::multimesh_create);
  288. ObjectTypeDB::bind_method(_MD("multimesh_set_mesh"),&VisualServer::multimesh_set_mesh);
  289. ObjectTypeDB::bind_method(_MD("multimesh_set_aabb"),&VisualServer::multimesh_set_aabb);
  290. ObjectTypeDB::bind_method(_MD("multimesh_instance_set_transform"),&VisualServer::multimesh_instance_set_transform);
  291. ObjectTypeDB::bind_method(_MD("multimesh_instance_set_color"),&VisualServer::multimesh_instance_set_color);
  292. ObjectTypeDB::bind_method(_MD("multimesh_get_mesh"),&VisualServer::multimesh_get_mesh);
  293. ObjectTypeDB::bind_method(_MD("multimesh_get_aabb"),&VisualServer::multimesh_get_aabb);
  294. ObjectTypeDB::bind_method(_MD("multimesh_instance_get_transform"),&VisualServer::multimesh_instance_get_transform);
  295. ObjectTypeDB::bind_method(_MD("multimesh_instance_get_color"),&VisualServer::multimesh_instance_get_color);
  296. ObjectTypeDB::bind_method(_MD("particles_create"),&VisualServer::particles_create);
  297. ObjectTypeDB::bind_method(_MD("particles_set_amount"),&VisualServer::particles_set_amount);
  298. ObjectTypeDB::bind_method(_MD("particles_get_amount"),&VisualServer::particles_get_amount);
  299. ObjectTypeDB::bind_method(_MD("particles_set_emitting"),&VisualServer::particles_set_emitting);
  300. ObjectTypeDB::bind_method(_MD("particles_is_emitting"),&VisualServer::particles_is_emitting);
  301. ObjectTypeDB::bind_method(_MD("particles_set_visibility_aabb"),&VisualServer::particles_set_visibility_aabb);
  302. ObjectTypeDB::bind_method(_MD("particles_get_visibility_aabb"),&VisualServer::particles_get_visibility_aabb);
  303. ObjectTypeDB::bind_method(_MD("particles_set_variable"),&VisualServer::particles_set_variable);
  304. ObjectTypeDB::bind_method(_MD("particles_get_variable"),&VisualServer::particles_get_variable);
  305. ObjectTypeDB::bind_method(_MD("particles_set_randomness"),&VisualServer::particles_set_randomness);
  306. ObjectTypeDB::bind_method(_MD("particles_get_randomness"),&VisualServer::particles_get_randomness);
  307. ObjectTypeDB::bind_method(_MD("particles_set_color_phases"),&VisualServer::particles_set_color_phases);
  308. ObjectTypeDB::bind_method(_MD("particles_get_color_phases"),&VisualServer::particles_get_color_phases);
  309. ObjectTypeDB::bind_method(_MD("particles_set_color_phase_pos"),&VisualServer::particles_set_color_phase_pos);
  310. ObjectTypeDB::bind_method(_MD("particles_get_color_phase_pos"),&VisualServer::particles_get_color_phase_pos);
  311. ObjectTypeDB::bind_method(_MD("particles_set_color_phase_color"),&VisualServer::particles_set_color_phase_color);
  312. ObjectTypeDB::bind_method(_MD("particles_get_color_phase_color"),&VisualServer::particles_get_color_phase_color);
  313. ObjectTypeDB::bind_method(_MD("particles_set_attractors"),&VisualServer::particles_set_attractors);
  314. ObjectTypeDB::bind_method(_MD("particles_get_attractors"),&VisualServer::particles_get_attractors);
  315. ObjectTypeDB::bind_method(_MD("particles_set_attractor_pos"),&VisualServer::particles_set_attractor_pos);
  316. ObjectTypeDB::bind_method(_MD("particles_get_attractor_pos"),&VisualServer::particles_get_attractor_pos);
  317. ObjectTypeDB::bind_method(_MD("particles_set_attractor_strength"),&VisualServer::particles_set_attractor_strength);
  318. ObjectTypeDB::bind_method(_MD("particles_get_attractor_strength"),&VisualServer::particles_get_attractor_strength);
  319. ObjectTypeDB::bind_method(_MD("particles_set_material"),&VisualServer::particles_set_material,DEFVAL(false));
  320. ObjectTypeDB::bind_method(_MD("particles_set_height_from_velocity"),&VisualServer::particles_set_height_from_velocity);
  321. ObjectTypeDB::bind_method(_MD("particles_has_height_from_velocity"),&VisualServer::particles_has_height_from_velocity);
  322. ObjectTypeDB::bind_method(_MD("light_create"),&VisualServer::light_create);
  323. ObjectTypeDB::bind_method(_MD("light_get_type"),&VisualServer::light_get_type);
  324. ObjectTypeDB::bind_method(_MD("light_set_color"),&VisualServer::light_set_color);
  325. ObjectTypeDB::bind_method(_MD("light_get_color"),&VisualServer::light_get_color);
  326. ObjectTypeDB::bind_method(_MD("light_set_shadow"),&VisualServer::light_set_shadow);
  327. ObjectTypeDB::bind_method(_MD("light_has_shadow"),&VisualServer::light_has_shadow);
  328. ObjectTypeDB::bind_method(_MD("light_set_volumetric"),&VisualServer::light_set_volumetric);
  329. ObjectTypeDB::bind_method(_MD("light_is_volumetric"),&VisualServer::light_is_volumetric);
  330. ObjectTypeDB::bind_method(_MD("light_set_projector"),&VisualServer::light_set_projector);
  331. ObjectTypeDB::bind_method(_MD("light_get_projector"),&VisualServer::light_get_projector);
  332. ObjectTypeDB::bind_method(_MD("light_set_var"),&VisualServer::light_set_param);
  333. ObjectTypeDB::bind_method(_MD("light_get_var"),&VisualServer::light_get_param);
  334. ObjectTypeDB::bind_method(_MD("skeleton_create"),&VisualServer::skeleton_create);
  335. ObjectTypeDB::bind_method(_MD("skeleton_resize"),&VisualServer::skeleton_resize);
  336. ObjectTypeDB::bind_method(_MD("skeleton_get_bone_count"),&VisualServer::skeleton_get_bone_count);
  337. ObjectTypeDB::bind_method(_MD("skeleton_bone_set_transform"),&VisualServer::skeleton_bone_set_transform);
  338. ObjectTypeDB::bind_method(_MD("skeleton_bone_get_transform"),&VisualServer::skeleton_bone_get_transform);
  339. ObjectTypeDB::bind_method(_MD("room_create"),&VisualServer::room_create);
  340. ObjectTypeDB::bind_method(_MD("room_set_bounds"),&VisualServer::room_set_bounds);
  341. ObjectTypeDB::bind_method(_MD("room_get_bounds"),&VisualServer::room_get_bounds);
  342. ObjectTypeDB::bind_method(_MD("portal_create"),&VisualServer::portal_create);
  343. ObjectTypeDB::bind_method(_MD("portal_set_shape"),&VisualServer::portal_set_shape);
  344. ObjectTypeDB::bind_method(_MD("portal_get_shape"),&VisualServer::portal_get_shape);
  345. ObjectTypeDB::bind_method(_MD("portal_set_enabled"),&VisualServer::portal_set_enabled);
  346. ObjectTypeDB::bind_method(_MD("portal_is_enabled"),&VisualServer::portal_is_enabled);
  347. ObjectTypeDB::bind_method(_MD("portal_set_disable_distance"),&VisualServer::portal_set_disable_distance);
  348. ObjectTypeDB::bind_method(_MD("portal_get_disable_distance"),&VisualServer::portal_get_disable_distance);
  349. ObjectTypeDB::bind_method(_MD("portal_set_disabled_color"),&VisualServer::portal_set_disabled_color);
  350. ObjectTypeDB::bind_method(_MD("portal_get_disabled_color"),&VisualServer::portal_get_disabled_color);
  351. ObjectTypeDB::bind_method(_MD("camera_create"),&VisualServer::camera_create);
  352. ObjectTypeDB::bind_method(_MD("camera_set_perspective"),&VisualServer::camera_set_perspective);
  353. ObjectTypeDB::bind_method(_MD("camera_set_orthogonal"),&VisualServer::_camera_set_orthogonal);
  354. ObjectTypeDB::bind_method(_MD("camera_set_transform"),&VisualServer::camera_set_transform);
  355. ObjectTypeDB::bind_method(_MD("viewport_create"),&VisualServer::viewport_create);
  356. ObjectTypeDB::bind_method(_MD("viewport_set_rect"),&VisualServer::_viewport_set_rect);
  357. ObjectTypeDB::bind_method(_MD("viewport_get_rect"),&VisualServer::_viewport_get_rect);
  358. ObjectTypeDB::bind_method(_MD("viewport_attach_camera"),&VisualServer::viewport_attach_camera,DEFVAL(RID()));
  359. ObjectTypeDB::bind_method(_MD("viewport_get_attached_camera"),&VisualServer::viewport_get_attached_camera);
  360. ObjectTypeDB::bind_method(_MD("viewport_get_scenario"),&VisualServer::viewport_get_scenario);
  361. ObjectTypeDB::bind_method(_MD("viewport_attach_canvas"),&VisualServer::viewport_attach_canvas);
  362. ObjectTypeDB::bind_method(_MD("viewport_remove_canvas"),&VisualServer::viewport_remove_canvas);
  363. ObjectTypeDB::bind_method(_MD("viewport_set_global_canvas_transform"),&VisualServer::viewport_set_global_canvas_transform);
  364. ObjectTypeDB::bind_method(_MD("scenario_create"),&VisualServer::scenario_create);
  365. ObjectTypeDB::bind_method(_MD("scenario_set_debug"),&VisualServer::scenario_set_debug);
  366. ObjectTypeDB::bind_method(_MD("instance_create"),&VisualServer::instance_create,DEFVAL(RID()));
  367. ObjectTypeDB::bind_method(_MD("instance_get_base"),&VisualServer::instance_get_base);
  368. ObjectTypeDB::bind_method(_MD("instance_get_base_aabb"),&VisualServer::instance_get_base);
  369. ObjectTypeDB::bind_method(_MD("instance_set_transform"),&VisualServer::instance_set_transform);
  370. ObjectTypeDB::bind_method(_MD("instance_get_transform"),&VisualServer::instance_get_transform);
  371. ObjectTypeDB::bind_method(_MD("instance_attach_object_instance_ID"),&VisualServer::instance_attach_object_instance_ID);
  372. ObjectTypeDB::bind_method(_MD("instance_get_object_instance_ID"),&VisualServer::instance_get_object_instance_ID);
  373. ObjectTypeDB::bind_method(_MD("instance_attach_skeleton"),&VisualServer::instance_attach_skeleton);
  374. ObjectTypeDB::bind_method(_MD("instance_get_skeleton"),&VisualServer::instance_get_skeleton);
  375. ObjectTypeDB::bind_method(_MD("instance_set_room"),&VisualServer::instance_set_room);
  376. ObjectTypeDB::bind_method(_MD("instance_get_room"),&VisualServer::instance_get_room);
  377. ObjectTypeDB::bind_method(_MD("instance_set_exterior"),&VisualServer::instance_set_exterior);
  378. ObjectTypeDB::bind_method(_MD("instance_is_exterior"),&VisualServer::instance_is_exterior);
  379. ObjectTypeDB::bind_method(_MD("instances_cull_aabb"),&VisualServer::instances_cull_aabb);
  380. ObjectTypeDB::bind_method(_MD("instances_cull_ray"),&VisualServer::instances_cull_ray);
  381. ObjectTypeDB::bind_method(_MD("instances_cull_convex"),&VisualServer::instances_cull_convex);
  382. ObjectTypeDB::bind_method(_MD("instance_geometry_override_material_param"),&VisualServer::instance_get_room);
  383. ObjectTypeDB::bind_method(_MD("instance_geometry_get_material_param"),&VisualServer::instance_get_room);
  384. ObjectTypeDB::bind_method(_MD("get_test_cube"),&VisualServer::get_test_cube);
  385. #endif
  386. ObjectTypeDB::bind_method(_MD("canvas_create"),&VisualServer::canvas_create);
  387. ObjectTypeDB::bind_method(_MD("canvas_item_create"),&VisualServer::canvas_item_create);
  388. ObjectTypeDB::bind_method(_MD("canvas_item_set_parent"),&VisualServer::canvas_item_set_parent);
  389. ObjectTypeDB::bind_method(_MD("canvas_item_get_parent"),&VisualServer::canvas_item_get_parent);
  390. ObjectTypeDB::bind_method(_MD("canvas_item_set_transform"),&VisualServer::canvas_item_set_transform);
  391. ObjectTypeDB::bind_method(_MD("canvas_item_set_custom_rect"),&VisualServer::canvas_item_set_custom_rect);
  392. ObjectTypeDB::bind_method(_MD("canvas_item_set_clip"),&VisualServer::canvas_item_set_clip);
  393. ObjectTypeDB::bind_method(_MD("canvas_item_set_opacity"),&VisualServer::canvas_item_set_opacity);
  394. ObjectTypeDB::bind_method(_MD("canvas_item_get_opacity"),&VisualServer::canvas_item_get_opacity);
  395. ObjectTypeDB::bind_method(_MD("canvas_item_set_self_opacity"),&VisualServer::canvas_item_set_self_opacity);
  396. ObjectTypeDB::bind_method(_MD("canvas_item_get_self_opacity"),&VisualServer::canvas_item_get_self_opacity);
  397. ObjectTypeDB::bind_method(_MD("canvas_item_set_z"),&VisualServer::canvas_item_set_z);
  398. ObjectTypeDB::bind_method(_MD("canvas_item_add_line"),&VisualServer::canvas_item_add_line, DEFVAL(1.0));
  399. ObjectTypeDB::bind_method(_MD("canvas_item_add_rect"),&VisualServer::canvas_item_add_rect);
  400. ObjectTypeDB::bind_method(_MD("canvas_item_add_texture_rect"),&VisualServer::canvas_item_add_texture_rect, DEFVAL(Color(1,1,1)), DEFVAL(false));
  401. ObjectTypeDB::bind_method(_MD("canvas_item_add_texture_rect_region"),&VisualServer::canvas_item_add_texture_rect_region, DEFVAL(Color(1,1,1)), DEFVAL(false));
  402. ObjectTypeDB::bind_method(_MD("canvas_item_add_style_box"),&VisualServer::_canvas_item_add_style_box, DEFVAL(Color(1,1,1)));
  403. // ObjectTypeDB::bind_method(_MD("canvas_item_add_primitive"),&VisualServer::canvas_item_add_primitive,DEFVAL(Vector<Vector2>()),DEFVAL(RID()));
  404. ObjectTypeDB::bind_method(_MD("canvas_item_add_circle"),&VisualServer::canvas_item_add_circle);
  405. ObjectTypeDB::bind_method(_MD("viewport_set_canvas_transform"),&VisualServer::viewport_set_canvas_transform);
  406. ObjectTypeDB::bind_method(_MD("canvas_item_clear"),&VisualServer::canvas_item_clear);
  407. ObjectTypeDB::bind_method(_MD("canvas_item_raise"),&VisualServer::canvas_item_raise);
  408. ObjectTypeDB::bind_method(_MD("cursor_set_rotation"),&VisualServer::cursor_set_rotation);
  409. ObjectTypeDB::bind_method(_MD("cursor_set_texture"),&VisualServer::cursor_set_texture);
  410. ObjectTypeDB::bind_method(_MD("cursor_set_visible"),&VisualServer::cursor_set_visible);
  411. ObjectTypeDB::bind_method(_MD("cursor_set_pos"),&VisualServer::cursor_set_pos);
  412. ObjectTypeDB::bind_method(_MD("black_bars_set_margins","left","top","right","bottom"),&VisualServer::black_bars_set_margins);
  413. ObjectTypeDB::bind_method(_MD("black_bars_set_images","left","top","right","bottom"),&VisualServer::black_bars_set_images);
  414. ObjectTypeDB::bind_method(_MD("make_sphere_mesh"),&VisualServer::make_sphere_mesh);
  415. ObjectTypeDB::bind_method(_MD("mesh_add_surface_from_planes"),&VisualServer::mesh_add_surface_from_planes);
  416. ObjectTypeDB::bind_method(_MD("draw"),&VisualServer::draw);
  417. ObjectTypeDB::bind_method(_MD("sync"),&VisualServer::sync);
  418. ObjectTypeDB::bind_method(_MD("free_rid"),&VisualServer::free);
  419. ObjectTypeDB::bind_method(_MD("set_default_clear_color"),&VisualServer::set_default_clear_color);
  420. ObjectTypeDB::bind_method(_MD("get_render_info"),&VisualServer::get_render_info);
  421. BIND_CONSTANT( NO_INDEX_ARRAY );
  422. BIND_CONSTANT( CUSTOM_ARRAY_SIZE );
  423. BIND_CONSTANT( ARRAY_WEIGHTS_SIZE );
  424. BIND_CONSTANT( MAX_PARTICLE_COLOR_PHASES );
  425. BIND_CONSTANT( MAX_PARTICLE_ATTRACTORS );
  426. BIND_CONSTANT( MAX_CURSORS );
  427. BIND_CONSTANT( TEXTURE_FLAG_MIPMAPS );
  428. BIND_CONSTANT( TEXTURE_FLAG_REPEAT );
  429. BIND_CONSTANT( TEXTURE_FLAG_FILTER );
  430. BIND_CONSTANT( TEXTURE_FLAG_CUBEMAP );
  431. BIND_CONSTANT( TEXTURE_FLAGS_DEFAULT );
  432. BIND_CONSTANT( CUBEMAP_LEFT );
  433. BIND_CONSTANT( CUBEMAP_RIGHT );
  434. BIND_CONSTANT( CUBEMAP_BOTTOM );
  435. BIND_CONSTANT( CUBEMAP_TOP );
  436. BIND_CONSTANT( CUBEMAP_FRONT );
  437. BIND_CONSTANT( CUBEMAP_BACK );
  438. BIND_CONSTANT( SHADER_MATERIAL ); ///< param 0: name
  439. BIND_CONSTANT( SHADER_POST_PROCESS ); ///< param 0: name
  440. BIND_CONSTANT( MATERIAL_FLAG_VISIBLE );
  441. BIND_CONSTANT( MATERIAL_FLAG_DOUBLE_SIDED );
  442. BIND_CONSTANT( MATERIAL_FLAG_INVERT_FACES );
  443. BIND_CONSTANT( MATERIAL_FLAG_UNSHADED );
  444. BIND_CONSTANT( MATERIAL_FLAG_ONTOP );
  445. BIND_CONSTANT( MATERIAL_FLAG_MAX );
  446. BIND_CONSTANT( MATERIAL_BLEND_MODE_MIX );
  447. BIND_CONSTANT( MATERIAL_BLEND_MODE_ADD );
  448. BIND_CONSTANT( MATERIAL_BLEND_MODE_SUB );
  449. BIND_CONSTANT( MATERIAL_BLEND_MODE_MUL );
  450. BIND_CONSTANT( FIXED_MATERIAL_PARAM_DIFFUSE );
  451. BIND_CONSTANT( FIXED_MATERIAL_PARAM_DETAIL );
  452. BIND_CONSTANT( FIXED_MATERIAL_PARAM_SPECULAR );
  453. BIND_CONSTANT( FIXED_MATERIAL_PARAM_EMISSION );
  454. BIND_CONSTANT( FIXED_MATERIAL_PARAM_SPECULAR_EXP );
  455. BIND_CONSTANT( FIXED_MATERIAL_PARAM_GLOW );
  456. BIND_CONSTANT( FIXED_MATERIAL_PARAM_NORMAL );
  457. BIND_CONSTANT( FIXED_MATERIAL_PARAM_SHADE_PARAM );
  458. BIND_CONSTANT( FIXED_MATERIAL_PARAM_MAX );
  459. BIND_CONSTANT( FIXED_MATERIAL_TEXCOORD_SPHERE );
  460. BIND_CONSTANT( FIXED_MATERIAL_TEXCOORD_UV );
  461. BIND_CONSTANT( FIXED_MATERIAL_TEXCOORD_UV_TRANSFORM );
  462. BIND_CONSTANT( FIXED_MATERIAL_TEXCOORD_UV2 );
  463. BIND_CONSTANT( ARRAY_VERTEX );
  464. BIND_CONSTANT( ARRAY_NORMAL );
  465. BIND_CONSTANT( ARRAY_TANGENT );
  466. BIND_CONSTANT( ARRAY_COLOR );
  467. BIND_CONSTANT( ARRAY_TEX_UV );
  468. BIND_CONSTANT( ARRAY_BONES );
  469. BIND_CONSTANT( ARRAY_WEIGHTS );
  470. BIND_CONSTANT( ARRAY_INDEX );
  471. BIND_CONSTANT( ARRAY_MAX );
  472. BIND_CONSTANT( ARRAY_FORMAT_VERTEX );
  473. BIND_CONSTANT( ARRAY_FORMAT_NORMAL );
  474. BIND_CONSTANT( ARRAY_FORMAT_TANGENT );
  475. BIND_CONSTANT( ARRAY_FORMAT_COLOR );
  476. BIND_CONSTANT( ARRAY_FORMAT_TEX_UV );
  477. BIND_CONSTANT( ARRAY_FORMAT_BONES );
  478. BIND_CONSTANT( ARRAY_FORMAT_WEIGHTS );
  479. BIND_CONSTANT( ARRAY_FORMAT_INDEX );
  480. BIND_CONSTANT( PRIMITIVE_POINTS );
  481. BIND_CONSTANT( PRIMITIVE_LINES );
  482. BIND_CONSTANT( PRIMITIVE_LINE_STRIP );
  483. BIND_CONSTANT( PRIMITIVE_LINE_LOOP );
  484. BIND_CONSTANT( PRIMITIVE_TRIANGLES );
  485. BIND_CONSTANT( PRIMITIVE_TRIANGLE_STRIP );
  486. BIND_CONSTANT( PRIMITIVE_TRIANGLE_FAN );
  487. BIND_CONSTANT( PRIMITIVE_MAX );
  488. BIND_CONSTANT( PARTICLE_LIFETIME );
  489. BIND_CONSTANT( PARTICLE_SPREAD );
  490. BIND_CONSTANT( PARTICLE_GRAVITY );
  491. BIND_CONSTANT( PARTICLE_LINEAR_VELOCITY );
  492. BIND_CONSTANT( PARTICLE_ANGULAR_VELOCITY );
  493. BIND_CONSTANT( PARTICLE_LINEAR_ACCELERATION );
  494. BIND_CONSTANT( PARTICLE_RADIAL_ACCELERATION );
  495. BIND_CONSTANT( PARTICLE_TANGENTIAL_ACCELERATION );
  496. BIND_CONSTANT( PARTICLE_INITIAL_SIZE );
  497. BIND_CONSTANT( PARTICLE_FINAL_SIZE );
  498. BIND_CONSTANT( PARTICLE_INITIAL_ANGLE );
  499. BIND_CONSTANT( PARTICLE_HEIGHT );
  500. BIND_CONSTANT( PARTICLE_HEIGHT_SPEED_SCALE );
  501. BIND_CONSTANT( PARTICLE_VAR_MAX );
  502. BIND_CONSTANT( LIGHT_DIRECTIONAL );
  503. BIND_CONSTANT( LIGHT_OMNI );
  504. BIND_CONSTANT( LIGHT_SPOT );
  505. BIND_CONSTANT( LIGHT_COLOR_DIFFUSE );
  506. BIND_CONSTANT( LIGHT_COLOR_SPECULAR );
  507. BIND_CONSTANT( LIGHT_PARAM_SPOT_ATTENUATION );
  508. BIND_CONSTANT( LIGHT_PARAM_SPOT_ANGLE );
  509. BIND_CONSTANT( LIGHT_PARAM_RADIUS );
  510. BIND_CONSTANT( LIGHT_PARAM_ENERGY );
  511. BIND_CONSTANT( LIGHT_PARAM_ATTENUATION );
  512. BIND_CONSTANT( LIGHT_PARAM_MAX );
  513. BIND_CONSTANT( SCENARIO_DEBUG_DISABLED );
  514. BIND_CONSTANT( SCENARIO_DEBUG_WIREFRAME );
  515. BIND_CONSTANT( SCENARIO_DEBUG_OVERDRAW );
  516. BIND_CONSTANT( INSTANCE_MESH );
  517. BIND_CONSTANT( INSTANCE_MULTIMESH );
  518. BIND_CONSTANT( INSTANCE_PARTICLES );
  519. BIND_CONSTANT( INSTANCE_LIGHT );
  520. BIND_CONSTANT( INSTANCE_ROOM );
  521. BIND_CONSTANT( INSTANCE_PORTAL );
  522. BIND_CONSTANT( INSTANCE_GEOMETRY_MASK );
  523. BIND_CONSTANT( INFO_OBJECTS_IN_FRAME );
  524. BIND_CONSTANT( INFO_VERTICES_IN_FRAME );
  525. BIND_CONSTANT( INFO_MATERIAL_CHANGES_IN_FRAME );
  526. BIND_CONSTANT( INFO_SHADER_CHANGES_IN_FRAME );
  527. BIND_CONSTANT( INFO_SURFACE_CHANGES_IN_FRAME );
  528. BIND_CONSTANT( INFO_DRAW_CALLS_IN_FRAME );
  529. BIND_CONSTANT( INFO_USAGE_VIDEO_MEM_TOTAL );
  530. BIND_CONSTANT( INFO_VIDEO_MEM_USED );
  531. BIND_CONSTANT( INFO_TEXTURE_MEM_USED );
  532. BIND_CONSTANT( INFO_VERTEX_MEM_USED );
  533. }
  534. void VisualServer::_canvas_item_add_style_box(RID p_item, const Rect2& p_rect, RID p_texture,const Vector<float>& p_margins, const Color& p_modulate) {
  535. ERR_FAIL_COND(p_margins.size()!=4);
  536. canvas_item_add_style_box(p_item, p_rect, p_texture,Vector2(p_margins[0],p_margins[1]),Vector2(p_margins[2],p_margins[3]),true,p_modulate);
  537. }
  538. void VisualServer::_camera_set_orthogonal(RID p_camera,float p_size,float p_z_near,float p_z_far) {
  539. camera_set_orthogonal(p_camera,p_size,p_z_near,p_z_far);
  540. }
  541. void VisualServer::_viewport_set_rect(RID p_viewport,const Rect2& p_rect) {
  542. ViewportRect r;
  543. r.x=p_rect.pos.x;
  544. r.y=p_rect.pos.y;
  545. r.width=p_rect.size.x;
  546. r.height=p_rect.size.y;
  547. viewport_set_rect(p_viewport,r);
  548. }
  549. Rect2 VisualServer::_viewport_get_rect(RID p_viewport) const {
  550. ViewportRect r=viewport_get_rect(p_viewport);
  551. return Rect2(r.x,r.y,r.width,r.height);
  552. }
  553. void VisualServer::mesh_add_surface_from_mesh_data( RID p_mesh, const Geometry::MeshData& p_mesh_data) {
  554. #if 1
  555. DVector<Vector3> vertices;
  556. DVector<Vector3> normals;
  557. for (int i=0;i<p_mesh_data.faces.size();i++) {
  558. const Geometry::MeshData::Face& f = p_mesh_data.faces[i];
  559. for (int j=2;j<f.indices.size();j++) {
  560. #define _ADD_VERTEX(m_idx)\
  561. vertices.push_back( p_mesh_data.vertices[ f.indices[m_idx] ] );\
  562. normals.push_back( f.plane.normal );
  563. _ADD_VERTEX( 0 );
  564. _ADD_VERTEX( j-1 );
  565. _ADD_VERTEX( j );
  566. }
  567. }
  568. int s = mesh_get_surface_count(p_mesh);
  569. Array d;
  570. d.resize(VS::ARRAY_MAX);
  571. d[ARRAY_VERTEX]=vertices;
  572. d[ARRAY_NORMAL]=normals;
  573. mesh_add_surface(p_mesh,PRIMITIVE_TRIANGLES, d);
  574. #else
  575. DVector<Vector3> vertices;
  576. for (int i=0;i<p_mesh_data.edges.size();i++) {
  577. const Geometry::MeshData::Edge& f = p_mesh_data.edges[i];
  578. vertices.push_back(p_mesh_data.vertices[ f.a]);
  579. vertices.push_back(p_mesh_data.vertices[ f.b]);
  580. }
  581. Array d;
  582. d.resize(VS::ARRAY_MAX);
  583. d[ARRAY_VERTEX]=vertices;
  584. mesh_add_surface(p_mesh,PRIMITIVE_LINES, d);
  585. #endif
  586. }
  587. void VisualServer::mesh_add_surface_from_planes( RID p_mesh, const DVector<Plane>& p_planes) {
  588. Geometry::MeshData mdata = Geometry::build_convex_mesh(p_planes);
  589. mesh_add_surface_from_mesh_data(p_mesh,mdata);
  590. }
  591. RID VisualServer::instance_create2(RID p_base, RID p_scenario) {
  592. RID instance = instance_create();
  593. instance_set_base(instance,p_base);
  594. instance_set_scenario(instance,p_scenario);
  595. return instance;
  596. }
  597. VisualServer::VisualServer() {
  598. // ERR_FAIL_COND(singleton);
  599. singleton=this;
  600. mm_policy=GLOBAL_DEF("render/mipmap_policy",0);
  601. if (mm_policy<0 || mm_policy>2)
  602. mm_policy=0;
  603. }
  604. VisualServer::~VisualServer() {
  605. singleton=NULL;
  606. }