00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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"
00037
00038
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
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
00086 sdlsrv.windowID = open_window(name, size, loc, argc, argv);
00087 if (!have_window) return FALSE;
00088
00089
00090
00091 if (ext->hasmultisample)
00092 aaAvailable = TRUE;
00093 else
00094 aaAvailable = FALSE;
00095
00096
00097 if (ext->hasmultisample) {
00098 aa_on();
00099
00100
00101 }
00102
00103 cueingAvailable = TRUE;
00104 cueing_on();
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
00115 reshape();
00116 normal();
00117 clear();
00118 update();
00119
00120
00121 return TRUE;
00122 }
00123
00124
00125 OpenGLDisplayDevice::~OpenGLDisplayDevice(void) {
00126 if (have_window) {
00127 free_opengl_ctx();
00128 }
00129
00130 SDL_Quit();
00131 }
00132
00133
00135
00136
00137
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
00152 screenX = 1280;
00153 screenY = 1024;
00154 W = size[0];
00155 H = size[1];
00156 if (loc) {
00157 SX = loc[0];
00158
00159
00160 SY = (screenY - loc[1]) - H;
00161 }
00162 ext->hasstereo = FALSE;
00163 ext->stereodrawforced = FALSE;
00164 ext->hasmultisample = FALSE;
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
00182 setup_initial_opengl_state();
00183
00184
00185 have_window = TRUE;
00186
00187 return 0;
00188 }
00189
00190
00192
00193 void OpenGLDisplayDevice::do_resize_window(int width, int height) {
00194
00195 }
00196
00197 void OpenGLDisplayDevice::do_reposition_window(int xpos, int ypos) {
00198
00199 }
00200
00201
00202
00203
00204
00205
00206 int OpenGLDisplayDevice::x(void) {
00207 int rx;
00208
00209 rx = 0;
00210
00211 return rx;
00212 }
00213
00214
00215
00216 int OpenGLDisplayDevice::y(void) {
00217 int ry;
00218
00219 ry = 0;
00220
00221 return ry;
00222 }
00223
00224
00225 int OpenGLDisplayDevice::shift_state(void) {
00226 int retval = 0;
00227
00228
00229 return retval;
00230 }
00231
00232
00233 int OpenGLDisplayDevice::spaceball(int *rx, int *ry, int *rz, int *tx, int *ty,
00234 int *tz, int *buttons) {
00235
00236 return 0;
00237 }
00238
00239
00240
00241
00242 void OpenGLDisplayDevice::set_cursor(int n) {
00243
00244 }
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254 void OpenGLDisplayDevice::queue_events(void) {
00255 }
00256
00257
00258
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
00278
00279
00280
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
00345 void OpenGLDisplayDevice::update(int do_update) {
00346 if(do_update)
00347 SDL_GL_SwapBuffers();
00348
00349 glDrawBuffer(GL_BACK);
00350 }
00351
00352