Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 #ifndef RVE_RENDER_SERVER_RENDER_TARGET_H
00031 #define RVE_RENDER_SERVER_RENDER_TARGET_H
00032 
00033 #include <rve_common/uuid.h>
00034 
00035 #include <OGRE/OgreTexture.h>
00036 #include <OGRE/OgreMaterial.h>
00037 
00038 #include <rve_msgs/RenderTargetFrameStats.h>
00039 
00040 #include <boost/shared_array.hpp>
00041 #include <boost/function.hpp>
00042 
00043 namespace Ogre
00044 {
00045 class RenderTarget;
00046 class RenderTexture;
00047 class RenderTarget;
00048 class MultiRenderTarget;
00049 class Rectangle2D;
00050 class SceneNode;
00051 class PlaneBoundedVolumeListSceneQuery;
00052 }
00053 
00054 namespace rve_render_server
00055 {
00056 
00057 class Camera;
00058 class Renderer;
00059 class RenderTargetToROSImage;
00060 class PickRenderValues;
00061 typedef std::vector<PickRenderValues> V_PickRenderValues;
00062 class ScreenRect;
00063 
00064 class RenderTarget
00065 {
00066 public:
00067   enum Type
00068   {
00069     Texture,
00070     Window
00071   };
00072 
00073   RenderTarget(const rve_common::UUID& id, Renderer* rend, uint32_t width, uint32_t height);
00074   virtual ~RenderTarget();
00075 
00076    const rve_common::UUID& getID();
00077    void attachCamera(Camera* cam);
00078    Camera* getCamera() { return cam_; }
00079 
00080   Ogre::RenderTarget* getOgreRenderTarget() { return final_target_; }
00081   const rve_msgs::RenderTargetFrameStats& getLastFrameStats() { return stats_; }
00082 
00083   typedef boost::function<void(V_PickRenderValues&)> PickCallback;
00084   void pick(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, const PickCallback& cb);
00085 
00086   bool renderNeeded() { return render_needed_; }
00087   void requestRender();
00088 
00089   virtual void beginRender();
00090   virtual void finishRender();
00091 
00092   virtual void resize(uint32_t width, uint32_t height) = 0;
00093   virtual Type getTargetType() = 0;
00094 
00095   uint32_t getWidth() { return width_; }
00096   uint32_t getHeight() { return height_; }
00097 
00098   ScreenRect* createScreenRect(const rve_common::UUID& id, uint32_t zorder, float x0, float y0, float x1, float y1);
00099   ScreenRect* getScreenRect(const rve_common::UUID& id);
00100   void destroyScreenRect(const rve_common::UUID& id);
00101 
00102 protected:
00103   void setTarget(Ogre::RenderTarget* target);
00104 
00105 private:
00106 
00107   struct PickRequest
00108   {
00109     PickRequest()
00110     : x0(0)
00111     , y0(0)
00112     , x1(0)
00113     , y1(0)
00114     {}
00115 
00116     PickRequest(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, const PickCallback& cb)
00117     : x0(x0)
00118     , y0(y0)
00119     , x1(x1)
00120     , y1(y1)
00121     , cb(cb)
00122     {}
00123 
00124     uint32_t x0;
00125     uint32_t y0;
00126     uint32_t x1;
00127     uint32_t y1;
00128     PickCallback cb;
00129   };
00130   typedef std::vector<PickRequest> V_PickRequest;
00131 
00132   typedef std::vector<Ogre::RenderTexture*> V_OgreRenderTexture;
00133   typedef std::vector<Ogre::TexturePtr> V_OgreTexture;
00134   typedef boost::shared_ptr<RenderTargetToROSImage> RenderTargetToROSImagePtr;
00135   typedef std::vector<RenderTargetToROSImagePtr> V_RenderTargetToROSImage;
00136   struct MRT
00137   {
00138     MRT()
00139     : mrt(0)
00140     {}
00141 
00142     Ogre::MultiRenderTarget* mrt;
00143     V_OgreRenderTexture rtts;
00144     V_OgreTexture textures;
00145     V_RenderTargetToROSImage ros_output;
00146   };
00147 
00148   void destroyResources();
00149   void createResources();
00150 
00151   void createMRT(MRT& mrt, const std::string& name, const std::string& scheme, uint32_t format);
00152   void destroyMRT(MRT& mrt);
00153 
00154   void setupRT(Ogre::RenderTarget* rt);
00155 
00156   void pick(const PickRequest& pr);
00157   void updatePickBuffer();
00158   void processPickRequests();
00159 
00160   rve_common::UUID id_;
00161   Ogre::RenderTarget* final_target_;
00162   RenderTargetToROSImagePtr final_target_out_;
00163   Renderer* renderer_;
00164   Camera* cam_;
00165   uint32_t width_;
00166   uint32_t height_;
00167   bool recreate_resources_;
00168   Ogre::Rectangle2D* screen_rect_;
00169   Ogre::MaterialPtr rect_material_;
00170 
00171   MRT gbuffer_target_;
00172   MRT weighted_average_alpha_target_;
00173 
00174   bool picked_;
00175   MRT picking_target_;
00176   bool pick_target_updated_this_frame_;
00177   bool pick_buffer_valid_;
00178   boost::shared_array<uint32_t> pick_buffer_;
00179   V_PickRequest pick_requests_;
00180   V_PickRequest new_pick_requests_;
00181   Ogre::PlaneBoundedVolumeListSceneQuery* pick_scene_query_;
00182 
00183   rve_msgs::RenderTargetFrameStats stats_;
00184 
00185   typedef std::vector<ScreenRect*> V_ScreenRect;
00186   V_ScreenRect screen_rects_;
00187 
00188   bool render_needed_;
00189   bool render_in_progress_;
00190 };
00191 
00192 } 
00193 
00194 #endif // RVE_RENDER_SERVER_RENDER_TARGET_H