Debian Codegraph @

cvd/videodisplay.h

raw symbols SCIP
1 //-*- c++ -*-
2 //////////////////////////////////////////////////////////////////////
3 // //
4 // VideoDisplay - Cheap and cheeful way of creating a GL X //
5 // display //
6 // //
7 // Tom Drummong & Paul Smith 2002 //
8 // //
9 //////////////////////////////////////////////////////////////////////
10
11 #ifndef __VIDEODISPLAY_H
12 #define __VIDEODISPLAY_H
13
14 #include <assert.h>
15 #include <math.h>
16 #include <string>
17
18 #include <GL/gl.h> // OpenGL proper
19 #include <GL/glx.h> // OpenGL (X extensions)
20 #include <X11/Xlib.h> //X11
21
22 #include <cvd/exceptions.h>
23 #include <cvd/image_ref.h>
24
25 namespace CVD
26 {
27
28 namespace Exceptions
29 {
30 /// %Exceptions specific to CVD::VideoDisplay
31 /// @ingroup gException
32 namespace VideoDisplay
33 {
34 /// Base class for all CVD::VideoDisplay exceptions
35 /// @ingroup gException
36 struct All : public CVD::Exceptions::All
37 {
38 using CVD::Exceptions::All::All;
39 };
40
41 /// An exception occurred during initialisation
42 /// @ingroup gException
43 struct InitialisationError : public All
44 {
45 InitialisationError(std::string w); ///< Construct from error string
46 };
47
48 /// An exception occurred during run-time
49 /// @ingroup gException
50 struct RuntimeError : public All
51 {
52 RuntimeError(std::string w); ///< Construct from error string
53 };
54 }
55 }
56
57 /// The default GLX display request, corresponding to the minimum specification Gl display visual (8-bit colour)
58 /// @relates VideoDisplay
59 extern int defAttr[];
60
61 /// A cheap and cheerful GL display window using X and the GLX library.
62 /// VideoDisplay maintains for you the mapping between your choice of local
63 /// GL co-ordinates, and image co-ordinates and provides functions for
64 /// converting between the two. It does not, however, manage any
65 /// X events, for example resize or redraw events. To develop a
66 /// more sophisticated display window, you must select which XEvents to monitor
67 /// using select_events() and then poll using pending() and use get_event()
68 /// to handle events.
69 /// @ingroup gGL
70 class VideoDisplay
71 {
72 struct DoNotMapStruct
73 {
74 DoNotMapStruct() { }
75 };
76
77 public:
78 static const DoNotMapStruct DoNotMap;
79
80 /// Construct (and display) a display window
81 /// @param left The local GL co-ordinate of the left of the window
82 /// @param top The local GL co-ordinate of the top of the window
83 /// @param right The local GL co-ordinate of the right of the window
84 /// @param bottom The local GL co-ordinate of bottom of the window
85 /// @param scale The number of image pixels per GL unit (default 1:1)
86 /// @param visualAttr The attributes passed to glXChooseVisual
87 /// @param map Whether or not to map the window
88 VideoDisplay(double left, double top, double right, double bottom, double scale = 1, int* visualAttr = defAttr, bool map = true);
89
90 /// Construct (and display) a display window
91 /// @param size The GL co-ordinate window goes from (0,0) to (size.x, size.y)
92 /// @param map Whether or not to map the window
93 /// @param visualAttr The attributes passed to glXChooseVisual
94 VideoDisplay(ImageRef size, const DoNotMapStruct&, int* visualAttr = defAttr);
95
96 /// Construct (and display) a display window
97 /// @param size The GL co-ordinate window goes from (0,0) to (size.x, size.y)
98 /// @param scale The number of image pixels per GL unit (default 1:1)
99 /// @param visualAttr The attributes passed to glXChooseVisual
100 VideoDisplay(ImageRef size, double scale = 1, int* visualAttr = defAttr);
101
102 /// Destructor. This also removes the window from the display
103 ~VideoDisplay();
104
105 /// Set the window title.
106 /// @param s The new title
107 void set_title(const std::string& s);
108
109 /// Returns the connection number for this display
110 int get_fd() { return ConnectionNumber(my_display); }
111 /// Select which X events to handle (by default, nothing is handled)
112 /// @param event_mask ORed list of XEvents to handle
113 void select_events(long event_mask);
114 /// Get the next X event from the queue by calling XNextEvent.
115 /// If the queue is empty, this function blocks until the next event
116 /// @param event The next event
117 void get_event(XEvent* event);
118 /// Where is the mouse pointer in the window?
119 /// @param x The x co-ordinate
120 /// @param y The y co-ordinate
121 void locate_display_pointer(int& x, int& y);
122 /// Where is the mouse pointer in local GL co-ordinates?
123 /// @param x The x co-ordinate
124 /// @param y The y co-ordinate
125 void locate_video_pointer(double& x, double& y);
126 /// Flushes the output buffer by calling XFlush
127 void flush();
128 /// How many events are waiting the queue?
129 int pending();
130
131 /// Set the desired mapping between local GL and image co-ordinates
132 /// @param left The local GL co-ordinate of the left of the window
133 /// @param top The local GL co-ordinate of the top of the window
134 /// @param right The local GL co-ordinate of the right of the window
135 /// @param bottom The local GL co-ordinate of bottom of the window
136 /// @param scale The number if image pixels per GL unit
137 void set_zoom(double left, double top, double right, double bottom, double scale);
138 /// Change the image mapping to zoom in around the specified point
139 /// @param cx The x co-ordinate of the zoom centre
140 /// @param cy The y co-ordinate of the zoom centre
141 /// @param factor The zoom factor (default 2)
142 void zoom_in(double cx, double cy, double factor = 2);
143 /// Change the image mapping to zoom out around the specified point
144 /// @param cx The x co-ordinate of the zoom centre
145 /// @param cy The y co-ordinate of the zoom centre
146 /// @param factor The zoom factor (default 2)
147 void zoom_out(double cx, double cy, double factor = 2)
148 {
149 assert(factor != 0.0);
150 zoom_in(cx, cy, 1 / factor);
151 }
152 /// Reset the image mapping to that when the VideoDisplay was created
153 void zoom_reset()
154 {
155 set_zoom(my_orig_left, my_orig_top, my_orig_right, my_orig_bottom, my_orig_scale);
156 }
157
158 /// What is the width of the local GL co-ordinates displayed?
159 double video_width() const { return fabs(my_right - my_left); }
160 /// What is the height of the local GL co-ordinates displayed?
161 double video_height() const { return fabs(my_bottom - my_top); }
162
163 /// Convert image co-ordinates into local GL co-ordinates
164 /// @param dx The image x co-ordinate to convert
165 /// @param dy The image y co-ordinate to convert
166 /// @param vx The returned local GL x co-ordinate
167 /// @param vy The returned local GL y co-ordinate
168 void display_to_video(int dx, int dy, double& vx, double& vy);
169 /// Convert local GL co-ordinates into image co-ordinates
170 /// @param dx The local GL x co-ordinate to convert
171 /// @param dy The local GL y co-ordinate to convert
172 /// @param vx The returned image x co-ordinate
173 /// @param vy The returned image y co-ordinate
174 void video_to_display(double dx, double dy, int& vx, int& vy);
175
176 /// Make this window the current GL context. If there is more than one
177 /// GL window being used in your program, this function should be called
178 /// before drawing to ensure you draw to the correct window.
179 void make_current();
180
181 /// Swap the front and back GL buffers. This calls glXSwapBuffers for the
182 /// current context.
183 void swap_buffers();
184 /// What is my display?
185 Display* display() { return my_display; }
186 /// Which is my window?
187 Window window() { return my_window; }
188
189 private:
190 double my_left;
191 double my_top;
192 double my_right;
193 double my_bottom;
194 bool my_positive_right;
195 bool my_positive_down;
196 double my_scale;
197 double my_orig_left;
198 double my_orig_top;
199 double my_orig_right;
200 double my_orig_bottom;
201 double my_orig_scale;
202 Display* my_display;
203 XVisualInfo* my_visual;
204 GLXContext my_glx_context;
205 Colormap my_cmap;
206 Window my_window;
207
208 VideoDisplay(VideoDisplay& copyof);
209 int operator=(VideoDisplay& copyof);
210
211 void init(double, double, double, double, double, int* visualAttr, bool);
212 };
213
214 } // CVD
215
216 #endif