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

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