/***********************************************************************/        /***********************************************************************/
/* FreeVR OpenGL tutorial example 0: ex0_xwin.c                        */        /* FreeVR OpenGL tutorial example 1: ex1_bare.c                        */
/* Last tested with: N/A (since this is not a VR program)              */        /* Last tested with: FreeVR version 0.6a                               */
/* Last Modified: December 3, 2009                                     */        /* Last Modified: March 23, 2004                                       */
/* URL: http://freevr.org                                              */        /* URL: http://freevr.org                                              */
/***********************************************************************/        /***********************************************************************/
/*                                                                     */        /* This example code demonstrates a VR application program that        */
/* This example code demonstrates an OpenGL application program that   */        /*   initializes the VR library routines, and renders a blue cube and  */
/*   displays a blue cube and a red pyramid in a simple X window.      */        /*   a red pyramid on the floor near the front wall.                   */
/*                                                                     */        /*                                                                     */
/*                                                                     */        /*                                                                     */
/* The features of this example include:                               */        /* The features of this example include:                               */
/*   - Opens an X window                                               */        /*   - FreeVR Library functions, variables, and macros:                */
/*   - Creates an OpenGL viewport                                      */        /*      * vrStart()                                                    */
/*   - Renders a simple static world                                   */        /*      * vrFunctionSetCallback(VRFUNC_DISPLAY, ...)                   */
                                                                                 /*      * vrExit()                                                     */
                                                                                 /*   - A procedure that renders a simple static world                  */
/*                                                                     */        /*                                                                     */
/*                                                                     */        /*                                                                     */
/* Considerations:                                                     */        /* Considerations:                                                     */
/*   - A lot of code is necessary to open the GLX window (X w/ OpenGL) */        /*   - No interaction at all                                           */
/*   - The main loop would typically watch for many more X Events      */        /*   - Program self-terminates after 10 seconds                        */
/*   - This is not a VR program                                        */        /*   - Does not handle everything in a civilized manner                */
/*                                                                     */        /*                                                                     */
/* This application uses the files:                                    */        /* This application uses the files:                                    */
/*   - shapes.c  (Bill Sherman's simple OpenGL shapes)                 */        /*   - shapes.c  (Bill Sherman's simple OpenGL shapes)                 */
/*                                                                     */        /*                                                                     */
/* Copyright 2010, William R. Sherman, All Rights Reserved.            */        /* Copyright 2010, William R. Sherman, All Rights Reserved.            */
/*   In particular, the right to republish this file in whole or in    */        /*   In particular, the right to republish this file in whole or in    */
/*   part, in any form (including electronic forms) is prohibited      */        /*   part, in any form (including electronic forms) is prohibited      */
/*   without written consent of the copyright holder.                  */        /*   without written consent of the copyright holder.                  */
/***********************************************************************/        /***********************************************************************/
#include <stdio.h>      /* needed for printf(), etc. */                          #include <stdio.h>      /* needed for printf(), etc. */
#include <stdlib.h>     /* needed for exit() */                                  #include <stdlib.h>     /* needed for exit() */
 
#include <GL/gl.h>      /* needed for all OpenGL functions & values */           #include <GL/gl.h>      /* needed for all OpenGL functions & values */
#include <GL/glx.h>     /* needed for X-windows interfacing to OpenGL */      
 
