File: glcanvas.c

package info (click to toggle)
libforms 1.0.93sp1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,548 kB
  • ctags: 9,107
  • sloc: ansic: 97,227; sh: 9,236; makefile: 858
file content (402 lines) | stat: -rw-r--r-- 9,701 bytes parent folder | download | duplicates (2)
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
396
397
398
399
400
401
402
/*
 *  This file is part of the XForms library package.
 *
 * XForms is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1, or
 * (at your option) any later version.
 *
 * XForms is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with XForms.  If not, see <http://www.gnu.org/licenses/>.
 */


/**
 * \file glcanvas.c
 *
 *  This file is part of the XForms library package.
 *  Copyright (c) 1996-2002  T.C. Zhao and Mark Overmars
 *  All rights reserved.
 *
 *  A sample implementation of OpenGL/Mesa canvas class.
 *
 *  For typical objects, ob->spec is the place for the
 *  object specific info. For OpenGL canvas, however, the
 *  ob->spec is already used by the generic canvas. We use
 *  ob->c_vdata to hang the OpenGL specific stuff.
 *
 *  See ../DEMOS/gl.c for an example use of glcanvas.
 *  See ../DEMOS/glwin.c for an example use of fl_glwinopen
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "GL/glx.h"
#include "GL/gl.h"
#include "include/forms.h"
#include "glcanvas.h"
#include <stdlib.h>

#define MAXATTRIB  34

typedef struct
{
    XVisualInfo * xvinfo;
    GLXContext    context;
    int           direct;
    int           glconfig[ MAXATTRIB ];
} CSPEC;


/***************************************
 * Default GL canvas configuration. The user can modify this default
 * using fl_set_glcanvas_defaults(const int *config) or
 * fl_set_glcanvas_attributes(FL_OBJECT *, const int *config)
 ***************************************/

static int glconfig[ MAXATTRIB ] =
{
    GLX_RGBA,
    GLX_DEPTH_SIZE, 1,
    GLX_RED_SIZE,   1,
    GLX_GREEN_SIZE, 1,
    GLX_BLUE_SIZE,  1,
    GLX_DOUBLEBUFFER,
    None
};

#define GLPROP( ob )   ( ( CSPEC * ) ( ob )->c_vdata )
#define ISGLC( ob )    ( ( ob ) && ( ob )->objclass == FL_GLCANVAS )

static int glx_init( FL_OBJECT * );
static int glx_activate( FL_OBJECT * );
static int glx_cleanup( FL_OBJECT * );
static void copy_attributes( int *,
                             const int * );


/***************************************
 * Add a GL canvas to the form
 ***************************************/

FL_OBJECT *
fl_add_glcanvas( int          type,
                 FL_Coord     x,
                 FL_Coord     y,
                 FL_Coord     w,
                 FL_Coord     h,
                 const char * label )
{
    FL_OBJECT * ob = fl_create_glcanvas( type, x, y, w, h, label );

    fl_add_object( fl_current_form, ob );
    return ob;
}


/***************************************
 * Modify the global defaults
 ***************************************/

void
fl_set_glcanvas_defaults( const int * config )
{
    copy_attributes( glconfig, config );
}


/***************************************
 ***************************************/

void
fl_get_glcanvas_defaults( int * config )
{
    copy_attributes( config, glconfig );
}


/***************************************
 * Modify the default configuration of a particular canvas
 ***************************************/

void
fl_set_glcanvas_attributes( FL_OBJECT * ob,
                            const int * config )
{
    if ( ! ISGLC( ob ) )
        fprintf( stderr, "object %s is not glcanvas\n",
                 ob ? ob->label : "null" );

    copy_attributes( GLPROP( ob )->glconfig, config );

    if ( ob->visible )
    {
        fl_hide_object( ob );
        fl_show_object( ob );
    }
}


/***************************************
 ***************************************/

void
fl_get_glcanvas_attributes( FL_OBJECT * ob,
                            int       * attributes )
{
    copy_attributes( attributes, GLPROP( ob )->glconfig );
}


/***************************************
 ***************************************/

void
fl_set_glcanvas_direct( FL_OBJECT * ob,
                        int         direct )
{
    if ( direct != GLPROP( ob )->direct )
    {
        GLPROP( ob )->direct = direct ? GL_TRUE : GL_FALSE;
        if ( ob->visible )
        {
            fl_hide_object( ob );
            fl_show_object( ob );
        }
    }
}


/***************************************
 ***************************************/

GLXContext
fl_get_glcanvas_context( FL_OBJECT * ob )
{
    return GLPROP( ob )->context;
}


/***************************************
 ***************************************/

XVisualInfo *
fl_get_glcanvas_xvisualinfo( FL_OBJECT * ob )
{
    return GLPROP( ob )->xvinfo;
}


/***************************************
 ***************************************/

