Sirikata
liboh/plugins/ogre/OgreSystem.hpp
Go to the documentation of this file.
00001 /*  Sirikata libproxyobject -- Ogre Graphics Plugin
00002  *  OgreSystem.hpp
00003  *
00004  *  Copyright (c) 2009, Daniel Reiter Horn
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions are
00009  *  met:
00010  *  * Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  *  * Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in
00014  *    the documentation and/or other materials provided with the
00015  *    distribution.
00016  *  * Neither the name of Sirikata nor the names of its contributors may
00017  *    be used to endorse or promote products derived from this software
00018  *    without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00021  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00022  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00023  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00024  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00026  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 
00033 #ifndef _SIRIKATA_OGRE_GRAPHICS_
00034 #define _SIRIKATA_OGRE_GRAPHICS_
00035 
00036 #include <sirikata/core/util/Platform.hpp>
00037 #include <sirikata/core/util/Time.hpp>
00038 #include <sirikata/proxyobject/ProxyObject.hpp>
00039 #include <sirikata/proxyobject/ProxyCreationListener.hpp>
00040 #include <sirikata/ogre/OgreHeaders.hpp>
00041 #include <OgreResourceManager.h>
00042 #include <OgrePixelFormat.h>
00043 
00044 #include <sirikata/ogre/OgreRenderer.hpp>
00045 
00046 #include <sirikata/oh/ConnectionEventListener.hpp>
00047 #include <sirikata/oh/HostedObject.hpp>
00048 
00049 namespace Ogre {
00050 struct RaySceneQueryResultEntry;
00051 class SubEntity;
00052 }
00053 
00054 namespace Sirikata {
00055 class ProxyObject;
00057 namespace Graphics {
00058 class ProxyEntity;
00059 class CubeMap;
00060 struct IntersectResult;
00061 class OgreSystemInputHandler;
00062 
00064 class OgreSystem :
00065         public OgreRenderer,
00066         protected SessionEventListener,
00067         public ProxyCreationListener,
00068         public ConnectionEventListener
00069 {
00070     ConnectionEventProvider* mConnectionEventProvider;
00071     VWObjectPtr mViewer;
00072     SpaceObjectReference mPresenceID;
00073 
00074 
00075     friend class OgreSystemInputHandler;
00076     OgreSystemInputHandler *mInputHandler;
00077     void allocInputHandler();
00078     void destroyInputHandler();
00079     void tickInputHandler(const Task::LocalTime& t) const;
00080 
00081 
00082     OgreSystem(Context* ctx,Network::IOStrandPtr sStrand);
00083     bool initialize(ConnectionEventProvider* cevtprovider, VWObjectPtr viewer, const SpaceObjectReference& presenceid, const String&options);
00084 
00085 
00086 
00087     Ogre::RaySceneQuery* mRayQuery;
00088     CubeMap *mCubeMap;
00089 
00090     Invokable* mOnReadyCallback;
00091     Invokable* mOnResetReadyCallback;
00092 
00093     void handleUIReady();
00094     void iHandleUIReady(Liveness::Token osAlive);
00095     void handleUIResetReady();
00096     void handleUpdateUIViewport(int32 left, int32 top, int32 right, int32 bottom);
00097 
00098     ProxyEntity* internalRayTrace(const Ogre::Ray &traceFrom,
00099         bool aabbOnly,
00100         int&resultCount,
00101         double &returnResult,
00102         Vector3f &returnNormal,
00103         int& returnSubMesh,
00104         IntersectResult *returnIntersectResult, bool texcoord,
00105         int which=0,
00106         SpaceObjectReference ignore = SpaceObjectReference::null()) const;
00107 
00108     Mesh::VisualPtr mDefaultMesh;
00109 public:
00110 
00111     Camera *mPrimaryCamera;
00112     Camera* mOverlayCamera;
00113 
00114     // Overrides for Service
00115     virtual void stop();
00116 
00117     // For classes that only have access to OgreSystem and not a Context
00118     Time simTime();
00119 
00120     VWObjectPtr getViewer() const { return mViewer; }
00121     SpaceObjectReference getViewerPresence() const { return mPresenceID; }
00122 
00123     String getResourcesDir() const { return mResourcesDir; }
00124 
00125     Camera*getPrimaryCamera() {
00126         return mPrimaryCamera;
00127     }
00128 
00129     static TimeSteppedSimulation* create(
00130         Context* ctx,
00131         ConnectionEventProvider* cevtprovider,
00132         HostedObjectPtr obj,
00133         const SpaceObjectReference& presenceid,
00134         const String& options,
00135         Network::IOStrandPtr sStrand
00136     )
00137     {
00138         OgreSystem*os= new OgreSystem(ctx,sStrand);
00139         if (os->initialize(cevtprovider, obj, presenceid, options))
00140             return os;
00141         delete os;
00142         return NULL;
00143     }
00144 
00145     ProxyEntity* getEntity(const SpaceObjectReference &proxyId) const;
00146     ProxyEntity* getEntity(const ProxyObjectPtr &proxy) const;
00147     // Invoked when a ProxyEntity is self destructing
00148     void entityDestroyed(ProxyEntity*);
00149 
00150     bool queryRay(const Vector3d&position,
00151                   const Vector3f&direction,
00152                   const double maxDistance,
00153                   ProxyObjectPtr ignore,
00154                   double &returnDistance,
00155                   Vector3f &returnNormal,
00156                   SpaceObjectReference &returnName);
00157 
00158     Entity* rayTrace(const Vector3d &position,
00159                      const Vector3f &direction,
00160                      int&resultCount,
00161                      double &returnResult,
00162                      Vector3f&returnNormal,
00163                      int&subent,
00164         int which=0,
00165         SpaceObjectReference ignore = SpaceObjectReference::null()) const;
00166 
00167 
00168     virtual void windowResized(Ogre::RenderWindow *rw);
00169     // Converts from the full window coordinates into coordinates for
00170     // the actual display viewport (i.e. excluding the overlay
00171     // regions). Note that these might be negative or greater than the
00172     // width of the viewport, in which case the return value will be
00173     // false.
00174     bool translateToDisplayViewport(float32 x, float32 y, float32* ox, float32* oy);
00175 
00176     virtual void poll();
00177 
00178     virtual bool renderOneFrame(Task::LocalTime t, Duration frameTime);
00179     virtual void preFrame(Task::LocalTime currentTime, Duration frameTime);
00180     virtual void postFrame(Task::LocalTime current, Duration frameTime);
00181 
00182 
00183     // ProxyCreationListener
00184     virtual void onCreateProxy(ProxyObjectPtr p);
00185     virtual void onDestroyProxy(ProxyObjectPtr p);
00186 
00187 
00188 
00189     virtual void attachCamera(const String&renderTargetName,Camera*);
00190     virtual void detachCamera(Camera*);
00191 
00192 
00193     virtual Mesh::VisualPtr defaultMesh() const { return mDefaultMesh; }
00194 
00195     // *******
00196 
00197     // ConnectionEventListener Interface
00198     virtual void onConnected(const Network::Address& addr);
00199     virtual void onDisconnected(const Network::Address& addr, bool requested, const String& reason);
00200 
00201     // SessionEventListener Interface
00202     virtual void onDisconnected(SessionEventProviderPtr from, const SpaceObjectReference& name);
00203 
00204     boost::any invokeQuit(std::vector<boost::any>& params);
00205     boost::any invokeSuspend(std::vector<boost::any>& params);
00206     boost::any invokeToggleSuspend(std::vector<boost::any>& params);
00207     boost::any invokeResume(std::vector<boost::any>& params);
00208     boost::any invokeScreenshot(std::vector<boost::any>& params);
00209     // Sets the onReady callback, invoked when the basic graphics and UI are
00210     // ready to be used.
00211     boost::any setOnReady(std::vector<boost::any>& params);
00212     // Hide the loading screen, if it was displayed.
00213     boost::any evalInUI(std::vector<boost::any>& params);
00214     // Helper which creates a WebView window, either
00215     boost::any createWindow(const String& name, bool is_html, bool is_file, String content, uint32 width, uint32 height);
00216     // Create a window using a URL
00217     boost::any createWindowURL(std::vector<boost::any>& params);
00218     // Create a window using a file
00219     boost::any createWindowFile(std::vector<boost::any>& params);
00220     // Create a window using HTML
00221     boost::any createWindowHTML(std::vector<boost::any>& params);
00222     // Dynamically load a javscript UI module
00223     boost::any addModuleToUI(std::vector<boost::any>& params);
00224     boost::any addTextModuleToUI(std::vector<boost::any>& params);
00225 
00226     // Add an input handler function which will be invoked for input
00227     // events, e.g. mouse and keyboard
00228     boost::any addInputHandler(std::vector<boost::any>& params);
00229     boost::any removeInputHandler(std::vector<boost::any>& params);
00230 
00231     boost::any pick(std::vector<boost::any>& params);
00232     boost::any bbox(std::vector<boost::any>& params);
00233     boost::any visible(std::vector<boost::any>& params);
00234     boost::any axis(std::vector<boost::any>& params);
00235     boost::any world2Screen(std::vector<boost::any>& params);
00236 
00237     boost::any newDrawing(std::vector<boost::any>& params);
00238     boost::any setMat(std::vector<boost::any>& params);
00239     boost::any setInheritOrient(std::vector<boost::any>& params);
00240     boost::any setInheritScale(std::vector<boost::any>& params);
00241     boost::any setVisible(std::vector<boost::any>& params);
00242     boost::any shape(std::vector<boost::any>& params);
00243 
00244     boost::any getCamera(std::vector<boost::any>& params);
00245     boost::any setCameraPosition(std::vector<boost::any>& params);
00246     boost::any setCameraOrientation(std::vector<boost::any>& params);
00247     boost::any setCameraNearClipPlane(std::vector<boost::any>& params);
00248     boost::any setCameraFarClipPlane(std::vector<boost::any>& params);
00249     boost::any setCameraFOVY(std::vector<boost::any>& params);
00250     boost::any getAnimationList(std::vector<boost::any>& params);
00251 
00252     boost::any startAnimation(std::vector<boost::any>& params);
00253     boost::any stopAnimation(std::vector<boost::any>& params);
00254     boost::any isReady(std::vector<boost::any>& params);
00255 
00256     boost::any setSkybox(std::vector<boost::any>& params);
00257 
00258     ~OgreSystem();
00259 
00260 private:
00261     void instantiateAllObjects(ProxyManagerPtr pop);
00262     double clamp(const double& val);
00263 
00264     void iOnSessionDisconnected(
00265         Liveness::Token osAlive, SessionEventProviderPtr from,
00266         const SpaceObjectReference& name);
00267     void iOnNetworkDisconnected(
00268         Liveness::Token osAlive,const Network::Address& addr,
00269         bool requested, const String& reason);
00270 
00271     void iOnCreateProxy(
00272         Liveness::Token osAlive, ProxyObjectPtr p, bool inInit);
00273     void iOnDestroyProxy(
00274         Liveness::Token osAlive,ProxyObjectPtr p);
00275 
00276 
00277     typedef std::tr1::unordered_map<SpaceObjectReference, ProxyEntity*, SpaceObjectReference::Hasher> EntityMap;
00278     EntityMap mEntityMap;
00279     String currentMat;
00280     bool mReady;
00281     bool initialized;
00282 };
00283 
00284 
00285 } }
00286 #endif