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

SDLOpenGLDisplayDevice.C

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr                                                                       
00003  *cr            (C) Copyright 1995-2019 The Board of Trustees of the           
00004  *cr                        University of Illinois                       
00005  *cr                         All Rights Reserved                        
00006  *cr                                                                   
00007  ***************************************************************************/
00008 /***************************************************************************
00009  * RCS INFORMATION:
00010  *
00011  *      $RCSfile: SDLOpenGLDisplayDevice.C,v $
00012  *      $Author: johns $        $Locker:  $             $State: Exp $
00013  *      $Revision: 1.41 $       $Date: 2020/02/26 07:21:45 $
00014  *
00015  ***************************************************************************/
00024 #include <stdlib.h>
00025 #include <math.h>
00026 #if defined(__APPLE__)
00027 #include <OpenGL/gl.h>
00028 #else
00029 #include <GL/gl.h>
00030 #endif
00031 #include <SDL.h>
00032 
00033 #include "OpenGLDisplayDevice.h"
00034 #include "Inform.h"
00035 #include "utilities.h"
00036 #include "config.h"   // VMD version strings etc
00037 
00038 // static data for this object
00039 static const char *glStereoNameStr[OPENGL_STEREO_MODES] = 
00040  { "Off", 
00041    "QuadBuffered", 
00042    "HDTV SideBySide",
00043    "Checkerboard",
00044    "ColumnInterleaved",
00045    "RowInterleaved",
00046    "Anaglyph", 
00047    "SideBySide", 
00048    "AboveBelow",
00049    "Left", 
00050    "Right" };
00051 
00052 static const char *glRenderNameStr[OPENGL_RENDER_MODES] =
00053 { "Normal",
00054   "GLSL",
00055   "Acrobat3D" };
00056 
00057 static const char *glCacheNameStr[OPENGL_CACHE_MODES] =
00058 { "Off",
00059   "On" };
00060 
00061 
00063 
00064 OpenGLDisplayDevice::OpenGLDisplayDevice()
00065 : OpenGLRenderer((char *) "VMD " VMDVERSION " OpenGL Display") {
00066 
00067   // set up data possible before opening window
00068   stereoNames = glStereoNameStr;
00069   stereoModes = OPENGL_STEREO_MODES;
00070 
00071   renderNames = glRenderNameStr;
00072   renderModes = OPENGL_RENDER_MODES;
00073 
00074   cacheNames = glCacheNameStr;
00075   cacheModes = OPENGL_CACHE_MODES;
00076 
00077   memset(&sdlsrv, 0, sizeof(sdlsrv));
00078   have_window = FALSE;
00079   screenX = screenY = 0;
00080   vmdapp = NULL;
00081 }
00082 
00083 int OpenGLDisplayDevice::init(int argc, char **argv, VMDApp* app, int *size, int *loc) {
00084 
00085   // open the window
00086   sdlsrv.windowID = open_window(name, size, loc, argc, argv);
00087   if (!have_window) return FALSE;
00088 
00089   // set flags for the capabilities of this display
00090   // whether we can do antialiasing or not.
00091   if (ext->hasmultisample) 
00092     aaAvailable = TRUE;  // we use multisampling over other methods
00093   else
00094     aaAvailable = FALSE; // no non-multisample implementation yet
00095 
00096   // set default settings
00097   if (ext->hasmultisample) {
00098     aa_on();  // enable fast multisample based antialiasing by default
00099               // other antialiasing techniques are slow, so only multisample
00100               // makes sense to enable by default.
00101   } 
00102 
00103   cueingAvailable = TRUE;
00104   cueing_on(); // leave depth cueing on by default, despite the speed hit.
00105 
00106   cullingAvailable = TRUE;
00107   culling_off();
00108 
00109   set_sphere_mode(sphereMode);
00110   set_sphere_res(sphereRes);
00111   set_line_width(lineWidth);
00112   set_line_style(lineStyle);
00113 
00114   // reshape and clear the display, which initializes some other variables
00115   reshape();
00116   normal();
00117   clear();
00118   update();
00119 
00120   // successfully opened window.
00121   return TRUE;
00122 }
00123 
00124 // destructor ... close the window
00125 OpenGLDisplayDevice::~OpenGLDisplayDevice(void) {
00126   if (have_window) {
00127     free_opengl_ctx(); // free display lists, textures, etc
00128   }
00129 
00130   SDL_Quit();
00131 }
00132 
00133 
00135 
00136 
00137 // create a new window and set it's characteristics
00138 int OpenGLDisplayDevice::open_window(char *nm, int *size, int *loc,
00139                                      int argc, char** argv) {
00140   int SX = 100, SY = 100, W, H;
00141  
00142   char *dispname = NULL;
00143   if ((dispname = getenv("VMDGDISPLAY")) == NULL)
00144     dispname = getenv("DISPLAY");
00145 
00146   if (SDL_Init(SDL_INIT_VIDEO) < 0) {
00147     msgErr << "Exiting due to SDL window creation failure." << sendmsg;
00148     SDL_Quit();
00149     return -1;
00150   }
00151   // get info about root window
00152   screenX = 1280; // XXX hack
00153   screenY = 1024;
00154   W = size[0];
00155   H = size[1];
00156   if (loc) {
00157     SX = loc[0];
00158     // The X11 screen uses Y increasing from upper-left corner down; this is
00159     // opposite to what GL does, which is the way VMD was set up originally
00160     SY = (screenY - loc[1]) - H;
00161   }
00162   ext->hasstereo = FALSE;        // stereo is off until we find out otherwise.
00163   ext->stereodrawforced = FALSE; // don't force stereo draws initially.
00164   ext->hasmultisample = FALSE;   // multisample is off until we find out otherwise.
00165   
00166   if (SDL_SetVideoMode(W, H, 32, SDL_OPENGL) == NULL) {
00167     msgInfo << "Tried 32 bit color and failed..." << sendmsg;
00168     if (SDL_SetVideoMode(W, H, 24, SDL_OPENGL) == NULL) { 
00169       msgInfo << "Tried 24 bit color and failed..." << sendmsg;
00170       if (SDL_SetVideoMode(W, H, 16, SDL_OPENGL) == NULL) {
00171         msgInfo << "Tried 16 bit color and failed..." << sendmsg;
00172         msgErr << "Cannot open display.  Exiting ..." << sendmsg;
00173         SDL_Quit();
00174         return -1;
00175       }
00176     }
00177   } 
00178 
00179   SDL_WM_SetCaption("VMD " VMDVERSION " OpenGL Display", NULL);
00180 
00181   // (9) configure the rendering properly
00182   setup_initial_opengl_state();  // setup initial OpenGL state
00183 
00184   // Tell init that we successfully created a window.
00185   have_window = TRUE;
00186 
00187   return 0; // return window id
00188 }
00189 
00190 
00192 
00193 void OpenGLDisplayDevice::do_resize_window(int width, int height) {
00194   // not implemented yet
00195 }
00196 
00197 void OpenGLDisplayDevice::do_reposition_window(int xpos, int ypos) {
00198   // not implemented yet
00199 }
00200 
00201 //
00202 // get the current state of the device's pointer (i.e. cursor if it has one)
00203 //
00204 
00205 // abs pos of cursor from lower-left corner of display
00206 int OpenGLDisplayDevice::x(void) {
00207   int rx;
00208 
00209   rx = 0;
00210 
00211   return rx;
00212 }
00213 
00214 
00215 // same, for y direction
00216 int OpenGLDisplayDevice::y(void) {
00217   int ry;
00218 
00219   ry = 0;
00220 
00221   return ry;
00222 }
00223 
00224 // return the current state of the shift, control, and alt keys
00225 int OpenGLDisplayDevice::shift_state(void) {
00226   int retval = 0;
00227 
00228   // return the result
00229   return retval;
00230 }
00231 
00232 // return the spaceball state, if any
00233 int OpenGLDisplayDevice::spaceball(int *rx, int *ry, int *rz, int *tx, int *ty,
00234 int *tz, int *buttons) {
00235   // not implemented yet
00236   return 0;
00237 }
00238 
00239 
00240 // set the Nth cursor shape as the current one.  If no arg given, the
00241 // default shape (n=0) is used.
00242 void OpenGLDisplayDevice::set_cursor(int n) {
00243   // unimplemented
00244 }
00245 
00246 
00247 //
00248 // event handling routines
00249 //
00250 
00251 // queue the standard events (need only be called once ... but this is
00252 // not done automatically by the window because it may not be necessary or
00253 // even wanted)
00254 void OpenGLDisplayDevice::queue_events(void) {
00255 }
00256 
00257 // read the next event ... returns an event type (one of the above ones),
00258 // and a value.  Returns success, and sets arguments.
00259 int OpenGLDisplayDevice::read_event(long &retdev, long &retval) {
00260   int done = 0;
00261   SDL_Event event;
00262 
00263   SDL_PollEvent(&event);
00264 
00265   if ( event.type == SDL_KEYDOWN ) {
00266     if ( event.key.keysym.sym == SDLK_ESCAPE ) {
00267 printf("ESC pressed!!\n");
00268       done = 1;
00269     }
00270   }
00271 
00272   return FALSE;
00273 }
00274 
00275 
00276 //
00277 // virtual routines for preparing to draw, drawing, and finishing drawing
00278 //
00279 
00280 // reshape the display after a shape change
00281 void OpenGLDisplayDevice::reshape(void) {
00282   xSize = 512;
00283   ySize = 512;
00284   xOrig = 0;
00285   yOrig = 0;
00286 
00287   switch (inStereo) {
00288     case OPENGL_STEREO_SIDE:
00289       set_screen_pos(0.5f * (float)xSize / (float)ySize);
00290       break;
00291 
00292     case OPENGL_STEREO_ABOVEBELOW:
00293       set_screen_pos(2.0f * (float)xSize / (float)ySize);
00294       break;
00295 
00296     case OPENGL_STEREO_STENCIL_CHECKERBOARD:
00297     case OPENGL_STEREO_STENCIL_COLUMNS:
00298     case OPENGL_STEREO_STENCIL_ROWS:
00299       enable_stencil_stereo(inStereo);
00300       set_screen_pos((float)xSize / (float)ySize);
00301       break;
00302  
00303     default:
00304       set_screen_pos((float)xSize / (float)ySize);
00305       break;
00306   }
00307 }
00308 
00309 unsigned char * OpenGLDisplayDevice::readpixels_rgb3u(int &x, int &y) {
00310   unsigned char * img;
00311 
00312   x = xSize;
00313   y = ySize;
00314 
00315   if ((img = (unsigned char *) malloc(x * y * 3)) != NULL) {
00316     glPixelStorei(GL_PACK_ALIGNMENT, 1);
00317     glReadPixels(0, 0, x, y, GL_RGB, GL_UNSIGNED_BYTE, img);
00318   } else {
00319     x = 0;
00320     y = 0;
00321   } 
00322 
00323   return img; 
00324 }
00325 
00326 unsigned char * OpenGLDisplayDevice::readpixels_rgba4u(int &x, int &y) {
00327   unsigned char * img;
00328 
00329   x = xSize;
00330   y = ySize;
00331 
00332   if ((img = (unsigned char *) malloc(x * y * 4)) != NULL) {
00333     glPixelStorei(GL_PACK_ALIGNMENT, 1);
00334     glReadPixels(0, 0, x, y, GL_RGBA, GL_UNSIGNED_BYTE, img);
00335   } else {
00336     x = 0;
00337     y = 0;
00338   } 
00339 
00340   return img; 
00341 }
00342 
00343 
00344 // update after drawing
00345 void OpenGLDisplayDevice::update(int do_update) {
00346   if(do_update)
00347     SDL_GL_SwapBuffers();
00348 
00349   glDrawBuffer(GL_BACK);
00350 }
00351 
00352 

Generated on Thu Apr 25 02:43:34 2024 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002