void RasterizerGLES3::set_current_render_target(RID p_render_target) { if (!p_render_target.is_valid() && storage->frame.current_rt && storage->frame.clear_request) { //handle pending clear request, if the framebuffer was not cleared glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo); print_line("unbind clear of: " + storage->frame.clear_request_color); glClearColor( storage->frame.clear_request_color.r, storage->frame.clear_request_color.g, storage->frame.clear_request_color.b, storage->frame.clear_request_color.a); glClear(GL_COLOR_BUFFER_BIT); } if (p_render_target.is_valid()) { RasterizerStorageGLES3::RenderTarget *rt = storage->render_target_owner.getornull(p_render_target); if (!rt) { storage->frame.current_rt = NULL; } ERR_FAIL_COND(!rt); storage->frame.current_rt = rt; storage->frame.clear_request = false; glViewport(0, 0, rt->width, rt->height); } else { storage->frame.current_rt = NULL; storage->frame.clear_request = false; glViewport(0, 0, OS::get_singleton()->get_window_size().width, OS::get_singleton()->get_window_size().height); glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo); } }
void MobileVRInterface::commit_for_eye(ARVRInterface::Eyes p_eye, RID p_render_target, const Rect2 &p_screen_rect) { _THREAD_SAFE_METHOD_ // We must have a valid render target ERR_FAIL_COND(!p_render_target.is_valid()); // Because we are rendering to our device we must use our main viewport! ERR_FAIL_COND(p_screen_rect == Rect2()); Rect2 dest = p_screen_rect; Vector2 eye_center; // we output half a screen dest.size.x *= 0.5; if (p_eye == ARVRInterface::EYE_LEFT) { eye_center.x = ((-intraocular_dist / 2.0) + (display_width / 4.0)) / (display_width / 2.0); } else if (p_eye == ARVRInterface::EYE_RIGHT) { dest.position.x = dest.size.x; eye_center.x = ((intraocular_dist / 2.0) - (display_width / 4.0)) / (display_width / 2.0); } // we don't offset the eye center vertically (yet) eye_center.y = 0.0; // unset our render target so we are outputting to our main screen by making RasterizerStorageGLES3::system_fbo our current FBO VSG::rasterizer->set_current_render_target(RID()); // and output VSG::rasterizer->output_lens_distorted_to_screen(p_render_target, dest, k1, k2, eye_center, oversample); };
void VisualServerCanvas::canvas_item_add_polygon(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, RID p_texture) { Item *canvas_item = canvas_item_owner.getornull(p_item); ERR_FAIL_COND(!canvas_item); #ifdef DEBUG_ENABLED int pointcount = p_points.size(); ERR_FAIL_COND(pointcount < 3); int color_size = p_colors.size(); int uv_size = p_uvs.size(); ERR_FAIL_COND(color_size != 0 && color_size != 1 && color_size != pointcount); ERR_FAIL_COND(uv_size != 0 && (uv_size != pointcount || !p_texture.is_valid())); #endif Vector<int> indices = Geometry::triangulate_polygon(p_points); if (indices.empty()) { ERR_EXPLAIN("Bad Polygon!"); ERR_FAIL_V(); } Item::CommandPolygon *polygon = memnew(Item::CommandPolygon); ERR_FAIL_COND(!polygon); polygon->texture = p_texture; polygon->points = p_points; polygon->uvs = p_uvs; polygon->colors = p_colors; polygon->indices = indices; polygon->count = indices.size(); canvas_item->rect_dirty = true; canvas_item->commands.push_back(polygon); }
void _ray_query_callback(const RID &p_rid, ObjectID p_id, int p_shape, const Vector2 &p_point, const Vector2 &p_normal) { Vector2 ray_end; if (p_rid.is_valid()) { ray_end = p_point; } else { ray_end = ray_to; } VisualServer *vs = VisualServer::get_singleton(); vs->canvas_item_clear(ray); vs->canvas_item_add_line(ray, ray_from, ray_end, p_rid.is_valid() ? Color(0, 1, 0.4) : Color(1, 0.4, 0), 2); if (p_rid.is_valid()) vs->canvas_item_add_line(ray, ray_end, ray_end + p_normal * 20, p_rid.is_valid() ? Color(0, 1, 0.4) : Color(1, 0.4, 0), 2); }
void BulletPhysicsServer::area_set_space(RID p_area, RID p_space) { AreaBullet *area = area_owner.get(p_area); ERR_FAIL_COND(!area); SpaceBullet *space = NULL; if (p_space.is_valid()) { space = space_owner.get(p_space); ERR_FAIL_COND(!space); } area->set_space(space); }
RID VisualServer::texture_create_from_image(const Image& p_image,uint32_t p_flags) { RID texture = texture_create(); texture_allocate(texture,p_image.get_width(), p_image.get_height(), p_image.get_format(), p_flags); //if it has mipmaps, use, else generate ERR_FAIL_COND_V(!texture.is_valid(),texture); texture_set_data(texture, p_image ); return texture; }
void Physics2DServerSW::area_set_space(RID p_area, RID p_space) { Area2DSW *area = area_owner.get(p_area); ERR_FAIL_COND(!area); Space2DSW *space = NULL; if (p_space.is_valid()) { space = space_owner.get(p_space); ERR_FAIL_COND(!space); } area->set_space(space); };
void Physics2DServerSW::body_set_space(RID p_body, RID p_space) { Body2DSW *body = body_owner.get(p_body); ERR_FAIL_COND(!body); Space2DSW *space = NULL; if (p_space.is_valid()) { space = space_owner.get(p_space); ERR_FAIL_COND(!space); } body->set_space(space); };
void BulletPhysicsServer::soft_body_set_space(RID p_body, RID p_space) { SoftBodyBullet *body = soft_body_owner.get(p_body); ERR_FAIL_COND(!body); SpaceBullet *space = NULL; if (p_space.is_valid()) { space = space_owner.get(p_space); ERR_FAIL_COND(!space); } if (body->get_space() == space) return; //pointles body->set_space(space); }
void Physics2DServerSW::body_set_space(RID p_body, RID p_space) { Body2DSW *body = body_owner.get(p_body); ERR_FAIL_COND(!body); Space2DSW *space = NULL; if (p_space.is_valid()) { space = space_owner.get(p_space); ERR_FAIL_COND(!space); } if (body->get_space() == space) return; //pointless body->clear_constraint_map(); body->set_space(space); };
void MobileVRInterface::commit_for_eye(ARVRInterface::Eyes p_eye, RID p_render_target, const Rect2 &p_screen_rect) { _THREAD_SAFE_METHOD_ // We must have a valid render target ERR_FAIL_COND(!p_render_target.is_valid()); // We must have an initialised shader ERR_FAIL_COND(lens_shader != NULL); // Because we are rendering to our device we must use our main viewport! ERR_FAIL_COND(p_screen_rect == Rect2()); float offset_x = 0.0; float aspect_ratio = 0.5 * p_screen_rect.size.x / p_screen_rect.size.y; Vector2 eye_center; if (p_eye == ARVRInterface::EYE_LEFT) { offset_x = -1.0; eye_center.x = ((-intraocular_dist / 2.0) + (display_width / 4.0)) / (display_width / 2.0); } else if (p_eye == ARVRInterface::EYE_RIGHT) { eye_center.x = ((intraocular_dist / 2.0) - (display_width / 4.0)) / (display_width / 2.0); } // unset our render target so we are outputting to our main screen by making RasterizerStorageGLES3::system_fbo our current FBO VSG::rasterizer->set_current_render_target(RID()); // now output to screen // VSG::rasterizer->blit_render_target_to_screen(p_render_target, screen_rect, 0); // get our render target RID eye_texture = VSG::storage->render_target_get_texture(p_render_target); uint32_t texid = VS::get_singleton()->texture_get_texid(eye_texture); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texid); lens_shader->bind(); lens_shader->set_uniform(LensDistortedShaderGLES3::OFFSET_X, offset_x); lens_shader->set_uniform(LensDistortedShaderGLES3::K1, k1); lens_shader->set_uniform(LensDistortedShaderGLES3::K2, k2); lens_shader->set_uniform(LensDistortedShaderGLES3::EYE_CENTER, eye_center); lens_shader->set_uniform(LensDistortedShaderGLES3::UPSCALE, oversample); lens_shader->set_uniform(LensDistortedShaderGLES3::ASPECT_RATIO, aspect_ratio); glBindVertexArray(half_screen_array); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); glBindVertexArray(0); };
void PhysicsServerSW::body_set_space(RID p_body, RID p_space) { BodySW *body = body_owner.get(p_body); ERR_FAIL_COND(!body); SpaceSW *space=NULL; if (p_space.is_valid()) { space = space_owner.get(p_space); ERR_FAIL_COND(!space); } if (body->get_space()==space) return; //pointles body->set_space(space); };
RID BulletPhysicsServer::joint_create_cone_twist(RID p_body_A, const Transform &p_local_frame_A, RID p_body_B, const Transform &p_local_frame_B) { RigidBodyBullet *body_A = rigid_body_owner.get(p_body_A); ERR_FAIL_COND_V(!body_A, RID()); JointAssertSpace(body_A, "A", RID()); RigidBodyBullet *body_B = NULL; if (p_body_B.is_valid()) { body_B = rigid_body_owner.get(p_body_B); JointAssertSpace(body_B, "B", RID()); JointAssertSameSpace(body_A, body_B, RID()); } JointBullet *joint = bulletnew(ConeTwistJointBullet(body_A, body_B, p_local_frame_A, p_local_frame_B)); AddJointToSpace(body_A, joint, true); CreateThenReturnRID(joint_owner, joint); }
void BakedLightmap::_clear_lightmaps() { ERR_FAIL_COND(!light_data.is_valid()); for (int i = 0; i < light_data->get_user_count(); i++) { Node *node = get_node(light_data->get_user_path(i)); int instance_idx = light_data->get_user_instance(i); if (instance_idx >= 0) { RID instance = node->call("get_bake_mesh_instance", instance_idx); if (instance.is_valid()) { VS::get_singleton()->instance_set_use_lightmap(instance, get_instance(), RID()); } } else { VisualInstance *vi = Object::cast_to<VisualInstance>(node); ERR_CONTINUE(!vi); VS::get_singleton()->instance_set_use_lightmap(vi->get_instance(), get_instance(), RID()); } } }
void PhysicsServerSW::area_set_space(RID p_area, RID p_space) { AreaSW *area = area_owner.get(p_area); ERR_FAIL_COND(!area); SpaceSW *space = NULL; if (p_space.is_valid()) { space = space_owner.get(p_space); ERR_FAIL_COND(!space); } if (area->get_space() == space) return; //pointless area->clear_constraints(); area->set_space(space); };
void SoftBodyVisualServerHandler::prepare(RID p_mesh, int p_surface) { clear(); ERR_FAIL_COND(!p_mesh.is_valid()); mesh = p_mesh; surface = p_surface; const uint32_t surface_format = VS::get_singleton()->mesh_surface_get_format(mesh, surface); const int surface_vertex_len = VS::get_singleton()->mesh_surface_get_array_len(mesh, p_surface); const int surface_index_len = VS::get_singleton()->mesh_surface_get_array_index_len(mesh, p_surface); uint32_t surface_offsets[VS::ARRAY_MAX]; buffer = VS::get_singleton()->mesh_surface_get_array(mesh, surface); stride = VS::get_singleton()->mesh_surface_make_offsets_from_format(surface_format, surface_vertex_len, surface_index_len, surface_offsets); offset_vertices = surface_offsets[VS::ARRAY_VERTEX]; offset_normal = surface_offsets[VS::ARRAY_NORMAL]; }
void VisualServerCanvas::canvas_item_set_parent(RID p_item, RID p_parent) { Item *canvas_item = canvas_item_owner.getornull(p_item); ERR_FAIL_COND(!canvas_item); if (canvas_item->parent.is_valid()) { if (canvas_owner.owns(canvas_item->parent)) { Canvas *canvas = canvas_owner.get(canvas_item->parent); canvas->erase_item(canvas_item); } else if (canvas_item_owner.owns(canvas_item->parent)) { Item *item_owner = canvas_item_owner.get(canvas_item->parent); item_owner->child_items.erase(canvas_item); } canvas_item->parent = RID(); } if (p_parent.is_valid()) { if (canvas_owner.owns(p_parent)) { Canvas *canvas = canvas_owner.get(p_parent); Canvas::ChildItem ci; ci.item = canvas_item; canvas->child_items.push_back(ci); canvas->children_order_dirty = true; } else if (canvas_item_owner.owns(p_parent)) { Item *item_owner = canvas_item_owner.get(p_parent); item_owner->child_items.push_back(canvas_item); item_owner->children_order_dirty = true; } else { ERR_EXPLAIN("Invalid parent"); ERR_FAIL(); } } canvas_item->parent = p_parent; }
RID BulletPhysicsServer::joint_create_hinge_simple(RID p_body_A, const Vector3 &p_pivot_A, const Vector3 &p_axis_A, RID p_body_B, const Vector3 &p_pivot_B, const Vector3 &p_axis_B) { RigidBodyBullet *body_A = rigid_body_owner.get(p_body_A); ERR_FAIL_COND_V(!body_A, RID()); JointAssertSpace(body_A, "A", RID()); RigidBodyBullet *body_B = NULL; if (p_body_B.is_valid()) { body_B = rigid_body_owner.get(p_body_B); JointAssertSpace(body_B, "B", RID()); JointAssertSameSpace(body_A, body_B, RID()); } ERR_FAIL_COND_V(body_A == body_B, RID()); JointBullet *joint = bulletnew(HingeJointBullet(body_A, body_B, p_pivot_A, p_pivot_B, p_axis_A, p_axis_B)); AddJointToSpace(body_A, joint, true); CreateThenReturnRID(joint_owner, joint); }
RID PhysicsServerSW::joint_create_pin(RID p_body_A,const Vector3& p_local_A,RID p_body_B,const Vector3& p_local_B) { BodySW *body_A = body_owner.get(p_body_A); ERR_FAIL_COND_V(!body_A,RID()); if (!p_body_B.is_valid()) { ERR_FAIL_COND_V(!body_A->get_space(),RID()); p_body_B=body_A->get_space()->get_static_global_body(); } BodySW *body_B = body_owner.get(p_body_B); ERR_FAIL_COND_V(!body_B,RID()); ERR_FAIL_COND_V(body_A==body_B,RID()); JointSW *joint = memnew( PinJointSW(body_A,p_local_A,body_B,p_local_B) ); RID rid = joint_owner.make_rid(joint); joint->set_self(rid); return rid; }
RID PhysicsServerSW::joint_create_generic_6dof(RID p_body_A,const Transform& p_local_frame_A,RID p_body_B,const Transform& p_local_frame_B) { BodySW *body_A = body_owner.get(p_body_A); ERR_FAIL_COND_V(!body_A,RID()); if (!p_body_B.is_valid()) { ERR_FAIL_COND_V(!body_A->get_space(),RID()); p_body_B=body_A->get_space()->get_static_global_body(); } BodySW *body_B = body_owner.get(p_body_B); ERR_FAIL_COND_V(!body_B,RID()); ERR_FAIL_COND_V(body_A==body_B,RID()); JointSW *joint = memnew( Generic6DOFJointSW(body_A,body_B,p_local_frame_A,p_local_frame_B,true) ); RID rid = joint_owner.make_rid(joint); joint->set_self(rid); return rid; }
void PhysicsServerSW::free(RID p_rid) { if (shape_owner.owns(p_rid)) { ShapeSW *shape = shape_owner.get(p_rid); while(shape->get_owners().size()) { ShapeOwnerSW *so=shape->get_owners().front()->key(); so->remove_shape(shape); } shape_owner.free(p_rid); memdelete(shape); } else if (body_owner.owns(p_rid)) { BodySW *body = body_owner.get(p_rid); // if (body->get_state_query()) // _clear_query(body->get_state_query()); // if (body->get_direct_state_query()) // _clear_query(body->get_direct_state_query()); body->set_space(NULL); while( body->get_shape_count() ) { body->remove_shape(0); } while (body->get_constraint_map().size()) { RID self = body->get_constraint_map().front()->key()->get_self(); ERR_FAIL_COND(!self.is_valid()); free(self); } body_owner.free(p_rid); memdelete(body); } else if (area_owner.owns(p_rid)) { AreaSW *area = area_owner.get(p_rid); // if (area->get_monitor_query()) // _clear_query(area->get_monitor_query()); area->set_space(NULL); while( area->get_shape_count() ) { area->remove_shape(0); } area_owner.free(p_rid); memdelete(area); } else if (space_owner.owns(p_rid)) { SpaceSW *space = space_owner.get(p_rid); while(space->get_objects().size()) { CollisionObjectSW *co = (CollisionObjectSW *)space->get_objects().front()->get(); co->set_space(NULL); } active_spaces.erase(space); free(space->get_default_area()->get_self()); free(space->get_static_global_body()); space_owner.free(p_rid); memdelete(space); } else if (joint_owner.owns(p_rid)) { JointSW *joint = joint_owner.get(p_rid); for(int i=0;i<joint->get_body_count();i++) { joint->get_body_ptr()[i]->remove_constraint(joint); } joint_owner.free(p_rid); memdelete(joint); } else { ERR_EXPLAIN("Invalid ID"); ERR_FAIL(); } };
void Particles2D::_notification(int p_what) { switch(p_what) { case NOTIFICATION_PROCESS: { _process_particles( get_process_delta_time() ); } break; case NOTIFICATION_ENTER_TREE: { float ppt=preprocess; while(ppt>0) { _process_particles(0.1); ppt-=0.1; } } break; case NOTIFICATION_DRAW: { if (particles.size()==0 || lifetime==0) return; RID ci=get_canvas_item(); Size2 size(1,1); Point2 center; int total_frames=1; if (!texture.is_null()) { size=texture->get_size(); size.x/=h_frames; size.y/=v_frames; total_frames=h_frames*v_frames; } float time_pos=(time/lifetime); Particle *pdata=&particles[0]; int particle_count=particles.size(); RID texrid; if (texture.is_valid()) texrid = texture->get_rid(); Matrix32 invxform; if (!local_space) invxform=get_global_transform().affine_inverse(); int start_particle = (int)(time * (float)particle_count / lifetime); for (int id=0;id<particle_count;++id) { int i = start_particle + id; if (i >= particle_count) { i -= particle_count; } Particle &p=pdata[i]; if (!p.active) continue; float ptime = ((float)i / particle_count)*explosiveness; if (ptime<time_pos) ptime=time_pos-ptime; else ptime=(1.0-ptime)+time_pos; uint32_t rand_seed=p.seed*(i+1); Color color; if(color_ramp.is_valid()) { color = color_ramp->get_color_at_offset(ptime); } else { color = default_color; } { float huerand=_rand_from_seed(&rand_seed); float huerot = param[PARAM_HUE_VARIATION] + randomness[PARAM_HUE_VARIATION] * huerand; if (Math::abs(huerot) > CMP_EPSILON) { float h=color.get_h(); float s=color.get_s(); float v=color.get_v(); float a=color.a; //float preh=h; h+=huerot; h=Math::abs(Math::fposmod(h,1.0)); //print_line("rand: "+rtos(randomness[PARAM_HUE_VARIATION])+" rand: "+rtos(huerand)); //print_line(itos(i)+":hue: "+rtos(preh)+" + "+rtos(huerot)+" = "+rtos(h)); color.set_hsv(h,s,v); color.a=a; } } float initial_size = param[PARAM_INITIAL_SIZE]+param[PARAM_INITIAL_SIZE]*_rand_from_seed(&rand_seed)*randomness[PARAM_FINAL_SIZE]; float final_size = param[PARAM_FINAL_SIZE]+param[PARAM_FINAL_SIZE]*_rand_from_seed(&rand_seed)*randomness[PARAM_FINAL_SIZE]; float size_mult=initial_size*(1.0-ptime) + final_size*ptime; //Size2 rectsize=size * size_mult; //rectsize=rectsize.floor(); //Rect2 r = Rect2(Vecto,rectsize); Matrix32 xform; if (p.rot) { xform.set_rotation(p.rot); xform.translate(-size*size_mult/2.0); xform.elements[2]+=p.pos; } else { xform.elements[2]=-size*size_mult/2.0; xform.elements[2]+=p.pos; } if (!local_space) { xform = invxform * xform; } xform.scale_basis(Size2(size_mult,size_mult)); VisualServer::get_singleton()->canvas_item_add_set_transform(ci,xform); if (texrid.is_valid()) { Rect2 src_rect; src_rect.size=size; if (total_frames>1) { int frame = Math::fast_ftoi(Math::floor(p.frame*total_frames)) % total_frames; src_rect.pos.x = size.x * (frame%h_frames); src_rect.pos.y = size.y * (frame/h_frames); } texture->draw_rect_region(ci,Rect2(Point2(),size),src_rect,color); //VisualServer::get_singleton()->canvas_item_add_texture_rect(ci,r,texrid,false,color); } else { VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2(),size),color); } } } break; } }