File: gl_util.inl

package info (click to toggle)
python-escript 5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 87,772 kB
  • ctags: 49,550
  • sloc: python: 585,488; cpp: 133,173; ansic: 18,675; xml: 3,283; sh: 690; makefile: 215
file content (395 lines) | stat: -rw-r--r-- 10,586 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 *  Copyright 2008-2013 Steven Dalton
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * @file gl_util.cc
 * Helpful functions for OpenGL applications.
 */

/*
 * David Gleich, Matt Rasmussen, Leonid Zhukov
 * 6 April 2006
 */

#pragma once

#include <map>

#include <cusp/opengl/spy/gl_util.h>

namespace cusp
{
namespace opengl
{
namespace spy
{

/**
 * Provide the height of a GLUT bitmap.
 *
 * This function is provided by the OpenGLUT and FreeGLUT libraries
 * but it not in GLUT proper, here we add it...
 *
 * @param font the GLUT font identifier
 * @return the height of the GLUT font in screen pixels.
 */
int glutBitmapHeight(void* font)
{
    if (font == GLUT_BITMAP_HELVETICA_18)
	{
		return (14);
	}
	else if (font == GLUT_BITMAP_9_BY_15)
	{
		return (9);
	}
	else if (font == GLUT_BITMAP_8_BY_13)
	{
		return (8);
	}

    return (0);
}

/**
 * Get the screen coordinates for a point in the world coordinates.
 * This function works for the currently selected OpenGL 
 * context.
 *
 * @param wx x world coordinate
 * @param wy y world coordinate
 * @param x output x screen coordinate
 * @param y output y screen coordinate
 */
void world_to_screen(float wx, float wy, int *x, int *y)
{
    GLdouble window_x;
    GLdouble window_y;
    GLdouble window_z;
    GLint viewport[4];
    GLdouble modelmatrix[16], projmatrix[16];


    glGetIntegerv(GL_VIEWPORT, viewport);
    glGetDoublev(GL_MODELVIEW_MATRIX, modelmatrix);
    glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
    
    gluProject(wx, wy, 0, modelmatrix, projmatrix, viewport, &window_x, &window_y, &window_z);
    *x = int(window_x);
    *y = int(window_y);
}

/**
 * Convert screen coordinates into world/windows/opengl coordinates.
 *
 * This function works for the currently selected OpenGL 
 * context.
 *
 * @param wx output x world coordinate
 * @param wy output y world coordinate
 * @param x x screen coordinate
 * @param y y screen coordinate
 */

void screen_to_world(int x, int y, double *wx, double *wy)
{
    GLint viewport[4];
    GLdouble modelmatrix[16], projmatrix[16];

    //  OpenGL y coordinate position  
    GLint realy;          
    
    // returned world x, y, z coords  
    GLdouble wz;      
    
    glGetIntegerv(GL_VIEWPORT, viewport);
    glGetDoublev(GL_MODELVIEW_MATRIX, modelmatrix);
    glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);

    int height = glutGet(GLUT_WINDOW_HEIGHT);

    //  note viewport[3] is height of window in pixels  
    realy = viewport[3] - (GLint) y + (height-(viewport[3]-viewport[1]))-viewport[1];
    
    gluUnProject((GLdouble)x, (GLdouble)realy, 0.0, 
                  modelmatrix, projmatrix, viewport, wx, wy, &wz);
}

/**
 * Get the 2d extents of the current screen in world coordinates.
 *
 * The points (x1,y1) and (x2,y2) enclose the viewable area.
 *
 * @param x1 the x coordinate of the world view, upper left of window
 * @param y1 the y coordinate of the world view, upper left of window
 * @param x2 the x coordinate of the world view, lower right of window
 * @param y2 the y coordinate of the world view, lower right of window
 */
void world_extents(float &x1, float &y1, float &x2, float &y2)
{
    GLint viewport[4];
    GLdouble modelmatrix[16], projmatrix[16];

    //  OpenGL y coordinate position  
    GLint x,y;
    GLint realy;          
    
    // returned world x, y, z coords  
    GLdouble wx,wy,wz;
    
    glGetIntegerv(GL_VIEWPORT, viewport);
    glGetDoublev(GL_MODELVIEW_MATRIX, modelmatrix);
    glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);

    int height = glutGet(GLUT_WINDOW_HEIGHT);
    int width = glutGet(GLUT_WINDOW_WIDTH);

    //
    // compute the upper left corner
    //
    x = 0;
    y = 0;

    //  note viewport[3] is height of window in pixels  
    realy = viewport[3] - (GLint) y + (height-(viewport[3]-viewport[1]))-viewport[1];
    
    gluUnProject((GLdouble)x, (GLdouble)realy, 0.0, 
                  modelmatrix, projmatrix, viewport, &wx, &wy, &wz);

    x1 = (float)wx;
    y1 = (float)wy;

    //
    // compute the lower right corner
    //
    x = width;
    y = height;

    //  note viewport[3] is height of window in pixels  
    realy = viewport[3] - (GLint) y + (height-(viewport[3]-viewport[1]))-viewport[1];
    
    gluUnProject((GLdouble)x, (GLdouble)realy, 0.0, 
                  modelmatrix, projmatrix, viewport, &wx, &wy, &wz);

