📄 glwin.c
字号:
static char *rcsid= "$Id: glwin.c,v 2.49 2000/07/27 12:43:36 schmelze 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#define GLW_FEEDBACK_BUFSIZE 1000000#define GLW_FEEDBACK_MAX_STRINGS 1000#define GLW_MAX_FONT_SIZE 50#define GLW_FONT_NAME "-*-helvetica-*-r-normal-*-%d-*-*-*-*-*-iso8859-1"#define GLW_DEFAULT_FONT_SIZE 0.035static 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, wToplevel; Pixmap pixmap; XVisualInfo *vInfoMain, *vInfoOverlay; int vInfoMainClass; int vInfoOverlayClass; Colormap cMapMain, cMapOverlay; GLXPixmap glx_pixmap; GLXContext cMain, cOverlay; int isMesa; 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 *display; int screen; Window root; Atom deleteWindowAtom; char *title,*xtitle,*state; void * info; int process; GLuint font_base[GLW_MAX_FONT_SIZE]; GLuint font_range[GLW_MAX_FONT_SIZE]; double font_size; 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 fb_mode; int fb_num_strings; char *fb_strings[GLW_FEEDBACK_MAX_STRINGS]; double fb_string_size[GLW_FEEDBACK_MAX_STRINGS]; GLfloat *fb_buffer; int fb_initial_size; int fb_size; 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; }/******************************************************************************/static int glwLoadFont(glWindow w,int isize){ Font id; int first, last; XFontStruct *font_info; char fontname[128]; if (debug) { printf("Load font size %d\n",isize); } if ((isize<0)&& (isize>=GLW_MAX_FONT_SIZE)) { glwError("font size out of range\n"); return 0; } sprintf(fontname,GLW_FONT_NAME,isize); font_info = XLoadQueryFont(w->display, 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[isize] = glGenLists((GLuint) last+1); w->font_range[isize]=last+1; if (w->font_base[isize] == 0) { printf ("out of display lists\n"); return 0; } glXUseXFont(id, first, last-first+1, w->font_base[isize]+first); /* *height = font_info->ascent + font_info->descent; *width = font_info->max_bounds.width; */ return 1;}void glwPrint(glWindow w,char *s){ if (w->fb_mode) { int buf=0; if (w->fb_num_strings<GLW_FEEDBACK_MAX_STRINGS) { glDrawPixels(1,1,GL_COLOR_INDEX,GL_INT,&buf); /* draw some cheap dummy */ glPassThrough(w->fb_num_strings); w->fb_strings[w->fb_num_strings]=strdup(s); w->fb_string_size[w->fb_num_strings]=w->font_size; w->fb_num_strings++; } } else { if (w->font_base[w->font_to_use]==0) return; glPushAttrib (GL_LIST_BIT); glListBase(w->font_base[w->font_to_use]); glCallLists((int)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 glwSetFontSize(glWindow w, double font_size){ int isize; isize=(int)(font_size*(double)w->h); if (isize<2) isize=2; if (isize>=GLW_MAX_FONT_SIZE) isize=GLW_MAX_FONT_SIZE-1; w->font_size=font_size; if (w->font_base[isize]==0) glwLoadFont(w,isize); w->font_to_use=isize;}/******************************************************************************/void glwGetMouseLoc(glWindow w,int *x, int *y){ int junk; *x = 0; *y = 0; XQueryPointer(w->display, 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->display, 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->display, w->cMapMain, c, w->vInfoMain->colormap_size); break; } XSync(w->display, 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->display, w->cMapMain, c, w->vInfoMain->colormap_size); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -