home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1991, Silicon Graphics, Inc.
- * All Rights Reserved.
- */
- /*
- * bufferglm: mixed model demo (Motif-based) shows how to switch back and
- * forth between single and double -buffer mode.
- *
- * left mouse : switch to single buffer
- * middle mouse: switch to double buffer
- * right mouse : animate the green sphere in the current buffer mode
- *
- * the program handles redraw (move, resize) and expose (pop, expose,
- * de-iconify) events. ESC key exits the program.
- *
- * Yusuf Attarwala -- September, 1991
- */
- #include <X11/Intrinsic.h>
- #include <Xm/Xm.h>
- #include <Xm/Frame.h>
- #include <X11/Xirisw/GlxMDraw.h>
-
- #include <gl.h>
- #include <gl/sphere.h>
-
- /* function declarations */
-
- void initialize();
- void exitAC();
- void installColormap();
- void createToplevel();
- void createSingleBuffer();
- void createDoubleBuffer();
- void toSingleBuffer();
- void toDoubleBuffer();
- void bindLight();
- void setMatrix();
- void loop();
- void drawScene();
- void exposeCB();
- void resizeCB();
- void initCB();
- void inputCB();
-
-
- static float material1[] = {
- DIFFUSE, 0.6,0.2,0.2,
- SPECULAR, 1.0,1.0,1.0,
- SHININESS, 120.0,
- LMNULL
- };
- static float material2[] = {
- DIFFUSE, 0.2,0.8,0.2,
- SPECULAR, 1.0,1.0,1.0,
- SHININESS, 120.0,
- LMNULL
- };
- static float infinite[] = {
- AMBIENT, 0.3,0.3,0.3,
- LOCALVIEWER, 0.0,
- LMNULL
- };
-
- static float white_light[] = {
- AMBIENT, 0.2, 0.2, 0.2,
- LCOLOR, 0.9, 0.9, 0.9,
- POSITION, 10.0,50.0,50.0,0.0,
- LMNULL
- };
-
-
- static char defaultTranslations[] =
- "<KeyUp>osfCancel: quit() ";
-
- static XtActionsRec actionsTable[] = {
- {"quit",exitAC},
- };
-
- Display *display;
- XtAppContext appContext;
- XtTranslations transTable;
- long doubleBuffer;
- Angle ax,ay,az;
- static Matrix iden = {1.0,0.,0.,0.,0.,1.0,0.,0.,0.,0.,1.0,0.,0.,0.,0.,1.0};
-
- Widget toplevel, /* toplevel shell */
- frameSB, /* frame to hold single buffer gl widget */
- frameDB, /* frame to hold double buffer gl widget */
- glwSB,
- glwDB,
- glw; /* current widget */
-
- Arg args[20];
- int acnt;
-
- main(argc,argv)
- int argc;
- char **argv;
- {
- XtToolkitInitialize();
- appContext = XtCreateApplicationContext();
- display = XtOpenDisplay(appContext, NULL, "Buffer","buffer",NULL,0,
- &argc,argv);
- if (!display) {
- printf("%s : Unable to open display\n",argv[0]);
- exit(0);
- }
-
- XtAppAddActions(appContext, actionsTable, XtNumber(actionsTable));
- transTable = XtParseTranslationTable(defaultTranslations);
-
- createToplevel();
- XtAppMainLoop(appContext);
- }
-
- void
- installColormap()
- {
- Window windows[3];
-
- windows[0] = XtWindow(toplevel);
- windows[1] = XtWindow(glwDB);
- windows[2] = XtWindow(glwSB);
- XSetWMColormapWindows(display, XtWindow(toplevel), windows, 3);
-
- }
-
- void
- createToplevel()
- {
- acnt = 0;
- XtSetArg(args[acnt],XmNminHeight, 300);acnt++;
- XtSetArg(args[acnt],XmNminWidth, 300);acnt++;
- XtSetArg(args[acnt],XmNminAspectX, 1);acnt++;
- XtSetArg(args[acnt],XmNminAspectY, 1);acnt++;
- XtSetArg(args[acnt],XmNmaxAspectX, 1);acnt++;
- XtSetArg(args[acnt],XmNmaxAspectY, 1);acnt++;
- toplevel = XtAppCreateShell("toplevel","buffer",
- applicationShellWidgetClass,
- display,args,acnt);
-
- createDoubleBuffer();
- XtRealizeWidget(toplevel);
- initialize();
- createSingleBuffer();
- installColormap();
-
- toSingleBuffer();
- installColormap();
- }
-
- void
- createSingleBuffer()
- {
- static GLXconfig glxConfigS [] = {
- { GLX_NORMAL, GLX_DOUBLE, FALSE },
- { GLX_NORMAL, GLX_RGB, TRUE },
- { GLX_NORMAL, GLX_ZSIZE, GLX_NOCONFIG },
- { 0, 0, 0 }
- };
-
- acnt = 0;
- frameSB = XtCreateWidget("frameSB", xmFrameWidgetClass,
- toplevel, args, acnt);
-
- /* NOTE : do not manage child here */
-
- acnt = 0;
- XtSetArg(args[acnt], GlxNglxConfig, glxConfigS); acnt++;
- glwSB = XtCreateWidget("glwSB", glxMDrawWidgetClass,
- frameSB, args, acnt);
-
- XtManageChild(glwSB);
-
- XtAddCallback(glwSB, GlxNexposeCallback, exposeCB, NULL);
- XtAddCallback(glwSB, GlxNresizeCallback, resizeCB, NULL);
- XtAddCallback(glwSB, GlxNginitCallback, initCB, NULL);
- XtAddCallback(glwSB, GlxNinputCallback, inputCB, NULL);
- XtOverrideTranslations(glwSB, transTable);
-
- }
-
- void
- createDoubleBuffer()
- {
- static GLXconfig glxConfigD [] = {
- { GLX_NORMAL, GLX_DOUBLE, TRUE },
- { GLX_NORMAL, GLX_RGB, TRUE },
- { GLX_NORMAL, GLX_ZSIZE, GLX_NOCONFIG },
- { 0, 0, 0 }
- };
-
- acnt = 0;
- frameDB = XtCreateWidget("frameDB", xmFrameWidgetClass,
- toplevel, args, acnt);
-
- XtManageChild(frameDB);
-
- acnt = 0;
- XtSetArg(args[acnt], GlxNglxConfig, glxConfigD); acnt++;
- glwDB = XtCreateWidget("glwDB", glxMDrawWidgetClass,
- frameDB, args, acnt);
- XtManageChild(glwDB);
-
- XtAddCallback(glwDB, GlxNexposeCallback, exposeCB, NULL);
- XtAddCallback(glwDB, GlxNresizeCallback, resizeCB, NULL);
- XtAddCallback(glwDB, GlxNginitCallback, initCB, NULL);
- XtAddCallback(glwDB, GlxNinputCallback, inputCB, NULL);
- XtOverrideTranslations(glwDB, transTable);
-
- }
-
- void
- resizeCB(w,client_data,glx_struct)
- Widget w;
- caddr_t client_data;
- GlxDrawCallbackStruct *glx_struct;
- {
- GLXwinset(XtDisplay(w), XtWindow(w));
- viewport(0, (Screencoord) glx_struct->width-1,
- 0, (Screencoord) glx_struct->height-1);
-
- drawScene();
-
- }
-
- void
- exposeCB(w,client_data,call_data)
- Widget w;
- caddr_t client_data;
- caddr_t call_data;
- {
- GLXwinset(XtDisplay(w), XtWindow(w));
- drawScene();
-
- }
-
- void
- initCB(w,client_data,call_data)
- Widget w;
- caddr_t client_data;
- caddr_t call_data;
- {
- GLXwinset(XtDisplay(w), XtWindow(w));
- zbuffer(TRUE);
- setMatrix();
- }
-
- void
- inputCB(w,client_data,glx_struct)
- Widget w;
- caddr_t client_data;
- GlxDrawCallbackStruct *glx_struct;
- {
- switch(glx_struct->event->type) {
- case ButtonPress :
- break;
- case ButtonRelease :
- switch(glx_struct->event->xbutton.button){
- case Button1 :
- toSingleBuffer();
- break;
- case Button2 :
- toDoubleBuffer();
- break;
- case Button3 :
- loop();
- break;
- }
- break;
- }
- }
-
-
- void
- exitAC()
- {
- gexit();
- XtCloseDisplay(display);
- exit(0);
- }
-
-
- void
- drawScene()
- {
- static float sp1[] = {0.2,0.2,0.2,3.0};
- static float sp2[] = {3.4,3.4,0.2,4.0};
-
- GLXwinset(XtDisplay(glw), XtWindow(glw));
- RGBcolor(0,0,0);
- clear();
- zclear();
-
- pushmatrix();
- lmbind(MATERIAL,1);
- sphdraw(sp1);
- rotate(ax,'x');
- rotate(ay,'y');
- rotate(az,'z');
- lmbind(MATERIAL,2);
- sphdraw(sp2);
- popmatrix();
-
- if (doubleBuffer) swapbuffers();
- }
-
- void
- toSingleBuffer()
- {
- acnt = 0;
- XtSetArg(args[acnt],XmNtitle, "SINGLE buffer");acnt++;
- XtSetValues(toplevel,args,acnt);
-
- XtManageChild(frameSB);
- XtUnmanageChild(frameDB);
- glw = glwSB;
- doubleBuffer = FALSE;
- GLXwinset(XtDisplay(glw), XtWindow(glw));
- setMatrix();
- bindLight();
- drawScene();
- }
-
- void
- toDoubleBuffer()
- {
- acnt = 0;
- XtSetArg(args[acnt],XmNtitle, "DOUBLE buffer");acnt++;
- XtSetValues(toplevel,args,acnt);
-
- XtManageChild(frameDB);
- XtUnmanageChild(frameSB);
- glw = glwDB;
- doubleBuffer = TRUE;
- GLXwinset(XtDisplay(glw), XtWindow(glw));
- setMatrix();
- bindLight();
- drawScene();
-
- }
-
- void
- initialize()
- {
- lmdef(DEFMATERIAL,1,11,material1);
- lmdef(DEFMATERIAL,2,11,material2);
- lmdef(DEFLIGHT, 1,14,white_light);
- lmdef(DEFLMODEL, 1,7,infinite);
-
- lsetdepth(getgdesc(GD_ZMIN),getgdesc(GD_ZMAX));
- shademodel(GOURAUD);
- sphmode(SPH_PRIM,SPH_MESH);
- sphmode(SPH_DEPTH,8);
-
- }
-
- void
- setMatrix()
- {
- mmode(MPROJECTION);
- ortho(-10.0,10.0,-10.0,10.0,-10.0,10.0);
- mmode(MVIEWING);
- loadmatrix(iden);
- lookat(0.0,0.0,3.0,0.0,0.0,0.0,0);
- }
-
- void
- bindLight()
- {
- pushmatrix();
- loadmatrix(iden);
- lmbind(LIGHT0,1);
- lmbind(LMODEL,1);
- popmatrix();
- }
-
- void
- loop()
- {
- register int i;
-
- for (i=0;i<50;i++) {
- ax += 50;
- ay -= 25;
- az += 50;
- if (ax >= 3600) ax = 0;
- if (ay <= -3600) ay = 0;
- if (az >= 3600) az = 0;
- drawScene();
- }
- }
-