00001 /*************** <auto-copyright.pl BEGIN do not edit this line> ************** 00002 * 00003 * VR Juggler is (C) Copyright 1998-2005 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$ 00027 * Date modified: $Date: 2006-07-12 17:21:40 -0500 (Wed, 12 Jul 2006) $ 00028 * Version: $Revision: 19032 $ 00029 * ----------------------------------------------------------------- 00030 * 00031 *************** <auto-copyright.pl END do not edit this line> ***************/ 00032 00033 #include <vrj/Draw/OGL/Config.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 keyboard/mouse 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/KeyboardMouse.h> 00066 #include <gadget/Type/KeyboardMouseProxy.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 void GlWindow::configWindow(vrj::Display* displayWindow) 00083 { 00084 vprASSERT(displayWindow != NULL); // We can't config to a NULL display 00085 mVrjDisplay = displayWindow; 00086 mVrjDisplay->getOriginAndSize(mOriginX, mOriginY, mWindowWidth, 00087 mWindowHeight); 00088 mHasBorder = mVrjDisplay->shouldDrawBorder(); 00089 mHideMouse = mVrjDisplay->shouldHideMouse(); 00090 } 00091 00092 void GlWindow::swapBuffers() 00093 { 00094 mSwapCount++; 00095 } 00096 00100 void GlWindow::finishSetup() 00101 { 00102 vprASSERT(mWindowIsOpen && "Pre-condition of being open failed"); 00103 00104 // --- Setup any attached simulator that is needed --- // 00105 // XXX: This is where we used to set the keyboard proxy for the simulator 00106 // this needs to be customized now since we do not acutally have a device here any more 00107 // Ideas: - look up device based on registry 00108 // - Do something else??? 00109 /* 00110 Viewport* viewport(NULL); 00111 SimViewport* sim_vp(NULL); 00112 unsigned num_vps = mVrjDisplay->getNumViewports(); 00113 for(unsigned vp_num=0; vp_num < num_vps; vp_num++) 00114 { 00115 viewport = mVrjDisplay->getViewport(vp_num); 00116 if(viewport->isSimulator()) 00117 { 00118 sim_vp = dynamic_cast<SimViewport*>(viewport); 00119 vprASSERT(NULL != sim_vp && "isSimulator lied"); 00120 00121 DrawSimInterface* draw_sim = sim_vp->getDrawSimInterface(); 00122 GlSimInterface* gl_draw_sim = dynamic_cast<GlSimInterface*>(draw_sim); 00123 if(NULL != gl_draw_sim) 00124 { 00125 // Setup the simulator 00126 // - Get the keyboard/mouse device 00127 // - Register a proxy that we will use 00128 // - Make device interface for that proxy 00129 // - Intialize the simulator 00130 vprASSERT(mIsEventSource && "Tried to use simulator with a non-keyboard enabled GL window. Bad programmer."); 00131 gadget::EventWindow* kb_dev = dynamic_cast<gadget::EventWindow*>(this); 00132 gadget::Input* input_dev = dynamic_cast<gadget::Input*>(this); 00133 vprASSERT((kb_dev != NULL) && (input_dev != NULL) && "Failed to cast glWindow impl to a gadget::EventWindow"); 00134 std::string kb_dev_name = input_dev->getInstanceName(); 00135 vprASSERT( gadget::InputManager::instance()->getDevice(kb_dev_name) != NULL); 00136 00137 gadget::EventWindowProxy* kb_proxy = new gadget::EventWindowProxy; 00138 kb_proxy->set(kb_dev_name, kb_dev); 00139 00140 std::string kb_proxy_name("GlWin-Sim-EventWindow-"); 00141 kb_proxy_name += kb_dev_name; 00142 kb_proxy_name += std::string("-Proxy"); 00143 kb_proxy->setName(kb_proxy_name); 00144 bool add_success = gadget::InputManager::instance()->addProxy(kb_proxy); 00145 00146 if ( ! add_success ) 00147 { 00148 vprDEBUG(vrjDBG_DRAW_MGR, vprDBG_WARNING_LVL) 00149 << clrOutBOLD(clrYELLOW, "WARNING") 00150 << ": [vrj::GlWindow::finishSetup()] Failed to add the " 00151 << "keyboard/mouse proxy. Check for a unique name." 00152 << std::endl << vprDEBUG_FLUSH; 00153 vprASSERT(false && "Failed to add sim wind kb proxy: Check for unique name"); 00154 } 00155 else 00156 { 00157 gadget::EventWindowInterface kb_interface; 00158 kb_interface.setProxy(kb_proxy); 00159 gl_draw_sim->setEventWindow(kb_interface); // Initialize the simulator 00160 } 00161 } 00162 else 00163 { 00164 vprDEBUG(vrjDBG_DRAW_MGR, vprDBG_CRITICAL_LVL) 00165 << clrOutBOLD(clrRED, "ERROR") 00166 << ": [vrj::GlWindow::finishSetup()] You configured a simulator " 00167 << "viewport, but I cannot find a DrawSimInterface for it.\n" 00168 << vprDEBUG_FLUSH; 00169 vprDEBUG_CONT(vrjDBG_DRAW_MGR, vprDBG_CRITICAL_LVL) 00170 << "Check your configuration for missing information.\n" 00171 << vprDEBUG_FLUSH; 00172 vprASSERT(false && "You configured a simulator viewport, but I cannot find a DrawSimInterface for it"); 00173 } 00174 } 00175 } 00176 */ 00177 } 00178 00179 void GlWindow::updateViewport() 00180 { 00181 glViewport(0, 0, mWindowWidth, mWindowHeight); 00182 setDirtyViewport(false); 00183 } 00184 00185 void GlWindow::setViewport(float xo, float yo, float xSize, float ySize) 00186 { 00187 vprASSERT( ((xo+xSize) <= 1.0f) && "X viewport sizes are out of range"); 00188 vprASSERT( ((yo+ySize) <= 1.0f) && "Y viewport sizes are out of range"); 00189 00190 unsigned ll_x = unsigned(xo * float(mWindowWidth)); 00191 unsigned ll_y = unsigned(yo * float(mWindowHeight)); 00192 unsigned x_size = unsigned(xSize * float(mWindowWidth)); 00193 unsigned y_size = unsigned(ySize * float(mWindowHeight)); 00194 00195 glViewport(ll_x, ll_y, x_size, y_size); 00196 } 00197 00198 void GlWindow::setViewBuffer(vrj::Viewport::View view) 00199 { 00200 if(!isStereo()) 00201 { 00202 glDrawBuffer(GL_BACK); 00203 } 00204 else if(Viewport::LEFT_EYE == view) 00205 { 00206 glDrawBuffer(GL_BACK_LEFT); 00207 } 00208 else if(Viewport::RIGHT_EYE == view) 00209 { 00210 glDrawBuffer(GL_BACK_RIGHT); 00211 } 00212 } 00213 00214 void GlWindow::setProjection(vrj::Projection* proj) 00215 { 00216 if (!mWindowIsOpen) 00217 { 00218 return; 00219 } 00220 00221 const float* frust = proj->getFrustum().frust; 00222 00223 vprDEBUG(vrjDBG_DRAW_MGR, vprDBG_HEX_LVL) << "---- Frustum ----\n" 00224 << proj->getFrustum().frust << std::endl 00225 << vprDEBUG_FLUSH; 00226 00227 // --- Set up the projection --- // 00228 glMatrixMode(GL_PROJECTION); 00229 { 00230 glLoadIdentity(); // Load identity matrix 00231 glFrustum(frust[Frustum::VJ_LEFT],frust[Frustum::VJ_RIGHT], 00232 frust[Frustum::VJ_BOTTOM],frust[Frustum::VJ_TOP], 00233 frust[Frustum::VJ_NEAR],frust[Frustum::VJ_FAR]); 00234 #ifdef USE_PROJECTION_MATRIX 00235 // Set camera rotation and position 00236 glMultMatrixf(proj->getViewMatrix().mData); 00237 #endif 00238 } 00239 glMatrixMode(GL_MODELVIEW); 00240 #ifndef USE_PROJECTION_MATRIX 00241 // Set camera rotation and position 00242 glLoadIdentity(); 00243 glMultMatrixf(proj->getViewMatrix().mData); 00244 #endif 00245 } 00246 00247 00248 int GlWindow::getNextWindowId() 00249 { 00250 vpr::Guard<vpr::Mutex> guard(mWinIdMutex); // Protect the id 00251 return mCurMaxWinId++; 00252 } 00253 00254 std::ostream& operator<<(std::ostream& out, GlWindow& win) 00255 { 00256 vprASSERT(win.mVrjDisplay != NULL); 00257 00258 //out << "-------- GlWindow --------" << endl; 00259 out << "Open: " << (win.mWindowIsOpen ? "Yes" : "No") << std::endl; 00260 out << "Stereo: " << (win.mInStereo ? "Yes" : "No") << std::endl; 00261 out << "Swap Count: " << win.mSwapCount << std::endl; 00262 out << "Display Info:\n" << *(win.mVrjDisplay) << std::endl; 00263 return out; 00264 } 00265 00266 } // end namespace
1.5.1