visual_server_viewport.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. #include "visual_server_viewport.h"
  2. #include "visual_server_global.h"
  3. #include "visual_server_canvas.h"
  4. #include "globals.h"
  5. void VisualServerViewport::_draw_viewport(Viewport *p_viewport) {
  6. /* Camera should always be BEFORE any other 3D */
  7. #if 0
  8. bool scenario_draw_canvas_bg=false;
  9. int scenario_canvas_max_layer=0;
  10. if (!p_viewport->hide_canvas && !p_viewport->disable_environment && scenario_owner.owns(p_viewport->scenario)) {
  11. Scenario *scenario=scenario_owner.get(p_viewport->scenario);
  12. if (scenario->environment.is_valid()) {
  13. if (rasterizer->is_environment(scenario->environment)) {
  14. scenario_draw_canvas_bg=rasterizer->environment_get_background(scenario->environment)==VS::ENV_BG_CANVAS;
  15. scenario_canvas_max_layer=rasterizer->environment_get_background_param(scenario->environment,VS::ENV_BG_PARAM_CANVAS_MAX_LAYER);
  16. }
  17. }
  18. }
  19. bool can_draw_3d=!p_viewport->hide_scenario && camera_owner.owns(p_viewport->camera) && scenario_owner.owns(p_viewport->scenario);
  20. if (scenario_draw_canvas_bg) {
  21. rasterizer->begin_canvas_bg();
  22. }
  23. if (!scenario_draw_canvas_bg && can_draw_3d) {
  24. _draw_viewport_camera(p_viewport,false);
  25. } else if (true /*|| !p_viewport->canvas_list.empty()*/){
  26. //clear the viewport black because of no camera? i seriously should..
  27. if (p_viewport->render_target_clear_on_new_frame || p_viewport->render_target_clear) {
  28. if (p_viewport->transparent_bg) {
  29. rasterizer->clear_viewport(Color(0,0,0,0));
  30. }
  31. else {
  32. Color cc=clear_color;
  33. if (scenario_draw_canvas_bg)
  34. cc.a=0;
  35. rasterizer->clear_viewport(cc);
  36. }
  37. p_viewport->render_target_clear=false;
  38. }
  39. }
  40. #endif
  41. if (p_viewport->clear_mode!=VS::VIEWPORT_CLEAR_NEVER) {
  42. VSG::rasterizer->clear_render_target(clear_color);
  43. if (p_viewport->clear_mode==VS::VIEWPORT_CLEAR_ONLY_NEXT_FRAME) {
  44. p_viewport->clear_mode=VS::VIEWPORT_CLEAR_NEVER;
  45. }
  46. }
  47. if (!p_viewport->hide_canvas) {
  48. int i=0;
  49. Map<Viewport::CanvasKey,Viewport::CanvasData*> canvas_map;
  50. Rect2 clip_rect(0,0,p_viewport->size.x,p_viewport->size.y);
  51. RasterizerCanvas::Light *lights=NULL;
  52. RasterizerCanvas::Light *lights_with_shadow=NULL;
  53. RasterizerCanvas::Light *lights_with_mask=NULL;
  54. Rect2 shadow_rect;
  55. int light_count=0;
  56. for (Map<RID,Viewport::CanvasData>::Element *E=p_viewport->canvas_map.front();E;E=E->next()) {
  57. Matrix32 xf = p_viewport->global_transform * E->get().transform;
  58. VisualServerCanvas::Canvas *canvas = static_cast<VisualServerCanvas::Canvas*>(E->get().canvas);
  59. //find lights in canvas
  60. for(Set<RasterizerCanvas::Light*>::Element *F=canvas->lights.front();F;F=F->next()) {
  61. RasterizerCanvas::Light* cl=F->get();
  62. if (cl->enabled && cl->texture.is_valid()) {
  63. //not super efficient..
  64. Size2 tsize(VSG::storage->texture_get_width(cl->texture),VSG::storage->texture_get_height(cl->texture));
  65. tsize*=cl->scale;
  66. Vector2 offset=tsize/2.0;
  67. cl->rect_cache=Rect2(-offset+cl->texture_offset,tsize);
  68. cl->xform_cache=xf * cl->xform;
  69. if (clip_rect.intersects_transformed(cl->xform_cache,cl->rect_cache)) {
  70. cl->filter_next_ptr=lights;
  71. lights=cl;
  72. cl->texture_cache=NULL;
  73. Matrix32 scale;
  74. scale.scale(cl->rect_cache.size);
  75. scale.elements[2]=cl->rect_cache.pos;
  76. cl->light_shader_xform = (cl->xform_cache * scale).affine_inverse();
  77. cl->light_shader_pos=cl->xform_cache[2];
  78. if (cl->shadow_buffer.is_valid()) {
  79. cl->shadows_next_ptr=lights_with_shadow;
  80. if (lights_with_shadow==NULL) {
  81. shadow_rect = cl->xform_cache.xform(cl->rect_cache);
  82. } else {
  83. shadow_rect=shadow_rect.merge( cl->xform_cache.xform(cl->rect_cache) );
  84. }
  85. lights_with_shadow=cl;
  86. cl->radius_cache=cl->rect_cache.size.length();
  87. }
  88. if (cl->mode==VS::CANVAS_LIGHT_MODE_MASK) {
  89. cl->mask_next_ptr=lights_with_mask;
  90. lights_with_mask=cl;
  91. }
  92. light_count++;
  93. }
  94. VSG::canvas_render->light_internal_update(cl->light_internal,cl);
  95. }
  96. }
  97. //print_line("lights: "+itos(light_count));
  98. canvas_map[ Viewport::CanvasKey( E->key(), E->get().layer) ]=&E->get();
  99. }
  100. if (lights_with_shadow) {
  101. //update shadows if any
  102. RasterizerCanvas::LightOccluderInstance * occluders=NULL;
  103. //make list of occluders
  104. for (Map<RID,Viewport::CanvasData>::Element *E=p_viewport->canvas_map.front();E;E=E->next()) {
  105. VisualServerCanvas::Canvas *canvas = static_cast<VisualServerCanvas::Canvas*>(E->get().canvas);
  106. Matrix32 xf = p_viewport->global_transform * E->get().transform;
  107. for(Set<RasterizerCanvas::LightOccluderInstance*>::Element *F=canvas->occluders.front();F;F=F->next()) {
  108. if (!F->get()->enabled)
  109. continue;
  110. F->get()->xform_cache = xf * F->get()->xform;
  111. if (shadow_rect.intersects_transformed(F->get()->xform_cache,F->get()->aabb_cache)) {
  112. F->get()->next=occluders;
  113. occluders=F->get();
  114. }
  115. }
  116. }
  117. //update the light shadowmaps with them
  118. RasterizerCanvas::Light *light=lights_with_shadow;
  119. while(light) {
  120. VSG::canvas_render->canvas_light_shadow_buffer_update(light->shadow_buffer,light->xform_cache.affine_inverse(),light->item_mask,light->radius_cache/1000.0,light->radius_cache*1.1,occluders,&light->shadow_matrix_cache);
  121. light=light->shadows_next_ptr;
  122. }
  123. // VSG::canvas_render->reset_canvas();
  124. }
  125. VSG::rasterizer->restore_render_target();
  126. #if 0
  127. if (scenario_draw_canvas_bg && canvas_map.front() && canvas_map.front()->key().layer>scenario_canvas_max_layer) {
  128. _draw_viewport_camera(p_viewport,!can_draw_3d);
  129. scenario_draw_canvas_bg=false;
  130. }
  131. #endif
  132. for (Map<Viewport::CanvasKey,Viewport::CanvasData*>::Element *E=canvas_map.front();E;E=E->next()) {
  133. VisualServerCanvas::Canvas *canvas = static_cast<VisualServerCanvas::Canvas*>(E->get()->canvas);
  134. // print_line("canvas "+itos(i)+" size: "+itos(I->get()->canvas->child_items.size()));
  135. //print_line("GT "+p_viewport->global_transform+". CT: "+E->get()->transform);
  136. Matrix32 xform = p_viewport->global_transform * E->get()->transform;
  137. RasterizerCanvas::Light *canvas_lights=NULL;
  138. RasterizerCanvas::Light *ptr=lights;
  139. while(ptr) {
  140. if (E->get()->layer>=ptr->layer_min && E->get()->layer<=ptr->layer_max) {
  141. ptr->next_ptr=canvas_lights;
  142. canvas_lights=ptr;
  143. }
  144. ptr=ptr->filter_next_ptr;
  145. }
  146. VSG::canvas->render_canvas( canvas,xform,canvas_lights,lights_with_mask,clip_rect );
  147. i++;
  148. #if 0
  149. if (scenario_draw_canvas_bg && E->key().layer>=scenario_canvas_max_layer) {
  150. _draw_viewport_camera(p_viewport,!can_draw_3d);
  151. scenario_draw_canvas_bg=false;
  152. }
  153. #endif
  154. }
  155. #if 0
  156. if (scenario_draw_canvas_bg) {
  157. _draw_viewport_camera(p_viewport,!can_draw_3d);
  158. scenario_draw_canvas_bg=false;
  159. }
  160. #endif
  161. //VSG::canvas_render->canvas_debug_viewport_shadows(lights_with_shadow);
  162. }
  163. }
  164. void VisualServerViewport::draw_viewports() {
  165. //sort viewports
  166. //draw viewports
  167. clear_color=GLOBAL_DEF("rendering/viewport/default_clear_color",Color(0.5,0.5,0.5));
  168. active_viewports.sort_custom<ViewportSort>();
  169. for(int i=0;i<active_viewports.size();i++) {
  170. Viewport *vp = active_viewports[i];
  171. if (vp->update_mode==VS::VIEWPORT_UPDATE_DISABLED)
  172. continue;
  173. ERR_CONTINUE( !vp->render_target.is_valid() );
  174. VSG::rasterizer->set_current_render_target(vp->render_target);
  175. _draw_viewport(vp);
  176. if (vp->viewport_to_screen_rect!=Rect2()) {
  177. //copy to screen if set as such
  178. VSG::rasterizer->set_current_render_target(RID());
  179. VSG::rasterizer->blit_render_target_to_screen(vp->render_target,vp->viewport_to_screen_rect,vp->viewport_to_screen);
  180. }
  181. if (vp->update_mode==VS::VIEWPORT_UPDATE_ONCE) {
  182. vp->update_mode=VS::VIEWPORT_UPDATE_DISABLED;
  183. }
  184. }
  185. }
  186. RID VisualServerViewport::viewport_create() {
  187. Viewport * viewport = memnew( Viewport );
  188. RID rid = viewport_owner.make_rid(viewport);
  189. viewport->self=rid;
  190. viewport->hide_scenario=false;
  191. viewport->hide_canvas=false;
  192. viewport->render_target=VSG::storage->render_target_create();
  193. return rid;
  194. }
  195. void VisualServerViewport::viewport_set_size(RID p_viewport,int p_width,int p_height){
  196. ERR_FAIL_COND(p_width<0 && p_height<0);
  197. Viewport * viewport = viewport_owner.getornull(p_viewport);
  198. ERR_FAIL_COND(!viewport);
  199. viewport->size=Size2(p_width,p_height);
  200. VSG::storage->render_target_set_size(viewport->render_target,p_width,p_height);
  201. }
  202. void VisualServerViewport::viewport_set_active(RID p_viewport,bool p_active) {
  203. Viewport * viewport = viewport_owner.getornull(p_viewport);
  204. ERR_FAIL_COND(!viewport);
  205. if (p_active) {
  206. ERR_FAIL_COND(active_viewports.find(viewport)!=-1);//already active
  207. active_viewports.push_back(viewport);
  208. } else {
  209. active_viewports.erase(viewport);
  210. }
  211. }
  212. void VisualServerViewport::viewport_set_parent_viewport(RID p_viewport,RID p_parent_viewport) {
  213. Viewport * viewport = viewport_owner.getornull(p_viewport);
  214. ERR_FAIL_COND(!viewport);
  215. viewport->parent=p_parent_viewport;
  216. }
  217. void VisualServerViewport::viewport_set_clear_mode(RID p_viewport,VS::ViewportClearMode p_clear_mode) {
  218. Viewport * viewport = viewport_owner.getornull(p_viewport);
  219. ERR_FAIL_COND(!viewport);
  220. viewport->clear_mode=p_clear_mode;
  221. }
  222. void VisualServerViewport::viewport_attach_to_screen(RID p_viewport,const Rect2& p_rect,int p_screen){
  223. Viewport * viewport = viewport_owner.getornull(p_viewport);
  224. ERR_FAIL_COND(!viewport);
  225. viewport->viewport_to_screen_rect=p_rect;
  226. viewport->viewport_to_screen=p_screen;
  227. }
  228. void VisualServerViewport::viewport_detach(RID p_viewport){
  229. Viewport * viewport = viewport_owner.getornull(p_viewport);
  230. ERR_FAIL_COND(!viewport);
  231. viewport->viewport_to_screen_rect=Rect2();
  232. viewport->viewport_to_screen=0;
  233. }
  234. void VisualServerViewport::viewport_set_update_mode(RID p_viewport,VS::ViewportUpdateMode p_mode){
  235. Viewport * viewport = viewport_owner.getornull(p_viewport);
  236. ERR_FAIL_COND(!viewport);
  237. viewport->update_mode=p_mode;
  238. }
  239. void VisualServerViewport::viewport_set_vflip(RID p_viewport,bool p_enable){
  240. Viewport * viewport = viewport_owner.getornull(p_viewport);
  241. ERR_FAIL_COND(!viewport);
  242. VSG::storage->render_target_set_flag(viewport->render_target,RasterizerStorage::RENDER_TARGET_VFLIP,p_enable);
  243. }
  244. RID VisualServerViewport::viewport_get_texture(RID p_viewport) const{
  245. const Viewport * viewport = viewport_owner.getornull(p_viewport);
  246. ERR_FAIL_COND_V(!viewport,RID());
  247. return VSG::storage->render_target_get_texture(viewport->render_target);
  248. }
  249. void VisualServerViewport::viewport_set_hide_scenario(RID p_viewport,bool p_hide){
  250. Viewport * viewport = viewport_owner.getornull(p_viewport);
  251. ERR_FAIL_COND(!viewport);
  252. viewport->hide_scenario=p_hide;
  253. }
  254. void VisualServerViewport::viewport_set_hide_canvas(RID p_viewport,bool p_hide){
  255. Viewport * viewport = viewport_owner.getornull(p_viewport);
  256. ERR_FAIL_COND(!viewport);
  257. viewport->hide_canvas=p_hide;
  258. }
  259. void VisualServerViewport::viewport_set_disable_environment(RID p_viewport,bool p_disable){
  260. Viewport * viewport = viewport_owner.getornull(p_viewport);
  261. ERR_FAIL_COND(!viewport);
  262. viewport->disable_environment=p_disable;
  263. }
  264. void VisualServerViewport::viewport_set_disable_3d(RID p_viewport,bool p_disable){
  265. Viewport * viewport = viewport_owner.getornull(p_viewport);
  266. ERR_FAIL_COND(!viewport);
  267. viewport->disable_3d=p_disable;
  268. VSG::storage->render_target_set_flag(viewport->render_target,RasterizerStorage::RENDER_TARGET_NO_3D,p_disable);
  269. }
  270. void VisualServerViewport::viewport_attach_camera(RID p_viewport,RID p_camera){
  271. Viewport * viewport = viewport_owner.getornull(p_viewport);
  272. ERR_FAIL_COND(!viewport);
  273. viewport->camera=p_camera;
  274. }
  275. void VisualServerViewport::viewport_set_scenario(RID p_viewport,RID p_scenario){
  276. Viewport * viewport = viewport_owner.getornull(p_viewport);
  277. ERR_FAIL_COND(!viewport);
  278. viewport->scenario=p_scenario;
  279. }
  280. void VisualServerViewport::viewport_attach_canvas(RID p_viewport,RID p_canvas){
  281. Viewport * viewport = viewport_owner.getornull(p_viewport);
  282. ERR_FAIL_COND(!viewport);
  283. ERR_FAIL_COND(viewport->canvas_map.has(p_canvas));
  284. VisualServerCanvas::Canvas *canvas = VSG::canvas->canvas_owner.getornull(p_canvas);
  285. ERR_FAIL_COND(!canvas);
  286. canvas->viewports.insert(p_viewport);
  287. viewport->canvas_map[p_canvas]=Viewport::CanvasData();
  288. viewport->canvas_map[p_canvas].layer=0;
  289. viewport->canvas_map[p_canvas].canvas=canvas;
  290. }
  291. void VisualServerViewport::viewport_remove_canvas(RID p_viewport,RID p_canvas){
  292. Viewport * viewport = viewport_owner.getornull(p_viewport);
  293. ERR_FAIL_COND(!viewport);
  294. VisualServerCanvas::Canvas *canvas = VSG::canvas->canvas_owner.getornull(p_canvas);
  295. ERR_FAIL_COND(!canvas);
  296. viewport->canvas_map.erase(p_canvas);
  297. canvas->viewports.erase(p_viewport);
  298. }
  299. void VisualServerViewport::viewport_set_canvas_transform(RID p_viewport,RID p_canvas,const Matrix32& p_offset){
  300. Viewport * viewport = viewport_owner.getornull(p_viewport);
  301. ERR_FAIL_COND(!viewport);
  302. ERR_FAIL_COND(!viewport->canvas_map.has(p_canvas));
  303. viewport->canvas_map[p_canvas].transform=p_offset;
  304. }
  305. void VisualServerViewport::viewport_set_transparent_background(RID p_viewport,bool p_enabled){
  306. Viewport * viewport = viewport_owner.getornull(p_viewport);
  307. ERR_FAIL_COND(!viewport);
  308. VSG::storage->render_target_set_flag(viewport->render_target,RasterizerStorage::RENDER_TARGET_TRANSPARENT,p_enabled);
  309. }
  310. void VisualServerViewport::viewport_set_global_canvas_transform(RID p_viewport,const Matrix32& p_transform){
  311. Viewport * viewport = viewport_owner.getornull(p_viewport);
  312. ERR_FAIL_COND(!viewport);
  313. viewport->global_transform=p_transform;
  314. }
  315. void VisualServerViewport::viewport_set_canvas_layer(RID p_viewport,RID p_canvas,int p_layer){
  316. Viewport * viewport = viewport_owner.getornull(p_viewport);
  317. ERR_FAIL_COND(!viewport);
  318. ERR_FAIL_COND(!viewport->canvas_map.has(p_canvas));
  319. viewport->canvas_map[p_canvas].layer=p_layer;
  320. }
  321. bool VisualServerViewport::free(RID p_rid) {
  322. Viewport * viewport = viewport_owner.getornull(p_rid);
  323. if (!viewport)
  324. return false;
  325. VSG::storage->free( viewport->render_target );
  326. while(viewport->canvas_map.front()) {
  327. viewport_remove_canvas(p_rid,viewport->canvas_map.front()->key());
  328. }
  329. viewport_set_scenario(p_rid,RID());
  330. active_viewports.erase(viewport);
  331. viewport_owner.free(p_rid);
  332. memdelete(viewport);
  333. return true;
  334. }
  335. VisualServerViewport::VisualServerViewport()
  336. {
  337. }