/***********************************************************************/        /***********************************************************************/
/* FreeVR OpenGL tutorial example 13: ex13_socket.c                    */        /* FreeVR OpenGL tutorial example 14a: ex14a_button.c                  */
/* Last tested with: FreeVR version 0.6a                               */        /* Last tested with: FreeVR version 0.6a                               */
/* Last Modified: July 19, 2004                                        */        /* Last Modified: January 11, 2007                                     */
/* URL: http://freevr.org                                              */        /* URL: http://freevr.org                                              */
/***********************************************************************/        /***********************************************************************/
/* This example code demonstrates a VR application program that        */        /* This example code demonstrates a VR application program that        */
/*   uses socket ports to receive commands.  Allowing other            */        /*   allows the user to use special objects in the virtual world.      */
/*   applications for to give commands to a running VR application.    */        /*   Because these objects can be used to control the functionality    */
                                                                                 /*   of the virtual world, they are refered to as "virtual controls".  */
                                                                                 /*   Specifically, in this example toggle-button virtual control is    */
                                                                                 /*   introduced.  The button is toggled by touching it with the wand.  */
/*                                                                     */        /*                                                                     */
/*                                                                     */        /*                                                                     */
/* The new features of this example include:                           */        /* The new features of this example include:                           */
/*   - Opening a socket to which clients can connect.                  */        /*   - the use of the expanded wrs_shapes.c file.                      */
/*   - Parsing commands read from a socket.                            */     
/*   - App. termination (quitting) can be initiated in the world sim.  */     
/*                                                                     */        /*                                                                     */
/* Considerations:                                                     */        /* Considerations:                                                     */
/*   - Sockets can be connected to using perl scripts, tcl shells,     */        /*   Intersection testing with an object must be done it its space     */
/*       telnet, etc.                                                  */        /*     (ie. UI objects in rw space, and virtual objects in vw space    */
/*   - This application uses very simple-minded parsing of incoming    */        /*   Actions from contacted object are specific to the object type.    */
/*       commands.  This is actually fine for clients such as tcl      */        /*   Don't forget to read/write lock all references to 'wd'.           */
/*       shells, voice commands sent via telnet, etc.  It is primarily */        /*   How does the user interface object affect the virtual world?      */
/*       direct human typing for which more robust parsing is material.*/     
/*   - The entire message string is assumed to come in one chunk.      */     
/*   - Only one client can connect at a time.                          */     
/*                                                                     */        /*                                                                     */
/* This application uses the files:                                    */        /* This application uses the files:                                    */
/*   - shapes.c  (Bill Sherman's simple OpenGL shapes)                 */        /*   - wrs_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() & getenv() */                       #include <stdlib.h>     /* needed for exit() & getenv() */
#include <signal.h>     /* needed for signal() and its arguments */              #include <signal.h>     /* needed for signal() and its arguments */
#include <unistd.h>     /* not sure why/whether this is needed */                #include <unistd.h>     /* not sure why/whether this is needed */
#include <math.h>       /* needed for fabs() */                                  #include <math.h>       /* needed for fabs() */
 
#include <GL/gl.h>      /* needed for all OpenGL functions & values */           #include <GL/gl.h>      /* needed for all OpenGL functions & values */
 
#include "freevr.h"     /* needed for FreeVR functions & values */               #include "freevr.h"     /* needed for FreeVR functions & values */
#define WAND_SENSOR     1       /* TODO: this should really be based on the c    #define WAND_SENSOR     1       /* TODO: this should really be based on the c
 
/* Define some values to adjust the compilation of this example */               /* Define some values to adjust the compilation of this example */
#define MAX_OBJECTS     4                                                        #define DRAW_UITEXT
                                                                                 #define USE_SOCKETS
                                                                                 #define MAX_OBJECTS     1000
 
 