void    exit(int status);                                                        #include "freevr.h"     /* needed for FreeVR functions & values */
void    draw_world(void);                       /* graphically render the wor 
                                                                              
#define WINDOW_WIDTH    300                                                   
#define WINDOW_HEIGHT   300                                                   
 
static int      snglBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, None};             
static int      dublBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER,  
static int      quadBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER,  
 
Display        *dpy;                                                             /****************************************************************************
Window          win;                                                             /* These function declarations are very important -- they will be used as arg
GLboolean       doubleBuffer = GL_TRUE;                                          void    draw_world(void);                       /* graphically render the wor
GLboolean       stereoBuffer = GL_TRUE;                                       
 
 
/***********************************************************************/        /***********************************************************************/
main(int argc, char* argv[])                                                     main()
{                                                                                {
        XVisualInfo    *vi;                                                   
        Colormap        cmap;                                                 
        XSetWindowAttributes swa;                                             
        GLXContext      cx;                                                   
        XEvent          event;                                                
        int             dummy;                                                
 
        /**********************************/                                             /**********************************/
        /***  initialize GLX routines   ***/                                             /*** initialize FreeVR routines ***/
        /**********************************/                                             /**********************************/
        /*** open a connection to the X server ***/                                      vrStart();
        dpy = XOpenDisplay(NULL);                                             
        if (dpy == NULL) {                                                    
                perror("could not open display");                             
                exit(0);                                                      
        }                                                                     
                                                                              
        /*** make sure OpenGL's GLX extension supported ***/                  
        if (!glXQueryExtension(dpy, &dummy, &dummy)) {                        
                perror("X server has no OpenGL GLX extension");               
                exit(0);                                                      
        }                                                                     
                                                                              
        /*** find an appropriate visual ***/                                  
        /* find an OpenGL-capable RGB visual with depth buffer */             
        vi = glXChooseVisual(dpy, DefaultScreen(dpy), quadBuf);               
        if (vi == NULL) {                                                     
                printf("Unable to acquire an X11 visual with stereo (quad) bu 
                vi = glXChooseVisual(dpy, DefaultScreen(dpy), dublBuf);       
                if (vi == NULL) {                                             
                        printf("Unable to acquire an X11 visual with double b 
                        vi = glXChooseVisual(dpy, DefaultScreen(dpy), snglBuf 
                        if (vi == NULL) {                                     
                                perror("no RGB visual with depth buffer");    
                                exit(0);                                      
                        }                                                     
                        doubleBuffer = GL_FALSE;                              
                }                                                             
                stereoBuffer = GL_FALSE;                                      
        }                                                                     
        if (vi->class != TrueColor) {                                         
                perror("TrueColor visual required for this program");         
                exit(0);                                                      
        }                                                                     
                                                                              
        /*** create an OpenGL rendering context  ***/                         
        /* create an OpenGL rendering context */                              
        cx = glXCreateContext(dpy, vi, /* no sharing of display lists */ None 
                               /* direct rendering if possible */ GL_TRUE);   
        if (cx == NULL) {                                                     
                perror("could not create rendering context");                 
                exit(0);                                                      
        }                                                                     
                                                                              
        /*** create an X window with the selected visual ***/                 
        /* create an X colormap since probably not using default visual */    
        cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,  
        swa.colormap = cmap;                                                  
        swa.border_pixel = 0;                                                 
        swa.event_mask = ExposureMask | ButtonPressMask | StructureNotifyMask 
        win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, WINDOW_WI 
                            InputOutput, vi->visual, CWBorderPixel | CWColorm 
        XSetStandardProperties(dpy, win, "simple-glx", "simple-glx", None, ar 
                                                                              
        /*** bind the rendering context to the window ***/                    
        glXMakeCurrent(dpy, win, cx);                                         
                                                                              
        /*** request the X window to be displayed on the screen ***/          
        XMapWindow(dpy, win);                                                 
 
        /*** configure the OpenGL context for rendering ***/                             /***************************************/
        glEnable(GL_DEPTH_TEST);/* enable depth buffering */                             /* set up the FreeVR library functions */
        glDepthFunc(GL_LESS);   /* pedantic, GL_LESS is the default */                   vrFunctionSetCallback(VRFUNC_DISPLAY, vrCallbackCreate(draw_world, 0)
        glClearDepth(1.0);      /* pedantic, 1.0 is the default */            
        /* frame buffer clears should be to black */                          
        glClearColor(0.0, 0.0, 0.0, 0.0);                                     
        /* set up projection transform */                                     
        glMatrixMode(GL_PROJECTION);                                          
        glLoadIdentity();                                                     
        glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);                           
        /* establish initial viewport */                                      
        glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);  /* pedantic, full win 
 
        /***************************/                                                    /***************************/
        /*** do world simulation ***/                                                    /*** do world simulation ***/
        /***************************/                                                    /***************************/
        while (1) {                                                                      /* do stuff here -- or not */
                /*** Get and handle inputs ***/                                          vrSleep(10000000);      /* 10 seconds */
                do {                                                          
                        /* Note, the do-while form will always enter the loop 
                        /*   when no events are waiting, and will then pause  
                        /*   the XNextEvent() function, which blocks.         
                        XNextEvent(dpy, &event);                              
                        switch (event.type) {                                 
                        case ConfigureNotify:                                 
                                glViewport(0, 0, event.xconfigure.width, even 
                        }                                                     
                } while (XPending(dpy));                                      
                                                                              
                /*** Handle view placement ***/                               
                glMatrixMode(GL_MODELVIEW);                                   
                glLoadIdentity();                                             
                glTranslatef(0.0, -3.0, 0.0);                                 
                                                                              
                /*** Render the world ***/                                    
                draw_world();                                                 
                                                                              
                /*** Swap buffers (or flush GL queue) ***/                    
                if (doubleBuffer)                                             
                        glXSwapBuffers(dpy, win);                             
                else    glFlush();                                            
        }                                                                     
 
        /*********************/                                                          /*********************/
        /*** close up shop ***/                                                          /*** close up shop ***/
        /*********************/                                                          /*********************/
                                                                                         vrExit();
                                                                                
        exit(0);                                                                         exit(0);
}                                                                                }
 
 
 
/* ----------------8<-----------------8<-----------------8<----------------*/    /* ----------------8<-----------------8<-----------------8<----------------*/
/* In a non-example applicatation, the following would be a separate file. */    /* In a non-example applicatation, the following would be a separate file. */
 
                /**************************/                                                     /**************************/
                /**************************/                                                     /**************************/
                /** The Graphics section **/                                                     /** The Graphics section **/
                /**************************/                                                     /**************************/
                /**************************/                                                     /**************************/
 
/* functions from shapes.c */                                                    /* functions from shapes.c */
void    draw_cube();                                                             void    draw_cube();
void    draw_pyramid();                                                          void    draw_pyramid();
 
 
/*************************************************************/                  /*************************************************************/
/* draw_world(): Render the two objects in the world -- the  */                  /* draw_world(): Render the two objects in the world -- the  */
/*   blue cube, and red pyramid -- such that they are on the */                  /*   blue cube, and red pyramid -- such that they are on the */
/*   XZ plane (typically the floor), and 6 feet forward      */                  /*   XZ plane (typically the floor), and 6 feet forward      */
/*   (typically on the other side of a CAVE's front screen). */                  /*   (typically on the other side of a CAVE's front screen). */
/*************************************************************/                  /*************************************************************/
void draw_world(void)                                                            void draw_world(void)
{                                                                                {
        /**************************************/                                         /**************************************/
        /* set the polygon shading parameters */                                         /* set the polygon shading parameters */
        glShadeModel(GL_SMOOTH);                                                         glShadeModel(GL_SMOOTH);
        glLineWidth(2.5);                                                     
 
        /**************************************/                                         /**************************************/
        /* clear the screen -- very important */                                         /* clear the screen -- very important */
        glClearColor(0.0, 0.0, 0.0, 0.0);                                                glClearColor(0.0, 0.0, 0.0, 0.0);
        glClearDepth(1.0);                                                               glClearDepth(1.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);                              glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
        /************************/                                                       /************************/
        /* draw all the objects */                                                       /* draw all the objects */
 
        /* a blue cube */                                                                /* a blue cube */
        glColor3ub(100, 100, 255);                                                       glColor3ub(100, 100, 255);
        glTranslatef(-2.0, 1.0, -6.0);                                                   glTranslatef(-2.0, 1.0, -6.0);
        draw_cube();                                                                     draw_cube();
        glColor3ub(0, 0, 0);                                                  
        draw_cube_outline();                                                  
 
        /* a red pyramid */                                                              /* a red pyramid */
        glColor3ub(255, 100, 100);                                                       glColor3ub(255, 100, 100);
        glTranslatef(4.0, 0.0, 0.0);                                                     glTranslatef(4.0, 0.0, 0.0);
        draw_pyramid();                                                                  draw_pyramid();
        glColor3ub(0, 0, 0);                                                  
        draw_pyramid_outline();                                               
}                                                                                }