home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / widgets / demos / mcube.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  8KB  |  331 lines

  1. /* mcube.c -- Demo program for the Mesa widget
  2.    Copyright (C) 1995 Thorsten.Ohl @ Physik.TH-Darmstadt.de
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2 of the License, or
  7.    (at your option) any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; see the file COPYING.  If not, write to
  16.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.    $Id: mcube.c,v 1.11 1995/05/19 20:30:58 ohl Exp $
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <stdarg.h>
  24. #include <math.h>
  25. #include <X11/X.h>
  26. #include <X11/Intrinsic.h>
  27. #include <X11/StringDefs.h>
  28. #include <X11/Shell.h>
  29. #include <X11/Xaw/Command.h>
  30. #include <X11/Xaw/Form.h>
  31. #include <GL/xmesa.h>
  32. #include <GL/gl.h>
  33. #include <GL/MesaDrawingArea.h>
  34.  
  35. static char *RCS_Id =
  36. "@(#) $Id: mcube.c,v 1.11 1995/05/19 20:30:58 ohl Exp $";
  37.  
  38. static GLint Black, Red, Green, Blue;
  39.  
  40. void quit_function (Widget, XtPointer, XtPointer);
  41.  
  42. void
  43. quit_function (Widget w, XtPointer closure, XtPointer call_data)
  44. {
  45.   exit (0);
  46. }
  47.  
  48. static void
  49. draw_cube (void)
  50. {
  51.   /* X faces */
  52.   glIndexi (Red);
  53.   glColor3f (1.0, 0.0, 0.0);
  54.   glBegin (GL_POLYGON);
  55.   {
  56.     glVertex3f (1.0, 1.0, 1.0);
  57.     glVertex3f (1.0, -1.0, 1.0);
  58.     glVertex3f (1.0, -1.0, -1.0);
  59.     glVertex3f (1.0, 1.0, -1.0);
  60.   }
  61.   glEnd ();
  62.  
  63.   glBegin (GL_POLYGON);
  64.   {
  65.     glVertex3f (-1.0, 1.0, 1.0);
  66.     glVertex3f (-1.0, 1.0, -1.0);
  67.     glVertex3f (-1.0, -1.0, -1.0);
  68.     glVertex3f (-1.0, -1.0, 1.0);
  69.   }
  70.   glEnd ();
  71.  
  72.   /* Y faces */
  73.   glIndexi (Green);
  74.   glColor3f (0.0, 1.0, 0.0);
  75.   glBegin (GL_POLYGON);
  76.   {
  77.     glVertex3f (1.0, 1.0, 1.0);
  78.     glVertex3f (1.0, 1.0, -1.0);
  79.     glVertex3f (-1.0, 1.0, -1.0);
  80.     glVertex3f (-1.0, 1.0, 1.0);
  81.   }
  82.   glEnd ();
  83.  
  84.   glBegin (GL_POLYGON);
  85.   {
  86.     glVertex3f (1.0, -1.0, 1.0);
  87.     glVertex3f (-1.0, -1.0, 1.0);
  88.     glVertex3f (-1.0, -1.0, -1.0);
  89.     glVertex3f (1.0, -1.0, -1.0);
  90.   }
  91.   glEnd ();
  92.  
  93.   /* Z faces */
  94.   glIndexi (Blue);
  95.   glColor3f (0.0, 0.0, 1.0);
  96.   glBegin (GL_POLYGON);
  97.   {
  98.     glVertex3f (1.0, 1.0, 1.0);
  99.     glVertex3f (-1.0, 1.0, 1.0);
  100.     glVertex3f (-1.0, -1.0, 1.0);
  101.     glVertex3f (1.0, -1.0, 1.0);
  102.   }
  103.   glEnd ();
  104.  
  105.   glBegin (GL_POLYGON);
  106.   {
  107.     glVertex3f (1.0, 1.0, -1.0);
  108.     glVertex3f (1.0, -1.0, -1.0);
  109.     glVertex3f (-1.0, -1.0, -1.0);
  110.     glVertex3f (-1.0, 1.0, -1.0);
  111.   }
  112.   glEnd ();
  113. }
  114.  
  115. GLfloat xrot, yrot, zrot;
  116.  
  117. static void
  118. init_display (Widget w)
  119. {
  120.   xrot = yrot = zrot = 0.0;
  121.  
  122.   GLwMakeCurrent (w);
  123.  
  124.   glClearColor (0.0, 0.0, 0.0, 0.0);
  125.   glClearIndex (Black);
  126.   glClearDepth (10.0);
  127.  
  128.   glMatrixMode (GL_PROJECTION);
  129.   glLoadIdentity ();
  130.   glFrustum (-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);
  131.   glTranslatef (0.0, 0.0, -3.0);
  132.  
  133.   glMatrixMode (GL_MODELVIEW);
  134.   glLoadIdentity ();
  135.  
  136.   glCullFace (GL_BACK);
  137.   glEnable (GL_CULL_FACE);
  138.  
  139.   glShadeModel (GL_FLAT);
  140. }
  141.  
  142. static void
  143. display (Widget w)
  144. {
  145.   GLwMakeCurrent (w);
  146.   glClear (GL_COLOR_BUFFER_BIT);
  147.   glPushMatrix ();
  148.   {
  149.     glRotatef (xrot, 1.0, 0.0, 0.0);
  150.     glRotatef (yrot, 0.0, 1.0, 0.0);
  151.     glRotatef (zrot, 0.0, 0.0, 1.0);
  152.     draw_cube ();
  153.   }
  154.   glPopMatrix ();
  155.   glFinish ();
  156.   XMesaSwapBuffers ();
  157.       
  158.   xrot += 1.0;
  159.   yrot += 0.7;
  160.   zrot -= 0.3;
  161. }
  162.  
  163.  
  164. static GLint
  165. alloc_color (Widget w, Colormap cmap, int red, int green, int blue)
  166.   XColor xcolor;
  167.   xcolor.red = red;
  168.   xcolor.green = green;
  169.   xcolor.blue = blue;
  170.   xcolor.flags = DoRed | DoGreen | DoBlue;
  171.   if (!XAllocColor (XtDisplay (w), cmap, &xcolor))
  172.     {
  173.       printf ("Couldn't allocate black!\n");
  174.       exit (1);
  175.     }
  176.   return xcolor.pixel;
  177. }
  178.  
  179. /* This is rather inefficient, but we don't mind for the moment,
  180.    because it works.  */
  181.  
  182. static void
  183. translate_pixels (Widget to, Widget from, ...)
  184. {
  185.   va_list ap;
  186.   char *name;
  187.   Colormap from_cmap, to_cmap;
  188.   XColor xcolor;
  189.  
  190.   XtVaGetValues (from, XtNcolormap, &from_cmap, NULL);
  191.   XtVaGetValues (to, XtNcolormap, &to_cmap, NULL);
  192.  
  193.   va_start (ap, from);
  194.   for (name = va_arg (ap, char *); name != NULL; name = va_arg (ap, char *))
  195.     {
  196.       XtVaGetValues (from, name, &xcolor.pixel, NULL);
  197.       XQueryColor (XtDisplay (from), from_cmap, &xcolor);
  198.       if (!XAllocColor (XtDisplay (to), to_cmap, &xcolor))
  199.     XtAppWarning (XtWidgetToApplicationContext (to),
  200.               "Couldn't allocate color!\n");
  201.       else
  202.     XtVaSetValues (from, name, xcolor.pixel, NULL);
  203.     }
  204.   va_end (ap);
  205. }
  206.  
  207. /* Just like the movies: do 24 frames per second. */
  208. unsigned long delay = 1000/24;
  209.  
  210. static void first_frame (Widget);
  211. static void next_frame (XtPointer, XtIntervalId *);
  212.  
  213. static void
  214. first_frame (Widget w)
  215. {
  216.   XtAppAddTimeOut (XtWidgetToApplicationContext (w),
  217.            delay, next_frame, (XtPointer) w);
  218. }
  219.  
  220. static void
  221. next_frame (XtPointer client_data, XtIntervalId *id)
  222. {
  223.   Widget w = (Widget) client_data;
  224.   first_frame (w);
  225.   display (w);
  226. }
  227.  
  228. static XrmOptionDescRec options[] =
  229. {
  230.   { "-debug", "*debug", XrmoptionNoArg, "True" },
  231.   { "-rgba", "*rgba", XrmoptionNoArg, "True" },
  232.   { "-doublebuffer", "*doublebuffer", XrmoptionNoArg, "True" },
  233.   { "-ximage", "*ximage", XrmoptionNoArg, "True" },
  234. };
  235.  
  236. static String fallback_resources[] =
  237. {
  238.   "*MesaDrawingArea.width: 100",
  239.   "*MesaDrawingArea.height: 100",
  240.   "*MesaDrawingArea.debug: false",
  241.   "*MesaDrawingArea.rgba: false",
  242.   "*MesaDrawingArea.installColormap: true",
  243.   "*MesaDrawingArea.doublebuffer: true",
  244.   "*MesaDrawingArea.ximage: false",
  245.   "*mesa.width: 300",
  246.   "*mesa.height: 300",
  247.   NULL
  248. };
  249.  
  250. int
  251. main (int argc, char *argv[])
  252. {
  253.   Widget top, frame, mesa, mesa1, quit;
  254.   XtAppContext app_context;
  255.   Boolean rgba, cmap_installed;
  256.  
  257.   top = XtVaAppInitialize (&app_context, "Mcube",
  258.                options, XtNumber (options),
  259.                &argc, argv, fallback_resources,
  260.                NULL);
  261.  
  262.   frame = XtVaCreateManagedWidget ("frame", formWidgetClass,
  263.                    top,
  264.                    NULL);
  265.   mesa = XtVaCreateManagedWidget ("mesa", mesaDrawingAreaWidgetClass,
  266.                   frame,
  267.                   GLwNshareLists, True,
  268.                   NULL);
  269.   mesa1 = XtVaCreateManagedWidget ("mesa1", mesaDrawingAreaWidgetClass,
  270.                    frame,
  271.                    GLwNshareLists, True,
  272.                    XtNwidth, 100, XtNheight, 100,
  273.                    XtNfromHoriz, mesa, XtNhorizDistance, 10,
  274.                    NULL);
  275.   quit = XtVaCreateManagedWidget ("quit", commandWidgetClass,
  276.                   frame,
  277.                                   XtNfromVert, mesa1, XtNvertDistance, 50,
  278.                                   XtNfromHoriz, mesa, XtNhorizDistance, 10,
  279.                   NULL);
  280.   XtAddCallback (quit, XtNcallback, quit_function, NULL);
  281.  
  282.   XtRealizeWidget (top);
  283.  
  284.   XtVaGetValues (mesa,
  285.          GLwNrgba, &rgba,
  286.          GLwNinstallColormap, &cmap_installed,
  287.          NULL);
  288.   if (rgba)
  289.     {
  290.       Black = Red = Green = Blue = 0;
  291.  
  292.       if (cmap_installed)
  293.     {
  294.       /* In RGBA mode, the Mesa widgets will have their own color map.
  295.          Adjust the colors of the other widgets so that--even if the rest
  296.          of the screen has wrong colors--all application widgets have the
  297.          right colors.  */
  298.              
  299.       translate_pixels (mesa, quit,
  300.                 XtNbackground, XtNforeground, XtNborder, NULL);
  301.       translate_pixels (mesa, frame, XtNbackground, XtNborder, NULL);
  302.  
  303.       /* Finally warp the pointer into the mesa widget, to make sure that
  304.          the user sees the right colors at the beginning.  */
  305.  
  306.       XWarpPointer (XtDisplay (mesa), None, XtWindow (mesa), 0, 0, 0, 0, 0, 0);
  307.     }
  308.     }
  309.   else
  310.     {
  311.       /* Allocate a few colors for use in color index mode.  */
  312.  
  313.       Colormap cmap;
  314.       cmap = DefaultColormap (XtDisplay (top), DefaultScreen (XtDisplay (top)));
  315.       Black = alloc_color (top, cmap, 0x0000, 0x0000, 0x0000);
  316.       Red   = alloc_color (top, cmap, 0xffff, 0x0000, 0x0000);
  317.       Green = alloc_color (top, cmap, 0x0000, 0xffff, 0x0000);
  318.       Blue  = alloc_color (top, cmap, 0x0000, 0x0000, 0xffff);
  319.     }
  320.  
  321.   init_display (mesa);
  322.   init_display (mesa1);
  323.  
  324.   first_frame (mesa);
  325.   first_frame (mesa1);
  326.   
  327.   XtAppMainLoop (app_context);
  328.   return (0);
  329. }
  330.