home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / Mesa-1.2.1 / demos / xdemo.c < prev   
Encoding:
C/C++ Source or Header  |  1995-07-05  |  6.4 KB  |  303 lines

  1. /* xdemo.c */
  2.  
  3.  
  4. /*
  5.  * Very simple demo of how to use the Mesa/X11 interface instead of the
  6.  * tk or aux toolkits.  This is the prefered method for real applications.
  7.  */
  8.  
  9.  
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <X11/Xlib.h>
  15. #include <X11/Xutil.h>
  16. #include "GL/xmesa.h"
  17. #include "GL/gl.h"
  18.  
  19.  
  20. extern sleep(int);
  21.  
  22.  
  23. static GLint Black, Red, Green, Blue;
  24.  
  25.  
  26.  
  27. static void make_window( char *title, int color_flag )
  28. {
  29.    int x = 10, y = 10, width = 400, height = 300;
  30.    Display *dpy;
  31.    int scr;
  32.    Window root, win;
  33.    Colormap cmap;
  34.    XColor xcolor;
  35.    int attr_flags;
  36.    XVisualInfo *visinfo;
  37.    XSetWindowAttributes attr;
  38.    XTextProperty tp;
  39.    XSizeHints sh;
  40.    XEvent e;
  41.    XMesaContext context;
  42.  
  43.  
  44.    /*
  45.     * Do the usual X things to make a window.
  46.     */
  47.  
  48.    dpy = XOpenDisplay(NULL);
  49.    if (!dpy) {
  50.       printf("Couldn't open default display!\n");
  51.       exit(1);
  52.    }
  53.  
  54.    scr = DefaultScreen(dpy);
  55.    root = RootWindow(dpy, scr);
  56.  
  57.    /* alloc visinfo struct */
  58.    visinfo = (XVisualInfo *) malloc( sizeof(XVisualInfo) );
  59.  
  60.    /* Get a visual and colormap */
  61.    if (color_flag) {
  62.       /* Open TrueColor window */
  63.  
  64. /*
  65.       if (!XMatchVisualInfo( dpy, scr, 24, TrueColor, visinfo )) {
  66.      printf("Couldn't get 24-bit TrueColor visual!\n");
  67.      exit(1);
  68.       }
  69. */
  70.       if (!XMatchVisualInfo( dpy, scr, 8, PseudoColor, visinfo )) {
  71.      printf("Couldn't get 8-bit PseudoColor visual!\n");
  72.      exit(1);
  73.       }
  74.  
  75.       cmap = XCreateColormap( dpy, root, visinfo->visual, AllocNone );
  76.       Black = Red = Green = Blue = 0;
  77.    }
  78.    else {
  79.       /* Open color index window */
  80.  
  81.       if (!XMatchVisualInfo( dpy, scr, 8, PseudoColor, visinfo )) {
  82.      printf("Couldn't get 8-bit PseudoColor visual\n");
  83.      exit(1);
  84.       }
  85.  
  86.       cmap = XCreateColormap( dpy, root, visinfo->visual, AllocNone );
  87.  
  88.       /* Allocate colors */
  89.       xcolor.red   = 0x0;
  90.       xcolor.green = 0x0;
  91.       xcolor.blue  = 0x0;
  92.       xcolor.flags = DoRed | DoGreen | DoBlue;
  93.       if (!XAllocColor( dpy, cmap, &xcolor )) {
  94.      printf("Couldn't allocate black!\n");
  95.      exit(1);
  96.       }
  97.       Black = xcolor.pixel;
  98.  
  99.       xcolor.red   = 0xffff;
  100.       xcolor.green = 0x0;
  101.       xcolor.blue  = 0x0;
  102.       xcolor.flags = DoRed | DoGreen | DoBlue;
  103.       if (!XAllocColor( dpy, cmap, &xcolor )) {
  104.      printf("Couldn't allocate red!\n");
  105.      exit(1);
  106.       }
  107.       Red = xcolor.pixel;
  108.  
  109.       xcolor.red   = 0x0;
  110.       xcolor.green = 0xffff;
  111.       xcolor.blue  = 0x0;
  112.       xcolor.flags = DoRed | DoGreen | DoBlue;
  113.       if (!XAllocColor( dpy, cmap, &xcolor )) {
  114.      printf("Couldn't allocate green!\n");
  115.      exit(1);
  116.       }
  117.       Green = xcolor.pixel;
  118.  
  119.       xcolor.red   = 0x0;
  120.       xcolor.green = 0x0;
  121.       xcolor.blue  = 0xffff;
  122.       xcolor.flags = DoRed | DoGreen | DoBlue;
  123.       if (!XAllocColor( dpy, cmap, &xcolor )) {
  124.      printf("Couldn't allocate blue!\n");
  125.      exit(1);
  126.       }
  127.       Blue = xcolor.pixel;
  128.    }
  129.  
  130.    /* set window attributes */
  131.    attr.colormap = cmap;
  132.    attr.event_mask = ExposureMask | StructureNotifyMask;
  133.    attr.border_pixel = BlackPixel( dpy, scr );
  134.    attr.background_pixel = BlackPixel( dpy, scr );
  135.    attr_flags = CWColormap | CWEventMask | CWBorderPixel | CWBackPixel;
  136.  
  137.    /* Create the window */
  138.    win = XCreateWindow( dpy, root, x,y, width, height, 0,
  139.                 visinfo->depth, InputOutput,
  140.                 visinfo->visual,
  141.                 attr_flags, &attr);
  142.    if (!win) {
  143.       printf("Couldn't open window!\n");
  144.       exit(1);
  145.    }
  146.  
  147.    XStringListToTextProperty(&title, 1, &tp);
  148.    sh.flags = USPosition | USSize;
  149.    XSetWMProperties(dpy, win, &tp, &tp, 0, 0, &sh, 0, 0);
  150.    XMapWindow(dpy, win);
  151.    while (1) {
  152.       XNextEvent( dpy, &e );
  153.       if (e.type == MapNotify && e.xmap.window == win) {
  154.      break;
  155.       }
  156.    }
  157.  
  158.  
  159.    /*
  160.     * Now do the special Mesa/Xlib stuff!
  161.     */
  162.  
  163.    /* Create a Mesa rendering context */
  164.    context = XMesaCreateContext( dpy, visinfo, (GLboolean) color_flag,
  165.                  GL_FALSE, GL_FALSE, NULL );
  166.    if (!context) {
  167.       printf("Couldn't create Mesa/X context!\n");
  168.       exit(1);
  169.    }
  170.  
  171.    XMesaBindWindow( context, win );
  172.    XMesaMakeCurrent( context );
  173.  
  174.    /* Ready to render! */
  175. }
  176.  
  177.  
  178.  
  179. static void draw_cube( void )
  180. {
  181.    /* X faces */
  182.    glIndexi( Red );
  183.    glColor3f( 1.0, 0.0, 0.0 );
  184.    glBegin( GL_POLYGON );
  185.    glVertex3f( 1.0, 1.0, 1.0 );
  186.    glVertex3f( 1.0, -1.0, 1.0 );
  187.    glVertex3f( 1.0, -1.0, -1.0 );
  188.    glVertex3f( 1.0, 1.0, -1.0 );
  189.    glEnd();
  190.  
  191.    glBegin( GL_POLYGON );
  192.    glVertex3f( -1.0, 1.0, 1.0 );
  193.    glVertex3f( -1.0, 1.0, -1.0 );
  194.    glVertex3f( -1.0, -1.0, -1.0 );
  195.    glVertex3f( -1.0, -1.0, 1.0 );
  196.    glEnd();
  197.  
  198.    /* Y faces */
  199.    glIndexi( Green );
  200.    glColor3f( 0.0, 1.0, 0.0 );
  201.    glBegin( GL_POLYGON );
  202.    glVertex3f(  1.0, 1.0,  1.0 );
  203.    glVertex3f(  1.0, 1.0, -1.0 );
  204.    glVertex3f( -1.0, 1.0, -1.0 );
  205.    glVertex3f( -1.0, 1.0,  1.0 );
  206.    glEnd();
  207.  
  208.    glBegin( GL_POLYGON );
  209.    glVertex3f(  1.0, -1.0,  1.0 );
  210.    glVertex3f( -1.0, -1.0,  1.0 );
  211.    glVertex3f( -1.0, -1.0, -1.0 );
  212.    glVertex3f(  1.0, -1.0, -1.0 );
  213.    glEnd();
  214.  
  215.    /* Z faces */
  216.    glIndexi( Blue );
  217.    glColor3f( 0.0, 0.0, 1.0 );
  218.    glBegin( GL_POLYGON );
  219.    glVertex3f(  1.0,  1.0,  1.0 );
  220.    glVertex3f( -1.0,  1.0,  1.0 );
  221.    glVertex3f( -1.0, -1.0,  1.0 );
  222.    glVertex3f(  1.0, -1.0,  1.0 );
  223.    glEnd();
  224.  
  225.    glBegin( GL_POLYGON );
  226.    glVertex3f(  1.0, 1.0, -1.0 );
  227.    glVertex3f(  1.0,-1.0, -1.0 );
  228.    glVertex3f( -1.0,-1.0, -1.0 );
  229.    glVertex3f( -1.0, 1.0, -1.0 );
  230.    glEnd();
  231. }
  232.  
  233.  
  234.  
  235.  
  236. static void display_loop( void )
  237. {
  238.    GLfloat xrot, yrot, zrot;
  239.  
  240.    xrot = yrot = zrot = 0.0;
  241.  
  242.    glClearColor( 0.0, 0.0, 0.0, 0.0 );
  243.    glClearIndex( Black );
  244.  
  245.    glMatrixMode( GL_PROJECTION );
  246.    glLoadIdentity();
  247.    glFrustum( -1.0, 1.0,  -1.0, 1.0,  1.0, 10.0 );
  248.    glTranslatef( 0.0, 0.0, -5.0 );
  249.  
  250.    glMatrixMode( GL_MODELVIEW );
  251.    glLoadIdentity();
  252.  
  253.    glCullFace( GL_BACK );
  254.    glEnable( GL_CULL_FACE );
  255.  
  256.    glShadeModel( GL_FLAT );
  257.  
  258.    while (1) {
  259.       glClear( GL_COLOR_BUFFER_BIT );
  260.       glPushMatrix();
  261.       glRotatef( xrot, 1.0, 0.0, 0.0 );
  262.       glRotatef( yrot, 0.0, 1.0, 0.0 );
  263.       glRotatef( zrot, 0.0, 0.0, 1.0 );
  264.  
  265.       draw_cube();
  266.  
  267.       glPopMatrix();
  268.       glFinish();
  269.  
  270.       sleep(1);  /* slow things down! */
  271.  
  272.       xrot += 10.0;
  273.       yrot += 7.0;
  274.       zrot -= 3.0;
  275.    }
  276.  
  277. }
  278.  
  279.  
  280.  
  281.  
  282. main( int argc, char *argv[] )
  283. {
  284.    if (argc<2) {
  285.       printf("Specify -ci for 8-bit color index or -rgb for RGB mode\n");
  286.       exit(1);
  287.    }
  288.  
  289.    if (strcmp(argv[1],"-ci")==0) {
  290.       make_window( argv[0], 0 );
  291.    }
  292.    else if (strcmp(argv[1],"-rgb")==0) {
  293.       make_window( argv[0], 1 );
  294.    }
  295.    else {
  296.       printf("Bad flag: %s\n", argv[1]);
  297.       exit(1);
  298.    }
  299.  
  300.    display_loop();
  301. }
  302.  
  303.