/* Define a structure for information about each individual object. */           /* Define a structure for information about each individual object. */
typedef struct {                                                                 typedef struct {
                float                   x, y, z;                /* location o                    float                   x, y, z;                /* location o
                                                                                                 float                   size;                   /* size of th
                GLubyte                 color[3];               /* color of t                    GLubyte                 color[3];               /* color of t
                enum {  cube,                                                                    enum {  cube,
                                                                                                         sphere,
                        pyramid,                                                                         pyramid,
                        ud_pyramid }    shape;                  /* shape of t                            ud_pyramid }    shape;                  /* shape of t
                                                                                                 enum {  vw_normal,
                                                                                                         ui_button }     type;                   /* type of th
                                                                                                 int                     ui_button_value;        /* button val
        } ObjectType;                                                                    } ObjectType;
 
/* Define a structure for holding a database of the virtual world. */            /* Define a structure for holding a database of the virtual world. */
typedef struct {                                                                 typedef struct {
                int             quit_bit;                       /* a flag to                     int             quit_bit;                       /* a flag to 
                int             number_of_objects;              /* number of                     int             number_of_objects;              /* number of 
                int             selected_object;                /* currently                     int             contacted_object;               /* object con
                                                                                                 int             last_contacted_object;          /* object pre
                ObjectType      object[MAX_OBJECTS];            /* array of o                    ObjectType      object[MAX_OBJECTS];            /* array of o
                vrLock          lock;                           /* to avoid b                    vrLock          lock;                           /* to avoid b
        } WorldDataType;                                                                 } WorldDataType;
 
 
/****************************************************************************    /****************************************************************************
/* These function declarations are very important -- they will be used as arg    /* These function declarations are very important -- they will be used as arg
void    exit_application(int signal);           /* cleanly close down operati    void    exit_application(int signal);           /* cleanly close down operati
 
void    init_gfx();                             /* initialize the graphics       void    init_gfx();                             /* initialize the graphics   
void    draw_world(WorldDataType *wd, vrRenderInfo *rendinfo); /* render the     void    draw_world(WorldDataType *wd, vrRenderInfo *rendinfo); /* render the 
void    init_world(WorldDataType *wd);          /* initialize the world param    void    init_world(WorldDataType *wd);          /* initialize the world param
void    update_world(WorldDataType *wd);        /* perform the world simulati    void    update_world(WorldDataType *wd);        /* perform the world simulati
 
void    end_world();                            /* close down the world opera    void    end_world();                            /* close down the world opera
void    init_objects(WorldDataType *wd);        /* initalize the objects in t    void    init_objects(WorldDataType *wd);        /* initalize the objects in t
 
 
/***********************************************************************/        /***********************************************************************/
main(int argc, char* argv[])                                                     main(int argc, char* argv[])
{                                                                                {
        WorldDataType   *wd;                                                             WorldDataType   *wd;
 
        /**********************************/                                             /**********************************/
        /*** initialize FreeVR routines ***/                                             /*** initialize FreeVR routines ***/
        /**********************************/                                             /**********************************/
        signal(SIGINT, SIG_IGN);                                                         signal(SIGINT, SIG_IGN);
        vrConfigure(&argc, argv, NULL);         /* allow CLAs to affect confi            vrConfigure(&argc, argv, NULL);         /* allow CLAs to affect confi
        vrStart();                                                                       vrStart();
        signal(SIGINT, exit_application);                                                signal(SIGINT, exit_application);
 
        /********************/                                                           /********************/
        /* create the world */                                                           /* create the world */
        wd = (WorldDataType *)vrShmemAlloc0(sizeof(WorldDataType));                      wd = (WorldDataType *)vrShmemAlloc0(sizeof(WorldDataType));
        wd->lock = vrLockCreate();                                                       wd->lock = vrLockCreate();
        init_world(wd);                                                                  init_world(wd);
 
        /***************************************/                                        /***************************************/
        /* set up the FreeVR library functions */                                        /* set up the FreeVR library functions */
        vrFunctionSetCallback(VRFUNC_DISPLAY_INIT, vrCallbackCreate(init_gfx,            vrFunctionSetCallback(VRFUNC_DISPLAY_INIT, vrCallbackCreate(init_gfx,
        vrFunctionSetCallback(VRFUNC_DISPLAY, vrCallbackCreate(draw_world, 1,            vrFunctionSetCallback(VRFUNC_DISPLAY, vrCallbackCreate(draw_world, 1,
 
        /********************************************************/                       /********************************************************/
        /* define the application name, authors, controls, etc. */                       /* define the application name, authors, controls, etc. */
        vrSystemSetName("example 13 -- socket");                                         vrSystemSetName("example 14a -- button widget");
        vrSystemSetAuthors("Bill Sherman");                                              vrSystemSetAuthors("Bill Sherman");
        vrSystemSetExtraInfo("An example to demonstrate communicating with th            vrSystemSetExtraInfo("An example to demonstrate a button virtual cont
        vrSystemSetStatusDescription("Application running fine");                        vrSystemSetStatusDescription("Application running fine");
        vrInputSet2switchDescription(0, "Terminate the application");                    vrInputSet2switchDescription(0, "Terminate the application");
        vrInputSet2switchDescription(1, "Move the selected object");                     vrInputSet2switchDescription(1, "Move the selected object");
        vrInputSet2switchDescription(3, "Reset the travel matrix (take us hom            vrInputSet2switchDescription(3, "Reset the travel matrix (take us hom
        vrInputSetValuatorDescription(0, "Rotate us in the world about the Y-            vrInputSetValuatorDescription(0, "Rotate us in the world about the Y-
        vrInputSetValuatorDescription(1, "Move us though the world in the dir            vrInputSetValuatorDescription(1, "Move us though the world in the dir
        vrInputSet6sensorDescription(WAND_SENSOR, "Move a short cyan pointer             vrInputSet6sensorDescription(WAND_SENSOR, "Move a short cyan pointer 
 
        /***************************/                                                    /***************************/
        /*** do world simulation ***/                                                    /*** do world simulation ***/
        /***************************/                                                    /***************************/
        vrUserTravelReset(VR_ALLUSERS);                                                  vrUserTravelReset(VR_ALLUSERS);
        /* run until the user has indicated it's time not to */                          /* run until the user has indicated it's time not to */
        while(!wd->quit_bit) {                                                           while(!wd->quit_bit) {
                vrFrame();                                                                       vrFrame();
                update_world(wd);                                                                update_world(wd);
        }                                                                                }
 
        /*********************/                                                          /*********************/
        /*** close up shop ***/                                                          /*** close up shop ***/
        /*********************/                                                          /*********************/
        exit_application(0);                                                             exit_application(0);
}                                                                                }
 
 
/********************************************************************/           /********************************************************************/
/* exit_application(): clean up anything started by the application */           /* exit_application(): clean up anything started by the application */
/*   (forked processes, open files, open sockets, etc.)             */           /*   (forked processes, open files, open sockets, etc.)             */
/********************************************************************/           /********************************************************************/
void exit_application(int signal)                                                void exit_application(int signal)
{                                                                                {
        end_world();                                                                     end_world();
 
        vrExit();                                                                        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 World Simulation section **/                                             /** The World Simulation section **/
                /**********************************/                                             /**********************************/
                /**********************************/                                             /**********************************/
 
                                                                                 #ifdef USE_SOCKETS /* { */
static int      listen_sock = -1;       /* socket to receive commands */         static int      listen_sock = -1;       /* socket to receive commands */
                                        /*   listen_sock is not in wd                                                    /*   listen_sock is not in wd        
                                        /*   because it needs to be                                                      /*   because it needs to be          
                                        /*   available to exit_application()                                             /*   available to exit_application() 
                                        /*   which can have no arguments.                                                /*   which can have no arguments.    
                                                                                 #endif /* } USE_SOCKETS */
 
 
/****************************************************************/               /****************************************************************/
/* init_world(): create the initial conditions of the dynamic   */               /* init_world(): create the initial conditions of the dynamic   */
/*   simulation.  Now with the addition of code to initialize a */               /*   simulation.  If the "VR_PORT" environment variable is set, */
/*   socket communication interface.                            */               /*   then its value is used as the initial server socket port.  */
/*                                                              */            
/*   The socket server port to be used can be set from the      */            
/*   "VR_PORT" environment variable.  If it is set, then its    */            
/*   value is used as the initial server socket port.           */            
/*   Otherwise, the arbitrarily chosen port number 4000 is used.*/               /*   Otherwise, the arbitrarily chosen port number 4000 is used.*/
/*                                                              */            
/*   Since IRIX, and perhaps other Unicies have a "feature" in  */               /*   Since IRIX, and perhaps other Unicies have a "feature" in  */
/*   which a socket port isn't usable again until after 60      */               /*   which a socket port isn't usable again until after 60      */
/*   seconds have passed since the connection was closed, if the*/               /*   seconds have passed since the connection was closed, if the*/
/*   initial socket isn't available, we try the next two ports  */               /*   initial socket isn't available, we try the next two ports  */
/*   in succession.  The selected port is reported to the tty.  */               /*   in succession.  The selected port is reported to the tty.  */
/****************************************************************/               /****************************************************************/
void init_world(WorldDataType *wd)                                               void init_world(WorldDataType *wd)
{                                                                                {
                                                                                 #ifdef USE_SOCKETS /* { */
        int     inet_port;                                                               int     inet_port;
        int     inet_port_max;                                                           int     inet_port_max;
        char    host_machine[128];                                                       char    host_machine[128];
                                                                                 #endif /* } USE_SOCKETS */
 
        /*************************/                                                      /*************************/
        /* set world state flags */                                                      /* set world state flags */
        vrLockWriteSet(wd->lock);                                                        vrLockWriteSet(wd->lock);
        wd->quit_bit = 0;                                                                wd->quit_bit = 0;
        vrLockWriteRelease(wd->lock);                                                    vrLockWriteRelease(wd->lock);
 
        /***************************/                                                    /***************************/
        /* setup the world content */                                                    /* setup the world content */
        init_objects(wd);                                                                init_objects(wd);
 
                                                                                 #ifdef USE_SOCKETS /* { */
        /******************************/                                                 /******************************/
        /* initialize the socket port */                                                 /* initialize the socket port */
        gethostname(host_machine, 128);                                                  gethostname(host_machine, 128);
 
        /* loop through N possible internet ports to hopefully find one that             /* loop through N possible internet ports to hopefully find one that 
        if (getenv("VR_PORT") != NULL)                                                   if (getenv("VR_PORT") != NULL)
                inet_port = atoi(getenv("VR_PORT"));                                             inet_port = atoi(getenv("VR_PORT"));
        else    inet_port = 4000;                                                        else    inet_port = 4000;
        for (inet_port_max = inet_port + 2; listen_sock < 0 && inet_port <= i            for (inet_port_max = inet_port + 2; listen_sock < 0 && inet_port <= i
                printf("Port %d -- ", inet_port);                                                printf("Port %d -- ", inet_port);
                fflush(stdout);                                                                  fflush(stdout);
                listen_sock = vrSocketCreateListen(&inet_port, 0);                               listen_sock = vrSocketCreateListen(&inet_port, 0);
        }                                                                                }
        inet_port--;                                                                     inet_port--;
        if (listen_sock < 0)                                                             if (listen_sock < 0)
                printf("WARNING: Unable to find an open socket port.\n");                        printf("WARNING: Unable to find an open socket port.\n");
        else    printf("\rConnect to '%s:%d' to send commands to the simulati            else    printf("\rConnect to '%s:%d' to send commands to the simulati
                                                                                 #endif /* } USE_SOCKETS */
}                                                                                }
 
 
/***************************************************************/                /***************************************************************/
/* init_objects(): place objects into the world data structure */                /* init_objects(): place objects into the world data structure */
/*   at some initial positions.                                */                /*   at some initial positions.                                */
/***************************************************************/                /***************************************************************/
void init_objects(WorldDataType *wd)                                             void init_objects(WorldDataType *wd)
{                                                                                {
        int     objnum = 0;                                                              int     objnum = 0;
 
        vrLockWriteSet(wd->lock);                                                        vrLockWriteSet(wd->lock);
 
        /* object 1 - a blue cube */                                                     /* object 1 - a blue cube */
                                                                                         wd->object[objnum].type = vw_normal;
        wd->object[objnum].shape = cube;                                                 wd->object[objnum].shape = cube;
                                                                                         wd->object[objnum].size = 1.0;
        wd->object[objnum].color[0] = 100;                                               wd->object[objnum].color[0] = 100;
        wd->object[objnum].color[1] = 100;                                               wd->object[objnum].color[1] = 100;
        wd->object[objnum].color[2] = 255;                                               wd->object[objnum].color[2] = 255;
        wd->object[objnum].x =  0.0;                                                     wd->object[objnum].x =  0.0;
        wd->object[objnum].y =  5.0;                                                     wd->object[objnum].y =  5.0;
        wd->object[objnum].z = -4.0;                                                     wd->object[objnum].z = -4.0;
        objnum++;                                                                        objnum++;
 
        /* object 2 - a red pyramid */                                                   /* object 2 - a red pyramid */
                                                                                         wd->object[objnum].type = vw_normal;
        wd->object[objnum].shape = pyramid;                                              wd->object[objnum].shape = pyramid;
                                                                                         wd->object[objnum].size = 1.0;
        wd->object[objnum].color[0] = 255;                                               wd->object[objnum].color[0] = 255;
        wd->object[objnum].color[1] = 100;                                               wd->object[objnum].color[1] = 100;
        wd->object[objnum].color[2] = 100;                                               wd->object[objnum].color[2] = 100;
        wd->object[objnum].x = -4.0;                                                     wd->object[objnum].x = -4.0;
        wd->object[objnum].y =  5.0;                                                     wd->object[objnum].y =  5.0;
        wd->object[objnum].z = -4.0;                                                     wd->object[objnum].z = -4.0;
        objnum++;                                                                        objnum++;
 
        /* object 3 - an upside-down green pyramid */                                    /* object 3 - an upside-down green pyramid */
                                                                                         wd->object[objnum].type = vw_normal;
        wd->object[objnum].shape = ud_pyramid;                                           wd->object[objnum].shape = ud_pyramid;
                                                                                         wd->object[objnum].size = 1.0;
        wd->object[objnum].color[0] = 100;                                               wd->object[objnum].color[0] = 100;
        wd->object[objnum].color[1] = 255;                                               wd->object[objnum].color[1] = 255;
        wd->object[objnum].color[2] = 100;                                               wd->object[objnum].color[2] = 100;
        wd->object[objnum].x =  4.0;                                                     wd->object[objnum].x =  4.0;
        wd->object[objnum].y =  5.0;                                                     wd->object[objnum].y =  5.0;
        wd->object[objnum].z = -4.0;                                                     wd->object[objnum].z = -4.0;
        objnum++;                                                                        objnum++;
 
                                                                                         /* object 4 - a user interface button */
                                                                                         wd->object[objnum].type = ui_button;
                                                                                         wd->object[objnum].shape = cube;
                                                                                         wd->object[objnum].size = 0.4;
                                                                                         wd->object[objnum].ui_button_value = 1;
                                                                                         wd->object[objnum].color[0] = 200;
                                                                                         wd->object[objnum].color[1] = 100;
                                                                                         wd->object[objnum].color[2] = 255;
                                                                                         wd->object[objnum].x = -4.0;
                                                                                         wd->object[objnum].y =  4.0;
                                                                                         wd->object[objnum].z = -2.0;
                                                                                         objnum++;
                                                                                
        wd->number_of_objects = objnum;                                                  wd->number_of_objects = objnum;
        wd->selected_object = -1;                                                        wd->contacted_object = -1;
                                                                                         wd->last_contacted_object = wd->contacted_object;
 
        /* sanity test */                                                                /* sanity test */
        if (objnum > MAX_OBJECTS) {                                                      if (objnum > MAX_OBJECTS) {
                printf("DANGER: Number of objects (%d), exceeds maximum allow                    printf("DANGER: Number of objects (%d), exceeds maximum allow
        }                                                                                }
 
        vrLockWriteRelease(wd->lock);                                                    vrLockWriteRelease(wd->lock);
}                                                                                }
 
 
/*******************************************************/                        /*******************************************************/
/* end_world(): clean up anything opened by simulation */                        /* end_world(): clean up anything opened by simulation */
/*******************************************************/                        /*******************************************************/
void end_world()                                                                 void end_world()
{                                                                                {
                                                                                 #ifdef USE_SOCKETS /* { */
        vrSocketClose(listen_sock);                                                      vrSocketClose(listen_sock);
                                                                                 #endif /* } USE_SOCKETS */
}                                                                                }
 
 
/*********************************************************************/          /*********************************************************************/
/* update_world(): In this version, we handle the user interactions. */          /* update_world(): In this version, we handle the user interactions. */
/*   Both selecting, and moving the selected object.                 */          /*   Both selecting, and moving the contacted object.                */
/*********************************************************************/          /*********************************************************************/
void update_world(WorldDataType *wd)                                             void update_world(WorldDataType *wd)
{                                                                                {
#define JS_EPSILON      0.125                           /* the dead-zone befo    #define JS_EPSILON      0.125                           /* the dead-zone befo
#define MOVE_FACTOR     2.5                             /* a scale factor to     #define MOVE_FACTOR     2.5                             /* a scale factor to 
#define TURN_FACTOR     25.0                            /* a scale factor to     #define TURN_FACTOR     25.0                            /* a scale factor to 
#define SUB_JS_EPSILON(a)       ((a)-copysign(JS_EPSILON,(a)))  /* a macro to    #define SUB_JS_EPSILON(a)       ((a)-copysign(JS_EPSILON,(a)))  /* a macro to
                                                                                 #ifdef USE_SOCKETS /* { */
static  char    *help_msg = "Socket commands:\n"                                 static  char    *help_msg = "Socket commands:\n"
                                "\thelp -- this message\n"                                                       "\thelp -- this message\n"
                                "\tquit -- quit the application\n"                                               "\tquit -- quit the application\n"
                                "\tclose -- close the connection\n"                                              "\tclose -- close the connection\n"
                                "\treset -- reset the application\n"                                             "\treset -- reset the application\n"
                                                                                                                 "\tbutton? -- report the value of the UI butt
                                                                                                                 "\tbutton on -- set the value of the UI butto
                                                                                                                 "\tbutton off -- set the value of the UI butt
                                "\tmove cube up -- translate the cube up 2 fe                                    "\tmove cube up -- translate the cube up 2 fe
                                "\tmove cube down -- translate the cube down                                     "\tmove cube down -- translate the cube down 
                                "\twhere -- report the user's position in the                                    "\twhere -- report the user's position in the
                                "\twhere am i -- report the user's position i                                    "\twhere am i -- report the user's position i
                                "\tshow ActiveWalls -- report some CAVE confi                                    "\tshow ActiveWalls -- report some CAVE confi
                "";                                                                              "";
static  int     cmd_socket = -1;        /* socket to get commands from */        static  int     cmd_socket = -1;        /* socket to get commands from */
static  char    buf[128];               /* socket read buffer          */        static  char    buf[128];               /* socket read buffer          */
static  char    msg[128];               /* socket send buffer          */        static  char    msg[128];               /* socket send buffer          */
        int     result;                 /* result of reading a socket  */                int     result;                 /* result of reading a socket  */
                                                                                 #endif /* } USE_SOCKETS */
 
static  vrTime          last_time = -1.0;               /* initially set to a    static  vrTime          last_time = -1.0;               /* initially set to a
        vrTime          sim_time = vrCurrentSimTime();  /* the current time o            vrTime          sim_time = vrCurrentSimTime();  /* the current time o
static  vrPoint         last_wand_vw_locpnt;            /* VW location of the    static  vrPoint         last_wand_vw_locpnt;            /* VW location of the
        vrPoint         wand_vw_locpnt;                 /* current location o            vrPoint         wand_vw_locpnt;                 /* current location o
                                                                                         vrPoint         wand_rw_locpnt;                 /* current location o
#define last_wand_vw_loc last_wand_vw_locpnt.v                                   #define last_wand_vw_loc last_wand_vw_locpnt.v
#define wand_vw_location wand_vw_locpnt.v                                        #define wand_vw_location wand_vw_locpnt.v
                                                                                 #define wand_rw_location wand_rw_locpnt.v
        vrVector        wand_rw_pointvec;               /* current vector poi            vrVector        wand_rw_pointvec;               /* current vector poi
        double          delta_time;                     /* time since last up            double          delta_time;                     /* time since last up
        double          delta_move;                     /* distance an object            double          delta_move;                     /* distance an object
        double          joy_x, joy_y;                   /* Joystick values */            double          joy_x, joy_y;                   /* Joystick values */
        int             selected_obj;                   /* the currently sele 
        int             touch_obj = -1;                 /* the currently touc            int             touch_obj = -1;                 /* the currently touc
        int             objnum;                         /* for looping throuh            int             objnum;                         /* for looping throuh
        ObjectType      *object;                        /* pointer to object             ObjectType      *object;                        /* pointer to object 
                                                                                         float           size;                           /* size of object -- 
                                                                                         int             button_value;
 
        /*****************************************************/                          /*****************************************************/
        /** Determine delta time from last simulation frame **/                          /** Determine delta time from last simulation frame **/
        /*****************************************************/                          /*****************************************************/
        if (last_time == -1.0)                                                           if (last_time == -1.0)
                delta_time = 0.0;                                                                delta_time = 0.0;
        else    delta_time = sim_time - last_time;                                       else    delta_time = sim_time - last_time;
        last_time = sim_time;                           /* now that delta_tim            last_time = sim_time;                           /* now that delta_tim
 
        /* skip the update if the delta isn't big enough */                              /* skip the update if the delta isn't big enough */
        if (delta_time <= 0.0)  /* can also choose a non-zero epsilon */                 if (delta_time <= 0.0)  /* can also choose a non-zero epsilon */
                return;                                                                          return;
 
        /**********************************/                                             /**********************************/
        /** Handle Manipulation via wand **/                                             /** Handle Manipulation via wand **/
        /**********************************/                                             /**********************************/
 
        /**********************************************************/                     /**********************************************************/
        /* check for contact between the wand and all the objects */                     /* check for contact between the wand and all the objects */
        vrLockReadSet(wd->lock);                                                         vrLockReadSet(wd->lock);
 
        vrPointGetVWFromUser6sensor(&wand_vw_locpnt, 0, WAND_SENSOR);                    vrPointGetVWFromUser6sensor(&wand_vw_locpnt, 0, WAND_SENSOR);
                                                                                         vrPointGetRWFrom6sensor(&wand_rw_locpnt, WAND_SENSOR);
 
        for (objnum = 0; objnum < wd->number_of_objects; objnum++) {                     for (objnum = 0; objnum < wd->number_of_objects; objnum++) {
                object = &(wd->object[objnum]);                                                  object = &(wd->object[objnum]);
                if ( (wand_vw_location[VR_X] < object->x + 1.0)                                  size = object->size;
                  && (wand_vw_location[VR_X] > object->x - 1.0)                 
                  && (wand_vw_location[VR_Y] < object->y + 1.0)                                  switch (object->type) {
                  && (wand_vw_location[VR_Y] > object->y - 1.0)                                  case vw_normal: /* determine contact with normal object */
                  && (wand_vw_location[VR_Z] < object->z + 1.0)                                          if ( (wand_vw_location[VR_X] < object->x + size)
                  && (wand_vw_location[VR_Z] > object->z - 1.0) ) {                                        && (wand_vw_location[VR_X] > object->x - size)
                        touch_obj = objnum;                                                                && (wand_vw_location[VR_Y] < object->y + size)
                                                                                                           && (wand_vw_location[VR_Y] > object->y - size)
                                                                                                           && (wand_vw_location[VR_Z] < object->z + size)
                                                                                                           && (wand_vw_location[VR_Z] > object->z - size) ) {
                                                                                                                 touch_obj = objnum;
                                                                                                         }
                                                                                                         break;
                                                                                                 
                                                                                                 case ui_button: /* determine contact with button widget */
                                                                                                         if ( (wand_rw_location[VR_X] < object->x + size)
                                                                                                           && (wand_rw_location[VR_X] > object->x - size)
                                                                                                           && (wand_rw_location[VR_Y] < object->y + size)
                                                                                                           && (wand_rw_location[VR_Y] > object->y - size)
                                                                                                           && (wand_rw_location[VR_Z] < object->z + size)
                                                                                                           && (wand_rw_location[VR_Z] > object->z - size) ) {
                                                                                                                 touch_obj = objnum;
                                                                                                         }
                                                                                                         break;
                }                                                                                }
        }                                                                                }
                                                                                         vrLockReadRelease(wd->lock);
 
        selected_obj = wd->selected_object;                                   
 
        vrLockReadRelease(wd->lock);                                                     /*************************************************************/
                                                                                         /* perform type-appropriate actions for the contacted object */
 
        if (selected_obj != touch_obj) {                                                 /* NOTE: ideally we would use write-locks specific to each */
                /* NOTE: a local variable (touch_obj) is used because it */              /*   object, and only where that object is being changed.  */
                /*   decreases the time the lock must be held.           */              vrLockWriteSet(wd->lock);       /* lock while changing an object's lo
                vrLockWriteSet(wd->lock);                                                wd->last_contacted_object = wd->contacted_object;
                wd->selected_object = touch_obj;                                         wd->contacted_object = touch_obj;
                vrLockWriteRelease(wd->lock);                                            if (wd->contacted_object != -1) {
                                                                                                 object = &(wd->object[wd->contacted_object]);
                                                                                                 size = object->size;
 
                selected_obj = touch_obj;                                                        switch (object->type) {
        }                                                                                        case vw_normal: /* perform manipulation on normal object */
                                                                                                         /* when left wand button is pressed, a contacted VW o
                                                                                                         if (vrGet2switchValue(1)) {
 
        /* when left wand button is pressed, the selected object is moved */                                     object->x += (wand_vw_location[VR_X] - last_w
        if (vrGet2switchValue(1) && (selected_obj != -1)) {                                                      object->y += (wand_vw_location[VR_Y] - last_w
                vrLockWriteSet(wd->lock);                                                                        object->z += (wand_vw_location[VR_Z] - last_w
                object = &(wd->object[selected_obj]);                                                    }
                object->x += (wand_vw_location[VR_X] - last_wand_vw_loc[VR_X]                            break;
                object->y += (wand_vw_location[VR_Y] - last_wand_vw_loc[VR_Y]   
                object->z += (wand_vw_location[VR_Z] - last_wand_vw_loc[VR_Z]                    case ui_button: /* perform manipulation on button widget */
                vrLockWriteRelease(wd->lock);                                                            /* this code only needed for direct-control of button
                                                                                
                                                                                                         /* CONTACT-trigger: when we begin to touch a button, 
                                                                                                         if (wd->contacted_object != wd->last_contacted_object
                                                                                                                 object->ui_button_value ^= 1;
                                                                                                         }
                                                                                                         break;
                                                                                                 }
        }                                                                                }
                                                                                         vrLockWriteRelease(wd->lock);
 
        /******************************************************************/             /******************************************************************/
        /* store the current wand location for next time through the loop */             /* store the current wand location for next time through the loop */
        last_wand_vw_loc[VR_X] = wand_vw_location[VR_X];                                 last_wand_vw_loc[VR_X] = wand_vw_location[VR_X];
        last_wand_vw_loc[VR_Y] = wand_vw_location[VR_Y];                                 last_wand_vw_loc[VR_Y] = wand_vw_location[VR_Y];
        last_wand_vw_loc[VR_Z] = wand_vw_location[VR_Z];                                 last_wand_vw_loc[VR_Z] = wand_vw_location[VR_Z];
 
 
        /****************************/                                                   /************************************************/
        /** Handle Travel via wand **/                                                   /** Handle Travel via wand and virtual devices **/
        /****************************/                                                   /************************************************/
 
        /* pressing the right wand button resets us to the initial position *            vrLockReadSet(wd->lock);        /* lock while getting the UI values *
        if (vrGet2switchValue(3)) {                                                      button_value = wd->object[3].ui_button_value;
                vrUserTravelReset(VR_ALLUSERS);                                          vrLockReadRelease(wd->lock);
        }                                                                     
 
        /* use wand joystick to fly through world */                                     /* only travel if UI-button is on */
        joy_x = vrGetValuatorValue(0);                                                   if (button_value) {
        joy_y = vrGetValuatorValue(1);                                                           /* pressing the right wand button resets us to the initial po
                                                                                                 if (vrGet2switchValue(3)) {
                                                                                                         vrUserTravelReset(VR_ALLUSERS);
                                                                                                 }
 
        if (fabs(joy_x) > JS_EPSILON)                                                            /* use wand joystick to fly through world */
                vrUserTravelRotateId(VR_ALLUSERS, VR_Y, (delta_time * SUB_JS_                    joy_x = vrGetValuatorValue(0);
                                                                                                 joy_y = vrGetValuatorValue(1);
 
        if (fabs(joy_y) > JS_EPSILON) {                                                          if (fabs(joy_x) > JS_EPSILON)
                delta_move = delta_time * SUB_JS_EPSILON(joy_y) * MOVE_FACTOR                            vrUserTravelRotateId(VR_ALLUSERS, VR_Y, (delta_time *
                vrVectorGetRWFrom6sensorDir(&wand_rw_pointvec, WAND_SENSOR, V   
                vrUserTravelTranslate3d(VR_ALLUSERS,                                             if (fabs(joy_y) > JS_EPSILON) {
                        wand_rw_pointvec.v[VR_X] * delta_move,                                           delta_move = delta_time * SUB_JS_EPSILON(joy_y) * MOV
                        wand_rw_pointvec.v[VR_Y] * delta_move,                                           vrVectorGetRWFrom6sensorDir(&wand_rw_pointvec, WAND_S
                        wand_rw_pointvec.v[VR_Z] * delta_move);                                          vrUserTravelTranslate3d(VR_ALLUSERS,
                                                                                                                 wand_rw_pointvec.v[VR_X] * delta_move,
                                                                                                                 wand_rw_pointvec.v[VR_Y] * delta_move,
                                                                                                                 wand_rw_pointvec.v[VR_Z] * delta_move);
                                                                                                 }
        }                                                                                }
 
 
                                                                                 #ifdef USE_SOCKETS /* { */
        /*******************************/                                                /*******************************/
        /* Handle Commands from socket */                                                /* Handle Commands from socket */
 
        /* if no connection (and a listen socket is open), see if someone is             /* if no connection (and a listen socket is open), see if someone is 
        if (cmd_socket < 0 && listen_sock >= 0) {                                        if (cmd_socket < 0 && listen_sock >= 0) {
                /* cmd_socket will be -1 when no one is connected */                             /* cmd_socket will be -1 when no one is connected */
                cmd_socket = vrSocketAnswer(listen_sock);                                        cmd_socket = vrSocketAnswer(listen_sock);
                if (cmd_socket > 0) {                                                            if (cmd_socket > 0) {
                        printf("New client connected.\n");                                               printf("New client connected.\n");
                        vrSocketSendMsg(cmd_socket, "Your wish is my command                             vrSocketSendMsg(cmd_socket, "Your wish is my command 
                }                                                                                }
        }                                                                                }
 
        /* if a connection, see if client sent a command.  If so, act on it *            /* if a connection, see if client sent a command.  If so, act on it *
        if (cmd_socket > 0) {                                                            if (cmd_socket > 0) {
                result = vrSocketReadMsgClean(cmd_socket, buf, 128);                             result = vrSocketReadMsgClean(cmd_socket, buf, 128);
                switch (result) {                                                                switch (result) {
                case -1:                                                                         case -1:
                        break;                                                                           break;
                case  0:                                                                         case  0:
                        printf("Client stopped communicating.\n");                                       printf("Client stopped communicating.\n");
                        close(cmd_socket);                                                               close(cmd_socket);
                        cmd_socket = -1;                                                                 cmd_socket = -1;
                        break;                                                                           break;
                default:                                                                         default:
                        printf("socket cmd = '%s'\n", buf);                                              printf("socket cmd = '%s'\n", buf);
                        /* parse command here -- very simple-minded parsing *                            /* parse command here -- very simple-minded parsing *
                        if (strcmp(buf, "quit") == 0) {                                                  if (strcmp(buf, "quit") == 0) {
                                /* set the world for termination */                                              /* set the world for termination */
                                wd->quit_bit = 1;                                                                wd->quit_bit = 1;
                        } else                                                                           } else
                        if (strcmp(buf, "help") == 0 || strcmp(buf, "?") == 0                            if (strcmp(buf, "help") == 0 || strcmp(buf, "?") == 0
                                /* give usage information */                                                     /* give usage information */
                                vrSocketSendMsg(cmd_socket, help_msg);                                           vrSocketSendMsg(cmd_socket, help_msg);
                        } else                                                                           } else
                        if (strcmp(buf, "close") == 0) {                                                 if (strcmp(buf, "close") == 0) {
                                /* close socket connection */                                                    /* close socket connection */
                                printf("Client ending communication.\n");                                        printf("Client ending communication.\n");
                                close(cmd_socket);                                                               close(cmd_socket);
                                cmd_socket = -1;                                                                 cmd_socket = -1;
                        } else                                                                           } else
                        if (strcmp(buf, "reset") == 0) {                                                 if (strcmp(buf, "reset") == 0) {
                                /* make the world as it was in the beginning                                     /* make the world as it was in the beginning 
                                init_objects(wd);                                                                init_objects(wd);
                                vrUserTravelReset(VR_ALLUSERS);                                                  vrUserTravelReset(VR_ALLUSERS);
                        } else                                                                           } else
                                                                                                         if (strcmp(buf, "button?") == 0) {
                                                                                                                 /* report the user-interface button value */
                                                                                                                 sprintf(msg, "The button is %s.\n", (wd->obje
                                                                                                                 vrSocketSendMsg(cmd_socket, msg);
                                                                                                         } else
                                                                                                         if (strcmp(buf, "button on") == 0) {
                                                                                                                 /* turn the user-interface button on */
                                                                                                                 wd->object[3].ui_button_value = 1;
                                                                                                         } else
                                                                                                         if (strcmp(buf, "button off") == 0) {
                                                                                                                 /* turn the user-interface button on */
                                                                                                                 wd->object[3].ui_button_value = 0;
                                                                                                         } else
                        if (strcmp(buf, "move cube up") == 0) {                                          if (strcmp(buf, "move cube up") == 0) {
                                /* move the blue cube up 2.0 units */                                            /* move the blue cube up 2.0 units */
                                wd->object[0].y += 2.0;                                                          wd->object[0].y += 2.0;
                        } else                                                                           } else
                        if (strcmp(buf, "move cube down") == 0) {                                        if (strcmp(buf, "move cube down") == 0) {
                                /* move the blue cube down 2.0 units */                                          /* move the blue cube down 2.0 units */
                                wd->object[0].y -= 2.0;                                                          wd->object[0].y -= 2.0;
                        } else                                                                           } else
                        if (strcmp(buf, "where") == 0 || strcmp(buf, "where a                            if (strcmp(buf, "where") == 0 || strcmp(buf, "where a
                                /* report the user's location */                                                 /* report the user's location */
                                vrMatrix        *headmat;                                                        vrMatrix        *headmat;
                                vrPoint         headloc;                                                         vrPoint         headloc;
 
                                headmat = vrMatrixCreate();                                                      headmat = vrMatrixCreate();
                                vrMatrixGetRWFromUserHead(headmat, 0);                                           vrMatrixGetRWFromUserHead(headmat, 0);
                                vrPointGetVWFromUserMatrix(&headloc, 0, headm                                    vrPointGetVWFromUserMatrix(&headloc, 0, headm
                                sprintf(msg, "You are at (%f %f %f).\n", head                                    sprintf(msg, "You are at (%f %f %f).\n", head
                                vrSocketSendMsg(cmd_socket, msg);                                                vrSocketSendMsg(cmd_socket, msg);
                        } else                                                                           } else
                        if (strcmp(buf, "show ActiveWalls") == 0) {                                      if (strcmp(buf, "show ActiveWalls") == 0) {
                                /* report the type of graphics (VR) display *                                    /* report the type of graphics (VR) display *
                                sprintf(msg, "Application has %d active walls                                    sprintf(msg, "Application has %d active walls
                                vrSocketSendMsg(cmd_socket, msg);                                                vrSocketSendMsg(cmd_socket, msg);
                        } else                                                                           } else
                        /* default: */ {                                                                 /* default: */ {
                                vrSocketSendMsg(cmd_socket, "Unknown command.                                    vrSocketSendMsg(cmd_socket, "Unknown command.
                        }                                                                                }
 
                        /* prompt for another command (if socket still open)                             /* prompt for another command (if socket still open) 
                        if (cmd_socket > 0) {                                                            if (cmd_socket > 0) {
                                vrSocketSendMsg(cmd_socket, "Your wish is my                                     vrSocketSendMsg(cmd_socket, "Your wish is my 
                        }                                                                                }
                }                                                                                }
        }                                                                                }
                                                                                 #endif /* } USE_SOCKETS */
 
        /*********************************/                                              /*********************************/
        /* Handle Commands from keyboard */                                              /* Handle Commands from keyboard */
        if (vrGet2switchValue(0)) {                                                      if (vrGet2switchValue(0)) {
                wd->quit_bit = 1;                                                                wd->quit_bit = 1;
        }                                                                                }
 
        /****************************************************/                           /****************************************************/
        /* yield to allow other processes access to the CPU */                           /* yield to allow other processes access to the CPU */
        vrSleep(0);                                                                      vrSleep(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 wrs_shapes.c */
void    draw_floor();                                                            void    draw_floor();
void    draw_cube();                                                             void    draw_cube();
void    draw_pyramid();                                                          void    draw_pyramid();
void    draw_cube_outline();                                                     void    draw_cube_outline();
void    draw_pyramid_outline();                                                  void    draw_pyramid_outline();
                                                                                 void    draw_box_outlineonly(float x2, float x1, float y2, float y1, float z2
 
 
/*********************************************************************/          /*********************************************************************/
/* init_gfx(): initialize general graphics properties (eg. lighting) */          /* init_gfx(): initialize general graphics properties (eg. lighting) */
/*********************************************************************/          /*********************************************************************/
void init_gfx()                                                                  void init_gfx()
{                                                                                {
        GLfloat location_0[4] = { 0.0, 10.0, 10.0, 1.0 /* 0 = directional, 1             GLfloat location_0[4] = { 0.0, 10.0, 10.0, 1.0 /* 0 = directional, 1 
                ambient_0[4] = { 0.2, 0.2, 0.2, 1.0 },                                           ambient_0[4] = { 0.2, 0.2, 0.2, 1.0 },
                diffuse_0[4] = { 0.8, 0.8, 0.8, 1.0 },                                           diffuse_0[4] = { 0.8, 0.8, 0.8, 1.0 },
                specular_0[4] = { 0.2, 0.2, 0.2, 1.0 };                                          specular_0[4] = { 0.2, 0.2, 0.2, 1.0 };
 
        /****************************************/                                       /****************************************/
        /* set the render initialization values */                                       /* set the render initialization values */
        glClearColor(0.0, 0.0, 0.0, 0.0);                                                glClearColor(0.0, 0.0, 0.0, 0.0);
        glClearDepth(1.0);                                                               glClearDepth(1.0);
 
        /********************************************************/                       /********************************************************/
        /* set the polygon shading/lighting/material parameters */                       /* set the polygon shading/lighting/material parameters */
        glShadeModel(GL_SMOOTH);                                                         glShadeModel(GL_SMOOTH);
 
        glEnable(GL_LIGHTING);                                                           glEnable(GL_LIGHTING);
 
        glLightfv(GL_LIGHT0, GL_POSITION, location_0);                                   glLightfv(GL_LIGHT0, GL_POSITION, location_0);
        glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_0);                                     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_0);
        glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse_0);                                     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse_0);
        glLightfv(GL_LIGHT0, GL_SPECULAR, specular_0);                                   glLightfv(GL_LIGHT0, GL_SPECULAR, specular_0);
        glEnable(GL_LIGHT0);                                                             glEnable(GL_LIGHT0);
 
        /* These two lines cause the vertex colors to be used (otherwise all             /* These two lines cause the vertex colors to be used (otherwise all 
        glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);                               glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
        glEnable(GL_COLOR_MATERIAL);                                                     glEnable(GL_COLOR_MATERIAL);
 
        glEnable(GL_NORMALIZE); /* this is required when calling glScalef() w            glEnable(GL_NORMALIZE); /* this is required when calling glScalef() w
 
        /***************************/                                                    /***************************/
        /* set the line parameters */                                                    /* set the line parameters */
        glEnable(GL_LINE_SMOOTH);                                                        glEnable(GL_LINE_SMOOTH);
        glLineWidth(2.0);                                                                glLineWidth(2.0);
}                                                                                }
 
 
/*******************************************************************/            /*******************************************************************/
/* draw_world(): Now will render how ever many objects are in the  */            /* draw_world(): Now will render how ever many objects are in the  */
/*   world data structure.  The color, type (and maybe rotation)   */            /*   world data structure.  The color, type (and maybe rotation)   */
/*   are all now stored for each object, in addition to location.  */            /*   are all now stored for each object, in addition to location.  */
/*   The currently selected object is highlighted by adding a      */         
/*   wireframe outline to the representation.                      */         
/*******************************************************************/            /*******************************************************************/
void draw_world(WorldDataType *wd, vrRenderInfo *rendinfo)                       void draw_world(WorldDataType *wd, vrRenderInfo *rendinfo)
{                                                                                {
static  GLfloat         location_0[4] = { 0.0, 10.0, 0.0, 1.0 /* 0 = directio    static  GLfloat         location_0[4] = { 0.0, 10.0, 0.0, 1.0 /* 0 = directio
static  char            msg[128];                       /* memory for text st    static  char            msg[128];                       /* memory for text st
        int             objnum;                         /* object counter */             int             objnum;                         /* object counter */
        ObjectType      *object;                        /* pointer to current            ObjectType      *object;                        /* pointer to current
                                                                                         float           size;                           /* size of current ob
 
        /**************************************/                                         /**************************************/
        /* clear the screen -- very important */                                         /* clear the screen -- very important */
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);                              glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
        /********************************************************************            /********************************************************************
        /* update the light's location -- must be done independently for each            /* update the light's location -- must be done independently for each
        glLightfv(GL_LIGHT0, GL_POSITION, location_0);                                   glLightfv(GL_LIGHT0, GL_POSITION, location_0);
 
        /************************************/                                           /************************************/
        /* draw a short pointer at the wand */                                           /* draw a short pointer at the wand */
        glPushMatrix();                                                                  glPushMatrix();
                glColor3ub(100, 255, 255);      /* cyan */                                       glColor3ub(100, 255, 255);      /* cyan */
                vrRenderTransform6sensor(rendinfo, WAND_SENSOR);                                 vrRenderTransform6sensor(rendinfo, WAND_SENSOR);
                glRotatef(-90.0, 1.0, 0.0, 0.0);                                                 glRotatef(-90.0, 1.0, 0.0, 0.0);
                glScalef(0.15, 0.15, 0.15);                                                      glScalef(0.15, 0.15, 0.15);
                glTranslatef(0.0, 1.5, 0.0);                                                     glTranslatef(0.0, -1.0, 0.0);   /* move the tip to (0,0,0) */
                draw_pyramid_outline();                                                          draw_pyramid_outline();
        glPopMatrix();                                                                   glPopMatrix();
 
        /*******************************************************/                        /********************************************************/
        /* draw an object that remains fixed in physical space */                        /* draw all the objects, taking care to put each one in */
        glColor3ub(200, 100, 255);      /* purple */                                     /*   the proper coordinate system (virtual world space  */
                                                                                         /*   vs. real world space for the user-interface).      */
                                                                                
                                                                                         /* draw the floor */
        glPushMatrix();                                                                  glPushMatrix();
                glTranslatef(-4.0, 1.0, -1.0);                                                   vrRenderTransformUserTravel(rendinfo);  /* change to virtual-
                draw_cube();                                                                     draw_floor();
        glPopMatrix();                                                                   glPopMatrix();
 
        /******************************************************************/  
        /* draw the rest of the objects in virtual (user navigated) space */  
        glPushMatrix();         /* save the real-world coordinate system */   
        vrRenderTransformUserTravel(rendinfo);  /* change to virtual-world co 
                                                                              
        draw_floor();                                                         
                                                                              
        vrLockReadSet(wd->lock);        /* lock while reading info about obje            vrLockReadSet(wd->lock);        /* lock while reading info about obje
        for (objnum = 0; objnum < wd->number_of_objects; objnum++) {                     for (objnum = 0; objnum < wd->number_of_objects; objnum++) {
                object = &(wd->object[objnum]);                                                  object = &(wd->object[objnum]);
 
                glColor3ubv(object->color);                                                      glColor3ubv(object->color);
                glPushMatrix();                                                                  glPushMatrix();
                        glTranslatef(object->x, object->y, object->z);                                   size = object->size;
                        switch (object->shape) {                                
                        case cube:                                                                       switch (object->type) {
                                if (objnum == wd->selected_object)              
                                        draw_cube_outline();                                             case vw_normal: /* render normal object (in virtual w
                                else    draw_cube();                                                             vrRenderTransformUserTravel(rendinfo);  /* ch
                                break;                                                                           glTranslatef(object->x, object->y, object->z)
                        case pyramid:                                                                            glScalef(size, size, size);
                                if (objnum == wd->selected_object)              
                                        draw_pyramid_outline();                                                  switch (object->shape) {
                                else    draw_pyramid();                                                          case cube:
                                                                                                                         if (objnum == wd->contacted_object)
                                                                                                                                 draw_cube_outline();
                                                                                                                         else    draw_cube();
                                                                                                                         break;
                                                                                                                 case sphere:
                                                                                                                         if (objnum == wd->contacted_object)
                                                                                                                                 draw_sphere_outline();
                                                                                                                         else    draw_sphere();
                                                                                                                         break;
                                                                                                                 case pyramid:
                                                                                                                         if (objnum == wd->contacted_object)
                                                                                                                                 draw_pyramid_outline();
                                                                                                                         else    draw_pyramid();
                                                                                                                         break;
                                                                                                                 case ud_pyramid:
                                                                                                                         glScalef(1.0, -1.0, 1.0);
                                                                                                                         if (objnum == wd->contacted_object)
                                                                                                                                 draw_pyramid_outline();
                                                                                                                         else    draw_pyramid();
                                                                                                                         break;
                                                                                                                 }
                                break;                                                                           break;
                        case ud_pyramid:                                        
                                glScalef(1.0, -1.0, 1.0);                                                case ui_button: /* render button widget (in real worl
                                if (objnum == wd->selected_object)                                               /* widgets remain in real-world coordinates *
                                        draw_pyramid_outline();                                                  glTranslatef(object->x, object->y, object->z)
                                else    draw_pyramid();                          #ifdef DRAW_UITEXT /* { */
                                                                                                                 glPushMatrix();
                                                                                                                         glTranslatef(0.0, -(size + 0.5), 0.0)
                                                                                
                                                                                                                         glRasterPos3f(0.0, 0.0, 0.0);   /* go
                                                                                                                         if (object->ui_button_value)
                                                                                                                                 vrRenderText(rendinfo, "on");
                                                                                                                         else    vrRenderText(rendinfo, "off")
                                                                                                                 glPopMatrix();
                                                                                 #endif /* } DRAW_UITEXT */
                                                                                                                 glScalef(size, size, size);
                                                                                                                 glPushMatrix();
                                                                                
                                                                                                                         /* for now we'll ignore shape and all
                                                                                
                                                                                                                         /* draw the base box */
                                                                                                                         if (objnum == wd->contacted_object)
                                                                                                                                 draw_cube_outline();
                                                                                                                         else    draw_cube();
                                                                                
                                                                                                                         /* draw the button part */
                                                                                                                         if (object->ui_button_value) {
                                                                                                                                 /* button is pressed (so bare
                                                                                
                                                                                                                                 glTranslatef(0.4, 0.0, 0.0);
                                                                                                                                 /* move color halfway to whit
                                                                                                                                 glColor3ub(     
                                                                                                                                         object->color[0] + ((
                                                                                                                                         object->color[1] + ((
                                                                                                                                         object->color[2] + ((
                                                                                                                         } else {
                                                                                                                                 /* button is not pressed (so 
                                                                                
                                                                                                                                 glTranslatef(0.8, 0.0, 0.0);
                                                                                                                                 /* move color halfway to blac
                                                                                                                                 glColor3ub(object->color[0]/2
                                                                                                                         }
                                                                                                                         glScalef(0.7, 0.7, 0.7);
                                                                                                                         draw_cube_outline();
                                                                                                                 glPopMatrix();
                                                                                
                                break;                                                                           break;
                        }                                                                                }
                glPopMatrix();                                                                   glPopMatrix();
        }                                                                                }
                                                                              
        glPopMatrix();  /* return to real-world coordinates */                
        /* NOTE: 2-D text is best rendered in a fixed location in the world *            /* NOTE: 2-D text is best rendered in a fixed location in the world *
 
        /*************************************************/                              /*************************************************/
        /* draw text indicating which object is selected */                              /* draw text indicating which object is contacted */
        if (wd->selected_object < 0) {                                                   if (wd->contacted_object < 0) {
                sprintf(msg, "Object selected: none");                                           sprintf(msg, "Object contacted: none");
        } else {                                                                         } else {
                sprintf(msg, "Object selected: %d", wd->selected_object);                        sprintf(msg, "Object contacted: %d", wd->contacted_object);
        }                                                                                }
 
        vrLockReadRelease(wd->lock);                                                     vrLockReadRelease(wd->lock);
 
        glDisable(GL_LIGHTING);         /* make the text bright regardless of            glDisable(GL_LIGHTING);         /* make the text bright regardless of
        glColor3ub(255, 255, 255);      /* white */                                      glColor3ub(255, 255, 255);      /* white */
        glPushMatrix();                                                                  glPushMatrix();
                glTranslatef(-4.0, 7.0, -5.0);  /* on the front screen */                        glTranslatef(-4.0, 7.0, -5.0);  /* on the front screen */
 
                glRasterPos3f(0.0, 0.0, 0.0);   /* go to the 3D location */                      glRasterPos3f(0.0, 0.0, 0.0);   /* go to the 3D location */
                vrRenderText(rendinfo, msg);                                                     vrRenderText(rendinfo, msg);
        glPopMatrix();                                                                   glPopMatrix();
        glEnable(GL_LIGHTING);                                                           glEnable(GL_LIGHTING);
}                                                                                }