Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

GlWindow.cpp

Go to the documentation of this file.
00001 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
00002  *
00003  * VR Juggler is (C) Copyright 1998-2003 by Iowa State University
00004  *
00005  * Original Authors:
00006  *   Allen Bierbaum, Christopher Just,
00007  *   Patrick Hartling, Kevin Meinert,
00008  *   Carolina Cruz-Neira, Albert Baker
00009  *
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Library General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Library General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Library General Public
00021  * License along with this library; if not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00023  * Boston, MA 02111-1307, USA.
00024  *
00025  * -----------------------------------------------------------------
00026  * File:          $RCSfile: GlWindow.cpp,v $
00027  * Date modified: $Date: 2004/04/13 01:57:07 $
00028  * Version:       $Revision: 1.59 $
00029  * -----------------------------------------------------------------
00030  *
00031  *************** <auto-copyright.pl END do not edit this line> ***************/
00032 
00033 #include <vrj/vrjConfig.h>
00034 
00035 #include <vpr/vpr.h>
00036 
00037 #ifdef VPR_OS_Darwin
00038 #   include <OpenGL/gl.h>
00039 #   include <OpenGL/glu.h>
00040 #else
00041 #   include <GL/gl.h>
00042 #   include <GL/glu.h>
00043 #endif
00044 
00045 #include <vrj/Display/Projection.h>
00046 #include <vrj/Display/CameraProjection.h>
00047 #include <vrj/Display/Frustum.h>
00048 #include <vrj/Util/Debug.h>
00049 #include <vrj/Display/Viewport.h>
00050 #include <vrj/Display/SimViewport.h>
00051 //#include <vrj/Display/SurfaceViewport.h>
00052 #include <vrj/Draw/OGL/GlWindow.h>
00053 #include <vrj/Draw/DrawSimInterface.h>
00054 
00055 #include <vrj/Draw/OGL/GlSimInterface.h>
00056 
00057 #include <gadget/InputManager.h>
00058 
00059 // Get info about Gadgeteer event window stuff for registering the simulator
00060 // below.
00061 // Note: This may seem kind of strange (and it is) but we need it
00062 // since all of our derived types are going to be gadget::Eventwindows as well
00063 // as GL Windows.  They need to be so we can get keyboard input for
00064 // the simulators that will run on/in this window
00065 #include <gadget/Type/EventWindow.h>
00066 #include <gadget/Type/EventWindowProxy.h>
00067 #include <gadget/Type/DeviceInterface.h>
00068 
00069 // This variable determines which matrix stack we put the viewing transformation
00070 // If it is on the proj matrix, then lighting and env maps work but fog breaks.
00071 #define USE_PROJECTION_MATRIX 1  /* Should we put the camera transforms on the
00072                                    Projection or modelview matrix */
00073 
00074 
00075 
00076 namespace vrj
00077 {
00078 
00079 int GlWindow::mCurMaxWinId = 0;
00080 vpr::Mutex GlWindow::mWinIdMutex;
00081 
00082 
00083 void GlWindow::configWindow(vrj::Display* displayWindow)
00084 {
00085    vprASSERT(displayWindow != NULL);      // We can't config to a NULL display
00086    mVrjDisplay = displayWindow;
00087    mVrjDisplay->getOriginAndSize(origin_x, origin_y, window_width, window_height);
00088    border = mVrjDisplay->shouldDrawBorder();
00089 
00091 }
00092 
00096 void GlWindow::finishSetup()
00097 {
00098    vprASSERT(window_is_open && "Pre-condition of being open failed");
00099 
00100    // --- Setup any attached simulator that is needed --- //
00101    Viewport* viewport = NULL;
00102    SimViewport* sim_vp = NULL;
00103    unsigned num_vps = mVrjDisplay->getNumViewports();
00104    for(unsigned vp_num=0; vp_num < num_vps; vp_num++)
00105    {
00106       viewport = mVrjDisplay->getViewport(vp_num);
00107       if(viewport->isSimulator())
00108       {
00109          sim_vp = dynamic_cast<SimViewport*>(viewport);
00110          vprASSERT(NULL != sim_vp && "isSimulator lied");
00111 
00112          DrawSimInterface* draw_sim = sim_vp->getDrawSimInterface();
00113          GlSimInterface* gl_draw_sim = dynamic_cast<GlSimInterface*>(draw_sim);
00114          if(NULL != gl_draw_sim)
00115          {
00116             // Setup the simulator
00117             // - Get the event window device
00118             // - Register a proxy that we will use
00119             // - Make device interface for that proxy
00120             // - Intialize the simulator
00121             vprASSERT(mAreEventSource && "Tried to use simulator with a non-keyboard enabled GL window. Bad programmer.");
00122             gadget::EventWindow* kb_dev = dynamic_cast<gadget::EventWindow*>(this);
00123             gadget::Input* input_dev = dynamic_cast<gadget::Input*>(this);
00124             vprASSERT((kb_dev != NULL) && (input_dev != NULL) && "Failed to cast glWindow impl to a gadget::EventWindow");
00125             std::string kb_dev_name = input_dev->getInstanceName();
00126             vprASSERT( gadget::InputManager::instance()->getDevice(kb_dev_name) != NULL);
00127 
00128             gadget::EventWindowProxy* kb_proxy = new gadget::EventWindowProxy;
00129             kb_proxy->set(kb_dev_name, kb_dev);
00130 
00131             std::string kb_proxy_name("GlWin-Sim-EventWindow-");
00132             kb_proxy_name += kb_dev_name;
00133             kb_proxy_name += std::string("-Proxy");
00134             kb_proxy->setName(kb_proxy_name);
00135             bool add_success = gadget::InputManager::instance()->addProxy(kb_proxy);
00136 
00137             if ( ! add_success )
00138             {
00139                vprDEBUG(vrjDBG_DRAW_MGR, vprDBG_WARNING_LVL)
00140                   << clrOutBOLD(clrYELLOW, "WARNING")
00141                   << ": [vrj::GlWindow::finishSetup()] Failed to add the "
00142                   << "event window proxy.  Check for a unique name."
00143                   << std::endl << vprDEBUG_FLUSH;
00144                vprASSERT(false && "Failed to add sim wind kb proxy: Check for unique name");
00145             }
00146             else
00147             {
00148                gadget::EventWindowInterface kb_interface;
00149                kb_interface.setProxy(kb_proxy);
00150                gl_draw_sim->setEventWindow(kb_interface); // Initialize the simulator
00151             }
00152          }
00153          else
00154          {
00155             vprDEBUG(vrjDBG_DRAW_MGR, vprDBG_CRITICAL_LVL)
00156                << clrOutBOLD(clrRED, "ERROR")
00157                << ": [vrj::GlWindow::finishSetup()] You configured a simulator "
00158                << "viewport, but I cannot find a DrawSimInterface for it.\n"
00159                << vprDEBUG_FLUSH;
00160             vprDEBUG_CONT(vrjDBG_DRAW_MGR, vprDBG_CRITICAL_LVL)
00161                << "Check your configuration for missing information.\n"
00162                << vprDEBUG_FLUSH;
00163             vprASSERT(false && "You configured a simulator viewport, but I cannot find a DrawSimInterface for it");
00164          }
00165       }
00166    }
00167 
00168 }
00169 
00170 
00171 
00172 void GlWindow::updateViewport()
00173 {
00174    glViewport(0,0, window_width, window_height);
00175    setDirtyViewport(false);
00176 }
00177 
00178 void GlWindow::setViewport(float xo, float yo, float xSize, float ySize)
00179 {
00180    vprASSERT( ((xo+xSize) <= 1.0f) && "X viewport sizes are out of range");
00181    vprASSERT( ((yo+ySize) <= 1.0f) && "Y viewport sizes are out of range");
00182 
00183    unsigned ll_x = unsigned(xo*float(window_width));
00184    unsigned ll_y = unsigned(yo*float(window_height));
00185    unsigned x_size = unsigned(xSize*float(window_width));
00186    unsigned y_size = unsigned(ySize*float(window_height));
00187 
00188    glViewport(ll_x, ll_y, x_size, y_size);
00189 }
00190 
00191 
00192 void GlWindow::setViewBuffer(vrj::Viewport::View view)
00193 {
00194    if(!isStereo())
00195       glDrawBuffer(GL_BACK);
00196    else if(Viewport::LEFT_EYE == view)
00197       glDrawBuffer(GL_BACK_LEFT);
00198    else if(Viewport::RIGHT_EYE == view)
00199       glDrawBuffer(GL_BACK_RIGHT);
00200 }
00201 
00202 void GlWindow::setProjection(vrj::Projection* proj)
00203 {
00204    if (!window_is_open)
00205       return;
00206 
00207    const float* frust = proj->getFrustum().frust;
00208 
00209    vprDEBUG(vrjDBG_DRAW_MGR, vprDBG_HEX_LVL)  << "---- Frustum ----\n"
00210                << proj->getFrustum().frust << std::endl
00211                << vprDEBUG_FLUSH;
00212 
00213    // --- Set up the projection --- //
00214    glMatrixMode(GL_PROJECTION);
00215    {
00216       glLoadIdentity();             // Load identity matrix
00217       glFrustum(frust[Frustum::VJ_LEFT],frust[Frustum::VJ_RIGHT],
00218                  frust[Frustum::VJ_BOTTOM],frust[Frustum::VJ_TOP],
00219                  frust[Frustum::VJ_NEAR],frust[Frustum::VJ_FAR]);
00220 #ifdef USE_PROJECTION_MATRIX
00221          // Set camera rotation and position
00222       glMultMatrixf(proj->getViewMatrix().mData);
00223 #endif
00224    }
00225    glMatrixMode(GL_MODELVIEW);
00226 #ifndef USE_PROJECTION_MATRIX
00227       // Set camera rotation and position
00228    glLoadIdentity();
00229    glMultMatrixf(proj->getViewMat().mData);
00230 #endif
00231 }
00232 
00233 
00234 int GlWindow::getNextWindowId()
00235 {
00236 vpr::Guard<vpr::Mutex> guard(mWinIdMutex);      // Protect the id
00237    return mCurMaxWinId++;
00238 }
00239 
00240 std::ostream& operator<<(std::ostream& out, GlWindow& win)
00241 {
00242    vprASSERT(win.mVrjDisplay != NULL);
00243 
00244    //out << "-------- GlWindow --------" << endl;
00245    out << "Open: " << (win.window_is_open ? "Yes" : "No") << std::endl;
00246    out << "Stereo: " << (win.in_stereo ? "Yes" : "No") << std::endl;
00247    out << "Display Info:\n" << *(win.mVrjDisplay) << std::endl;
00248    return out;
00249 }
00250 
00251 } // end namespace

Generated on Sun May 2 15:10:17 2004 for VR Juggler by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002