viewport.h

Go to the documentation of this file.
00001 /*
00002  * viewport.h
00003  *
00004  * Copyright (C) 2008,2009,2010  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are 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 the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  * Basic support for multiple viewports.  This is so that (for example) multiple
00032  * people can play at the same physical machine.
00033  */
00034 
00035 #ifndef WAVE_GLUT_VIEWPORT_H__
00036 #define WAVE_GLUT_VIEWPORT_H__
00037 
00038 // includes --------------------------------------------------------------------
00039 #include "common/common.h"
00040 #include "geometry/geometry_2d.h"
00041 #include "threadsafe/smart_ptr.h"
00042 
00043 
00044 namespace view {
00045 
00046 
00047 ////////////////////////////////////////////////////////////////////////////////
00048 ///
00049 /// \ingroup wave_glut
00050 /// \defgroup viewport
00051 ///
00052 /// \n
00053 /// This library defines objects to help clients manage multiple viewports on
00054 /// a single window or screen.  A common use case is a client wanting to
00055 /// present multiple different 3D scenes on the same window.
00056 ///
00057 ////////////////////////////////////////////////////////////////////////////////
00058 /*@{*/
00059 
00060 
00061 typedef rect2d_t<int> recti_t;
00062 
00063 struct render_info_t {
00064         // constructor, manipulators
00065         render_info_t(void) throw() { this->clear(); }
00066         void clear(void) throw() {
00067                         width = height = 0;
00068                 }
00069         void dump(IN const char * txt) const throw() {
00070                         DPRINTF("%s (viewport): %d x %d", txt, width, height);
00071                 }
00072         bool isValid(void) const throw() {
00073                         return ((width > 0) && (height > 0));
00074                 }
00075 
00076         // data fields
00077         int             width;  ///< viewport width (pixels)
00078         int             height; ///< viewport height (pixels)
00079 };
00080 
00081 
00082 
00083 ///     If you are creating viewports, you'll need an object that supports this
00084 ///     interface to handle the per-viewport callbacks for display.
00085 class Host {
00086 public:
00087         // virtual destructor --------------------------------------------------
00088         virtual ~Host(void) throw();
00089 
00090         // view::Host class interface methods ----------------------------------
00091 
00092         /// host is being called to render this viewport's 3D scene
00093         virtual void render3D(IN int id, IN const render_info_t& ri) = 0;
00094 
00095         /// host is being called to render this viewport's 2D overlay.  The
00096         ///     OpenGL drawing context has already been set for 2D drawing.
00097         ///     2D coordinates are relative to the viewport (so 0,0 is the
00098         ///     top left of the viewport).
00099         ///     Reminder: this library uses the wavepacket 2D coordinate
00100         ///     convention, where y=0 is the top of the screen, and positive
00101         ///     y is down.  This is opposite to the usual OpenGL convention.
00102         virtual void render2D(IN int id, IN const render_info_t& ri) = 0;
00103 };
00104 
00105 
00106 
00107 ///     This object will let you create/destroy viewports as needed.
00108 class Manager {
00109 public:
00110         // virtual destructor --------------------------------------------------
00111         virtual ~Manager(void) throw();
00112 
00113         // view::Manager class interface methods -------------------------------
00114         virtual int getViewportCount(void) const = 0;
00115 
00116         /// render all viewports.  The Manager will make two passes: first all
00117         ///     viewports will be called on Host::render3D(), then 2D context
00118         ///     will be set up, and then all viewports will be called on
00119         ///     Host::render2D().
00120         virtual void render(IN int windowWidth, IN int windowHeight) = 0;
00121 
00122         /// given an x/y coordinate, who owns it?  Returns -1 if no one owns
00123         ///     if someone owns, returns the x and y coordinates relative to the
00124         ///     viewport of that owner.
00125         virtual int getOwnerOfCoordinate(IN int x, IN int y,
00126                                 IN int width, IN int height,
00127                                 OUT int& ownerX, OUT int& ownerY) = 0;
00128 
00129         /// caller can request a new viewport be created.  Caller is responsible
00130         ///     for providing the ID of the new viewport.  Returns false on
00131         ///     failure (fails if ID is already in use).  Viewport IDs must be
00132         ///     greater than zero.
00133         virtual bool createViewport(IN int id,
00134                                 smart_ptr<Host> host) = 0;
00135 
00136         /// remove the specified viewport.  Returns false on failure (no
00137         ///     viewport with the specified ID found).
00138         virtual bool removeViewport(IN int id) = 0;
00139 
00140         // static factory methods ----------------------------------------------
00141         static smart_ptr<Manager> create(void);
00142 };
00143 
00144 
00145 
00146 };      // view namespace
00147 
00148 
00149 #endif  // WAVE_GLUT_VIEWPORT_H__
00150