glcontext.cc

来自「机器人人3D仿真工具,可以加入到Simbad仿真环境下应用。」· CC 代码 · 共 644 行 · 第 1/2 页

CC
644
字号
/* *  Gazebo - Outdoor Multi-Robot Simulator *  Copyright (C) 2003   *     Nate Koenig & Andrew Howard * *  This program is free software; you can redistribute it and/or modify *  it under the terms of the GNU General Public License as published by *  the Free Software Foundation; either version 2 of the License, or *  (at your option) any later version. * *  This program is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *  GNU General Public License for more details. * *  You should have received a copy of the GNU General Public License *  along with this program; if not, write to the Free Software *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA * *//* Desc: OpenGL context wrapper   * Author: Andrew Howard * Date: 7 Oct 2004 * CVS: $Id: GLContext.cc,v 1.20 2005/04/26 18:29:46 natepak Exp $ *//** @page gazebo_opengl OpenGL SupportGazebo uses OpenGL to render images for both user feedback (ObserverCammodel) and simulated cameras (e.g., MonoCam, StereoHead,SonyVID30 models).  This document describes OpenGL concepts with whichusers should be familiar, and lists some limitations of particularOpenGL driver implementations.@par Offscreen rendering and the renderMethod attributeWith version 0.5, Gazebo has moved to an off-screen rendering model;i.e., camera images are rendered to memory rather than the screen.This approach has a number of significant advantages:- Support for console mode operation (no GUI; good for batch jobs).- Support for stereo cameras (with simulated disparity maps).- No weird X window manager artifacts in simulated camera images (aswas the case with Gazebo 0.4).Unfortunately, hardware/driver support for (accelerated) offscreenrendering currently seems to be very patchy, with at least fourdifferent methods available:-# @b XLIB : Render to an unmapped X window.  This is just a sneaky way of using   the standard on-screen rendering pipeline (to a hidden window).   - Pros: hardware accelerated   - Cons: only works on some X11 implementations (e.g. works on OS X,     but not on Linux/Xorg).-# @b SGIX : Use the SGIX pbuffer extension (which was designed specifically for   this purpose).   - Pros: hardware accelerated   - Cons: only available on some chipsets (works on NVidia; not sure     about ATI).-# @b GLX : Use the GLX pixmap extension (write to offscreen pixmap).   - Pros: standard API.   - Cons: no hardware acceleration.-# @b GLXP : Use the GLX pbuffer API in GLX 1.3.   - Pros: standard API   - Cons: not supported on some drivers?Since different platforms support different solutions, Gazeboimplements methods 1, 2 and 3, selectable via the @c renderMethodattribute in the camera models.  Users can also select the @b AUTOmethod, which automatically cycles though the methods to find one thatworks.@par Hardware compatability listDue to the off-screen rendering issues described above, Gazebois not compatable with certain hardware/software combinations.Known working (and not working) combinations are listed below.- Linux/nVidia  - Gentoo Linux, XOrg X11  - nVidia GeForce (various) with nVidia binary driver  - Supports: GLXP (accelerated), SGIX (accelerated), GLX (non-accelerated)  - Notes: XLIB produces corrupted images- Linux/ATI  - Gentoo Linux, XOrg X11  - ATI ? with ATI flgrx driver  - Supports: GLXP (accelerated), GLX (non-accelerated)  - Notes: XLIB produces corrupt images; SGIX crashes Gazebo.  - OSX/nVidia  - Apple OS X 10.3, Apple X11 1.0  - nVidia with Apple drivers  - Supports: XLIB (accelerated), GLX (non-accelerated)Please let us know of any non-working combinations.  @par Performance tipsGazebo can be very CPU intensive, with almost all of the cycles goinginto rendering or collision detection.  Here are some tips for makingrendering faster:- To good approximation, rendering time is proportional to the the  number cameras (since each camera must render the scene), the size  of the camera image(s), and the camera update rate(s).  Reducing any  or all of these will increase rendering speed significantly.- Rendering time scales with scene complexity (number of vertices).  Some models are particularly vertex rich, and must be used  sparingly.  A good way to check the scene complexity is to force  wire frame rendering using the @c polygonFill attribute; this will  show all of the triangles in the scene.- Re-generate terrains with a sparser grid and/or larger error bound;  this can dramatically reduce the number of vertices in a terrain.- Turn off skins or re-create the skin model with a reduced set of  vertices.@par Camera frustrums and clip planesGazebo uses the standard OpenGL perspective projection (pin-holecamera).  The field-of-view for such cameras is defined by fourattributes:- The horizontal field of view.- The aspect ratio (image width/height).- Near and far clip planes.The easiest way to understand these attributes is through the notionof a frustrum (depicted below).@image html frustrum.gif "Camera frustrum"The frustrum defines the viewable volume in three dimensions, with thenear and far clip attributes bounding the depth of the volume (i.e.,the camera will only see points whose depth lies between the near andfar clip values).Clip values also have a second function: the ratio nearClip/farClipcontrols the effective resolution of the Z-buffer used by OpenGL forhidden surface removal.  See the OpenGL documentation for details, butsuffice to say setting the near clip to zero and the far clip toinfinity is a @b bad @b idea.  Lowering this ratio will lead to betterquality rendering.*//** @} */#if HAVE_CONFIG_H  #include <config.h>#endif#include <assert.h>#include <stdlib.h>#include <string.h>#include <GL/glut.h>#include "Error.hh"#include "GLContext.hh"//////////////////////////////////////////////////////////////////////////////// Global varsextern Display *display;extern bool optUseXlib;//////////////////////////////////////////////////////////////////////////////// Keep track of the total number of contextsstatic int contextCount = 0;//////////////////////////////////////////////////////////////////////////////// Constructor GLContext::GLContext(){  this->contextIndex = contextCount++;    return;}//////////////////////////////////////////////////////////////////////////////// DestructorGLContext::~GLContext(){  return;}//////////////////////////////////////////////////////////////////////////////// Initialize the cameraint GLContext::Init(int width, int height, int color, int alpha, int depth, const char *method, GLXContext shareList){  this->reqWidth = width;  this->reqHeight = height;  this->reqColor = color;  this->reqAlpha = alpha;  this->reqDepth = depth;    if (::display == NULL)  {    PRINT_ERR("X display not set");    return -1;  }  // Try SGIX pbuffer rendering  if (strcasecmp(method, "SGIX") == 0)  {    if (InitSGIX(shareList) != 0)      return -1;  }  else if (strcasecmp(method, "GLXP") == 0)  {    if (InitGLXP(shareList))      return -1;  }  // Try GLX pixmap rendering  else  if (strcasecmp(method, "GLX") == 0)  {    if (InitGLXPixmap(shareList) != 0)      return -1;  }  // Try Xlib rendering  else if (strcasecmp(method, "XLIB") == 0)  {    if (InitXlib(shareList) != 0)      return -1;  }  else  {#ifdef PLATFORM_OSX    // On OS X, we try Xlib rendering first (no pbuffer support), the    // fall back to GLX pixbuff rendering.    if (InitXlib(shareList) != 0)    {      if (InitGLXPixmap(shareList) != 0)      {        PRINT_ERR("unable to initialize GL context");        return -1;      }    }#else    // On Linux we try GLXP hardware rendering first, then    // fall back to SGIX hardware rendering, then fallback    // to GLX pixbuff.  Xlib rendering doesnt seem    // to work on Linux (needs more testing).    if (InitGLXP(shareList) != 0)    {      if (InitSGIX(shareList) != 0)      {              if (InitGLXPixmap(shareList) != 0)        {          PRINT_ERR("unable to initialize GL context");          return -1;        }      }    }    #endif  }  return 0;}//////////////////////////////////////////////////////////////////////////////// Create unmapped Xlib window for offscreen renderingint GLContext::InitXlib(GLXContext shareList){  XVisualInfo *visual;  Colormap cmap;  XSetWindowAttributes swa;  Window win;  GLXContext cx;  // REMOVE XEvent event;  int attrCount;  int attrList[32];  // TODO: honor color bits request  attrCount = 0;  attrList[attrCount++] = GLX_RGBA;  attrList[attrCount++] = GLX_DEPTH_SIZE;  attrList[attrCount++] = this->reqDepth;  attrList[attrCount++] = GLX_DOUBLEBUFFER;  attrList[attrCount++] = None;    /* get an appropriate visual */  visual = glXChooseVisual(::display, DefaultScreen(::display), attrList);  if (!visual)  {    PRINT_MSG0(2, "unable to get suitable visual");    return -1;  }    /* create a GLX context */  cx = glXCreateContext(::display, visual, shareList, GL_TRUE);  if (!cx)  {    PRINT_MSG0(2, "unable to create suitable context");    return -1;  }  /* create a color map */  cmap = XCreateColormap(::display, RootWindow(::display, visual->screen),                         visual->visual, AllocNone);  /* create a window */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?