| 1 | ////////////////////////////////////////////////////////////////////// |
| 2 | // // |
| 3 | // VideoDisplay - Cheap and cheeful way of creating a GL X // |
| 4 | // display // |
| 5 | // // |
| 6 | // Tom Drummong & Paul Smith 2002 // |
| 7 | // // |
| 8 | ////////////////////////////////////////////////////////////////////// |
| 9 | |
| 10 | #include "cvd/videodisplay.h" |
| 11 | |
| 12 | //#include <cmath> |
| 13 | //#include <cstdlib> |
| 14 | //#include <iostream> |
| 15 | //#include <cassert> |
| 16 | |
| 17 | #include <assert.h> |
| 18 | #include <iostream> |
| 19 | #include <math.h> |
| 20 | #include <stdlib.h> |
| 21 | |
| 22 | using namespace std; |
| 23 | using namespace CVD; |
| 24 | |
| 25 | CVD::Exceptions::VideoDisplay::InitialisationError::InitialisationError(string w) |
| 26 | : CVD::Exceptions::VideoDisplay::All("VideoDisplay inintialisation error: " + w) |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | CVD::Exceptions::VideoDisplay::RuntimeError::RuntimeError(string w) |
| 31 | : CVD::Exceptions::VideoDisplay::All("VideoDisplay error: " + w) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | //namespace CVD { |
| 36 | int CVD::defAttr[] = { GLX_RGBA, |
| 37 | GLX_RED_SIZE, 3, |
| 38 | GLX_GREEN_SIZE, 3, |
| 39 | GLX_BLUE_SIZE, 2, |
| 40 | GLX_DOUBLEBUFFER, 0, |
| 41 | // GLX_DEPTH_SIZE, 8, |
| 42 | // GLX_STENCIL_SIZE, 8, |
| 43 | 0 }; |
| 44 | |
| 45 | CVD::VideoDisplay::VideoDisplay(double left, double top, double right, double bottom, double scale, int* visualAttr, bool map) |
| 46 | : my_left(left) |
| 47 | , my_top(top) |
| 48 | , my_right(right) |
| 49 | , my_bottom(bottom) |
| 50 | , my_scale(scale) |
| 51 | , my_orig_left(left) |
| 52 | , my_orig_top(top) |
| 53 | , my_orig_right(right) |
| 54 | , my_orig_bottom(bottom) |
| 55 | , my_orig_scale(scale) |
| 56 | { |
| 57 | init(my_left, my_top, my_right, my_bottom, scale, visualAttr, map); |
| 58 | } |
| 59 | |
| 60 | CVD::VideoDisplay::VideoDisplay(ImageRef s, double scale, int* visualAttr) |
| 61 | : my_left(0) |
| 62 | , my_top(0) |
| 63 | , my_right(s.x) |
| 64 | , my_bottom(s.y) |
| 65 | , my_scale(scale) |
| 66 | , my_orig_left(0) |
| 67 | , my_orig_top(0) |
| 68 | , my_orig_right(s.x) |
| 69 | , my_orig_bottom(s.y) |
| 70 | , my_orig_scale(scale) |
| 71 | { |
| 72 | init(my_left, my_top, my_right, my_bottom, scale, visualAttr, true); |
| 73 | } |
| 74 | |
| 75 | CVD::VideoDisplay::VideoDisplay(ImageRef s, const DoNotMapStruct&, int* visualAttr) |
| 76 | : my_left(0) |
| 77 | , my_top(0) |
| 78 | , my_right(s.x) |
| 79 | , my_bottom(s.y) |
| 80 | , my_scale(1.0) |
| 81 | , my_orig_left(0) |
| 82 | , my_orig_top(0) |
| 83 | , my_orig_right(s.x) |
| 84 | , my_orig_bottom(s.y) |
| 85 | , my_orig_scale(1.0) |
| 86 | { |
| 87 | init(my_left, my_top, my_right, my_bottom, 1.0, visualAttr, false); |
| 88 | } |
| 89 | |
| 90 | void CVD::VideoDisplay::init(double left, double top, double right, double bottom, double scale, int* visualAttr, bool map) |
| 91 | { |
| 92 | // Need these for converting mouse clicks efficiently |
| 93 | // (This stays the same even when zooming) |
| 94 | my_positive_right = right > left; |
| 95 | my_positive_down = bottom > top; |
| 96 | |
| 97 | my_display = XOpenDisplay(NULL); |
| 98 | if(my_display == NULL) |
| 99 | throw Exceptions::VideoDisplay::InitialisationError(string("Can't connect to display ") + getenv("DISPLAY")); |
| 100 | |
| 101 | // get an appropriate visual |
| 102 | my_visual = glXChooseVisual(my_display, DefaultScreen(my_display), visualAttr); |
| 103 | if(my_visual == NULL) |
| 104 | throw Exceptions::VideoDisplay::InitialisationError(string("No matching visual on display ") + getenv("DISPLAY")); |
| 105 | |
| 106 | // check I got an rgba visual (32 bit) |
| 107 | int isRgba; |
| 108 | (void)glXGetConfig(my_display, my_visual, GLX_RGBA, &isRgba); |
| 109 | assert(isRgba); |
| 110 | |
| 111 | // create a GLX context |
| 112 | if((my_glx_context = glXCreateContext(my_display, my_visual, 0, GL_TRUE)) == NULL) |
| 113 | throw Exceptions::VideoDisplay::InitialisationError(string("Cannot create GL context on ") + getenv("DISPLAY")); |
| 114 | |
| 115 | // create a colormap (it's empty for rgba visuals) |
| 116 | my_cmap = XCreateColormap(my_display, |
| 117 | RootWindow(my_display, my_visual->screen), |
| 118 | my_visual->visual, |
| 119 | AllocNone); |
| 120 | |
| 121 | // set the attributes for the window |
| 122 | XSetWindowAttributes swa; |
| 123 | swa.colormap = my_cmap; |
| 124 | swa.border_pixel = 0; |
| 125 | swa.event_mask = StructureNotifyMask; |
| 126 | |
| 127 | // Work out how big the display is |
| 128 | int xsize = static_cast<int>(fabs(right - left) * scale); |
| 129 | int ysize = static_cast<int>(fabs(bottom - top) * scale); |
| 130 | // create a window |
| 131 | my_window = XCreateWindow(my_display, |
| 132 | RootWindow(my_display, my_visual->screen), |
| 133 | 0, // init_x |
| 134 | 0, // init_y |
| 135 | xsize, |
| 136 | ysize, |
| 137 | 0, |
| 138 | my_visual->depth, |
| 139 | InputOutput, |
| 140 | my_visual->visual, |
| 141 | CWBorderPixel | CWColormap | CWEventMask, &swa); |
| 142 | |
| 143 | if(my_window == 0) |
| 144 | throw Exceptions::VideoDisplay::InitialisationError(string("Cannot create a window on ") + getenv("DISPLAY")); |
| 145 | |
| 146 | set_title("Video Display"); |
| 147 | |
| 148 | if(map) |
| 149 | { |
| 150 | XMapWindow(my_display, my_window); |
| 151 | |
| 152 | // discard all event up to the MapNotify |
| 153 | XEvent ev; |
| 154 | do |
| 155 | { |
| 156 | XNextEvent(my_display, &ev); |
| 157 | } while(ev.type != MapNotify); |
| 158 | } |
| 159 | |
| 160 | // Connect the GLX context to the window |
| 161 | if(!glXMakeCurrent(my_display, my_window, my_glx_context)) |
| 162 | throw Exceptions::VideoDisplay::InitialisationError("Cannot make window current to GL context"); |
| 163 | |
| 164 | XSelectInput(my_display, |
| 165 | my_window, |
| 166 | ButtonPressMask | ButtonReleaseMask | Button1MotionMask | PointerMotionHintMask | KeyPressMask); |
| 167 | |
| 168 | // Sort out the GL scaling |
| 169 | glLoadIdentity(); |
| 170 | glViewport(0, 0, xsize, ysize); |
| 171 | |
| 172 | glColor3f(1.0, 1.0, 1.0); |
| 173 | |
| 174 | glDrawBuffer(GL_FRONT); // On Vino, we only use the front buffer |
| 175 | |
| 176 | set_zoom(left, top, right, bottom, scale); |
| 177 | } |
| 178 | |
| 179 | void CVD::VideoDisplay::set_zoom(double left, double top, double right, double bottom, double scale) |
| 180 | { |
| 181 | // Make sure that this is the current window |
| 182 | make_current(); |
| 183 | |
| 184 | my_left = left; |
| 185 | my_top = top; |
| 186 | my_right = right; |
| 187 | my_bottom = bottom; |
| 188 | my_scale = scale; |
| 189 | |
| 190 | glMatrixMode(GL_MODELVIEW); |
| 191 | glLoadIdentity(); |
| 192 | |
| 193 | glMatrixMode(GL_PROJECTION); |
| 194 | |
| 195 | glLoadIdentity(); |
| 196 | glOrtho(left, right, bottom, top, -1, 1); // If the origin is the top left |
| 197 | |
| 198 | glRasterPos2d(left, top); |
| 199 | |
| 200 | // video is now the same way as upside down to graphics! |
| 201 | glPixelZoom(static_cast<float>(scale), static_cast<float>(-scale)); |
| 202 | //glPixelZoom(1,-1); |
| 203 | } |
| 204 | |
| 205 | void CVD::VideoDisplay::zoom_in(double cx, double cy, double factor) |
| 206 | { |
| 207 | double width = my_right - my_left; |
| 208 | double height = my_bottom - my_top; |
| 209 | |
| 210 | //Work out the new half-widths |
| 211 | width = width / 2 / factor; |
| 212 | height = height / 2 / factor; |
| 213 | |
| 214 | // Work out the new image corners. scale is how much things are zoomed up, so |
| 215 | // this is just multipled by the factor |
| 216 | set_zoom(cx - width, cy - height, cx + width, cy + height, my_scale * factor); |
| 217 | } |
| 218 | |
| 219 | CVD::VideoDisplay::~VideoDisplay() |
| 220 | { |
| 221 | glXMakeCurrent(my_display, None, NULL); |
| 222 | glXDestroyContext(my_display, my_glx_context); |
| 223 | |
| 224 | XUnmapWindow(my_display, my_window); |
| 225 | XDestroyWindow(my_display, my_window); |
| 226 | XFlush(my_display); |
| 227 | XCloseDisplay(my_display); |
| 228 | } |
| 229 | |
| 230 | void CVD::VideoDisplay::set_title(const string& s) |
| 231 | { |
| 232 | // give the window a name |
| 233 | XStoreName(my_display, my_window, s.c_str()); |
| 234 | } |
| 235 | |
| 236 | void CVD::VideoDisplay::select_events(long event_mask) |
| 237 | { |
| 238 | XSelectInput(my_display, my_window, event_mask); |
| 239 | XFlush(my_display); |
| 240 | } |
| 241 | |
| 242 | void CVD::VideoDisplay::flush() |
| 243 | { |
| 244 | XFlush(my_display); |
| 245 | } |
| 246 | |
| 247 | void CVD::VideoDisplay::get_event(XEvent* event) |
| 248 | { |
| 249 | XNextEvent(my_display, event); |
| 250 | } |
| 251 | |
| 252 | int CVD::VideoDisplay::pending() |
| 253 | { |
| 254 | return XPending(my_display); |
| 255 | } |
| 256 | |
| 257 | void CVD::VideoDisplay::make_current() |
| 258 | { |
| 259 | // Connect the GLX context to the window |
| 260 | if(!glXMakeCurrent(my_display, my_window, my_glx_context)) |
| 261 | throw Exceptions::VideoDisplay::InitialisationError("Cannot make window current to GL context"); |
| 262 | } |
| 263 | |
| 264 | // |
| 265 | // DISPLAY TO VIDEO |
| 266 | // Converts a point on the display to the video's coordinates |
| 267 | // |
| 268 | void CVD::VideoDisplay::display_to_video(int dx, int dy, double& vx, double& vy) |
| 269 | { |
| 270 | vx = dx / my_scale; |
| 271 | if(my_positive_right) |
| 272 | vx = my_left + vx; |
| 273 | else |
| 274 | vx = my_left - vx; |
| 275 | |
| 276 | vy = dy / my_scale; |
| 277 | if(my_positive_down) |
| 278 | vy = my_top + vy; |
| 279 | else |
| 280 | vy = my_top - vy; |
| 281 | } |
| 282 | |
| 283 | // |
| 284 | // VIDEO TO DISPLAY |
| 285 | // Converts a point on the display to the video's coordinates |
| 286 | // |
| 287 | void CVD::VideoDisplay::video_to_display(double vx, double vy, int& dx, int& dy) |
| 288 | { |
| 289 | double ix, iy; |
| 290 | if(my_positive_right) |
| 291 | ix = vx - my_left; |
| 292 | else |
| 293 | ix = my_left - vx; |
| 294 | ix = ix * my_scale; |
| 295 | |
| 296 | if(my_positive_down) |
| 297 | iy = vy - my_top; |
| 298 | else |
| 299 | iy = my_top - vy; |
| 300 | iy = iy * my_scale; |
| 301 | |
| 302 | dx = static_cast<int>(ix); |
| 303 | dy = static_cast<int>(iy); |
| 304 | } |
| 305 | |
| 306 | // |
| 307 | // LOCATE DISPLAY POINTER |
| 308 | // Where is the pointer in the window? |
| 309 | // |
| 310 | void CVD::VideoDisplay::locate_display_pointer(int& x, int& y) |
| 311 | { |
| 312 | Window root_return; |
| 313 | Window child_return; |
| 314 | int root_x_return; |
| 315 | int root_y_return; |
| 316 | unsigned int mask_return; |
| 317 | |
| 318 | XQueryPointer(my_display, |
| 319 | my_window, |
| 320 | &root_return, |
| 321 | &child_return, |
| 322 | &root_x_return, |
| 323 | &root_y_return, |
| 324 | &x, |
| 325 | &y, |
| 326 | &mask_return); |
| 327 | } |
| 328 | |
| 329 | // |
| 330 | // LOCATE DISPLAY POINTER |
| 331 | // Where is the pointer in the video frame? |
| 332 | // |
| 333 | void CVD::VideoDisplay::locate_video_pointer(double& x, double& y) |
| 334 | { |
| 335 | int dx, dy; |
| 336 | locate_display_pointer(dx, dy); |
| 337 | display_to_video(dx, dy, x, y); |
| 338 | } |
| 339 | |
| 340 | void CVD::VideoDisplay::swap_buffers() |
| 341 | { |
| 342 | glXSwapBuffers(my_display, glXGetCurrentDrawable()); |
| 343 | } |
| 344 | |
| 345 | //} // namespace CVD |
| 346 | |
| 347 | const VideoDisplay::DoNotMapStruct VideoDisplay::DoNotMap; |