    x2 = (float)wx;
    y2 = (float)wy;
}

/**
 * Scale distance (val) on the screen into world coordinates.  This 
 * function aids finding the world-size of "one-pixel" objects,
 * for example.
 *
 * This function works for the currently selected OpenGL 
 * context.
 *
 * @param val the size on the screen in pixels
 * @return the world size of a val pixels on the screen
 */
float scale_to_world(float val)
{
    GLdouble wx, wy, wx2, wy2;
    screen_to_world(0, 0, &wx, &wy);
    screen_to_world(0, int(val), &wx2, &wy2);
    return float(wy2 - wy);
}

#ifndef GL_UTIL_GLUT_TEXT_FONT
#define GL_UTIL_GLUT_TEXT_FONT GLUT_BITMAP_HELVETICA_18
#endif // GL_UTIL_GLUT_TEXT_FONT

/**
 * Draw a text string using GLUT.
 *
 * To change the DEFAULT font, simply define 
 * GL_UTIL_GLUT_TEXT_FONT
 *
 * @param x the x world coordinate of the text string
 * @param y the y world coordinate of the text string
 * @param string the string to draw
 * @param font an optional parameter for the text font.
 */
void output_text_string(float x, float y, const char *string, void* font)
{
    int len, i;

    glRasterPos2f(x, y);
    len = (int) strlen(string);
    for (i = 0; i < len; i++) 
    {
        glutBitmapCharacter(font, string[i]);
    }
}

/**
 * Get the screen extent of the text string with the current font.
 *
 * To change the DEFAULT font, simply define 
 * GL_UTIL_GLUT_TEXT_FONT
 *
 * @param width the width of the text rendering
 * @param height the height of the text rendering
 * @param string the string to draw
 * @param font an optional parameter for the text font.
 */
void text_extends_screen(int *width, int *height, const char *string, void* font)
{
    int w, h;

    w = glutBitmapLength(font, (const unsigned char*)string);
    h = glutBitmapHeight(font);

    *width = w;
    *height = h;
}

/**
 * Get the world extent of the text string with the current font.
 *
 * To change the DEFAULT font, simply define 
 * GL_UTIL_GLUT_TEXT_FONT
 *
 * @param width the width of the text rendering
 * @param height the height of the text rendering
 * @param string the string to draw
 * @param font an optional parameter for the text font.
 */
void text_extends_world(float *width, float *height, const char *string, void* font)
{
    int w, h;

    w = glutBitmapLength(font, (const unsigned char*)string);
    h = glutBitmapHeight(font);

    double ww,wh;
    double wx0,wy0;

    screen_to_world(0,0,&wx0, &wy0);
    screen_to_world(w,h,&ww, &wh);
    *width = (float)(ww-wx0);
    *height = (float)(wh-wy0);
}


/**
 * Draw a text string to the screen.
 *
 * This function provides a series of options about how to render a text
 * string and is more useful than the output_text_string function.
 *
 * By default, the x and y coordinates are world coordinates, and not
 * screen coordinates.  Also, the flags specify the orientation
 * of the string with respect to the point.
 *
 * GL_U_TEXT_WORLD_COORDS -- default, x and y are world coordinates
 * GL_U_TEXT_SCREEN_COORDS -- x and y are screen coordinates
 *
 * GL_U_TEXT_LEFT_X -- default, x is the left horizatonal coordinate of the text string
 * GL_U_TEXT_CENTER_X -- x is the center horizatonal coordinate of the text string
 * GL_U_TEXT_RIGHT_X -- x is the right horizatonal coordinate of the text string
 *
 * GL_U_TEXT_BOTTOM_Y -- default, y is the bottom vertical coordinate of the text string
 * GL_U_TEXT_CENTER_Y -- y is the center vertical coordinate of hte text string
 * GL_U_TEXT_TOP_Y -- default, y is the top vertical coordinate of the text string
 *
 * @param x the x coordinate of the text string
 * @param y the y coordinate of the text string
 * @param string the text string
 * @param flags how to interpret the text string
 * @param font an optional parameter for the text font.
 */
void draw_text(float x, float y, const char *string, unsigned int flags, void* font)
{
    float wx, wy;

    // first, convert everything to screen coordinates
    if (flags & GL_U_TEXT_SCREEN_COORDS)
    {
        double wx8, wy8;
        screen_to_world((int)x, (int)y, &wx8, &wy8);
        wx = (float)wx8;
        wy = (float)wy8;
    }
    else
    {
        wx = x;
        wy = y;
    }
    
    float str_wf, str_hf;
    text_extends_world(&str_wf, &str_hf, string, font);

    // adjust the screen coordinate based on the text alignment options
    if (flags & GL_U_TEXT_CENTER_X)
    {
        wx -= str_wf/2.f;
    }
    else if (flags & GL_U_TEXT_RIGHT_X)
    {
        wx -= str_wf;
    }

    if (flags & GL_U_TEXT_CENTER_Y)
    {
        wy += str_hf/2.f;
    }
    else if (flags & GL_U_TEXT_TOP_Y)
    {
        wy += str_hf;
    }

    output_text_string(wx, wy, string, font);
}


static std::map<int, void*> gl_util_window_to_data;

/**
 * Get the window data pointer for the glut window with window_id.
 *
 * This pointer must previously have been set, otherwise, you will
 * get NULL returned.
 *
 * @param window_id the glut window_id
 * @return the pointer previously registered with set_window_data
 */
void* get_window_data(int window_id)
{
    return (gl_util_window_to_data[window_id]);
}

/**
 * Set the window data pointer for a glut window.
 *
 * This function allows you to associate a pointer with a GLUT
 * window based on the window id.
 *
 * An easy way to use this function is to keep each window
 * as a class, and register the class "this" pointer as the window
 * data.  Then, you can always convert from a static callback
 * to a class-specific call-back.
 *
 * @param window_id the glut window id
 * @param data the data pointer 
 */
void set_window_data(int window_id, void* data)
{
    gl_util_window_to_data[window_id] = data;
}

} // end spy
} // end opengl
} // end cusp