visual_server.cpp 32 KB

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