home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / stereo / opengl / clip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  7.4 KB  |  275 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <stdio.h>
  18.  
  19. #include <Xm/Xm.h>
  20. #include <Xm/Frame.h>
  21. #include <X11/Intrinsic.h>
  22. #include <X11/extensions/SGIStereo.h>
  23.  
  24. #include <GL/gl.h>
  25. #include <GL/glu.h>
  26. #include <GL/glx.h>
  27. #include <GL/GLwMDrawA.h>
  28.  
  29. #define Color_Buffer_Size        1
  30. #define Depth_Buffer_Size        1
  31. int                    sphere_slices = 16,
  32.                     sphere_stacks = 16;
  33. int                    xRot = 0, yRot = 0,
  34.                     prevy = 0, prevx = 0;
  35.  
  36.  
  37. void                    initCB(Widget, XtPointer, XtPointer),
  38.                     exposeCB(Widget, XtPointer, XtPointer),
  39.                     resizeCB(Widget, XtPointer, XtPointer);
  40. void                    clip_display(Display *, Window),
  41.                     nanSolidSphere(float *),
  42.                     nanWireSphere(float *);
  43. void                     start_rtn(Widget, XEvent *, String *,
  44.                           Cardinal *),
  45.                     rtn(Widget, XEvent *, String *, 
  46.                         Cardinal *),
  47.                     end_rtn(Widget, XEvent *, String *,
  48.                         Cardinal *);
  49.  
  50.  
  51. main(int argc, char **argv)
  52. {
  53.   XtAppContext        application_context;
  54.   Widget        toplevel, glw;
  55.   Arg            args[10];
  56.   int             n = 0;
  57.   GLXContext        glw_context;
  58.   XtTranslations    trans;
  59.   String         fallback[] = {
  60.                           "*frame*shadowType: SHADOW_IN",
  61.                       "*boiler*width: 400",
  62.                       "*boiler*height: 400",
  63.                       NULL
  64.                       };
  65.   
  66.   char             *molTranslations =
  67.                           "#override\n\
  68.                                          <Btn1Down>:start_rtn() \n\
  69.                                          <Btn1Motion>:rtn() \n\
  70.                                          <Btn1Up>:end_rtn()\n";
  71.  
  72.   XtActionsRec        actionsTable[] = {
  73.                           {"start_rtn", start_rtn},
  74.                       {"rtn", rtn},
  75.                       {"end_rtn", end_rtn},
  76.                      };
  77.   
  78.   toplevel = XtAppInitialize(&application_context, "BoilerPlate",
  79.                  (XrmOptionDescList) NULL, 0,
  80.                  &argc, (String *)argv,
  81.                  fallback, (ArgList)NULL, 0);
  82.  
  83.   trans = XtParseTranslationTable(molTranslations);
  84.   XtAppAddActions(application_context, actionsTable, XtNumber(actionsTable));
  85.  
  86.   n=0;
  87.   XtSetArg(args[n], GLwNrgba, TRUE); n++;
  88.   XtSetArg(args[n], GLwNdoublebuffer, TRUE); n++;
  89.   XtSetArg(args[n], GLwNredSize, Color_Buffer_Size); n++;
  90.   XtSetArg(args[n], GLwNgreenSize, Color_Buffer_Size); n++;
  91.   XtSetArg(args[n], GLwNblueSize, Color_Buffer_Size); n++;
  92.   XtSetArg(args[n], GLwCDepthSize, Depth_Buffer_Size); n++;
  93.  
  94.   glw = XtCreateWidget("boiler", glwMDrawingAreaWidgetClass, 
  95.                toplevel, args, n);
  96.   
  97.   XtAddCallback(glw, GLwNginitCallback, initCB, &glw_context);
  98.   XtAddCallback(glw, GLwNexposeCallback, exposeCB, &glw_context);
  99.   XtAddCallback(glw, GLwNresizeCallback, resizeCB, &glw_context);
  100.  
  101.   XtOverrideTranslations(glw, trans);
  102.   XtManageChild(glw);
  103.   
  104.   XtRealizeWidget(toplevel);
  105.   XtAppMainLoop(application_context);
  106. }
  107.  
  108. void 
  109. initCB(Widget w, XtPointer client_data, XtPointer call_data)
  110. {
  111.   Display    *display = XtDisplay(w);
  112.   int        screen = DefaultScreen(display);
  113.   Window    window = XtWindow(w);
  114.   XVisualInfo    *vi;
  115.   GLXContext    *glw_context = (GLXContext *) client_data;
  116.   Arg        args[10];
  117.  
  118.   XtSetArg(args[0], GLwNvisualInfo, &vi);
  119.   XtGetValues(w, args, 1);
  120.  
  121.   *glw_context = glXCreateContext(display, vi, None, GL_TRUE);
  122.   glXMakeCurrent(display, window, *glw_context);
  123.  
  124.   glMatrixMode(GL_PROJECTION);
  125.   glLoadIdentity();
  126.   gluPerspective(30.0,  1.0,  .25,  15.0);
  127.   glMatrixMode(GL_MODELVIEW);
  128.   glLoadIdentity();
  129.  
  130.   glClearColor(0.0, 0.0, 0.0, 1.0);
  131.   glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  132.   glXSwapBuffers(display, window);
  133.   glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  134.  
  135.   glEnable(GL_LINE_SMOOTH);
  136.   glEnable(GL_BLEND);
  137.   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  138.   glLineWidth(2.0);
  139.   clip_display(display, window);
  140. }
  141.  
  142. void
  143. exposeCB(Widget w, XtPointer client_data, XtPointer call_data)
  144. {
  145.   Display       *display = XtDisplay(w);
  146.   Window    window = XtWindow(w);
  147.   GLXContext    *glw_context = (GLXContext *) client_data;
  148.  
  149.   glXMakeCurrent(display, window, *glw_context);
  150.   clip_display(display, window);
  151. }
  152.  
  153. void
  154. resizeCB(Widget w, XtPointer client_data, XtPointer call_data)
  155. {
  156.   Display       *display = XtDisplay(w);
  157.   Window        window = XtWindow(w);
  158.   GLXContext    *glw_context = (GLXContext *) client_data;
  159.   GLwDrawingAreaCallbackStruct *Call_Data = 
  160.                        (GLwDrawingAreaCallbackStruct *) call_data;
  161.   
  162.   glXMakeCurrent(display, window, *glw_context);
  163.   glViewport(0, 0,
  164.          (GLuint) Call_Data->width-1,
  165.          (GLuint) Call_Data->height-1);
  166.   
  167.   clip_display(display, window);
  168. }
  169.  
  170.  
  171. void clip_display(Display *d, Window w)
  172. {
  173.   static float         x = 0.0;
  174.   GLdouble         eqn[4] = {0.0, 1.0, 0.0, 0.0};
  175.   GLdouble         eqn2[4] ={1.0, 0.0, 0.0, 0.0};
  176.   float            params[4] = {0.0, 0.0, 0.0, 1.0};
  177.  
  178.   glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  179.   
  180.   glColor3f(1.0, 1.0, 1.0);
  181.   glPushMatrix();
  182.   glTranslatef(0.0, 0.0, -5.0);
  183.       
  184.   glClipPlane(GL_CLIP_PLANE0, eqn);
  185.   glEnable(GL_CLIP_PLANE0);
  186.   glPushMatrix();
  187.   glRotatef(x += 5, 1.0, 0.0, 0.0);
  188.   glClipPlane(GL_CLIP_PLANE1, eqn2);
  189.   glEnable(GL_CLIP_PLANE1);
  190.   glPopMatrix();
  191.  
  192.   glRotatef(90., 1.0, 0.0, 0.0);
  193.       
  194.   nanWireSphere(params);
  195.   glPopMatrix();
  196.   glFlush();
  197.   glXSwapBuffers(d, w);
  198. }
  199.  
  200. void nanSolidSphere(float *params)
  201. {
  202.   static GLUquadricObj  *quadObj;
  203.   static int            entry = 0;
  204.   double                radius;
  205.  
  206.   radius = params[3];
  207.   glEnable(GL_COLOR_MATERIAL);
  208.   glPushMatrix();
  209.   glTranslatef(params[0], params[1], params[2]);
  210.   if (!entry)
  211.     {
  212.       quadObj = gluNewQuadric ();
  213.       gluQuadricDrawStyle (quadObj, GLU_FILL);
  214.       gluQuadricOrientation(quadObj, GLU_OUTSIDE);
  215.       gluQuadricNormals (quadObj, GLU_SMOOTH);
  216.     }
  217.   gluSphere (quadObj, radius, sphere_slices, sphere_stacks);
  218.   glPopMatrix();
  219.   glDisable(GL_COLOR_MATERIAL);
  220. }
  221.   
  222. void nanWireSphere(float *params)
  223. {
  224.   static GLUquadricObj  *quadObj;
  225.   static int            entry = 0;
  226.   double                radius;
  227.  
  228.   radius = params[3];
  229.   glEnable(GL_COLOR_MATERIAL);
  230.   glPushMatrix();
  231.   glTranslatef(params[0], params[1], params[2]);
  232.   if (!entry)
  233.     {
  234.       quadObj = gluNewQuadric ();
  235.       gluQuadricDrawStyle (quadObj, GLU_LINE);
  236.       gluQuadricOrientation(quadObj, GLU_OUTSIDE);
  237.       gluQuadricNormals (quadObj, GLU_SMOOTH);
  238.     }
  239.   gluSphere (quadObj, radius, sphere_slices, sphere_stacks);
  240.   glPopMatrix();
  241.   glDisable(GL_COLOR_MATERIAL);
  242. }
  243.   
  244. void start_rtn(Widget w, XEvent *event, String *s, Cardinal *c)
  245. {
  246.   prevx = event->xbutton.x;
  247.   prevy = event->xbutton.y;
  248. }
  249.  
  250. void rtn(Widget w, XEvent *event, String *s, Cardinal *c)
  251. {
  252.   int x = event->xbutton.x;
  253.   int y = event->xbutton.y;
  254.  
  255.   yRot +=  5 * (x - prevx);
  256.   xRot +=  5 * (y - prevy);
  257.   prevx = x;
  258.   prevy = y;
  259.   glPushMatrix();
  260.   glRotatef(xRot*0.1, 1.0, 0.0, 0.0);
  261.   glRotatef(yRot*0.1, 0.0, 1.0, 0.0);
  262.   clip_display(XtDisplay(w), XtWindow(w));
  263.   glPopMatrix();
  264. }
  265.  
  266. void
  267. end_rtn(Widget w, XEvent *event, String *s, Cardinal *c)
  268. {
  269.   glPushMatrix();
  270.   glRotatef(xRot*0.1, 1.0, 0.0, 0.0);
  271.   glRotatef(yRot*0.1, 0.0, 1.0, 0.0);
  272.   clip_display(XtDisplay(w), XtWindow(w));
  273.   glPopMatrix();
  274. }    
  275.