camera.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. /*************************************************************************/
  2. /* camera.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 "camera.h"
  30. #include "camera_matrix.h"
  31. #include "scene/resources/material.h"
  32. #include "scene/resources/surface_tool.h"
  33. void Camera::_update_audio_listener_state() {
  34. }
  35. void Camera::_request_camera_update() {
  36. _update_camera();
  37. }
  38. void Camera::_update_camera_mode() {
  39. force_change=true;
  40. switch(mode) {
  41. case PROJECTION_PERSPECTIVE: {
  42. set_perspective(fov,near,far);
  43. } break;
  44. case PROJECTION_ORTHOGONAL: {
  45. set_orthogonal(size,near,far);
  46. } break;
  47. }
  48. }
  49. bool Camera::_set(const StringName& p_name, const Variant& p_value) {
  50. bool changed_all=false;
  51. if (p_name=="projection") {
  52. int proj = p_value;
  53. if (proj==PROJECTION_PERSPECTIVE)
  54. mode=PROJECTION_PERSPECTIVE;
  55. if (proj==PROJECTION_ORTHOGONAL)
  56. mode=PROJECTION_ORTHOGONAL;
  57. changed_all=true;
  58. } else if (p_name=="fov" || p_name=="fovy" || p_name=="fovx")
  59. fov=p_value;
  60. else if (p_name=="size" || p_name=="sizex" || p_name=="sizey")
  61. size=p_value;
  62. else if (p_name=="near")
  63. near=p_value;
  64. else if (p_name=="far")
  65. far=p_value;
  66. else if (p_name=="keep_aspect")
  67. set_keep_aspect_mode(KeepAspect(int(p_value)));
  68. else if (p_name=="vaspect")
  69. set_keep_aspect_mode(p_value?KEEP_WIDTH:KEEP_HEIGHT);
  70. else if (p_name=="h_offset")
  71. h_offset=p_value;
  72. else if (p_name=="v_offset")
  73. v_offset=p_value;
  74. else if (p_name=="current") {
  75. if (p_value.operator bool()) {
  76. make_current();
  77. } else {
  78. clear_current();
  79. }
  80. } else if (p_name=="visible_layers") {
  81. set_visible_layers(p_value);
  82. } else if (p_name=="environment") {
  83. set_environment(p_value);
  84. } else
  85. return false;
  86. _update_camera_mode();
  87. if (changed_all)
  88. _change_notify();
  89. return true;
  90. }
  91. bool Camera::_get(const StringName& p_name,Variant &r_ret) const {
  92. if (p_name=="projection") {
  93. r_ret= mode;
  94. } else if (p_name=="fov" || p_name=="fovy" || p_name=="fovx")
  95. r_ret= fov;
  96. else if (p_name=="size" || p_name=="sizex" || p_name=="sizey")
  97. r_ret= size;
  98. else if (p_name=="near")
  99. r_ret= near;
  100. else if (p_name=="far")
  101. r_ret= far;
  102. else if (p_name=="keep_aspect")
  103. r_ret= int(keep_aspect);
  104. else if (p_name=="current") {
  105. if (is_inside_tree() && get_tree()->is_node_being_edited(this)) {
  106. r_ret=current;
  107. } else {
  108. r_ret=is_current();
  109. }
  110. } else if (p_name=="visible_layers") {
  111. r_ret=get_visible_layers();
  112. } else if (p_name=="h_offset") {
  113. r_ret=get_h_offset();
  114. } else if (p_name=="v_offset") {
  115. r_ret=get_v_offset();
  116. } else if (p_name=="environment") {
  117. r_ret=get_environment();
  118. } else
  119. return false;
  120. return true;
  121. }
  122. void Camera::_get_property_list( List<PropertyInfo> *p_list) const {
  123. p_list->push_back( PropertyInfo( Variant::INT, "projection", PROPERTY_HINT_ENUM, "Perspective,Orthogonal") );
  124. switch(mode) {
  125. case PROJECTION_PERSPECTIVE: {
  126. p_list->push_back( PropertyInfo( Variant::REAL, "fov" , PROPERTY_HINT_RANGE, "1,179,0.1",PROPERTY_USAGE_NOEDITOR) );
  127. if (keep_aspect==KEEP_WIDTH)
  128. p_list->push_back( PropertyInfo( Variant::REAL, "fovx" , PROPERTY_HINT_RANGE, "1,179,0.1",PROPERTY_USAGE_EDITOR) );
  129. else
  130. p_list->push_back( PropertyInfo( Variant::REAL, "fovy" , PROPERTY_HINT_RANGE, "1,179,0.1",PROPERTY_USAGE_EDITOR) );
  131. } break;
  132. case PROJECTION_ORTHOGONAL: {
  133. p_list->push_back( PropertyInfo( Variant::REAL, "size" , PROPERTY_HINT_RANGE, "1,16384,0.01",PROPERTY_USAGE_NOEDITOR ) );
  134. if (keep_aspect==KEEP_WIDTH)
  135. p_list->push_back( PropertyInfo( Variant::REAL, "sizex" , PROPERTY_HINT_RANGE, "0.1,16384,0.01",PROPERTY_USAGE_EDITOR) );
  136. else
  137. p_list->push_back( PropertyInfo( Variant::REAL, "sizey" , PROPERTY_HINT_RANGE, "0.1,16384,0.01",PROPERTY_USAGE_EDITOR) );
  138. } break;
  139. }
  140. p_list->push_back( PropertyInfo( Variant::REAL, "near" , PROPERTY_HINT_EXP_RANGE, "0.01,4096.0,0.01") );
  141. p_list->push_back( PropertyInfo( Variant::REAL, "far" , PROPERTY_HINT_EXP_RANGE, "0.01,4096.0,0.01") );
  142. p_list->push_back( PropertyInfo( Variant::INT, "keep_aspect",PROPERTY_HINT_ENUM,"Keep Width,Keep Height") );
  143. p_list->push_back( PropertyInfo( Variant::BOOL, "current" ) );
  144. p_list->push_back( PropertyInfo( Variant::INT, "visible_layers",PROPERTY_HINT_ALL_FLAGS ) );
  145. p_list->push_back( PropertyInfo( Variant::OBJECT, "environment",PROPERTY_HINT_RESOURCE_TYPE,"Environment" ) );
  146. p_list->push_back( PropertyInfo( Variant::REAL, "h_offset" ) );
  147. p_list->push_back( PropertyInfo( Variant::REAL, "v_offset" ) );
  148. }
  149. void Camera::_update_camera() {
  150. Transform tr = get_camera_transform();
  151. tr.origin+=tr.basis.get_axis(1)*v_offset;
  152. tr.origin+=tr.basis.get_axis(0)*h_offset;
  153. VisualServer::get_singleton()->camera_set_transform( camera, tr );
  154. // here goes listener stuff
  155. // if (viewport_ptr && is_inside_scene() && is_current())
  156. // get_viewport()->_camera_transform_changed_notify();
  157. if (is_inside_tree() && is_current()) {
  158. get_viewport()->_camera_transform_changed_notify();
  159. }
  160. if (is_current() && get_world().is_valid()) {
  161. get_world()->_update_camera(this);
  162. }
  163. }
  164. void Camera::_notification(int p_what) {
  165. switch(p_what) {
  166. case NOTIFICATION_ENTER_WORLD: {
  167. bool first_camera = get_viewport()->_camera_add(this);
  168. if (!get_tree()->is_node_being_edited(this) && (current || first_camera))
  169. make_current();
  170. } break;
  171. case NOTIFICATION_TRANSFORM_CHANGED: {
  172. _request_camera_update();
  173. } break;
  174. case NOTIFICATION_EXIT_WORLD: {
  175. if (!get_tree()->is_node_being_edited(this)) {
  176. if (is_current()) {
  177. clear_current();
  178. current=true; //keep it true
  179. } else {
  180. current=false;
  181. }
  182. }
  183. get_viewport()->_camera_remove(this);
  184. } break;
  185. case NOTIFICATION_BECAME_CURRENT: {
  186. if (get_world().is_valid()) {
  187. get_world()->_register_camera(this);
  188. }
  189. } break;
  190. case NOTIFICATION_LOST_CURRENT: {
  191. if (get_world().is_valid()) {
  192. get_world()->_remove_camera(this);
  193. }
  194. } break;
  195. }
  196. }
  197. Transform Camera::get_camera_transform() const {
  198. return get_global_transform().orthonormalized();
  199. }
  200. void Camera::set_perspective(float p_fovy_degrees, float p_z_near, float p_z_far) {
  201. if (!force_change && fov==p_fovy_degrees && p_z_near==near && p_z_far==far && mode==PROJECTION_PERSPECTIVE)
  202. return;
  203. fov=p_fovy_degrees;
  204. near=p_z_near;
  205. far=p_z_far;
  206. mode=PROJECTION_PERSPECTIVE;
  207. VisualServer::get_singleton()->camera_set_perspective(camera,fov,near,far);
  208. update_gizmo();
  209. force_change=false;
  210. }
  211. void Camera::set_orthogonal(float p_size, float p_z_near, float p_z_far) {
  212. if (!force_change && size==p_size && p_z_near==near && p_z_far==far && mode==PROJECTION_ORTHOGONAL)
  213. return;
  214. size = p_size;
  215. near=p_z_near;
  216. far=p_z_far;
  217. mode=PROJECTION_ORTHOGONAL;
  218. force_change=false;
  219. VisualServer::get_singleton()->camera_set_orthogonal(camera,size,near,far);
  220. update_gizmo();
  221. }
  222. RID Camera::get_camera() const {
  223. return camera;
  224. };
  225. void Camera::make_current() {
  226. current=true;
  227. if (!is_inside_tree())
  228. return;
  229. get_viewport()->_camera_set(this);
  230. //get_scene()->call_group(SceneMainLoop::GROUP_CALL_REALTIME,camera_group,"_camera_make_current",this);
  231. }
  232. void Camera::clear_current() {
  233. current=false;
  234. if (!is_inside_tree())
  235. return;
  236. if (get_viewport()->get_camera()==this) {
  237. get_viewport()->_camera_set(NULL);
  238. get_viewport()->_camera_make_next_current(this);
  239. }
  240. }
  241. bool Camera::is_current() const {
  242. if (is_inside_tree() && !get_tree()->is_node_being_edited(this)) {
  243. return get_viewport()->get_camera()==this;
  244. } else
  245. return current;
  246. return false;
  247. }
  248. bool Camera::_can_gizmo_scale() const {
  249. return false;
  250. }
  251. RES Camera::_get_gizmo_geometry() const {
  252. Ref<SurfaceTool> surface_tool( memnew( SurfaceTool ));
  253. Ref<FixedMaterial> mat( memnew( FixedMaterial ));
  254. mat->set_parameter( FixedMaterial::PARAM_DIFFUSE,Color(1.0,0.5,1.0,0.5) );
  255. mat->set_line_width(4);
  256. mat->set_flag(Material::FLAG_DOUBLE_SIDED,true);
  257. mat->set_flag(Material::FLAG_UNSHADED,true);
  258. //mat->set_hint(Material::HINT_NO_DEPTH_DRAW,true);
  259. surface_tool->begin(Mesh::PRIMITIVE_LINES);
  260. surface_tool->set_material(mat);
  261. switch(mode) {
  262. case PROJECTION_PERSPECTIVE: {
  263. Vector3 side=Vector3( Math::sin(Math::deg2rad(fov)), 0, -Math::cos(Math::deg2rad(fov)) );
  264. Vector3 nside=side;
  265. nside.x=-nside.x;
  266. Vector3 up=Vector3(0,side.x,0);
  267. #define ADD_TRIANGLE( m_a, m_b, m_c)\
  268. {\
  269. surface_tool->add_vertex(m_a);\
  270. surface_tool->add_vertex(m_b);\
  271. surface_tool->add_vertex(m_b);\
  272. surface_tool->add_vertex(m_c);\
  273. surface_tool->add_vertex(m_c);\
  274. surface_tool->add_vertex(m_a);\
  275. }
  276. ADD_TRIANGLE( Vector3(), side+up, side-up );
  277. ADD_TRIANGLE( Vector3(), nside+up, nside-up );
  278. ADD_TRIANGLE( Vector3(), side+up, nside+up );
  279. ADD_TRIANGLE( Vector3(), side-up, nside-up );
  280. side.x*=0.25;
  281. nside.x*=0.25;
  282. Vector3 tup( 0, up.y*3/2,side.z);
  283. ADD_TRIANGLE( tup, side+up, nside+up );
  284. } break;
  285. case PROJECTION_ORTHOGONAL: {
  286. #define ADD_QUAD( m_a, m_b, m_c, m_d)\
  287. {\
  288. surface_tool->add_vertex(m_a);\
  289. surface_tool->add_vertex(m_b);\
  290. surface_tool->add_vertex(m_b);\
  291. surface_tool->add_vertex(m_c);\
  292. surface_tool->add_vertex(m_c);\
  293. surface_tool->add_vertex(m_d);\
  294. surface_tool->add_vertex(m_d);\
  295. surface_tool->add_vertex(m_a);\
  296. }
  297. float hsize=size*0.5;
  298. Vector3 right(hsize,0,0);
  299. Vector3 up(0,hsize,0);
  300. Vector3 back(0,0,-1.0);
  301. Vector3 front(0,0,0);
  302. ADD_QUAD( -up-right,-up+right,up+right,up-right);
  303. ADD_QUAD( -up-right+back,-up+right+back,up+right+back,up-right+back);
  304. ADD_QUAD( up+right,up+right+back,up-right+back,up-right);
  305. ADD_QUAD( -up+right,-up+right+back,-up-right+back,-up-right);
  306. right.x*=0.25;
  307. Vector3 tup( 0, up.y*3/2,back.z );
  308. ADD_TRIANGLE( tup, right+up+back, -right+up+back );
  309. } break;
  310. }
  311. return surface_tool->commit();
  312. }
  313. Vector3 Camera::project_ray_normal(const Point2& p_pos) const {
  314. Vector3 ray = project_local_ray_normal(p_pos);
  315. return get_camera_transform().basis.xform(ray).normalized();
  316. };
  317. Vector3 Camera::project_local_ray_normal(const Point2& p_pos) const {
  318. if (!is_inside_tree()) {
  319. ERR_EXPLAIN("Camera is not inside scene.");
  320. ERR_FAIL_COND_V(!is_inside_tree(),Vector3());
  321. }
  322. #if 0
  323. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  324. Vector2 cpos = p_pos;
  325. #else
  326. Size2 viewport_size = get_viewport()->get_camera_rect_size();
  327. Vector2 cpos = get_viewport()->get_camera_coords(p_pos);
  328. #endif
  329. Vector3 ray;
  330. if (mode==PROJECTION_ORTHOGONAL) {
  331. ray=Vector3(0,0,-1);
  332. } else {
  333. CameraMatrix cm;
  334. cm.set_perspective(fov,viewport_size.get_aspect(),near,far,keep_aspect==KEEP_WIDTH);
  335. float screen_w,screen_h;
  336. cm.get_viewport_size(screen_w,screen_h);
  337. ray=Vector3( ((cpos.x/viewport_size.width)*2.0-1.0)*screen_w, ((1.0-(cpos.y/viewport_size.height))*2.0-1.0)*screen_h,-near).normalized();
  338. }
  339. return ray;
  340. };
  341. Vector3 Camera::project_ray_origin(const Point2& p_pos) const {
  342. if (!is_inside_tree()) {
  343. ERR_EXPLAIN("Camera is not inside scene.");
  344. ERR_FAIL_COND_V(!is_inside_tree(),Vector3());
  345. }
  346. #if 0
  347. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  348. Vector2 cpos = p_pos;
  349. #else
  350. Size2 viewport_size = get_viewport()->get_camera_rect_size();
  351. Vector2 cpos = get_viewport()->get_camera_coords(p_pos);
  352. #endif
  353. ERR_FAIL_COND_V( viewport_size.y == 0, Vector3() );
  354. // float aspect = viewport_size.x / viewport_size.y;
  355. if (mode == PROJECTION_PERSPECTIVE) {
  356. return get_camera_transform().origin;
  357. } else {
  358. Vector2 pos = cpos / viewport_size;
  359. float vsize,hsize;
  360. if (keep_aspect==KEEP_WIDTH) {
  361. vsize = size/viewport_size.get_aspect();
  362. hsize = size;
  363. } else {
  364. hsize = size*viewport_size.get_aspect();
  365. vsize = size;
  366. }
  367. Vector3 ray;
  368. ray.x = pos.x * (hsize) - hsize/2;
  369. ray.y = (1.0 - pos.y) * (vsize) - vsize/2;
  370. ray.z = -near;
  371. ray = get_camera_transform().xform(ray);
  372. return ray;
  373. };
  374. };
  375. bool Camera::is_position_behind(const Vector3& p_pos) const {
  376. Transform t = get_global_transform();
  377. Vector3 eyedir = -get_global_transform().basis.get_axis(2).normalized();
  378. return eyedir.dot(p_pos) < (eyedir.dot(t.origin)+near);
  379. }
  380. Point2 Camera::unproject_position(const Vector3& p_pos) const {
  381. if (!is_inside_tree()) {
  382. ERR_EXPLAIN("Camera is not inside scene.");
  383. ERR_FAIL_COND_V(!is_inside_tree(),Vector2());
  384. }
  385. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  386. CameraMatrix cm;
  387. if (mode==PROJECTION_ORTHOGONAL)
  388. cm.set_orthogonal(size,viewport_size.get_aspect(),near,far,keep_aspect==KEEP_WIDTH);
  389. else
  390. cm.set_perspective(fov,viewport_size.get_aspect(),near,far,keep_aspect==KEEP_WIDTH);
  391. Plane p(get_camera_transform().xform_inv(p_pos),1.0);
  392. p=cm.xform4(p);
  393. p.normal/=p.d;
  394. Point2 res;
  395. res.x = (p.normal.x * 0.5 + 0.5) * viewport_size.x;
  396. res.y = (-p.normal.y * 0.5 + 0.5) * viewport_size.y;
  397. return res;
  398. }
  399. Vector3 Camera::project_position(const Point2& p_point) const {
  400. if (!is_inside_tree()) {
  401. ERR_EXPLAIN("Camera is not inside scene.");
  402. ERR_FAIL_COND_V(!is_inside_tree(),Vector3());
  403. }
  404. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  405. CameraMatrix cm;
  406. if (mode==PROJECTION_ORTHOGONAL)
  407. cm.set_orthogonal(size,viewport_size.get_aspect(),near,far,keep_aspect==KEEP_WIDTH);
  408. else
  409. cm.set_perspective(fov,viewport_size.get_aspect(),near,far,keep_aspect==KEEP_WIDTH);
  410. Size2 vp_size;
  411. cm.get_viewport_size(vp_size.x,vp_size.y);
  412. Vector2 point;
  413. point.x = (p_point.x/viewport_size.x) * 2.0 - 1.0;
  414. point.y = (1.0-(p_point.y/viewport_size.y)) * 2.0 - 1.0;
  415. point*=vp_size;
  416. Vector3 p(point.x,point.y,-near);
  417. return get_camera_transform().xform(p);
  418. }
  419. /*
  420. void Camera::_camera_make_current(Node *p_camera) {
  421. if (p_camera==this) {
  422. VisualServer::get_singleton()->viewport_attach_camera(viewport_id,camera);
  423. active=true;
  424. } else {
  425. if (active && p_camera==NULL) {
  426. //detech camera because no one else will claim it
  427. VisualServer::get_singleton()->viewport_attach_camera(viewport_id,RID());
  428. }
  429. active=false;
  430. }
  431. }
  432. */
  433. void Camera::set_environment(const Ref<Environment>& p_environment) {
  434. environment=p_environment;
  435. if (environment.is_valid())
  436. VS::get_singleton()->camera_set_environment(camera,environment->get_rid());
  437. else
  438. VS::get_singleton()->camera_set_environment(camera,RID());
  439. }
  440. Ref<Environment> Camera::get_environment() const {
  441. return environment;
  442. }
  443. void Camera::set_keep_aspect_mode(KeepAspect p_aspect) {
  444. keep_aspect=p_aspect;
  445. VisualServer::get_singleton()->camera_set_use_vertical_aspect(camera,p_aspect==KEEP_WIDTH);
  446. _change_notify();
  447. }
  448. Camera::KeepAspect Camera::get_keep_aspect_mode() const{
  449. return keep_aspect;
  450. }
  451. void Camera::_bind_methods() {
  452. ObjectTypeDB::bind_method( _MD("project_ray_normal","screen_point"), &Camera::project_ray_normal);
  453. ObjectTypeDB::bind_method( _MD("project_local_ray_normal","screen_point"), &Camera::project_local_ray_normal);
  454. ObjectTypeDB::bind_method( _MD("project_ray_origin","screen_point"), &Camera::project_ray_origin);
  455. ObjectTypeDB::bind_method( _MD("unproject_position","world_point"), &Camera::unproject_position);
  456. ObjectTypeDB::bind_method( _MD("is_position_behind","world_point"), &Camera::is_position_behind);
  457. ObjectTypeDB::bind_method( _MD("project_position","screen_point"), &Camera::project_position);
  458. ObjectTypeDB::bind_method( _MD("set_perspective","fov","z_near","z_far"),&Camera::set_perspective );
  459. ObjectTypeDB::bind_method( _MD("set_orthogonal","size","z_near","z_far"),&Camera::set_orthogonal );
  460. ObjectTypeDB::bind_method( _MD("make_current"),&Camera::make_current );
  461. ObjectTypeDB::bind_method( _MD("clear_current"),&Camera::clear_current );
  462. ObjectTypeDB::bind_method( _MD("is_current"),&Camera::is_current );
  463. ObjectTypeDB::bind_method( _MD("get_camera_transform"),&Camera::get_camera_transform );
  464. ObjectTypeDB::bind_method( _MD("get_fov"),&Camera::get_fov );
  465. ObjectTypeDB::bind_method( _MD("get_size"),&Camera::get_size );
  466. ObjectTypeDB::bind_method( _MD("get_zfar"),&Camera::get_zfar );
  467. ObjectTypeDB::bind_method( _MD("get_znear"),&Camera::get_znear );
  468. ObjectTypeDB::bind_method( _MD("get_projection"),&Camera::get_projection );
  469. ObjectTypeDB::bind_method( _MD("set_visible_layers","mask"),&Camera::set_visible_layers );
  470. ObjectTypeDB::bind_method( _MD("get_visible_layers"),&Camera::get_visible_layers );
  471. ObjectTypeDB::bind_method(_MD("set_environment","env:Environment"),&Camera::set_environment);
  472. ObjectTypeDB::bind_method(_MD("get_environment:Environment"),&Camera::get_environment);
  473. ObjectTypeDB::bind_method(_MD("set_keep_aspect_mode","mode"),&Camera::set_keep_aspect_mode);
  474. ObjectTypeDB::bind_method(_MD("get_keep_aspect_mode"),&Camera::get_keep_aspect_mode);
  475. //ObjectTypeDB::bind_method( _MD("_camera_make_current"),&Camera::_camera_make_current );
  476. BIND_CONSTANT( PROJECTION_PERSPECTIVE );
  477. BIND_CONSTANT( PROJECTION_ORTHOGONAL );
  478. BIND_CONSTANT( KEEP_WIDTH );
  479. BIND_CONSTANT( KEEP_HEIGHT );
  480. }
  481. float Camera::get_fov() const {
  482. return fov;
  483. }
  484. float Camera::get_size() const {
  485. return size;
  486. }
  487. float Camera::get_znear() const {
  488. return near;
  489. }
  490. float Camera::get_zfar() const {
  491. return far;
  492. }
  493. Camera::Projection Camera::get_projection() const {
  494. return mode;
  495. }
  496. void Camera::set_visible_layers(uint32_t p_layers) {
  497. layers=p_layers;
  498. VisualServer::get_singleton()->camera_set_visible_layers(camera,layers);
  499. }
  500. uint32_t Camera::get_visible_layers() const{
  501. return layers;
  502. }
  503. Vector<Plane> Camera::get_frustum() const {
  504. ERR_FAIL_COND_V(!is_inside_world(),Vector<Plane>());
  505. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  506. CameraMatrix cm;
  507. if (mode==PROJECTION_PERSPECTIVE)
  508. cm.set_perspective(fov,viewport_size.get_aspect(),near,far,keep_aspect==KEEP_WIDTH);
  509. else
  510. cm.set_orthogonal(size,viewport_size.get_aspect(),near,far,keep_aspect==KEEP_WIDTH);
  511. return cm.get_projection_planes(get_camera_transform());
  512. }
  513. void Camera::set_v_offset(float p_offset) {
  514. v_offset=p_offset;
  515. _update_camera();;
  516. }
  517. float Camera::get_v_offset() const {
  518. return v_offset;
  519. }
  520. void Camera::set_h_offset(float p_offset) {
  521. h_offset=p_offset;
  522. _update_camera();
  523. }
  524. float Camera::get_h_offset() const {
  525. return h_offset;
  526. }
  527. Camera::Camera() {
  528. camera = VisualServer::get_singleton()->camera_create();
  529. size=1;
  530. fov=0;
  531. near=0;
  532. far=0;
  533. current=false;
  534. force_change=false;
  535. mode=PROJECTION_PERSPECTIVE;
  536. set_perspective(60.0,0.1,100.0);
  537. keep_aspect=KEEP_HEIGHT;
  538. layers=0xfffff;
  539. v_offset=0;
  540. h_offset=0;
  541. VisualServer::get_singleton()->camera_set_visible_layers(camera,layers);
  542. //active=false;
  543. }
  544. Camera::~Camera() {
  545. VisualServer::get_singleton()->free(camera);
  546. }