void
fl_activate_glcanvas( FL_OBJECT * ob )
{
    if ( FL_ObjWin( ob ) )
        glx_activate( ob );
}


/***************************************
 * Interface routines
 ***************************************/

FL_OBJECT *
fl_create_glcanvas( int          type,
                    FL_Coord     x,
                    FL_Coord     y,
                    FL_Coord     w,
                    FL_Coord     h,
                    const char * label )
{
    FL_OBJECT *ob = fl_create_canvas( type, x, y, w, h, label );

    ob->objclass = FL_GLCANVAS;
    fl_modify_canvas_prop( ob, glx_init, glx_activate, glx_cleanup );

    /* Initialize glcanvas specific stuff */

    ob->c_vdata = fl_calloc( 1, sizeof( CSPEC ) );
    memcpy( GLPROP( ob )->glconfig, glconfig, sizeof glconfig );
    GLPROP( ob )->direct = GL_TRUE;

    return ob;
}


/***************************************
 * Initialize the OpenGL stuff before window creation
 ***************************************/

static int
glx_init( FL_OBJECT * ob )
{
    XVisualInfo *vi;
    GLXContext context;

    /* Query for OpenGL capabilities */

    if ( ! glXQueryExtension( fl_display, 0, 0 ) )
    {
        fprintf( stderr, "GLCanvas: OpenGL not supported\n" );
        return -1;
    }

    /* Quit if we can't get a visual */

    if ( ! ( vi = glXChooseVisual( fl_display, fl_screen,
                                   GLPROP( ob )->glconfig ) ) )
    {
        fprintf( stderr, "GLCanvas: Can't get visual\n" );
        return -1;
    }

    /* Change canvas defaults */

    fl_set_canvas_visual( ob, vi->visual );
    fl_set_canvas_depth( ob, vi->depth );
    fl_set_canvas_colormap( ob, fl_create_colormap( vi, 1 ) );

    context = glXCreateContext( fl_display, vi, None, GLPROP( ob )->direct );

    if ( ! context )
    {
        fprintf( stderr, "Can't create GLX context!\n" );
        return -1;
    }

    /* Under some conditions the parent of the gl canvas might go away,
       leaving the old context and vi hanging. */

    glx_cleanup( ob );

    GLPROP( ob )->context = context;
    GLPROP( ob )->xvinfo = vi;

    return 0;
}


/***************************************
 * This routine is called after a valid window is created. It simply
 * binds the OpenGL context to the canvas window
 ***************************************/

static int
glx_activate( FL_OBJECT * ob )
{
    glXMakeCurrent( fl_display, FL_ObjWin( ob ), GLPROP( ob )->context );
    return 0;
}


/***************************************
 * Cleanup is called before destroying the window.  Might be called
 * more than once
 ***************************************/

static int
glx_cleanup( FL_OBJECT * ob )
{
    if ( GLPROP( ob )->context )
    {
        glXDestroyContext( fl_display, GLPROP( ob )->context );
        XFree( GLPROP( ob )->xvinfo );
        GLPROP( ob )->context = 0;
        GLPROP( ob )->xvinfo = 0;
    }

    return 0;
}


/***************************************
 ***************************************/

static void
copy_attributes( int *       dest,
                 const int * src )
{
    while ( *src != None )
        *dest++ = *src++;
    *dest = None;
}


/*************************************************************
 * Open a top-level OpenGL window.
 *
 * fl_winset won't work for switching OpenGL windows.
 * Use glXMakeCurrent
 ************************************************************/

Window
fl_glwincreate( int *        config,
                GLXContext * context,
                int          w,
                int          h )
{
    XVisualInfo *xvi;
    XSetWindowAttributes xswa;
    unsigned int mask;
    Window win;

    if ( ! glXQueryExtension( fl_display, 0, 0 ) )
        return None;

    if ( ! ( xvi = glXChooseVisual( fl_display, fl_screen,
                                    config ? config : glconfig ) ) )
        return None;

    *context = glXCreateContext( fl_display, xvi, None, GL_TRUE );

    xswa.colormap = fl_create_colormap( xvi, 1 );
    xswa.border_pixel = 0;
    mask = CWColormap | CWBorderPixel;

    xswa.event_mask = ExposureMask | StructureNotifyMask;
    mask |= CWEventMask;

    win = XCreateWindow( fl_display, RootWindow( fl_display, fl_screen ),
                         0, 0, w, h, 0,
                         xvi->depth, InputOutput,
                         xvi->visual, mask, &xswa );
    if ( win )
        glXMakeCurrent( fl_display, win, *context );

    return win;
}


/***************************************
 ***************************************/

Window
fl_glwinopen( int *        config,
              GLXContext * context,
              int          w,
              int          h )
{
    Window win;

    if ( ( win = fl_glwincreate( config, context, w, h ) ) )
        fl_winshow( win );

    return win;
}


/*
 * Local variables:
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */