/[escript]/trunk/cusplibrary/cusp/opengl/spy/detail/gl_util.inl
ViewVC logotype

Contents of /trunk/cusplibrary/cusp/opengl/spy/detail/gl_util.inl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5148 - (show annotations)
Mon Sep 15 01:25:23 2014 UTC (6 years ago) by caltinay
File size: 10586 byte(s)
Merging ripley diagonal storage + CUDA support into trunk.
Options file version has been incremented due to new options
'cuda' and 'nvccflags'.

1 /*
2 * Copyright 2008-2013 Steven Dalton
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /**
18 * @file gl_util.cc
19 * Helpful functions for OpenGL applications.
20 */
21
22 /*
23 * David Gleich, Matt Rasmussen, Leonid Zhukov
24 * 6 April 2006
25 */
26
27 #pragma once
28
29 #include <map>
30
31 #include <cusp/opengl/spy/gl_util.h>
32
33 namespace cusp
34 {
35 namespace opengl
36 {
37 namespace spy
38 {
39
40 /**
41 * Provide the height of a GLUT bitmap.
42 *
43 * This function is provided by the OpenGLUT and FreeGLUT libraries
44 * but it not in GLUT proper, here we add it...
45 *
46 * @param font the GLUT font identifier
47 * @return the height of the GLUT font in screen pixels.
48 */
49 int glutBitmapHeight(void* font)
50 {
51 if (font == GLUT_BITMAP_HELVETICA_18)
52 {
53 return (14);
54 }
55 else if (font == GLUT_BITMAP_9_BY_15)
56 {
57 return (9);
58 }
59 else if (font == GLUT_BITMAP_8_BY_13)
60 {
61 return (8);
62 }
63
64 return (0);
65 }
66
67 /**
68 * Get the screen coordinates for a point in the world coordinates.
69 * This function works for the currently selected OpenGL
70 * context.
71 *
72 * @param wx x world coordinate
73 * @param wy y world coordinate
74 * @param x output x screen coordinate
75 * @param y output y screen coordinate
76 */
77 void world_to_screen(float wx, float wy, int *x, int *y)
78 {
79 GLdouble window_x;
80 GLdouble window_y;
81 GLdouble window_z;
82 GLint viewport[4];
83 GLdouble modelmatrix[16], projmatrix[16];
84
85
86 glGetIntegerv(GL_VIEWPORT, viewport);
87 glGetDoublev(GL_MODELVIEW_MATRIX, modelmatrix);
88 glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
89
90 gluProject(wx, wy, 0, modelmatrix, projmatrix, viewport, &window_x, &window_y, &window_z);
91 *x = int(window_x);
92 *y = int(window_y);
93 }
94
95 /**
96 * Convert screen coordinates into world/windows/opengl coordinates.
97 *
98 * This function works for the currently selected OpenGL
99 * context.
100 *
101 * @param wx output x world coordinate
102 * @param wy output y world coordinate
103 * @param x x screen coordinate
104 * @param y y screen coordinate
105 */
106
107 void screen_to_world(int x, int y, double *wx, double *wy)
108 {
109 GLint viewport[4];
110 GLdouble modelmatrix[16], projmatrix[16];
111
112 // OpenGL y coordinate position
113 GLint realy;
114
115 // returned world x, y, z coords
116 GLdouble wz;
117
118 glGetIntegerv(GL_VIEWPORT, viewport);
119 glGetDoublev(GL_MODELVIEW_MATRIX, modelmatrix);
120 glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
121
122 int height = glutGet(GLUT_WINDOW_HEIGHT);
123
124 // note viewport[3] is height of window in pixels
125 realy = viewport[3] - (GLint) y + (height-(viewport[3]-viewport[1]))-viewport[1];
126
127 gluUnProject((GLdouble)x, (GLdouble)realy, 0.0,
128 modelmatrix, projmatrix, viewport, wx, wy, &wz);
129 }
130
131 /**
132 * Get the 2d extents of the current screen in world coordinates.
133 *
134 * The points (x1,y1) and (x2,y2) enclose the viewable area.
135 *
136 * @param x1 the x coordinate of the world view, upper left of window
137 * @param y1 the y coordinate of the world view, upper left of window
138 * @param x2 the x coordinate of the world view, lower right of window
139 * @param y2 the y coordinate of the world view, lower right of window
140 */
141 void world_extents(float &x1, float &y1, float &x2, float &y2)
142 {
143 GLint viewport[4];
144 GLdouble modelmatrix[16], projmatrix[16];
145
146 // OpenGL y coordinate position
147 GLint x,y;
148 GLint realy;
149
150 // returned world x, y, z coords
151 GLdouble wx,wy,wz;
152
153 glGetIntegerv(GL_VIEWPORT, viewport);
154 glGetDoublev(GL_MODELVIEW_MATRIX, modelmatrix);
155 glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
156
157 int height = glutGet(GLUT_WINDOW_HEIGHT);
158 int width = glutGet(GLUT_WINDOW_WIDTH);
159
160 //
161 // compute the upper left corner
162 //
163 x = 0;
164 y = 0;
165
166 // note viewport[3] is height of window in pixels
167 realy = viewport[3] - (GLint) y + (height-(viewport[3]-viewport[1]))-viewport[1];
168
169 gluUnProject((GLdouble)x, (GLdouble)realy, 0.0,
170 modelmatrix, projmatrix, viewport, &wx, &wy, &wz);
171
172 x1 = (float)wx;
173 y1 = (float)wy;
174
175 //
176 // compute the lower right corner
177 //
178 x = width;
179 y = height;
180
181 // note viewport[3] is height of window in pixels
182 realy = viewport[3] - (GLint) y + (height-(viewport[3]-viewport[1]))-viewport[1];
183
184 gluUnProject((GLdouble)x, (GLdouble)realy, 0.0,
185 modelmatrix, projmatrix, viewport, &wx, &wy, &wz);
186
187 x2 = (float)wx;
188 y2 = (float)wy;
189 }
190
191 /**
192 * Scale distance (val) on the screen into world coordinates. This
193 * function aids finding the world-size of "one-pixel" objects,
194 * for example.
195 *
196 * This function works for the currently selected OpenGL
197 * context.
198 *
199 * @param val the size on the screen in pixels
200 * @return the world size of a val pixels on the screen
201 */
202 float scale_to_world(float val)
203 {
204 GLdouble wx, wy, wx2, wy2;
205 screen_to_world(0, 0, &wx, &wy);
206 screen_to_world(0, int(val), &wx2, &wy2);
207 return float(wy2 - wy);
208 }
209
210 #ifndef GL_UTIL_GLUT_TEXT_FONT
211 #define GL_UTIL_GLUT_TEXT_FONT GLUT_BITMAP_HELVETICA_18
212 #endif // GL_UTIL_GLUT_TEXT_FONT
213
214 /**
215 * Draw a text string using GLUT.
216 *
217 * To change the DEFAULT font, simply define
218 * GL_UTIL_GLUT_TEXT_FONT
219 *
220 * @param x the x world coordinate of the text string
221 * @param y the y world coordinate of the text string
222 * @param string the string to draw
223 * @param font an optional parameter for the text font.
224 */
225 void output_text_string(float x, float y, const char *string, void* font)
226 {
227 int len, i;
228
229 glRasterPos2f(x, y);
230 len = (int) strlen(string);
231 for (i = 0; i < len; i++)
232 {
233 glutBitmapCharacter(font, string[i]);
234 }
235 }
236
237 /**
238 * Get the screen extent of the text string with the current font.
239 *
240 * To change the DEFAULT font, simply define
241 * GL_UTIL_GLUT_TEXT_FONT
242 *
243 * @param width the width of the text rendering
244 * @param height the height of the text rendering
245 * @param string the string to draw
246 * @param font an optional parameter for the text font.
247 */
248 void text_extends_screen(int *width, int *height, const char *string, void* font)
249 {
250 int w, h;
251
252 w = glutBitmapLength(font, (const unsigned char*)string);
253 h = glutBitmapHeight(font);
254
255 *width = w;
256 *height = h;
257 }
258
259 /**
260 * Get the world extent of the text string with the current font.
261 *
262 * To change the DEFAULT font, simply define
263 * GL_UTIL_GLUT_TEXT_FONT
264 *
265 * @param width the width of the text rendering
266 * @param height the height of the text rendering
267 * @param string the string to draw
268 * @param font an optional parameter for the text font.
269 */
270 void text_extends_world(float *width, float *height, const char *string, void* font)
271 {
272 int w, h;
273
274 w = glutBitmapLength(font, (const unsigned char*)string);
275 h = glutBitmapHeight(font);
276
277 double ww,wh;
278 double wx0,wy0;
279
280 screen_to_world(0,0,&wx0, &wy0);
281 screen_to_world(w,h,&ww, &wh);
282 *width = (float)(ww-wx0);
283 *height = (float)(wh-wy0);
284 }
285
286
287 /**
288 * Draw a text string to the screen.
289 *
290 * This function provides a series of options about how to render a text
291 * string and is more useful than the output_text_string function.
292 *
293 * By default, the x and y coordinates are world coordinates, and not
294 * screen coordinates. Also, the flags specify the orientation
295 * of the string with respect to the point.
296 *
297 * GL_U_TEXT_WORLD_COORDS -- default, x and y are world coordinates
298 * GL_U_TEXT_SCREEN_COORDS -- x and y are screen coordinates
299 *
300 * GL_U_TEXT_LEFT_X -- default, x is the left horizatonal coordinate of the text string
301 * GL_U_TEXT_CENTER_X -- x is the center horizatonal coordinate of the text string
302 * GL_U_TEXT_RIGHT_X -- x is the right horizatonal coordinate of the text string
303 *
304 * GL_U_TEXT_BOTTOM_Y -- default, y is the bottom vertical coordinate of the text string
305 * GL_U_TEXT_CENTER_Y -- y is the center vertical coordinate of hte text string
306 * GL_U_TEXT_TOP_Y -- default, y is the top vertical coordinate of the text string
307 *
308 * @param x the x coordinate of the text string
309 * @param y the y coordinate of the text string
310 * @param string the text string
311 * @param flags how to interpret the text string
312 * @param font an optional parameter for the text font.
313 */
314 void draw_text(float x, float y, const char *string, unsigned int flags, void* font)
315 {
316 float wx, wy;
317
318 // first, convert everything to screen coordinates
319 if (flags & GL_U_TEXT_SCREEN_COORDS)
320 {
321 double wx8, wy8;
322 screen_to_world((int)x, (int)y, &wx8, &wy8);
323 wx = (float)wx8;
324 wy = (float)wy8;
325 }
326 else
327 {
328 wx = x;
329 wy = y;
330 }
331
332 float str_wf, str_hf;
333 text_extends_world(&str_wf, &str_hf, string, font);
334
335 // adjust the screen coordinate based on the text alignment options
336 if (flags & GL_U_TEXT_CENTER_X)
337 {
338 wx -= str_wf/2.f;
339 }
340 else if (flags & GL_U_TEXT_RIGHT_X)
341 {
342 wx -= str_wf;
343 }
344
345 if (flags & GL_U_TEXT_CENTER_Y)
346 {
347 wy += str_hf/2.f;
348 }
349 else if (flags & GL_U_TEXT_TOP_Y)
350 {
351 wy += str_hf;
352 }
353
354 output_text_string(wx, wy, string, font);
355 }
356
357
358 static std::map<int, void*> gl_util_window_to_data;
359
360 /**
361 * Get the window data pointer for the glut window with window_id.
362 *
363 * This pointer must previously have been set, otherwise, you will
364 * get NULL returned.
365 *
366 * @param window_id the glut window_id
367 * @return the pointer previously registered with set_window_data
368 */
369 void* get_window_data(int window_id)
370 {
371 return (gl_util_window_to_data[window_id]);
372 }
373
374 /**
375 * Set the window data pointer for a glut window.
376 *
377 * This function allows you to associate a pointer with a GLUT
378 * window based on the window id.
379 *
380 * An easy way to use this function is to keep each window
381 * as a class, and register the class "this" pointer as the window
382 * data. Then, you can always convert from a static callback
383 * to a class-specific call-back.
384 *
385 * @param window_id the glut window id
386 * @param data the data pointer
387 */
388 void set_window_data(int window_id, void* data)
389 {
390 gl_util_window_to_data[window_id] = data;
391 }
392
393 } // end spy
394 } // end opengl
395 } // end cusp

  ViewVC Help
Powered by ViewVC 1.1.26