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
|
/*
*
* 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; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*
*/
/*
* $Id: gl.c,v 0.88 1998/02/22 05:24:46 zhao Beta $
*.
* This file is part of the XForms library package.
* Copyright (c) 1996-2002 T.C. Zhao
* 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
*
*/
#if defined(F_ID) || defined(DEBUG)
char *fl_id_gl = "$Id: gl.c,v 0.88 1998/02/22 05:24:46 zhao Beta $";
#endif
#include "GL/glx.h"
#include "GL/gl.h"
#include "forms.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 *), 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;
}
if (!(vi = glXChooseVisual(fl_display, fl_screen, GLPROP(ob)->glconfig)))
{
/* if can't get a visual, quit */
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 mask;
Window win;
if (!glXQueryExtension(fl_display, 0, 0))
return 0;
if (!(xvi = glXChooseVisual(fl_display, fl_screen, config ? config : glconfig)))
return 0;
*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;
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;
}
|