glwin.c
来自「有限元学习研究用源代码(老外的),供科研人员参考」· C语言 代码 · 共 2,180 行 · 第 1/4 页
C
2,180 行
static char *rcsid= "$Id: glwin.c,v 2.28 1998/09/04 15:25:48 fuhrmann Exp $";
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <X11/cursorfont.h>
#include <GL/glx.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdarg.h>
#include <assert.h>
#include "glwin.h"
#define GLW_BUFSIZE 256
static int debug=0;
static char glw_string_buffer[GLW_BUFSIZE];
#define glwTrace(x) {if (debug) fprintf(stderr,"glwin: %5d: %s\n",__LINE__,x);}
void glwDebug(void)
{
debug=!debug;
if (debug)
fprintf(stderr,"glwin: debugging enabled.\n");
else
fprintf(stderr,"glwin: debugging disabled.\n");
}
struct glWindowStruct
{
int x, y, w, h;
GLenum type;
GLenum dmPolicy;
Window wMain, wOverlay;
Pixmap pixmap;
XVisualInfo *vInfoMain, *vInfoOverlay;
int vInfoMainClass;
int vInfoOverlayClass;
Colormap cMapMain, cMapOverlay;
GLXPixmap glx_pixmap;
GLXContext cMain, cOverlay;
void (*RedrawFunc)(glWindow, void *);
int control_mode;
void (*ExposeFunc)(glWindow, void *,int, int);
void (*ReshapeFunc)(glWindow, void *,int, int);
GLenum (*KeyDownFunc)(glWindow, void *,int, GLenum);
GLenum (*MouseDownFunc)(glWindow, void *,int, int, GLenum);
GLenum (*MouseUpFunc)(glWindow, void *,int, int, GLenum);
GLenum (*MouseMoveFunc)(glWindow, void *,int, int, GLenum);
int lastEventType;
GLenum drawAllowFlag;
Display *xDisplay;
int xScreen;
Window wRoot;
Atom deleteWindowAtom;
char *title,*xtitle,*state;
void * info;
int process;
GLuint font_base[GLW_MAX_FONTS];
int font_to_use;
int keep_aspect;
Cursor wait_cursor;
Cursor user_cursor;
int recording;
int recorded_frame_num;
int frame_num;
FILE *moviepipe;
int skip;
int offscreen;
};
static glWindow CurrentWindow=NULL;
/******************************************************************************/
float colorMaps[] = {
0.000000, 1.000000, 0.000000, 1.000000, 0.000000, 1.000000,
0.000000, 1.000000, 0.333333, 0.776471, 0.443137, 0.556863,
0.443137, 0.556863, 0.219608, 0.666667, 0.666667, 0.333333,
0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333,
0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333,
0.666667, 0.333333, 0.039216, 0.078431, 0.117647, 0.156863,
0.200000, 0.239216, 0.278431, 0.317647, 0.356863, 0.400000,
0.439216, 0.478431, 0.517647, 0.556863, 0.600000, 0.639216,
0.678431, 0.717647, 0.756863, 0.800000, 0.839216, 0.878431,
0.917647, 0.956863, 0.000000, 0.000000, 0.000000, 0.000000,
0.000000, 0.000000, 0.000000, 0.000000, 0.247059, 0.247059,
0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059,
0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039,
0.498039, 0.498039, 0.749020, 0.749020, 0.749020, 0.749020,
0.749020, 0.749020, 0.749020, 0.749020, 1.000000, 1.000000,
1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
0.000000, 0.000000, 0.247059, 0.247059, 0.247059, 0.247059,
0.247059, 0.247059, 0.247059, 0.247059, 0.498039, 0.498039,
0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039,
0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020,
0.749020, 0.749020, 1.000000, 1.000000, 1.000000, 1.000000,
1.000000, 1.000000, 1.000000, 1.000000, 0.000000, 0.000000,
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059,
0.247059, 0.247059, 0.498039, 0.498039, 0.498039, 0.498039,
0.498039, 0.498039, 0.498039, 0.498039, 0.749020, 0.749020,
0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020,
1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
1.000000, 1.000000, 0.000000, 0.000000, 0.000000, 0.000000,
0.000000, 0.000000, 0.000000, 0.000000, 0.247059, 0.247059,
0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059,
0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039,
0.498039, 0.498039, 0.749020, 0.749020, 0.749020, 0.749020,
0.749020, 0.749020, 0.749020, 0.749020, 1.000000, 1.000000,
1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
0.000000, 0.000000, 0.247059, 0.247059, 0.247059, 0.247059,
0.247059, 0.247059, 0.247059, 0.247059, 0.498039, 0.498039,
0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039,
0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020,
0.749020, 0.749020, 1.000000, 1.000000, 1.000000, 1.000000,
1.000000, 1.000000, 1.000000, 1.000000, 0.000000, 0.000000,
1.000000, 1.000000, 0.000000, 0.000000, 1.000000, 1.000000,
0.333333, 0.443137, 0.776471, 0.556863, 0.443137, 0.219608,
0.556863, 0.666667, 0.666667, 0.333333, 0.666667, 0.333333,
0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333,
0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333,
0.039216, 0.078431, 0.117647, 0.156863, 0.200000, 0.239216,
0.278431, 0.317647, 0.356863, 0.400000, 0.439216, 0.478431,
0.517647, 0.556863, 0.600000, 0.639216, 0.678431, 0.717647,
0.756863, 0.800000, 0.839216, 0.878431, 0.917647, 0.956863,
0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726,
0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451,
0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176,
0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000,
0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726,
0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451,
0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176,
0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000,
0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726,
0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451,
0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176,
0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000,
0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726,
0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451,
0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176,
0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000,
0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726,
0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451,
0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176,
0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000,
0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726,
0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451,
0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176,
0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000,
0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726,
0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451,
0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176,
0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000,
0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726,
0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451,
0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176,
0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000,
0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726,
0.854902, 1.000000, 0.000000, 0.000000, 0.000000, 0.000000,
1.000000, 1.000000, 1.000000, 1.000000, 0.333333, 0.443137,
0.443137, 0.219608, 0.776471, 0.556863, 0.556863, 0.666667,
0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333,
0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333,
0.666667, 0.333333, 0.666667, 0.333333, 0.039216, 0.078431,
0.117647, 0.156863, 0.200000, 0.239216, 0.278431, 0.317647,
0.356863, 0.400000, 0.439216, 0.478431, 0.517647, 0.556863,
0.600000, 0.639216, 0.678431, 0.717647, 0.756863, 0.800000,
0.839216, 0.878431, 0.917647, 0.956863, 0.000000, 0.000000,
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
0.000000, 0.000000, 0.247059, 0.247059, 0.247059, 0.247059,
0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059,
0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059,
0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059,
0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059,
0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059,
0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059,
0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039,
0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039,
0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039,
0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039,
0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039,
0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039,
0.498039, 0.498039, 0.498039, 0.498039, 0.749020, 0.749020,
0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020,
0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020,
0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020,
0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020,
0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020,
0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020,
0.749020, 0.749020, 1.000000, 1.000000, 1.000000, 1.000000,
1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
};
float glwRGBMap[8][3] = {
{
0, 0, 0
},
{
1, 0, 0
},
{
0, 1, 0
},
{
1, 1, 0
},
{
0, 0, 1
},
{
1, 0, 1
},
{
0, 1, 1
},
{
1, 1, 1
}
};
/******************************************************************************/
#define glwError(msg) glw_error(__LINE__,msg)
static void glw_error(int line, char * msg)
{
fprintf(stderr,"\nglwin.c: error on line %d: %s\n",line,msg);
}
#if defined(DEC) && defined(vms)
static int ffs (unsigned int mask)
{
int num;
unsigned int bit;
if (mask == 0) return 0;
for (num = 1, bit = 1; (mask & bit) != 0; num++, bit <<= 1) {}
return num;
}
#endif /* DEC */
/******************************************************************************/
int glwGetColorMapSize(glWindow w)
{
return w->vInfoMain->colormap_size;
}
/******************************************************************************/
int glwLoadFont(glWindow w,int ifont,char *fontName)
{
Font id;
int first, last;
XFontStruct *font_info;
if ((ifont<0)&& (ifont>=GLW_MAX_FONTS))
{
glwError("font number out of range\n");
return 0;
}
font_info = XLoadQueryFont(w->xDisplay, fontName);
if (font_info == NULL) {
glwError("font not found\n");
return 0;
}
id = font_info->fid;
first = font_info->min_char_or_byte2;
last = font_info->max_char_or_byte2;
w->font_base[ifont] = glGenLists((GLuint) last+1);
if (w->font_base[ifont] == 0)
{
printf ("out of display lists\n");
return 0;
}
glXUseXFont(id, first, last-first+1, w->font_base[ifont]+first);
/* *height = font_info->ascent + font_info->descent;
*width = font_info->max_bounds.width; */
return 1;
}
void glwPrint(glWindow w,char *s)
{
if (w->font_base[w->font_to_use]==0) return;
glPushAttrib (GL_LIST_BIT);
glListBase(w->font_base[w->font_to_use]);
glCallLists(strlen(s), GL_UNSIGNED_BYTE, (GLubyte *) s);
glPopAttrib ();
}
void glwPrintf(glWindow w,char *format,...)
{
va_list ptr;
va_start(ptr,format);
vsprintf(glw_string_buffer,format,ptr);
va_end(ptr);
glwPrint(w,glw_string_buffer);
}
void glwSelectFont(glWindow w, int font_to_use)
{
if ((font_to_use<0)&& (font_to_use>=GLW_MAX_FONTS))
{
glwError("font number out of range\n");
return;
}
w->font_to_use=font_to_use;
}
/******************************************************************************/
void glwGetMouseLoc(glWindow w,int *x, int *y)
{
int junk;
*x = 0;
*y = 0;
XQueryPointer(w->xDisplay, w->wMain, (Window *)&junk, (Window *)&junk,
&junk, &junk, x, y, (unsigned int *)&junk);
}
/******************************************************************************/
void glwSetFogRamp(glWindow w,int density, int startIndex)
{
XColor c[256];
int rShift, gShift, bShift, intensity, fogValues, colorValues;
int i, j, k;
switch (w->vInfoMainClass) {
case DirectColor:
fogValues = 1 << density;
colorValues = 1 << startIndex;
for (i = 0; i < colorValues; i++) {
for (j = 0; j < fogValues; j++) {
k = i * fogValues + j;
intensity = i * fogValues + j * colorValues;
if (intensity > w->vInfoMain->colormap_size) {
intensity = w->vInfoMain->colormap_size;
}
intensity = (intensity << 8) | intensity;
rShift = ffs((unsigned int)w->vInfoMain->red_mask) - 1;
gShift = ffs((unsigned int)w->vInfoMain->green_mask) - 1;
bShift = ffs((unsigned int)w->vInfoMain->blue_mask) - 1;
c[k].pixel = ((k << rShift) & w->vInfoMain->red_mask) |
((k << gShift) & w->vInfoMain->green_mask) |
((k << bShift) & w->vInfoMain->blue_mask);
c[k].red = (unsigned short)intensity;
c[k].green = (unsigned short)intensity;
c[k].blue = (unsigned short)intensity;
c[k].flags = DoRed | DoGreen | DoBlue;
}
}
XStoreColors(w->xDisplay, w->cMapMain, c, w->vInfoMain->colormap_size);
break;
case GrayScale:
case PseudoColor:
fogValues = 1 << density;
colorValues = 1 << startIndex;
for (i = 0; i < colorValues; i++) {
for (j = 0; j < fogValues; j++) {
k = i * fogValues + j;
intensity = i * fogValues + j * colorValues;
if (intensity > w->vInfoMain->colormap_size) {
intensity = w->vInfoMain->colormap_size;
}
intensity = (intensity << 8) | intensity;
c[k].pixel = k;
c[k].red = (unsigned short)intensity;
c[k].green = (unsigned short)intensity;
c[k].blue = (unsigned short)intensity;
c[k].flags = DoRed | DoGreen | DoBlue;
}
}
XStoreColors(w->xDisplay, w->cMapMain, c, w->vInfoMain->colormap_size);
break;
}
XSync(w->xDisplay, 0);
}
/******************************************************************************/
void glwSetGreyRamp(glWindow w)
{
XColor c[256];
float intensity;
int rShift, gShift, bShift, i;
switch (w->vInfoMainClass) {
case DirectColor:
for (i = 0; i < w->vInfoMain->colormap_size; i++) {
intensity = (float)i / (float)w->vInfoMain->colormap_size *
65535.0 + 0.5;
rShift = ffs((unsigned int)w->vInfoMain->red_mask) - 1;
gShift = ffs((unsigned int)w->vInfoMain->green_mask) - 1;
bShift = ffs((unsigned int)w->vInfoMain->blue_mask) - 1;
c[i].pixel = ((i << rShift) & w->vInfoMain->red_mask) |
((i << gShift) & w->vInfoMain->green_mask) |
((i << bShift) & w->vInfoMain->blue_mask);
c[i].red = (unsigned short)intensity;
c[i].green = (unsigned short)intensity;
c[i].blue = (unsigned short)intensity;
c[i].flags = DoRed | DoGreen | DoBlue;
}
XStoreColors(w->xDisplay, w->cMapMain, c, w->vInfoMain->colormap_size);
break;
case GrayScale:
case PseudoColor:
for (i = 0; i < w->vInfoMain->colormap_size; i++) {
intensity = (float)i / (float)w->vInfoMain->colormap_size *
65535.0 + 0.5;
c[i].pixel = i;
c[i].red = (unsigned short)intensity;
c[i].green = (unsigned short)intensity;
c[i].blue = (unsigned short)intensity;
c[i].flags = DoRed | DoGreen | DoBlue;
}
XStoreColors(w->xDisplay, w->cMapMain, c, w->vInfoMain->colormap_size);
break;
}
XSync(w->xDisplay, 0);
}
/******************************************************************************/
void glwSetOneColor(glWindow w,int index, float r, float g, float b)
{
XColor c;
int rShift, gShift, bShift;
switch (w->vInfoMainClass) {
case DirectColor:
rShift = ffs((unsigned int)w->vInfoMain->red_mask) - 1;
gShift = ffs((unsigned int)w->vInfoMain->green_mask) - 1;
bShift = ffs((unsigned int)w->vInfoMain->blue_mask) - 1;
c.pixel = ((index << rShift) & w->vInfoMain->red_mask) |
((index << gShift) & w->vInfoMain->green_mask) |
((index << bShift) & w->vInfoMain->blue_mask);
c.red = (unsigned short)(r * 65535.0 + 0.5);
c.green = (unsigned short)(g * 65535.0 + 0.5);
c.blue = (unsigned short)(b * 65535.0 + 0.5);
c.flags = DoRed | DoGreen | DoBlue;
XStoreColor(w->xDisplay, w->cMapMain, &c);
break;
case GrayScale:
case PseudoColor:
if (index < w->vInfoMain->colormap_size) {
c.pixel = index;
c.red = (unsigned short)(r * 65535.0 + 0.5);
c.green = (unsigned short)(g * 65535.0 + 0.5);
c.blue = (unsigned short)(b * 65535.0 + 0.5);
c.flags = DoRed | DoGreen | DoBlue;
XStoreColor(w->xDisplay, w->cMapMain, &c);
}
break;
}
XSync(w->xDisplay, 0);
}
/******************************************************************************/
void glwSetOverlayMap(glWindow w, int size, float *rgb)
{
XColor c;
unsigned long *buf;
int max, i;
if (w->vInfoMainClass == PseudoColor) {
max = (size > w->vInfoOverlay->colormap_size) ?
w->vInfoOverlay->colormap_size : size;
buf = (unsigned long *)calloc(max, sizeof(unsigned long));
XAllocColorCells(w->xDisplay, w->cMapOverlay, True, NULL, 0, buf, max-1);
for (i = 1; i < max; i++) {
c.pixel = i;
c.red = (unsigned short)(rgb[i] * 65535.0 + 0.5);
c.green = (unsigned short)(rgb[size+i] * 65535.0 + 0.5);
c.blue = (unsigned short)(rgb[size*2+i] * 65535.0 + 0.5);
c.flags = DoRed | DoGreen | DoBlue;
XStoreColor(w->xDisplay, w->cMapOverlay, &c);
}
free(buf);
}
XSync(w->xDisplay, 0);
}
/******************************************************************************/
void glwSetRGBMap(glWindow w,int size, float *rgb)
{
XColor c;
int rShift, gShift, bShift, max, i;
switch (w->vInfoMainClass) {
case DirectColor:
max = (size > w->vInfoMain->colormap_size) ? w->vInfoMain->colormap_size
: size;
for (i = 0; i < max; i++) {
rShift = ffs((unsigned int)w->vInfoMain->red_mask) - 1;
gShift = ffs((unsigned int)w->vInfoMain->green_mask) - 1;
bShift = ffs((unsigned int)w->vInfoMain->blue_mask) - 1;
c.pixel = ((i << rShift) & w->vInfoMain->red_mask) |
((i << gShift) & w->vInfoMain->green_mask) |
((i << bShift) & w->vInfoMain->blue_mask);
c.red = (unsigned short)(rgb[i] * 65535.0 + 0.5);
c.green = (unsigned short)(rgb[size+i] * 65535.0 + 0.5);
c.blue = (unsigned short)(rgb[size*2+i] * 65535.0 + 0.5);
c.flags = DoRed | DoGreen | DoBlue;
XStoreColor(w->xDisplay, w->cMapMain, &c);
}
break;
case GrayScale:
case PseudoColor:
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?